From db7ad421990a8e236f2cdbb3d2fe2a93808edbfc Mon Sep 17 00:00:00 2001 From: canpadawan Date: Wed, 29 Jun 2016 11:54:36 +0200 Subject: [PATCH 1/7] add << and >> for serialization --- canbus.cpp | 51 ++++++++++++++++++++--------------- canbus.h | 10 +++++-- connections/canconmanager.cpp | 6 +++++ connections/canconmanager.h | 11 ++++++++ 4 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 connections/canconmanager.cpp create mode 100644 connections/canconmanager.h diff --git a/canbus.cpp b/canbus.cpp index 91e83ad..d0f4646 100644 --- a/canbus.cpp +++ b/canbus.cpp @@ -3,10 +3,10 @@ CANBus::CANBus() { - speed = 250000; - listenOnly = false; - singleWire = false; - active = false; + speed = 250000; + listenOnly = false; + singleWire = false; + active = false; } @@ -24,44 +24,53 @@ bool CANBus::operator==(const CANBus& bus) const{ active == bus.active; } - -void CANBus::setSpeed(int newSpeed) -{ +void CANBus::setSpeed(int newSpeed){ speed = newSpeed; } -void CANBus::setListenOnly(bool mode) -{ +void CANBus::setListenOnly(bool mode){ listenOnly = mode; } -void CANBus::setSingleWire(bool mode) -{ +void CANBus::setSingleWire(bool mode){ singleWire = mode; } -void CANBus::setEnabled(bool mode) -{ +void CANBus::setEnabled(bool mode){ active = mode; } -int CANBus::getSpeed() -{ +int CANBus::getSpeed(){ return speed; } -bool CANBus::isListenOnly() -{ +bool CANBus::isListenOnly(){ return listenOnly; } -bool CANBus::isSingleWire() -{ +bool CANBus::isSingleWire(){ return singleWire; } -bool CANBus::isActive() -{ +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; +} diff --git a/canbus.h b/canbus.h index 8dd8044..9b39358 100644 --- a/canbus.h +++ b/canbus.h @@ -1,6 +1,6 @@ #ifndef CANBus_H #define CANBus_H - +#include class CANBus { @@ -8,7 +8,8 @@ public: CANBus(); CANBus(const CANBus&); bool operator==(const CANBus&) const; - virtual ~CANBus(){}; /*TODO: remove connection from CANBus and add CANBus as an element of CANConnection */ + virtual ~CANBus(){}; + int speed; bool listenOnly; bool singleWire; @@ -25,4 +26,9 @@ public: bool isActive(); }; +QDataStream& operator<<( QDataStream & pStream, const CANBus& pCanBus ); +QDataStream & operator>>(QDataStream & pStream, CANBus& pCanBus); + +Q_DECLARE_METATYPE(CANBus); + #endif // CANBus_H diff --git a/connections/canconmanager.cpp b/connections/canconmanager.cpp new file mode 100644 index 0000000..c363d2a --- /dev/null +++ b/connections/canconmanager.cpp @@ -0,0 +1,6 @@ +#include "canconmanager.h" + +CANConManager::CANConManager() +{ + +} diff --git a/connections/canconmanager.h b/connections/canconmanager.h new file mode 100644 index 0000000..fd17d6e --- /dev/null +++ b/connections/canconmanager.h @@ -0,0 +1,11 @@ +#ifndef CANCONMANAGER_H +#define CANCONMANAGER_H + + +class CANConManager +{ +public: + CANConManager(); +}; + +#endif // CANCONMANAGER_H \ No newline at end of file From e402ea76fa427897fd346a0a04d7e5c646bbfa98 Mon Sep 17 00:00:00 2001 From: canpadawan Date: Wed, 29 Jun 2016 11:55:58 +0200 Subject: [PATCH 2/7] add CANConManager --- connections/canconmanager.cpp | 92 ++++++++++++++++++++++++++++++++++- connections/canconmanager.h | 34 +++++++++++-- connections/canconnection.cpp | 2 +- connections/canconnection.h | 2 +- connections/socketcan.cpp | 2 +- 5 files changed, 124 insertions(+), 8 deletions(-) diff --git a/connections/canconmanager.cpp b/connections/canconmanager.cpp index c363d2a..b0b5a37 100644 --- a/connections/canconmanager.cpp +++ b/connections/canconmanager.cpp @@ -1,6 +1,94 @@ #include "canconmanager.h" -CANConManager::CANConManager() -{ +CANConManager* CANConManager::mInstance = NULL; + +CANConManager* CANConManager::getInstance() +{ + if(!mInstance) + mInstance = new CANConManager(); + + return mInstance; +} + + +CANConManager::CANConManager(QObject *parent): QObject(parent) +{ + connect(&mTimer, SIGNAL(timeout()), this, SLOT(refreshCanList())); + mTimer.setInterval(500); /*tick twice a second */ + mTimer.setSingleShot(false); + mTimer.start(); +} + + +CANConManager::~CANConManager() +{ + mTimer.stop(); + mInstance = NULL; +} + + +void CANConManager::add(CANConnection* pConn_p) +{ + connect(pConn_p, SIGNAL(notify()), this, SLOT(refreshCanList())); + mConns.append(pConn_p); +} + + +void CANConManager::remove(CANConnection* pConn_p) +{ + disconnect(pConn_p, 0, this, 0); + mConns.removeOne(pConn_p); +} + + +void CANConManager::refreshCanList() +{ + QObject* sender_p = QObject::sender(); + + if( sender_p != &mTimer) + { + /* if we are not the sender, the signal is coming from a connection */ + /* refresh only the given connection */ + if(mConns.contains((CANConnection*) sender_p)) + refreshConnection((CANConnection*)sender_p); + } + else + { + foreach (CANConnection* conn_p, mConns) + refreshConnection((CANConnection*)conn_p); + } +} + + +QList& CANConManager::getConnections() +{ + return mConns; +} + + +CANConnection* CANConManager::getByName(const QString& pName) const +{ + foreach(CANConnection* conn_p, mConns) + { + if(conn_p->getPort() == pName) + return conn_p; + } + + return NULL; +} + + +void CANConManager::refreshConnection(CANConnection* pConn_p) +{ + CANFrame* frame_p = NULL; + QVector frames; + + while( (frame_p = pConn_p->getQueue().peek() ) ) { + frames.append(*frame_p); + pConn_p->getQueue().dequeue(); + } + + if(frames.size()) + emit framesReceived(pConn_p, frames); } diff --git a/connections/canconmanager.h b/connections/canconmanager.h index fd17d6e..66896f1 100644 --- a/connections/canconmanager.h +++ b/connections/canconmanager.h @@ -1,11 +1,39 @@ #ifndef CANCONMANAGER_H #define CANCONMANAGER_H +#include +#include -class CANConManager +#include "canconnection.h" + +class CANConManager : public QObject { + Q_OBJECT + public: - CANConManager(); + static CANConManager* getInstance(); + virtual ~CANConManager(); + + void add(CANConnection* pConn_p); + void remove(CANConnection* pConn_p); + QList& getConnections(); + + CANConnection* getByName(const QString& pName) const; + +signals: + void framesReceived(CANConnection* pConn_p, QVector& pFrames); + +private slots: + void refreshCanList(); + +private: + explicit CANConManager(QObject *parent = 0); + void refreshConnection(CANConnection* pConn_p); + + static CANConManager* mInstance; + QList mConns; + QTimer mTimer; }; -#endif // CANCONMANAGER_H \ No newline at end of file +#endif // CANCONNECTIONMODEL_H + diff --git a/connections/canconnection.cpp b/connections/canconnection.cpp index cb88194..778890f 100644 --- a/connections/canconnection.cpp +++ b/connections/canconnection.cpp @@ -191,7 +191,7 @@ bool CANConnection::sendFrames(const QList& pFrames) } -int CANConnection::getNumBuses() { +int CANConnection::getNumBuses() const{ return mNumBuses; } diff --git a/connections/canconnection.h b/connections/canconnection.h index 117b0c7..b0c8f51 100644 --- a/connections/canconnection.h +++ b/connections/canconnection.h @@ -42,7 +42,7 @@ public: * @brief getNumBuses * @return returns the number of buses of the device */ - int getNumBuses(); + int getNumBuses() const; /** * @brief getPort diff --git a/connections/socketcan.cpp b/connections/socketcan.cpp index 64eaa0f..b49e1fe 100644 --- a/connections/socketcan.cpp +++ b/connections/socketcan.cpp @@ -147,7 +147,7 @@ void SocketCan::errorReceived(QCanBusDevice::CanBusError error) const void SocketCan::framesWritten(qint64 count) { - qDebug() << "Number of frames written:" << count; + //qDebug() << "Number of frames written:" << count; } void SocketCan::framesReceived() From 9addf436517c43473714f7027ba27d541ae92eb4 Mon Sep 17 00:00:00 2001 From: canpadawan Date: Wed, 29 Jun 2016 11:57:05 +0200 Subject: [PATCH 3/7] attempt to use new canconnection abstraction --- scriptcontainer.cpp | 45 ++++++++++++++++++++++++++++++++++--------- scriptcontainer.h | 3 ++- scriptingwindow.cpp | 47 ++++++++++++++++++++++++++------------------- scriptingwindow.h | 8 ++------ 4 files changed, 67 insertions(+), 36 deletions(-) diff --git a/scriptcontainer.cpp b/scriptcontainer.cpp index 84b7280..5c4bfa4 100644 --- a/scriptcontainer.cpp +++ b/scriptcontainer.cpp @@ -1,14 +1,12 @@ -#include "scriptcontainer.h" - #include #include -ScriptContainer::ScriptContainer() -{ - fileName = QString(); - filePath = QString(); - scriptText = QString(); +#include "scriptcontainer.h" +#include "connections/canconmanager.h" +ScriptContainer::ScriptContainer(QString& pConName): + mConName(pConName) +{ connect(&timer, SIGNAL(timeout()), this, SLOT(tick())); } @@ -68,6 +66,25 @@ void ScriptContainer::setFilter(QJSValue id, QJSValue mask, QJSValue bus) CANFilter filter; filter.setFilter(idVal, maskVal, busVal); filters.append(filter); + + qDebug() << "FIXME setFilter"; + CANConnection* conn_p = CANConManager::getInstance()->getByName(mConName); + if(conn_p) + { + for(int i=0 ; igetNumBuses() ; i++) + { + QVector fltrs; + foreach(const CANFilter& flt, filters) + { + if(flt.bus==i) { + fltrs.append({flt.ID, flt.mask, true}); + } + } + + if(fltrs.size()) + conn_p->setFilters(i, fltrs, false); + } + } } void ScriptContainer::setTickInterval(QJSValue interval) @@ -86,6 +103,14 @@ void ScriptContainer::clearFilters() { qDebug() << "Called clear filters"; filters.clear(); + + qDebug() << "FIXME clearFilters"; + CANConnection* conn_p = CANConManager::getInstance()->getByName(mConName); + if(conn_p) + { + for(int i=0 ; igetNumBuses() ; i++) + conn_p->setFilters(i, QVector(), false); + } } void ScriptContainer::sendFrame(QJSValue bus, QJSValue id, QJSValue length, QJSValue data) @@ -109,8 +134,10 @@ void ScriptContainer::sendFrame(QJSValue bus, QJSValue id, QJSValue length, QJSV if (frame.ID > 0x7FF) frame.extended = true; - //qDebug() << "Sending frame from script"; - emit sendCANFrame(&frame); + qDebug() << "sending frame from script"; + CANConnection* conn_p = CANConManager::getInstance()->getByName(mConName); + if(conn_p) + conn_p->sendFrame(frame); } void ScriptContainer::gotFrame(const CANFrame &frame) diff --git a/scriptcontainer.h b/scriptcontainer.h index fd4b33d..75caec2 100644 --- a/scriptcontainer.h +++ b/scriptcontainer.h @@ -13,12 +13,13 @@ class ScriptContainer : public QObject Q_OBJECT public: - ScriptContainer(); + ScriptContainer(QString&); void gotFrame(const CANFrame &frame); QString fileName; QString filePath; QString scriptText; + QString mConName; public slots: void compileScript(); diff --git a/scriptingwindow.cpp b/scriptingwindow.cpp index 19ab11d..58115dd 100644 --- a/scriptingwindow.cpp +++ b/scriptingwindow.cpp @@ -3,9 +3,10 @@ #include #include +#include #include -#include "mainwindow.h" +#include "connections/canconmanager.h" ScriptingWindow::ScriptingWindow(const QVector *frames, QWidget *parent) : QDialog(parent), @@ -23,7 +24,8 @@ ScriptingWindow::ScriptingWindow(const QVector *frames, QWidget *paren connect(ui->btnRemoveScript, &QAbstractButton::pressed, this, &ScriptingWindow::deleteCurrentScript); connect(ui->btnRevertScript, &QAbstractButton::pressed, this, &ScriptingWindow::revertScript); connect(ui->btnSaveScript, &QAbstractButton::pressed, this, &ScriptingWindow::saveScript); - connect(MainWindow::getReference(), &MainWindow::framesUpdated, this, &ScriptingWindow::updatedFrames); + + connect(CANConManager::getInstance(), &CANConManager::framesReceived, this, &ScriptingWindow::newFrames); ui->txtScriptSource->setLexer(new QsciLexerJavaScript(ui->txtScriptSource)); } @@ -33,27 +35,22 @@ ScriptingWindow::~ScriptingWindow() delete ui; } -void ScriptingWindow::updatedFrames(int numFrames) + +void ScriptingWindow::newFrames(const CANConnection* pConn, const QVector& pFrames) { - CANFrame thisFrame; - //-1 means all frames deleted and -2 means a full refresh, neither of which we care about here. - if (numFrames > 0) + /*FIXME: name of the probe and bus should be checked */ + Q_UNUSED(pConn); + + for (int j = 0; j < scripts.length(); j++) { - if (numFrames > modelFrames->count()) return; - qDebug() << "Got frames into script window: " << numFrames; - //for every new frame pass it on to each script container. The container will determine if it needs to actually - //notify the script and do that if applicable. - for (int i = modelFrames->count() - numFrames; i < modelFrames->count(); i++) + foreach(const CANFrame& frame, pFrames) { - thisFrame = modelFrames->at(i); - for (int j = 0; j < scripts.length(); j++) - { - scripts[j]->gotFrame(thisFrame); - } + scripts[j]->gotFrame(frame); } } } + void ScriptingWindow::closeEvent(QCloseEvent *event) { Q_UNUSED(event); @@ -115,13 +112,19 @@ void ScriptingWindow::loadNewScript() QStringList fileList = filename.split('/'); QString justFileName = fileList[fileList.length() - 1]; ui->listLoadedScripts->addItem(justFileName); - container = new ScriptContainer(); + /* get the first connection in the list for now */ + qDebug() << "FIXME: connection is always the first in list"; + QString portName; + QList list = CANConManager::getInstance()->getConnections(); + if(!list.isEmpty()) + portName = list.first()->getPort(); + + container = new ScriptContainer(portName); container->fileName = justFileName; container->filePath = filename; container->scriptText = contents; container->setErrorWidget(ui->listErrors); container->compileScript(); - connect(container, &ScriptContainer::sendCANFrame, this, &ScriptingWindow::sendCANFrame); scripts.append(container); currentScript = container; ui->txtScriptSource->setText(container->scriptText); @@ -135,13 +138,17 @@ void ScriptingWindow::createNewScript() { ScriptContainer *container; - container = new ScriptContainer(); + QString portName; + QList list = CANConManager::getInstance()->getConnections(); + if(!list.isEmpty()) + portName = list.first()->getPort(); + + container = new ScriptContainer(portName); container->fileName = "UNNAMED_" + QString::number((qrand() % 10000)) + ".js"; container->filePath = QString(); container->scriptText = QString(); container->setErrorWidget(ui->listErrors); - connect(container, &ScriptContainer::sendCANFrame, this, &ScriptingWindow::sendCANFrame); scripts.append(container); ui->listLoadedScripts->addItem(container->fileName); currentScript = container; diff --git a/scriptingwindow.h b/scriptingwindow.h index 179e672..6713460 100644 --- a/scriptingwindow.h +++ b/scriptingwindow.h @@ -3,6 +3,7 @@ #include "can_structs.h" #include "scriptcontainer.h" +#include "connections/canconnection.h" #include #include @@ -28,12 +29,7 @@ private slots: void saveScript(); void revertScript(); void recompileScript(); - void updatedFrames(int); - -public slots: - -signals: - void sendCANFrame(const CANFrame *); + void newFrames(const CANConnection*, const QVector&); private: void closeEvent(QCloseEvent *event); From 916ed4e6577ea9c325591ac80720e799c944eb8f Mon Sep 17 00:00:00 2001 From: canpadawan Date: Wed, 29 Jun 2016 11:57:52 +0200 Subject: [PATCH 4/7] canframemodel now receives frames from CANConManager --- canframemodel.cpp | 45 +++++++++++++++++++-------------------------- canframemodel.h | 6 ++++-- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/canframemodel.cpp b/canframemodel.cpp index f1a3b64..e1cb200 100644 --- a/canframemodel.cpp +++ b/canframemodel.cpp @@ -282,7 +282,7 @@ QVariant CANFrameModel::headerData(int section, Qt::Orientation orientation, } -void CANFrameModel::addFrame(CANFrame& frame, bool autoRefresh = false) +void CANFrameModel::addFrame(const CANFrame& frame, bool autoRefresh = false) { /*TODO: remove mutex */ mutex.lock(); @@ -346,6 +346,15 @@ void CANFrameModel::addFrame(CANFrame& frame, bool autoRefresh = false) mutex.unlock(); } + +void CANFrameModel::addFrames(const CANConnection*, const QVector& pFrames) +{ + foreach(const CANFrame& frame, pFrames) + { + addFrame(frame); + } +} + void CANFrameModel::sendRefresh() { qDebug() << "Sending mass refresh"; @@ -377,38 +386,22 @@ void CANFrameModel::sendRefresh(int pos) //issue a refresh for the last num entries in the model. //used by the serial worker to do batch updates so it doesn't //have to send thousands of messages per second -void CANFrameModel::sendBulkRefresh(int num) +int CANFrameModel::sendBulkRefresh() { - //yes, num was sent to us by the serial worker but we actually have a better idea - //of how many by tracking the number we last knew about as opposed to how many rows there - //are now. - num = filteredFrames.count() - lastUpdateNumFrames; + int num = filteredFrames.count() - lastUpdateNumFrames; + if (num <= 0) return 0; - if (num < 0) return; - - //qDebug() << "Num: " << num; - - if (num == 0 && !overwriteDups) return; - if (filteredFrames.count() == 0) return; + if (num == 0 && !overwriteDups) return 0; + if (filteredFrames.count() == 0) return 0; lastUpdateNumFrames += num; //done this way to avoid asking for filteredFrames.count() again qDebug() << "Bulk refresh of " << num; - if (!overwriteDups) - { - //if (num > filteredFrames.count()) num = filteredFrames.count(); - //qDebug() << "From " << (filteredFrames.count() - num) << " to " << (filteredFrames.count() - 1); - //beginInsertRows(QModelIndex(), filteredFrames.count() - num, filteredFrames.count() - 1); - //endInsertRows(); - beginResetModel(); - endResetModel(); - } - else - { - beginResetModel(); - endResetModel(); - } + beginResetModel(); + endResetModel(); + + return num; } void CANFrameModel::clearFrames() diff --git a/canframemodel.h b/canframemodel.h index ddea3ce..57e6e6a 100644 --- a/canframemodel.h +++ b/canframemodel.h @@ -8,6 +8,7 @@ #include #include "can_structs.h" #include "dbchandler.h" +#include "connections/canconnection.h" class CANFrameModel: public QAbstractTableModel { @@ -25,7 +26,7 @@ public: void sendRefresh(); void sendRefresh(int); - void sendBulkRefresh(int); + int sendBulkRefresh(); void clearFrames(); void setDBCHandler(DBCHandler *); void setInterpetMode(bool); @@ -46,7 +47,8 @@ public: const QMap *getFiltersReference() const; //this neither public slots: - void addFrame(CANFrame&, bool); + void addFrame(const CANFrame&, bool); + void addFrames(const CANConnection*, const QVector&); signals: void updatedFiltersList(); From a9fa6dcd907c55d13ea645b749c9af9264dcd460 Mon Sep 17 00:00:00 2001 From: canpadawan Date: Wed, 29 Jun 2016 12:00:05 +0200 Subject: [PATCH 5/7] fix several UI weirdness --- canconnectionmodel.cpp | 69 +++++--- canconnectionmodel.h | 12 +- connectionwindow.cpp | 384 ++++++++++++++++++----------------------- connectionwindow.h | 28 ++- 4 files changed, 234 insertions(+), 259 deletions(-) diff --git a/canconnectionmodel.cpp b/canconnectionmodel.cpp index 3f96f94..404b3cd 100644 --- a/canconnectionmodel.cpp +++ b/canconnectionmodel.cpp @@ -1,11 +1,16 @@ -#include "connections/canconnection.h" #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 { @@ -52,17 +57,20 @@ QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation int CANConnectionModel::columnCount(const QModelIndex &parent) const { + Q_UNUSED(parent); return 7; } -int CANConnectionModel::rowCount(const QModelIndex &parent) const { - int rows=0; - QList::const_iterator iter; +int CANConnectionModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); - for (iter = mConns.begin() ; iter != mConns.end() ; ++iter) { - rows+=(*iter)->getNumBuses(); - } + int rows=0; + QList& conns = CANConManager::getInstance()->getConnections(); + + foreach(const CANConnection* conn_p, conns) + rows+=conn_p->getNumBuses(); return rows; } @@ -124,49 +132,64 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const void CANConnectionModel::add(CANConnection* pConn_p) { + CANConManager* manager = CANConManager::getInstance(); + + connect(pConn_p, SIGNAL(notify()), manager, SLOT(refreshCanList())); + beginResetModel(); - mConns.append(pConn_p); + manager->getConnections().append(pConn_p); endResetModel(); } void CANConnectionModel::remove(CANConnection* pConn_p) { + CANConManager* manager = CANConManager::getInstance(); + + disconnect(pConn_p, 0, manager, 0); + beginResetModel(); - mConns.removeOne(pConn_p); + manager->getConnections().removeOne(pConn_p); endResetModel(); } -QList& CANConnectionModel::getConnections() -{ - return mConns; -} - - CANConnection* CANConnectionModel::getAtIdx(int pIdx, int& pBusId) const { if (pIdx < 0) return NULL; int i=0; - QList::const_iterator iter = mConns.begin(); + QList& conns = CANConManager::getInstance()->getConnections(); - for (iter = mConns.begin() ; iter != mConns.end() ; ++iter) { - if( i <= pIdx && pIdx < i+(*iter)->getNumBuses() ) { + foreach(CANConnection* conn_p, conns) + { + if( i <= pIdx && pIdx < i+conn_p->getNumBuses() ) { pBusId = pIdx - i; - return (*iter); + return conn_p; } - i+= (*iter)->getNumBuses(); + i+= conn_p->getNumBuses(); } return NULL; } -void CANConnectionModel::refreshView() +void CANConnectionModel::refresh(int pIndex) { - beginResetModel(); - endResetModel(); + QModelIndex begin; + QModelIndex end; + + if(pIndex>=0) { + begin = createIndex(pIndex, 0); + end = begin; + } + else { + begin = createIndex(0, 0); + begin = createIndex(rowCount()-1, 0); + /*beginResetModel(); + endResetModel();*/ + } + dataChanged(begin, end); } diff --git a/canconnectionmodel.h b/canconnectionmodel.h index c91c9e2..68a8d45 100644 --- a/canconnectionmodel.h +++ b/canconnectionmodel.h @@ -15,25 +15,19 @@ class CANConnectionModel : public QAbstractTableModel public: explicit CANConnectionModel(QObject *parent = 0); + virtual ~CANConnectionModel(); - // Header: + // 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); - QList& getConnections(); CANConnection* getAtIdx(int, int&) const; - void refreshView(); - -private: - QList mConns; + void refresh(int pIndex=-1); }; #endif // CANCONNECTIONMODEL_H diff --git a/connectionwindow.cpp b/connectionwindow.cpp index 9ef7f3b..6b1fc77 100644 --- a/connectionwindow.cpp +++ b/connectionwindow.cpp @@ -4,29 +4,31 @@ #include "connectionwindow.h" #include "ui_connectionwindow.h" #include "connections/canconfactory.h" - -#define FALSE 0 -#define TRUE 1 +#include "connections/canconmanager.h" +#include "canbus.h" -ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : + +ConnectionWindow::ConnectionWindow(QWidget *parent) : QDialog(parent), - ui(new Ui::ConnectionWindow), - mRefreshReqOngoing(FALSE) + ui(new Ui::ConnectionWindow) { ui->setupUi(this); - settings = new QSettings(); + QSettings settings; qRegisterMetaType("CANBus"); qRegisterMetaType("const CANFrame *"); qRegisterMetaType *>("const QList *"); - connModel = new CANConnectionModel(); + qRegisterMetaTypeStreamOperators>(); + qRegisterMetaTypeStreamOperators>(); + qRegisterMetaTypeStreamOperators(); + qRegisterMetaTypeStreamOperators>(); + + + connModel = new CANConnectionModel(this); ui->tableConnections->setModel(connModel); - - canModel = cModel; - ui->tableConnections->setColumnWidth(0, 50); ui->tableConnections->setColumnWidth(1, 110); ui->tableConnections->setColumnWidth(2, 110); @@ -36,13 +38,8 @@ ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : ui->tableConnections->setColumnWidth(6, 75); ui->tableConnections->setColumnWidth(7, 75); - int temp = settings->value("Main/DefaultConnectionType", 0).toInt(); - - //currentPortName = settings->value("Main/DefaultConnectionPort", "").toString(); - - //currentSpeed1 = -1; - - ui->ckSingleWire->setChecked(settings->value("Main/SingleWireMode", false).toBool()); + //int temp = settings.value("Main/DefaultConnectionType", 0).toInt(); + ui->ckSingleWire->setChecked(settings.value("Main/SingleWireMode", false).toBool()); ui->cbSpeed->addItem(tr("")); ui->cbSpeed->addItem(tr("125000")); @@ -51,6 +48,9 @@ ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : ui->cbSpeed->addItem(tr("1000000")); ui->cbSpeed->addItem(tr("33333")); + /* load connection configuration */ + loadConnections(); + #ifdef Q_OS_LINUX ui->rbSocketCAN->setEnabled(isSocketCanAvailable()); #endif @@ -60,105 +60,81 @@ ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : #endif connect(ui->btnOK, &QAbstractButton::clicked, this, &ConnectionWindow::handleOKButton); - connect(ui->rbGVRET, &QAbstractButton::toggled, this, &ConnectionWindow::handleConnTypeChanged); - connect(ui->rbKvaser, &QAbstractButton::toggled, this, &ConnectionWindow::handleConnTypeChanged); - connect(ui->rbSocketCAN, &QAbstractButton::toggled, this, &ConnectionWindow::handleConnTypeChanged); + 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::selectionChanged, this, &ConnectionWindow::handleConnSelectionChanged); - connect(connModel, &QAbstractItemModel::modelReset, this, &ConnectionWindow::handleConnSelectionChanged); + 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); - - - qDebug() << "Serial worker thread starting"; - - connect(&mTicker, SIGNAL(timeout()), this, SLOT(refreshCanList())); - /* tick frequency has a huge impact on performances */ - /* TODO: make this configurable and part of the connection constructor to let connection configure the length of the queue */ - mTicker.setInterval(500); /*tick twice a second */ - mTicker.setSingleShot(false); - mTicker.start(); } ConnectionWindow::~ConnectionWindow() { - QList& conns = connModel->getConnections(); + QList& conns = CANConManager::getInstance()->getConnections(); CANConnection* conn_p; + /* save configuration */ + saveConnections(); + /* delete connections */ - while(!conns.isEmpty()) { + while(!conns.isEmpty()) + { conn_p = conns.takeFirst(); conn_p->stop(); delete conn_p; } - delete connModel; - - mTicker.stop(); - delete settings; delete ui; } + void ConnectionWindow::showEvent(QShowEvent* event) { QDialog::showEvent(event); qDebug() << "Show connectionwindow"; - handleConnTypeChanged(); + ui->tableConnections->selectRow(0); } -void ConnectionWindow::refreshCanList() { - - QList& conns = connModel->getConnections(); - CANFrame* frame_p = NULL; - - foreach (CANConnection* conn_p, conns) - { - while( (frame_p = conn_p->getQueue().peek() ) ) { - canModel->addFrame(*frame_p, true); - conn_p->getQueue().dequeue(); - } - } - - /* erase flag (this should be done before we start dequeuing...) */ - mRefreshReqOngoing.store(FALSE); -} - -void ConnectionWindow::handleNewConn() -{ - ui->tableConnections->selectionModel()->clearSelection(); - ui->tableConnections->selectionModel()->clearCurrentIndex(); - handleConnSelectionChanged(); -} void ConnectionWindow::setSuspendAll(bool pSuspend) { - qDebug() << "setSuspendAll"; + QList& conns = CANConManager::getInstance()->getConnections(); - QList::iterator iter; - QList& conns = connModel->getConnections(); + foreach(CANConnection* conn_p, conns) + conn_p->suspend(pSuspend); - for (iter = conns.begin(); iter != conns.end(); ++iter) - (*iter)->suspend(pSuspend); + connModel->refresh(); } + void ConnectionWindow::setActiveAll(bool pActive) { - QList::iterator iter; - QList& conns = connModel->getConnections(); CANBus bus; + QList& conns = CANConManager::getInstance()->getConnections(); - for (iter = conns.begin(); iter != conns.end(); ++iter) { - for(int i=0 ; i<(*iter)->getNumBuses() ; i++) { - if( (*iter)->getBusSettings(i, bus) ) { + foreach(CANConnection* conn_p, conns) + { + for(int i=0 ; igetNumBuses() ; i++) { + if( conn_p->getBusSettings(i, bus) ) { bus.active = pActive; - (*iter)->setBusSettings(i, bus); + conn_p->setBusSettings(i, bus); } } } + + connModel->refresh(); } + +void ConnectionWindow::handleNewConn() +{ + ui->tableConnections->setCurrentIndex(QModelIndex()); +} + + void ConnectionWindow::handleEnableAll() { setActiveAll(true); @@ -180,8 +156,10 @@ void ConnectionWindow::handleConnTypeChanged() /* status */ void ConnectionWindow::connectionStatus(CANCon::status pStatus) { + Q_UNUSED(pStatus); + qDebug() << "Connectionstatus changed"; - connModel->refreshView(); + connModel->refresh(); } @@ -219,78 +197,15 @@ void ConnectionWindow::handleOKButton() /* update bus settings */ conn_p->setBusSettings(busId, bus); - connModel->refreshView(); + connModel->refresh(whichRow); } - else //new connection + else if( ! CANConManager::getInstance()->getByName(getPortName()) ) { -#if 0 - if (ui->rbGVRET->isChecked()) - { - - SerialWorker *serial = new SerialWorker(canModel, connModel->rowCount()); - connect(serial, SIGNAL(busStatus(int,int,int)), this, SLOT(receiveBusStatus(int,int,int))); - connect(serial, SIGNAL(connectionSuccess(CANConnection*)), this, SLOT(connectionSuccess(CANConnection*))); - CANConnectionContainer* container = new CANConnectionContainer(serial); - - - qDebug() << "Setup initial connection object"; - - CANBus bus; - bus.active = ui->ckEnabled->isChecked(); - bus.busNum = serial->getBusBase(); - bus.container = container; - bus.listenOnly = ui->ckListenOnly->isChecked(); - bus.singleWire = ui->ckSingleWire->isChecked(); - - if (ui->cbSpeed->currentIndex() < 1) bus.speed = 0; //default speed - else if (ui->cbSpeed->currentIndex() == 1) - { - bus.speed = 0; - bus.active = false; - } - else bus.speed = ui->cbSpeed->currentText().toInt(); - - connModel->addBus(bus); - - int numBuses = serial->getNumBuses(); - for (int i = 1; i < numBuses; i++) - { - bus.active = false; - bus.listenOnly = false; - bus.singleWire = false; - bus.speed = 250000; - bus.busNum = serial->getBusBase() + i; - bus.container = container; - connModel->addBus(bus); - qDebug() << "Added bus " << bus.busNum; - } - - //call through signal/slot interface without using connect - QMetaObject::invokeMethod(serial, "updatePortName", - Qt::QueuedConnection, - Q_ARG(QString, ui->cbPort->currentText())); -#endif - /* create connection */ - conn_p = CanConFactory::create(getConnectionType(), getPortName()); + conn_p = create(getConnectionType(), getPortName()); if(!conn_p) return; - /* connect signal */ - connect(conn_p, SIGNAL(status(CANCon::status)), - this, SLOT(connectionStatus(CANCon::status))); - - //conn_p->setCallback(std::bind(&ConnectionWindow::callback, this, std::placeholders::_1)); - - /*TODO add return value and checks */ - conn_p->start(); - /*{ - QVector flters; - flters.append({0x305, 0xFFFF, true}); - conn_p->setFilters(0, flters, false); - connect(conn_p, SIGNAL(notify()), this, SLOT(refreshCanList())); - }*/ - for (int i=0 ; igetNumBuses() ; i++) { /* set bus configuration */ CANBus bus; @@ -311,45 +226,27 @@ void ConnectionWindow::handleOKButton() } -void ConnectionWindow::receiveBusStatus(int bus, int speed, int status) -{ -#if 0 - qDebug() << "bus " << bus << " speed " << speed << " status " << status; - CANBus *busRef = connModel->getBus(bus); - if (status & 40) busRef->setSpeed(speed); - if (status & 8) //update enabled status - { - busRef->setEnabled((status & 1)?true:false); - } - if (status & 0x10) //update single wire status - { - busRef->setSingleWire((status & 2)?true:false); - } - if (status & 0x20) //update listen only status - { - busRef->setListenOnly((status & 4)?true:false); - } - connModel->refreshView(); -#endif -} -void ConnectionWindow::handleConnSelectionChanged() +void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) { - int selIdx = ui->tableConnections->selectionModel()->currentIndex().row(); + 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->cbPort->setEnabled(true); - ui->rbGVRET->setEnabled(true); - ui->rbKvaser->setEnabled(true); - ui->rbSocketCAN->setEnabled(true); - ui->cbSpeed->setEnabled(false); - + ui->rbGVRET->setChecked(true); ui->cbPort->setCurrentIndex(0); ui->ckListenOnly->setChecked(false); ui->ckSingleWire->setChecked(false); - ui->ckEnabled->setChecked(true); + ui->ckEnabled->setChecked(false); } else { @@ -363,25 +260,16 @@ void ConnectionWindow::handleConnSelectionChanged() ui->btnOK->setText(tr("Update Connection Settings")); - ui->cbPort->setEnabled(false); - ui->rbGVRET->setEnabled(false); - ui->rbKvaser->setEnabled(false); - ui->rbSocketCAN->setEnabled(false); - ui->cbSpeed->setEnabled(true); - - switch(conn_p->getType()) { - 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: {} - } + setPortName(conn_p->getType(), conn_p->getPort()); + setSpeed(bus.getSpeed()); ui->ckListenOnly->setChecked(bus.isListenOnly()); ui->ckSingleWire->setChecked(bus.isSingleWire()); ui->ckEnabled->setChecked(bus.isActive()); - int speed = bus.getSpeed(); - setSpeed(speed); + /* this won't be called if elements are disabled */ } + + handleConnTypeChanged(); } @@ -394,10 +282,7 @@ void ConnectionWindow::selectSerial() ports = QSerialPortInfo::availablePorts(); for (int i = 0; i < ports.count(); i++) - { ui->cbPort->addItem(ports[i].portName()); - //if (currentPortName == ports[i].portName()) ui->cbPort->setCurrentIndex(i); - } } void ConnectionWindow::selectKvaser() @@ -438,6 +323,32 @@ void ConnectionWindow::setSpeed(int speed0) } +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); + 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() @@ -507,7 +418,7 @@ void ConnectionWindow::handleRemoveConn() CANConnection* conn_p = connModel->getAtIdx(selIdx, busId); if(!conn_p) return; - /* remove connection from model */ + /* remove connection from model & manager */ connModel->remove(conn_p); /* stop and delete connection */ @@ -520,28 +431,6 @@ void ConnectionWindow::handleRevert() } -void ConnectionWindow::sendFrame(const CANFrame *frame) -{ -#if 0 - CANBus *bus = connModel->getBus(frame->bus); - if (bus == NULL) return; - QMetaObject::invokeMethod(bus->getContainer()->getRef(), "sendFrame", - Qt::QueuedConnection, - Q_ARG(const CANFrame *, frame)); -#endif -} - -void ConnectionWindow::sendFrameBatch(const QList *frames) -{ -#if 0 - if (frames->count() == 0) return; - CANBus *bus = connModel->getBus(frames->at(0).bus); - if (bus == NULL) return; - QMetaObject::invokeMethod(bus->getContainer()->getRef(), "sendFrameBatch", - Qt::QueuedConnection, - Q_ARG(const QList *, frames)); -#endif -} bool ConnectionWindow::isSocketCanAvailable() { @@ -554,3 +443,74 @@ bool ConnectionWindow::isSocketCanAvailable() #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() +{ + QSettings settings; + + /* fill connection list */ + QVector portNames = settings.value("connections/portNames").value>(); + QVector devTypes = settings.value("connections/types").value>(); + QList busses = settings.value("connections/busses").value>(); + + + for(int i=0 ; igetNumBuses() ; j++) + conn_p->setBusSettings(j, busses.takeFirst()); + } + /* add connection to model */ + connModel->add(conn_p); + } +} + +void ConnectionWindow::saveConnections() +{ + QList& conns = CANConManager::getInstance()->getConnections(); + + QSettings settings; + QVector portNames; + QVector devTypes; + QList busses; + + /* delete connections */ + foreach(CANConnection* conn_p, conns) + { + portNames.append(conn_p->getPort()); + devTypes.append(conn_p->getType()); + + for(int i=0 ; igetNumBuses() ; 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)); +} diff --git a/connectionwindow.h b/connectionwindow.h index 5cc9b4c..56062da 100644 --- a/connectionwindow.h +++ b/connectionwindow.h @@ -8,8 +8,9 @@ #include #include #include +#include #include "canconnectionmodel.h" -#include "canframemodel.h" +#include "connections/canconnection.h" class CANConnectionModel; @@ -24,11 +25,10 @@ class ConnectionWindow : public QDialog Q_OBJECT public: - explicit ConnectionWindow(CANFrameModel *canModel, QWidget *parent = 0); + explicit ConnectionWindow(QWidget *parent = 0); ~ConnectionWindow(); void showEvent(QShowEvent *); - int getSpeed(); - QString getPortName(); //name of port to connect to + CANCon::type getConnectionType(); bool getSWMode(); @@ -39,40 +39,38 @@ signals: public slots: void setSpeed(int speed0); void setSWMode(bool mode); - void sendFrame(const CANFrame *); - void sendFrameBatch(const QList *); - void setSuspendAll(bool); + void setSuspendAll(bool pSuspend); private slots: void handleOKButton(); void handleConnTypeChanged(); - void handleConnSelectionChanged(); + void currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous); void handleRemoveConn(); void handleEnableAll(); void handleDisableAll(); void handleRevert(); void handleNewConn(); - void receiveBusStatus(int bus, int speed, int status); void connectionStatus(CANCon::status); - void refreshCanList(); - private: Ui::ConnectionWindow *ui; QList ports; QSettings *settings; CANConnectionModel *connModel; - CANFrameModel *canModel; - QTimer mTicker; - - QAtomicInt mRefreshReqOngoing; 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 From d4b6575f5da44310c1c94ad8ae8d8378cdb338b6 Mon Sep 17 00:00:00 2001 From: canpadawan Date: Wed, 29 Jun 2016 12:02:16 +0200 Subject: [PATCH 6/7] minor changes and fixes --- SavvyCAN.pro | 6 ++-- canfilter.h | 2 +- connectionwindow.ui | 4 +-- mainwindow.cpp | 83 +++++++++++++++++---------------------------- mainwindow.ui | 5 ++- 5 files changed, 42 insertions(+), 58 deletions(-) diff --git a/SavvyCAN.pro b/SavvyCAN.pro index 34879f4..b1369e2 100644 --- a/SavvyCAN.pro +++ b/SavvyCAN.pro @@ -54,7 +54,8 @@ SOURCES += main.cpp\ connections/canconnection.cpp \ connections/socketcan.cpp \ connections/canconfactory.cpp \ - connections/gvretserial.cpp + connections/gvretserial.cpp \ + connections/canconmanager.cpp HEADERS += mainwindow.h \ can_structs.h \ @@ -97,7 +98,8 @@ HEADERS += mainwindow.h \ connections/socketcan.h \ connections/canconconst.h \ connections/canconfactory.h \ - connections/gvretserial.h + connections/gvretserial.h \ + connections/canconmanager.h FORMS += mainwindow.ui \ graphingwindow.ui \ diff --git a/canfilter.h b/canfilter.h index 91a84e3..c2cb71b 100644 --- a/canfilter.h +++ b/canfilter.h @@ -10,7 +10,7 @@ public: void setFilter(uint32_t id, uint32_t mask, int bus); bool checkFilter(uint32_t id, int bus); -private: +public: uint32_t ID; uint32_t mask; int bus; diff --git a/connectionwindow.ui b/connectionwindow.ui index f3a5b39..5e7ffc4 100644 --- a/connectionwindow.ui +++ b/connectionwindow.ui @@ -74,7 +74,7 @@ - + Connection Type @@ -116,7 +116,7 @@ - + 0 diff --git a/mainwindow.cpp b/mainwindow.cpp index f19f682..59a8d84 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -4,7 +4,7 @@ #include #include #include -#include "canframemodel.h" +#include "connections/canconmanager.h" #include "utility.h" #include "serialworker.h" @@ -62,28 +62,6 @@ MainWindow::MainWindow(QWidget *parent) : ui->canFramesView->setColumnWidth(6, 275); QHeaderView *HorzHdr = ui->canFramesView->horizontalHeader(); HorzHdr->setStretchLastSection(true); //causes the data column to automatically fill the tableview - //enabling the below line kills performance in every way imaginable. Left here as a warning. Do not do this. - //ui->canFramesView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); - - /* - worker = new SerialWorker(model); - worker->moveToThread(&serialWorkerThread); - connect(&serialWorkerThread, &QThread::finished, worker, &QObject::deleteLater); - connect(&serialWorkerThread, &QThread::started, worker, &SerialWorker::run); //setup timers within the proper thread - connect(this, &MainWindow::sendSerialPort, worker, &SerialWorker::setSerialPort, Qt::QueuedConnection); - connect(worker, &SerialWorker::frameUpdateRapid, this, &MainWindow::gotFrames, Qt::QueuedConnection); - connect(this, &MainWindow::updateBaudRates, worker, &SerialWorker::updateBaudRates, Qt::QueuedConnection); - connect(this, &MainWindow::sendCANFrame, worker, &SerialWorker::sendFrame, Qt::QueuedConnection); - connect(worker, &SerialWorker::connectionSuccess, this, &MainWindow::connectionSucceeded, Qt::QueuedConnection); - connect(worker, &SerialWorker::connectionFailure, this, &MainWindow::connectionFailed, Qt::QueuedConnection); - connect(worker, &SerialWorker::deviceInfo, this, &MainWindow::gotDeviceInfo, Qt::QueuedConnection); - connect(this, &MainWindow::closeSerialPort, worker, &SerialWorker::closeSerialPort, Qt::QueuedConnection); - connect(this, &MainWindow::startFrameCapturing, worker, &SerialWorker::startFrameCapture); - connect(this, &MainWindow::stopFrameCapturing, worker, &SerialWorker::stopFrameCapture); - connect(this, &MainWindow::settingsUpdated, worker, &SerialWorker::readSettings); - serialWorkerThread.start(); - serialWorkerThread.setPriority(QThread::HighPriority); - */ graphingWindow = NULL; frameInfoWindow = NULL; @@ -146,6 +124,8 @@ MainWindow::MainWindow(QWidget *parent) : connect(ui->actionUDS_Scanner, &QAction::triggered, this, &MainWindow::showUDSScanWindow); connect(ui->actionISO_TP_Decoder, &QAction::triggered, this, &MainWindow::showISOInterpreterWindow); + connect(CANConManager::getInstance(), &CANConManager::framesReceived, model, &CANFrameModel::addFrames); + lbStatusConnected.setText(tr("Not connected")); updateFileStatus(); lbStatusDatabase.setText(tr("No DBC database loaded")); @@ -179,8 +159,7 @@ MainWindow::MainWindow(QWidget *parent) : model->clearFrames(); //Automatically create the connection window so it can be updated even if we never opened it. - connectionWindow = new ConnectionWindow(model); - connect(connectionWindow, SIGNAL(updateConnectionSettings(QString,QString,int,int)), this, SLOT(updateConnectionSettings(QString,QString,int,int))); + connectionWindow = new ConnectionWindow(); connect(this, SIGNAL(suspendCapturing(bool)), connectionWindow, SLOT(setSuspendAll(bool))); } @@ -480,28 +459,30 @@ void MainWindow::filterClearAll() void MainWindow::tickGUIUpdate() { - int elapsed = elapsedTime->elapsed(); - if(elapsed) { - framesPerSec += rxFrames * 1000 / elapsed - (framesPerSec / 4); - elapsedTime->restart(); - } - else - framesPerSec = 0; - - model->sendBulkRefresh(rxFrames); - - ui->lbNumFrames->setText(QString::number(model->rowCount())); - if (ui->cbAutoScroll->isChecked()) ui->canFramesView->scrollToBottom(); - ui->lbFPS->setText(QString::number(framesPerSec / 4)); - if (rxFrames > 0) + rxFrames = model->sendBulkRefresh(); + if(rxFrames>0) { - bDirty = true; - emit framesUpdated(rxFrames); //anyone care that frames were updated? + int elapsed = elapsedTime->elapsed(); + if(elapsed) { + framesPerSec = rxFrames * 1000 / elapsed; + elapsedTime->restart(); + } + else + framesPerSec = 0; + + ui->lbNumFrames->setText(QString::number(model->rowCount())); + if (ui->cbAutoScroll->isChecked()) ui->canFramesView->scrollToBottom(); + ui->lbFPS->setText(QString::number(framesPerSec)); + if (rxFrames > 0) + { + bDirty = true; + emit framesUpdated(rxFrames); //anyone care that frames were updated? + } + + if (model->needsFilterRefresh()) updateFilterList(); + + rxFrames = 0; } - - if (model->needsFilterRefresh()) updateFilterList(); - - rxFrames = 0; } void MainWindow::gotFrames(int framesSinceLastUpdate) @@ -867,7 +848,7 @@ void MainWindow::showFrameSenderWindow() else frameSenderWindow = new FrameSenderWindow(model->getFilteredListReference()); - connect(frameSenderWindow, &FrameSenderWindow::sendCANFrame, connectionWindow, &ConnectionWindow::sendFrame); + //connect(frameSenderWindow, &FrameSenderWindow::sendCANFrame, connectionWindow, &ConnectionWindow::sendFrame); } frameSenderWindow->show(); } @@ -881,8 +862,8 @@ void MainWindow::showPlaybackWindow() else playbackWindow = new FramePlaybackWindow(model->getFilteredListReference()); - connect(playbackWindow, SIGNAL(sendCANFrame(const CANFrame*)), connectionWindow, SLOT(sendFrame(const CANFrame*)), Qt::QueuedConnection); - connect(playbackWindow, SIGNAL(sendFrameBatch(const QList*)), connectionWindow, SLOT(sendFrameBatch(const QList*)), Qt::QueuedConnection); + //connect(playbackWindow, SIGNAL(sendCANFrame(const CANFrame*)), connectionWindow, SLOT(sendFrame(const CANFrame*)), Qt::QueuedConnection); + //connect(playbackWindow, SIGNAL(sendFrameBatch(const QList*)), connectionWindow, SLOT(sendFrameBatch(const QList*)), Qt::QueuedConnection); } playbackWindow->show(); } @@ -892,7 +873,7 @@ void MainWindow::showFirmwareUploaderWindow() if (!firmwareUploaderWindow) { firmwareUploaderWindow = new FirmwareUploaderWindow(model->getListReference()); - connect(firmwareUploaderWindow, SIGNAL(sendCANFrame(const CANFrame*)), connectionWindow, SLOT(sendFrame(const CANFrame*))); + //connect(firmwareUploaderWindow, SIGNAL(sendCANFrame(const CANFrame*)), connectionWindow, SLOT(sendFrame(const CANFrame*))); //connect(worker, SIGNAL(gotTargettedFrame(int)), firmwareUploaderWindow, SLOT(gotTargettedFrame(int))); } firmwareUploaderWindow->show(); @@ -942,7 +923,6 @@ void MainWindow::showScriptingWindow() if (!scriptingWindow) { scriptingWindow = new ScriptingWindow(model->getListReference()); - connect(scriptingWindow, &ScriptingWindow::sendCANFrame, connectionWindow, &ConnectionWindow::sendFrame); } scriptingWindow->show(); } @@ -995,8 +975,7 @@ void MainWindow::showConnectionSettingsWindow() { if (!connectionWindow) { - connectionWindow = new ConnectionWindow(model); - connect(connectionWindow, SIGNAL(updateConnectionSettings(QString,QString,int,int)), this, SLOT(updateConnectionSettings(QString,QString,int,int))); + connectionWindow = new ConnectionWindow(); } connectionWindow->show(); } diff --git a/mainwindow.ui b/mainwindow.ui index 327ac92..a4850ac 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -103,6 +103,9 @@ Suspend Capturing + + false + @@ -197,7 +200,7 @@ 0 0 800 - 27 + 19 From fb768245b300e6202dcbd8960eddb5fffbe31e52 Mon Sep 17 00:00:00 2001 From: canpadawan Date: Wed, 29 Jun 2016 13:19:36 +0200 Subject: [PATCH 7/7] a couple more bugfix for connectionwindow --- canconnectionmodel.cpp | 12 +++--- connectionwindow.cpp | 97 ++++++++++++++++++------------------------ 2 files changed, 47 insertions(+), 62 deletions(-) diff --git a/canconnectionmodel.cpp b/canconnectionmodel.cpp index 404b3cd..084d2cc 100644 --- a/canconnectionmodel.cpp +++ b/canconnectionmodel.cpp @@ -182,14 +182,12 @@ void CANConnectionModel::refresh(int pIndex) QModelIndex end; if(pIndex>=0) { - begin = createIndex(pIndex, 0); - end = begin; + begin = createIndex(pIndex, 0); + end = createIndex(pIndex, columnCount()-1); } else { - begin = createIndex(0, 0); - begin = createIndex(rowCount()-1, 0); - /*beginResetModel(); - endResetModel();*/ + begin = createIndex(0, 0); + end = createIndex(rowCount()-1, columnCount()-1); } - dataChanged(begin, end); + dataChanged(begin, end, QVector(Qt::DisplayRole)); } diff --git a/connectionwindow.cpp b/connectionwindow.cpp index 6b1fc77..5919e06 100644 --- a/connectionwindow.cpp +++ b/connectionwindow.cpp @@ -21,11 +21,6 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : qRegisterMetaType("const CANFrame *"); qRegisterMetaType *>("const QList *"); - qRegisterMetaTypeStreamOperators>(); - qRegisterMetaTypeStreamOperators>(); - qRegisterMetaTypeStreamOperators(); - qRegisterMetaTypeStreamOperators>(); - connModel = new CANConnectionModel(this); ui->tableConnections->setModel(connModel); @@ -37,8 +32,6 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : ui->tableConnections->setColumnWidth(5, 75); ui->tableConnections->setColumnWidth(6, 75); ui->tableConnections->setColumnWidth(7, 75); - - //int temp = settings.value("Main/DefaultConnectionType", 0).toInt(); ui->ckSingleWire->setChecked(settings.value("Main/SingleWireMode", false).toBool()); ui->cbSpeed->addItem(tr("")); @@ -51,10 +44,7 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : /* load connection configuration */ loadConnections(); -#ifdef Q_OS_LINUX ui->rbSocketCAN->setEnabled(isSocketCanAvailable()); -#endif - #ifdef Q_OS_WIN ui->rbKvaser->setEnabled(true); #endif @@ -171,6 +161,7 @@ void ConnectionWindow::handleOKButton() if (whichRow > -1) { + /* set information for selected connection */ int busId; CANBus bus; bool ret; @@ -243,10 +234,11 @@ void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QMode { ui->btnOK->setText(tr("Create New Connection")); ui->rbGVRET->setChecked(true); - ui->cbPort->setCurrentIndex(0); ui->ckListenOnly->setChecked(false); ui->ckSingleWire->setChecked(false); ui->ckEnabled->setChecked(false); + setSpeed(0); + setPortName(CANCon::GVRET_SERIAL, ""); } else { @@ -259,17 +251,12 @@ void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QMode if(!ret) return; ui->btnOK->setText(tr("Update Connection Settings")); - - setPortName(conn_p->getType(), conn_p->getPort()); - setSpeed(bus.getSpeed()); - ui->ckListenOnly->setChecked(bus.isListenOnly()); ui->ckSingleWire->setChecked(bus.isSingleWire()); ui->ckEnabled->setChecked(bus.isActive()); - /* this won't be called if elements are disabled */ + setSpeed(bus.getSpeed()); + setPortName(conn_p->getType(), conn_p->getPort()); } - - handleConnTypeChanged(); } @@ -287,40 +274,30 @@ void ConnectionWindow::selectSerial() void ConnectionWindow::selectKvaser() { -#ifdef Q_OS_WIN /* set combobox page visible */ ui->stPort->setCurrentWidget(ui->cbPage); -#endif } void ConnectionWindow::selectSocketCan() { -#ifdef Q_OS_LINUX /* set edit text page visible */ ui->stPort->setCurrentWidget(ui->etPage); -#endif } void ConnectionWindow::setSpeed(int speed0) { - bool found = false; - - qDebug() << "Set Speed " << speed0; - for (int i = 0; i < ui->cbSpeed->count(); i++) { if (ui->cbSpeed->itemText(i).toInt() == speed0) { ui->cbSpeed->setCurrentIndex(i); - found = true; + return; } } - if (!found) - { - ui->cbSpeed->addItem(QString::number(speed0)); - ui->cbSpeed->setCurrentIndex(ui->cbSpeed->count() - 1); - } + /* add custom speed */ + ui->cbSpeed->addItem(QString::number(speed0)); + ui->cbSpeed->setCurrentIndex(ui->cbSpeed->count() - 1); } void ConnectionWindow::setPortName(CANCon::type pType, QString pPortName) @@ -328,19 +305,28 @@ void ConnectionWindow::setPortName(CANCon::type pType, QString pPortName) switch(pType) { case CANCon::GVRET_SERIAL: - { - ui->rbGVRET->setChecked(true); - - break; - } + 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: { - ui->rbKvaser->setChecked(true); + int idx = ui->cbPort->findText(pPortName); + if( idx<0 ) idx=0; + ui->cbPort->setCurrentIndex(idx); break; } case CANCon::SOCKETCAN: { - ui->rbSocketCAN->setChecked(true); ui->lePort->setText(pPortName); break; } @@ -349,24 +335,18 @@ 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: - return -1; - break; - case 0: - return -1; - break; - case 1: - return 0; - break; - default: - return (ui->cbSpeed->currentText().toInt()); - break; + case -1: + case 0: + return -1; + case 1: + return 0; + default: + return (ui->cbSpeed->currentText().toInt()); } } @@ -424,6 +404,9 @@ void ConnectionWindow::handleRemoveConn() /* stop and delete connection */ conn_p->stop(); delete conn_p; + + /* select first connection in list */ + ui->tableConnections->selectRow(0); } void ConnectionWindow::handleRevert() @@ -436,9 +419,8 @@ bool ConnectionWindow::isSocketCanAvailable() { #ifdef Q_OS_LINUX foreach (const QByteArray &backend, QCanBus::instance()->plugins()) { - if (backend == "socketcan") { + if (backend == "socketcan") return true; - } } #endif return false; @@ -466,6 +448,11 @@ CANConnection* ConnectionWindow::create(CANCon::type pTye, QString pPortName) void ConnectionWindow::loadConnections() { + qRegisterMetaTypeStreamOperators>(); + qRegisterMetaTypeStreamOperators>(); + qRegisterMetaTypeStreamOperators(); + qRegisterMetaTypeStreamOperators>(); + QSettings settings; /* fill connection list */ @@ -496,7 +483,7 @@ void ConnectionWindow::saveConnections() QVector devTypes; QList busses; - /* delete connections */ + /* save connections */ foreach(CANConnection* conn_p, conns) { portNames.append(conn_p->getPort());