Skip to content

Commit

Permalink
Have CopyDebugInfoButton change text to Copied... on click
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
jonathan-eq committed Nov 1, 2024
1 parent 8c312f2 commit 3b1db99
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/ert/gui/simulation/run_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/ert/unit_tests/gui/ertwidgets/test_copy_debug_info_button.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3b1db99

Please sign in to comment.