From 3b1db99eb00cc8c783063eed20c2e53fa13ec991 Mon Sep 17 00:00:00 2001 From: Jonathan Karlsen Date: Thu, 31 Oct 2024 15:11:41 +0100 Subject: [PATCH] Have CopyDebugInfoButton change text to `Copied...` on click This commit refactors the copydebuginfobutton in run_dialog.py into its own class, and makes it change text `Copy Debug Info" -> `Copied...` when clicked while running the callback passed to it. After one second, it changes it text back to `Copy Debug Info`. --- src/ert/gui/simulation/run_dialog.py | 35 ++++++++++++++++--- .../ertwidgets/test_copy_debug_info_button.py | 23 ++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 tests/ert/unit_tests/gui/ertwidgets/test_copy_debug_info_button.py diff --git a/src/ert/gui/simulation/run_dialog.py b/src/ert/gui/simulation/run_dialog.py index da485d73af1..986afa3a057 100644 --- a/src/ert/gui/simulation/run_dialog.py +++ b/src/ert/gui/simulation/run_dialog.py @@ -3,7 +3,7 @@ import logging from pathlib import Path from queue import SimpleQueue -from typing import Optional +from typing import Callable, Optional from qtpy.QtCore import QModelIndex, QSize, Qt, QThread, QTimer, Signal, Slot from qtpy.QtGui import ( @@ -224,10 +224,10 @@ def __init__( self.kill_button = QPushButton("Terminate experiment") self.restart_button = QPushButton("Rerun failed") self.restart_button.setHidden(True) - self.copy_debug_info_button = QPushButton("Copy Debug Info") - self.copy_debug_info_button.setToolTip("Copies useful information to clipboard") - self.copy_debug_info_button.clicked.connect(self.produce_clipboard_debug_info) - self.copy_debug_info_button.setObjectName("copy_debug_info_button") + + self.copy_debug_info_button = CopyDebugInfoButton( + on_click=self.produce_clipboard_debug_info.emit + ) size = 20 spin_movie = QMovie("img:loading.gif") @@ -532,6 +532,31 @@ def restart_failed_realizations(self) -> None: self.run_experiment(restart=True) +class CopyDebugInfoButton(QPushButton): + _initial_text = "Copy Debug Info" + _clicked_text = "Copied..." + + def __init__(self, on_click: Callable[[], None]): + QPushButton.__init__(self, CopyDebugInfoButton._initial_text) + self.setToolTip("Copies useful information to clipboard") + self.setObjectName("copy_debug_info_button") + self.setFixedWidth(140) + + def alternate_button_text_on_click_and_call_callback() -> None: + self._alternate_button_text() + on_click() + QTimer.singleShot(1000, self._alternate_button_text) + + self.clicked.connect(alternate_button_text_on_click_and_call_callback) + + def _alternate_button_text(self) -> None: + self.setText( + CopyDebugInfoButton._initial_text + if self.text() == CopyDebugInfoButton._clicked_text + else CopyDebugInfoButton._clicked_text + ) + + # Cannot use a non-static method here as # it is called when the object is destroyed # https://stackoverflow.com/questions/16842955 diff --git a/tests/ert/unit_tests/gui/ertwidgets/test_copy_debug_info_button.py b/tests/ert/unit_tests/gui/ertwidgets/test_copy_debug_info_button.py new file mode 100644 index 00000000000..dca0b5ce655 --- /dev/null +++ b/tests/ert/unit_tests/gui/ertwidgets/test_copy_debug_info_button.py @@ -0,0 +1,23 @@ +from pytestqt.qtbot import QtBot +from qtpy.QtCore import Qt + +from ert.gui.simulation.run_dialog import CopyDebugInfoButton + + +def test_copy_debug_info_button_alterates_text_when_pressed(qtbot: QtBot): + button_clicked = False + + def on_click(): + nonlocal button_clicked + button_clicked = True + + button = CopyDebugInfoButton(on_click=on_click) + qtbot.addWidget(button) + + assert button.text() == CopyDebugInfoButton._initial_text + qtbot.mouseClick(button, Qt.MouseButton.LeftButton) + assert button.text() == CopyDebugInfoButton._clicked_text + qtbot.wait_until( + lambda: button.text() == CopyDebugInfoButton._initial_text, timeout=2000 + ) + assert button_clicked