aboutsummaryrefslogtreecommitdiffstats
path: root/Lib/defconQt/objects/colorWidgets.py
blob: f8b401029a16fc1fd2c684eb3318c8ce98f522db (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from PyQt5.QtCore import pyqtSignal, QSize, Qt
from PyQt5.QtWidgets import (QColorDialog, QStyle, QStyleOptionFrame,
                             QStylePainter, QWidget)


class ColorVignette(QWidget):
    """
    A widget that presents a color in the form of a vignette. Opens up a color
    picker dialog upon double click unless readOnly is set to True.

    Inspired by ColorPreview and ColorSelector, by Mattia Basaglia.
    """

    colorChanged = pyqtSignal()

    def __init__(self, color, parent=None):
        super().__init__(parent)
        self._color = color
        self._margins = (0, 2, 0, -2)
        self._readOnly = False

    def color(self):
        return self._color

    def setColor(self, color):
        self._color = color
        self.update()

    def mouseDoubleClickEvent(self, event):
        event.accept()
        if self._readOnly:
            return
        dialog = QColorDialog()
        ok = dialog.exec_()
        if ok:
            self.setColor(dialog.currentColor())
            self.colorChanged.emit()

    def margins(self):
        dx1, dy1, dx2, dy2 = self._margins
        return (dx1, dy1, -dx2, -dy2)

    def setMargins(self, left, top, right, bottom):
        self._margins = (left, top, -right, -bottom)

    def readOnly(self):
        return self._readOnly

    def setReadOnly(self, value):
        self._readOnly = value

    def paint(self, painter, rect):
        panel = QStyleOptionFrame()
        panel.initFrom(self)
        panel.lineWidth = 2
        panel.midLineWidth = 0
        panel.rect = panel.rect.adjusted(*self._margins)
        panel.state = panel.state | QStyle.State_Sunken
        self.style().drawPrimitive(QStyle.PE_Frame, panel, painter, self)
        r = self.style().subElementRect(QStyle.SE_FrameContents, panel, self)
        painter.fillRect(r, Qt.white)
        painter.fillRect(r.adjusted(2, 2, -2, -2), self._color)

    def paintEvent(self, event):
        painter = QStylePainter(self)
        self.paint(painter, self.geometry())

    def resizeEvent(self, event):
        self.update()

    def sizeHint(self):
        return QSize(24, 24)