From 09b8d64f4ad2d1d4002bec190c1511b3b77c575d Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Mon, 13 May 2019 20:50:16 -0400 Subject: [PATCH] Add a reset connection button. Automatically disconnects and reconnects to try to heal a broken connection. --- connections/canconmanager.cpp | 5 +++ connections/canconmanager.h | 1 + connections/canconnectionmodel.cpp | 8 ++++ connections/canconnectionmodel.h | 1 + connections/connectionwindow.cpp | 69 +++++++++++++++++++++--------- connections/connectionwindow.h | 1 + ui/connectionwindow.ui | 7 +++ 7 files changed, 71 insertions(+), 21 deletions(-) diff --git a/connections/canconmanager.cpp b/connections/canconmanager.cpp index 2b285b4..6e5020d 100644 --- a/connections/canconmanager.cpp +++ b/connections/canconmanager.cpp @@ -66,6 +66,11 @@ void CANConManager::remove(CANConnection* pConn_p) mConns.removeOne(pConn_p); } +void CANConManager::replace(int idx, CANConnection* pConn_p) +{ + mConns.replace(idx, pConn_p); +} + //Get total number of buses currently registered with the program int CANConManager::getNumBuses() { diff --git a/connections/canconmanager.h b/connections/canconmanager.h index 5a5f9dc..a479dd8 100644 --- a/connections/canconmanager.h +++ b/connections/canconmanager.h @@ -17,6 +17,7 @@ public: void add(CANConnection* pConn_p); void remove(CANConnection* pConn_p); + void replace(int idx, CANConnection* pConn_p); QList& getConnections(); void stopAllConnections(); diff --git a/connections/canconnectionmodel.cpp b/connections/canconnectionmodel.cpp index 12d4f25..6c68564 100644 --- a/connections/canconnectionmodel.cpp +++ b/connections/canconnectionmodel.cpp @@ -126,6 +126,14 @@ void CANConnectionModel::remove(CANConnection* pConn_p) endResetModel(); } +void CANConnectionModel::replace(int idx , CANConnection* pConn_p) +{ + CANConManager* manager = CANConManager::getInstance(); + + beginResetModel(); + manager->replace(idx, pConn_p); + endResetModel(); +} CANConnection* CANConnectionModel::getAtIdx(int pIdx) const { diff --git a/connections/canconnectionmodel.h b/connections/canconnectionmodel.h index 8ea3ed3..5140eae 100644 --- a/connections/canconnectionmodel.h +++ b/connections/canconnectionmodel.h @@ -25,6 +25,7 @@ public: void add(CANConnection* pConn_p); void remove(CANConnection* pConn_p); + void replace(int idx , CANConnection* pConn_p); CANConnection* getAtIdx(int) const; void refresh(int pIndex=-1); diff --git a/connections/connectionwindow.cpp b/connections/connectionwindow.cpp index c14158a..56dff96 100644 --- a/connections/connectionwindow.cpp +++ b/connections/connectionwindow.cpp @@ -55,6 +55,7 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : connect(ui->ckEnableConsole, &QCheckBox::toggled, this, &ConnectionWindow::consoleEnableChanged); connect(ui->btnClearDebug, &QPushButton::clicked, this, &ConnectionWindow::handleClearDebugText); connect(ui->btnNewConnection, &QPushButton::clicked, this, &ConnectionWindow::handleNewConn); + connect(ui->btnResetConn, &QPushButton::clicked, this, &ConnectionWindow::handleResetConn); connect(ui->tableConnections->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &ConnectionWindow::currentRowChanged); connect(ui->tabBuses, &QTabBar::currentChanged, this, &ConnectionWindow::currentTabChanged); connect(ui->btnSaveBus, &QPushButton::clicked, this, &ConnectionWindow::saveBusSettings); @@ -207,6 +208,53 @@ void ConnectionWindow::handleNewConn() delete thisDialog; } +void ConnectionWindow::handleRemoveConn() +{ + int selIdx = ui->tableConnections->selectionModel()->currentIndex().row(); + if (selIdx <0) return; + + qDebug() << "remove connection at index: " << selIdx; + + CANConnection* conn_p = connModel->getAtIdx(selIdx); + if(!conn_p) return; + + /* remove connection from model & manager */ + connModel->remove(conn_p); + + /* stop and delete connection */ + conn_p->stop(); + delete conn_p; + + /* select first connection in list */ + ui->tableConnections->selectRow(0); +} + +void ConnectionWindow::handleResetConn() +{ + QString port, driver; + CANCon::type type; + + int selIdx = ui->tableConnections->selectionModel()->currentIndex().row(); + if (selIdx <0) return; + + qDebug() << "remove connection at index: " << selIdx; + + CANConnection* conn_p = connModel->getAtIdx(selIdx); + if(!conn_p) return; + + type = conn_p->getType(); + port = conn_p->getPort(); + driver = conn_p->getDriver(); + + /* stop and delete connection */ + conn_p->stop(); + + conn_p = nullptr; + + conn_p = create(type, port, driver); + if (conn_p) connModel->replace(selIdx, conn_p); +} + /* status */ void ConnectionWindow::connectionStatus(CANConStatus pStatus) { @@ -365,27 +413,6 @@ void ConnectionWindow::handleSendText() { emit sendDebugData(bytes); } -void ConnectionWindow::handleRemoveConn() -{ - int selIdx = ui->tableConnections->selectionModel()->currentIndex().row(); - if (selIdx <0) return; - - qDebug() << "remove connection at index: " << selIdx; - - CANConnection* conn_p = connModel->getAtIdx(selIdx); - if(!conn_p) return; - - /* remove connection from model & manager */ - connModel->remove(conn_p); - - /* stop and delete connection */ - conn_p->stop(); - delete conn_p; - - /* select first connection in list */ - ui->tableConnections->selectRow(0); -} - CANConnection* ConnectionWindow::create(CANCon::type pTye, QString pPortName, QString pDriver) { CANConnection* conn_p; diff --git a/connections/connectionwindow.h b/connections/connectionwindow.h index 1af482f..22769fc 100644 --- a/connections/connectionwindow.h +++ b/connections/connectionwindow.h @@ -46,6 +46,7 @@ private slots: void consoleEnableChanged(bool checked); void handleRemoveConn(); void handleNewConn(); + void handleResetConn(); void handleClearDebugText(); void handleSendHex(); void handleSendText(); diff --git a/ui/connectionwindow.ui b/ui/connectionwindow.ui index cbc6119..12a6ada 100644 --- a/ui/connectionwindow.ui +++ b/ui/connectionwindow.ui @@ -43,6 +43,13 @@ + + + + Reset Selected Device + + +