colorDialog.py 1.9 KB

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