Skip to content

Commit fe229be

Browse files
committed
PolygonMapItem objects: clean-up code
1 parent 648d696 commit fe229be

File tree

7 files changed

+98
-102
lines changed

7 files changed

+98
-102
lines changed

plotpy/images/items/polygonmap.png

448 Bytes
Loading

plotpy/items/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
RangeInfo,
5050
SelectedLegendBoxItem,
5151
)
52-
from .polygon import PolygonMapItem
52+
from .polygonmap import PolygonMapItem
5353
from .shape import (
5454
AbstractShape,
5555
Axes,
Lines changed: 26 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@
1919
from qwt import QwtPlotItem
2020

2121
from plotpy.config import _
22-
from plotpy.interfaces import (
23-
IBasePlotItem,
24-
ICurveItemType,
25-
ISerializableType,
26-
ITrackableItemType,
27-
)
28-
from plotpy.items.curve.base import SELECTED_SYMBOL
29-
from plotpy.styles.curve import CurveParam
22+
from plotpy.interfaces import IBasePlotItem, ISerializableType, ITrackableItemType
23+
from plotpy.styles import PolygonMapParam
3024

3125
if TYPE_CHECKING: # pragma: no cover
3226
import guidata.dataset.io
@@ -40,7 +34,6 @@
4034
def simplify_poly(pts, off, scale, bounds):
4135
"""Simplify a polygon by removing points outside the canvas"""
4236
ax, bx, ay, by = scale
43-
xm, ym, xM, yM = bounds
4437
a = np.array([[ax, ay]])
4538
b = np.array([[bx, by]])
4639
_pts = a * pts + b
@@ -57,9 +50,10 @@ def simplify_poly(pts, off, scale, bounds):
5750

5851

5952
class PolygonMapItem(QwtPlotItem):
60-
"""
61-
Construct a curve `plot item` with the parameters *curveparam*
62-
(see :py:class:`.styles.CurveParam`)
53+
"""Construct a PolygonMapItem object
54+
55+
Args:
56+
param: Polygon parameters
6357
"""
6458

6559
__implements__ = (IBasePlotItem, ISerializableType)
@@ -71,20 +65,20 @@ class PolygonMapItem(QwtPlotItem):
7165
_can_move = False
7266
_can_rotate = False
7367

74-
def __init__(self, curveparam=None):
68+
def __init__(self, param: PolygonMapParam | None = None) -> None:
7569
super().__init__()
76-
if curveparam is None:
77-
self.param = CurveParam(_("PolygonMap"), icon="curve.png")
70+
if param is None:
71+
self.param = PolygonMapParam(_("PolygonMap"), icon="polygonmap.png")
7872
else:
79-
self.param = curveparam
73+
self.param = param
8074
self.selected = False
8175
self.immutable = True # set to false to allow moving points around
8276
self._pts = None # Array of points Mx2
8377
self._n = None # Array of polygon offsets/ends Nx1
8478
# (polygon k points are _pts[_n[k-1]:_n[k]])
8579
self._c = None # Color of polygon Nx2 [border,background] as RGBA uint32
8680
self.update_params()
87-
self.setIcon(get_icon("curve.png"))
81+
self.setIcon(get_icon("polygonmap.png"))
8882

8983
def types(self) -> tuple[type[IItemType], ...]:
9084
"""Returns a group or category for this item.
@@ -93,7 +87,7 @@ def types(self) -> tuple[type[IItemType], ...]:
9387
Returns:
9488
tuple: Tuple of class objects inheriting from IItemType
9589
"""
96-
return (ICurveItemType, ITrackableItemType, ISerializableType)
90+
return (ITrackableItemType, ISerializableType)
9791

9892
def can_select(self) -> bool:
9993
"""
@@ -164,56 +158,6 @@ def set_rotatable(self, state: bool) -> None:
164158
"""
165159
self._can_rotate = state
166160

167-
def setPen(self, x):
168-
"""
169-
170-
:param x:
171-
"""
172-
pass
173-
174-
def setBrush(self, x):
175-
"""
176-
177-
:param x:
178-
"""
179-
pass
180-
181-
def setSymbol(self, x):
182-
"""
183-
184-
:param x:
185-
"""
186-
pass
187-
188-
def setCurveAttribute(self, x, y):
189-
"""
190-
191-
:param x:
192-
:param y:
193-
"""
194-
pass
195-
196-
def setStyle(self, x):
197-
"""
198-
199-
:param x:
200-
"""
201-
pass
202-
203-
def setCurveType(self, x):
204-
"""
205-
206-
:param x:
207-
"""
208-
pass
209-
210-
def setBaseline(self, x):
211-
"""
212-
213-
:param x:
214-
"""
215-
pass
216-
217161
def __reduce__(self):
218162
state = (self.param, self._pts, self._n, self._c, self.z())
219163
res = (PolygonMapItem, (), state)
@@ -228,9 +172,11 @@ def __setstate__(self, state):
228172

229173
def serialize(
230174
self,
231-
writer: guidata.dataset.io.HDF5Writer
232-
| guidata.dataset.io.INIWriter
233-
| guidata.dataset.io.JSONWriter,
175+
writer: (
176+
guidata.dataset.io.HDF5Writer
177+
| guidata.dataset.io.INIWriter
178+
| guidata.dataset.io.JSONWriter
179+
),
234180
) -> None:
235181
"""Serialize object to HDF5 writer
236182
@@ -242,13 +188,15 @@ def serialize(
242188
writer.write(self._c, group_name="Cdata")
243189
writer.write(self.z(), group_name="z")
244190
self.param.update_param(self)
245-
writer.write(self.param, group_name="curveparam")
191+
writer.write(self.param, group_name="param")
246192

247193
def deserialize(
248194
self,
249-
reader: guidata.dataset.io.HDF5Reader
250-
| guidata.dataset.io.INIReader
251-
| guidata.dataset.io.JSONReader,
195+
reader: (
196+
guidata.dataset.io.HDF5Reader
197+
| guidata.dataset.io.INIReader
198+
| guidata.dataset.io.JSONReader
199+
),
252200
) -> None:
253201
"""Deserialize object from HDF5 reader
254202
@@ -260,8 +208,8 @@ def deserialize(
260208
c = reader.read(group_name="Cdata", func=reader.read_array)
261209
self.set_data(pts, n, c)
262210
self.setZ(reader.read("z"))
263-
self.param = CurveParam(_("PolygonMap"), icon="curve.png")
264-
reader.read("curveparam", instance=self.param)
211+
self.param = PolygonMapParam(_("PolygonMap"), icon="polygonmap.png")
212+
reader.read("param", instance=self.param)
265213
self.update_params()
266214

267215
def set_readonly(self, state: bool) -> None:
@@ -307,19 +255,12 @@ def select(self) -> None:
307255
Select the object and eventually change its appearance to highlight the
308256
fact that it's selected
309257
"""
310-
self.selected = True
311-
self.setSymbol(SELECTED_SYMBOL)
312-
self.invalidate_plot()
313258

314259
def unselect(self) -> None:
315260
"""
316261
Unselect the object and eventually restore its original appearance to
317262
highlight the fact that it's not selected anymore
318263
"""
319-
self.selected = False
320-
# Restoring initial curve parameters:
321-
self.param.update_item(self)
322-
self.invalidate_plot()
323264

324265
def get_data(self):
325266
"""Return curve data x, y (NumPy arrays)"""
@@ -451,7 +392,7 @@ def set_item_parameters(self, itemparams: ItemParameters) -> None:
451392
Args:
452393
itemparams: Item parameters
453394
"""
454-
update_dataset(self.param, itemparams.get("CurveParam"), visible_only=True)
395+
update_dataset(self.param, itemparams.get("PolygonMapParam"), visible_only=True)
455396
self.update_params()
456397

457398
def draw(
@@ -469,7 +410,6 @@ def draw(
469410
yMap: Y axis scale map
470411
canvasRect: Canvas rectangle
471412
"""
472-
# from time import time
473413
p1x = xMap.p1()
474414
s1x = xMap.s1()
475415
ax = (xMap.p2() - p1x) / (xMap.s2() - s1x)
@@ -481,13 +421,9 @@ def draw(
481421
_n = self._n
482422
fgcol = QG.QColor()
483423
bgcol = QG.QColor()
484-
# t0 = time()
485424
polygons = simplify_poly(
486425
self._pts, _n, (ax, bx, ay, by), canvasRect.getCoords()
487426
)
488-
# t1 = time()
489-
# print len(polygons), t1-t0
490-
# t2 = time()
491427
for poly, num in polygons:
492428
points = []
493429
for i in range(poly.shape[0]):
@@ -498,7 +434,6 @@ def draw(
498434
painter.setPen(QG.QPen(fgcol))
499435
painter.setBrush(QG.QBrush(bgcol))
500436
painter.drawPolygon(pg)
501-
# print "poly:", time()-t2
502437

503438
def boundingRect(self) -> QC.QRectF:
504439
"""Return the bounding rectangle of the shape

plotpy/styles/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
LegendParam,
7070
LegendParam_MS,
7171
)
72+
from plotpy.styles.polygonmap import PolygonMapParam, PolygonParam_MS
7273
from plotpy.styles.shape import (
7374
AnnotationParam,
7475
AnnotationParam_MS,

plotpy/styles/curve.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
)
2727

2828
if TYPE_CHECKING:
29-
from plotpy.items import CurveItem, PolygonMapItem
29+
from plotpy.items import CurveItem
3030

3131

3232
class CurveParam(DataSet):
33-
"""Dataset defining the parameters of a CurveItem or PolygonMapItem"""
33+
"""Dataset defining the parameters of a CurveItem"""
3434

3535
_multiselection = False
3636
label = StringItem(_("Title"), default="").set_prop(
@@ -49,23 +49,23 @@ class CurveParam(DataSet):
4949
"display", active=_use_dsamp_prop
5050
)
5151

52-
def update_param(self, curve: CurveItem | PolygonMapItem) -> None:
53-
"""Updates the parameters using values from a given CurveItem/PolygonMapItem
52+
def update_param(self, curve: CurveItem) -> None:
53+
"""Updates the parameters using values from a given CurveItem
5454
5555
Args:
56-
curve: reference CurveItem/PolygonMapItem instance
56+
curve: reference CurveItem instance
5757
"""
5858
self.label = str(curve.title().text())
5959
self.symbol.update_param(curve.symbol())
6060
self.line.update_param(curve.pen())
6161
self.curvestyle = CURVESTYLE_NAME[curve.style()]
6262
self.baseline = curve.baseline()
6363

64-
def update_item(self, curve: CurveItem | PolygonMapItem) -> None:
65-
"""Updates a given CurveItem/PolygonMapItem using the current parameters
64+
def update_item(self, curve: CurveItem) -> None:
65+
"""Updates a given CurveItem using the current parameters
6666
6767
Args:
68-
curve: instance of CurveItem/PolygonMapItem to update
68+
curve: instance of CurveItem to update
6969
"""
7070

7171
plot = curve.plot()

plotpy/styles/polygonmap.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
7+
from guidata.dataset import DataSet, GetAttrProp, StringItem
8+
9+
from plotpy.config import _
10+
from plotpy.styles.base import ItemParameters
11+
12+
if TYPE_CHECKING:
13+
from plotpy.items import PolygonMapItem
14+
15+
16+
class PolygonMapParam(DataSet):
17+
"""Dataset defining the parameters of a PolygonMapItem instance"""
18+
19+
_multiselection = False
20+
label = StringItem(_("Title"), default="").set_prop(
21+
"display", hide=GetAttrProp("_multiselection")
22+
)
23+
24+
def update_param(self, item: PolygonMapItem) -> None:
25+
"""Updates the parameters using values from a given PolygonMapItem
26+
27+
Args:
28+
item: reference PolygonMapItem instance
29+
"""
30+
self.label = str(item.title().text())
31+
32+
def update_item(self, item: PolygonMapItem) -> None:
33+
"""Updates a given PolygonMapItem using the current parameters
34+
35+
Args:
36+
item: instance of PolygonMapItem to update
37+
"""
38+
plot = item.plot()
39+
if plot is not None:
40+
plot.blockSignals(True) # Avoid unwanted calls of update_param
41+
# triggered by the setter methods below
42+
if not self._multiselection:
43+
# Non common parameters
44+
item.setTitle(self.label)
45+
if plot is not None:
46+
plot.blockSignals(False)
47+
48+
49+
class PolygonParam_MS(PolygonMapParam):
50+
"""Same as PolygonParam but for multiselection"""
51+
52+
_multiselection = True
53+
54+
55+
ItemParameters.register_multiselection(PolygonMapParam, PolygonParam_MS)

plotpy/tests/items/test_polygons.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from guidata.qthelpers import qt_app_context
1616

1717
from plotpy.items import PolygonMapItem
18-
from plotpy.plot import PlotDialog
18+
from plotpy.plot import PlotDialog, PlotOptions
1919

2020
# Create a sample dataset consisting of tesselated circles randomly placed
2121
# in a box
@@ -50,7 +50,12 @@ def create_circle():
5050
def test_polygons():
5151
"""Test"""
5252
with qt_app_context(exec_loop=True):
53-
win = PlotDialog(edit=True, toolbar=True, title="Sample multi-polygon item")
53+
win = PlotDialog(
54+
edit=True,
55+
toolbar=True,
56+
title="Sample multi-polygon item",
57+
options=PlotOptions(show_itemlist=True),
58+
)
5459
plot = win.manager.get_plot()
5560
plot.set_aspect_ratio(lock=True)
5661
plot.set_antialiasing(False)

0 commit comments

Comments
 (0)