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 Oct 31, 2024
1 parent d294930 commit f48a33c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
30 changes: 25 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 @@ -531,6 +531,26 @@ def restart_failed_realizations(self) -> None:
self.kill_button.setVisible(True)
self.run_experiment(restart=True)

def _on_finished(self) -> None:
self.flag_simulation_done = True
for file_dialog in self.findChildren(FileDialog):
file_dialog.close()


class CopyDebugInfoButton(QPushButton):
def __init__(self, on_click: Callable[[], None]):
QPushButton.__init__(self, "Copy Debug Info")
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.setText("Copied...")
on_click()
QTimer.singleShot(1000, lambda: self.setText("Copy Debug Info"))

self.clicked.connect(alternate_button_text_on_click_and_call_callback)


# Cannot use a non-static method here as
# it is called when the object is destroyed
Expand Down
21 changes: 21 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,21 @@
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)
initial_button_text = button.text()
assert initial_button_text == "Copy Debug Info"
qtbot.mouseClick(button, Qt.MouseButton.LeftButton)
assert button.text() == "Copied..."
qtbot.wait_until(lambda: button.text() == initial_button_text, timeout=2000)
assert button_clicked

0 comments on commit f48a33c

Please sign in to comment.