# -*- coding: iso-8859-1 -*-

"""
Module implementing ThreadsDialog.
"""

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from Ui_Threads import Ui_Ui_Threads
import time
import sys

class ThreadsDialog(QDialog, Ui_Ui_Threads):
    """
    Class documentation goes here.
    """
    def __init__(self, parent = None):
        """
        Constructor
        """
        QDialog.__init__(self, parent)
        self.setupUi(self)
       
    
    def result(self,  message):
      self.btnOK.setEnabled(True)        
      QMessageBox.information(None, '', message)            
      
    
    @pyqtSignature("")
    def on_btnOK_clicked(self):
      self.btnOK.setEnabled(False)
      self.theThread = MyThread(self.spinBox.value(),  self.parent())
      self.connect(self.theThread, SIGNAL("serviceFinished(QString)"), self.result)  
      self.theThread.start()



class MyThread(QThread):

    def __init__(self,  maxIt,  parent = None):
        QThread.__init__(self, parent)
        self.maxIt = maxIt
        print "created"

    def run(self):
	print "started", self.maxIt
        for i in range(self.maxIt):
           print i

        self.emit(SIGNAL("serviceFinished(QString)"),  'Thread finished after '+str(i)+' iterations. Finished: ')
        pass      


a = QApplication(sys.argv)
dlg = ThreadsDialog()
dlg.show()
a.exec_()