Skip to content

Commit acbd663

Browse files
committed
Add copy all button to suggestor
1 parent 5677188 commit acbd663

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

src/ert/gui/ertwidgets/suggestor/suggestor.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
QVBoxLayout,
2020
QWidget,
2121
)
22+
from typing_extensions import override
2223

2324
from ert.gui import is_dark_mode
2425

26+
from .. import CopyButton
2527
from ._colors import BLUE_TEXT
2628
from ._suggestor_message import SuggestorMessage
2729

@@ -105,6 +107,32 @@ def _combine_messages(infos: Sequence[ErrorInfo]) -> list[tuple[str, list[str]]]
105107
"""
106108

107109

110+
class _CopyAllButton(CopyButton):
111+
def __init__(
112+
self,
113+
errors: list[ErrorInfo],
114+
warnings: list[WarningInfo],
115+
deprecations: list[WarningInfo],
116+
) -> None:
117+
super().__init__()
118+
self.setText(" Copy all messages")
119+
self.all_messages = "\n\n".join(
120+
[
121+
f"{info.message}" + (f"\n{info.location()}" if info.location() else "")
122+
for info in (errors + warnings + deprecations)
123+
]
124+
)
125+
self.setStyleSheet(SECONDARY_BUTTON_STYLE)
126+
127+
@override
128+
def copy(self) -> None:
129+
logger.info(
130+
"Copy all button in Suggestor used. "
131+
f"Copied {len(self.all_messages)} characters"
132+
)
133+
self.copy_text(self.all_messages)
134+
135+
108136
class Suggestor(QWidget):
109137
def __init__(
110138
self,
@@ -147,9 +175,15 @@ def __init__(
147175

148176
action_buttons = QWidget(parent=self)
149177
action_buttons_layout = QHBoxLayout()
150-
action_buttons_layout.insertStretch(-1, -1)
151178
action_buttons_layout.setContentsMargins(0, 24, 0, 0)
152179

180+
if any([errors, warnings, deprecations]):
181+
action_buttons_layout.addWidget(
182+
_CopyAllButton(errors, warnings, deprecations)
183+
)
184+
185+
action_buttons_layout.addStretch()
186+
153187
if continue_action:
154188
action_buttons_layout.addWidget(self._continue_button())
155189

tests/ert/unit_tests/gui/test_suggestor.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import re
2+
13
import pytest
2-
from PyQt6.QtWidgets import QWidget
4+
from PyQt6.QtWidgets import QApplication, QPushButton, QWidget
35

4-
from ert.config import ErrorInfo
6+
from ert.config import ErrorInfo, WarningInfo
57
from ert.gui.ertwidgets import Suggestor
8+
from ert.gui.ertwidgets.suggestor.suggestor import _CopyAllButton
69

710

811
@pytest.mark.parametrize(
@@ -21,3 +24,34 @@ def test_suggestor_combines_errors_with_the_same_message(qtbot, errors, expected
2124
msg_layout = msgs.layout()
2225
assert msg_layout is not None
2326
assert msg_layout.count() == expected_num
27+
28+
29+
def test_that_copy_all_button_concatenates_errors_warnings_and_deprecations(qtbot):
30+
errors = [ErrorInfo("error", filename="script.py", line="5")]
31+
warnings = [WarningInfo("warning", filename="script.py", line="6")]
32+
deprecations = [
33+
WarningInfo("deprecation", filename="script.py", line="7", is_deprecation=True)
34+
]
35+
suggestor = Suggestor(errors, warnings, deprecations, lambda: None)
36+
37+
all_buttons = suggestor.findChildren(QPushButton)
38+
copy_all_button = next(
39+
button for button in all_buttons if isinstance(button, _CopyAllButton)
40+
)
41+
copy_all_button.click()
42+
43+
expected_clipboard = """error
44+
script.py: Line 5
45+
46+
warning
47+
script.py: Line 6
48+
49+
deprecation
50+
script.py: Line 7"""
51+
52+
def remove_whitespaces(s: str) -> str:
53+
return re.sub(r"\s+", "", s)
54+
55+
assert remove_whitespaces(QApplication.clipboard().text()) == remove_whitespaces(
56+
expected_clipboard
57+
)

0 commit comments

Comments
 (0)