-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtility.py
More file actions
149 lines (135 loc) · 6 KB
/
Utility.py
File metadata and controls
149 lines (135 loc) · 6 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
# SDTrimSP GUI - a graphical user interface for SDTrimSP to simulate sputtering, ion implantation and the dynamic
# effects of ion irradiation
#
# Copyright(C) 2022, Paul S.Szabo, David Weichselbaum, Herbert Biber, Christian Cupak, Andreas Mutzke,
# Richard A.Wilhelm, Friedrich Aumayr
#
# This program implements libraries of the Qt framework (https://www.qt.io/).
#
# 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 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/.
from enum import Enum
from PyQt5.QtCore import QFileInfo
from PyQt5.QtGui import QColor, QFont
from PyQt5.QtWidgets import QHBoxLayout, QLabel, QSpinBox, QDoubleSpinBox, QCheckBox,\
QComboBox, QLineEdit, QMessageBox, QFileDialog, QGraphicsDropShadowEffect, QVBoxLayout
class inputHLayout(QHBoxLayout):
class InputType(Enum):
SPINBOX = 1
COMBOBOX = 2
LINEEDIT = 3
DOUBLESPINBOX = 4
def __init__(self, parent, label, inputType, defaultValue, inputRange=(0,1e8), entries=[]):
super().__init__()
self.label = QLabel(label, parent)
self.addWidget(self.label)
assert isinstance(inputType, inputHLayout.InputType)
if inputType in [inputHLayout.InputType.SPINBOX, inputHLayout.InputType.DOUBLESPINBOX]:
if inputType == inputHLayout.InputType.SPINBOX:
self.input = QSpinBox(parent)
defaultValue = int(defaultValue)
inputRange = (int(inputRange[0]),int(inputRange[1]))
else:
self.input = QDoubleSpinBox(parent)
self.input.setButtonSymbols(QSpinBox.NoButtons)
self.input.setMinimumSize(50, 20)
self.input.setRange(inputRange[0], inputRange[1])
self.input.setValue(defaultValue)
elif inputType == inputHLayout.InputType.COMBOBOX:
self.input = QComboBox(parent)
self.input.addItems(entries)
self.input.setCurrentIndex(defaultValue)
elif inputType == inputHLayout.InputType.LINEEDIT:
self.input = QLineEdit(parent)
self.input.setPlaceholderText(str(defaultValue))
self.input.setMaxLength(int(inputRange))
self.addWidget(self.input)
class VBoxTitleLayout(QVBoxLayout):
'''
if addStretch is a bool: addStretch(1) after title if True, else do nothing
if addStretch is an integer: addSpacing(addStretch) after title
'''
def __init__(self, parent, title, titleStyle, spacing, addStretch):
super().__init__()
self.setSpacing(spacing)
self.hl = QHBoxLayout()
self.title = QLabel(title, parent)
self.title.setStyleSheet(titleStyle)
self.hl.addWidget(self.title)
if type(addStretch) == bool and addStretch:
self.hl.addStretch(1)
elif type(addStretch) == int:
self.hl.addSpacing(addStretch)
self.addLayout(self.hl)
# Field types
class FieldType(Enum):
ABUNDANCE = 1
SEGMENTS = 2
OBJECT = 3
"""Limits the sum of the values held by the objects' fields (defined by the fieldType)
to the given maximum. The last object's field fills up to the maximum, if possible, and is also
disabled."""
def limitSum(objects, maximum, fieldType):
if len(objects) == 0:
return
total = 0
for o in objects[:-1]:
field = getField(o, fieldType)
field.setEnabled(True)
newTotal = total + field.value()
if newTotal > maximum:
field.setValue(maximum - total)
total = maximum
else:
total = newTotal
field = getField(objects[-1], fieldType)
field.setEnabled(False)
field.setValue(maximum - total)
"""Returns a property of the object or the object itself, depending on the given fieldType"""
def getField(obj, fieldType):
assert isinstance(fieldType, FieldType)
if fieldType == FieldType.ABUNDANCE:
return obj.abundance
elif fieldType == FieldType.SEGMENTS:
return obj.segmentCount
elif fieldType == FieldType.OBJECT:
return obj
def selectFileDialog(parentWidget, forSaving, instruction, startDir, fileFilter):
if forSaving:
fullFilePath, _ = QFileDialog.getSaveFileName(parentWidget, instruction, startDir, fileFilter)
else:
fullFilePath, _ = QFileDialog.getOpenFileName(parentWidget, instruction, startDir, fileFilter)
fileName = QFileInfo(fullFilePath).baseName()
if len(fileName) == 0:
return None
return fullFilePath
def showMessageBox(parent, icon, windowTitle, text, infoMessage='', detailedMessage='', standardButtons=QMessageBox.Ok, checkBoxText='', expandDetails=False):
msgBox = QMessageBox(icon, windowTitle, text, standardButtons, parent)
font = QFont()
font.setBold(False)
msgBox.setFont(font)
msgBox.setInformativeText(infoMessage)
msgBox.setDetailedText(detailedMessage)
if len(checkBoxText) > 0:
msgBox.setCheckBox(QCheckBox(checkBoxText, msgBox))
if expandDetails: # Automatically expand the details
for b in msgBox.buttons():
if msgBox.buttonRole(b) == QMessageBox.ActionRole:
b.click()
break
return msgBox, msgBox.exec()
def setWidgetHighlight(widget, enabled, color=QColor(255,0,0,255)):
if not enabled:
widget.setGraphicsEffect(None)
return
dse = QGraphicsDropShadowEffect()
dse.setColor(color)
dse.setOffset(0)
dse.setBlurRadius(10)
widget.setGraphicsEffect(dse)