Trying to fix opening files in MacOS.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ad5001 2021-08-16 22:05:51 +02:00
parent 9ab61a9c0f
commit 6682b75df3
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
2 changed files with 61 additions and 1 deletions

View file

@ -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])

View file

@ -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 <https://www.gnu.org/licenses/>.
"""
# 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)