colorDialog.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 QColorDialog, QDialogButtonBox
  17. except ImportError:
  18. from PyQt4.QtGui import *
  19. from PyQt4.QtCore import *
  20. BB = QDialogButtonBox
  21. class ColorDialog(QColorDialog):
  22. def __init__(self, parent=None):
  23. super(ColorDialog, self).__init__(parent)
  24. self.setOption(QColorDialog.ShowAlphaChannel)
  25. # The Mac native dialog does not support our restore button.
  26. self.setOption(QColorDialog.DontUseNativeDialog)
  27. # Add a restore defaults button.
  28. # The default is set at invocation time, so that it
  29. # works across dialogs for different elements.
  30. self.default = None
  31. self.bb = self.layout().itemAt(1).widget()
  32. self.bb.addButton(BB.RestoreDefaults)
  33. self.bb.clicked.connect(self.checkRestore)
  34. def getColor(self, value=None, title=None, default=None):
  35. self.default = default
  36. if title:
  37. self.setWindowTitle(title)
  38. if value:
  39. self.setCurrentColor(value)
  40. return self.currentColor() if self.exec_() else None
  41. def checkRestore(self, button):
  42. if self.bb.buttonRole(button) & BB.ResetRole and self.default:
  43. self.setCurrentColor(self.default)