Commit dd437829 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Minor update of the progress dialog

parent ad7fdeac
#!/usr/bin/env python #!/usr/bin/env python
import sys import sys
import PySide
import time import time
import signal import signal
from collections import deque
import PySide
from PySide.QtCore import QThread, Signal, QObject from PySide.QtCore import QThread, Signal, QObject
from PySide.QtGui import (QApplication, from PySide.QtGui import (QApplication,
...@@ -12,46 +13,77 @@ from PySide.QtGui import (QApplication, ...@@ -12,46 +13,77 @@ from PySide.QtGui import (QApplication,
QProgressDialog) QProgressDialog)
class MyThread(QThread, QObject): class MyThread(QThread, QObject):
signal = Signal(str) ''' Thread waiting for data on stdin and sending signals to the prgress
progress = Signal(int) bar in case something came in.
quit = Signal() Text can be formatted:
* if the line starts with a number, it will use the value (range 0..100)
to update the progress bar, and it will display the text above the bar.
* if the line starts with @ERROR@ the process will stop and all the
history will be dispayed in a log dialog.
* in all other cases, the line is appended to the log
'''
text = Signal(str)
progress = Signal(int)
end = Signal()
error = Signal()
log = deque()
force_quit = False
def run(self): def run(self):
value = 10 value = 10
while True: while True:
# read from stdin without any buffering # read from stdin without any buffering
if self.force_quit:
return
line = sys.stdin.readline() line = sys.stdin.readline()
if len(line) == 0: if len(line) == 0:
print ("Bye") print("Bye")
self.quit.emit() self.end.emit()
return
elif line[0] == 'q':
print ("Quit")
self.quit.emit()
return return
else: else:
split = line.split()
try: try:
split = line.split() # check for format "NUMBER text"
possible_val = split[0] possible_val = split[0]
value = int(possible_val) value = int(possible_val)
if 100 < value < 0:
raise ValueError
text = ' '.join(split[1:]) text = ' '.join(split[1:])
self.progress.emit(value) self.progress.emit(value)
self.text.emit(text)
if value == 100:
self.end.emit()
return
except (ValueError, IndexError): except (ValueError, IndexError):
text = line text = line
self.signal.emit(text) self.log.append(text)
time.sleep(0.1) try:
if split[0] == '@ERROR@':
self.error.emit()
except IndexError:
pass
def run_gui(): def run_gui():
app = QApplication(sys.argv) app = QApplication(sys.argv)
thread = MyThread() thread = MyThread()
progress = QProgressDialog() progress = QProgressDialog()
progress.setValue(0) progress.setValue(0)
thread.signal.connect(progress.setLabelText) progress.setAutoClose(True)
thread.quit.connect(progress.cancel)
thread.progress.connect(progress.setValue) thread.text.connect (progress.setLabelText)
thread.start() thread.end.connect (progress.cancel)
progress.exec_() thread.progress.connect (progress.setValue)
thread.setTerminationEnabled(True)
thread.start()
progress.exec_()
if progress.wasCanceled:
print 'Cancel was pressed'
thread.force_quit = True
thread.wait()
print "end"
def main(): def main():
signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGINT, signal.SIG_DFL)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment