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

Application logic to system tray #190

Merged
merged 4 commits into from
Mar 14, 2022
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
2 changes: 1 addition & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class Libs(str, Enum):

# libc = "/usr/lib/libc++.1.dylib"
tesseract = "/usr/local/bin/tesseract"
libtess = "/usr/local/Cellar/tesseract/5.0.1/lib/libtesseract.5.dylib"
libtess = "/usr/local/Cellar/tesseract/5.1.0/lib/libtesseract.5.dylib"
liblept = "/usr/local/opt/leptonica/lib/liblept.5.dylib"
libarchive = "/usr/local/opt/libarchive/lib/libarchive.13.dylib"
libpng = "/usr/local/opt/libpng/lib/libpng16.16.dylib"
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ disable = [
convention = "google"
add-ignore = "D107,D104,D103,D100,D105"


[tool.pytest.ini_options]
addopts = "--cov normcap --cov-report xml:cov.xml --cov-report html"
#addopts = "--cov normcap --cov-report xml:cov.xml --cov-report html"
testpaths = ["src/tests"]
qt_api = "pyside6"
markers = ["skip_on_gh: do not run during CI/CD on github"]
Expand Down
9 changes: 5 additions & 4 deletions src/normcap/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from importlib import metadata, resources

# TODO: Manual test multi screen
# TODO: Slim down packages

# Workaround for older tesseract version 4.0.0 on e.g. Debian Buster
locale.setlocale(locale.LC_ALL, "C")
Expand Down Expand Up @@ -52,7 +51,7 @@ def _set_environ_for_briefcase():
from normcap import __version__
from normcap.args import create_argparser
from normcap.gui import system_info, utils
from normcap.gui.main_window import MainWindow
from normcap.gui.tray import SystemTray

logging.basicConfig(
format="%(asctime)s - %(levelname)-7s - %(name)s:%(lineno)d - %(message)s",
Expand Down Expand Up @@ -91,9 +90,11 @@ def main():
utils.copy_tessdata_files_to_config_dir()

app = QtWidgets.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(True)
app.setQuitOnLastWindowClosed(False)

logger.debug("System info:\n%s", system_info.to_dict())

MainWindow(vars(args)).show()
tray = SystemTray(app, vars(args))
tray.setVisible(True)

sys.exit(app.exec_())
4 changes: 2 additions & 2 deletions src/normcap/gui/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def send_notification(self, capture: Capture):
notification_icon = get_icon(icon_file, "tool-magic-symbolic")

title, message = self.compose_notification(capture)
self.parent().tray.show()
self.parent().tray.showMessage(title, message, notification_icon)
self.parent().show()
self.parent().showMessage(title, message, notification_icon)

# Delay quit or hide to get notification enough time to show up.
delay = 5000 if sys.platform == "win32" else 500
Expand Down
86 changes: 38 additions & 48 deletions src/normcap/gui/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import sys

from PySide6 import QtCore

Expand All @@ -8,50 +7,41 @@
logger = logging.getLogger(__name__)


def init_settings(*args, initial: dict, reset=False) -> QtCore.QSettings:
"""Prepare QT settings.

Apply defaults to missing setting and overwrite with initially
provided settings (e.g. from CLI args).
"""
settings = QtCore.QSettings(*args)
settings.setFallbacksEnabled(False)

if reset:
settings = _remove_all_keys(settings)
settings = _set_missing_to_default(settings, DEFAULT_SETTINGS)
settings = _update_from_dict(settings, initial)
if sys.platform == "darwin":
# TODO: Remove after adding working tray support to MacOS
settings.setValue("tray", "false")
settings.sync()

return settings


def _update_from_dict(settings, update_dict):
for key, value in update_dict.items():
if settings.contains(key):
if value is not None:
settings.setValue(key, value)
elif key in ["reset", "verbose", "very_verbose"]:
continue
else:
logger.debug("Skip update of non existing setting (%s: %s)", key, value)
return settings


def _remove_all_keys(settings):
logger.info("Remove existing settings")
for key in settings.allKeys():
settings.remove(key)
return settings


def _set_missing_to_default(settings, defaults):
for d in defaults:
key, value = d.key, d.value
if key not in settings.allKeys() or (settings.value(key) is None):
logger.debug("Reset settings to (%s: %s)", key, value)
settings.setValue(key, value)
return settings
class Settings(QtCore.QSettings):
"""Customized settings."""

def __init__(self, *args, initial: dict, reset=False):
super().__init__(*args)
self.setFallbacksEnabled(False)

# Do nicer?
if reset:
self.reset()

self._set_missing_to_default(DEFAULT_SETTINGS)
self._update_from_dict(initial)

self.sync()

def reset(self):
"""Remove all existing settings and values."""
logger.info("Remove existing settings")
for key in self.allKeys():
self.remove(key)

def _set_missing_to_default(self, defaults):
for d in defaults:
key, value = d.key, d.value
if key not in self.allKeys() or (self.value(key) is None):
logger.debug("Reset settings to (%s: %s)", key, value)
self.setValue(key, value)

def _update_from_dict(self, settings_dict):
for key, value in settings_dict.items():
if self.contains(key):
if value is not None:
self.setValue(key, value)
elif key in ["reset", "verbose", "very_verbose"]:
continue
else:
logger.debug("Skip update of non existing setting (%s: %s)", key, value)
9 changes: 4 additions & 5 deletions src/normcap/gui/settings_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,17 @@
class Communicate(QtCore.QObject):
"""SettingsMenu' communication bus."""

on_setting_changed = QtCore.Signal(tuple)
on_open_url = QtCore.Signal(str)
on_quit_or_hide = QtCore.Signal(str)


class SettingsMenu(QtWidgets.QToolButton):
"""Button to adjust setting on main window top right."""

def __init__(self, window_main: QtWidgets.QMainWindow):
super().__init__(window_main)
def __init__(self, parent: QtWidgets.QMainWindow, settings: QtCore.QSettings):
super().__init__(parent)
self.setObjectName("settings_icon")
self.settings = window_main.settings
self.settings = settings

self.setCursor(QtCore.Qt.ArrowCursor)
self.setFixedSize(38, 38)
Expand Down Expand Up @@ -150,7 +149,7 @@ def _on_item_click(self, action: QtGui.QAction):
value = languages

if None not in [setting, value]:
self.com.on_setting_changed.emit((setting, value))
self.settings.setValue(str(setting), value)

def _add_settings_section(self, menu):
settings_group = QtGui.QActionGroup(menu)
Expand Down
41 changes: 0 additions & 41 deletions src/normcap/gui/system_tray.py

This file was deleted.

Loading