|
17 | 17 | import qtpy.QtCore as QC |
18 | 18 | import qtpy.QtGui as QG |
19 | 19 | import qtpy.QtWidgets as QW |
| 20 | +from guidata.qthelpers import qt_app_context |
20 | 21 |
|
21 | 22 | from plotpy.widgets.colormap_editor import ColorMapEditor |
22 | 23 | from plotpy.widgets.colormap_widget import CustomQwtLinearColormap |
23 | 24 |
|
24 | | -if __name__ == "__main__": |
25 | | - print("Initialization of a default colormap editor widget") |
26 | | - app = QW.QApplication([]) |
27 | | - editor = ColorMapEditor(None) |
28 | | - red = QG.QColor(QC.Qt.GlobalColor.red) |
29 | | - green = QG.QColor(QC.Qt.GlobalColor.green) |
30 | | - editor.colormap_widget.add_handle_at_relative_pos(0.5, red) |
31 | | - editor.show() |
32 | | - app.exec_() |
33 | 25 |
|
34 | | - cmap_tuples = editor.get_colormap().to_tuples() |
35 | | - print( |
36 | | - "Initialization of a new colormap editor with the previous colormap: ", |
37 | | - cmap_tuples, |
38 | | - ) |
39 | | - new_cmap = CustomQwtLinearColormap.from_iterable(cmap_tuples) |
40 | | - print(f"{new_cmap.to_tuples()}") |
41 | | - editor = ColorMapEditor(None, colormap=new_cmap) |
42 | | - editor.show() |
43 | | - app.exec_() |
| 26 | +def test_colormap_manager() -> None: |
| 27 | + """Test the colormap editor widget and the CustomQwtLinearColormap class |
| 28 | + by using multiple methods to initialize and export the colormap. |
| 29 | + """ |
| 30 | + with qt_app_context(exec_loop=True): |
| 31 | + print("Initialization of a default colormap editor widget") |
| 32 | + editor = ColorMapEditor(None) |
| 33 | + red = QG.QColor(QC.Qt.GlobalColor.red) |
| 34 | + green = QG.QColor(QC.Qt.GlobalColor.green) |
| 35 | + editor.colormap_widget.add_handle_at_relative_pos(0.5, red) |
| 36 | + editor.show() |
| 37 | + |
| 38 | + cmap_tuples = editor.get_colormap().to_tuples() |
| 39 | + print( |
| 40 | + "Initialization of a new colormap editor with the previous colormap: ", |
| 41 | + cmap_tuples, |
| 42 | + ) |
| 43 | + new_cmap = CustomQwtLinearColormap.from_iterable(cmap_tuples) |
| 44 | + print(f"{new_cmap.to_tuples()}") |
| 45 | + editor = ColorMapEditor(None, colormap=new_cmap) |
| 46 | + editor.show() |
| 47 | + |
| 48 | + cmap_tuples = editor.get_colormap().to_tuples() |
| 49 | + print( |
| 50 | + "Initialization of a new default colormap editor, " |
| 51 | + "modified post-initialization with the previous colormap: ", |
| 52 | + cmap_tuples, |
| 53 | + ) |
| 54 | + new_cmap = CustomQwtLinearColormap.from_iterable(cmap_tuples) |
| 55 | + editor = ColorMapEditor(None) |
| 56 | + editor.set_colormap(new_cmap) |
| 57 | + editor.show() |
44 | 58 |
|
45 | | - cmap_tuples = editor.get_colormap().to_tuples() |
46 | | - print( |
47 | | - "Initialization of a new default colormap editor, " |
48 | | - "modified post-initialization with the previous colormap: ", |
49 | | - cmap_tuples, |
50 | | - ) |
51 | | - new_cmap = CustomQwtLinearColormap.from_iterable(cmap_tuples) |
52 | | - editor = ColorMapEditor(None) |
53 | | - editor.set_colormap(new_cmap) |
54 | | - editor.show() |
55 | | - app.exec_() |
| 59 | + cmap_tuples = editor.get_colormap().to_tuples() |
| 60 | + cmap_tuples = tuple((int(val * 255 + 1), color) for val, color in cmap_tuples) |
| 61 | + print( |
| 62 | + "Initialization of a new default colormap editor, " |
| 63 | + "modified post-initialization with the previous colormap with stops scaled by " |
| 64 | + "255 + 1: ", |
| 65 | + cmap_tuples, |
| 66 | + ) |
| 67 | + new_cmap = CustomQwtLinearColormap.from_iterable(cmap_tuples) |
| 68 | + editor = ColorMapEditor(None) |
| 69 | + editor.set_colormap(new_cmap) |
| 70 | + editor.show() |
56 | 71 |
|
57 | | - cmap_tuples = editor.get_colormap().to_tuples() |
58 | | - cmap_tuples = tuple((int(val * 255 + 1), color) for val, color in cmap_tuples) |
59 | | - print( |
60 | | - "Initialization of a new default colormap editor, " |
61 | | - "modified post-initialization with the previous colormap with stops scaled by " |
62 | | - "255 + 1: ", |
63 | | - cmap_tuples, |
64 | | - ) |
65 | | - new_cmap = CustomQwtLinearColormap.from_iterable(cmap_tuples) |
66 | | - editor = ColorMapEditor(None) |
67 | | - editor.set_colormap(new_cmap) |
68 | | - editor.show() |
69 | | - app.exec_() |
| 72 | + print( |
| 73 | + "Initialization of a new default colormap editor, " |
| 74 | + "modified post-initialization with the previous colormap where the red stop is " |
| 75 | + "replaced with a green stop: ", |
| 76 | + cmap_tuples, |
| 77 | + ) |
70 | 78 |
|
71 | | - print( |
72 | | - "Initialization of a new default colormap editor, " |
73 | | - "modified post-initialization with the previous colormap where the red stop is " |
74 | | - "replaced with a green stop: ", |
75 | | - cmap_tuples, |
76 | | - ) |
| 79 | + editor = ColorMapEditor(None) |
| 80 | + editor.set_colormap(new_cmap) |
| 81 | + editor.colormap_widget.edit_color_stop(1, None, green) |
| 82 | + editor.show() |
77 | 83 |
|
78 | | - editor = ColorMapEditor(None) |
79 | | - editor.set_colormap(new_cmap) |
80 | | - editor.colormap_widget.edit_color_stop(1, None, green) |
81 | | - editor.show() |
82 | | - app.exec_() |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + test_colormap_manager() |
0 commit comments