toolBar.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #
  2. # Copyright (C) 2011 Michael Pitidis, Hussein Abdulwahid.
  3. #
  4. # This file is part of Labelme.
  5. #
  6. # Labelme is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Labelme is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Labelme. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. try:
  20. from PyQt5 import QtCore
  21. from PyQt5 import QtWidgets
  22. except ImportError:
  23. from PyQt4 import QtCore
  24. from PyQt4 import QtGui as QtWidgets
  25. class ToolBar(QtWidgets.QToolBar):
  26. def __init__(self, title):
  27. super(ToolBar, self).__init__(title)
  28. layout = self.layout()
  29. m = (0, 0, 0, 0)
  30. layout.setSpacing(0)
  31. layout.setContentsMargins(*m)
  32. self.setContentsMargins(*m)
  33. self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
  34. def addAction(self, action):
  35. if isinstance(action, QtWidgets.QWidgetAction):
  36. return super(ToolBar, self).addAction(action)
  37. btn = ToolButton()
  38. btn.setDefaultAction(action)
  39. btn.setToolButtonStyle(self.toolButtonStyle())
  40. self.addWidget(btn)
  41. class ToolButton(QtWidgets.QToolButton):
  42. """ToolBar companion class which ensures all buttons have the same size."""
  43. minSize = (60, 60)
  44. def minimumSizeHint(self):
  45. ms = super(ToolButton, self).minimumSizeHint()
  46. w1, h1 = ms.width(), ms.height()
  47. w2, h2 = self.minSize
  48. ToolButton.minSize = max(w1, w2), max(h1, h2)
  49. return QtCore.QSize(*ToolButton.minSize)