-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlabel.py
More file actions
287 lines (245 loc) · 9.17 KB
/
label.py
File metadata and controls
287 lines (245 loc) · 9.17 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# -*- coding: utf-8 -*-
from __future__ import annotations
from typing import TYPE_CHECKING
from guidata.dataset import (
BeginGroup,
BeginTabGroup,
BoolItem,
ChoiceItem,
ColorItem,
DataSet,
EndGroup,
EndTabGroup,
FloatItem,
GetAttrProp,
IntItem,
NotProp,
Obj,
StringItem,
TextItem,
update_dataset,
)
from qtpy import QtGui as QG
from plotpy.config import _
from plotpy.styles.base import FontItem, ItemParameters, LineStyleItem, SymbolItem
if TYPE_CHECKING:
from plotpy.items import LabelItem, LegendBoxItem
class LabelParam(DataSet):
"""Parameters for a label item."""
_multiselection = False
_legend = False
_no_contents = True
label = StringItem(_("Title"), default="").set_prop(
"display", hide=GetAttrProp("_multiselection")
)
_styles = BeginTabGroup("Styles")
# -------------------------------------------------------------- Contents tab
___cont = BeginGroup(_("Contents")).set_prop(
"display", icon="label.png", hide=GetAttrProp("_no_contents")
)
contents = TextItem("", default="").set_prop(
"display", hide=GetAttrProp("_no_contents")
)
___econt = EndGroup(_("Contents")).set_prop(
"display", hide=GetAttrProp("_no_contents")
)
# ---------------------------------------------------------------- Symbol tab
symbol = SymbolItem(_("Symbol")).set_prop(
"display", icon="diamond.png", hide=GetAttrProp("_legend")
)
# ---------------------------------------------------------------- Border tab
border = LineStyleItem(
_("Border"), default=Obj(color="#cbcbcb"), help=_("set width to 0 to disable")
).set_prop("display", icon="dashdot.png")
# ------------------------------------------------------------------ Text tab
___text = BeginGroup(_("Text")).set_prop("display", icon="font.png")
font = FontItem(_("Text font"))
color = ColorItem(_("Text color"), default="#000000")
bgcolor = ColorItem(_("Background color"), default="#ffffff")
bgalpha = FloatItem(_("Background transparency"), min=0.0, max=1.0, default=0.8)
___etext = EndGroup(_("Text"))
# -------------------------------------------------------------- Position tab
___position = BeginGroup(_("Position")).set_prop("display", icon="move.png")
_begin_anchor = BeginGroup(_("Position relative to anchor")).set_prop(
"display", hide=GetAttrProp("_multiselection")
)
anchor = ChoiceItem(
_("Corner"),
[
("TL", _("Top left")),
("TR", _("Top right")),
("BL", _("Bottom left")),
("BR", _("Bottom right")),
("L", _("Left")),
("R", _("Right")),
("T", _("Top")),
("B", _("Bottom")),
("C", _("Center")),
],
default="TL",
help=_("Label position relative to anchor point"),
).set_prop("display", hide=GetAttrProp("_multiselection"))
xc = IntItem(
_("ΔX"),
default=5,
help=_("Horizontal offset (pixels) relative to anchor point"),
).set_prop("display", hide=GetAttrProp("_multiselection"))
yc = (
IntItem(
_("ΔY"),
default=5,
help=_("Vertical offset (pixels) relative to anchor point"),
)
.set_pos(col=1)
.set_prop("display", hide=GetAttrProp("_multiselection"))
)
_end_anchor = EndGroup(_("Anchor")).set_prop(
"display", hide=GetAttrProp("_multiselection")
)
_begin_anchorpos = BeginGroup(_("Anchor position")).set_prop(
"display", hide=GetAttrProp("_multiselection")
)
_abspos_prop = GetAttrProp("abspos")
abspos = (
BoolItem(text=_("Attach to canvas"), label=_("Anchor"), default=True)
.set_prop("display", store=_abspos_prop)
.set_prop("display", hide=GetAttrProp("_multiselection"))
)
xg = (
FloatItem(_("X"), default=0.0, help=_("X-axis position in canvas coordinates"))
.set_prop("display", active=NotProp(_abspos_prop))
.set_prop("display", hide=GetAttrProp("_multiselection"))
)
yg = (
FloatItem(_("Y"), default=0.0, help=_("Y-axis position in canvas coordinates"))
.set_pos(col=1)
.set_prop("display", active=NotProp(_abspos_prop))
.set_prop("display", hide=GetAttrProp("_multiselection"))
)
move_anchor = (
ChoiceItem(
_("Interact"),
(
(True, _("moving object changes anchor position")),
(False, _("moving object changes label position")),
),
default=True,
)
.set_prop("display", active=NotProp(_abspos_prop))
.set_prop("display", hide=GetAttrProp("_multiselection"))
)
absg = (
ChoiceItem(
_("Position"),
[
("TL", _("Top left")),
("TR", _("Top right")),
("BL", _("Bottom left")),
("BR", _("Bottom right")),
("L", _("Left")),
("R", _("Right")),
("T", _("Top")),
("B", _("Bottom")),
("C", _("Center")),
],
default="TL",
help=_("Absolute position on canvas"),
)
.set_prop("display", active=_abspos_prop)
.set_prop("display", hide=GetAttrProp("_multiselection"))
)
_end_anchorpos = EndGroup(_("Anchor position")).set_prop(
"display", hide=GetAttrProp("_multiselection")
)
___eposition = EndGroup(_("Position"))
# ----------------------------------------------------------------------- End
_endstyles = EndTabGroup("Styles")
def update_param(self, item: LabelItem) -> None:
"""Update the parameters associated with the label item.
Args:
item: The label item from which to update the parameters.
"""
# The following is necessary only for shape labels:
# when shape is just created (and not yet moved), we need to update
# these attributes
update_dataset(self, item.labelparam)
if self.abspos:
self.absg = item.G
else:
self.xg, self.yg = item.G
self.xc, self.yc = item.C
self.label = item.title().text()
def update_item(self, item: LabelItem) -> None:
"""Update the label item with the parameters.
Args:
item: The label item to update.
"""
if not self._multiselection:
if self.abspos:
item.G = self.absg
else:
item.G = (self.xg, self.yg)
item.C = self.xc, self.yc
item.anchor = self.anchor
item.setTitle(self.label)
item.marker = self.symbol.build_symbol()
item.border_pen = self.border.build_pen()
item.set_text_style(self.font.build_font(), self.color)
color = QG.QColor(self.bgcolor)
color.setAlphaF(self.bgalpha)
item.bg_brush = QG.QBrush(color)
class LabelParam_MS(LabelParam):
"""Parameters for a label item in multiselection mode."""
_multiselection = True
ItemParameters.register_multiselection(LabelParam, LabelParam_MS)
class LegendParam(LabelParam):
"""Parameters for a legend box item."""
_legend = True
label = StringItem(_("Title"), default="").set_prop("display", hide=True)
def update_item(self, item: LegendBoxItem) -> None:
"""Update the legend item with the parameters.
Args:
item: The legend item to update.
"""
super().update_item(item)
if not self._multiselection:
item.setTitle(self.get_title())
class LegendParam_MS(LegendParam):
"""Parameters for a legend box item in multiselection mode."""
_multiselection = True
ItemParameters.register_multiselection(LegendParam, LegendParam_MS)
class LabelParamWithContents(LabelParam):
"""Parameters for a label item with contents.
Args:
title: The title of the label item.
comment: The comment associated with the label item.
icon: The icon associated with the label item.
"""
_no_contents = False
def __init__(
self, title: str | None = None, comment: str | None = None, icon: str = ""
) -> None:
self.plain_text = None
super().__init__(title, comment, icon)
def update_param(self, item: LabelItem) -> None:
"""Update the parameters associated with the label item.
Args:
item: The label item from which to update the parameters.
"""
super().update_param(item)
self.contents = self.plain_text = item.get_plain_text()
def update_item(self, item: LabelItem) -> None:
"""Update the label item with the parameters.
Args:
item: The label item to update.
"""
super().update_item(item)
if self.plain_text is not None and self.contents != self.plain_text:
text = self.contents.replace("\n", "<br>")
item.set_text(text)
class LabelParamWithContents_MS(LabelParamWithContents):
"""Parameters for a label item with contents in multiselection mode."""
_multiselection = True
ItemParameters.register_multiselection(
LabelParamWithContents, LabelParamWithContents_MS
)