diff --git a/connections/canconnectionmodel.cpp b/connections/canconnectionmodel.cpp index 726c334..dd88ad9 100644 --- a/connections/canconnectionmodel.cpp +++ b/connections/canconnectionmodel.cpp @@ -40,10 +40,10 @@ QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation return QString(tr("Single Wire")); break; case 6: - return QString(tr("Status")); + return QString(tr("Active")); break; case 7: - return QString(tr("Active")); + return QString(tr("Status")); break; } } @@ -54,11 +54,10 @@ QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation return QVariant(); } - int CANConnectionModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent); - return 7; + return 8; } @@ -75,22 +74,87 @@ int CANConnectionModel::rowCount(const QModelIndex &parent) const 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; + + //you can't set speed, single wire, or listen only on socketcan devices so + //detect if we're using GVRET where you can and turn that functionality on + bool editParams = false; + if (conn_p->getType() == CANCon::GVRET_SERIAL) editParams = true; + + switch (index.column()) + { + case 3: //speed + if (editParams) return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled; + else return Qt::ItemFlag::NoItemFlags; + break; + case 4: //listen only + case 5: //single wire + if (editParams) return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsUserCheckable; + else return Qt::ItemFlag::NoItemFlags; + break; + case 6: //enabled + return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsUserCheckable; + break; + default: + return Qt::ItemFlag::ItemIsEnabled; + break; + } +} + +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 (index.column()) + { + case 3: //speed + bus.speed = value.toInt(); + break; + case 4: //listen only + bus.listenOnly = value.toBool(); + break; + case 5: //single wire + bus.singleWire = value.toBool(); + break; + case 6: //active + bus.active = value.toBool(); + break; + } + conn_p->setBusSettings(busId, bus); + return true; +} QVariant CANConnectionModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); + int busId; + CANConnection *conn_p = getAtIdx(index.row(), busId); + CANBus bus; + bool ret; + ret = conn_p->getBusSettings(busId, bus); + bool isSocketCAN = (conn_p->getType() == CANCon::SOCKETCAN) ? true: false; if (role == Qt::DisplayRole) { - int busId; - CANConnection *conn_p = getAtIdx(index.row(), busId); if(!conn_p) return QVariant(); - CANBus bus; - bool ret; - ret = conn_p->getBusSettings(busId, bus); if(!ret) return QVariant(); switch (index.column()) @@ -114,18 +178,34 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const else qDebug() << "Tried to show connection port but connection was NULL"; break; case 3: //speed - return QString::number(bus.speed); + if (!isSocketCAN) return QString::number(bus.speed); + else return QString("N/A"); case 4: //Listen Only - return (bus.listenOnly) ? "True" : "False"; + return QVariant(); case 5: //Single Wire - return (bus.singleWire) ? "True" : "False"; + return QVariant(); case 6: //Status - return (conn_p->getStatus()==CANCon::CONNECTED) ? "Connected" : "Not Connected"; + return QVariant(); case 7: //Active - return (bus.active) ? "True" : "False"; + return (conn_p->getStatus()==CANCon::CONNECTED) ? "Connected" : "Not Connected"; default: {} } } + if (role == Qt::CheckStateRole) + { + switch (index.column()) + { + case 4: + return (bus.listenOnly) ? Qt::Checked : Qt::Unchecked; + break; + case 5: + return (bus.singleWire) ? Qt::Checked : Qt::Unchecked; + break; + case 6: + return (bus.active) ? Qt::Checked : Qt::Unchecked; + break; + } + } return QVariant(); } @@ -172,7 +252,6 @@ CANConnection* CANConnectionModel::getAtIdx(int pIdx, int& pBusId) const return NULL; } - void CANConnectionModel::refresh(int pIndex) { QModelIndex begin; diff --git a/connections/canconnectionmodel.h b/connections/canconnectionmodel.h index 68a8d45..190dd27 100644 --- a/connections/canconnectionmodel.h +++ b/connections/canconnectionmodel.h @@ -21,6 +21,10 @@ public: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; 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); diff --git a/connections/connectionwindow.cpp b/connections/connectionwindow.cpp index ccaeaab..b28bb93 100644 --- a/connections/connectionwindow.cpp +++ b/connections/connectionwindow.cpp @@ -24,25 +24,17 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : connModel = new CANConnectionModel(this); ui->tableConnections->setModel(connModel); - ui->tableConnections->setColumnWidth(0, 50); - ui->tableConnections->setColumnWidth(1, 110); - ui->tableConnections->setColumnWidth(2, 110); - ui->tableConnections->setColumnWidth(3, 110); - ui->tableConnections->setColumnWidth(4, 75); - ui->tableConnections->setColumnWidth(5, 75); - ui->tableConnections->setColumnWidth(6, 75); - ui->tableConnections->setColumnWidth(7, 75); + ui->tableConnections->setColumnWidth(0, 40); + ui->tableConnections->setColumnWidth(1, 70); + ui->tableConnections->setColumnWidth(2, 70); + 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); QHeaderView *HorzHdr = ui->tableConnections->horizontalHeader(); HorzHdr->setStretchLastSection(true); //causes the data column to automatically fill the tableview - ui->ckSingleWire->setChecked(settings.value("Main/SingleWireMode", false).toBool()); - - ui->cbSpeed->addItem(tr("125000")); - ui->cbSpeed->addItem(tr("250000")); - ui->cbSpeed->addItem(tr("500000")); - ui->cbSpeed->addItem(tr("1000000")); - ui->cbSpeed->addItem(tr("33333")); - ui->textConsole->setEnabled(false); ui->btnClearDebug->setEnabled(false); ui->btnSendHex->setEnabled(false); @@ -61,9 +53,7 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : connect(ui->rbGVRET, &QAbstractButton::clicked, this, &ConnectionWindow::handleConnTypeChanged); connect(ui->rbKvaser, &QAbstractButton::clicked, this, &ConnectionWindow::handleConnTypeChanged); connect(ui->rbSocketCAN, &QAbstractButton::clicked, this, &ConnectionWindow::handleConnTypeChanged); - connect(ui->btnRevert, &QPushButton::clicked, this, &ConnectionWindow::handleRevert); connect(ui->tableConnections->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &ConnectionWindow::currentRowChanged); - connect(ui->btnNewConn, &QPushButton::clicked, this, &ConnectionWindow::handleNewConn); connect(ui->btnActivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleEnableAll); connect(ui->btnDeactivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleDisableAll); connect(ui->btnRemoveBus, &QPushButton::clicked, this, &ConnectionWindow::handleRemoveConn); @@ -236,10 +226,10 @@ void ConnectionWindow::handleOKButton() if(!ret) return; - bus.setListenOnly(ui->ckListenOnly->isChecked()); - bus.setSingleWire(ui->ckSingleWire->isChecked()); - bus.setEnabled(ui->ckEnabled->isChecked()); - bus.setSpeed(ui->cbSpeed->currentText().toInt()); + //bus.setListenOnly(ui->ckListenOnly->isChecked()); + //bus.setSingleWire(ui->ckSingleWire->isChecked()); + //bus.setEnabled(ui->ckEnabled->isChecked()); + //bus.setSpeed(ui->cbSpeed->currentText().toInt()); /* update bus settings */ conn_p->setBusSettings(busId, bus); @@ -255,11 +245,11 @@ void ConnectionWindow::handleOKButton() for (int i=0 ; igetNumBuses() ; i++) { /* set bus configuration */ CANBus bus; - bus.active = ui->ckEnabled->isChecked(); - bus.listenOnly = ui->ckListenOnly->isChecked(); - bus.singleWire = ui->ckSingleWire->isChecked(); + //bus.active = ui->ckEnabled->isChecked(); + //bus.listenOnly = ui->ckListenOnly->isChecked(); + //bus.singleWire = ui->ckSingleWire->isChecked(); - bus.speed = ui->cbSpeed->currentText().toInt(); + //bus.speed = ui->cbSpeed->currentText().toInt(); /* update bus settings */ conn_p->setBusSettings(i, bus); @@ -280,6 +270,7 @@ void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QMode 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); @@ -291,9 +282,6 @@ void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QMode { ui->btnOK->setText(tr("Create New Connection")); ui->rbGVRET->setChecked(true); - ui->ckListenOnly->setChecked(false); - ui->ckSingleWire->setChecked(false); - ui->ckEnabled->setChecked(false); setSpeed(0); setPortName(CANCon::GVRET_SERIAL, ""); } @@ -313,9 +301,6 @@ void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QMode if(!ret) return; ui->btnOK->setText(tr("Update Connection Settings")); - ui->ckListenOnly->setChecked(bus.isListenOnly()); - ui->ckSingleWire->setChecked(bus.isSingleWire()); - ui->ckEnabled->setChecked(bus.isActive()); setSpeed(bus.getSpeed()); setPortName(conn_p->getType(), conn_p->getPort()); } @@ -349,7 +334,6 @@ void ConnectionWindow::selectSerial() { /* set combobox page visible */ ui->stPort->setCurrentWidget(ui->cbPage); - ui->cbSpeed->setEnabled(true); ui->cbPort->clear(); ports = QSerialPortInfo::availablePorts(); @@ -362,7 +346,6 @@ void ConnectionWindow::selectKvaser() { /* set combobox page visible */ ui->stPort->setCurrentWidget(ui->cbPage); - ui->cbSpeed->setEnabled(false); } void ConnectionWindow::selectSocketCan() @@ -373,18 +356,6 @@ void ConnectionWindow::selectSocketCan() void ConnectionWindow::setSpeed(int speed0) { - for (int i = 0; i < ui->cbSpeed->count(); i++) - { - if (ui->cbSpeed->itemText(i).toInt() == speed0) - { - ui->cbSpeed->setCurrentIndex(i); - return; - } - } - - /* add custom speed */ - ui->cbSpeed->addItem(QString::number(speed0)); - ui->cbSpeed->setCurrentIndex(ui->cbSpeed->count() - 1); } void ConnectionWindow::setPortName(CANCon::type pType, QString pPortName) @@ -393,9 +364,6 @@ void ConnectionWindow::setPortName(CANCon::type pType, QString pPortName) { case CANCon::GVRET_SERIAL: ui->rbGVRET->setChecked(true); - ui->cbSpeed->setEnabled(true); - ui->ckListenOnly->setEnabled(true); - ui->ckSingleWire->setEnabled(true); break; case CANCon::KVASER: ui->rbKvaser->setChecked(true); @@ -403,9 +371,6 @@ void ConnectionWindow::setPortName(CANCon::type pType, QString pPortName) case CANCon::SOCKETCAN: ui->rbSocketCAN->setChecked(true); //you can't configure any of the below three with socketcan so dim them out - ui->cbSpeed->setEnabled(false); - ui->ckListenOnly->setEnabled(false); - ui->ckSingleWire->setEnabled(false); break; default: {} } @@ -435,16 +400,6 @@ void ConnectionWindow::setPortName(CANCon::type pType, QString pPortName) //-1 means leave it at whatever it booted up to. 0 means disable. Otherwise the actual rate we want. int ConnectionWindow::getSpeed() { - switch (ui->cbSpeed->currentIndex()) - { - case -1: - case 0: - return -1; - case 1: - return 0; - default: - return (ui->cbSpeed->currentText().toInt()); - } } QString ConnectionWindow::getPortName() @@ -475,12 +430,10 @@ CANCon::type ConnectionWindow::getConnectionType() void ConnectionWindow::setSWMode(bool mode) { - ui->ckSingleWire->setChecked(mode); } bool ConnectionWindow::getSWMode() { - if (ui->ckSingleWire->checkState() == Qt::Checked) return true; return false; } diff --git a/ui/connectionwindow.ui b/ui/connectionwindow.ui index 634ba67..e2578af 100644 --- a/ui/connectionwindow.ui +++ b/ui/connectionwindow.ui @@ -25,6 +25,9 @@ + + true + QAbstractItemView::SingleSelection @@ -35,13 +38,6 @@ - - - - New Connection - - - @@ -49,10 +45,6 @@ - - - - @@ -193,45 +185,6 @@ - - - - Single Wire Mode - - - - - - - Listen Only Mode - - - - - - - Enabled - - - - - - - - - CAN Speed - - - - - - - true - - - - - @@ -241,15 +194,21 @@ - - - - Revert - - - + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -319,19 +278,6 @@ - - - - Qt::Vertical - - - - 20 - 40 - - - -