-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmainwindow.py
164 lines (134 loc) · 6.52 KB
/
mainwindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from gui.views.py_files.mainwindow import Ui_MainWindow
from gui.models.data_storage import DataStorage
from gui.calls.tabs.soctab import SocTab
from gui.calls.tabs.reducedspacetab import ReducedSpaceTab
from gui.calls.tabs.optimizationtab import OptimizationTab
from gui.calls.tabs.metamodeltab import MetamodelTab
from gui.calls.tabs.loadsimtab import LoadSimTab
from gui.calls.tabs.hessianextractiontab import HessianExtractionTab
from gui.calls.tabs.doetab import DoeTab
from PyQt5.QtWidgets import (QApplication, QComboBox, QFileDialog,
QItemDelegate, QLineEdit, QMainWindow,
QMessageBox)
from PyQt5.QtCore import QAbstractItemModel, QRegExp, Qt
import pathlib
import sys
# append directory to sys path
sys.path.append(str(pathlib.Path(__file__).resolve().parent))
class MainWindow(QMainWindow):
_DOETAB_IDX = 1
_METAMODELTAB_IDX = 2
_OPTTAB_IDX = 3
_REDSPACETAB_IDX = 4
_HESSIANTAB_IDX = 5
_SOCTAB_IDX = 6
def __init__(self):
# ------------------------- UI Initialization -------------------------
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setWindowState(Qt.WindowMaximized)
self.setWindowTitle('Metacontrol - untitled.mtc')
maintab = self.ui.tabMainWidget
# disable the tabs
maintab.setTabEnabled(self._DOETAB_IDX, False)
maintab.setTabEnabled(self._METAMODELTAB_IDX, False)
maintab.setTabEnabled(self._OPTTAB_IDX, False)
maintab.setTabEnabled(self._HESSIANTAB_IDX, False)
maintab.setTabEnabled(self._SOCTAB_IDX, False)
# ------------------------ Internal variables -------------------------
self.application_database = DataStorage()
# ----------------------- Load the tabs widgets -----------------------
self.tab_loadsim = LoadSimTab(self.application_database,
parent_tab=self.ui.simulationTab)
self.tab_doe = DoeTab(self.application_database,
parent_tab=self.ui.samplingTab)
self.tab_metamodel = MetamodelTab(self.application_database,
parent_tab=self.ui.metamodelTab)
self.tab_optimization = OptimizationTab(self.application_database,
parent_tab=self.ui.optimizationTab)
self.tab_reducedspace = ReducedSpaceTab(self.application_database,
parent_tab=self.ui.reducedspaceTab)
self.tab_hessianext = HessianExtractionTab(self.application_database,
parent_tab=self.ui.hessianextractionTab)
self.tab_soc = SocTab(self.application_database,
parent_tab=self.ui.socTab)
# ------------------------ Actions connections ------------------------
self.ui.actionOpen.triggered.connect(self.open_file)
self.ui.actionSave.triggered.connect(self.save_file)
self.ui.actionSave_As.triggered.connect(self.save_file_as)
# --------------------------- Signals/Slots ---------------------------
# sampling tab enabled
self.application_database.sampling_enabled.connect(
self.on_sampling_enabled)
# metamodel tab enabled
self.application_database.metamodel_enabled.connect(
self.on_metamodel_enabled)
# optimization tab enabled
self.application_database.metamodel_enabled.connect(
self.on_optimization_enabled)
# hessian tab enabled
self.application_database.hessian_enabled.connect(
self.on_hessianextraction_enabled)
# soc tab enabled
self.application_database.soc_enabled.connect(
self.on_soc_enabled
)
def open_file(self):
"""Prompts the user to select which .mtc file to open.
"""
homedir = pathlib.Path().home()
mtc_filename, _ = \
QFileDialog.getOpenFileName(self,
"Select the .mtc file to open.",
str(homedir),
"Metacontrol files (*.mtc)")
if mtc_filename != "":
# the user provided a valid file (did not canceled the dialog).
self.application_database.load(mtc_filename)
# update the window title
self.setWindowTitle("Metacontrol - " + mtc_filename)
def save_file(self):
"""Saves the current application configuration as is. If it is a new
file prompts the user to select the location and name of the .mtc file.
"""
current_mtc_name = self.windowTitle().split('Metacontrol - ')[1]
if pathlib.Path(current_mtc_name).is_file():
# file exists, save it
self.application_database.save(current_mtc_name)
else:
# new file, prompt the user
self.save_file_as()
def save_file_as(self):
"""Prompts the user to select location and name of the .mtc file to
save.
"""
dialog_title = "Select the name and where to save the .mtc file"
filetype = "Metacontrol files (*.mtc)"
homedir = pathlib.Path().home()
mtc_filepath, _ = QFileDialog.getSaveFileName(self,
dialog_title,
str(homedir),
filetype)
if mtc_filepath != '':
# change the window title
self.setWindowTitle("Metacontrol - " + mtc_filepath)
# save the file
self.application_database.save(mtc_filepath)
def on_sampling_enabled(self, is_enabled):
self.ui.tabMainWidget.setTabEnabled(self._DOETAB_IDX, is_enabled)
def on_metamodel_enabled(self, is_enabled):
self.ui.tabMainWidget.setTabEnabled(self._METAMODELTAB_IDX, is_enabled)
def on_optimization_enabled(self, is_enabled):
self.ui.tabMainWidget.setTabEnabled(self._OPTTAB_IDX, is_enabled)
def on_hessianextraction_enabled(self, is_enabled):
self.ui.tabMainWidget.setTabEnabled(self._HESSIANTAB_IDX, is_enabled)
def on_soc_enabled(self, is_enabled):
self.ui.tabMainWidget.setTabEnabled(self._SOCTAB_IDX, is_enabled)
if __name__ == "__main__":
from gui.calls.base import my_exception_hook
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.excepthook = my_exception_hook
sys.exit(app.exec_())