toolBar.py 1.9 KB

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