Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify controlflow of menu onclick handler #683

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
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
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