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

Add custom dialog to catch events

parent 75a00695
#!/usr/bin/env python
from __future__ import print_function
import sys
import time
import signal
import os
from collections import deque
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,
QMessageBox,
QDialog,
QPushButton,
QProgressDialog)
log = deque()
class MyThread(QThread, QObject):
''' Thread waiting for data on stdin and sending signals to the prgress
bar in case something came in.
......@@ -26,7 +36,6 @@ class MyThread(QThread, QObject):
progress = Signal(int)
end = Signal()
error = Signal()
log = deque()
force_quit = False
def run(self):
......@@ -56,34 +65,80 @@ class MyThread(QThread, QObject):
return
except (ValueError, IndexError):
text = line
self.log.append(text)
log.append(text)
try:
if split[0] == '@ERROR@':
self.error.emit()
return
except IndexError:
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():
app = QApplication(sys.argv)
thread = MyThread()
progress = QProgressDialog()
app = QApplication(sys.argv)
thread = MyThread()
dialog = MyDialog()
progress = dialog.bar
progress.setValue(0)
progress.setAutoClose(True)
thread.text.connect (progress.setLabelText)
thread.end.connect (progress.cancel)
thread.text.connect (dialog.label.setText)
thread.end.connect (dialog.complete_or_cancel)
thread.end.connect (dialog.close)
thread.progress.connect (progress.setValue)
thread.setTerminationEnabled(True)
thread.error.connect (handle_error)
thread.start()
progress.exec_()
if progress.wasCanceled:
print 'Cancel was pressed'
thread.force_quit = True
dialog.exec_()
thread.wait()
print "end"
def main():
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