Fixing bugs with opening relative files, adding checks for file, and header for files by default.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Adsooi 2021-07-20 15:05:06 +02:00
parent c772d1e82c
commit de4f08682d
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
2 changed files with 25 additions and 7 deletions

View file

@ -1 +1 @@
{"xzoom":100,"yzoom":485,"xmin":-1,"ymax":1,"xaxisstep":"1","yaxisstep":"0.1","xaxislabel":"ω (rad/s)","yaxislabel":"G (dB)","logscalex":false,"linewidth":2,"showxgrad":true,"showygrad":true,"textsize":22,"history":[[["EditedProperty",["h","Function","labelX",3,2.5,false]],["EditedProperty",["h","Function","labelPosition","above-left","above",false]],["EditedProperty",["h","Function","labelPosition","above","left",false]],["EditedProperty",["h","Function","labelX",2.5,2,false]],["EditedProperty",["h","Function","labelX",2,2.2,false]],["EditedProperty",["h","Function","labelX",2.2,9,false]]],[]],"width":1908,"height":1011,"objects":{"Sequence":[["u",true,"#ff5500","name + value",true,true,{"1":"cos(u[n])"},{"0":"-1"},"above-left",8]],"Function":[["f",true,"#2b156d","name + value","integral((x + 1), x, \"cos (t)\", \"t\")","ℝ⁺*","","application","left",7,true,true],["g",true,"#ac539c","name + value","0.739083","","","application","below",-1,true,false],["h",true,"#00aa00","name + value","derivative(\"cos t\", \"t\", x)","","","application","left",9,true,true]],"Repartition":[["X",true,"#1E1EAF","name",true,true,{"0":"0.2","1":"0.1","2":"0.2"},"above",2.5]],"Point":[["A",true,"#060759","name + value","4","(-0.5)","top","●"]]},"type":"logplotv1"} LPFv1{"xzoom":100,"yzoom":485,"xmin":-1,"ymax":1,"xaxisstep":"1","yaxisstep":"0.1","xaxislabel":"ω (rad/s)","yaxislabel":"G (dB)","logscalex":false,"linewidth":2,"showxgrad":true,"showygrad":true,"textsize":22,"history":[[["EditedProperty",["h","Function","labelX",3,2.5,false]],["EditedProperty",["h","Function","labelPosition","above-left","above",false]],["EditedProperty",["h","Function","labelPosition","above","left",false]],["EditedProperty",["h","Function","labelX",2.5,2,false]],["EditedProperty",["h","Function","labelX",2,2.2,false]],["EditedProperty",["h","Function","labelX",2.2,9,false]]],[]],"width":1908,"height":1005,"objects":{"Sequence":[["u",true,"#ff5500","name + value",true,true,{"1":"cos(u[n])"},{"0":"-1"},"above-left",8]],"Function":[["f",true,"#2b156d","name + value","integral((x + 1), x, \"cos (t)\", \"t\")","ℝ⁺*","","application","left",7,true,true],["g",true,"#ac539c","name + value","0.739083","","","application","below",-1,true,false],["h",true,"#00aa00","name + value","derivative(\"cos t\", \"t\", x)","","","application","left",9,true,true]],"Repartition":[["X",true,"#1E1EAF","name",true,true,{"0":"0.2","1":"0.1","2":"0.2"},"above",2.5]],"Point":[["A",true,"#060759","name + value","4","(-0.5)","top","●"]]},"type":"logplotv1"}

View file

@ -25,13 +25,14 @@ from PySide2 import __version__ as PySide2_version
import os import os
import tempfile import tempfile
from platform import release as os_release from platform import release as os_release
from json import dumps from json import dumps, loads
from sys import platform, argv, version as sys_version from sys import platform, argv, version as sys_version
import webbrowser import webbrowser
__VERSION__ = "0.0.1.dev0" __VERSION__ = "0.0.1.dev0"
tempfile = tempfile.mkstemp(suffix='.png')[1] tempfile = tempfile.mkstemp(suffix='.png')[1]
pwd = os.getcwd()
def get_linux_theme(): def get_linux_theme():
des = { des = {
@ -50,19 +51,39 @@ class Helper(QObject):
@Slot(str, str) @Slot(str, str)
def write(self, filename, filedata): def write(self, filename, filedata):
os.chdir(pwd)
if os.path.exists(os.path.dirname(os.path.realpath(filename))): if os.path.exists(os.path.dirname(os.path.realpath(filename))):
if filename.split(".")[-1] == "lpf":
# Add header to file
filedata = "LPFv1" + filedata
f = open(os.path.realpath(filename), 'w', -1, 'utf8') f = open(os.path.realpath(filename), 'w', -1, 'utf8')
f.write(filedata) f.write(filedata)
f.close() f.close()
os.chdir(os.path.dirname(os.path.realpath(__file__)))
@Slot(str, result=str) @Slot(str, result=str)
def load(self, filename): def load(self, filename):
os.chdir(pwd)
data = '{}'
if os.path.exists(os.path.realpath(filename)): if os.path.exists(os.path.realpath(filename)):
f = open(os.path.realpath(filename), 'r', -1, 'utf8') f = open(os.path.realpath(filename), 'r', -1, 'utf8')
data = f.read() data = f.read()
f.close() f.close()
return data try:
return '{}' if data[:5] == "LPFv1":
# V1 version of the file
data = data[5:]
elif data[0] == "{" and "type" in loads(data) and loads(data)["type"] == "logplotv1":
pass
else:
raise Exception("Invalid LogarithmPlotter file.")
except Exception as e: # If file can't be loaded
from PySide2.QtWidgets import QMessageBox
QMessageBox.warning(None, 'LogarithmPlotter', 'Could not open file "{}":\n{}'.format(filename, e), QMessageBox.Ok) # Cannot parse file
else:
QMessageBox.warning(None, 'LogarithmPlotter', 'Could not open file: "{}"\nFile does not exist.'.format(filename), QMessageBox.Ok) # Cannot parse file
os.chdir(os.path.dirname(os.path.realpath(__file__)))
return data
@Slot(result=str) @Slot(result=str)
def gettmpfile(self): def gettmpfile(self):
@ -91,7 +112,6 @@ class Helper(QObject):
webbrowser.open(url) webbrowser.open(url)
def run(): def run():
pwd = os.getcwd()
os.chdir(os.path.dirname(os.path.realpath(__file__))) os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.environ["QT_QUICK_CONTROLS_STYLE"] = { os.environ["QT_QUICK_CONTROLS_STYLE"] = {
@ -114,10 +134,8 @@ def run():
engine.addImportPath(os.path.realpath(os.path.join(os.getcwd(), "qml"))) engine.addImportPath(os.path.realpath(os.path.join(os.getcwd(), "qml")))
engine.load(os.path.realpath(os.path.join(os.getcwd(), "qml", "eu", "ad5001", "LogarithmPlotter", "LogarithmPlotter.qml"))) engine.load(os.path.realpath(os.path.join(os.getcwd(), "qml", "eu", "ad5001", "LogarithmPlotter", "LogarithmPlotter.qml")))
os.chdir(pwd)
if len(argv) > 0 and os.path.exists(argv[-1]) and argv[-1].split('.')[-1] in ['json', 'lgg', 'lpf']: if len(argv) > 0 and os.path.exists(argv[-1]) and argv[-1].split('.')[-1] in ['json', 'lgg', 'lpf']:
engine.rootObjects()[0].loadDiagram(argv[-1]) engine.rootObjects()[0].loadDiagram(argv[-1])
os.chdir(os.path.dirname(os.path.realpath(__file__)))
if not engine.rootObjects(): if not engine.rootObjects():
print("No root object") print("No root object")