Skip to content

Commit

Permalink
refactor: simplify controlflow of menu onclick handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dynobo committed Feb 9, 2025
1 parent 01307e4 commit 2570c0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
24 changes: 10 additions & 14 deletions normcap/gui/menu_button.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Create the settings button and its menu."""

import sys
from typing import Any, Optional, Union
from typing import Optional, Union

from PySide6 import QtCore, QtGui, QtWidgets

Expand Down Expand Up @@ -134,14 +134,10 @@ def _show_message_box(self, text: str) -> None:
message_box.exec()

@QtCore.Slot(QtGui.QAction) # type: ignore # pyside typhint bug?
def on_item_click(self, action: QtGui.QAction) -> None:
# TODO: Reduce Cyclomatic Complexity
def on_item_click(self, action: QtGui.QAction) -> None: # noqa: PLR0911
action_name = action.objectName()
group = action.actionGroup()
group_name = group.objectName() if group else None
value: Optional[Any] = None

# Menu items which trigger actions

if action_name == "close":
self.com.on_close_in_settings.emit("Clicked close in settings")
Expand Down Expand Up @@ -170,20 +166,20 @@ def on_item_click(self, action: QtGui.QAction) -> None:
self.com.on_open_url.emit(action_name)
return

# Menu items which change settings
if group_name in ["settings_group", "detection_group"]:
setting = action_name
value = action.isChecked()
self.settings.setValue(action_name, action.isChecked())
self.com.on_setting_change.emit(action_name)
return

if group_name == "language_group":
setting = "language"
languages = [a.objectName() for a in group.actions() if a.isChecked()]
if not languages:
# If all languages are unselected, keep the currently clicked active:
languages = [action_name]
action.setChecked(True)
value = languages

self.settings.setValue(str(setting), value)
self.com.on_setting_change.emit(str(setting))
self.settings.setValue("language", languages)
self.com.on_setting_change.emit("language")
return

@QtCore.Slot()
def populate_menu_entries(self) -> None:
Expand Down
10 changes: 4 additions & 6 deletions tests/tests_gui/test_update_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,11 @@ def _mocked_downloader_get(cls, url: str, timeout: float):
monkeypatch.setattr(downloader.Downloader, "get", _mocked_downloader_get)

# WHEN the update check is triggered (with debug log level)
with (
caplog.at_level(logging.DEBUG),
qtbot.wait_signal(
with caplog.at_level(logging.DEBUG): # noqa: SIM117 # Otherwise ast false positive
with qtbot.wait_signal(
checker.com.on_version_checked, timeout=200, raising=False
) as result,
):
checker.com.check.emit()
) as result:
checker.com.check.emit()

# THEN the update message is (not) shown with certain text
# and certain messages are logged
Expand Down

0 comments on commit 2570c0c

Please sign in to comment.