forked from Tivieta/CENTAUR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
220 lines (181 loc) · 7.31 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2018 Julius Susanto. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
"""
CENTAUR: Hybrid Power System Simulation
Main window
Author: Julius Susanto
Last edited: January 2018
"""
import os, sys
from PyQt4 import QtGui
from gui.gui_solar import solar_ui
from gui.gui_loads import loads_ui
from gui.gui_battery import battery_ui
from gui.gui_gen import gen_ui
from gui.gui_project import project_ui
from gui.gui_sim import sim_ui
import matplotlib.backends.backend_tkagg
import gui.globals as globals
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
globals.init()
self.initUI()
def initUI(self):
"""Set up and initialise main GUI window"""
self.resize(1200, 750)
self.centre()
self.setWindowTitle('CENTAUR')
self.setWindowIcon(QtGui.QIcon('media\sigma.png'))
"""
Tabs
"""
tab_widget = QtGui.QTabWidget()
tab1 = QtGui.QWidget()
tab2 = QtGui.QWidget()
tab3 = QtGui.QWidget()
tab4 = QtGui.QWidget()
tab5 = QtGui.QWidget()
tab6 = QtGui.QWidget()
self.tab_widget = tab_widget
tab_widget.addTab(tab1, "Project")
tab_widget.addTab(tab2, "Loads")
tab_widget.addTab(tab3, "Solar PV")
tab_widget.addTab(tab4, "Battery")
tab_widget.addTab(tab5, "Genset")
tab_widget.addTab(tab6, "Simulation")
self.page1 = project_ui(tab1)
self.page2 = loads_ui(tab2)
self.page3 = solar_ui(tab3)
self.page4 = battery_ui(tab4)
self.page5 = gen_ui(tab5)
self.page6 = sim_ui(tab6)
self.page1.setup(self)
self.page2.setup(self)
self.page3.setup(self)
self.page4.setup(self)
self.page5.setup(self)
self.page6.setup(self)
self.pages = [self.page1, self.page2, self.page3, self.page4, self.page5]
"""
Actions
"""
exitAction = QtGui.QAction(QtGui.QIcon('media\exit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(QtGui.qApp.quit)
saveAsAction = QtGui.QAction(QtGui.QIcon('media\saveas.ico'), 'Save &As', self)
saveAsAction.setShortcut('Ctrl+A')
saveAsAction.setStatusTip('Save project as')
saveAsAction.triggered.connect(self.save_as_fn)
openAction = QtGui.QAction(QtGui.QIcon('media\open.ico'), '&Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Open project')
openAction.triggered.connect(self.open_fn)
aboutAction = QtGui.QAction('&About CENTAUR', self)
aboutAction.setStatusTip('About CENTAUR')
aboutAction.triggered.connect(self.about_dialog)
helpAction = QtGui.QAction('&User Manual', self)
helpAction.setShortcut('F1')
helpAction.setStatusTip('User documentation')
helpAction.triggered.connect(self.user_manual)
"""
Menubar
"""
menu_bar = QtGui.QMenuBar()
fileMenu = menu_bar.addMenu('&File')
fileMenu.addAction(saveAsAction)
fileMenu.addAction(openAction)
fileMenu.addSeparator()
fileMenu.addAction(exitAction)
helpMenu = menu_bar.addMenu('&Help')
helpMenu.addAction(helpAction)
helpMenu.addSeparator()
helpMenu.addAction(aboutAction)
"""
Status line.
"""
self.status_message = QtGui.QStatusBar()
vbox = QtGui.QVBoxLayout()
vbox.addWidget(menu_bar)
vbox.addWidget(tab_widget)
vbox.addWidget(self.status_message)
self.setLayout(vbox)
def centre(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def refresh_data(self):
"""Refresh each page with data from globals variables."""
for p in self.pages:
p.refresh_data()
def show_status_message(self, message, error = False, beep = False):
"""Display a status message on the status line.
If error is True the status text will be coloured red.
If beep is True then the application will beep.
"""
if(error):
self.status_message.setStyleSheet('QStatusBar {color: red}')
else:
self.status_message.setStyleSheet('')
if beep:
QtGui.QApplication.beep()
self.status_message.showMessage(message)
def save_as_fn(self):
"""Function for the Save As action."""
fname = QtGui.QFileDialog.getSaveFileName(self, "Save Data File As", "", "CENTAUR project files (*.ctr)")
if fname:
for p in self.pages:
p.update_data()
if globals.write_project_to_file(fname):
self.show_status_message("Write to file " + fname + " successful.")
self.refresh_data()
else:
self.show_status_message("Failed to save " + fname + ".", error = True, beep = True)
else:
self.show_status_message("Save As cancelled.")
def save_fn(self):
"""Function for the Save action."""
if globals.filename != "":
if globals.write_project_to_file(globals.filename):
self.refresh_data()
else:
self.show_status_message("Failed to save " + globals.filename + ".", error = True, beep = True)
def open_fn(self):
"""Function for the Open action."""
##########################################################
# TODO: Confirmation for opening file if data is unsaved #
##########################################################
fname = QtGui.QFileDialog.getOpenFileName(self, "Open Data File", "", "CENTAUR project files (*.ctr)")
if fname:
if globals.load_project_from_file(fname):
self.refresh_data()
self.show_status_message("File " + fname + " successfully loaded.")
else:
self.show_status_message("Failed to open " + fname + ".", error = True, beep = True)
else:
self.show_status_message("Open Data File cancelled.")
def user_manual(self):
"""Launch user manual (Github link)"""
os.system("start \"\" https://github.com/susantoj/CENTAUR/wiki")
def about_dialog(self):
"""Show About dialog box"""
QtGui.QMessageBox.about(self, "About CENTAUR",
"""<b>CENTAUR</b> is a hybrid power system simulation package.
<p>
Version: <b>v1.0<b><p>
<p>
<p>© 2018 Julius Susanto</p>
<p>All rights reserved.</p>
""")
def main():
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()