diff options
Diffstat (limited to 'todo/util/gui/color_input.py')
-rw-r--r-- | todo/util/gui/color_input.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/todo/util/gui/color_input.py b/todo/util/gui/color_input.py new file mode 100644 index 0000000..032aea1 --- /dev/null +++ b/todo/util/gui/color_input.py @@ -0,0 +1,31 @@ +from PyQt5 import QtWidgets, QtCore, QtGui + +class ColorInput(QtWidgets.QLineEdit): + + def __init__(self, init_color: QtGui.QColor, parent): + super().__init__(parent) + self.setReadOnly(True) + self.installEventFilter(self) + self._color = init_color + self.update(init_color) + self._is_editing = False + + def eventFilter(self, source, event): + if source is self and event.type() == QtCore.QEvent.FocusIn: + if not self._is_editing: + self._is_editing = True + color = QtWidgets.QColorDialog.getColor(self._color, self) + if color.isValid(): + self.update(color) + else: + self._is_editing = False + self.clearFocus() + return super(ColorInput, self).eventFilter(source, event) + + def update(self, color: QtGui.QColor): + self._color = color + self.setText(color.name().upper()) + palette = QtGui.QPalette() + palette.setColor(QtGui.QPalette.Base, color) + palette.setColor(QtGui.QPalette.Text, color) + self.setPalette(palette) |