class SampleGUIClientWindow(QMainWindow):
    def __init__(self, parent=None):
        super(SampleGUIClientWindow, self).__init__(parent)

        self.create_main_frame()
        self.create_client()
        self.create_timers()

    def create_main_frame(self):
        self.circle_widget = CircleWidget()
        self.doit_button = QPushButton('Do it!')
        self.doit_button.clicked.connect(self.on_doit)
        self.log_widget = LogWidget()

        hbox = QHBoxLayout()
        hbox.addWidget(self.circle_widget)
        hbox.addWidget(self.doit_button)
        hbox.addWidget(self.log_widget)

        main_frame = QWidget()
        main_frame.setLayout(hbox)

        self.setCentralWidget(main_frame)

    def create_client(self):
        self.client = SocketClientThread()
        self.client.start()

    def create_timers(self):
        self.circle_timer = QTimer(self)
        self.circle_timer.timeout.connect(self.circle_widget.next)
        self.circle_timer.start(25)

        self.client_reply_timer = QTimer(self)
        self.client_reply_timer.timeout.connect(self.on_client_reply_timer)
        self.client_reply_timer.start(100)

    def on_doit(self):
        self.client.cmd_q.put(ClientCommand(ClientCommand.CONNECT,
                                            SERVER_ADDR))
        self.client.cmd_q.put(ClientCommand(ClientCommand.SEND, 'hello'))
        self.client.cmd_q.put(ClientCommand(ClientCommand.RECEIVE))
        self.client.cmd_q.put(ClientCommand(ClientCommand.CLOSE))

    def on_client_reply_timer(self):
        try:
            reply = self.client.reply_q.get(block=False)
            status = "SUCCESS" if reply.type == ClientReply.SUCCESS else "ERROR"
            self.log('Client reply %s: %s' % (status, reply.data))
        except queue.Empty:
            pass

    def log(self, msg):
        timestamp = '[%010.3f]' % time.clock()
        self.log_widget.append(timestamp + ' ' + str(msg))
class SampleGUIClientWindow(QMainWindow):
    def __init__(self, parent=None):
        super(SampleGUIClientWindow, self).__init__(parent)
        
        self.create_main_frame()
        self.create_client()
        self.create_timers()
    
    def create_main_frame(self):
        self.circle_widget = CircleWidget()
        self.doit_button = QPushButton('Do it!')
        #self.connect(self.doit_button, SIGNAL("Clicked()"), self.Button_clicked)
        #self.doit_button.clicked.connect(self.on_doit)
        self.log_widget = LogWidget()
        
        hbox = QHBoxLayout()
        hbox.addWidget(self.circle_widget)
        hbox.addWidget(self.doit_button)
        hbox.addWidget(self.log_widget)
        
        main_frame = QWidget()
        main_frame.setLayout(hbox)
        
        self.setCentralWidget(main_frame)
    
    def create_client(self):
        self.client = SocketClientThread()
        self.client.start()
        
    def create_timers(self):
        self.circle_timer = QTimer(self)
        #self.circle_timer.timeout.connect(self.circle_widget.next)
        self.circle_timer.start(25)
        
        self.client_reply_timer = QTimer(self)
        #self.client_reply_timer.timeout.connect(self.on_client_reply_timer)
        self.client_reply_timer.start(100)
    
    def on_doit(self):
        self.client.cmd_q.put(ClientCommand(ClientCommand.CONNECT, SERVER_ADDR))
        self.client.cmd_q.put(ClientCommand(ClientCommand.SEND, 'hello'))
        self.client.cmd_q.put(ClientCommand(ClientCommand.RECEIVE))
        self.client.cmd_q.put(ClientCommand(ClientCommand.CLOSE))

    def on_client_reply_timer(self):
        try:
            reply = self.client.reply_q.get(block=False)
            status = "SUCCESS" if reply.type == ClientReply.SUCCESS else "ERROR"
            self.log('Client reply %s: %s' % (status, reply.data))
        except Queue.Empty:
            pass

    def log(self, msg):
        timestamp = '[%010.3f]' % time.clock()
        self.log_widget.append(timestamp + ' ' + str(msg))
 def create_client(self):
     self.client = SocketClientThread()
     self.client.start()
 def create_client(self):
     self.client = SocketClientThread()
     self.client.start()