toolBar.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (c) <2015-Present> Tzutalin
  2. # Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba,
  3. # William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. # associated documentation files (the "Software"), to deal in the Software without restriction, including without
  5. # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  6. # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  8. # the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  9. # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  10. # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  12. # THE SOFTWARE.
  13. try:
  14. from PyQt5.QtGui import *
  15. from PyQt5.QtCore import *
  16. from PyQt5.QtWidgets import *
  17. except ImportError:
  18. from PyQt4.QtGui import *
  19. from PyQt4.QtCore import *
  20. class ToolBar(QToolBar):
  21. def __init__(self, title):
  22. super(ToolBar, self).__init__(title)
  23. layout = self.layout()
  24. m = (0, 0, 0, 0)
  25. layout.setSpacing(0)
  26. layout.setContentsMargins(*m)
  27. self.setContentsMargins(*m)
  28. self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
  29. def addAction(self, action):
  30. if isinstance(action, QWidgetAction):
  31. return super(ToolBar, self).addAction(action)
  32. btn = ToolButton()
  33. btn.setDefaultAction(action)
  34. btn.setToolButtonStyle(self.toolButtonStyle())
  35. self.addWidget(btn)
  36. class ToolButton(QToolButton):
  37. """ToolBar companion class which ensures all buttons have the same size."""
  38. minSize = (60, 60)
  39. def minimumSizeHint(self):
  40. ms = super(ToolButton, self).minimumSizeHint()
  41. w1, h1 = ms.width(), ms.height()
  42. w2, h2 = self.minSize
  43. ToolButton.minSize = max(w1, w2), max(h1, h2)
  44. return QSize(*ToolButton.minSize)