Major revamp of the connection window. Hopefully it is more clear and

intuitive now.
This commit is contained in:
Collin Kidder
2019-03-05 21:25:32 -05:00
parent 70e8af85af
commit ec49413f61
17 changed files with 611 additions and 661 deletions
+124 -296
View File
@@ -10,6 +10,7 @@
#include "connections/canconmanager.h"
#include "canbus.h"
#include <QSettings>
#include <connections/newconnectiondialog.h>
ConnectionWindow::ConnectionWindow(QWidget *parent) :
QDialog(parent),
@@ -24,16 +25,15 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) :
qRegisterMetaType<const CANFrame *>("const CANFrame *");
qRegisterMetaType<const QList<CANFrame> *>("const QList<CANFrame> *");
//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<int>::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<CANConnection*>& conns = CANConManager::getInstance()->getConnections();
foreach(CANConnection* conn_p, conns)
conn_p->suspend(pSuspend);
connModel->refresh();
}
void ConnectionWindow::setActiveAll(bool pActive)
{
CANBus bus;
QList<CANConnection*>& conns = CANConManager::getInstance()->getConnections();
foreach(CANConnection* conn_p, conns)
{
for(int i=0 ; i<conn_p->getNumBuses() ; 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 &current, const QModelIndex &previous)
@@ -293,43 +291,33 @@ void ConnectionWindow::currentRowChanged(const QModelIndex &current, 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;