-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
230 lines (181 loc) · 7.38 KB
/
gui.py
File metadata and controls
230 lines (181 loc) · 7.38 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
# LacCore/CSDCO
# Useful compound GUI objects and methods for PyQt applications.
import logging, os, platform
from PyQt5 import QtWidgets, QtCore
def warnbox(parent, title="Warning", message=""):
QtWidgets.QMessageBox.warning(parent, title, message)
def errbox(parent, title="Error", message=""):
QtWidgets.QMessageBox.critical(parent, title, message)
def infobox(parent, title="Information", message=""):
QtWidgets.QMessageBox.information(parent, title, message)
def promptbox(parent, title="Prompt", message=""):
response = QtWidgets.QMessageBox.question(parent, title, message)
return response == QtWidgets.QMessageBox.Yes
def inputbox(parent, title="Get Input", message="[message]", default_text="[enter value]"):
text, confirmed = QtWidgets.QInputDialog.getText(parent, title, message, QtWidgets.QLineEdit.Normal, default_text)
return text, confirmed
def chooseDirectory(parent, path=""):
dlg = QtWidgets.QFileDialog(parent, "Choose directory", path)
selectedDir = dlg.getExistingDirectory(parent)
return selectedDir
def chooseFile(parent, path=""):
dlg = QtWidgets.QFileDialog(parent, "Choose file", path)
chosenFile = dlg.getOpenFileName(parent)
return chosenFile
def chooseFiles(parent, path=""):
dlg = QtWidgets.QFileDialog(parent, "Choose file(s)", path)
chosenFiles = dlg.getOpenFileNames(parent)
return chosenFiles
def chooseSaveFile(parent, path=""):
dlg = QtWidgets.QFileDialog(parent, "Save file", path)
saveFile = dlg.getSaveFileName(parent)
return saveFile
# Provides file drag and drop support. Inheriting classes must call
# self.setAcceptDrops(True) and self.setAcceptMethod([provided method])
# for DnD behavior to work.
class DragAndDropMixin:
def __init__(self):
self.acceptMethod = None
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
paths = [u.toLocalFile() for u in event.mimeData().urls()]
if self.acceptMethod:
self.acceptMethod(paths)
else:
event.ignore()
# client-provided method to handle a single arugment: a list of file paths
def setAcceptMethod(self, method):
self.acceptMethod = method
# list of files with Add and Remove buttons
class FileListPanel(QtWidgets.QWidget, DragAndDropMixin):
def __init__(self, title):
QtWidgets.QWidget.__init__(self)
self.initUI(title)
self.setAcceptDrops(True)
self.setAcceptMethod(self.addFiles)
def initUI(self, title):
vlayout = QtWidgets.QVBoxLayout(self)
vlayout.addWidget(LabelFactory.makeItemLabel(title))
self.sslist = QtWidgets.QListWidget()
self.sslist.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
vlayout.addWidget(self.sslist)
arlayout = QtWidgets.QHBoxLayout()
self.addButton = QtWidgets.QPushButton("Add...")
self.addButton.clicked.connect(self.onAdd)
self.rmButton = QtWidgets.QPushButton("Remove")
self.rmButton.clicked.connect(self.onRemove)
self._enableRemove()
arlayout.addWidget(self.addButton)
arlayout.addWidget(self.rmButton)
vlayout.setSpacing(0)
vlayout.addLayout(arlayout)
vlayout.setContentsMargins(0,0,0,0)
def addFile(self, newfile):
self.sslist.addItem(QtWidgets.QListWidgetItem(newfile))
self._enableRemove()
def addFiles(self, filelist):
for f in filelist:
self.addFile(f)
def getFiles(self):
return [self.sslist.item(idx).text() for idx in range(self.sslist.count())]
def clear(self):
self.sslist.clear()
def onAdd(self):
files = chooseFiles(self)
for f in files[0]:
self.addFile(f)
def onRemove(self):
for sel in self.sslist.selectedItems():
self.sslist.takeItem(self.sslist.row(sel))
self._enableRemove()
def _enableRemove(self):
self.rmButton.setEnabled(self.sslist.count() > 0)
class ProgressPanel(QtWidgets.QWidget):
def __init__(self, parent, initialText='[progress text]'):
QtWidgets.QWidget.__init__(self)
self.parent = parent
layout = QtWidgets.QVBoxLayout(self)
self.label = QtWidgets.QLabel(initialText)
self.progress = QtWidgets.QProgressBar()
self.progress.setMaximum(100)
layout.addWidget(self.label)
layout.addWidget(self.progress)
def setValue(self, val):
self.progress.setValue(val)
def setText(self, text):
self.label.setText(text)
def setValueAndText(self, val, text):
self.setValue(val)
self.setText(text)
self._update()
def _update(self):
self.parent.app.processEvents()
def clear(self):
self.setValueAndText(0, "")
# Handler to direct python logging output to QTextEdit control
class LogTextArea(logging.Handler):
def __init__(self, parent, label):
self.parent = parent
self.layout = QtWidgets.QVBoxLayout()
logging.Handler.__init__(self)
self.logText = QtWidgets.QTextEdit(parent)
self.logText.setReadOnly(True)
self.logText.setToolTip("It's all happening.")
self.layout.setContentsMargins(0,0,0,0)
self.layout.setSpacing(0)
self.layout.addWidget(QtWidgets.QLabel(label))
self.layout.addWidget(self.logText)
self.verboseCheckbox = QtWidgets.QCheckBox("Include Debugging Information")
self.layout.addWidget(self.verboseCheckbox)
def isVerbose(self):
return self.verboseCheckbox.isChecked()
def emit(self, record):
msg = self.format(record)
self.logText.insertPlainText(msg + "\n")
self.parent.app.processEvents()
def write(self, m):
pass
class ButtonPanel(QtWidgets.QWidget):
def __init__(self, button):
QtWidgets.QWidget.__init__(self)
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(button)
class TwoButtonPanel(QtWidgets.QWidget):
def __init__(self, button1, button2):
QtWidgets.QWidget.__init__(self)
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(button1)
layout.addWidget(button2)
# add help text below widget
def HelpTextDecorator(widget, helpText, spacing=5):
layout = QtWidgets.QVBoxLayout()
layout.setSpacing(spacing)
layout.setContentsMargins(0,0,0,0)
layout.addWidget(widget)
layout.addWidget(LabelFactory.makeDescLabel(helpText))
return layout
# create appropriately-sized labels for the current OS
class LabelFactory:
# main label for an item
# On Mac, use standard font. On Windows, bold font.
@classmethod
def makeItemLabel(cls, text):
label = QtWidgets.QLabel(text)
if platform.system() == "Windows":
label.setStyleSheet("QLabel {font-weight: bold;}")
return label
# label for help/description text
# On Mac, use a smaller font. On Windows, standard font.
@classmethod
def makeDescLabel(cls, text):
label = QtWidgets.QLabel(text)
if platform.system() == "Darwin":
label.setStyleSheet("QLabel {font-size: 11pt;}")
return label