toolBar.py 1.7 KB

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