Skip to content

Commit

Permalink
Refactor Suggestor
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Dec 22, 2023
1 parent 85bd657 commit 99b76a3
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 264 deletions.
1 change: 0 additions & 1 deletion src/ert/gui/ertwidgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ def wrapper(*arg):
from .customdialog import CustomDialog # noqa
from .summarypanel import SummaryPanel # noqa
from .pathchooser import PathChooser # noqa
from .suggestor_message import SuggestorMessage # noqa
264 changes: 7 additions & 257 deletions src/ert/gui/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import functools
import logging
import os
import warnings
Expand All @@ -7,24 +6,13 @@
from typing import Optional, cast

import pkg_resources
from PyQt5.QtGui import QCursor, QIcon
from PyQt5.QtGui import QIcon
from qtpy.QtCore import QDir, QLocale, Qt
from qtpy.QtWidgets import (
QApplication,
QFrame,
QGridLayout,
QHBoxLayout,
QLabel,
QPushButton,
QScrollArea,
QVBoxLayout,
QWidget,
)
from qtpy.QtWidgets import QApplication

from ert.config import ConfigValidationError, ConfigWarning, ErtConfig
from ert.enkf_main import EnKFMain
from ert.gui.about_dialog import AboutDialog
from ert.gui.ertwidgets import SuggestorMessage, SummaryPanel
from ert.gui.ertwidgets import SummaryPanel
from ert.gui.main_window import ErtMainWindow
from ert.gui.simulation import SimulationPanel
from ert.gui.tools.event_viewer import (
Expand All @@ -46,6 +34,8 @@
from ert.storage import open_storage
from ert.storage.local_storage import local_storage_set_ert_config

from .suggestor import Suggestor


def run_gui(args: Namespace, plugin_manager: Optional[ErtPluginManager] = None):
# Replace Python's exception handler for SIGINT with the system default.
Expand Down Expand Up @@ -127,7 +117,7 @@ def _start_initial_gui_window(
error_messages += error.errors
logger.info("Error in config file shown in gui: '%s'", str(error))
return (
_setup_suggester(
Suggestor(
error_messages,
config_warnings,
deprecations,
Expand Down Expand Up @@ -162,7 +152,7 @@ def _start_initial_gui_window(
_main_window = _setup_main_window(ert, args, log_handler, plugin_manager)
if deprecations or config_warnings:
return (
_setup_suggester(
Suggestor(
error_messages,
config_warnings,
deprecations,
Expand Down Expand Up @@ -212,246 +202,6 @@ def _clicked_about_button(about_dialog):
about_dialog.show()


BUTTON_STYLE = """
QPushButton {
background-color: #007079;
color: white;
border-radius: 4px;
border: 2px solid #045e8d;
height: 36px;
padding: 0px 16px 0px 16px;
}
QPushButton:hover {
background: #004f55;
};
"""

LINK_STYLE = """
QPushButton {
color: #045e8d;
border: 0px solid white;
margin-left: 34px;
height: 36px;
padding: 0px 12px 0px 12px;
text-decoration: underline;
text-align: left;
font-size: 16px;
padding: 0px;
}
"""

DISABLED_BUTTON_STYLE = """
background-color: #eaeaea;
color: #bebebe;
border-radius: 4px;
border: 2px solid #eaeaea;
height: 36px;
padding: 0px 16px 0px 16px;
"""

SECONDARY_BUTTON_STYLE = """
QPushButton {
background-color: #f7f7f7;
color: #007079;
border-radius: 4px;
border: 2px solid #007079;
height: 36px;
padding: 0px 16px 0px 16px;
}
QPushButton:hover {
background-color: #deedee;
};
"""


def _setup_suggester(
errors,
warning_msgs,
suggestions,
ert_window=None,
plugin_manager: Optional[ErtPluginManager] = None,
):
container = QWidget()
container.setLayout(QVBoxLayout())
data_widget = QWidget()
container.layout().addWidget(
QLabel(
"""\
<p style="font-size: 28px;">Some problems detected</p>
<p> The following problems were detected while reading
the ert configuration file. </p>
"""
)
)
container.setWindowTitle("ERT")
container.layout().addWidget(data_widget)
container.resize(1440, 1024)
container.setStyleSheet("background-color: #f7f7f7;")
container.layout().setContentsMargins(32, 47, 32, 16)
container.layout().setSpacing(32)

if ert_window is not None:
data_widget.notifier = ert_window.notifier
data_widget.setLayout(QHBoxLayout())
data_widget.layout().setSpacing(16)
data_widget.layout().setContentsMargins(0, 0, 0, 0)

help_button_frame = QFrame()
help_button_frame.setContentsMargins(0, 0, 0, 0)
help_button_frame.setStyleSheet(
"background-color: #eaeaea; border-radius: 4px; border: 2px solid #dcdcdc"
)
help_button_frame.setMinimumWidth(388)
help_button_frame.setMaximumWidth(388)
help_buttons_layout = QVBoxLayout()
help_buttons_layout.setContentsMargins(0, 30, 20, 20)
help_button_frame.setLayout(help_buttons_layout)

help_links = plugin_manager.get_help_links() if plugin_manager else {}

help_header = QLabel("Helpful links")
help_header.setContentsMargins(0, 0, 0, 0)
help_header.setStyleSheet(
"font-size: 24px; color: #045e8d; border: none; margin-left: 30px;"
)
help_buttons_layout.addWidget(help_header, alignment=Qt.AlignTop)

separator = QFrame()
separator.setFrameShape(QFrame.HLine)
separator.setStyleSheet("color: #dcdcdc;")
separator.setFixedWidth(388)
help_buttons_layout.addWidget(separator)

for menu_label, link in help_links.items():
button = QPushButton(menu_label)
button.setStyleSheet(LINK_STYLE)
button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
button.setObjectName(menu_label)
button.clicked.connect(
functools.partial(_clicked_help_button, menu_label, link)
)
help_buttons_layout.addWidget(button)

about_button = QPushButton("About")
about_button.setStyleSheet(LINK_STYLE)
about_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
about_button.setObjectName("about_button")
help_buttons_layout.addWidget(about_button)
help_buttons_layout.addStretch(-1)

diag = AboutDialog(data_widget)
about_button.clicked.connect(lambda: _clicked_about_button(diag))

suggest_msgs = QWidget()
suggest_msgs.setContentsMargins(0, 0, 16, 0)
buttons = QWidget()
suggest_layout = QGridLayout()
suggest_layout.setContentsMargins(0, 0, 0, 0)
suggest_layout.setColumnMinimumWidth(0, 450)
suggest_layout.setSpacing(24)
buttons_layout = QHBoxLayout()

column = 0
row = 0
num = 0
for msg in errors:
suggest_layout.addWidget(SuggestorMessage.error_msg(msg), row, column)
if column:
row += 1
column = (column + 1) % 2
num += 1
for msg in warning_msgs:
suggest_layout.addWidget(SuggestorMessage.warning_msg(msg), row, column)
if column:
row += 1
column = (column + 1) % 2
num += 1
for msg in suggestions:
suggest_layout.addWidget(SuggestorMessage.deprecation_msg(msg), row, column)
if column:
row += 1
column = (column + 1) % 2
num += 1
suggest_layout.setRowStretch(row + 1, 1)
width = 1440
height = 1024
if num <= 1:
width -= 450
else:
suggest_layout.setColumnMinimumWidth(1, 450)
suggest_layout.setColumnStretch(2, 1)
if row < 4:
height -= (4 - (row + column)) * 150
container.resize(width, height)

suggest_msgs.setLayout(suggest_layout)
scroll = QScrollArea()
scroll.setStyleSheet(
"""
QScrollArea {
border: none;
width: 128px;
}
QScrollBar {
border: none;
background-color: #f7f7f7;
width: 10px;
}
QScrollBar::handle {
border: none;
background-color: #dcdcdc;
border-radius: 4px;
}
QScrollBar::sub-line:vertical, QScrollBar::add-line:vertical {
background: none;
}
"""
)
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
scroll.setWidgetResizable(True)
scroll.setWidget(suggest_msgs)
scroll.setContentsMargins(0, 0, 0, 0)

def run_pressed():
ert_window.show()
ert_window.activateWindow()
ert_window.raise_()
ert_window.adjustSize()
container.close()

run = QPushButton("Open ERT")
give_up = QPushButton("Cancel")
if ert_window is None:
run.setStyleSheet(DISABLED_BUTTON_STYLE)
run.setEnabled(False)
give_up.setStyleSheet(BUTTON_STYLE)
else:
run.setStyleSheet(BUTTON_STYLE)
run.setEnabled(True)
give_up.setStyleSheet(SECONDARY_BUTTON_STYLE)

run.setObjectName("run_ert_button")
run.pressed.connect(run_pressed)
give_up.pressed.connect(container.close)

buttons_layout.insertStretch(-1, -1)
buttons_layout.setContentsMargins(0, 24, 0, 0)
buttons_layout.addWidget(run)
buttons_layout.addWidget(give_up)

buttons.setLayout(buttons_layout)
problem_area = QWidget()
problem_area.setLayout(QVBoxLayout())
problem_area.setContentsMargins(0, 0, 0, 0)
problem_area.layout().setContentsMargins(0, 0, 0, 0)
problem_area.layout().addWidget(scroll)
problem_area.layout().addWidget(buttons)
data_widget.layout().addWidget(problem_area)
data_widget.layout().addWidget(help_button_frame)
return container


def _setup_main_window(
ert: EnKFMain,
args: Namespace,
Expand Down
3 changes: 3 additions & 0 deletions src/ert/gui/suggestor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .suggestor import Suggestor

__all__ = ["Suggestor"]
15 changes: 15 additions & 0 deletions src/ert/gui/suggestor/_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BLUE_BACKGROUND = "#d5eaf4"
BLUE_TEXT = "#045e8d"
RED_BACKGROUND = "#ffd2d1"
RED_TEXT = "#90042c"
YELLOW_BACKGROUND = "#ffe7d6"
YELLOW_TEXT = "#7f4600"

__all__ = [
"BLUE_BACKGROUND",
"BLUE_TEXT",
"RED_BACKGROUND",
"RED_TEXT",
"YELLOW_BACKGROUND",
"YELLOW_TEXT",
]
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
QWidget,
)

from ._colors import (
BLUE_BACKGROUND,
BLUE_TEXT,
RED_BACKGROUND,
RED_TEXT,
YELLOW_BACKGROUND,
YELLOW_TEXT,
)


def _svg_icon(image_name):
widget = QtSvg.QSvgWidget(f"img:{image_name}.svg")
Expand Down Expand Up @@ -60,17 +69,17 @@ def __init__(self, header, text_color, bg_color, icon, info):
@classmethod
def error_msg(cls, info):
return SuggestorMessage(
"Error: ", "#90042c", "#ffd2d1", _svg_icon("error"), info
"Error: ", RED_TEXT, RED_BACKGROUND, _svg_icon("error"), info
)

@classmethod
def warning_msg(cls, info):
return SuggestorMessage(
"Warning: ", "#7f4600", "#ffe7d6", _svg_icon("warning"), info
"Warning: ", YELLOW_TEXT, YELLOW_BACKGROUND, _svg_icon("warning"), info
)

@classmethod
def deprecation_msg(cls, info):
return SuggestorMessage(
"Deprecation: ", "#045e8d", "#d5eaf4", _svg_icon("bell"), info
"Deprecation: ", BLUE_TEXT, BLUE_BACKGROUND, _svg_icon("bell"), info
)
Loading

0 comments on commit 99b76a3

Please sign in to comment.