-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdialogs.py
More file actions
154 lines (130 loc) · 6.61 KB
/
dialogs.py
File metadata and controls
154 lines (130 loc) · 6.61 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
from PySide6.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QProgressBar, QPlainTextEdit, QWidget, QMessageBox
import PySide6.QtCore as QtCore
import logging
from ui_aboutdialog import Ui_AboutDialog
class Progress_Dialog(QDialog):
'''Simple dialog to display progress of a task and activate a close button when complete.'''
def __init__(self, parent=None, title: str = "Progress", message: str = "Task Progress:", autoclose: bool = False ):
super().__init__(parent)
self.autoclose = autoclose
if not autoclose:
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Close)
self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False)
self.buttonBox.button(QDialogButtonBox.Close).clicked.connect(self.close)
self.setWindowTitle(title)
self.setModal(True)
self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowTitleHint)
self.reportLabel = QLabel()
self.progressBar = QProgressBar()
self.messageLabel = QLabel(message)
self.layout = QVBoxLayout()
self.layout.addWidget(self.messageLabel)
self.layout.addWidget(self.progressBar)
self.layout.addWidget(self.reportLabel)
if not autoclose:
self.layout.addWidget(self.buttonBox)
self.setLayout(self.layout)
def report(self, msg: str):
'''Display a message below the progress bar.'''
self.reportLabel.setText(msg)
def finished(self):
'''Call this when action is finished and dialog can be closed.'''
if self.autoclose:
self.close()
else:
if not self.reportLabel.text:
self.report('Task Complete')
self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True)
class External_Process_Dialog(Progress_Dialog):
'''Dialog for monitoring an external process.'''
def __init__(self, parent=None, title: str = "Progress", message: str = "Task Progress:", autoclose: bool = False):
super().__init__(parent, title, message, autoclose)
self.layout.removeWidget(self.reportLabel)
self.reportLabel.deleteLater()
self.text = QPlainTextEdit()
self.text.setReadOnly(True)
if self.autoclose:
self.layout.addWidget(self.text)
else:
self.layout.insertWidget(self.layout.count()-1, self.text)
def report(self, msg: str):
self.text.appendPlainText(msg)
class File_Upload_Progress_Dialog(QDialog):
halt = False # set to True to cancel after the curent file is finished uploading
def __init__(self, parent = None, multifile=False, autoclose: bool = False):
super().__init__(parent)
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Cancel)
self.buttonBox.button(QDialogButtonBox.Cancel).clicked.connect(self.cancel_button_handler)
self.setWindowTitle("Uploading Files")
self.setModal(True)
self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowTitleHint )
self.layout = QVBoxLayout()
self.fileNameLabel = QLabel("File: ")
self.layout.addWidget(self.fileNameLabel)
self.fileProgressBar = QProgressBar()
self.fileProgressBar.setTextVisible(True)
self.layout.addWidget(self.fileProgressBar)
self.totalProgressBar = QProgressBar()
if multifile:
self.totalProgressBar.setTextVisible(True)
self.totalProgressBar.setValue(0)
self.layout.addWidget(self.totalProgressBar)
self.layout.addWidget(self.buttonBox)
self.setLayout(self.layout)
self.autoclose = autoclose
def set_num_files(self, num_files: int):
'''Set the number of fiiles to be uploaded in this batch.'''
self.totalProgressBar.setMaximum(num_files)
self.totalProgressBar.setFormat("%v/%m")
def file_completed(self):
'''Update file completed count in progress bar'''
self.totalProgressBar.setValue(self.totalProgressBar.value() + 1)
def set_file_size(self, size: int):
'''Set the size of the current file being uploaded, in bytes'''
self.fileProgressBar.setMaximum(size)
def set_bytes_uploaded(self, b: int):
'''Set the number of bytes uploaded for the current file'''
self.fileProgressBar.setValue(b)
def cancel_button_handler(self):
self.halt = True
self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False)
self.buttonBox.button(QDialogButtonBox.Cancel).setText('Canceling...')
logging.getLogger().info('Canceling file upload.')
def upload_complete(self):
'''Call this when all file uploads are completed and the window can be closed.'''
if self.autoclose:
self.close()
else:
self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True)
self.buttonBox.button(QDialogButtonBox.Cancel).setText('Close')
self.buttonBox.button(QDialogButtonBox.Cancel).clicked.disconnect()
self.buttonBox.button(QDialogButtonBox.Cancel).clicked.connect(self.close)
self.fileNameLabel.setText('Upload complete!')
logging.getLogger().info('Upload complete!')
class Loading_Box(QDialog):
'''Displays an animated "loading" box for actions without specific completion measures.'''
def __init__(self, parent: QWidget = None, message: str = "Loading...", autoclose: bool = True) -> None:
super().__init__(parent)
self.setModal(True)
self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint)
self.layout = QVBoxLayout()
self.layout.addWidget(QLabel(message))
bar = QProgressBar()
bar.setMaximum(0)
self.layout.addWidget(bar)
self.setLayout(self.layout)
self.autoclose = autoclose
def error_handler(error, info = None, parent: QWidget = None) -> None:
'''Display an error message to the user and log to the log file.'''
logging.getLogger().error(error)
box = QMessageBox(QMessageBox.Critical, 'Error', str(error), QMessageBox.Close)
if info:
box.setInformativeText(str(info))
box.exec()
class AboutDialog(Ui_AboutDialog, QDialog):
def __init__(self, version: str, authors: str, homepage: str, parent: QWidget = None):
super().__init__(parent)
self.setupUi(self)
self.version_label.setText(self.version_label.text() + version)
self.authors_label.setText(self.authors_label.text() + authors)
self.homepage_label.setText(self.homepage_label.text() + f'<a href="{homepage}">{homepage}</a>')