LogarithmPlotter/LogarithmPlotter/logarithmplotter.py

160 lines
5.9 KiB
Python
Raw Normal View History

2021-07-31 22:49:44 +00:00
"""
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
2024-01-10 23:11:09 +00:00
* Copyright (C) 2021-2024 Ad5001
2021-07-31 22:49:44 +00:00
*
* 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/>.
"""
from time import time
2023-05-21 22:15:09 +00:00
from PySide6.QtWidgets import QApplication
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtCore import Qt, QTranslator, QLocale
from PySide6.QtGui import QIcon
2021-07-31 22:49:44 +00:00
from tempfile import TemporaryDirectory
2021-08-26 15:48:12 +00:00
from os import getcwd, chdir, environ, path, remove, close
2021-07-31 22:49:44 +00:00
from platform import release as os_release
2021-08-26 16:16:26 +00:00
from sys import platform, argv, version as sys_version, exit
from sys import path as sys_path
start_time = time()
# Create the temporary directory for saving copied screenshots and latex files
tempdir = TemporaryDirectory()
tmpfile = path.join(tempdir.name, 'graph.png')
pwd = getcwd()
chdir(path.dirname(path.realpath(__file__)))
if path.realpath(path.join(getcwd(), "..")) not in sys_path:
sys_path.append(path.realpath(path.join(getcwd(), "..")))
from LogarithmPlotter import __VERSION__
from LogarithmPlotter.util import config, native
from LogarithmPlotter.util.update import check_for_updates
from LogarithmPlotter.util.helper import Helper
2022-03-05 17:19:20 +00:00
from LogarithmPlotter.util.latex import Latex
2024-03-29 16:53:10 +00:00
from LogarithmPlotter.util.js import PyJSValue
2021-07-31 22:49:44 +00:00
config.init()
2021-07-31 22:49:44 +00:00
2024-03-29 16:53:10 +00:00
2021-07-31 22:49:44 +00:00
def get_linux_theme():
des = {
"KDE": "Fusion",
"gnome": "Basic",
"lxqt": "Fusion",
"mate": "Fusion",
2021-07-31 22:49:44 +00:00
}
if "XDG_SESSION_DESKTOP" in environ:
return des[environ["XDG_SESSION_DESKTOP"]] if environ["XDG_SESSION_DESKTOP"] in des else "Fusion"
2021-07-31 22:49:44 +00:00
else:
# Android
return "Material"
2024-03-29 16:53:10 +00:00
2021-07-31 22:49:44 +00:00
def run():
if not 'QT_QUICK_CONTROLS_STYLE' in environ:
environ["QT_QUICK_CONTROLS_STYLE"] = {
"linux": get_linux_theme(),
"freebsd": get_linux_theme(),
"win32": "Universal" if os_release == "10" else "Fusion",
"cygwin": "Fusion",
2023-05-26 12:54:28 +00:00
"darwin": "macOS"
}[platform]
2024-03-29 16:53:10 +00:00
dep_time = time()
2024-03-29 16:53:10 +00:00
print("Loaded dependencies in " + str((dep_time - start_time) * 1000) + "ms.")
2021-07-31 22:49:44 +00:00
icon_fallbacks = QIcon.fallbackSearchPaths();
2022-01-30 00:37:46 +00:00
base_icon_path = path.join(getcwd(), "qml", "eu", "ad5001", "LogarithmPlotter", "icons")
icon_fallbacks.append(path.realpath(path.join(base_icon_path, "common")))
icon_fallbacks.append(path.realpath(path.join(base_icon_path, "objects")))
icon_fallbacks.append(path.realpath(path.join(base_icon_path, "history")))
icon_fallbacks.append(path.realpath(path.join(base_icon_path, "settings")))
icon_fallbacks.append(path.realpath(path.join(base_icon_path, "settings", "custom")))
2021-07-31 22:49:44 +00:00
QIcon.setFallbackSearchPaths(icon_fallbacks);
2024-03-29 16:53:10 +00:00
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
2021-07-31 22:49:44 +00:00
app = QApplication(argv)
app.setApplicationName("LogarithmPlotter")
app.setDesktopFileName("eu.ad5001.LogarithmPlotter.desktop")
2021-07-31 22:49:44 +00:00
app.setOrganizationName("Ad5001")
app.styleHints().setShowShortcutsInContextMenus(True)
app.setWindowIcon(QIcon(path.realpath(path.join(getcwd(), "logarithmplotter.svg"))))
2024-03-29 16:53:10 +00:00
# Installing translators
translator = QTranslator()
# Check if lang is forced.
2024-03-29 16:53:10 +00:00
forcedlang = [p for p in argv if p[:7] == "--lang="]
locale = QLocale(forcedlang[0][7:]) if len(forcedlang) > 0 else QLocale()
2024-03-29 16:53:10 +00:00
if translator.load(locale, "lp", "_", path.realpath(path.join(getcwd(), "i18n"))):
app.installTranslator(translator);
2024-03-29 16:53:10 +00:00
2021-08-16 20:05:51 +00:00
# Installing macOS file handler.
macOSFileOpenHandler = None
if platform == "darwin":
macOSFileOpenHandler = native.MacOSFileOpenHandler()
app.installEventFilter(macOSFileOpenHandler)
2024-03-29 16:53:10 +00:00
2021-07-31 22:49:44 +00:00
engine = QQmlApplicationEngine()
2022-01-27 21:05:53 +00:00
global tmpfile
helper = Helper(pwd, tmpfile)
latex = Latex(tempdir)
2024-03-29 16:53:10 +00:00
js_globals = PyJSValue(engine.globalObject())
js_globals.Modules = engine.newObject()
js_globals.Helper = engine.newQObject(helper)
js_globals.Latex = engine.newQObject(latex)
2021-07-31 22:49:44 +00:00
engine.rootContext().setContextProperty("TestBuild", "--test-build" in argv)
engine.rootContext().setContextProperty("StartTime", dep_time)
2024-03-29 16:53:10 +00:00
app.translate("About", "About LogarithmPlotter")
# FOR SOME REASON, if this isn't included, Qt refuses to load the QML file.
2021-07-31 22:49:44 +00:00
engine.addImportPath(path.realpath(path.join(getcwd(), "qml")))
engine.load(path.realpath(path.join(getcwd(), "qml", "eu", "ad5001", "LogarithmPlotter", "LogarithmPlotter.qml")))
2021-07-31 22:49:44 +00:00
if not engine.rootObjects():
print("No root object", path.realpath(path.join(getcwd(), "qml")))
print(path.realpath(path.join(getcwd(), "qml", "eu", "ad5001", "LogarithmPlotter", "LogarithmPlotter.qml")))
exit(-1)
2022-01-27 21:05:53 +00:00
# Open the current diagram
chdir(pwd)
if len(argv) > 0 and path.exists(argv[-1]) and argv[-1].split('.')[-1] in ['lpf']:
2024-03-29 16:53:10 +00:00
js_globals.Modules.IO.loadDiagram(argv[-1])
chdir(path.dirname(path.realpath(__file__)))
2024-03-29 16:53:10 +00:00
2021-08-16 20:05:51 +00:00
if platform == "darwin":
2024-03-29 16:53:10 +00:00
macOSFileOpenHandler.init_io(js_globals.Modules.IO)
# Check for LaTeX installation if LaTeX support is enabled
if config.getSetting("enable_latex"):
latex.check_latex_install()
2024-03-29 16:53:10 +00:00
# Check for updates
if config.getSetting("check_for_updates"):
check_for_updates(__VERSION__, engine.rootObjects()[0])
2024-03-29 16:53:10 +00:00
2023-10-07 17:11:19 +00:00
exit_code = app.exec()
2024-03-29 16:53:10 +00:00
tempdir.cleanup()
config.save()
2021-07-31 22:49:44 +00:00
exit(exit_code)
2024-03-29 16:53:10 +00:00
2021-07-31 22:49:44 +00:00
if __name__ == "__main__":
run()