colorDialog.py 2.0 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. from PyQt4.QtWidgets import *
  27. BB = QDialogButtonBox
  28. class ColorDialog(QColorDialog):
  29. def __init__(self, parent=None):
  30. super(ColorDialog, self).__init__(parent)
  31. self.setOption(QColorDialog.ShowAlphaChannel)
  32. # The Mac native dialog does not support our restore button.
  33. self.setOption(QColorDialog.DontUseNativeDialog)
  34. ## Add a restore defaults button.
  35. # The default is set at invocation time, so that it
  36. # works across dialogs for different elements.
  37. self.default = None
  38. self.bb = self.layout().itemAt(1).widget()
  39. self.bb.addButton(BB.RestoreDefaults)
  40. self.bb.clicked.connect(self.checkRestore)
  41. def getColor(self, value=None, title=None, default=None):
  42. self.default = default
  43. if title:
  44. self.setWindowTitle(title)
  45. if value:
  46. self.setCurrentColor(value)
  47. return self.currentColor() if self.exec_() else None
  48. def checkRestore(self, button):
  49. if self.bb.buttonRole(button) & BB.ResetRole and self.default:
  50. self.setCurrentColor(self.default)