def __init__(self, host, port): QtGui.QMainWindow.__init__(self) # load the design from the .ui file self.ui = uic.loadUi("./resources/mainwindow.ui") self.ui.show() self.CLIENT = Client(host, port) # connect all the signals -> slots, event filters eventHandler = LineEditEventHandler(self) self.ui.lineEdit.installEventFilter(eventHandler) self.connect(self.ui.lineEdit, QtCore.SIGNAL("returnPressed()"), self.doAction) self.connect(self.ui.actionQuit, QtCore.SIGNAL('triggered()'), QtGui.qApp, QtCore.SLOT('quit()')) self.connect(self.ui.actionAbout, QtCore.SIGNAL("activated()"), self.about) self.thread = Worker() self.connect(self.thread, QtCore.SIGNAL("data"), self.update) self.thread.listen(self.CLIENT.socket) # set up instance vars self.connected = False self.client_id = "" # And any settings # No vertical headers for the table self.ui.tableWidget.verticalHeader().setVisible(False) # No sorting allowed, must always be sorted by increasing priority self.ui.tableWidget.setSortingEnabled(False) # Give focus to the line edit self.ui.lineEdit.setFocus()
class Tasker(QtGui.QMainWindow): """ This is my main application class. It creates all the windows and connects all the signals/slots """ def __init__(self, host, port): QtGui.QMainWindow.__init__(self) # load the design from the .ui file self.ui = uic.loadUi("./resources/mainwindow.ui") self.ui.show() self.CLIENT = Client(host, port) # connect all the signals -> slots, event filters eventHandler = LineEditEventHandler(self) self.ui.lineEdit.installEventFilter(eventHandler) self.connect(self.ui.lineEdit, QtCore.SIGNAL("returnPressed()"), self.doAction) self.connect(self.ui.actionQuit, QtCore.SIGNAL('triggered()'), QtGui.qApp, QtCore.SLOT('quit()')) self.connect(self.ui.actionAbout, QtCore.SIGNAL("activated()"), self.about) self.thread = Worker() self.connect(self.thread, QtCore.SIGNAL("data"), self.update) self.thread.listen(self.CLIENT.socket) # set up instance vars self.connected = False self.client_id = "" # And any settings # No vertical headers for the table self.ui.tableWidget.verticalHeader().setVisible(False) # No sorting allowed, must always be sorted by increasing priority self.ui.tableWidget.setSortingEnabled(False) # Give focus to the line edit self.ui.lineEdit.setFocus() def update(self, data): obj = json.loads(data) # there will always be an update message # and always have the data to update try: if obj['type'] == 'error': self.ui.plainTextEdit.appendHtml("<span style='background-color: red'>ERROR: %s</span>" % obj['update']) else: self.ui.plainTextEdit.appendPlainText(obj['update']) self.updateTaskTable(obj['data']) except KeyError: pass def doAction(self): """ This method (slot) is responsible for sending the data to the server and updating the text box, and table with the response """ text = str(self.ui.lineEdit.text()) self.ui.lineEdit.clear() try: command = text.split()[0] except IndexError: return if not command == "connect" and not self.connected: self.ui.plainTextEdit.appendHtml("<span style='background-color: red'>ERROR: You must connect first</span>") return if command == 'connect': if self.connected: self.ui.plainTextEdit.appendHtml("<span style='background-color: red'>ERROR: You're already connected!</span>") return self.connectToServer(text) elif command == 'addTask': self.addTask(text) elif command == "prioritize": self.setPriority(text) elif command == "accept": self.accept(text) elif command == 'complete': self.complete(text) else: self.ui.plainTextEdit.appendHtml("<span style='background-color: red'>ERROR: Command <strong>'%s'</strong> not recognized</span>" % text) return def connectToServer(self, text): """ Responsible for connecting to the server, setting the client name """ self.CLIENT.connect() self.CLIENT.send({'command': text, 'client_id': self.client_id}) self.connected = True self.client_id = ' '.join(text.split()[1:]) def addTask(self, text): """ Add a task to the table """ if len(text.split()) < 2: self.ui.plainTextEdit.appendHtml("<span style='background-color: red'>ERROR: You must provide a task name</span>") return self.CLIENT.send({'command': text, 'client_id': self.client_id}) def setPriority(self, text): """ Responsible for sending along the priority and task name """ if len(text.split()) < 3: self.ui.plainTextEdit.appendHtml("<span style='background-color: red'>ERROR: You must provide a task name and priority</span>") return self.CLIENT.send({'command': text, 'client_id': self.client_id}) def accept(self, text): """ Responsible for sending along the completer name """ if len(text.split()) < 2: self.ui.plainTextEdit.appendHtml("<span style='background-color: red'>ERROR: You must provide a task name</span>") return self.CLIENT.send({'command': text, 'client_id': self.client_id}) def complete(self, text): """ Responsible for sending the task name and completion percentage """ if len(text.split()) < 3: self.ui.plainTextEdit.appendHtml("<span style='background-color: red'>ERROR: You must provide a task " "name and completion percentage (0 - 100)</span>") return if int(text.split()[-1]) < 0 or int(text.split()[-1]) > 100: self.ui.plainTextEdit.appendHtml("<span style='background-color: red'>ERROR: Your completion value must be " "between 0 and 100 inclusive (0 - 100)</span>") return self.CLIENT.send({'command': text, 'client_id': self.client_id}) def updateTaskTable(self, table): """ This method updates the task table with the data sent to it. The data is in the form of: [["Item 1", "Completer", "5", "12"], ["Item 2", "", "2", "0"]]""" self.ui.tableWidget.setRowCount(len(table)) for y, row in enumerate(table): for x, cell in enumerate(row): item = QtGui.QTableWidgetItem(cell) item.setTextAlignment(QtCore.Qt.AlignCenter) item.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEnabled) self.ui.tableWidget.setItem(y, x, item) def about(self): """ Show our about dialog """ msgbox = QtGui.QMessageBox(self) msgbox.setIcon(QtGui.QMessageBox.Information) msgbox.setTextFormat(QtCore.Qt.RichText) msgbox.setText("<h1 align='center'>pyTasker</h1>" "<p>Written by <a href='mailto:[email protected]'>Nicholas Presta</a> (#0553282) on October 9th</p>" "<p>This application was written in <a href='http://www.python.org/'>Python</a> " "and <a href='http://qt.nokia.com'>Qt</a>, developed on " "<a href='http://www.opensuse.org/en/'>OpenSuse Linux</a> in <a href='http://www.vim.org/'>Vim</a>.</p>" "<p>Icons are from the <a href='http://www.oxygen-icons.org/'>Oxygen</a> set and are licensed under the " "Attribution-ShareAlike 3.0 Unported license.</p>") msgbox.setWindowTitle("About pyTasker") msgbox.setWindowIcon(QtGui.QIcon("./resources/icon.png")) msgbox.exec_()
socket.send('id', socket.id) print("[INFO] Connected!") @sio.event def connect_error(): print("[ERROR] The connection failed!") @sio.event def disconnect(): socket.isConnected = False print("[INFO] Disconnected!") socket = Client(sio, "CH0001") running = True while running: option = input( ' -pres q to exit \n -pres p update position \n -pres s to update status\n' ) if option == 'q': running = False elif option == 'p': socket.update_position(randrange(10), randrange(10)) elif option == 's': socket.update_status(randrange(3)) socket.disconnect()