color_dialog.py 1.2 KB

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