blob: 032aea1603151757dbc4e3525a777fdcd5e7e1ae (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)
|