bimtester: fix gui, works with feature file now instead of feature directory

This commit is contained in:
Bernd Hahnebach
2021-11-29 10:22:47 +01:00
parent f529bc37ee
commit 1a5b5dd303
+103 -52
View File
@@ -1,6 +1,11 @@
import os import os
import json
import sys import sys
import webbrowser
import bimtester.reports
import bimtester.run import bimtester.run
from PySide2 import QtCore from PySide2 import QtCore
from PySide2 import QtGui from PySide2 import QtGui
from PySide2 import QtWidgets from PySide2 import QtWidgets
@@ -14,9 +19,25 @@ def run():
class GuiWidgetBimTester(QtWidgets.QWidget): class GuiWidgetBimTester(QtWidgets.QWidget):
def __init__(self, args=[]): def __init__(self, args=None):
super(GuiWidgetBimTester, self).__init__() super(GuiWidgetBimTester, self).__init__()
self.args = args
if args is not None:
self.args = args
else:
self.args = {
"action": "",
"advanced_arguments": "",
"console": False, # has to be False to get a report file
"feature": "",
"ifc": "",
"path": "",
"report": "",
"steps": "",
"schema_file": "",
"schema_name": "",
"lang": "",
}
self._setup_ui() self._setup_ui()
@@ -26,7 +47,9 @@ class GuiWidgetBimTester(QtWidgets.QWidget):
def _setup_ui(self): def _setup_ui(self):
package_path = os.path.dirname(os.path.realpath(__file__)) package_path = os.path.dirname(os.path.realpath(__file__))
iconpath = os.path.join(package_path, "resources", "icons", "bimtester.ico") iconpath = os.path.join(
package_path, "resources", "icons", "bimtester.ico"
)
""" """
# as svg # as svg
@@ -54,23 +77,22 @@ class GuiWidgetBimTester(QtWidgets.QWidget):
_ifcfile_browse_btn.setText("...") _ifcfile_browse_btn.setText("...")
_ifcfile_browse_btn.clicked.connect(self.select_ifcfile) _ifcfile_browse_btn.clicked.connect(self.select_ifcfile)
# feature files path # feature file
# use a layout with a frame and a title, see solver framework tp _featurefile_label = QtWidgets.QLabel("Feature file", self)
# beside button self.featurefile_text = QtWidgets.QLineEdit()
ffifc_str = "Feature files in a directory 'features' beside the IFC file." _featurefile_browse_btn = QtWidgets.QToolButton()
featuredirfromifc_label = QtWidgets.QLabel(ffifc_str, self) _featurefile_browse_btn.setText("...")
_featurefile_browse_btn.clicked.connect(self.select_featurefile)
# path browser and line edit
_ffdir_str = "Feature files directory. " "'features' directory has to be in there."
_featurefilesdir_label = QtWidgets.QLabel(_ffdir_str, self)
self.featurefilesdir_text = QtWidgets.QLineEdit()
self.feafilesdir_browse_btn = QtWidgets.QToolButton()
self.feafilesdir_browse_btn.setText("...")
self.feafilesdir_browse_btn.clicked.connect(self.select_featurefilesdir)
# buttons # buttons
self.run_button = QtWidgets.QPushButton(QtGui.QIcon.fromTheme("document-new"), "Run") self.run_button = QtWidgets.QPushButton(
self.close_button = QtWidgets.QPushButton(QtGui.QIcon.fromTheme("window-close"), "Close") QtGui.QIcon.fromTheme("document-new"),
"Run"
)
self.close_button = QtWidgets.QPushButton(
QtGui.QIcon.fromTheme("window-close"),
"Close"
)
self.run_button.clicked.connect(self.run_bimtester) self.run_button.clicked.connect(self.run_bimtester)
self.close_button.clicked.connect(self.close_widget) self.close_button.clicked.connect(self.close_widget)
_buttons = QtWidgets.QHBoxLayout() _buttons = QtWidgets.QHBoxLayout()
@@ -81,17 +103,15 @@ class GuiWidgetBimTester(QtWidgets.QWidget):
layout = QtWidgets.QGridLayout() layout = QtWidgets.QGridLayout()
layout.addWidget(theicon, 1, 0, alignment=QtCore.Qt.AlignRight) layout.addWidget(theicon, 1, 0, alignment=QtCore.Qt.AlignRight)
layout.addWidget(featuredirfromifc_label, 2, 0) layout.addWidget(_featurefile_label, 2, 0)
layout.addWidget(self.featurefile_text, 3, 0)
layout.addWidget(_featurefile_browse_btn, 3, 1)
layout.addWidget(_featurefilesdir_label, 3, 0) layout.addWidget(_ifcfile_label, 4, 0)
layout.addWidget(self.featurefilesdir_text, 4, 0) layout.addWidget(self.ifcfile_text, 5, 0)
layout.addWidget(self.feafilesdir_browse_btn, 4, 1) layout.addWidget(_ifcfile_browse_btn, 5, 1)
layout.addWidget(_ifcfile_label, 5, 0) layout.addLayout(_buttons, 6, 0)
layout.addWidget(self.ifcfile_text, 6, 0)
layout.addWidget(_ifcfile_browse_btn, 6, 1)
layout.addLayout(_buttons, 7, 0)
# row stretches by 10 compared to the others, std is 0 # row stretches by 10 compared to the others, std is 0
# first parameter is the row number # first parameter is the row number
# second is the stretch factor. # second is the stretch factor.
@@ -99,51 +119,82 @@ class GuiWidgetBimTester(QtWidgets.QWidget):
self.setLayout(layout) self.setLayout(layout)
def select_ifcfile(self): def select_ifcfile(self):
ifcfile = QtWidgets.QFileDialog.getOpenFileName(self, dir=self.get_ifcfile())[0] ifcfile = QtWidgets.QFileDialog.getOpenFileName(
self, dir=self.get_ifcfile()
)[0]
self.set_ifcfile(ifcfile) self.set_ifcfile(ifcfile)
def set_ifcfile(self, a_file): def set_ifcfile(self, a_file):
self.ifcfile_text.setText(a_file) self.ifcfile_text.setText(a_file)
def get_ifcfile(self): def get_ifcfile(self):
return self.ifcfile_text.text() # get rid of all spaces, new lines etc, might because of copy the text
# https://stackoverflow.com/a/37001613
return " ".join(self.ifcfile_text.text().split())
def select_featurefilesdir(self): def select_featurefile(self):
thedir = self.featurefilesdir_text.text() featurefile = QtWidgets.QFileDialog.getOpenFileName(
features_path = QtWidgets.QFileDialog.getExistingDirectory( self, dir=self.get_ifcfile()
self, )[0]
caption="Choose features directory ...", self.set_featurefile(featurefile)
dir=thedir,
options=QtWidgets.QFileDialog.HideNameFilterDetails,
)
self.set_featurefilesdir(features_path)
def set_featurefilesdir(self, a_directory): def set_featurefile(self, a_file):
self.featurefilesdir_text.setText(a_directory) self.featurefile_text.setText(a_file)
def get_featurefilesdir(self): def get_featurefile(self):
return self.featurefilesdir_text.text() # get rid of all spaces, new lines etc, might because of copy the text
# https://stackoverflow.com/a/37001613
return " ".join(self.featurefile_text.text().split())
def run_bimtester(self): def run_bimtester(self):
print("Run BIMTester by the GUI") print("Run BIMTester by the GUI")
QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
the_features_path = self.get_featurefilesdir() # set the_feature file and ifc file in args
print(the_features_path) the_featurefile = self.get_featurefile()
# get ifc file
the_ifcfile = self.get_ifcfile() the_ifcfile = self.get_ifcfile()
print(the_ifcfile) if os.path.isfile(the_featurefile):
self.args["feature"] = the_featurefile
has_feature_file = True
else:
print("Feature file does not exist: {}".format(the_featurefile))
# TODO Qt messagebox
has_feature_file = False
if os.path.isfile(the_ifcfile):
self.args["ifc"] = the_ifcfile
has_ifc_file = True
else:
print("IFC file does not exist: {}".format(the_ifcfile))
# TODO Qt messagebox
has_ifc_file = False
print(json.dumps(self.args, indent=4))
# overwrite the_features_path and ifcfile in args # run bimtester
patched_args = self.args if has_feature_file is True and has_ifc_file is True:
patched_args["featuresdir"] = the_features_path report_json = bimtester.run.TestRunner(
patched_args["ifcfile"] = the_ifcfile self.args["ifc"],
self.args["schema_file"]
).run(self.args)
bimtester.run.TestRunner("file.ifc").run({}) # create html report
if os.path.isfile(report_json):
report_html = os.path.join(
os.path.dirname(os.path.realpath(report_json)),
"report.html"
)
bimtester.reports.ReportGenerator().generate(
report_json,
report_html
)
print("HTML report generated: {}".format(report_html))
else:
print("JSON report file does not exist: {}".format(report_json))
QtWidgets.QApplication.restoreOverrideCursor() QtWidgets.QApplication.restoreOverrideCursor()
if os.path.isfile(report_html):
webbrowser.open(report_html)
def close_widget(self): def close_widget(self):
self.close() self.close()