Skip to content

Commit

Permalink
Handle button visual indications based on actions
Browse files Browse the repository at this point in the history
Use SidebarToolButton instead of QToolButton
  • Loading branch information
andreas-el committed Jan 10, 2025
1 parent d833851 commit 4480295
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
27 changes: 20 additions & 7 deletions src/ert/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import functools
import webbrowser

from qtpy.QtCore import QSize, Qt, Signal, Slot
from qtpy.QtCore import QCoreApplication, QEvent, QSize, Qt, Signal, Slot
from qtpy.QtGui import QCloseEvent, QCursor, QIcon, QMouseEvent
from qtpy.QtWidgets import (
QAction,
Expand Down Expand Up @@ -50,16 +50,16 @@
BUTTON_STYLE_SHEET_LIGHT: str = (
BUTTON_STYLE_SHEET
+ """
QToolButton:hover {background-color: rgba(50, 50, 50, 90);}
QToolButton:hover {background-color: rgba(50, 50, 50, 50);}
QToolButton:checked {background-color: rgba(50, 50, 50, 120);}
"""
)

BUTTON_STYLE_SHEET_DARK: str = (
BUTTON_STYLE_SHEET
+ """
QToolButton:hover {background-color: rgba(30, 30, 30, 150);}
QToolButton:checked {background-color: rgba(30, 30, 30, 120);}
QToolButton:hover {background-color: rgba(20, 20, 20, 90);}
QToolButton:checked {background-color: rgba(20, 20, 20, 150);}
"""
)

Expand Down Expand Up @@ -118,16 +118,17 @@ def __init__(
self._experiment_panel: ExperimentPanel | None = None
self._plot_window: PlotWindow | None = None
self._manage_experiments_panel: ManageExperimentsPanel | None = None
self._add_sidebar_button(
self.simulation_button = self._add_sidebar_button(
"Start simulation", QIcon("img:library_add.svg")
).click()
)
plot_button = self._add_sidebar_button("Create plot", QIcon("img:timeline.svg"))
plot_button.setToolTip("Right click to open external window")
self._add_sidebar_button("Manage experiments", QIcon("img:build_wrench.svg"))
self.results_button = self._add_sidebar_button(
"Simulation status", QIcon("img:in_progress.svg")
)
self.results_button.setEnabled(False)
self.simulation_button.click()
self.run_dialog_counter = 0

self.vbox_layout.addStretch()
Expand Down Expand Up @@ -190,6 +191,16 @@ def select_central_widget(self) -> None:
for i, widget in self.central_panels_map.items():
widget.setVisible(i == index_name)

if index_name not in [
button.text().replace("\n", " ")
for button in self.button_group.buttons()
]:
self.results_button.setChecked(True)

@Slot()
def onMenuAboutToHide(self) -> None:
QCoreApplication.sendEvent(self.results_button, QEvent(QEvent.Type.Leave))

@Slot(object)
def slot_add_widget(self, run_dialog: RunDialog) -> None:
for widget in self.central_panels_map.values():
Expand Down Expand Up @@ -219,8 +230,10 @@ def add_sim_run_option(simulation_id: str) -> None:
if self.run_dialog_counter == 2:
# swap from button to menu selection
self.results_button.clicked.disconnect(self.select_central_widget)
self.results_button.setMenu(QMenu())
menu = QMenu()
self.results_button.setMenu(menu)
self.results_button.setPopupMode(QToolButton.InstantPopup)
menu.aboutToHide.connect(self.onMenuAboutToHide)

for prev_date_time, widget in self.central_panels_map.items():
if isinstance(widget, RunDialog):
Expand Down
23 changes: 21 additions & 2 deletions tests/ert/ui_tests/gui/test_main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,12 +782,17 @@ def test_that_simulation_status_button_adds_menu_on_subsequent_runs(
def find_and_click_button(
button_name: str, should_click: bool, expected_enabled_state: bool
):
button = gui.findChild(QToolButton, button_name)
button = gui.findChild(SidebarToolButton, button_name)
assert button
assert button.isEnabled() == expected_enabled_state
if should_click:
qtbot.mouseClick(button, Qt.LeftButton)

def find_and_check_selected(button_name: str, expected_selected_state: bool):
button = gui.findChild(SidebarToolButton, button_name)
assert button
assert button.isChecked() == expected_selected_state

def run_experiment():
run_experiment_panel = wait_for_child(gui, qtbot, ExperimentPanel)
qtbot.wait_until(lambda: not run_experiment_panel.isHidden(), timeout=5000)
Expand Down Expand Up @@ -817,7 +822,9 @@ def wait_for_simulation_completed():
qtbot.wait_until(lambda: not run_dialog.isHidden(), timeout=5000)

# verify no drop menu
button_simulation_status = gui.findChild(QToolButton, "button_Simulation_status")
button_simulation_status = gui.findChild(
SidebarToolButton, "button_Simulation_status"
)
assert button_simulation_status.menu() is None

find_and_click_button("button_Start_simulation", True, True)
Expand All @@ -832,6 +839,18 @@ def wait_for_simulation_completed():
QTimer.singleShot(500, lambda: handle_run_path_dialog(gui, qtbot, True))
run_experiment()
wait_for_simulation_completed()

# click on something else just to shift focus
find_and_click_button("button_Start_simulation", True, True)
# verify correct button in focus
find_and_check_selected("button_Start_simulation", True)
find_and_check_selected("button_Simulation_status", False)

assert len(button_simulation_status.menu().actions()) == 3
for choice in button_simulation_status.menu().actions():
assert "Single realization test-run" in choice.text()

# verify correct button in focus when selecting from drop-down
choice.trigger()
find_and_check_selected("button_Start_simulation", False)
find_and_check_selected("button_Simulation_status", True)

0 comments on commit 4480295

Please sign in to comment.