Further source file restructuring

This commit is contained in:
Collin Kidder
2016-12-11 21:36:25 -05:00
parent 0b46f564ab
commit b7a1afcd8f
17 changed files with 14 additions and 756 deletions
+76
View File
@@ -0,0 +1,76 @@
#include <QObject>
#include "canbus.h"
CANBus::CANBus()
{
speed = 250000;
listenOnly = false;
singleWire = false;
active = false;
}
CANBus::CANBus(const CANBus& pBus) :
speed(pBus.speed),
listenOnly(pBus.listenOnly),
singleWire(pBus.singleWire),
active(pBus.active) {}
bool CANBus::operator==(const CANBus& bus) const{
return speed == bus.speed &&
listenOnly == bus.listenOnly &&
singleWire == bus.singleWire &&
active == bus.active;
}
void CANBus::setSpeed(int newSpeed){
speed = newSpeed;
}
void CANBus::setListenOnly(bool mode){
listenOnly = mode;
}
void CANBus::setSingleWire(bool mode){
singleWire = mode;
}
void CANBus::setEnabled(bool mode){
active = mode;
}
int CANBus::getSpeed(){
return speed;
}
bool CANBus::isListenOnly(){
return listenOnly;
}
bool CANBus::isSingleWire(){
return singleWire;
}
bool CANBus::isActive(){
return active;
}
QDataStream& operator<<( QDataStream & pStream, const CANBus& pCanBus )
{
pStream << pCanBus.speed;
pStream << pCanBus.listenOnly;
pStream << pCanBus.singleWire;
pStream << pCanBus.active;
return pStream;
}
QDataStream & operator>>(QDataStream & pStream, CANBus& pCanBus)
{
pStream >> pCanBus.speed;
pStream >> pCanBus.listenOnly;
pStream >> pCanBus.singleWire;
pStream >> pCanBus.active;
return pStream;
}
+34
View File
@@ -0,0 +1,34 @@
#ifndef CANBus_H
#define CANBus_H
#include <QDataStream>
class CANBus
{
public:
CANBus();
CANBus(const CANBus&);
bool operator==(const CANBus&) const;
virtual ~CANBus(){};
int speed;
bool listenOnly;
bool singleWire;
bool active; //is this bus turned on?
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.
int getSpeed();
bool isListenOnly();
bool isSingleWire();
bool isActive();
};
QDataStream& operator<<( QDataStream & pStream, const CANBus& pCanBus );
QDataStream & operator>>(QDataStream & pStream, CANBus& pCanBus);
Q_DECLARE_METATYPE(CANBus);
#endif // CANBus_H
+194
View File
@@ -0,0 +1,194 @@
#include "canconnectionmodel.h"
#include "connections/canconnection.h"
#include "connections/canconmanager.h"
CANConnectionModel::CANConnectionModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
CANConnectionModel::~CANConnectionModel()
{
}
QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
{
switch (section)
{
case 0:
return QString(tr("Bus"));
break;
case 1:
return QString(tr("Type"));
break;
case 2:
return QString(tr("Port"));
break;
case 3:
return QString(tr("Speed"));
break;
case 4:
return QString(tr("Listen Only"));
break;
case 5:
return QString(tr("Single Wire"));
break;
case 6:
return QString(tr("Status"));
break;
case 7:
return QString(tr("Active"));
break;
}
}
else
return QString::number(section + 1);
return QVariant();
}
int CANConnectionModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 7;
}
int CANConnectionModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
int rows=0;
QList<CANConnection*>& conns = CANConManager::getInstance()->getConnections();
foreach(const CANConnection* conn_p, conns)
rows+=conn_p->getNumBuses();
return rows;
}
QVariant CANConnectionModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
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())
{
case 0: //bus
//return QString::number(busId);
return QString::number(index.row());
break;
case 1: //type
if (conn_p)
switch (conn_p->getType()) {
case CANCon::KVASER: return "KVASER";
case CANCon::SOCKETCAN: return "SocketCAN";
case CANCon::GVRET_SERIAL: return "GVRET";
default: {}
}
else qDebug() << "Tried to show connection type but connection was NULL";
break;
case 2: //port
if (conn_p) return conn_p->getPort();
else qDebug() << "Tried to show connection port but connection was NULL";
break;
case 3: //speed
return QString::number(bus.speed);
case 4: //Listen Only
return (bus.listenOnly) ? "True" : "False";
case 5: //Single Wire
return (bus.singleWire) ? "True" : "False";
case 6: //Status
return (conn_p->getStatus()==CANCon::CONNECTED) ? "Connected" : "Not Connected";
case 7: //Active
return (bus.active) ? "True" : "False";
default: {}
}
}
return QVariant();
}
void CANConnectionModel::add(CANConnection* pConn_p)
{
CANConManager* manager = CANConManager::getInstance();
connect(pConn_p, SIGNAL(notify()), manager, SLOT(refreshCanList()));
beginResetModel();
manager->getConnections().append(pConn_p);
endResetModel();
}
void CANConnectionModel::remove(CANConnection* pConn_p)
{
CANConManager* manager = CANConManager::getInstance();
disconnect(pConn_p, 0, manager, 0);
beginResetModel();
manager->getConnections().removeOne(pConn_p);
endResetModel();
}
CANConnection* CANConnectionModel::getAtIdx(int pIdx, int& pBusId) const
{
if (pIdx < 0)
return NULL;
int i=0;
QList<CANConnection*>& 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;
}
void CANConnectionModel::refresh(int pIndex)
{
QModelIndex begin;
QModelIndex end;
if(pIndex>=0) {
begin = createIndex(pIndex, 0);
end = createIndex(pIndex, columnCount()-1);
}
else {
begin = createIndex(0, 0);
end = createIndex(rowCount()-1, columnCount()-1);
}
dataChanged(begin, end, QVector<int>(Qt::DisplayRole));
}
+33
View File
@@ -0,0 +1,33 @@
#ifndef CANCONNECTIONMODEL_H
#define CANCONNECTIONMODEL_H
#include <QAbstractTableModel>
#include "canbus.h"
#include "connections/canconnection.h"
#include "connectionwindow.h"
class CANConnectionModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit CANConnectionModel(QObject *parent = 0);
virtual ~CANConnectionModel();
// from abstractmodel:
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;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
void add(CANConnection* pConn_p);
void remove(CANConnection* pConn_p);
CANConnection* getAtIdx(int, int&) const;
void refresh(int pIndex=-1);
};
#endif // CANCONNECTIONMODEL_H
+499
View File
@@ -0,0 +1,499 @@
#include <QCanBus>
#include <QThread>
#include "connectionwindow.h"
#include "ui_connectionwindow.h"
#include "connections/canconfactory.h"
#include "connections/canconmanager.h"
#include "canbus.h"
ConnectionWindow::ConnectionWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::ConnectionWindow)
{
ui->setupUi(this);
QSettings settings;
qRegisterMetaType<CANBus>("CANBus");
qRegisterMetaType<const CANFrame *>("const CANFrame *");
qRegisterMetaType<const QList<CANFrame> *>("const QList<CANFrame> *");
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->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"));
/* load connection configuration */
loadConnections();
ui->rbSocketCAN->setEnabled(isSocketCanAvailable());
#ifdef Q_OS_WIN
ui->rbKvaser->setEnabled(true);
#endif
connect(ui->btnOK, &QAbstractButton::clicked, this, &ConnectionWindow::handleOKButton);
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);
}
ConnectionWindow::~ConnectionWindow()
{
QList<CANConnection*>& conns = CANConManager::getInstance()->getConnections();
CANConnection* conn_p;
/* save configuration */
saveConnections();
/* delete connections */
while(!conns.isEmpty())
{
conn_p = conns.takeFirst();
conn_p->stop();
delete conn_p;
}
delete ui;
}
void ConnectionWindow::showEvent(QShowEvent* event)
{
QDialog::showEvent(event);
qDebug() << "Show connectionwindow";
ui->tableConnections->selectRow(0);
currentRowChanged(ui->tableConnections->currentIndex(), ui->tableConnections->currentIndex());
}
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::handleNewConn()
{
ui->tableConnections->setCurrentIndex(QModelIndex());
currentRowChanged(ui->tableConnections->currentIndex(), ui->tableConnections->currentIndex());
}
void ConnectionWindow::handleEnableAll()
{
setActiveAll(true);
}
void ConnectionWindow::handleDisableAll()
{
setActiveAll(false);
}
void ConnectionWindow::handleConnTypeChanged()
{
if (ui->rbGVRET->isChecked()) selectSerial();
if (ui->rbKvaser->isChecked()) selectKvaser();
if (ui->rbSocketCAN->isChecked()) selectSocketCan();
}
/* status */
void ConnectionWindow::connectionStatus(CANCon::status pStatus)
{
Q_UNUSED(pStatus);
qDebug() << "Connectionstatus changed";
connModel->refresh();
}
void ConnectionWindow::handleOKButton()
{
int whichRow = ui->tableConnections->selectionModel()->currentIndex().row();
CANConnection* conn_p = NULL;
if (whichRow > -1)
{
/* set information for selected connection */
int busId;
CANBus bus;
bool ret;
conn_p = connModel->getAtIdx(whichRow, busId);
if(!conn_p) return;
ret = conn_p->getBusSettings(busId, bus);
if(!ret) return;
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);
connModel->refresh(whichRow);
}
else if( ! CANConManager::getInstance()->getByName(getPortName()) )
{
/* create connection */
conn_p = create(getConnectionType(), getPortName());
if(!conn_p)
return;
for (int i=0 ; i<conn_p->getNumBuses() ; i++) {
/* set bus configuration */
CANBus bus;
bus.active = ui->ckEnabled->isChecked();
bus.listenOnly = ui->ckListenOnly->isChecked();
bus.singleWire = ui->ckSingleWire->isChecked();
bus.speed = ui->cbSpeed->currentText().toInt();
/* update bus settings */
conn_p->setBusSettings(i, bus);
}
/* add connection to model */
connModel->add(conn_p);
}
}
void ConnectionWindow::currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
{
Q_UNUSED(previous);
int selIdx = current.row();
/* enable / diable connection type */
ui->stPort->setEnabled(selIdx==-1);
ui->gbType->setEnabled(selIdx==-1);
ui->lPort->setEnabled(selIdx==-1);
/* set parameters */
if (selIdx == -1)
{
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, "");
}
else
{
int busId;
bool ret;
CANBus bus;
CANConnection* conn_p = connModel->getAtIdx(selIdx, busId);
if(!conn_p) return;
ret = conn_p->getBusSettings(busId, bus);
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());
}
}
void ConnectionWindow::selectSerial()
{
/* set combobox page visible */
ui->stPort->setCurrentWidget(ui->cbPage);
ui->cbPort->clear();
ports = QSerialPortInfo::availablePorts();
for (int i = 0; i < ports.count(); i++)
ui->cbPort->addItem(ports[i].portName());
}
void ConnectionWindow::selectKvaser()
{
/* set combobox page visible */
ui->stPort->setCurrentWidget(ui->cbPage);
}
void ConnectionWindow::selectSocketCan()
{
/* set edit text page visible */
ui->stPort->setCurrentWidget(ui->etPage);
}
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)
{
switch(pType)
{
case CANCon::GVRET_SERIAL:
ui->rbGVRET->setChecked(true); break;
case CANCon::KVASER:
ui->rbKvaser->setChecked(true); break;
case CANCon::SOCKETCAN:
ui->rbSocketCAN->setChecked(true); 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::SOCKETCAN:
{
ui->lePort->setText(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()
{
switch (ui->cbSpeed->currentIndex())
{
case -1:
case 0:
return -1;
case 1:
return 0;
default:
return (ui->cbSpeed->currentText().toInt());
}
}
QString ConnectionWindow::getPortName()
{
switch( getConnectionType() ) {
case CANCon::GVRET_SERIAL:
case CANCon::KVASER:
return ui->cbPort->currentText();
case CANCon::SOCKETCAN:
return ui->lePort->text();
default:
qDebug() << "getPortName: can't get port";
}
return "";
}
CANCon::type ConnectionWindow::getConnectionType()
{
if (ui->rbGVRET->isChecked()) return CANCon::GVRET_SERIAL;
if (ui->rbKvaser->isChecked()) return CANCon::KVASER;
if (ui->rbSocketCAN->isChecked()) return CANCon::SOCKETCAN;
qDebug() << "getConnectionType: error";
return CANCon::NONE;
}
void ConnectionWindow::setSWMode(bool mode)
{
ui->ckSingleWire->setChecked(mode);
}
bool ConnectionWindow::getSWMode()
{
if (ui->ckSingleWire->checkState() == Qt::Checked) return true;
return false;
}
void ConnectionWindow::handleRemoveConn()
{
int selIdx = ui->tableConnections->selectionModel()->currentIndex().row();
if (selIdx <0) return;
qDebug() << "remove connection at index: " << selIdx;
int busId;
CANConnection* conn_p = connModel->getAtIdx(selIdx, busId);
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::handleRevert()
{
}
bool ConnectionWindow::isSocketCanAvailable()
{
#ifdef Q_OS_LINUX
foreach (const QByteArray &backend, QCanBus::instance()->plugins()) {
if (backend == "socketcan")
return true;
}
#endif
return false;
}
CANConnection* ConnectionWindow::create(CANCon::type pTye, QString pPortName)
{
CANConnection* conn_p;
/* create connection */
conn_p = CanConFactory::create(pTye, pPortName);
if(conn_p)
{
/* connect signal */
connect(conn_p, SIGNAL(status(CANCon::status)),
this, SLOT(connectionStatus(CANCon::status)));
/*TODO add return value and checks */
conn_p->start();
}
return conn_p;
}
void ConnectionWindow::loadConnections()
{
qRegisterMetaTypeStreamOperators<QVector<QString>>();
qRegisterMetaTypeStreamOperators<QVector<int>>();
qRegisterMetaTypeStreamOperators<CANBus>();
qRegisterMetaTypeStreamOperators<QList<CANBus>>();
QSettings settings;
/* fill connection list */
QVector<QString> portNames = settings.value("connections/portNames").value<QVector<QString>>();
QVector<int> devTypes = settings.value("connections/types").value<QVector<int>>();
QList<CANBus> busses = settings.value("connections/busses").value<QList<CANBus>>();
for(int i=0 ; i<portNames.count() ; i++)
{
CANConnection* conn_p = create((CANCon::type)devTypes[i], portNames[i]);
if(conn_p)
{
for(int j=0 ; j<conn_p->getNumBuses() ; j++)
conn_p->setBusSettings(j, busses.takeFirst());
}
/* add connection to model */
connModel->add(conn_p);
}
if (connModel->rowCount() > 0) {
ui->tableConnections->selectRow(0);
}
}
void ConnectionWindow::saveConnections()
{
QList<CANConnection*>& conns = CANConManager::getInstance()->getConnections();
QSettings settings;
QVector<QString> portNames;
QVector<int> devTypes;
QList<CANBus> busses;
/* save connections */
foreach(CANConnection* conn_p, conns)
{
portNames.append(conn_p->getPort());
devTypes.append(conn_p->getType());
for(int i=0 ; i<conn_p->getNumBuses() ; i++)
{
CANBus bus;
conn_p->getBusSettings(i, bus);
busses.append(bus);
}
}
settings.setValue("connections/portNames", QVariant::fromValue(portNames));
settings.setValue("connections/types", QVariant::fromValue(devTypes));
settings.setValue("connections/busses", QVariant::fromValue(busses));
}
+76
View File
@@ -0,0 +1,76 @@
#ifndef CONNECTIONWINDOW_H
#define CONNECTIONWINDOW_H
#include <QSerialPortInfo>
#include <QDialog>
#include <QDebug>
#include <QSettings>
#include <QTimer>
#include <QItemSelection>
#include "canconnectionmodel.h"
#include "connections/canconnection.h"
class CANConnectionModel;
namespace Ui {
class ConnectionWindow;
}
class ConnectionWindow : public QDialog
{
Q_OBJECT
public:
explicit ConnectionWindow(QWidget *parent = 0);
~ConnectionWindow();
void showEvent(QShowEvent *);
CANCon::type getConnectionType();
bool getSWMode();
signals:
void updateBusSettings(CANBus *bus);
void updatePortName(QString port);
public slots:
void setSpeed(int speed0);
void setSWMode(bool mode);
void setSuspendAll(bool pSuspend);
private slots:
void handleOKButton();
void handleConnTypeChanged();
void currentRowChanged(const QModelIndex &current, const QModelIndex &previous);
void handleRemoveConn();
void handleEnableAll();
void handleDisableAll();
void handleRevert();
void handleNewConn();
void connectionStatus(CANCon::status);
private:
Ui::ConnectionWindow *ui;
QList<QSerialPortInfo> ports;
QSettings *settings;
CANConnectionModel *connModel;
void selectSerial();
void selectKvaser();
void selectSocketCan();
bool isSocketCanAvailable();
int getSpeed();
QString getPortName();
void setPortName(CANCon::type pType, QString pPortName);
void setActiveAll(bool pActive);
CANConnection* create(CANCon::type pTye, QString pPortName);
void loadConnections();
void saveConnections();
};
#endif // CONNECTIONWINDOW_H