From ec49413f6153dc04168a7e5cd30b26ddbe2af184 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Tue, 5 Mar 2019 21:25:32 -0500 Subject: [PATCH] Major revamp of the connection window. Hopefully it is more clear and intuitive now. --- SavvyCAN.pro | 9 +- config.h | 2 +- connections/canbus.cpp | 2 +- connections/canbus.h | 2 +- connections/canconmanager.cpp | 11 + connections/canconmanager.h | 1 + connections/canconnection.cpp | 2 +- connections/canconnectionmodel.cpp | 154 ++-------- connections/canconnectionmodel.h | 5 +- connections/connectionwindow.cpp | 420 ++++++++-------------------- connections/connectionwindow.h | 35 +-- connections/gvretserial.cpp | 6 +- connections/newconnectiondialog.cpp | 177 ++++++++++++ connections/newconnectiondialog.h | 47 ++++ connections/serialbusconnection.cpp | 4 +- ui/connectionwindow.ui | 286 +++++++------------ ui/newconnectiondialog.ui | 109 ++++++++ 17 files changed, 611 insertions(+), 661 deletions(-) create mode 100644 connections/newconnectiondialog.cpp create mode 100644 connections/newconnectiondialog.h create mode 100644 ui/newconnectiondialog.ui diff --git a/SavvyCAN.pro b/SavvyCAN.pro index 0ef31a1..1e02488 100644 --- a/SavvyCAN.pro +++ b/SavvyCAN.pro @@ -66,7 +66,8 @@ SOURCES += main.cpp\ frameplaybackobject.cpp \ helpwindow.cpp \ blfhandler.cpp \ - re/sniffer/SnifferDelegate.cpp + re/sniffer/SnifferDelegate.cpp \ + connections/newconnectiondialog.cpp HEADERS += mainwindow.h \ can_structs.h \ @@ -123,7 +124,8 @@ HEADERS += mainwindow.h \ frameplaybackobject.h \ helpwindow.h \ blfhandler.h \ - re/sniffer/SnifferDelegate.h + re/sniffer/SnifferDelegate.h \ + connections/newconnectiondialog.h FORMS += ui/candatagrid.ui \ ui/connectionwindow.ui \ @@ -150,7 +152,8 @@ FORMS += ui/candatagrid.ui \ ui/udsscanwindow.ui \ ui/bisectwindow.ui \ ui/signalviewerwindow.ui \ - helpwindow.ui + helpwindow.ui \ + ui/newconnectiondialog.ui DISTFILES += diff --git a/config.h b/config.h index d0168bd..3db1f12 100644 --- a/config.h +++ b/config.h @@ -1,7 +1,7 @@ #ifndef CONFIG #define CONFIG -#define VERSION 192 +#define VERSION 193 //try to keep this in sync. //SavvyCAN will complain if you connect a GVRET board with a revision diff --git a/connections/canbus.cpp b/connections/canbus.cpp index 13aeb11..24238f7 100644 --- a/connections/canbus.cpp +++ b/connections/canbus.cpp @@ -40,7 +40,7 @@ void CANBus::setSingleWire(bool mode){ singleWire = mode; } -void CANBus::setEnabled(bool mode){ +void CANBus::setActive(bool mode){ //qDebug() << "CANBUS SetEnabled = " << mode; active = mode; } diff --git a/connections/canbus.h b/connections/canbus.h index 73f945f..fb2d382 100644 --- a/connections/canbus.h +++ b/connections/canbus.h @@ -20,7 +20,7 @@ public: void setSpeed(int); // new speed void setListenOnly(bool); //bool for whether to only listen void setSingleWire(bool); //bool for whether to use single wire mode - void setEnabled(bool); //whether this bus should be enabled or not. + void setActive(bool); //whether this bus should be enabled or not. int getSpeed(); bool isListenOnly(); bool isSingleWire(); diff --git a/connections/canconmanager.cpp b/connections/canconmanager.cpp index 56540d4..20e0b3f 100644 --- a/connections/canconmanager.cpp +++ b/connections/canconmanager.cpp @@ -77,6 +77,17 @@ int CANConManager::getNumBuses() return buses; } +int CANConManager::getBusBase(CANConnection *which) +{ + int buses = 0; + foreach(CANConnection* conn_p, mConns) + { + if (conn_p != which) buses += conn_p->getNumBuses(); + else return buses; + } + return -1; +} + void CANConManager::refreshCanList() { QObject* sender_p = QObject::sender(); diff --git a/connections/canconmanager.h b/connections/canconmanager.h index 5023f25..5a5f9dc 100644 --- a/connections/canconmanager.h +++ b/connections/canconmanager.h @@ -26,6 +26,7 @@ public: void resetTimeBasis(); int getNumBuses(); + int getBusBase(CANConnection *); /** * @brief sendFrame sends a single frame out the desired bus diff --git a/connections/canconnection.cpp b/connections/canconnection.cpp index ec99099..6a41758 100644 --- a/connections/canconnection.cpp +++ b/connections/canconnection.cpp @@ -210,7 +210,7 @@ void CANConnection::setConfigured(int pBusId, bool pConfigured) { bool CANConnection::getBusConfig(int pBusId, CANBus& pBus) { if( pBusId < 0 || pBusId >= getNumBuses() || !isConfigured(pBusId)) return false; - + qDebug() << "getBusConfig id: " << pBusId; pBus = mBusData[pBusId].mBus; return true; } diff --git a/connections/canconnectionmodel.cpp b/connections/canconnectionmodel.cpp index ac07450..12d4f25 100644 --- a/connections/canconnectionmodel.cpp +++ b/connections/canconnectionmodel.cpp @@ -12,14 +12,11 @@ CANConnectionModel::~CANConnectionModel() } enum class Column { - Bus = 0, ///< A sequential number describing the bus - Type = 1, ///< The CAN driver/backend type, e.g. GVRET, peakcan, or socketcan + Type = 0, ///< The CAN driver/backend type, e.g. GVRET, peakcan, or socketcan + Subtype = 1, ///< Mostly used by SerialBus devices to pick the sub type Port = 2, ///< The CAN hardware port, e.g. can0 for socketcan - Speed = 3, ///< The bus speed in bit/second - ListenOnly = 4, ///< True if the bus is in listen-only mode - SingleWire = 5, ///< True if the bus operates in single-wire mode - Active = 6, ///< True if the bus is activated for sending and receiving - Status = 7 ///< The bus status as text message + NumBuses = 3, ///< Number of buses exposed by this device. Usually non-GVRET devices will just have one + Status = 4 ///< The bus status as text message }; QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation, int role) const @@ -31,20 +28,14 @@ QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation { switch (Column(section)) { - case Column::Bus: - return QString(tr("Bus")); case Column::Type: return QString(tr("Type")); + case Column::Subtype: + return QString(tr("Subtype")); case Column::Port: return QString(tr("Port")); - case Column::Speed: - return QString(tr("Speed")); - case Column::ListenOnly: - return QString(tr("Listen Only")); - case Column::SingleWire: - return QString(tr("Single Wire")); - case Column::Active: - return QString(tr("Active")); + case Column::NumBuses: + return QString(tr("Buses")); case Column::Status: return QString(tr("Status")); } @@ -59,7 +50,7 @@ QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation int CANConnectionModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent); - return 8; + return 5; } @@ -67,78 +58,9 @@ int CANConnectionModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); - int rows = 0; QList& conns = CANConManager::getInstance()->getConnections(); - foreach(const CANConnection* conn_p, conns) - rows += conn_p->getNumBuses(); - - //qDebug() << "Num Rows: " << rows; - - return rows; -} - -Qt::ItemFlags CANConnectionModel::flags(const QModelIndex &index) const -{ - if (!index.isValid()) - return Qt::ItemFlag::NoItemFlags; - - int busId; - CANConnection *conn_p = getAtIdx(index.row(), busId); - if (!conn_p) return Qt::ItemFlag::NoItemFlags; - - //socketcan is limited for what you can set but the other serialbus - //devices should be able to set the stuff. For now let anyone try - //and the underlying drivers can do with them as they will - bool editParams = true; - //if (conn_p->getType() == CANCon::GVRET_SERIAL) editParams = true; - - switch (Column(index.column())) - { - case Column::Speed: - if (editParams) return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled; - return Qt::ItemFlag::NoItemFlags; - case Column::ListenOnly: - case Column::SingleWire: - if (editParams) return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsUserCheckable; - return Qt::ItemFlag::NoItemFlags; - case Column::Active: - return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsUserCheckable; - default: - return Qt::ItemFlag::ItemIsEnabled; - } -} - -bool CANConnectionModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - qDebug() << "setData: " << index.row() << ":" << index.column() << " role: " << role << " Val: " << value; - - int busId; - CANConnection *conn_p = getAtIdx(index.row(), busId); - if (!conn_p) return false; - CANBus bus; - bool ret; - ret = conn_p->getBusSettings(busId, bus); - if (!ret) return false; - - switch (Column(index.column())) - { - case Column::Speed: - bus.speed = value.toInt(); - break; - case Column::ListenOnly: - bus.listenOnly = value.toBool(); - break; - case Column::SingleWire: - bus.singleWire = value.toBool(); - break; - case Column::Active: - bus.active = value.toBool(); - break; - default: {} - } - conn_p->setBusSettings(busId, bus); - return true; + return conns.count(); } QVariant CANConnectionModel::data(const QModelIndex &index, int role) const @@ -147,23 +69,16 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const return QVariant(); //qDebug() << "Row: " << index.row(); - int busId; - CANConnection *conn_p = getAtIdx(index.row(), busId); - CANBus bus; + CANConnection *conn_p = getAtIdx(index.row()); bool ret; if (!conn_p) return QVariant(); - ret = conn_p->getBusSettings(busId, bus); - bool isSocketCAN = (conn_p->getType() == CANCon::SERIALBUS) ? true: false; - //qDebug() << "ConnP: " << conn_p << " ret " << ret; + bool isSocketCAN = (conn_p->getType() == CANCon::SERIALBUS) ? true: false; if (role == Qt::DisplayRole) { switch (Column(index.column())) { - case Column::Bus: - //return QString::number(busId); - return QString::number(index.row()); case Column::Type: if (conn_p) switch (conn_p->getType()) { @@ -178,34 +93,16 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const if (conn_p) return conn_p->getPort(); else qDebug() << "Tried to show connection port but connection was NULL"; break; - case Column::Speed: - if(!ret) return QVariant(); - if (!isSocketCAN) return QString::number(bus.speed); - else return QString("N/A"); - case Column::ListenOnly: - return QVariant(); - case Column::SingleWire: - return QVariant(); - case Column::Active: - return QVariant(); + case Column::Subtype: + return conn_p->getDriver(); + break; + case Column::NumBuses: + return conn_p->getNumBuses(); + break; case Column::Status: return (conn_p->getStatus()==CANCon::CONNECTED) ? "Connected" : "Not Connected"; } } - if (role == Qt::CheckStateRole) - { - switch (Column(index.column())) - { - case Column::ListenOnly: - return (bus.listenOnly) ? Qt::Checked : Qt::Unchecked; - case Column::SingleWire: - return (bus.singleWire) ? Qt::Checked : Qt::Unchecked; - case Column::Active: - return (bus.active) ? Qt::Checked : Qt::Unchecked; - default: {} - } - } - return QVariant(); } @@ -230,25 +127,14 @@ void CANConnectionModel::remove(CANConnection* pConn_p) } -CANConnection* CANConnectionModel::getAtIdx(int pIdx, int& pBusId) const +CANConnection* CANConnectionModel::getAtIdx(int pIdx) const { if (pIdx < 0) return NULL; - int i=0; QList& conns = CANConManager::getInstance()->getConnections(); - foreach(CANConnection* conn_p, conns) - { - if( i <= pIdx && pIdx < i+conn_p->getNumBuses() ) { - pBusId = pIdx - i; - return conn_p; - } - - i+= conn_p->getNumBuses(); - } - - return NULL; + return conns.at(pIdx); } void CANConnectionModel::refresh(int pIndex) diff --git a/connections/canconnectionmodel.h b/connections/canconnectionmodel.h index f4a0094..8ea3ed3 100644 --- a/connections/canconnectionmodel.h +++ b/connections/canconnectionmodel.h @@ -21,15 +21,12 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - Qt::ItemFlags flags(const QModelIndex &index) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - int rowCount(const QModelIndex &parent = QModelIndex()) const override; void add(CANConnection* pConn_p); void remove(CANConnection* pConn_p); - CANConnection* getAtIdx(int, int&) const; + CANConnection* getAtIdx(int) const; void refresh(int pIndex=-1); }; diff --git a/connections/connectionwindow.cpp b/connections/connectionwindow.cpp index 07f6804..86c592b 100644 --- a/connections/connectionwindow.cpp +++ b/connections/connectionwindow.cpp @@ -10,6 +10,7 @@ #include "connections/canconmanager.h" #include "canbus.h" #include +#include ConnectionWindow::ConnectionWindow(QWidget *parent) : QDialog(parent), @@ -24,16 +25,15 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : qRegisterMetaType("const CANFrame *"); qRegisterMetaType *>("const QList *"); + + //List of devices with details. None of it can be edited. connection type, serialbus type, port name, number of buses, status connModel = new CANConnectionModel(this); ui->tableConnections->setModel(connModel); - ui->tableConnections->setColumnWidth(0, 40); - ui->tableConnections->setColumnWidth(1, 70); - ui->tableConnections->setColumnWidth(2, 70); + ui->tableConnections->setColumnWidth(0, 100); + ui->tableConnections->setColumnWidth(1, 100); + ui->tableConnections->setColumnWidth(2, 100); ui->tableConnections->setColumnWidth(3, 70); - ui->tableConnections->setColumnWidth(4, 70); - ui->tableConnections->setColumnWidth(5, 70); - ui->tableConnections->setColumnWidth(6, 70); - ui->tableConnections->setColumnWidth(7, 90); + ui->tableConnections->setColumnWidth(4, 200); QHeaderView *HorzHdr = ui->tableConnections->horizontalHeader(); HorzHdr->setStretchLastSection(true); //causes the data column to automatically fill the tableview @@ -47,32 +47,30 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : { /* load connection configuration */ loadConnections(); - } + } - ui->rbSocketCAN->setEnabled(isSerialBusAvailable()); - - connect(ui->btnOK, &QAbstractButton::clicked, this, &ConnectionWindow::handleOKButton); - connect(ui->rbGVRET, &QAbstractButton::clicked, this, &ConnectionWindow::handleConnTypeChanged); - connect(ui->rbSocketCAN, &QAbstractButton::clicked, this, &ConnectionWindow::handleConnTypeChanged); - connect(ui->rbRemote, &QAbstractButton::clicked, this, &ConnectionWindow::handleConnTypeChanged); - connect(ui->cbDeviceType, QOverload::of(&QComboBox::currentIndexChanged), this, &ConnectionWindow::handleDeviceTypeChanged); - connect(ui->tableConnections->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &ConnectionWindow::currentRowChanged); - connect(ui->btnActivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleEnableAll); - connect(ui->btnDeactivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleDisableAll); - connect(ui->btnReconnect, &QPushButton::clicked, this, &ConnectionWindow::handleReconnect); - connect(ui->btnRemoveBus, &QPushButton::clicked, this, &ConnectionWindow::handleRemoveConn); - connect(ui->btnClearDebug, &QPushButton::clicked, this, &ConnectionWindow::handleClearDebugText); + connect(ui->btnDisconnect, &QPushButton::clicked, this, &ConnectionWindow::handleRemoveConn); connect(ui->btnSendHex, &QPushButton::clicked, this, &ConnectionWindow::handleSendHex); connect(ui->btnSendText, &QPushButton::clicked, this, &ConnectionWindow::handleSendText); 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->tableConnections->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &ConnectionWindow::currentRowChanged); + connect(ui->tabBuses, &QTabBar::currentChanged, this, &ConnectionWindow::currentTabChanged); + connect(ui->btnSaveBus, &QPushButton::clicked, this, &ConnectionWindow::saveBusSettings); - ui->lblDeviceType->setHidden(true); - ui->cbDeviceType->setHidden(true); + ui->cbBusSpeed->addItem("50000"); + ui->cbBusSpeed->addItem("100000"); + ui->cbBusSpeed->addItem("125000"); + ui->cbBusSpeed->addItem("250000"); + ui->cbBusSpeed->addItem("500000"); + ui->cbBusSpeed->addItem("1000000"); rxBroadcast = new QUdpSocket(this); rxBroadcast->bind(QHostAddress::AnyIPv4, 17222); connect(rxBroadcast, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); + } @@ -114,7 +112,6 @@ void ConnectionWindow::showEvent(QShowEvent* event) readSettings(); ui->tableConnections->selectRow(0); currentRowChanged(ui->tableConnections->currentIndex(), ui->tableConnections->currentIndex()); - handleConnTypeChanged(); } void ConnectionWindow::closeEvent(QCloseEvent *event) @@ -163,48 +160,6 @@ void ConnectionWindow::writeSettings() } } -void ConnectionWindow::setSuspendAll(bool pSuspend) -{ - QList& conns = CANConManager::getInstance()->getConnections(); - - foreach(CANConnection* conn_p, conns) - conn_p->suspend(pSuspend); - - connModel->refresh(); -} - - -void ConnectionWindow::setActiveAll(bool pActive) -{ - CANBus bus; - QList& conns = CANConManager::getInstance()->getConnections(); - - foreach(CANConnection* conn_p, conns) - { - for(int i=0 ; igetNumBuses() ; i++) { - if( conn_p->getBusSettings(i, bus) ) { - bus.active = pActive; - conn_p->setBusSettings(i, bus); - } - } - } - - connModel->refresh(); -} - -void ConnectionWindow::handleReconnect() -{ - int selIdx = ui->tableConnections->selectionModel()->currentIndex().row(); - if (selIdx <0) return; - - int busId; - CANConnection* conn_p = connModel->getAtIdx(selIdx, busId); - if(!conn_p) return; - - conn_p->stop(); - conn_p->start(); -} - void ConnectionWindow::consoleEnableChanged(bool checked) { ui->textConsole->setEnabled(checked); ui->btnClearDebug->setEnabled(checked); @@ -229,35 +184,21 @@ void ConnectionWindow::consoleEnableChanged(bool checked) { void ConnectionWindow::handleNewConn() { - ui->tableConnections->setCurrentIndex(QModelIndex()); - currentRowChanged(ui->tableConnections->currentIndex(), ui->tableConnections->currentIndex()); -} + NewConnectionDialog *thisDialog = new NewConnectionDialog(&remoteDeviceIP); + CANCon::type newType; + QString newPort; + QString newDriver; + CANConnection *conn; - -void ConnectionWindow::handleEnableAll() -{ - setActiveAll(true); -} - -void ConnectionWindow::handleDisableAll() -{ - setActiveAll(false); -} - -void ConnectionWindow::handleConnTypeChanged() -{ - if (ui->rbGVRET->isChecked()) selectSerial(); - if (ui->rbSocketCAN->isChecked()) selectSocketCan(); - if (ui->rbRemote->isChecked()) selectRemote(); -} - -void ConnectionWindow::handleDeviceTypeChanged() -{ - ui->cbPort->clear(); - canDevices = QCanBus::instance()->availableDevices(ui->cbDeviceType->currentText()); - - for (int i = 0; i < canDevices.count(); i++) - ui->cbPort->addItem(canDevices[i].name()); + if (thisDialog->exec() == QDialog::Accepted) + { + newType = thisDialog->getConnectionType(); + newPort = thisDialog->getPortName(); + newDriver = thisDialog->getDriverName(); + conn = create(newType, newPort, newDriver); + if (conn) connModel->add(conn); + } + delete thisDialog; } /* status */ @@ -269,22 +210,79 @@ void ConnectionWindow::connectionStatus(CANConStatus pStatus) connModel->refresh(); } - -void ConnectionWindow::handleOKButton() +void ConnectionWindow::saveBusSettings() { - CANConnection* conn_p = nullptr; + int selIdx = ui->tableConnections->currentIndex().row(); + int offset = ui->tabBuses->currentIndex(); - if( ! CANConManager::getInstance()->getByName(getPortName()) ) - { - /* create connection */ - //qDebug() << "Create connection type: " << getConnectionType() << " port: " << getPortName() << " driver: " << getDriverName(); - conn_p = create(getConnectionType(), getPortName(), getDriverName()); - if(!conn_p) - return; - /* add connection to model */ - connModel->add(conn_p); - consoleEnableChanged(ui->ckEnableConsole->isChecked()); + /* set parameters */ + if (selIdx == -1) { + return; } + else + { + CANConnection* conn_p = connModel->getAtIdx(selIdx); + CANBus bus; + if(!conn_p) return; + + if (!conn_p->getBusSettings(offset, bus)) + { + qDebug() << "Could not retrieve bus settings!"; + return; + } + + bus.setSpeed(ui->cbBusSpeed->currentText().toInt()); + bus.setActive(ui->ckEnable->isChecked()); + bus.setListenOnly(ui->ckListenOnly->isChecked()); + conn_p->setBusSettings(offset, bus); + } +} + +void ConnectionWindow::populateBusDetails(int offset) +{ + int selIdx = ui->tableConnections->currentIndex().row(); + + /* set parameters */ + if (selIdx == -1) { + return; + } + else + { + bool ret; + int numBuses; + + CANConnection* conn_p = connModel->getAtIdx(selIdx); + CANBus bus; + if(!conn_p) return; + + if (!conn_p->getBusSettings(offset, bus)) + { + qDebug() << "Could not retrieve bus settings!"; + return; + } + + int busBase = CANConManager::getInstance()->getBusBase(conn_p); + ui->lblBusNum->setText(QString::number(busBase + offset)); + ui->ckListenOnly->setChecked(bus.isListenOnly()); + ui->ckEnable->setChecked(bus.isActive()); + + bool found = false; + for (int i = 0; i < ui->cbBusSpeed->count(); i++) + { + if (bus.getSpeed() == ui->cbBusSpeed->itemText(i).toInt()) + { + found = true; + ui->cbBusSpeed->setCurrentIndex(i); + break; + } + } + if (!found) ui->cbBusSpeed->addItem(QString::number(bus.getSpeed())); + } +} + +void ConnectionWindow::currentTabChanged(int newIdx) +{ + populateBusDetails(newIdx); } void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) @@ -293,43 +291,33 @@ void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QMode int selIdx = current.row(); - int busId; - - disconnect(connModel->getAtIdx(previous.row(), busId), SIGNAL(debugOutput(QString)), 0, 0); - disconnect(this, SIGNAL(sendDebugData(QByteArray)), connModel->getAtIdx(previous.row(), busId), SLOT(debugInput(QByteArray))); -return; - - /* enable / diable connection type */ - ui->stPort->setEnabled(selIdx==-1); - ui->gbType->setEnabled(selIdx==-1); - ui->lPort->setEnabled(selIdx==-1); + disconnect(connModel->getAtIdx(previous.row()), SIGNAL(debugOutput(QString)), 0, 0); + disconnect(this, SIGNAL(sendDebugData(QByteArray)), connModel->getAtIdx(previous.row()), SLOT(debugInput(QByteArray))); /* set parameters */ - if (selIdx == -1) - { - ui->btnOK->setText(tr("Create New Connection")); - ui->rbGVRET->setChecked(true); - setSpeed(0); - setPortName(CANCon::GVRET_SERIAL, "", ""); + if (selIdx == -1) { + ui->groupBus->setEnabled(false); + return; } else { bool ret; - CANBus bus; - CANConnection* conn_p = connModel->getAtIdx(selIdx, busId); + ui->groupBus->setEnabled(true); + int numBuses; + + CANConnection* conn_p = connModel->getAtIdx(selIdx); if(!conn_p) return; - if (ui->ckEnableConsole->isChecked()) { //only connect if console is actually enabled - connect(conn_p, SIGNAL(debugOutput(QString)), this, SLOT(getDebugText(QString))); - connect(this, SIGNAL(sendDebugData(QByteArray)), conn_p, SLOT(debugInput(QByteArray))); + numBuses = conn_p->getNumBuses(); + if (numBuses == 1) //remove all tabs if there is only one bus + { + for (int i = 0; i < ui->tabBuses->count(); i++) ui->tabBuses->removeTab(0); } - - ret = conn_p->getBusSettings(busId, bus); - if(!ret) return; - - ui->btnOK->setText(tr("Update Connection Settings")); - setSpeed(bus.getSpeed()); - setPortName(conn_p->getType(), conn_p->getPort(), conn_p->getDriver()); + else + { + for (int i = 0; i < numBuses; i++) ui->tabBuses->addTab(QString::number(i+1)); + } + populateBusDetails(0); } } @@ -357,152 +345,6 @@ void ConnectionWindow::handleSendText() { emit sendDebugData(bytes); } -void ConnectionWindow::selectSerial() -{ - ui->lPort->setText("Port:"); - /* set combobox page visible */ - ui->stPort->setCurrentWidget(ui->cbPage); - ui->lblDeviceType->setHidden(true); - ui->cbDeviceType->setHidden(true); - - ui->cbPort->clear(); - ports = QSerialPortInfo::availablePorts(); - - for (int i = 0; i < ports.count(); i++) - ui->cbPort->addItem(ports[i].portName()); -} - -void ConnectionWindow::selectSocketCan() -{ - ui->lPort->setText("Port:"); - /* set edit text page visible */ - ui->stPort->setCurrentWidget(ui->cbPage); - ui->lblDeviceType->setHidden(false); - ui->cbDeviceType->setHidden(false); - - ui->cbDeviceType->clear(); - QStringList plugins; - plugins = QCanBus::instance()->plugins(); - for (int i = 0; i < plugins.count(); i++) - ui->cbDeviceType->addItem(plugins[i]); -} - -void ConnectionWindow::selectRemote() -{ - ui->lPort->setText("IP Address:"); - ui->stPort->setCurrentWidget(ui->cbPage); - ui->lblDeviceType->setHidden(true); - ui->cbDeviceType->setHidden(true); - ui->cbPort->clear(); - foreach(QString pName, remoteDeviceIP) - { - ui->cbPort->addItem(pName); - } -} - -void ConnectionWindow::setSpeed(int speed0) -{ - Q_UNUSED(speed0); -} - -void ConnectionWindow::setPortName(CANCon::type pType, QString pPortName, QString pDriver) -{ - switch(pType) - { - case CANCon::GVRET_SERIAL: - ui->rbGVRET->setChecked(true); - break; - case CANCon::SERIALBUS: - ui->rbSocketCAN->setChecked(true); - //you can't configure any of the below three with socketcan so dim them out - break; - default: {} - } - - /* refresh names whenever needed */ - handleConnTypeChanged(); - - switch(pType) - { - case CANCon::GVRET_SERIAL: - { - int idx = ui->cbPort->findText(pPortName); - if( idx<0 ) idx=0; - ui->cbPort->setCurrentIndex(idx); - break; - } - case CANCon::SERIALBUS: - { - int idx = ui->cbDeviceType->findText(pDriver); - if (idx < 0) idx = 0; - ui->cbDeviceType->setCurrentIndex(idx); - idx = ui->cbPort->findText(pPortName); - if( idx < 0 ) idx = 0; - ui->cbPort->setCurrentIndex(idx); - break; - } - case CANCon::REMOTE: - { - int idx = ui->cbPort->findText(pPortName); - if (idx > -1) ui->cbPort->setCurrentIndex(idx); - else ui->cbPort->addItem(pPortName); - break; - } - default: {} - } -} - - -//-1 means leave it at whatever it booted up to. 0 means disable. Otherwise the actual rate we want. -int ConnectionWindow::getSpeed() -{ - return -1; -} - -QString ConnectionWindow::getPortName() -{ - switch( getConnectionType() ) { - case CANCon::GVRET_SERIAL: - case CANCon::SERIALBUS: - case CANCon::REMOTE: - return ui->cbPort->currentText(); - default: - qDebug() << "getPortName: can't get port"; - } - - return ""; -} - -QString ConnectionWindow::getDriverName() -{ - if (getConnectionType() == CANCon::SERIALBUS) - { - return ui->cbDeviceType->currentText(); - } - - return ""; -} - -CANCon::type ConnectionWindow::getConnectionType() -{ - if (ui->rbGVRET->isChecked()) return CANCon::GVRET_SERIAL; - if (ui->rbSocketCAN->isChecked()) return CANCon::SERIALBUS; - if (ui->rbRemote->isChecked()) return CANCon::REMOTE; - qDebug() << "getConnectionType: error"; - return CANCon::NONE; -} - - -void ConnectionWindow::setSWMode(bool mode) -{ - Q_UNUSED(mode); -} - -bool ConnectionWindow::getSWMode() -{ - return false; -} - void ConnectionWindow::handleRemoveConn() { int selIdx = ui->tableConnections->selectionModel()->currentIndex().row(); @@ -510,8 +352,7 @@ void ConnectionWindow::handleRemoveConn() qDebug() << "remove connection at index: " << selIdx; - int busId; - CANConnection* conn_p = connModel->getAtIdx(selIdx, busId); + CANConnection* conn_p = connModel->getAtIdx(selIdx); if(!conn_p) return; /* remove connection from model & manager */ @@ -525,19 +366,6 @@ void ConnectionWindow::handleRemoveConn() ui->tableConnections->selectRow(0); } -void ConnectionWindow::handleRevert() -{ - -} - - -bool ConnectionWindow::isSerialBusAvailable() -{ - if (QCanBus::instance()->plugins().count() > 0) return true; - return false; -} - - CANConnection* ConnectionWindow::create(CANCon::type pTye, QString pPortName, QString pDriver) { CANConnection* conn_p; diff --git a/connections/connectionwindow.h b/connections/connectionwindow.h index 515d34e..bf6bd77 100644 --- a/connections/connectionwindow.h +++ b/connections/connectionwindow.h @@ -3,8 +3,8 @@ -#include #include +#include #include #include #include @@ -30,61 +30,36 @@ public: explicit ConnectionWindow(QWidget *parent = 0); ~ConnectionWindow(); - CANCon::type getConnectionType(); - bool getSWMode(); - signals: void updateBusSettings(CANBus *bus); void updatePortName(QString port); void sendDebugData(QByteArray bytes); public slots: - void setSpeed(int speed0); - void setSWMode(bool mode); - - void setSuspendAll(bool pSuspend); - void getDebugText(QString debugText); private slots: - void handleOKButton(); - void handleConnTypeChanged(); - void handleDeviceTypeChanged(); void currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous); + void currentTabChanged(int newIdx); void consoleEnableChanged(bool checked); void handleRemoveConn(); - void handleEnableAll(); - void handleDisableAll(); - void handleReconnect(); - void handleRevert(); void handleNewConn(); void handleClearDebugText(); void handleSendHex(); void handleSendText(); + void saveBusSettings(); void connectionStatus(CANConStatus); void readPendingDatagrams(); private: - Ui::ConnectionWindow *ui; - QList ports; - QList canDevices; + Ui::ConnectionWindow *ui; QSettings *settings; CANConnectionModel *connModel; QUdpSocket *rxBroadcast; QVector remoteDeviceIP; - void selectSerial(); - void selectKvaser(); - void selectSocketCan(); - void selectRemote(); - bool isSerialBusAvailable(); - int getSpeed(); - QString getPortName(); - QString getDriverName(); - void setPortName(CANCon::type pType, QString pPortName, QString pDriver); - - void setActiveAll(bool pActive); CANConnection* create(CANCon::type pTye, QString pPortName, QString pDriver); + void populateBusDetails(int offset); void loadConnections(); void saveConnections(); void showEvent(QShowEvent *); diff --git a/connections/gvretserial.cpp b/connections/gvretserial.cpp index 86d1b0f..0d42518 100644 --- a/connections/gvretserial.cpp +++ b/connections/gvretserial.cpp @@ -819,8 +819,8 @@ void GVRetSerial::procRXChar(unsigned char c) qDebug() << "Baud 1 = " << can1Baud; mBusData[0].mBus.setSpeed(can0Baud); mBusData[1].mBus.setSpeed(can1Baud); - mBusData[0].mBus.setEnabled(can0Enabled); - mBusData[1].mBus.setEnabled(can1Enabled); + mBusData[0].mBus.setActive(can0Enabled); + mBusData[1].mBus.setActive(can1Enabled); mBusData[0].mConfigured = true; mBusData[1].mConfigured = true; @@ -962,7 +962,7 @@ void GVRetSerial::procRXChar(unsigned char c) if (getNumBuses() > 2) { mBusData[2].mBus.setSpeed(swcanBaud); - mBusData[2].mBus.setEnabled(swcanEnabled); + mBusData[2].mBus.setActive(swcanEnabled); } setStatus(CANCon::CONNECTED); diff --git a/connections/newconnectiondialog.cpp b/connections/newconnectiondialog.cpp new file mode 100644 index 0000000..ad15569 --- /dev/null +++ b/connections/newconnectiondialog.cpp @@ -0,0 +1,177 @@ +#include +#include "newconnectiondialog.h" +#include "ui_newconnectiondialog.h" + +NewConnectionDialog::NewConnectionDialog(QVector* ips, QWidget *parent) : + QDialog(parent), + ui(new Ui::NewConnectionDialog), + remoteDeviceIP(ips) +{ + ui->setupUi(this); + + ui->rbSocketCAN->setEnabled(isSerialBusAvailable()); + + connect(ui->rbGVRET, &QAbstractButton::clicked, this, &NewConnectionDialog::handleConnTypeChanged); + connect(ui->rbSocketCAN, &QAbstractButton::clicked, this, &NewConnectionDialog::handleConnTypeChanged); + connect(ui->rbRemote, &QAbstractButton::clicked, this, &NewConnectionDialog::handleConnTypeChanged); + connect(ui->cbDeviceType, QOverload::of(&QComboBox::currentIndexChanged), this, &NewConnectionDialog::handleDeviceTypeChanged); + connect(ui->btnOK, &QPushButton::clicked, this, &NewConnectionDialog::handleCreateButton); + + ui->lblDeviceType->setHidden(true); + ui->cbDeviceType->setHidden(true); + selectSerial(); +} + +NewConnectionDialog::~NewConnectionDialog() +{ + delete ui; +} + +void NewConnectionDialog::handleCreateButton() +{ + accept(); +} + +void NewConnectionDialog::handleConnTypeChanged() +{ + if (ui->rbGVRET->isChecked()) selectSerial(); + if (ui->rbSocketCAN->isChecked()) selectSocketCan(); + if (ui->rbRemote->isChecked()) selectRemote(); +} + +void NewConnectionDialog::handleDeviceTypeChanged() +{ + + ui->cbPort->clear(); + canDevices = QCanBus::instance()->availableDevices(ui->cbDeviceType->currentText()); + + for (int i = 0; i < canDevices.count(); i++) + ui->cbPort->addItem(canDevices[i].name()); +} + +void NewConnectionDialog::selectSerial() +{ + ui->lPort->setText("Serial Port:"); + + ui->lblDeviceType->setHidden(true); + ui->cbDeviceType->setHidden(true); + + ui->cbPort->clear(); + ports = QSerialPortInfo::availablePorts(); + + for (int i = 0; i < ports.count(); i++) + ui->cbPort->addItem(ports[i].portName()); +} + +void NewConnectionDialog::selectSocketCan() +{ + ui->lPort->setText("Port:"); + ui->lblDeviceType->setHidden(false); + ui->cbDeviceType->setHidden(false); + + ui->cbDeviceType->clear(); + QStringList plugins; + plugins = QCanBus::instance()->plugins(); + for (int i = 0; i < plugins.count(); i++) + ui->cbDeviceType->addItem(plugins[i]); + +} + +void NewConnectionDialog::selectRemote() +{ + ui->lPort->setText("IP Address:"); + ui->lblDeviceType->setHidden(true); + ui->cbDeviceType->setHidden(true); + ui->cbPort->clear(); + foreach(QString pName, *remoteDeviceIP) + { + ui->cbPort->addItem(pName); + } +} + +void NewConnectionDialog::setPortName(CANCon::type pType, QString pPortName, QString pDriver) +{ + + switch(pType) + { + case CANCon::GVRET_SERIAL: + ui->rbGVRET->setChecked(true); + break; + case CANCon::SERIALBUS: + ui->rbSocketCAN->setChecked(true); + //you can't configure any of the below three with socketcan so dim them out + break; + default: {} + } + + /* refresh names whenever needed */ + //handleConnTypeChanged(); + + switch(pType) + { + case CANCon::GVRET_SERIAL: + { + int idx = ui->cbPort->findText(pPortName); + if( idx<0 ) idx=0; + ui->cbPort->setCurrentIndex(idx); + break; + } + case CANCon::SERIALBUS: + { + int idx = ui->cbDeviceType->findText(pDriver); + if (idx < 0) idx = 0; + ui->cbDeviceType->setCurrentIndex(idx); + idx = ui->cbPort->findText(pPortName); + if( idx < 0 ) idx = 0; + ui->cbPort->setCurrentIndex(idx); + break; + } + case CANCon::REMOTE: + { + int idx = ui->cbPort->findText(pPortName); + if (idx > -1) ui->cbPort->setCurrentIndex(idx); + else ui->cbPort->addItem(pPortName); + break; + } + default: {} + } +} + +QString NewConnectionDialog::getPortName() +{ + switch( getConnectionType() ) { + case CANCon::GVRET_SERIAL: + case CANCon::SERIALBUS: + case CANCon::REMOTE: + return ui->cbPort->currentText(); + default: + qDebug() << "getPortName: can't get port"; + } + + return ""; +} + +QString NewConnectionDialog::getDriverName() +{ + if (getConnectionType() == CANCon::SERIALBUS) + { + return ui->cbDeviceType->currentText(); + } + return "N/A"; +} + +CANCon::type NewConnectionDialog::getConnectionType() +{ + if (ui->rbGVRET->isChecked()) return CANCon::GVRET_SERIAL; + if (ui->rbSocketCAN->isChecked()) return CANCon::SERIALBUS; + if (ui->rbRemote->isChecked()) return CANCon::REMOTE; + qDebug() << "getConnectionType: error"; + + return CANCon::NONE; +} + +bool NewConnectionDialog::isSerialBusAvailable() +{ + if (QCanBus::instance()->plugins().count() > 0) return true; + return false; +} diff --git a/connections/newconnectiondialog.h b/connections/newconnectiondialog.h new file mode 100644 index 0000000..f826b70 --- /dev/null +++ b/connections/newconnectiondialog.h @@ -0,0 +1,47 @@ +#ifndef NEWCONNECTIONDIALOG_H +#define NEWCONNECTIONDIALOG_H + +#include +#include +#include +#include +#include +#include "canconnectionmodel.h" +#include "connections/canconnection.h" + +namespace Ui { +class NewConnectionDialog; +} + +class NewConnectionDialog : public QDialog +{ + Q_OBJECT + +public: + explicit NewConnectionDialog(QVector* ips, QWidget *parent = nullptr); + ~NewConnectionDialog(); + + CANCon::type getConnectionType(); + QString getPortName(); + QString getDriverName(); + +public slots: + void handleConnTypeChanged(); + void handleDeviceTypeChanged(); + void handleCreateButton(); + +private: + Ui::NewConnectionDialog *ui; + QList ports; + QList canDevices; + QVector* remoteDeviceIP; + + void selectSerial(); + void selectKvaser(); + void selectSocketCan(); + void selectRemote(); + bool isSerialBusAvailable(); + void setPortName(CANCon::type pType, QString pPortName, QString pDriver); +}; + +#endif // NEWCONNECTIONDIALOG_H diff --git a/connections/serialbusconnection.cpp b/connections/serialbusconnection.cpp index af67b9e..03891ff 100644 --- a/connections/serialbusconnection.cpp +++ b/connections/serialbusconnection.cpp @@ -46,7 +46,7 @@ void SerialBusConnection::piStarted() mTimer.setInterval(1000); mTimer.setSingleShot(false); //keep ticking mTimer.start(); - mBusData[0].mBus.setEnabled(true); + mBusData[0].mBus.setActive(true); mBusData[0].mConfigured = true; } @@ -281,7 +281,7 @@ void SerialBusConnection::testConnection() { CANBus bus; if(getBusConfig(0, bus)) { - bus.setEnabled(true); + bus.setActive(true); setBusSettings(0, bus); } diff --git a/ui/connectionwindow.ui b/ui/connectionwindow.ui index 0e52eae..d132606 100644 --- a/ui/connectionwindow.ui +++ b/ui/connectionwindow.ui @@ -19,7 +19,7 @@ - Connections: + Connected Devices: @@ -37,195 +37,104 @@ - - - - - Reconnect - - - - - - - Remove Selected Connection - - - - - - - Activate All Connections - - - - - - - Deactivate All Connections - - - - + + + Add New Device Connection + + + + + + + Disconnect Selected Device + + + + + + + false + + + Bus Details: + + + Qt::AlignCenter + + + + + + + + + + + CAN Bus Number: + + + + + + + + + + + + + + Speed: + + + + + + + + + + Listen Only: + + + + + + + + + + + + + + Enable Bus: + + + + + + + + + + + + + + Save Bus Settings + + + + + + + - - - - Connection Type - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - - - - - - GVRET Serial - - - true - - - - - - - false - - - QT SerialBus Devices (SocketCAN, PeakCAN, etc) - - - - - - - Remote Host - - - - - - - - - - Device Type: - - - - - - - - - - - 0 - 0 - - - - Port: - - - - - - - - 0 - 0 - - - - - 0 - 0 - - - - Qt::LeftToRight - - - 1 - - - - - 0 - 0 - - - - Qt::LeftToRight - - - - - - - 0 - 0 - - - - - 341 - 16777215 - - - - true - - - - - - - - - - - - 0 - 0 - - - - - - - - - - - - - - Create New Connection - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - @@ -299,6 +208,13 @@ + + + QTabBar + QWidget +
qtabbar.h
+
+
diff --git a/ui/newconnectiondialog.ui b/ui/newconnectiondialog.ui new file mode 100644 index 0000000..de55127 --- /dev/null +++ b/ui/newconnectiondialog.ui @@ -0,0 +1,109 @@ + + + NewConnectionDialog + + + + 0 + 0 + 356 + 282 + + + + New Connection + + + + + + Connection Type + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + + Serial Connection + + + true + + + + + + + false + + + QT SerialBus Devices (SocketCAN, PeakCAN, etc) + + + + + + + Network connection + + + + + + + + + + SerialBus Device Type: + + + + + + + + + + + 0 + 0 + + + + Serial Port: + + + + + + + + 0 + 0 + + + + + 341 + 16777215 + + + + true + + + + + + + Create New Connection + + + + + + + +