-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqt_gui.py
More file actions
584 lines (489 loc) · 21.2 KB
/
qt_gui.py
File metadata and controls
584 lines (489 loc) · 21.2 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
"""
================================================================================================================
Qt GUI for PyPeCT2S
================================================================================================================
Created by G.H. Allison, University of Sheffield, Sheffield, United Kingdom.
Copyright (C) 2024 George H. Allison
Contact: ghallison1@sheffield.ac.uk or xinshan.li@sheffield.ac.uk
----------------------------------------------------------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
________________________________________________________________________________________________________________
"""
import sys
import os
import numpy as np
import importlib.util
import glob
from PyQt6.QtWidgets import QTabWidget, QComboBox, QStyleFactory, QWidget, QVBoxLayout, QMessageBox, QSizePolicy, \
QTextEdit, QHBoxLayout, QLabel, QLineEdit, QPushButton, QSpinBox, QCheckBox, QToolTip
from PyQt6.QtGui import QColor, QIcon, QGuiApplication
from PyQt6.QtCore import QCoreApplication, Qt
import core_libs
from core_libs import *
import fem_libs
from fem_libs import *
import mesh_libs
from mesh_libs import *
import post_libs
from post_libs import *
class MyApplication(QWidget):
def __init__(self, *args, **kwargs):
super(MyApplication, self).__init__(*args, **kwargs)
if sys.stdout is None:
sys.stdout = open(os.devnull, "w")
if sys.stderr is None:
sys.stderr = open(os.devnull, "w")
"""
----------------------------
Variables
----------------------------
"""
self.gui_vars = core_libs.gui_vars.GuiVariables()
# If running as a frozen executable, use sys._MEIPASS
if getattr(sys, 'frozen', False):
self.application_path = sys._MEIPASS
else:
self.application_path = os.path.dirname(os.path.realpath(__file__))
np.set_printoptions(precision=4, floatmode='fixed')
def init_ui(self):
main_layout = QVBoxLayout()
self.setLayout(main_layout)
self.tab = QTabWidget(self)
self.setWindowIcon(QIcon(os.path.join(self.application_path, 'icons/INSIGNEO.png')))
# Script Dropdowns
drop_layout = QHBoxLayout()
drop_widget = QWidget()
# Meshing Dropdown
self.msh_dropdown = QComboBox()
self.msh_dropdown.currentIndexChanged.connect(lambda: self.load_script_content(self.msh_dropdown, "Meshing"))
self.discover_scripts(self.msh_dropdown, "mesh")
# Material Dropdown
self.mat_dropdown = QComboBox()
self.mat_dropdown.currentIndexChanged.connect(lambda: self.load_script_content(self.mat_dropdown, "Material"))
self.discover_scripts(self.mat_dropdown, "material")
# FEM Dropdown
self.fem_dropdown = QComboBox()
self.fem_dropdown.currentIndexChanged.connect(lambda: self.load_script_content(self.fem_dropdown, "FEM"))
self.discover_scripts(self.fem_dropdown, "fem")
# Results Dropdown
self.res_dropdown = QComboBox()
self.res_dropdown.currentIndexChanged.connect(lambda: self.load_script_content(self.res_dropdown, "Results"))
self.discover_scripts(self.res_dropdown, "post")
drop_layout.addWidget(self.msh_dropdown)
drop_layout.addWidget(self.mat_dropdown)
drop_layout.addWidget(self.fem_dropdown)
drop_layout.addWidget(self.res_dropdown)
drop_widget.setLayout(drop_layout)
# STL Path
stl_layout = QHBoxLayout()
stl_label = QLabel("STL Path:")
self.stl_path_edit = QLineEdit()
self.stl_path_edit.setPlaceholderText("Max 248 characters")
self.stl_path_edit.setObjectName('stl_path_box')
stl_browse_btn = QPushButton("Browse")
stl_browse_btn.clicked.connect(lambda: core_libs.gui_funcs.browse_file_path('Select STL File', self.gui_vars, 'stl_path', 'STL Files (*.stl);;All Files (*)', 'stl_path_box', self))
stl_layout.addWidget(stl_label)
stl_layout.addWidget(self.stl_path_edit)
stl_layout.addWidget(stl_browse_btn)
# Save Path
save_layout = QHBoxLayout()
save_label = QLabel("Save Path:")
self.save_path_edit = QLineEdit()
self.save_path_edit.setPlaceholderText("Max 248 characters")
self.save_path_edit.setObjectName('save_path_box')
save_browse_btn = QPushButton("Browse")
save_browse_btn.clicked.connect(lambda: core_libs.gui_funcs.browse_dir_path('Select Save Path', self.gui_vars, 'save_path', 'save_path_box', self))
save_layout.addWidget(save_label)
save_layout.addWidget(self.save_path_edit)
save_layout.addWidget(save_browse_btn)
# Core Count
core_layout = QHBoxLayout()
core_count_label = QLabel("Core Count:")
self.core_count_spin = QSpinBox()
self.core_count_spin.setValue(self.gui_vars.core_count)
self.core_count_spin.setMinimum(1)
self.core_count_spin.setMaximum(self.gui_vars.phys_core)
core_layout.addWidget(core_count_label)
core_layout.addWidget(self.core_count_spin)
# Batch mode toggle
batch_layout = QHBoxLayout()
self.batch_mode_check = QCheckBox("Batch Mode", self)
self.batch_mode_check.setChecked(self.gui_vars.batch_mode)
self.batch_mode_check.stateChanged.connect(lambda value: core_libs.gui_funcs.on_state_changed(value, self.gui_vars, 'batch_mode'))
batch_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
batch_layout.addWidget(self.batch_mode_check)
# bonemat rotation mode toggle
bm_rot_layout = QHBoxLayout()
self.bm_rot_check = QCheckBox("Segmented with RAS Coordinate System", self)
self.bm_rot_check.setToolTip(
"This option will rotate bones segmented bones RAS coordinate system.\n"
"Matching the LPS system used by Bonemat."
)
self.bm_rot_check.setChecked(self.gui_vars.bm_rot)
self.bm_rot_check.stateChanged.connect(lambda value: core_libs.gui_funcs.on_state_changed(value, self.gui_vars, 'bm_rot'))
bm_rot_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
bm_rot_layout.addWidget(self.bm_rot_check)
# INSIGNEO Label
INSGNO_layout = QHBoxLayout()
Insigneo_label = QLabel()
Insigneo_label.setText(f'<a href="https://insigneo.github.io/PyPeCT2S/">HELP</a> | <a href="https://www.sheffield.ac.uk">University of Sheffield</a> | <a href="https://www.sheffield.ac.uk/insigneo">INSIGNEO Institute</a> | <a href="https://ct2s.insigneo.org">CT2S</a>')
Insigneo_label.setOpenExternalLinks(True)
Insigneo_label.setAlignment(Qt.AlignmentFlag.AlignRight)
throbr = core_libs.waitingspinnerwidget.QtWaitingSpinner(self, False, False)
throbr.setObjectName("throbr")
throbr.setRoundness(100.0)
throbr.setMinimumTrailOpacity(5.0)
throbr.setTrailFadePercentage(85.0)
throbr.setNumberOfLines(100)
throbr.setLineLength(4)
throbr.setLineWidth(1)
throbr.setInnerRadius(4)
throbr.setRevolutionsPerSecond(0.5)
throbr.setColor(QColor("#0078D7"))
INSGNO_layout.addWidget(throbr)
INSGNO_layout.addWidget(Insigneo_label)
# Log Window
log_layout = QVBoxLayout()
log_label = QLabel("Log:")
self.log_text_edit = QTextEdit()
self.log_text_edit.setReadOnly(True)
self.log_text_edit.setPlainText("---")
self.log_text_edit.append(
"<b>"
"This software has been designed for research purposes only and has not been reviewed or approved "
"by medical device regulation bodies."
"</b>"
)
self.log_text_edit.append("---")
self.log_text_edit.append(
"PyPeCT2S Copyright(C) 2024 George H. Allison\n"
"This program comes with ABSOLUTELY NO WARRANTY; for details click HELP.\n"
"This is free software, and you are welcome to redistribute it "
"under certain conditions; click HELP for details."
)
self.log_text_edit.append("---")
self.log_text_edit.append("Please ensure you are connected to an ANSYS license server.")
self.log_text_edit.append(f"Multithreading with maximum {os.cpu_count()} threads.")
self.log_text_edit.append(
"ANSYS is best with physical cores, the program has limited selection to use the "
f"{self.gui_vars.phys_core} physical cores present."
"\n"
"ANSYS solvers are CPU intensive, hyperthreading can degrade the solver performance."
)
self.log_text_edit.append("---\n")
self.log_text_edit.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
self.log_text_edit.setMinimumHeight(200)
log_layout.addWidget(log_label)
log_layout.addWidget(self.log_text_edit)
# Batch CPU combo
batch_cpu_layout = QHBoxLayout()
batch_cpu_layout.addLayout(core_layout)
batch_cpu_layout.addStretch()
batch_cpu_layout.addLayout(bm_rot_layout)
batch_cpu_layout.addStretch()
batch_cpu_layout.addLayout(batch_layout)
mesh_tab = QWidget(self)
layout = QVBoxLayout()
mesh_tab.setLayout(layout)
mat_tab = QWidget(self)
layout = QVBoxLayout()
mat_tab.setLayout(layout)
fem_tab = QWidget(self)
layout = QVBoxLayout()
fem_tab.setLayout(layout)
res_tab = QWidget(self)
layout = QVBoxLayout()
res_tab.setLayout(layout)
self.tab.addTab(mesh_tab, "Meshing")
self.tab.addTab(mat_tab, "Material")
self.tab.addTab(fem_tab, "FEM")
self.tab.addTab(res_tab, "Results")
self.tab.setTabEnabled(1, True) # Enable or disable Bonemat Tab, Disabled for Student edition.
self.tab.setTabEnabled(2, True) # Enable or disable FEM Tab, Disabled for Student edition.
main_layout.addLayout(stl_layout)
main_layout.addLayout(save_layout)
main_layout.addWidget(drop_widget)
main_layout.addWidget(self.tab)
main_layout.addLayout(batch_cpu_layout)
main_layout.addLayout(log_layout)
main_layout.addLayout(INSGNO_layout)
sys.stdout = self.log_text_edit_redirector(self.log_text_edit, sys.stdout)
sys.stderr = self.log_text_edit_redirector(self.log_text_edit, sys.stderr)
self.colour_mode()
self.style_mode()
self.setWindowTitle("PyPeCT2S")
self.show()
self.stl_path_edit.textChanged.connect(lambda: self.check_path_length(self.stl_path_edit))
self.save_path_edit.textChanged.connect(lambda: self.check_path_length(self.save_path_edit))
self.core_count_spin.valueChanged.connect(lambda value: core_libs.gui_funcs.on_value_changed(value, self.gui_vars, 'core_count'))
self.stl_path_edit.textChanged.connect(lambda value: core_libs.gui_funcs.on_value_changed(value, self.gui_vars, 'stl_path'))
self.save_path_edit.textChanged.connect(lambda value: core_libs.gui_funcs.on_value_changed(value, self.gui_vars, 'save_path'))
self.gui_vars.app_path = self.application_path
lambda value: core_libs.gui_funcs.on_value_changed(value, self.gui_vars, 'app_path')
def discover_scripts(self, drop, group):
drop.addItem(f"Select {group} script")
module_path = os.path.join(self.application_path, f"{group}_libs") # Directory where scripts are stored
# Find all .py files in the module_path, excluding __init__.py
for filepath in glob.glob(os.path.join(module_path, "*.py")):
if os.path.basename(filepath) == "__init__.py":
continue # Skip __init__.py
module_name = os.path.splitext(os.path.basename(filepath))[0]
spec = importlib.util.spec_from_file_location(module_name, filepath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
if hasattr(module, 'get_name'):
drop.addItem(module.get_name(), module)
def load_script_content(self, drop, group):
index = drop.currentIndex()
module = drop.itemData(index)
if module and hasattr(module, 'gui_elements'):
gui_structure = module.gui_elements()
# Find the index of the tab name in the tab widget
tab_index = None
for i in range(self.tab.count()):
if self.tab.tabText(i) == str(group):
tab_index = i
break
# If the tab exists, clear its content and set new content
if tab_index is not None:
try:
# Create a new QWidget for the content
new_content_widget = QWidget()
# Use the build_layout function to construct the layout from the gui_structure
layout = core_libs.gui_funcs.build_layout(gui_structure)
# Set the constructed layout to the new_content_widget
new_content_widget.setLayout(layout)
# Replace the current widget in the tab with the new_content_widget
self.tab.removeTab(tab_index)
self.tab.insertTab(tab_index, new_content_widget, str(group))
except Exception as e:
print(f"Error updating {str(group)} tab content: {e}")
def colour_mode(self):
# Get the system's default palette
system_palette = QGuiApplication.palette()
# Apply the system palette to the application
self.setPalette(system_palette)
# Update the application's style to match the system theme
QCoreApplication.instance().setStyle(QStyleFactory.create('Fusion'))
def style_mode(self):
ico_path = (os.path.join(self.application_path, "icons")).replace("\\", "/")
# Define the stylesheet
stylesheet = f"""
QWidget {{
font-family: 'Segoe UI';
font-size: 10pt;
}}
QPushButton {{
background-color: #0078D7;
color: #FFF;
border-style: none;
padding: 5px 15px;
border-radius: 2px;
}}
QPushButton:hover {{
background-color: #005EA6;
}}
QPushButton:pressed {{
background-color: #003C73;
}}
QPushButton:disabled {{
background-color: #808080;
color: #FFF;
}}
QComboBox {{
border: 1px solid #808080;
border-radius: 4px;
padding: 4px;
}}
QComboBox::drop-down {{
border: 0px;
}}
QComboBox::down-arrow {{
image: url({ico_path}/arrow_down_g.svg);
width: 16px;
height: 16px;
margin-right: 5px;
}}
QComboBox QListView {{
font-size: 10pt;
border: 0px solid #808080;
border-radius: 4px;
padding: 0px;
outline: none;
}}
QLineEdit {{
border: 1px solid #808080;
border-radius: 2px;
padding: 5px;
}}
QLineEdit:focus {{
border: 1px solid #0078D7;
}}
QTabWidget::pane {{
border: 1px solid #808080;
top: 0px;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
border-top-right-radius: 4px;
}}
QTabBar::tab {{
padding: 4px 8px 4px 8px;
border: 0px solid #808080;
border-top-left-radius: 2px;
border-top-right-radius: 2px;
}}
QTabBar::tab:selected {{
background-color: #0078D7;
color: #FFF;
}}
QLabel {{
margin: 0px;
}}
QScrollBar:vertical {{
border: none;
width: 12px;
margin: 14px 2px 14px 2px;
border-radius: 0px;
}}
QScrollBar::handle:vertical {{
background-color: #808080;
min-height: 10px;
border-radius: 4px;
}}
QScrollBar::handle:vertical:hover{{
background-color: #0078D7;
}}
QScrollBar::handle:vertical:pressed {{
background-color: #0078D7;
}}
QScrollBar::sub-line:vertical {{
image: url({ico_path}/arrow_up_g.svg);
border: none;
height: 14px;
border-radius: 1px;
subcontrol-position: top;
subcontrol-origin: margin;
}}
QScrollBar::sub-line:vertical:hover {{
background-color: #0078D7;
image: url({ico_path}/arrow_up_w.svg);
}}
QScrollBar::sub-line:vertical:pressed {{
background-color: #FFF;
image: url({ico_path}/arrow_up_g.svg);
}}
QScrollBar::add-line:vertical {{
image: url({ico_path}/arrow_down_g.svg);
border: none;
height: 14px;
border-radius: 1px;
subcontrol-position: bottom;
subcontrol-origin: margin;
}}
QScrollBar::add-line:vertical:hover {{
background-color: #0078D7;
image: url({ico_path}/arrow_down_w.svg);
}}
QScrollBar::add-line:vertical:pressed {{
background-color: #FFF;
image: url({ico_path}/arrow_down_g.svg);
}}
QSpinBox {{
border: 1px solid #808080;
border-radius: 2px;
padding: 4px;
}}
QSpinBox::up-button {{
image: url({ico_path}/arrow_up_g.svg);
width: 12px;
height: 12px;
padding: 2px;
}}
QSpinBox::up-button:hover {{
background-color: #0078D7;
image: url({ico_path}/arrow_up_w.svg);
}}
QSpinBox::down-button {{
image: url({ico_path}/arrow_down_g.svg);
width: 12px;
height: 12px;
padding: 2px;
}}
QSpinBox::down-button:hover {{
background-color: #0078D7;
image: url({ico_path}/arrow_down_w.svg);
}}
QSpinBox:focus {{
border: 1px solid #0078D7;
}}
QDoubleSpinBox {{
border: 1px solid #808080;
border-radius: 2px;
padding: 4px;
}}
QDoubleSpinBox::up-button {{
image: url({ico_path}/arrow_up_g.svg);
width: 12px;
height: 12px;
padding: 2px;
}}
QDoubleSpinBox::up-button:hover {{
background-color: #0078D7;
image: url({ico_path}/arrow_up_w.svg);
}}
QDoubleSpinBox::down-button {{
image: url({ico_path}/arrow_down_g.svg);
width: 12px;
height: 12px;
padding: 2px;
}}
QDoubleSpinBox::down-button:hover {{
background-color: #0078D7;
image: url({ico_path}/arrow_down_w.svg);
}}
QDoubleSpinBox:focus {{
border: 1px solid #0078D7;
}}
QCheckBox {{
spacing: 5px;
}}
QCheckBox::indicator:checked {{
background-color: #0078D7;
border: 1px solid #808080;
border-radius: 4px;
width: 14px;
height: 14px;
}}
QCheckBox::indicator:unchecked {{
border: 1px solid #808080;
border-radius: 4px;
width: 14px;
height: 14px;
}}
"""
# Apply the stylesheet to the application
QCoreApplication.instance().setStyleSheet(stylesheet)
def check_path_length(self, line_edit):
max_length = 248
text = line_edit.text()
if len(text) > max_length:
line_edit.setStyleSheet("QLineEdit { background-color: rgba(255, 0, 0, 0.2); }")
# Show warning message box
msg_box = QMessageBox()
msg_box.setIcon(QMessageBox.Icon.Warning)
msg_box.setWindowTitle("Warning")
msg_box.setText(f"ANSYS supports a {line_edit.placeholderText()}! \n Please select an alternative path.")
msg_box.exec()
else:
pass
def log_text_edit_redirector(self, log_text_edit, stream):
return core_libs.texteditredirector.TextEditRedirector(log_text_edit, stream)