Skip to content

Commit 697e484

Browse files
committed
Use widget rather than index for tab
1 parent 7a55556 commit 697e484

3 files changed

Lines changed: 136 additions & 44 deletions

File tree

src/ert/gui/plotting/plot_window.py

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@
6767
from .widgets.plot_ensemble_selection_widget import EnsembleSelectionWidget
6868
from .widgets.plot_widget import Plotter, PlotWidget
6969

70-
RESPONSE_DEFAULT = 0
71-
GEN_KW_DEFAULT = 3
72-
STD_DEV_DEFAULT = 7
73-
7470
EVEREST_UPPER_BATCH_LIMIT = 20
7571

7672
logger = logging.getLogger(__name__)
@@ -232,23 +228,20 @@ def __init__(
232228
self._central_tab.currentChanged.connect(self.current_tab_changed)
233229
self.log_plot_tab_usage(self._central_tab.tabText(0), default=True)
234230

235-
self._prev_tab_widget_index = -1
236-
self._current_tab_index = -1
237231
self._prev_key_dimensionality = -1
238232
self._prev_key: str | None = None
239233
self._prev_key_origin: str | None = None
240-
self._prev_tab_widget_index_map: dict[int, int] = {}
241234
if self.is_everest:
242-
self._prev_tab_widget_index_map = {
243-
1: 0,
244-
2: 1,
245-
3: 0, # Fallback
235+
self._default_tab_for_dimensionality = {
236+
1: self._widget_by_name(ENSEMBLE),
237+
2: self._widget_by_name(EVEREST_BATCH_OBJECTIVE_FUNCTION_PLOT),
238+
3: self._widget_by_name(ENSEMBLE), # Fallback
246239
}
247240
else:
248-
self._prev_tab_widget_index_map = {
249-
2: RESPONSE_DEFAULT,
250-
1: GEN_KW_DEFAULT,
251-
3: STD_DEV_DEFAULT,
241+
self._default_tab_for_dimensionality = {
242+
1: self._widget_by_name(HISTOGRAM),
243+
2: self._widget_by_name(ENSEMBLE),
244+
3: self._widget_by_name(STD_DEV),
252245
}
253246

254247
QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)
@@ -356,7 +349,6 @@ def get_plot_api_version(self) -> str:
356349

357350
@Slot(int)
358351
def current_tab_changed(self, index: int) -> None:
359-
self._current_tab_index = index
360352
self.update_plot()
361353
self.log_plot_tab_usage(self._central_tab.tabText(index))
362354

@@ -665,6 +657,15 @@ def add_plot_widget(
665657
self._plot_widgets.append(plot_widget)
666658
self._central_tab.setTabEnabled(index, enabled)
667659

660+
def _find_widget_by_name(self, name: str) -> PlotWidget | None:
661+
return next((w for w in self._plot_widgets if w.name == name), None)
662+
663+
def _widget_by_name(self, name: str) -> PlotWidget:
664+
widget = self._find_widget_by_name(name)
665+
if widget is None:
666+
raise ValueError(f"No plot tab named '{name}'")
667+
return widget
668+
668669
def _edit_axis_label(self, axis: str) -> None:
669670
label_names = {"x": "x-label", "y": "y-label"}
670671
if axis not in label_names:
@@ -772,12 +773,6 @@ def keySelected(self) -> None:
772773
def everest_data_origin_check(origin: list[str]) -> bool:
773774
return key_def.metadata.get("data_origin") in origin
774775

775-
def everest_widget_locator(widget_name: str) -> PlotWidget | None:
776-
return next(
777-
(w for w in self._plot_widgets if w.name == widget_name),
778-
None,
779-
)
780-
781776
everest_plot_and_origin = [
782777
(EVEREST_OBJECTIVE_FUNCTION_PLOT, ["everest_objectives"]),
783778
(EVEREST_BATCH_OBJECTIVE_FUNCTION_PLOT, ["everest_batch_objectives"]),
@@ -790,15 +785,17 @@ def everest_available_widget_selection(
790785
widget_tuple_list: list[tuple[str, list[str]]],
791786
) -> None:
792787
for widget_name, origin in widget_tuple_list:
793-
widget = everest_widget_locator(widget_name)
794-
if widget:
795-
if everest_data_origin_check(origin):
796-
if widget not in available_widgets:
797-
available_widgets.append(widget)
798-
elif widget in available_widgets:
799-
available_widgets.remove(widget)
788+
widget = self._widget_by_name(widget_name)
789+
if everest_data_origin_check(origin):
790+
if widget not in available_widgets:
791+
available_widgets.append(widget)
792+
elif widget in available_widgets:
793+
available_widgets.remove(widget)
800794

801-
everest_available_widget_selection(everest_plot_and_origin)
795+
if self.is_everest:
796+
everest_available_widget_selection(everest_plot_and_origin)
797+
798+
previous_widget = self._central_tab.currentWidget()
802799

803800
# Enabling/disabling tab triggers the
804801
# current_tab_changed event which also triggers
@@ -814,22 +811,19 @@ def everest_available_widget_selection(
814811
current_widget = self._central_tab.currentWidget()
815812

816813
if 0 < self._prev_key_dimensionality != key_def.dimensionality:
817-
if self._current_tab_index == -1:
818-
self._current_tab_index = self._prev_tab_widget_index
819-
self._prev_tab_widget_index_map[self._prev_key_dimensionality] = (
820-
self._current_tab_index
821-
)
822-
current_widget = self._central_tab.widget(
823-
self._prev_tab_widget_index_map[key_def.dimensionality]
824-
)
825-
self._current_tab_index = -1
814+
if isinstance(previous_widget, PlotWidget):
815+
self._default_tab_for_dimensionality[self._prev_key_dimensionality] = (
816+
previous_widget
817+
)
818+
current_widget = self._default_tab_for_dimensionality[
819+
key_def.dimensionality
820+
]
826821

827822
if current_widget not in available_widgets and available_widgets:
828823
current_widget = available_widgets[0]
829824

830825
self._central_tab.setCurrentWidget(current_widget)
831826
self._central_tab.currentChanged.connect(self.current_tab_changed)
832-
self._prev_tab_widget_index = self._central_tab.currentIndex()
833827
self._prev_key_dimensionality = key_def.dimensionality
834828
self._prev_key = key_def.key
835829
self._prev_key_origin = key_def.metadata.get("data_origin")

tests/ert/ui_tests/gui/test_main_window.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
from ert.gui.main import ErtMainWindow, GUILogHandler, _setup_main_window
4242
from ert.gui.main_window import SidebarToolButton
4343
from ert.gui.plotting.plot_window import (
44-
GEN_KW_DEFAULT,
45-
RESPONSE_DEFAULT,
4644
PlotApi,
4745
PlotWindow,
4846
)
@@ -394,15 +392,15 @@ def get_log_checkbox():
394392

395393
# check default selections
396394
click_plotter_item(response_index)
397-
assert plot_window._central_tab.currentIndex() == RESPONSE_DEFAULT
395+
assert plot_window._central_tab.currentIndex() == tab_index_by_text("Ensemble")
398396

399397
# no log scale checkbox yet
400398
cb = get_log_checkbox()
401399
assert cb is not None
402400
assert not cb.isVisible()
403401

404402
click_plotter_item(gen_kw_index)
405-
assert plot_window._central_tab.currentIndex() == GEN_KW_DEFAULT
403+
assert plot_window._central_tab.currentIndex() == tab_index_by_text("Histogram")
406404

407405
# alter selections
408406
click_plotter_item(response_index)

tests/ert/unit_tests/gui/tools/plot/test_plot_window.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from ert.gui.plotting.utils import PlotConfig, PlotContext
2525
from ert.gui.plotting.utils.plot_maps import (
2626
DISTRIBUTION,
27+
ENSEMBLE,
28+
ERT_PLOT_MAP,
2729
GAUSSIAN_KDE,
2830
HISTOGRAM,
2931
STATISTICS,
@@ -683,6 +685,104 @@ def test_that_log_scale_state_is_preserved_when_switching_plot_tabs(
683685
assert log_checkbox.isChecked()
684686

685687

688+
def _plot_window_with_response_and_gen_kw_keys(
689+
qtbot: QtBot, monkeypatch: pytest.MonkeyPatch
690+
) -> PlotWindow:
691+
mock_plot_api_cls = MagicMock(spec=PlotApi)
692+
mock_plot_api = MagicMock(spec=PlotApi)
693+
mock_plot_api_cls.return_value = mock_plot_api
694+
695+
storage_version = "0.0"
696+
mock_plot_api.api_version = storage_version
697+
monkeypatch.setattr(
698+
"ert.gui.plotting.plot_window.get_storage_api_version",
699+
lambda: storage_version,
700+
)
701+
monkeypatch.setattr("ert.gui.plotting.plot_window.PlotApi", mock_plot_api_cls)
702+
703+
mock_plot_api.responses_api_key_defs = [
704+
PlotApiKeyDefinition(
705+
"POLY_RES",
706+
index_type="VALUE",
707+
metadata={"data_origin": "gen_data"},
708+
observations=False,
709+
dimensionality=2,
710+
response=MagicMock(type="gen_data"),
711+
)
712+
]
713+
mock_plot_api.parameters_api_key_defs = [
714+
PlotApiKeyDefinition(
715+
"gen_kw",
716+
index_type=None,
717+
metadata={"data_origin": "GEN_KW"},
718+
observations=False,
719+
dimensionality=1,
720+
parameter=GenKwConfig(
721+
name="gen_kw",
722+
distribution={"name": "uniform", "min": 0, "max": 1},
723+
),
724+
)
725+
]
726+
mock_plot_api.has_history_data.return_value = False
727+
mock_plot_api.get_all_ensembles.return_value = []
728+
729+
plot_window = PlotWindow(config_file="", ens_path=Path(), parent=None)
730+
qtbot.addWidget(plot_window)
731+
plot_window.show()
732+
return plot_window
733+
734+
735+
def _select_data_type_key(plot_window: PlotWindow, key: str) -> None:
736+
filter_model = plot_window._data_type_keys_widget.filter_model
737+
for row in range(filter_model.rowCount()):
738+
index = filter_model.index(row, 0)
739+
if str(filter_model.data(index)) == key:
740+
plot_window._data_type_keys_widget.data_type_keys_widget.setCurrentIndex(
741+
index
742+
)
743+
return
744+
raise AssertionError(f"Data type key '{key}' not found")
745+
746+
747+
def _current_tab_name(plot_window: PlotWindow) -> str:
748+
return plot_window._central_tab.tabText(plot_window._central_tab.currentIndex())
749+
750+
751+
def test_that_default_plot_tab_is_unaffected_by_plot_map_ordering(
752+
qtbot: QtBot, monkeypatch: pytest.MonkeyPatch
753+
) -> None:
754+
# Reversing the plot map changes every tab position, so a default tab
755+
# resolved by position would land on the wrong plot type.
756+
monkeypatch.setattr(
757+
"ert.gui.plotting.plot_window.ERT_PLOT_MAP",
758+
dict(reversed(list(ERT_PLOT_MAP.items()))),
759+
)
760+
761+
plot_window = _plot_window_with_response_and_gen_kw_keys(qtbot, monkeypatch)
762+
763+
_select_data_type_key(plot_window, "POLY_RES")
764+
_select_data_type_key(plot_window, "gen_kw")
765+
assert _current_tab_name(plot_window) == HISTOGRAM
766+
767+
_select_data_type_key(plot_window, "POLY_RES")
768+
assert _current_tab_name(plot_window) == ENSEMBLE
769+
770+
771+
def test_that_plot_tab_last_used_for_a_data_type_is_restored_when_returning_to_it(
772+
qtbot: QtBot, monkeypatch: pytest.MonkeyPatch
773+
) -> None:
774+
plot_window = _plot_window_with_response_and_gen_kw_keys(qtbot, monkeypatch)
775+
776+
_select_data_type_key(plot_window, "gen_kw")
777+
plot_window._central_tab.setCurrentWidget(
778+
plot_window._find_widget_by_name(GAUSSIAN_KDE)
779+
)
780+
781+
_select_data_type_key(plot_window, "POLY_RES")
782+
_select_data_type_key(plot_window, "gen_kw")
783+
assert _current_tab_name(plot_window) == GAUSSIAN_KDE
784+
785+
686786
@pytest.mark.parametrize("tab_name", [HISTOGRAM, DISTRIBUTION, GAUSSIAN_KDE])
687787
@pytest.mark.parametrize(
688788
("values", "expected_visible"),

0 commit comments

Comments
 (0)