diff --git a/LogarithmPlotter/logarithmplotter.py b/LogarithmPlotter/logarithmplotter.py index 0072aec..3d98e7b 100644 --- a/LogarithmPlotter/logarithmplotter.py +++ b/LogarithmPlotter/logarithmplotter.py @@ -44,7 +44,7 @@ if path.realpath(path.join(getcwd(), "..")) not in sys_path: sys_path.append(path.realpath(path.join(getcwd(), ".."))) -from LogarithmPlotter import config, __VERSION__ +from LogarithmPlotter import config, native, __VERSION__ from LogarithmPlotter.update import check_for_updates config.init() @@ -165,6 +165,13 @@ def run(): app.setApplicationName("LogarithmPlotter") app.setOrganizationName("Ad5001") app.setWindowIcon(QIcon(path.realpath(path.join(getcwd(), "logarithmplotter.svg")))) + + # Installing macOS file handler. + macOSFileOpenHandler = None + if platform == "darwin": + macOSFileOpenHandler = native.MacOSFileOpenHandler() + app.installEventFilter(macOSFileOpenHandler) + engine = QQmlApplicationEngine() helper = Helper() engine.rootContext().setContextProperty("Helper", helper) @@ -183,6 +190,9 @@ def run(): print("No root object") exit(-1) + if platform == "darwin": + macOSFileOpenHandler.init_graphics(engine.rootObjects()[0]) + # Check for updates if config.getSetting("check_for_updates"): check_for_updates(__VERSION__, engine.rootObjects()[0]) diff --git a/LogarithmPlotter/native.py b/LogarithmPlotter/native.py new file mode 100644 index 0000000..dc7797d --- /dev/null +++ b/LogarithmPlotter/native.py @@ -0,0 +1,50 @@ +""" + * LogarithmPlotter - Create graphs with logarithm scales. + * Copyright (C) 2021 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" + +# This file contains stuff for native interactions with each OS. + +from PySide2.QtCore import QObject, QEvent + +# On macOS, opening a file through finder can only be fetched through the +# QFileOpenEvent and NOT throught command line parameters. +class MacOSFileOpenHandler(QObject): + def __init__(self): + self.initilized = False + self.mainwindow = None + self.opened_file = "" + QObject.__init__(self) + + def init_graphics(self, mainwindow): + self.mainwindow = mainwindow + self.initilized = True + if self.opened_file != "": + self.open_file() + + def open_file(self): + self.mainwindow.loadDiagram(self.opened_file) + + def eventFilter(self, obj, event): + if event.type() == QEvent.FileOpen: + print("Got file", event.file(), self.initilized) + self.opened_file = event.file() + if self.initilized: + self.open_file() + return True + else: + # standard event processing + return QObject.eventFilter(self, obj, event)