-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.py
More file actions
643 lines (582 loc) · 27.7 KB
/
Copy pathMainWindow.py
File metadata and controls
643 lines (582 loc) · 27.7 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
'''
Main Window
@Author:
David Solano
'''
# Imports:
import logging
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtCore import *
import os
import sys
# This is needed for the one executable file to work:
def resource_path(relative_path):
'''
Function to get the temp folder path when the executable file running
@params:
relative_path -> (String)
@return:
base_path + relative_path -> (String)
'''
try:
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
except Exception as e:
logging.error('Raised error while in resource_path. Error -> %s.', e)
class MainWindow(QMainWindow):
def __init__(self):
'''
Initializes the Main window parameters
@param:
self -> build in action
@retun:
None
'''
try:
super().__init__()
width = 550
height =700
self.setFixedWidth(550)
self.setFixedHeight(700)
self.current_path = os.path.dirname(__file__) # Gets current path of this file
self.setWindowIcon(QIcon(resource_path(self.current_path + "\\SolarCarTeam_LogoIcon.ico")))
self.setWindowTitle("Packet Parser")
self.resize(width, height) # Login window width and height
# Sets the window to be displayed in the center of the screenS
qr = self.frameGeometry()
qr.moveCenter(self.screen().availableGeometry().center())
self.move(qr.topLeft())
# Default font and color
self.default_font = QFont('Arial', 11)
self.setFont(self.default_font) # Apply default font to the whole application
# Create the tab widget
self.tabs = QTabWidget()
self.setCentralWidget(self.tabs) # Set tabs as the central widget of the main window
self.tabs.tabBar().setStyleSheet("""
QTabBar::tab {
width: 275px;
height: 35px;
}
""")
# Create the first tab
self.tab1 = QWidget()
self.tabs.addTab(self.tab1, "Hermes")
self.setup_tab1()
# Create the second tab
self.tab2 = QWidget()
self.tabs.addTab(self.tab2, "Dashboard")
self.setup_tab2()
except Exception as e:
logging.error("Raised error initializing the MainWindow. Error -> %s", e)
def setup_tab1(self):
'''
Sets up the widgets for the Hermes tab
@param:
self -> build in action
@retun:
None
'''
try:
layout = QVBoxLayout()
layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)
# Attach File Section
csv_label = QLabel("Attach File")
csv_label.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
font-weight: bold; /* Font weight */
color: black; /* Font color */
""")
layout.addWidget(csv_label)
csv_input_layout = QHBoxLayout()
self.csv_lineEdit_Hermes = QLineEdit()
self.csv_lineEdit_Hermes.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: white; /* Font color */
""")
csv_input_layout.addWidget(self.csv_lineEdit_Hermes)
csv_button = QPushButton("Browse")
csv_button.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: white; /* Font color */
""")
csv_button.clicked.connect(self.browse_buttonFunction_Hermes)
csv_input_layout.addWidget(csv_button)
layout.addLayout(csv_input_layout)
# Generate Section
generate_button = QPushButton("Generate")
generate_button.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: white; /* Font color */
""")
generate_button.setFixedSize(250, 60)
generate_button.clicked.connect(lambda: self.generate_buttonFunction_Hermes(self.csv_lineEdit_Hermes.text()))
center_layout = QHBoxLayout()
center_layout.addStretch()
center_layout.addWidget(generate_button)
center_layout.addStretch()
layout.addLayout(center_layout)
# Label Section
label_layout = QHBoxLayout()
label_layout.addStretch()
self.hidden_label_Hermes = QLabel("This is a hidden label")
self.hidden_label_Hermes.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: red; /* Font color */
font-weight: bold; /* Font weight */
""")
self.hidden_label_Hermes.setVisible(False)
label_layout.addWidget(self.hidden_label_Hermes)
label_layout.addStretch()
layout.addLayout(label_layout)
# Generated Files Section
generated_files_label_layout = QHBoxLayout()
generated_files_label_layout.addStretch()
generated_files_label = QLabel("Generated Files")
generated_files_label.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
font-weight: bold; /* Font weight */
color: black; /* Font color */
""")
generated_files_label_layout.addWidget(generated_files_label)
generated_files_label_layout.addStretch()
layout.addLayout(generated_files_label_layout)
# Progress Bar
progress_bar_layout = QHBoxLayout()
progress_bar_layout.addStretch()
self.progress_bar_Hermes = QProgressBar()
self.progress_bar_Hermes.setStyleSheet("""
QProgressBar {
border: 2px solid grey;
border-radius: 5px;
background: white;
}
QProgressBar::chunk {
background-color: #4CAF50; /* Green color for progress */
width: 20px;
}
""")
self.progress_bar_Hermes.setFixedSize(400, 25)
self.progress_bar_Hermes.setValue(0)
self.progress_bar_Hermes.setTextVisible(False)
progress_bar_layout.addWidget(self.progress_bar_Hermes)
progress_bar_layout.addStretch()
layout.addLayout(progress_bar_layout)
# Tree Section
self.tree_widget_Hermes = QTreeWidget()
self.tree_widget_Hermes.setColumnCount(1)
self.tree_widget_Hermes.setHeaderLabels(["File Name"])
self.tree_widget_Hermes.headerItem().setTextAlignment(0, Qt.AlignmentFlag.AlignCenter)
self.tree_widget_Hermes.setStyleSheet("""
QTreeWidget {
background: rgba(255, 255, 255, 150); /* Semi-transparent white background for tree */
border: 1px solid rgba(0, 0, 0, 50); /* Optional: Semi-transparent border */
}
QHeaderView::section {
background: rgb(77, 77, 77); /* Matching grayish color for the header */
color: white; /* White text for better readability */
font-weight: bold; /* Header font style */
border: 1px solid rgba(0, 0, 0, 50); /* Optional: Subtle border for header sections */
}
QTreeWidget::item {
background: rgba(255, 255, 255, 100); /* Semi-transparent item background */
}
""")
layout.addWidget(self.tree_widget_Hermes)
# Save Files Section
save_button_layout = QHBoxLayout()
# Add stretch to center buttons
save_button_layout.addStretch()
# Save Selected Button
self.save_selected_button_Hermes = QPushButton("Save Selected")
self.save_selected_button_Hermes.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: white; /* Font color */
""")
self.save_selected_button_Hermes.setFixedSize(250, 60)
self.save_selected_button_Hermes.setDisabled(True) # Initially disabled
self.save_selected_button_Hermes.clicked.connect(self.saveSelected_buttonFunction_Hermes)
save_button_layout.addWidget(self.save_selected_button_Hermes)
# Space between buttons
save_button_layout.addSpacing(20)
# Save All Files Button
self.save_files_button_Hermes = QPushButton("Save All Files")
self.save_files_button_Hermes.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: white; /* Font color */
""")
self.save_files_button_Hermes.setFixedSize(250, 60)
self.save_files_button_Hermes.setDisabled(True) # Initially disabled
self.save_files_button_Hermes.clicked.connect(self.saveAllFiles_buttonFunction_Hermes)
save_button_layout.addWidget(self.save_files_button_Hermes)
# Add stretch to center buttons
save_button_layout.addStretch()
# Add the layout to the main layout
layout.addLayout(save_button_layout)
# Version Section
version_label = QLabel("01.00.00")
version_label.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 12px; /* Font size */
color: gray; /* Font color */
font-weight: normal; /* Font weight */
""")
version_layout = QHBoxLayout()
version_layout.addStretch()
version_layout.addWidget(version_label)
layout.addLayout(version_layout)
# Set the layout for the tab
self.tab1.setLayout(layout)
# Background image
current_path = os.path.dirname(__file__).replace("\\", "/").replace('c', "C")
background_path = resource_path(current_path + "/SolarCar_Hermesbackground.png")
if not os.path.exists(background_path):
raise FileNotFoundError(f"Background image not found: {background_path}")
self.tab1.setObjectName("tab1")
self.tab1.setStyleSheet(f"""
QWidget#tab1 {{
background-image: url("{background_path}");
background-repeat: no-repeat;
background-position: center;
}}
""")
except Exception as e:
logging.error("Raised error at setup_tab1. Error -> %s", e)
def setup_tab2(self):
'''
Sets up the widgets for the Dashboard tab
@param:
self -> build in action
@retun:
None
'''
try:
layout = QVBoxLayout()
layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)
# Attach File Section
csv_label = QLabel("Attach File")
csv_label.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
font-weight: bold; /* Font weight */
color: white; /* Font color */
""")
layout.addWidget(csv_label)
csv_input_layout = QHBoxLayout()
self.csv_lineEdit_Dashboard = QLineEdit()
self.csv_lineEdit_Dashboard.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: white; /* Font color */
""")
csv_input_layout.addWidget(self.csv_lineEdit_Dashboard)
csv_button = QPushButton("Browse")
csv_button.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: white; /* Font color */
""")
csv_button.clicked.connect(self.browse_buttonFunction_Dashboard)
csv_input_layout.addWidget(csv_button)
layout.addLayout(csv_input_layout)
# Generate Section
generate_button = QPushButton("Generate")
generate_button.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: white; /* Font color */
""")
generate_button.setFixedSize(250, 60)
generate_button.clicked.connect(lambda: self.generate_buttonFunction_Dashboard(self.csv_lineEdit_Dashboard.text()))
center_layout = QHBoxLayout()
center_layout.addStretch()
center_layout.addWidget(generate_button)
center_layout.addStretch()
layout.addLayout(center_layout)
# Label Section
label_layout = QHBoxLayout()
label_layout.addStretch()
self.hidden_label_Dashboard = QLabel("This is a hidden label")
self.hidden_label_Dashboard.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: red; /* Font color */
font-weight: bold; /* Font weight */
""")
self.hidden_label_Dashboard.setVisible(False)
label_layout.addWidget(self.hidden_label_Dashboard)
label_layout.addStretch()
layout.addLayout(label_layout)
# Generated Files Section
generated_files_label_layout = QHBoxLayout()
generated_files_label_layout.addStretch()
generated_files_label = QLabel("Generated Files")
generated_files_label.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
font-weight: bold; /* Font weight */
color: white; /* Font color */
""")
generated_files_label_layout.addWidget(generated_files_label)
generated_files_label_layout.addStretch()
layout.addLayout(generated_files_label_layout)
# Progress Bar
progress_bar_layout = QHBoxLayout()
progress_bar_layout.addStretch()
self.progress_bar_Dashboard = QProgressBar()
self.progress_bar_Dashboard.setStyleSheet("""
QProgressBar {
border: 2px solid grey;
border-radius: 5px;
background: white;
}
QProgressBar::chunk {
background-color: #4CAF50; /* Green color for progress */
width: 20px;
}
""")
self.progress_bar_Dashboard.setFixedSize(400, 25)
self.progress_bar_Dashboard.setValue(0)
self.progress_bar_Dashboard.setTextVisible(False)
progress_bar_layout.addWidget(self.progress_bar_Dashboard)
progress_bar_layout.addStretch()
layout.addLayout(progress_bar_layout)
# Tree Section
self.tree_widget_Dashboard = QTreeWidget()
self.tree_widget_Dashboard.setColumnCount(1)
self.tree_widget_Dashboard.setHeaderLabels(["File Name"])
self.tree_widget_Dashboard.headerItem().setTextAlignment(0, Qt.AlignmentFlag.AlignCenter)
self.tree_widget_Dashboard.setStyleSheet("""
QTreeWidget {
background: rgba(255, 255, 255, 150); /* Semi-transparent white background for tree */
border: 1px solid rgba(0, 0, 0, 50); /* Optional: Semi-transparent border */
}
QHeaderView::section {
background: rgb(77, 77, 77); /* Matching grayish color for the header */
color: white; /* White text for better readability */
font-weight: bold; /* Header font style */
border: 1px solid rgba(0, 0, 0, 50); /* Optional: Subtle border for header sections */
}
QTreeWidget::item {
background: rgba(255, 255, 255, 100); /* Semi-transparent item background */
}
""")
layout.addWidget(self.tree_widget_Dashboard)
# Save Files Section
save_button_layout = QHBoxLayout()
# Add stretch to center buttons
save_button_layout.addStretch()
# Save Selected Button
self.save_selected_button_Dashboard = QPushButton("Save Selected")
self.save_selected_button_Dashboard.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: white; /* Font color */
""")
self.save_selected_button_Dashboard.setFixedSize(250, 60)
self.save_selected_button_Dashboard.setDisabled(True) # Initially disabled
self.save_selected_button_Dashboard.clicked.connect(self.saveSelected_buttonFunction_Dashboard)
save_button_layout.addWidget(self.save_selected_button_Dashboard)
# Space between buttons
save_button_layout.addSpacing(20)
# Save All Files Button
self.save_files_button_Dashboard = QPushButton("Save All Files")
self.save_files_button_Dashboard.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 18px; /* Font size */
color: white; /* Font color */
""")
self.save_files_button_Dashboard.setFixedSize(250, 60)
self.save_files_button_Dashboard.setDisabled(True) # Initially disabled
self.save_files_button_Dashboard.clicked.connect(self.saveAllFiles_buttonFunction_Dashboard)
save_button_layout.addWidget(self.save_files_button_Dashboard)
# Add stretch to center buttons
save_button_layout.addStretch()
# Add the layout to the main layout
layout.addLayout(save_button_layout)
#Version Section:
#Label:
version_label = QLabel("01.00.00")
version_label.setStyleSheet("""
font-family: 'Arial'; /* Font name */
font-size: 12px; /* Font size */
color: gray; /* Font color */
font-weight: normal; /* Font weight */
""")
# Create a horizontal layout to align it to the bottom-right corner
version_layout = QHBoxLayout()
version_layout.addStretch() # Stretch to push label to the right
version_layout.addWidget(version_label) # Add the version label to layout
layout.addLayout(version_layout)
# Set the layout for the tab
self.tab2.setLayout(layout)
# Update current path
current_path = os.path.dirname(__file__).replace("\\", "/").replace('c', "C")
background_path = resource_path(current_path + "/SolarCar_Dashboardbackground.png")
# Ensure the background file exists
if not os.path.exists(background_path):
raise FileNotFoundError(f"Background image not found: {background_path}")
# Set the object name for tab2
self.tab2.setObjectName("tab2")
# Apply the background image to tab1 only
self.tab2.setStyleSheet(f"""
QWidget#tab2 {{
background-image: url("{background_path}");
background-repeat: no-repeat;
background-position: center;
}}
""")
except Exception as e:
logging.error("Raised error at setup_tab2. Error -> %s", e)
def browse_buttonFunction_Hermes(self):
'''
Hermes: Provides a UI for users to use to select an Excel file
@param:
self -> build in action
@return:
None
'''
try:
options = QFileDialog.Option.ReadOnly # Use specific option directly
file_name, _ = QFileDialog.getOpenFileName(
self, # parent window
"Select Excel File", # dialog title
"", # default directory
"Excel Files (*.xlsx *.xls);;All Files (*)", # file filter for Excel files
options=options # pass options here
)
if file_name:
self.csv_lineEdit_Hermes.setText(file_name)
except Exception as e:
logging.error("Raised error at browse_buttonFunction_Hermes. Error -> %s", e)
def generate_buttonFunction_Hermes(self, csvFilePath):
'''
Hermes: Generates cpp files based on the CSV file indicated by the user
@param:
self -> build in action\n
csvFilePath -> String
@retun:
None
'''
try:
self.hidden_label_Hermes.setVisible(False)
if(csvFilePath == '' or '.xlsx' not in csvFilePath):
self.hidden_label_Hermes.setVisible(True)
self.hidden_label_Hermes.setText("Invalid Filepath")
else:
self.hidden_label_Hermes.setVisible(False)
print(csvFilePath)
# Packet parser thenn generates the files onto a hidden "temp" folder inside the application and then returns the names of all files
# These files names is then use to add the rows to the table widget.
except Exception as e:
logging.error("Raised error at generate_buttonFunction_Hermes. Error -> %s", e)
def saveAllFiles_buttonFunction_Hermes(self):
'''
Hermes: Saves all files in the Tree widget data
@param:
self -> build in action\n
csvFilePath -> String
@retun:
None
'''
try:
# Inside the "temp folder created by the Packet Parser python file all the files are compressed as a zip file.
# This zip file path is used to as reference to save the zip file onto user local computer
pass
except Exception as e:
logging.error("Raised error at save_files_buttonFunction_Hermes. Error -> %s", e)
def saveSelected_buttonFunction_Hermes(self):
'''
Hermes: Saves selected files indicated by the user in the Tree widget data
@param:
self -> build in action\n
csvFilePath -> String
@retun:
None
'''
try:
# Inside the "temp folder created by the Packet Parser python file all the files are compressed as a zip file.
# This zip file path is used to as reference to save the zip file onto user local computer
pass
except Exception as e:
logging.error("Raised error at save_files_buttonFunction_Hermes. Error -> %s", e)
def browse_buttonFunction_Dashboard(self):
'''
Hermes: Provides a UI for users to use to select an Excel file
@param:
self -> build in action
@return:
None
'''
try:
options = QFileDialog.Option.ReadOnly # Use specific option directly
file_name, _ = QFileDialog.getOpenFileName(
self, # parent window
"Select Excel File", # dialog title
"", # default directory
"Excel Files (*.xlsx *.xls);;All Files (*)", # file filter for Excel files
options=options # pass options here
)
if file_name:
self.csv_lineEdit_Dashboard.setText(file_name)
except Exception as e:
logging.error("Raised error at browse_buttonFunction_Dashboard. Error -> %s", e)
def generate_buttonFunction_Dashboard(self, csvFilePath):
'''
Dashboard: Generates cpp files based on the CSV file indicated by the user
@param:
self -> build in action\n
csvFilePath -> String
@retun:
None
'''
try:
self.hidden_label_Dashboard.setVisible(False)
if(csvFilePath == '' or '.xlsx' not in csvFilePath):
self.hidden_label_Dashboard.setVisible(True)
self.hidden_label_Dashboard.setText("Invalid Filepath")
else:
self.hidden_label_Dashboard.setVisible(False)
print(csvFilePath)
# Packet parser thenn generates the files onto a hidden "temp" folder inside the application and then returns the names of all files
# These files names is then use to add the rows to the table widget.
except Exception as e:
logging.error("Raised error at generate_buttonFunction_Dashboard. Error -> %s", e)
def saveAllFiles_buttonFunction_Dashboard(self):
'''
Hermes: Saves all files in the Tree widget data
@param:
self -> build in action\n
csvFilePath -> String
@retun:
None
'''
try:
# Inside the "temp folder created by the Packet Parser python file all the files are compressed as a zip file.
# This zip file path is used to as reference to save the zip file onto user local computer
pass
except Exception as e:
logging.error("Raised error at save_files_buttonFunction_Hermes. Error -> %s", e)
def saveSelected_buttonFunction_Dashboard(self):
'''
Hermes: Saves selected files indicated by the user in the Tree widget data
@param:
self -> build in action\n
csvFilePath -> String
@retun:
None
'''
try:
# Inside the "temp folder created by the Packet Parser python file all the files are compressed as a zip file.
# This zip file path is used to as reference to save the zip file onto user local computer
pass
except Exception as e:
logging.error("Raised error at save_files_buttonFunction_Hermes. Error -> %s", e)