toolBar.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.QtGui import *
  21. from PyQt5.QtCore import *
  22. from PyQt5.QtWidgets import *
  23. except ImportError:
  24. from PyQt4.QtGui import *
  25. from PyQt4.QtCore import *
  26. class ToolBar(QToolBar):
  27. def __init__(self, title):
  28. super(ToolBar, self).__init__(title)
  29. layout = self.layout()
  30. m = (0, 0, 0, 0)
  31. layout.setSpacing(0)
  32. layout.setContentsMargins(*m)
  33. self.setContentsMargins(*m)
  34. self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
  35. def addAction(self, action):
  36. if isinstance(action, QWidgetAction):
  37. return super(ToolBar, self).addAction(action)
  38. btn = ToolButton()
  39. btn.setDefaultAction(action)
  40. btn.setToolButtonStyle(self.toolButtonStyle())
  41. self.addWidget(btn)
  42. class ToolButton(QToolButton):
  43. """ToolBar companion class which ensures all buttons have the same size."""
  44. minSize = (60, 60)
  45. def minimumSizeHint(self):
  46. ms = super(ToolButton, self).minimumSizeHint()
  47. w1, h1 = ms.width(), ms.height()
  48. w2, h2 = self.minSize
  49. ToolButton.minSize = max(w1, w2), max(h1, h2)
  50. return QSize(*ToolButton.minSize)