-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor_swatch.py
More file actions
105 lines (69 loc) · 2.92 KB
/
color_swatch.py
File metadata and controls
105 lines (69 loc) · 2.92 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from PyQt6.QtCore import Qt, QObject, QPointF, pyqtSignal
from PyQt6.QtGui import QColor
from color_swatch_item import ColorSwatchItem
class ColorSwatch(QObject):
color_swatches = list()
position_changed = pyqtSignal(QPointF)
anchor_changed = pyqtSignal(QPointF)
color_changed = pyqtSignal(QColor)
active_changed = pyqtSignal(bool)
def __init__(self, project, anchor_alignment : Qt.AlignmentFlag):
super().__init__()
ColorSwatch.color_swatches.append(self)
self.project = project
self.anchor_alignment = anchor_alignment
self.position = QPointF()
self.anchor = QPointF()
self.color = QColor(25, 25, 25)
self.active = False
self.color_sample_link = None
self.color_swatch_item = ColorSwatchItem(self)
def __del__(self):
"""Remove color swatch from tracking when deleted."""
if self in ColorSwatch.color_swatches:
ColorSwatch.color_swatches.remove(self)
def set_position(self, position : QPointF):
"""Set position in scene coordinates."""
if position is self.position:
return
self.position = position
self.position_changed.emit(self.position)
anchor_offset = QPointF()
half_size = ColorSwatchItem.swatch_size / 2
if self.anchor_alignment == Qt.AlignmentFlag.AlignLeft:
anchor_offset = QPointF(-half_size, 0)
if self.anchor_alignment == Qt.AlignmentFlag.AlignRight:
anchor_offset = QPointF(half_size, 0)
if self.anchor_alignment == Qt.AlignmentFlag.AlignTop:
anchor_offset = QPointF(0, -half_size)
if self.anchor_alignment == Qt.AlignmentFlag.AlignBottom:
anchor_offset = QPointF(0, half_size)
self.set_anchor(self.position + anchor_offset)
def set_anchor(self, anchor : QPointF):
"""Set anchor in scene coordinates."""
if anchor is self.anchor:
return
self.anchor = anchor
self.anchor_changed.emit(self.anchor)
def activate(self, color_sample_link):
"""Activate the color swatch."""
self.color_sample_link = color_sample_link
if not self.active:
self.active = True
self.active_changed.emit(self.active)
def deactivate(self):
"""De-activate the color swatch."""
self.color_sample_link = None
if self.active:
self.active = False
self.active_changed.emit(self.active)
self.reset_color()
def set_color(self, color : QColor):
"""Set color."""
if color is self.color:
return
self.color = color
self.color_changed.emit(self.color)
def reset_color(self):
"""Reset color to default."""
self.set_color(QColor(25, 25, 25))