Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions modmesh/pilot/_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
from . import airfoil

if _pcore.enable:
from PySide6.QtCore import Qt
from PySide6.QtGui import QAction
from PySide6.QtWidgets import (QApplication, QLabel,
QVBoxLayout, QHBoxLayout,
QGroupBox, QButtonGroup, QPushButton,
QRadioButton, QDialog)
from . import _mesh
from . import _euler1d
from . import _burgers1d
Expand Down Expand Up @@ -67,6 +72,73 @@ def __call__(cls, *args, **kw):
return cls._instances[cls]


class AppearanceDialog(QDialog):
"""
AppearanceDialog class for managing the general look
and feel of Qt widgets.

This class inherits from the QDialog class and provides radio buttons
for seleting color themes.
"""
def __init__(self, parent=None):
super().__init__(parent)
self.qApp = QApplication.instance()
self.init_ui()

def ok(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about naming it on_ok() like the rest of the functions?

controller.on_close_appearance()

def on_click_light_mode(self):
self.qApp.styleHints().setColorScheme(Qt.ColorScheme.Light)

def on_click_dark_mode(self):
self.qApp.styleHints().setColorScheme(Qt.ColorScheme.Dark)

def on_click_system_mode(self):
self.qApp.styleHints().setColorScheme(Qt.ColorScheme.Unknown)

def init_ui(self):
self.setWindowTitle("Appearance")
layout = QVBoxLayout()

layout.addWidget(QLabel("Light/Dark Mode Toggle"))

light_mode_button = QRadioButton("Light")
dark_mode_button = QRadioButton("Dark")
system_mode_button = QRadioButton("System")
light_mode_button.clicked.connect(self.on_click_light_mode)
dark_mode_button.clicked.connect(self.on_click_dark_mode)
system_mode_button.clicked.connect(self.on_click_system_mode)
light_mode_button.setFocusPolicy(Qt.NoFocus)
dark_mode_button.setFocusPolicy(Qt.NoFocus)
system_mode_button.setFocusPolicy(Qt.NoFocus)

color_scheme_name = str(self.qApp.styleHints().colorScheme())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.qApp.styleHints().colorScheme() already returns Qt.ColorScheme. See https://doc.qt.io/qt-6/qt.html#ColorScheme-enum , also available in PySide https://doc.qt.io/qtforpython-6/PySide6/QtCore/Qt.html#PySide6.QtCore.Qt.ColorScheme

Code to compare the enum will be more robust:

scheme = self.qapp.styleHints().colorScheme()
if scheme == Qt.ColorScheme.Light:
    light_mode_button.setChecked(True)
elif scheme == Qt.ColorScheme.Dark:
    dark_mode_button.setChecked(True)
else:  # Qt.ColorScheme.Unknown -> "System"
    system_mode_button.setChecked(True)

You forgot Unknown (System). Please add it back.

We should also use balanced if/elif/else most of the time.

if color_scheme_name == "ColorScheme.Light":
light_mode_button.setChecked(True)
if color_scheme_name == "ColorScheme.Dark":
dark_mode_button.setChecked(True)

button_group = QButtonGroup()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep button_group as a member data, otherwise it will be garbage collected after the end of this init_ui() function (because it is not added to another widget to keep the live).

button_group.addButton(light_mode_button, 0)
button_group.addButton(dark_mode_button, 1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bug: Both dark and system are assigned number 1 and treat the same way. A fix is like:

        button_group.addButton(dark_mode_button, 1)
        button_group.addButton(system_mode_button, 2)

button_group.addButton(system_mode_button, 1)

hbox_1 = QHBoxLayout()
hbox_1.addWidget(light_mode_button)
hbox_1.addWidget(dark_mode_button)
hbox_1.addWidget(system_mode_button)
group_box_1 = QGroupBox()
group_box_1.setLayout(hbox_1)
layout.addWidget(group_box_1)

ok_button = QPushButton("OK")
ok_button.clicked.connect(self.ok)
layout.addWidget(ok_button)

self.setLayout(layout)


class _Controller(metaclass=_Singleton):
def __init__(self):
# Do not construct any Qt member objects before calling launch(), or
Expand All @@ -83,6 +155,18 @@ def __init__(self):
self.canvas = None
self.openprofiledata = None
self.runprofiling = None
self.appearance_open = False
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appearance_open is not used. Remove the dead code.


def on_close_appearance(self):
self.appearance_open = False
if self.appearance_dialog:
self.appearance_dialog.close()

def on_open_appearance(self):
self.appearance_open = True
if not self.appearance_dialog:
self.appearance_dialog = AppearanceDialog()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppearanceDialog is created without a parent, and won't be owned by the main window. Is it intented?

self.appearance_dialog.exec_()

def __getattr__(self, name):
return None if self._rmgr is None else getattr(self._rmgr, name)
Expand Down Expand Up @@ -137,6 +221,8 @@ def _addAction(menu, text, tip, func, checkable=False, checked=False):
self.openprofiledata.populate_menu()
self.runprofiling.populate_menu()

self.appearance_dialog = AppearanceDialog()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you construct AppearanceDialog at two places, here and at line 168?


if sys.platform != 'darwin':
_addAction(
menu=wm.fileMenu,
Expand All @@ -145,6 +231,15 @@ def _addAction(menu, text, tip, func, checkable=False, checked=False):
func=lambda: wm.quit(),
)

_addAction(
menu=wm.windowMenu,
text="Appearance",
tip="Manage the app's look and feel",
func=controller.on_open_appearance,
checkable=False,
checked=False,
)

_addAction(
menu=wm.windowMenu,
text="Console",
Expand Down
Loading