Skip to content

Commit a6b39a3

Browse files
committed
Fix curve color and style cycle
1 parent 95a7247 commit a6b39a3

File tree

7 files changed

+377
-337
lines changed

7 files changed

+377
-337
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ for future and past milestones.
3232
* Fixed [Issue #55](https://github.com/DataLab-Platform/DataLab/issues/55) - Changing image bounds in Image View has no effect on the associated image object properties
3333
* Fixed [Issue #56](https://github.com/DataLab-Platform/DataLab/issues/56) - "Test data" plugin: `AttributeError: 'NoneType' object has no attribute 'data'` when canceling "Create image with peaks"
3434
* Fixed [Issue #57](https://github.com/DataLab-Platform/DataLab/issues/57) - Circle and ellipse result shapes are not transformed properly
35+
* Curve color and style cycle:
36+
* Before this release, this cycle was handled by the same mechanism either for the
37+
Signal Panel or the HDF5 Browser, which was not the expected behavior
38+
* Now, the cycle is handled separately: the HDF5 Browser or the Text Import
39+
Wizard use always the same color and style for curves, and they don't interfere
40+
with the Signal Panel cycle
3541

3642
## DataLab Version 0.12.0 ##
3743

cdl/core/gui/panel/signal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from cdl.core.gui.processor.signal import SignalProcessor
3030
from cdl.core.io.signal import SignalIORegistry
3131
from cdl.core.model.signal import (
32-
CurveStyles,
32+
CURVESTYLES,
3333
SignalObj,
3434
create_signal_from_param,
3535
new_signal_param,
@@ -136,4 +136,4 @@ def toggle_anti_aliasing(self, state: bool) -> None:
136136

137137
def reset_curve_styles(self) -> None:
138138
"""Reset curve styles"""
139-
CurveStyles.reset_styles()
139+
CURVESTYLES.reset_styles()

cdl/core/model/signal.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
from __future__ import annotations
1616

17+
from contextlib import contextmanager
1718
from copy import deepcopy
18-
from typing import TYPE_CHECKING
19+
from typing import TYPE_CHECKING, Generator
1920
from uuid import uuid4
2021

2122
import guidata.dataset as gds
@@ -52,27 +53,42 @@ class CurveStyles:
5253
)
5354
LINESTYLES = ("SolidLine", "DashLine", "DashDotLine", "DashDotDotLine")
5455

55-
def style_generator(): # pylint: disable=no-method-argument
56+
def __init__(self) -> None:
57+
self.__suspend = False
58+
self.curve_style = self.style_generator()
59+
60+
@staticmethod
61+
def style_generator() -> Generator[tuple[str, str], None, None]:
5662
"""Cycling through curve styles"""
5763
while True:
5864
for linestyle in CurveStyles.LINESTYLES:
5965
for color in CurveStyles.COLORS:
6066
yield (color, linestyle)
6167

62-
CURVE_STYLE = style_generator()
63-
64-
@classmethod
65-
def apply_style(cls, param: CurveParam):
68+
def apply_style(self, param: CurveParam) -> None:
6669
"""Apply style to curve"""
67-
color, linestyle = next(cls.CURVE_STYLE)
70+
if self.__suspend:
71+
# Suspend mode: always apply the first style
72+
color, linestyle = CurveStyles.COLORS[0], CurveStyles.LINESTYLES[0]
73+
else:
74+
color, linestyle = next(self.curve_style)
6875
param.line.color = color
6976
param.line.style = linestyle
7077
param.symbol.marker = "NoSymbol"
7178

72-
@classmethod
73-
def reset_styles(cls):
79+
def reset_styles(self) -> None:
7480
"""Reset styles"""
75-
cls.CURVE_STYLE = cls.style_generator()
81+
self.curve_style = self.style_generator()
82+
83+
@contextmanager
84+
def suspend(self) -> Generator[None, None, None]:
85+
"""Suspend style generator"""
86+
self.__suspend = True
87+
yield
88+
self.__suspend = False
89+
90+
91+
CURVESTYLES = CurveStyles() # This is the unique instance of the CurveStyles class
7692

7793

7894
class ROIParam(gds.DataSet):
@@ -316,7 +332,7 @@ def make_item(self, update_from: CurveItem = None) -> CurveItem:
316332
elif len(self.xydata) == 4: # x, y, dx, dy error bar signal
317333
x, y, dx, dy = self.xydata
318334
item = make.merror(x.real, y.real, dx.real, dy.real, label=self.title)
319-
CurveStyles.apply_style(item.param)
335+
CURVESTYLES.apply_style(item.param)
320336
else:
321337
raise RuntimeError("data not supported")
322338
if update_from is None:

cdl/tests/scenarios/demo.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
get_test_image,
3131
)
3232
from cdl.tests.features.common.roi_app import create_test_image_with_roi
33-
from cdl.widgets import fitdialog
3433

3534
if TYPE_CHECKING: # pragma: no cover
3635
from cdl.core.gui.main import CDLMainWindow

cdl/widgets/h5browser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from cdl.config import _
3232
from cdl.core.io.h5 import H5Importer
33-
from cdl.core.model.signal import CurveStyles
33+
from cdl.core.model.signal import CURVESTYLES
3434
from cdl.env import execenv
3535
from cdl.obj import ImageObj, SignalObj
3636
from cdl.utils.qthelpers import qt_handle_error_message
@@ -402,11 +402,11 @@ def update_plot_preview(self, node: BaseNode) -> None:
402402
if isinstance(obj, SignalObj):
403403
obj: SignalObj
404404
widget = self.curvewidget
405-
CurveStyles.reset_styles()
406405
else:
407406
obj: ImageObj
408407
widget = self.imagewidget
409-
item = obj.make_item()
408+
with CURVESTYLES.suspend():
409+
item = obj.make_item()
410410
plot = widget.get_plot()
411411
plot.del_all_items()
412412
plot.add_item(item)

cdl/widgets/textimport.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from qtpy.compat import getopenfilename
3737

3838
from cdl.config import Conf, _
39-
from cdl.core.model.signal import CurveStyles
39+
from cdl.core.model.signal import CURVESTYLES
4040
from cdl.obj import ImageObj, SignalObj, create_image, create_signal
4141
from cdl.utils.qthelpers import create_progress_bar, save_restore_stds
4242
from cdl.widgets.wizard import Wizard, WizardPage
@@ -476,12 +476,12 @@ def initialize_page(self) -> None:
476476
plot = self.plot_widget.get_plot()
477477
plot.del_all_items()
478478
if self.destination == "signal":
479-
CurveStyles.reset_styles()
480479
xydata = data.T
481480
x = np.arange(len(xydata[0]))
482481
if len(xydata) == 1:
483482
obj = create_signal("", x=x, y=xydata[0])
484-
item = obj.make_item()
483+
with CURVESTYLES.suspend():
484+
item = obj.make_item()
485485
plot.add_item(item)
486486
self.__objitmlist = [(obj, item)]
487487
else:
@@ -497,7 +497,8 @@ def initialize_page(self) -> None:
497497
break
498498
yidx = ycol if param.first_col_is_x else ycol - 1
499499
obj = create_signal("", x=x, y=xydata[yidx])
500-
item = obj.make_item()
500+
with CURVESTYLES.suspend():
501+
item = obj.make_item()
501502
plot.add_item(item)
502503
self.__objitmlist.append((obj, item))
503504
QW.QApplication.processEvents()

0 commit comments

Comments
 (0)