Commit 556892be authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Add custom dialog to catch events

parent 75a00695
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys import sys
import time import time
import signal import signal
import os
from collections import deque from collections import deque
import PySide import PySide
from PySide.QtCore import QThread, Signal, QObject from PySide import QtGui
from PySide.QtCore import QThread, Signal, QObject, Qt, Slot
from PySide.QtGui import (QApplication, from PySide.QtGui import (QApplication,
QMessageBox, QMessageBox,
QDialog,
QPushButton,
QProgressDialog) QProgressDialog)
log = deque()
class MyThread(QThread, QObject): class MyThread(QThread, QObject):
''' Thread waiting for data on stdin and sending signals to the prgress ''' Thread waiting for data on stdin and sending signals to the prgress
bar in case something came in. bar in case something came in.
...@@ -26,7 +36,6 @@ class MyThread(QThread, QObject): ...@@ -26,7 +36,6 @@ class MyThread(QThread, QObject):
progress = Signal(int) progress = Signal(int)
end = Signal() end = Signal()
error = Signal() error = Signal()
log = deque()
force_quit = False force_quit = False
def run(self): def run(self):
...@@ -56,34 +65,80 @@ class MyThread(QThread, QObject): ...@@ -56,34 +65,80 @@ class MyThread(QThread, QObject):
return return
except (ValueError, IndexError): except (ValueError, IndexError):
text = line text = line
self.log.append(text) log.append(text)
try: try:
if split[0] == '@ERROR@': if split[0] == '@ERROR@':
self.error.emit() self.error.emit()
return
except IndexError: except IndexError:
pass pass
class MyDialog(QDialog):
def __init__(self):
super(MyDialog, self).__init__()
self.bar = QtGui.QProgressBar()
self.more_button = QtGui.QPushButton("Details")
self.extension = QtGui.QWidget()
self.log_window = QtGui.QListWidget()
self.label = QtGui.QLabel()
# Layouts
self.top_layout = QtGui.QVBoxLayout()
self.ext_layout = QtGui.QVBoxLayout()
self.main_layout = QtGui.QVBoxLayout()
self.more_button.setCheckable(True)
self.more_button.hide()
self.more_button.toggled.connect(self.log_window.setVisible)
self.top_layout.addWidget(self.label)
self.top_layout.addWidget(self.bar)
#self.top_layout.addWidget(self.more_button)
self.top_layout.setStretch(0, 0)
self.top_layout.setStretch(1, 0)
self.top_layout.setStretch(2, 1)
self.main_layout.addLayout(self.top_layout)
self.main_layout.addWidget(self.log_window)
self.setLayout(self.main_layout)
self.main_layout.setStretch(2, 1)
self.setWindowTitle("TASTE")
self.extension.hide()
self.log_window.hide()
self.done = False
@Slot()
def complete_or_cancel(self):
self.done = True
def closeEvent(self, e):
if not self.done:
e.ignore()
def handle_error():
print("== An error occured, here is the log ==")
print("\n".join(log))
def run_gui(): def run_gui():
app = QApplication(sys.argv) app = QApplication(sys.argv)
thread = MyThread() thread = MyThread()
progress = QProgressDialog() dialog = MyDialog()
progress = dialog.bar
progress.setValue(0) progress.setValue(0)
progress.setAutoClose(True)
thread.text.connect (progress.setLabelText) thread.text.connect (dialog.label.setText)
thread.end.connect (progress.cancel) thread.end.connect (dialog.complete_or_cancel)
thread.end.connect (dialog.close)
thread.progress.connect (progress.setValue) thread.progress.connect (progress.setValue)
thread.setTerminationEnabled(True) thread.error.connect (handle_error)
thread.start() thread.start()
progress.exec_() dialog.exec_()
if progress.wasCanceled:
print 'Cancel was pressed'
thread.force_quit = True
thread.wait() thread.wait()
print "end"
def main(): def main():
signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGINT, signal.SIG_DFL)
......
Markdown is supported
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