colorDialog.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. try:
  2. from PyQt5 import QtWidgets
  3. except ImportError:
  4. from PyQt4 import QtGui as QtWidgets
  5. class ColorDialog(QtWidgets.QColorDialog):
  6. def __init__(self, parent=None):
  7. super(ColorDialog, self).__init__(parent)
  8. self.setOption(QtWidgets.QColorDialog.ShowAlphaChannel)
  9. # The Mac native dialog does not support our restore button.
  10. self.setOption(QtWidgets.QColorDialog.DontUseNativeDialog)
  11. # Add a restore defaults button.
  12. # The default is set at invocation time, so that it
  13. # works across dialogs for different elements.
  14. self.default = None
  15. self.bb = self.layout().itemAt(1).widget()
  16. self.bb.addButton(QtWidgets.QDialogButtonBox.RestoreDefaults)
  17. self.bb.clicked.connect(self.checkRestore)
  18. def getColor(self, value=None, title=None, default=None):
  19. self.default = default
  20. if title:
  21. self.setWindowTitle(title)
  22. if value:
  23. self.setCurrentColor(value)
  24. return self.currentColor() if self.exec_() else None
  25. def checkRestore(self, button):
  26. if self.bb.buttonRole(button) & \
  27. QtWidgets.QDialogButtonBox.ResetRole and self.default:
  28. self.setCurrentColor(self.default)