"""
This widget contains the functionality of a startup dialog. The widget
can be executed as a standalone version.
########################################
Functionalities provided in this widget:
########################################
1. Choose a username to create a folder where the data is saved.
Defaults to Tenshinhan.
2. Choose a hardware properties file for the given setup/laboratory that
you are using
"""
if __name__ == "__main__":
# Add directories to path for imports
import os, sys, inspect
currentdir = os.path.dirname(
os.path.abspath(inspect.getfile(inspect.currentframe()))
)
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, os.path.join(currentdir, "ui_files"))
sys.path.insert(0, parentdir)
# PyQt imports
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtCore import QRunnable, QThreadPool
# Import RegEx input validators
from regex_validators import *
# Import of file made from UI designer
from ui_startupdialog import Ui_Dialog_startup as Ui_startup
[docs]class DialogStartup(QtWidgets.QDialog, Ui_startup):
def __init__(self, *args, default_json_path: str = None, obj=None, **kwargs):
super(QtWidgets.QDialog, self).__init__(*args, **kwargs)
self.setupUi(self)
self.setWindowTitle("Jagan - A. Kondratiev, A.R. Thun, G. Wille")
self.setWindowIcon(QtGui.QIcon("gui/icon.png"))
# Placeholder username
self.username = "Tenshinhan"
self.lineEdit_username.setText(self.username)
if default_json_path:
self.default_json_path = default_json_path
else:
self.default_json_path = os.getcwd()
# Set default path
self.lineEdit_json.setText(self.default_json_path)
# Setting up signals
self.lineEdit_username.editingFinished.connect(self.update_username)
self.pushButton_ok.clicked.connect(self.update_accept)
self.pushButton_cancel.clicked.connect(self.update_cancel)
# Open Dialogs
self.pushButton_browse_json.clicked.connect(self.open_hardware_json)
[docs] def update_username(self):
self.username = self.lineEdit_username.text()
[docs] def open_hardware_json(self):
starting_path = self.default_json_path
fileName = QFileDialog.getOpenFileName(
self, "Open Json Hardware File", starting_path, "json files (*.json)"
)
# If a path exists, set the lineEdit to the file, chosen in the
# browse dialog
if fileName[0] != "":
self.lineEdit_json.setText(fileName[0])
elif ".json" not in fileName[0]:
QtWidgets.QMessageBox.warning(
self, "Error", "The chosen file is not a JSON-File"
)
self.hardware_config_path = fileName[0]
[docs] def update_accept(self):
self.hardware_config_path = self.lineEdit_json.text()
if ".json" not in self.hardware_config_path:
QtWidgets.QMessageBox.warning(
self, "Error", "The chosen file is not a JSON-File"
)
elif self.username != "" and self.lineEdit_json.text() != "":
self.accept()
else:
QtWidgets.QMessageBox.warning(
self, "Error", "Empty Username or Path to JSON-File"
)
[docs] def update_cancel(self):
self.close()
if __name__ == "__main__":
# TESTING -----------------------------------------------------------
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Please god let this work too")
app = QtWidgets.QApplication(sys.argv)
dialog = DialogStartup()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
print(dialog.username)
print(dialog.hardware_config_path)
window = MainWindow()
window.show()
app.exec()