forked from cdens/MultiTabProgram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainprogram.py
executable file
·430 lines (330 loc) · 17.3 KB
/
mainprogram.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# =============================================================================
# Code: mainprogram.py
# Author: Casey R. Densmore, 25JUN2019
#
# Purpose: Creates QMainWindow class with basic functions for a GUI with
# multiple tabs. Requires PyQt5 module (pip instal PyQt5)
#
# General functions within main.py "RunProgram" class of QMainWindow:
# o __init__: Calls functions to initialize GUI
# o initUI: Builds GUI window
# o makenewtab: Creates a new tab window (make all widgets/buttons here)
# o whatTab: gets identifier for open tab
# o renametab: renames open tab
# o setnewtabcolor: sets the background color pattern for new tabs
# o closecurrenttab: closes open tab
# o savedataincurtab: saves data in open tab (saved file types depend on tab type and user preferences)
# o postwarning: posts a warning box specified message
# o posterror: posts an error box with a specified message
# o postwarning_option: posts a warning box with Okay/Cancel options
# o closeEvent: pre-existing function that closes the GUI- function modified to prompt user with an "are you sure" box
#
# =============================================================================
# =============================================================================
# CALL NECESSARY MODULES HERE
# =============================================================================
from sys import argv, exit
from platform import system as cursys
from struct import calcsize
from os import remove, path, listdir
from traceback import print_exc as trace_error
if cursys() == 'Windows':
from ctypes import windll
from shutil import copy as shcopy
from PyQt5.QtWidgets import (QMainWindow, QAction, QApplication, QMenu, QLineEdit, QLabel, QSpinBox, QCheckBox,
QPushButton, QMessageBox, QWidget, QFileDialog, QComboBox, QTextEdit, QTabWidget, QVBoxLayout, QInputDialog,
QGridLayout, QDoubleSpinBox, QTableWidget, QTableWidgetItem, QHeaderView, QProgressBar, QDesktopWidget,
QStyle, QStyleOptionTitleBar)
from PyQt5.QtCore import QObjectCleanupHandler, Qt, pyqtSlot
from PyQt5.QtGui import QIcon, QColor, QPalette, QBrush, QLinearGradient, QFont
from PyQt5.Qt import QThreadPool
# DEFINE CLASS FOR PROGRAM (TO BE CALLED IN MAIN)
class RunProgram(QMainWindow):
# =============================================================================
# INITIALIZE WINDOW, INTERFACE
# =============================================================================
def __init__(self):
super().__init__()
try:
self.initUI() #creates GUI window
self.buildmenu() #Creates interactive menu, options to create tabs and start autoQC
self.makenewtab() #Opens first tab
except Exception:
trace_error()
self.posterror("Failed to initialize the program.")
def initUI(self):
#setting window size
cursize = QDesktopWidget().availableGeometry(self).size()
titleBarHeight = self.style().pixelMetric(QStyle.PM_TitleBarHeight, QStyleOptionTitleBar(), self)
self.resize(cursize.width(), cursize.height()-titleBarHeight)
# setting title/icon, background color
self.setWindowTitle('Blank Multitab GUI Window')
#self.setWindowIcon(QIcon('pathway_to_icon_here.png'))
p = self.palette()
p.setColor(self.backgroundRole(), QColor(255,255,255)) #white background
self.setPalette(p)
#sets app ID to ensure that any additional windows appear under the same tab
if cursys() == 'Windows':
myappid = 'APP_ID' # arbitrary string
windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
#changing font size
font = QFont()
font.setPointSize(11)
font.setFamily("Arial")
self.setFont(font)
# prepping to include tabs
mainWidget = QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QVBoxLayout()
mainWidget.setLayout(mainLayout)
self.tabWidget = QTabWidget()
mainLayout.addWidget(self.tabWidget)
self.myBoxLayout = QVBoxLayout()
self.tabWidget.setLayout(self.myBoxLayout)
self.show()
#setting slash dependent on OS- for file naming and handling
global slash
if cursys() == 'Windows':
slash = '\\'
else:
slash = '/'
#setting up dictionary to store data for each tab
global alltabdata
alltabdata = []
#tab tracking
self.totaltabs = 0
self.tabnumbers = []
# =============================================================================
# BUILD MENU, GENERAL SETTINGS
# =============================================================================
#builds file menu for GUI
def buildmenu(self):
#setting up primary menu bar
menubar = self.menuBar()
FileMenu = menubar.addMenu('Options')
#File>New Profile Editor Tab
newptab = QAction('&New Tab',self)
newptab.setShortcut('Ctrl+N')
newptab.triggered.connect(self.makenewtab)
FileMenu.addAction(newptab)
#File>Rename Current Tab
renametab = QAction('&Rename Current Tab',self)
renametab.setShortcut('Ctrl+R')
renametab.triggered.connect(self.renametab)
FileMenu.addAction(renametab)
#File>Close Current Tab
closetab = QAction('&Close Current Tab',self)
closetab.setShortcut('Ctrl+X')
closetab.triggered.connect(self.closecurrenttab)
FileMenu.addAction(closetab)
#File>Save Files
savedataintab = QAction('&Save',self)
savedataintab.setShortcut('Ctrl+S')
savedataintab.triggered.connect(self.savedataincurtab)
FileMenu.addAction(savedataintab)
# =============================================================================
# SIGNAL PROCESSOR TAB AND INPUTS HERE
# =============================================================================
def makenewtab(self):
try:
newtabnum = self.addnewtab()
#creates dictionary entry for current tab- you can add additional key/value combinations for the opened tab at any point after the dictionary has been initialized
alltabdata.append({"tab":QWidget(),"tablayout":QGridLayout(),
"tabtype":"newtab","testvariable":False})
self.setnewtabcolor(alltabdata[newtabnum]["tab"])
alltabdata[newtabnum]["tablayout"].setSpacing(10)
#creating new tab, assigning basic info
self.tabWidget.addTab(alltabdata[newtabnum]["tab"],'New Tab')
self.tabWidget.setCurrentIndex(newtabnum)
self.tabWidget.setTabText(newtabnum, "New Tab #" + str(self.totaltabs))
alltabdata[newtabnum]["tabnum"] = self.totaltabs #assigning unique, unchanging number to current tab
alltabdata[newtabnum]["tablayout"].setSpacing(10)
#and add new buttons and other widgets
alltabdata[newtabnum]["tabwidgets"] = {}
#making widgets
alltabdata[newtabnum]["tabwidgets"]["sourcetitle"] = QLabel(' Source:') #1
alltabdata[newtabnum]["tabwidgets"]["refresh"] = QPushButton('Refresh') # 2
alltabdata[newtabnum]["tabwidgets"]["options"] = QComboBox() #3
alltabdata[newtabnum]["tabwidgets"]["options"].addItem('Item A')
alltabdata[newtabnum]["tabwidgets"]["options"].addItem('Item B')
alltabdata[newtabnum]["currentoption"] = alltabdata[newtabnum]["tabwidgets"]["options"].currentText()
alltabdata[newtabnum]["tabwidgets"]["sb1title"] = QLabel('Spinbox 1:') #4
alltabdata[newtabnum]["tabwidgets"]["sb2title"] = QLabel('Spinbox 2:') #5
alltabdata[newtabnum]["tabwidgets"]["sb1"] = QSpinBox() #6
alltabdata[newtabnum]["tabwidgets"]["sb1"].setRange(1,99)
alltabdata[newtabnum]["tabwidgets"]["sb1"].setSingleStep(1)
alltabdata[newtabnum]["tabwidgets"]["sb1"].setValue(12)
alltabdata[newtabnum]["tabwidgets"]["sb2"] = QDoubleSpinBox() #7
alltabdata[newtabnum]["tabwidgets"]["sb2"].setRange(20, 30)
alltabdata[newtabnum]["tabwidgets"]["sb2"].setSingleStep(0.25)
alltabdata[newtabnum]["tabwidgets"]["sb2"].setDecimals(2)
alltabdata[newtabnum]["tabwidgets"]["sb2"].setValue(23.5)
#run function every time spinbox value is changed
#alltabdata[newtabnum]["tabwidgets"]["sb2"].valueChanged.connect(self.callbackfunction)
alltabdata[newtabnum]["tabwidgets"]["b1"] = QPushButton('Button 1') #8
alltabdata[newtabnum]["tabwidgets"]["b2"] = QPushButton('Button 2') #9
alltabdata[newtabnum]["tabwidgets"]["b3"] = QPushButton('Button 3') #10
alltabdata[newtabnum]["tabwidgets"]["t1t"] = QLabel('Entry 1:') #11
alltabdata[newtabnum]["tabwidgets"]["t1"] = QLineEdit('E1 example') #12
alltabdata[newtabnum]["tabwidgets"]["t2t"] = QLabel('Entry 2: ') #13
alltabdata[newtabnum]["tabwidgets"]["t2"] = QLineEdit('E2 example') #14
alltabdata[newtabnum]["tabwidgets"]["t3t"] = QLabel('Entry 3: ') #15
alltabdata[newtabnum]["tabwidgets"]["t3"] = QLineEdit('E3 example') #16
alltabdata[newtabnum]["tabwidgets"]["t4t"] = QLabel('Entry 4: ') #17
alltabdata[newtabnum]["tabwidgets"]["t4"] = QLineEdit('E4 example') #18
alltabdata[newtabnum]["tabwidgets"]["t5t"] = QLabel('Entry 5: ') #19
alltabdata[newtabnum]["tabwidgets"]["t5"] = QLineEdit('E5 example') #20
#formatting widgets
alltabdata[newtabnum]["tabwidgets"]["sb1title"].setAlignment(Qt.AlignRight | Qt.AlignVCenter)
alltabdata[newtabnum]["tabwidgets"]["sb2title"].setAlignment(Qt.AlignRight | Qt.AlignVCenter)
alltabdata[newtabnum]["tabwidgets"]["t1t"].setAlignment(Qt.AlignRight | Qt.AlignVCenter)
alltabdata[newtabnum]["tabwidgets"]["t2t"].setAlignment(Qt.AlignRight | Qt.AlignVCenter)
alltabdata[newtabnum]["tabwidgets"]["t3t"].setAlignment(Qt.AlignRight | Qt.AlignVCenter)
alltabdata[newtabnum]["tabwidgets"]["t4t"].setAlignment(Qt.AlignRight | Qt.AlignVCenter)
alltabdata[newtabnum]["tabwidgets"]["t5t"].setAlignment(Qt.AlignRight | Qt.AlignVCenter)
#should be 19 entries
widgetorder = ["sourcetitle","refresh","options","sb1title","sb2title","sb1","sb2",
"b1","b2","b3","t1t","t1","t2t","t2","t3t","t3",
"t4t","t4","t5t","t5"]
wrows = [1,1,2,3,4,3,4,5,5,6,1,1,2,2,3,3,4,4,5,5]
wcols = [2,3,2,2,2,3,3,2,3,3,4,5,4,5,4,5,4,5,4,5]
wrext = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
wcolext = [1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1]
#adding user inputs
for i,r,c,re,ce in zip(widgetorder,wrows,wcols,wrext,wcolext):
alltabdata[newtabnum]["tablayout"].addWidget(alltabdata[newtabnum]["tabwidgets"][i],r,c,re,ce)
#adjusting stretch factors for all rows/columns
colstretch = [5,1,1,1,1,1,1]
for col,cstr in zip(range(0,len(colstretch)),colstretch):
alltabdata[newtabnum]["tablayout"].setColumnStretch(col,cstr)
rowstretch = [1,1,1,1,1,1,1,1,10]
for row,rstr in zip(range(0,len(rowstretch)),rowstretch):
alltabdata[newtabnum]["tablayout"].setRowStretch(row,rstr)
#making the current layout for the tab
alltabdata[newtabnum]["tab"].setLayout(alltabdata[newtabnum]["tablayout"])
except Exception: #if something breaks
trace_error()
self.posterror("Failed to build new tab")
# =============================================================================
# TAB MANIPULATION OPTIONS, OTHER GENERAL FUNCTIONS
# =============================================================================
#handles tab indexing
def addnewtab(self):
#creating numeric ID for newly opened tab
self.totaltabs += 1
self.tabnumbers.append(self.totaltabs)
newtabnum = self.tabWidget.count()
return newtabnum
#gets index of open tab in GUI
def whatTab(self):
return self.tabnumbers[self.tabWidget.currentIndex()]
#renames tab (only user-visible name, not alltabdata dict key)
def renametab(self):
try:
curtab = self.tabWidget.currentIndex()
name, ok = QInputDialog.getText(self, 'Rename Current Tab', 'Enter new tab name:',QLineEdit.Normal,str(self.tabWidget.tabText(curtab)))
if ok:
self.tabWidget.setTabText(curtab,name)
except Exception:
trace_error()
self.posterror("Failed to rename the current tab")
#sets default color scheme for tabs
def setnewtabcolor(self,tab):
p = QPalette()
gradient = QLinearGradient(0, 0, 0, 400)
gradient.setColorAt(0.0, QColor(255,253,253))
#gradient.setColorAt(1.0, QColor(248, 248, 255))
gradient.setColorAt(1.0, QColor(255, 225, 225))
p.setBrush(QPalette.Window, QBrush(gradient))
tab.setAutoFillBackground(True)
tab.setPalette(p)
#closes a tab
def closecurrenttab(self):
try:
reply = QMessageBox.question(self, 'Message',
"Are you sure to close the current tab?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
#getting tab to close
indextoclose = self.tabWidget.currentIndex()
#add any additional necessary commands (stop threads, prevent memory leaks, etc) here
#closing tab
self.tabWidget.removeTab(indextoclose)
#removing current tab data from the alltabdata dict, correcting tabnumbers variable
alltabdata.pop(indextoclose)
self.tabnumbers.pop(indextoclose)
except Exception:
trace_error()
self.posterror("Failed to close the current tab")
#save data in open tab
def savedataincurtab(self):
try:
#getting directory to save files from QFileDialog
outdir = str(QFileDialog.getExistingDirectory(self, "Select Directory to Save File(s)"))
if outdir == '':
QApplication.restoreOverrideCursor()
return False
except:
self.posterror("Error raised in directory selection")
return
try:
QApplication.setOverrideCursor(Qt.WaitCursor)
#pulling all relevant data
ctab = self.tabWidget.currentIndex()
#write code to save open files here
except Exception:
trace_error() #if something else in the file save code broke
self.posterror("Filed to save files")
QApplication.restoreOverrideCursor()
return False
finally:
QApplication.restoreOverrideCursor()
return True
#warning message
def postwarning(self,warningtext):
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText(warningtext)
msg.setWindowTitle("Warning")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
#error message
def posterror(self,errortext):
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText(errortext)
msg.setWindowTitle("Error")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
#warning message with options (Okay or Cancel)
def postwarning_option(self,warningtext):
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText(warningtext)
msg.setWindowTitle("Warning")
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
outval = msg.exec_()
option = 'unknown'
if outval == 1024:
option = 'okay'
elif outval == 4194304:
option = 'cancel'
return option
#add warning message before closing GUI
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Message',
"Are you sure to close the application? \n All unsaved work will be lost!", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
if self.preferencesopened:
self.settingsthread.close()
event.accept()
else:
event.ignore()
# =============================================================================
# EXECUTE PROGRAM
# =============================================================================
if __name__ == '__main__':
app = QApplication(argv)
ex = RunProgram()
exit(app.exec_())