-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathitems.py
More file actions
483 lines (361 loc) · 13.2 KB
/
items.py
File metadata and controls
483 lines (361 loc) · 13.2 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the BSD 3-Clause
# (see plotpy/LICENSE for details)
"""
plotpy.widget.interfaces
-----------------------------
The `interfaces` module provides object interface classes for :mod:`plotpy`.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
if TYPE_CHECKING:
import guidata.io
from qtpy.QtCore import QPointF
from plotpy.styles.base import ItemParameters
class IItemType:
"""Item types are used to categorized items in a
broader way than objects obeying IBasePlotItem.
The necessity arises from the fact that Plotpy Items
can inherit from different base classes and still
provide functionalities akin to a given ItemType
the types() method of an item returns a list of interfaces
this item supports
"""
class ITrackableItemType(IItemType):
def get_closest_coordinates(self, x: float, y: float) -> tuple[float, float]:
"""
Get the closest coordinates to the given point
Args:
x: X coordinate
y: Y coordinate
Returns:
tuple[float, float]: Closest coordinates
"""
def get_coordinates_label(self, x: float, y: float) -> str:
"""
Get the coordinates label for the given coordinates
Args:
x: X coordinate
y: Y coordinate
Returns:
str: Coordinates label
"""
class IDecoratorItemType(IItemType):
"""represents a decorative item (usually not active)
such as grid, or axes markers"""
class ICurveItemType(IItemType):
"""A curve"""
class IImageItemType(IItemType):
"""An image"""
class IVoiImageItemType(IItemType):
"""An image with set_lut_range, get_lut_range"""
def set_lut_range(self, lut_range: tuple[float, float]) -> None:
"""
Set the current active lut range
Args:
lut_range: Lut range, tuple(min, max)
Example:
>>> item.set_lut_range((0.0, 1.0))
"""
def get_lut_range(self) -> tuple[float, float]:
"""Get the current active lut range
Returns:
tuple[float, float]: Lut range, tuple(min, max)
"""
return 0.0, 1.0
def get_lut_range_full(self) -> tuple[float, float]:
"""Return full dynamic range
Returns:
tuple[float, float]: Lut range, tuple(min, max)
"""
return 10.0, 20.0
def get_lut_range_max(self) -> tuple[float, float]:
"""Get maximum range for this dataset
Returns:
tuple[float, float]: Lut range, tuple(min, max)
"""
return 0.0, 255.0
class IColormapImageItemType(IItemType):
"""An image with an associated colormap"""
class IExportROIImageItemType(IItemType):
"""An image with export_roi"""
def export_roi(
self,
src_rect: tuple[float, float, float, float],
dst_rect: tuple[float, float, float, float],
dst_image: np.ndarray,
apply_lut: bool = False,
apply_interpolation: bool = False,
original_resolution: bool = False,
force_interp_mode: str | None = None,
force_interp_size: int | None = None,
) -> None:
"""
Export a rectangular area of the image to another image
Args:
src_rect: Source rectangle
dst_rect: Destination rectangle
dst_image: Destination image
apply_lut: Apply lut (Default value = False)
apply_interpolation: Apply interpolation (Default value = False)
original_resolution: Original resolution (Default value = False)
force_interp_mode: Force interpolation mode (Default value = None)
force_interp_size: Force interpolation size (Default value = None)
"""
pass
class ICSImageItemType(IItemType):
"""An image supporting X/Y cross sections"""
def get_xsection(self, y0: float | int, apply_lut: bool = False) -> np.ndarray:
"""Return cross section along x-axis at y=y0
Args:
y0: Y0
apply_lut: Apply lut (Default value = False)
Returns:
np.ndarray: Cross section along x-axis at y=y0
"""
def get_ysection(self, x0: float | int, apply_lut: bool = False) -> np.ndarray:
"""Return cross section along y-axis at x=x0
Args:
x0: X0
apply_lut: Apply lut (Default value = False)
Returns:
np.ndarray: Cross section along y-axis at x=x0
"""
def get_average_xsection(
self, x0: float, y0: float, x1: float, y1: float, apply_lut: bool = False
) -> np.ndarray:
"""Return average cross section along x-axis for the given rectangle
Args:
x0: X0 of top left corner
y0: Y0 of top left corner
x1: X1 of bottom right corner
y1: Y1 of bottom right corner
apply_lut: Apply lut (Default value = False)
Returns:
np.ndarray: Average cross section along x-axis
"""
def get_average_ysection(
self, x0: float, y0: float, x1: float, y1: float, apply_lut: bool = False
) -> np.ndarray:
"""Return average cross section along y-axis
Args:
x0: X0 of top left corner
y0: Y0 of top left corner
x1: X1 of bottom right corner
y1: Y1 of bottom right corner
apply_lut: Apply lut (Default value = False)
Returns:
np.ndarray: Average cross section along y-axis
"""
class IShapeItemType(IItemType):
"""A shape (annotation)"""
pass
class ISerializableType(IItemType):
"""An item that can be serialized"""
def serialize(
self,
writer: guidata.io.HDF5Writer | guidata.io.INIWriter | guidata.io.JSONWriter,
) -> None:
"""Serialize object to HDF5 writer
Args:
writer: HDF5, INI or JSON writer
"""
def deserialize(
self,
reader: guidata.io.HDF5Reader | guidata.io.INIReader | guidata.io.JSONReader,
) -> None:
"""Deserialize object from HDF5 reader
Args:
reader: HDF5, INI or JSON reader
"""
class IBasePlotItem:
"""
This is the interface that QwtPlotItem objects must implement
to be handled by *BasePlot* widgets
"""
selected = False # True if this item is selected
_readonly = False
_private = False
_can_select = True # Indicate this item can be selected
_can_move = True
_can_resize = True
_can_rotate = True
def set_selectable(self, state: bool) -> None:
"""Set item selectable state
Args:
state: True if item is selectable, False otherwise
"""
self._can_select = state
def set_resizable(self, state: bool) -> None:
"""Set item resizable state
(or any action triggered when moving an handle, e.g. rotation)
Args:
state: True if item is resizable, False otherwise
"""
self._can_resize = state
def set_movable(self, state: bool) -> None:
"""Set item movable state
Args:
state: True if item is movable, False otherwise
"""
self._can_move = state
def set_rotatable(self, state: bool) -> None:
"""Set item rotatable state
Args:
state: True if item is rotatable, False otherwise
"""
self._can_rotate = state
def can_select(self) -> bool:
"""
Returns True if this item can be selected
Returns:
bool: True if item can be selected, False otherwise
"""
return self._can_select
def can_resize(self) -> bool:
"""
Returns True if this item can be resized
Returns:
bool: True if item can be resized, False otherwise
"""
return self._can_resize
def can_move(self) -> bool:
"""
Returns True if this item can be moved
Returns:
bool: True if item can be moved, False otherwise
"""
return self._can_move
def can_rotate(self) -> bool:
"""
Returns True if this item can be rotated
Returns:
bool: True if item can be rotated, False otherwise
"""
return self._can_rotate
def types(self) -> tuple[type[IItemType], ...]:
"""Returns a group or category for this item.
This should be a tuple of class objects inheriting from IItemType
Returns:
tuple: Tuple of class objects inheriting from IItemType
"""
def set_readonly(self, state: bool) -> None:
"""Set object readonly state
Args:
state: True if object is readonly, False otherwise
"""
self._readonly = state
def is_readonly(self) -> bool:
"""Return object readonly state
Returns:
bool: True if object is readonly, False otherwise
"""
return self._readonly
def set_private(self, state: bool) -> None:
"""Set object as private
Args:
state: True if object is private, False otherwise
"""
self._private = state
def is_private(self) -> bool:
"""Return True if object is private
Returns:
bool: True if object is private, False otherwise
"""
return self._private
def select(self) -> None:
"""
Select the object and eventually change its appearance to highlight the
fact that it's selected
"""
# should call plot.invalidate() or replot to force redraw
def unselect(self) -> None:
"""
Unselect the object and eventually restore its original appearance to
highlight the fact that it's not selected anymore
"""
# should call plot.invalidate() or replot to force redraw
def hit_test(self, pos: QPointF) -> tuple[float, float, bool, None]:
"""Return a tuple (distance, attach point, inside, other_object)
Args:
pos: Position
Returns:
tuple: Tuple with four elements: (distance, attach point, inside,
other_object).
Description of the returned values:
* distance: distance in pixels (canvas coordinates) to the closest
attach point
* attach point: handle of the attach point
* inside: True if the mouse button has been clicked inside the object
* other_object: if not None, reference of the object which will be
considered as hit instead of self
"""
def update_item_parameters(self) -> None:
"""Update item parameters (dataset) from object properties"""
def get_item_parameters(self, itemparams: ItemParameters) -> None:
"""
Appends datasets to the list of DataSets describing the parameters
used to customize apearance of this item
Args:
itemparams: Item parameters
"""
def set_item_parameters(self, itemparams: ItemParameters) -> None:
"""
Change the appearance of this item according
to the parameter set provided
Args:
itemparams: Item parameters
"""
def move_local_point_to(self, handle: int, pos: QPointF, ctrl: bool = None) -> None:
"""Move a handle as returned by hit_test to the new position
Args:
handle: Handle
pos: Position
ctrl: True if <Ctrl> button is being pressed, False otherwise
"""
def move_local_shape(self, old_pos: QPointF, new_pos: QPointF) -> None:
"""Translate the shape such that old_pos becomes new_pos in canvas coordinates
Args:
old_pos: Old position
new_pos: New position
"""
def move_with_selection(self, delta_x: float, delta_y: float) -> None:
"""Translate the item together with other selected items
Args:
delta_x: Translation in plot coordinates along x-axis
delta_y: Translation in plot coordinates along y-axis
"""
class IBaseImageItem:
"""
QwtPlotItem image objects handled by *BasePlot* widgets must implement
_both_ the IBasePlotItem interface and this one
"""
_can_sethistogram = False # A levels histogram will be bound to image
def can_sethistogram(self) -> bool:
"""
Returns True if this item can be associated with a levels histogram
Returns:
bool: True if item can be associated with a levels histogram,
False otherwise
"""
return self._can_sethistogram
class IHistDataSource:
def get_histogram(
self, nbins: int, drange: tuple[float, float] | None = None
) -> tuple[np.ndarray, np.ndarray]:
"""
Return a tuple (hist, bins) where hist is a list of histogram values
Args:
nbins: number of bins
drange: lower and upper range of the bins. If not provided, range is
simply (data.min(), data.max()). Values outside the range are ignored.
Returns:
Tuple (hist, bins)
Example of implementation:
def get_histogram(self, nbins, drange=None):
data = self.get_data()
return np.histogram(data, bins=nbins, range=drange)
"""
pass