#!/usr/bin/env python

import sys
from PyQt4 import QtGui, QtCore
from Jixi.connectionPanel import connectionPanel
from Jixi.statusPanel import statusPanel
from Jixi.commandPanel import commandPanel
from Jixi.consolePanel import consolePanel
from Jixi.jSerialThread import jRead
from Jixi.jConfig import jConfig
from Jixi.jComPort import jComPort
import Jixi.jStatus
import serial

class jixiApp(QtGui.QApplication):

  def __init__(self, args):
    super(jixiApp, self).__init__(args)
    self.config = jConfig()

class mainWindow(QtGui.QMainWindow):

    def __init__(self):
        super(mainWindow, self).__init__()
        self.setWindowIcon(QtGui.QIcon('web.png'))
        self.statusBar().showMessage('Ready')
        c = QtGui.QWidget()
        self.setCentralWidget(c)

        grid = QtGui.QGridLayout()
        c.setLayout(grid)

        self.connectionw = connectionPanel()
        grid.addWidget(self.connectionw, 0,0)
        self.statusw = statusPanel()
        grid.addWidget(self.statusw, 1,0)
        grid.addWidget(commandPanel(), 0,1, 2, 1)
        grid.addWidget(consolePanel(), 2,0, 1, 2)

        self.comport = jComPort()
        #self.comport.open()
        self.readThread = jRead(self.comport)
        self.readThread.start()
        self.connect(self.readThread, QtCore.SIGNAL("read_data(QString)"), self.read_data)

        self.show()

    def read_data(self, txt):
        txt = str(txt).strip()
        Jixi.jStatus.msg(txt)
        if (txt[0]=='<' and txt[-1]=='>'):
            # looks like a status message
            self.statusw.update(txt)

app = jixiApp(sys.argv)
w = mainWindow()
sys.exit(app.exec_())


