mirror of
https://github.com/CopterExpress/clever-show.git
synced 2026-06-02 18:19:33 +00:00
Added indication of connected/disconnected copters
This commit is contained in:
@@ -7,6 +7,9 @@ from PyQt5 import QtCore, QtGui, QtWidgets
|
||||
from PyQt5.QtCore import Qt as Qt
|
||||
|
||||
|
||||
ModelStateRole = 999
|
||||
|
||||
|
||||
class CopterData:
|
||||
class_basic_attrs = indexed.IndexedOrderedDict([('copter_id', None), ('anim_id', None),
|
||||
('batt_v', None), ('batt_p', None),
|
||||
@@ -108,6 +111,14 @@ class CopterDataModel(QtCore.QAbstractTableModel):
|
||||
contents = contents or self.data_contents
|
||||
return filter(lambda x: calibration_ready_check(x), contents)
|
||||
|
||||
def get_row_by_id(self, copter_id):
|
||||
try:
|
||||
row = next(filter(lambda x: x.copter_id == copter_id, self.data_contents))
|
||||
except StopIteration:
|
||||
return None
|
||||
else:
|
||||
return self.data_contents.index(row)
|
||||
|
||||
def rowCount(self, n=None):
|
||||
return len(self.data_contents)
|
||||
|
||||
@@ -164,9 +175,10 @@ class CopterDataModel(QtCore.QAbstractTableModel):
|
||||
|
||||
if role == Qt.CheckStateRole:
|
||||
self.data_contents[index.row()].states.checked = value
|
||||
|
||||
elif role == Qt.EditRole:
|
||||
self.data_contents[index.row()][index.column()] = value
|
||||
elif role == ModelStateRole:
|
||||
self.data_contents[index.row()].states[index.column()] = value
|
||||
else:
|
||||
return False
|
||||
|
||||
@@ -185,9 +197,9 @@ class CopterDataModel(QtCore.QAbstractTableModel):
|
||||
roles |= Qt.ItemIsUserCheckable #| Qt.ItemIsEditable
|
||||
return roles
|
||||
|
||||
@QtCore.pyqtSlot(int, int, QtCore.QVariant)
|
||||
def update_item(self, row, col, value):
|
||||
self.setData(self.index(row, col), value)
|
||||
@QtCore.pyqtSlot(int, int, QtCore.QVariant, QtCore.QVariant)
|
||||
def update_item(self, row, col, value, role=Qt.EditRole):
|
||||
self.setData(self.index(row, col), value, role)
|
||||
|
||||
@QtCore.pyqtSlot(object)
|
||||
def add_client(self, client):
|
||||
@@ -317,7 +329,7 @@ class CopterProxyModel(QtCore.QSortFilterProxyModel):
|
||||
|
||||
|
||||
class SignalManager(QtCore.QObject):
|
||||
update_data_signal = QtCore.pyqtSignal(int, int, QtCore.QVariant)
|
||||
update_data_signal = QtCore.pyqtSignal(int, int, QtCore.QVariant, QtCore.QVariant)
|
||||
add_client_signal = QtCore.pyqtSignal(object)
|
||||
|
||||
|
||||
|
||||
@@ -111,6 +111,11 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
def new_client_connected(self, client: Client):
|
||||
self.signals.add_client_signal.emit(StatedCopterData(copter_id=client.copter_id, client=client))
|
||||
|
||||
def client_connection_changed(self, client: Client):
|
||||
row = self.model.get_row_by_id(client.copter_id)
|
||||
if row is not None:
|
||||
self.signals.update_data_signal.emit(row, 0, client.connected, ModelStateRole)
|
||||
|
||||
def init_ui(self):
|
||||
# Connecting
|
||||
self.ui.check_button.clicked.connect(self.selfcheck_selected)
|
||||
@@ -166,8 +171,10 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
client.get_response("time", self._set_copter_data, callback_args=(8, copter.copter_id))
|
||||
|
||||
def _set_copter_data(self, value, col, copter_id):
|
||||
row = self.model.data_contents.index(next(
|
||||
filter(lambda x: x.copter_id == copter_id, self.model.data_contents)))
|
||||
row = self.model.get_row_by_id(copter_id)
|
||||
if row is None:
|
||||
logging.error("No such client!")
|
||||
return
|
||||
|
||||
if col == 1:
|
||||
data = value
|
||||
@@ -194,7 +201,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
print("No column matched for response")
|
||||
return
|
||||
|
||||
self.signals.update_data_signal.emit(row, col, data)
|
||||
self.signals.update_data_signal.emit(row, col, data, Qt.EditRole)
|
||||
|
||||
@pyqtSlot()
|
||||
@confirmation_required("This operation will takeoff selected copters with delay and start animation. Proceed?")
|
||||
@@ -493,7 +500,11 @@ if __name__ == "__main__":
|
||||
#app.exec_()
|
||||
with loop:
|
||||
window = MainWindow()
|
||||
|
||||
Client.on_first_connect = window.new_client_connected
|
||||
Client.on_connect = window.client_connection_changed
|
||||
Client.on_disconnect = window.client_connection_changed
|
||||
|
||||
server = Server(on_stop=app.quit)
|
||||
server.start()
|
||||
loop.run_forever()
|
||||
|
||||
Reference in New Issue
Block a user