class Terminals(QWidget): ready = pyqtSignal(bool) def __init__(self, parent=None): QWidget.__init__(self, parent) self.ui = uic.loadUiType('terminal.ui')[0]() self.ui.setupUi(self) self.notifier = QSystemTrayIcon(QIcon('arrow-up-icon.png'), self) self.notifier.show() self.model = None self.mainloop = None self.delegate = None def test_display(self): if self.mainloop: self.mainloop.test_display() def update_device_config(self): if self.mainloop: self.mainloop.update_config() def terminal_open(self, addr): if self.mainloop: self.mainloop.terminal_open(addr) def terminal_close(self, addr): if self.mainloop: self.mainloop.terminal_close(addr) def start_mainloop(self): if self.mainloop is None: self.mainloop = Mainloop(parent=self) self.mainloop.ready.connect(self.on_mainloop_ready) self.mainloop.notify.connect(lambda title, msg: self.notifier.showMessage(title, msg)) self.mainloop.start() def stop_mainloop(self): if self.mainloop: self.mainloop.state.disconnect() self.mainloop.ready.disconnect() self.mainloop.notify.disconnect() [self.ui.terminals.closePersistentEditor(self.model.index(row, 0)) for row in xrange(self.model.rowCount())] self.mainloop.stop() self.mainloop = None def update_model(self): self.stop_mainloop() self.start_mainloop() def on_mainloop_ready(self, ok, titles): if ok: self.model = QStandardItemModel(len(titles), 1) [self.model.setItem(i, QStandardItem(str(addr))) for i, addr in enumerate(titles.keys())] self.delegate = TerminalDelegate(self.mainloop, titles) self.mainloop.report.connect(self.delegate.report) self.mainloop.state.connect(self.delegate.state) self.ui.terminals.setModel(self.model) self.ui.terminals.setItemDelegateForColumn(0, self.delegate) [self.ui.terminals.openPersistentEditor(self.model.index(row, 0)) for row in xrange(self.model.rowCount())] self.mainloop.db.free_places_update.connect(self.ui.free_places.setValue) self.mainloop.update_config() else: self.model = None self.mainloop = None self.ready.emit(ok)
class Terminals(QWidget): ready = pyqtSignal(bool) def __init__(self, parent=None): QWidget.__init__(self, parent) self.ui = uic.loadUiType('terminal.ui')[0]() self.ui.setupUi(self) self.notifier = QSystemTrayIcon(QIcon('arrow-up-icon.png'), self) self.notifier.show() self.session = None self.model = None self.devices = {} self.mainloop = None self.delegate = None def test_display(self): if self.mainloop: self.mainloop.test_display() def update_device_config(self): if self.mainloop: self.mainloop.update_config() def terminal_open(self, addr): if self.mainloop: self.mainloop.terminal_open(addr) def terminal_close(self, addr): if self.mainloop: self.mainloop.terminal_close(addr) def begin_session(self, fio, access): if access in ['operator']: self.ui.cashier.setText(fio) self.session = (fio, access) return True return False def end_session(self, block=False): self.ui.cashier.setText(u'') self.session = None self.stop_mainloop(block) return True def on_mainloop_stopped(self): self.mainloop = None if self.session: self.start_mainloop() def start_mainloop(self): if self.mainloop is None: self.mainloop = Mainloop(devices=self.devices) self.mainloop.ready.connect(self.on_mainloop_ready) self.mainloop.stopped.connect(self.on_mainloop_stopped) self.mainloop.notify.connect(lambda title, msg: self.notifier.showMessage(title, msg)) self.mainloop.start() def stop_mainloop(self, block=False): if self.mainloop: try: self.mainloop.state.disconnect() self.mainloop.ready.disconnect() self.mainloop.notify.disconnect() except TypeError: print 'NOTE: mainloop signals disconnect' if self.model: [self.ui.terminals.closePersistentEditor(self.model.index(row, 0)) for row in xrange(self.model.rowCount())] self.mainloop.stop(block) else: self.ready.emit(False) def reset_mainloop(self, devices): self.devices = devices if self.mainloop is None: self.start_mainloop() else: self.stop_mainloop() def on_mainloop_ready(self, ok, titles): if ok: self.model = QStandardItemModel(len(titles), 1) [self.model.setItem(i, QStandardItem(str(addr))) for i, addr in enumerate(titles.keys())] self.delegate = TerminalDelegate(self.mainloop, titles) self.mainloop.report.connect(self.delegate.report) self.mainloop.state.connect(self.delegate.state) self.ui.terminals.setModel(self.model) self.ui.terminals.setItemDelegateForColumn(0, self.delegate) [self.ui.terminals.openPersistentEditor(self.model.index(row, 0)) for row in xrange(self.model.rowCount())] self.mainloop.db.free_places_update.connect(self.ui.free_places.setValue) self.mainloop.update_config() else: self.model = None self.mainloop = None self.ready.emit(ok)