Fix to make access of canbus frames const correct. It's now not really possible to modify the captured canbus frames from outside the model class that holds them. This is safer and more future proof.

Boring update but it might save head scratching in the future.
This commit is contained in:
Collin Kidder
2015-06-24 20:44:38 -04:00
parent c14f57c98f
commit 1af5c10506
16 changed files with 64 additions and 44 deletions
+23 -7
View File
@@ -260,13 +260,29 @@ void CANFrameModel::clearFrames()
mutex.unlock(); mutex.unlock();
} }
//Is this safe? Maybe not but if we don't change it then that's OK /*
//Is it the best C++ practice? Probably not. This breaks the MVC paradigm * Since the getListReference function returns readonly
//but, it's for a good cause. * you can't insert frames with it. Instead this function
//Implement proper "const"ness for this function. No one should be * allows for a mass import of frames into the model
//adding frames via this reference. Unfortunately, I've done just that in */
//places like the file loading code. void CANFrameModel::insertFrames(const QVector<CANFrame> &newFrames)
QVector<CANFrame>* CANFrameModel::getListReference() {
beginInsertRows(QModelIndex(), frames.count() + 1, frames.count() + newFrames.count());
for (int i = 0; i < newFrames.count(); i++)
{
frames.append(newFrames[i]);
}
endInsertRows();
}
/*
*This used to not be const correct but it is now. So, there's little harm in
* allowing external code to peek at our frames. There's just no touching.
* This ability to get a direct read-only reference speeds up a variety of
* external code that needs to access frames directly and doesn't care about
* this model's normal output mechanism.
*/
const QVector<CANFrame>* CANFrameModel::getListReference() const
{ {
return &frames; return &frames;
} }
+2 -1
View File
@@ -31,7 +31,8 @@ public:
void setOverwriteMode(bool); void setOverwriteMode(bool);
void normalizeTiming(); void normalizeTiming();
void recalcOverwrite(); void recalcOverwrite();
QVector<CANFrame> *getListReference(); void insertFrames(const QVector<CANFrame> &newFrames);
const QVector<CANFrame> *getListReference() const; //thou shalt not modify these frames externally!
private: private:
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef CONFIG #ifndef CONFIG
#define CONFIG #define CONFIG
#define VERSION 114 #define VERSION 115
#endif // CONFIG #endif // CONFIG
+1 -1
View File
@@ -6,7 +6,7 @@ const QColor FlowViewWindow::graphColors[8] = {Qt::blue, Qt::green, Qt::black, Q
Qt::gray, Qt::yellow, Qt::cyan, Qt::darkMagenta}; //4 5 6 7 Qt::gray, Qt::yellow, Qt::cyan, Qt::darkMagenta}; //4 5 6 7
FlowViewWindow::FlowViewWindow(QVector<CANFrame> *frames, QWidget *parent) : FlowViewWindow::FlowViewWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::FlowViewWindow) ui(new Ui::FlowViewWindow)
{ {
+2 -2
View File
@@ -13,7 +13,7 @@ class FlowViewWindow : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit FlowViewWindow(QVector<CANFrame> *frames, QWidget *parent = 0); explicit FlowViewWindow(const QVector<CANFrame> *frames, QWidget *parent = 0);
~FlowViewWindow(); ~FlowViewWindow();
void showEvent(QShowEvent*); void showEvent(QShowEvent*);
@@ -38,7 +38,7 @@ private:
Ui::FlowViewWindow *ui; Ui::FlowViewWindow *ui;
QList<int> foundID; QList<int> foundID;
QList<CANFrame> frameCache; QList<CANFrame> frameCache;
QVector<CANFrame> *modelFrames; const QVector<CANFrame> *modelFrames;
unsigned char refBytes[8]; unsigned char refBytes[8];
unsigned char currBytes[8]; unsigned char currBytes[8];
int currentPosition; int currentPosition;
+5 -5
View File
@@ -117,7 +117,7 @@ bool FrameFileIO::loadCRTDFile(QString filename, QVector<CANFrame>* frames)
return true; return true;
} }
bool FrameFileIO::saveCRTDFile(QString filename, QVector<CANFrame>* frames) bool FrameFileIO::saveCRTDFile(QString filename, const QVector<CANFrame>* frames)
{ {
QFile *outFile = new QFile(filename); QFile *outFile = new QFile(filename);
@@ -197,7 +197,7 @@ bool FrameFileIO::loadNativeCSVFile(QString filename, QVector<CANFrame>* frames)
return true; return true;
} }
bool FrameFileIO::saveNativeCSVFile(QString filename, QVector<CANFrame>* frames) bool FrameFileIO::saveNativeCSVFile(QString filename, const QVector<CANFrame>* frames)
{ {
QFile *outFile = new QFile(filename); QFile *outFile = new QFile(filename);
@@ -273,7 +273,7 @@ bool FrameFileIO::loadGenericCSVFile(QString filename, QVector<CANFrame>* frames
return true; return true;
} }
bool FrameFileIO::saveGenericCSVFile(QString filename, QVector<CANFrame>* frames) bool FrameFileIO::saveGenericCSVFile(QString filename, const QVector<CANFrame>* frames)
{ {
return false; return false;
} }
@@ -345,7 +345,7 @@ bool FrameFileIO::loadLogFile(QString filename, QVector<CANFrame>* frames)
return true; return true;
} }
bool FrameFileIO::saveLogFile(QString filename, QVector<CANFrame>* frames) bool FrameFileIO::saveLogFile(QString filename, const QVector<CANFrame>* frames)
{ {
return false; return false;
} }
@@ -403,7 +403,7 @@ bool FrameFileIO::loadMicrochipFile(QString filename, QVector<CANFrame>* frames)
return true; return true;
} }
bool FrameFileIO::saveMicrochipFile(QString filename, QVector<CANFrame>* frames) bool FrameFileIO::saveMicrochipFile(QString filename, const QVector<CANFrame>* frames)
{ {
return false; return false;
} }
+5 -5
View File
@@ -26,11 +26,11 @@ public:
static bool loadGenericCSVFile(QString, QVector<CANFrame>*); static bool loadGenericCSVFile(QString, QVector<CANFrame>*);
static bool loadLogFile(QString, QVector<CANFrame>*); static bool loadLogFile(QString, QVector<CANFrame>*);
static bool loadMicrochipFile(QString, QVector<CANFrame>*); static bool loadMicrochipFile(QString, QVector<CANFrame>*);
static bool saveCRTDFile(QString, QVector<CANFrame>*); static bool saveCRTDFile(QString, const QVector<CANFrame>*);
static bool saveNativeCSVFile(QString, QVector<CANFrame>*); static bool saveNativeCSVFile(QString, const QVector<CANFrame>*);
static bool saveGenericCSVFile(QString, QVector<CANFrame>*); static bool saveGenericCSVFile(QString, const QVector<CANFrame>*);
static bool saveLogFile(QString, QVector<CANFrame>*); static bool saveLogFile(QString, const QVector<CANFrame>*);
static bool saveMicrochipFile(QString, QVector<CANFrame>*); static bool saveMicrochipFile(QString, const QVector<CANFrame>*);
static QString loadFrameFile(QVector<CANFrame>*); static QString loadFrameFile(QVector<CANFrame>*);
}; };
+1 -1
View File
@@ -3,7 +3,7 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <QtDebug> #include <QtDebug>
FrameInfoWindow::FrameInfoWindow(QVector<CANFrame> *frames, QWidget *parent) : FrameInfoWindow::FrameInfoWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::FrameInfoWindow) ui(new Ui::FrameInfoWindow)
{ {
+2 -2
View File
@@ -14,7 +14,7 @@ class FrameInfoWindow : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit FrameInfoWindow(QVector<CANFrame> *frames, QWidget *parent = 0); explicit FrameInfoWindow(const QVector<CANFrame> *frames, QWidget *parent = 0);
~FrameInfoWindow(); ~FrameInfoWindow();
void showEvent(QShowEvent*); void showEvent(QShowEvent*);
@@ -27,7 +27,7 @@ private:
QList<int> foundID; QList<int> foundID;
QList<CANFrame> frameCache; QList<CANFrame> frameCache;
QVector<CANFrame> *modelFrames; const QVector<CANFrame> *modelFrames;
void refreshIDList(); void refreshIDList();
}; };
+1 -1
View File
@@ -16,7 +16,7 @@
* *
*/ */
FramePlaybackWindow::FramePlaybackWindow(QVector<CANFrame> *frames, SerialWorker *worker, QWidget *parent) : FramePlaybackWindow::FramePlaybackWindow(const QVector<CANFrame> *frames, SerialWorker *worker, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::FramePlaybackWindow) ui(new Ui::FramePlaybackWindow)
{ {
+2 -2
View File
@@ -27,7 +27,7 @@ class FramePlaybackWindow : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit FramePlaybackWindow(QVector<CANFrame> *frames, SerialWorker *worker, QWidget *parent = 0); explicit FramePlaybackWindow(const QVector<CANFrame> *frames, SerialWorker *worker, QWidget *parent = 0);
~FramePlaybackWindow(); ~FramePlaybackWindow();
private slots: private slots:
@@ -57,7 +57,7 @@ private:
Ui::FramePlaybackWindow *ui; Ui::FramePlaybackWindow *ui;
QList<int> foundID; QList<int> foundID;
QList<CANFrame> frameCache; QList<CANFrame> frameCache;
QVector<CANFrame> *modelFrames; const QVector<CANFrame> *modelFrames;
int currentPosition; int currentPosition;
QTimer *playbackTimer; QTimer *playbackTimer;
SerialWorker *serialWorker; SerialWorker *serialWorker;
+1 -1
View File
@@ -2,7 +2,7 @@
#include "ui_framesenderwindow.h" #include "ui_framesenderwindow.h"
#include "utility.h" #include "utility.h"
FrameSenderWindow::FrameSenderWindow(QVector<CANFrame> *frames, QWidget *parent) : FrameSenderWindow::FrameSenderWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::FrameSenderWindow) ui(new Ui::FrameSenderWindow)
{ {
+2 -2
View File
@@ -15,7 +15,7 @@ class FrameSenderWindow : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit FrameSenderWindow(QVector<CANFrame> *frames, QWidget *parent = 0); explicit FrameSenderWindow(const QVector<CANFrame> *frames, QWidget *parent = 0);
~FrameSenderWindow(); ~FrameSenderWindow();
private slots: private slots:
@@ -26,7 +26,7 @@ private:
Ui::FrameSenderWindow *ui; Ui::FrameSenderWindow *ui;
QList<FrameSendData> sendingData; QList<FrameSendData> sendingData;
QList<CANFrame> frameCache; QList<CANFrame> frameCache;
QVector<CANFrame> *modelFrames; const QVector<CANFrame> *modelFrames;
QTimer *intervalTimer; QTimer *intervalTimer;
void doModifiers(int); void doModifiers(int);
+3 -3
View File
@@ -4,7 +4,7 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <QDebug> #include <QDebug>
GraphingWindow::GraphingWindow(QVector<CANFrame> *frames, QWidget *parent) : GraphingWindow::GraphingWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::GraphingWindow) ui(new Ui::GraphingWindow)
{ {
@@ -344,8 +344,8 @@ void GraphingWindow::addNewGraph()
void GraphingWindow::createGraph(GraphParams &params, bool createGraphParam) void GraphingWindow::createGraph(GraphParams &params, bool createGraphParam)
{ {
int tempVal; int tempVal;
float yminval=10000000, ymaxval = -1000000; float yminval=10000000.0, ymaxval = -1000000.0;
float xminval=100000000000, xmaxval = -100000000000; float xminval=10000000000.0, xmaxval = -10000000000.0;
qDebug() << "New Graph ID: " << params.ID; qDebug() << "New Graph ID: " << params.ID;
qDebug() << "Start byte: " << params.startByte; qDebug() << "Start byte: " << params.startByte;
+2 -2
View File
@@ -29,7 +29,7 @@ class GraphingWindow : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit GraphingWindow(QVector<CANFrame> *, QWidget *parent = 0); explicit GraphingWindow(const QVector<CANFrame> *, QWidget *parent = 0);
~GraphingWindow(); ~GraphingWindow();
void showEvent(QShowEvent*); void showEvent(QShowEvent*);
@@ -53,7 +53,7 @@ private slots:
private: private:
Ui::GraphingWindow *ui; Ui::GraphingWindow *ui;
QList<CANFrame> frameCache; QList<CANFrame> frameCache;
QVector<CANFrame> *modelFrames; const QVector<CANFrame> *modelFrames;
QList<GraphParams> graphParams; QList<GraphParams> graphParams;
QPen selectedPen; QPen selectedPen;
bool needScaleSetup; //do we need to set x,y graphing extents?s bool needScaleSetup; //do we need to set x,y graphing extents?s
+11 -8
View File
@@ -307,18 +307,21 @@ void MainWindow::handleLoadFile()
ui->canFramesView->scrollToTop(); ui->canFramesView->scrollToTop();
model->clearFrames(); model->clearFrames();
if (dialog.selectedNameFilter() == filters[0]) result = FrameFileIO::loadCRTDFile(filename, model->getListReference()); QVector<CANFrame> tempFrames;
if (dialog.selectedNameFilter() == filters[1]) result = FrameFileIO::loadNativeCSVFile(filename, model->getListReference());
if (dialog.selectedNameFilter() == filters[2]) result = FrameFileIO::loadGenericCSVFile(filename, model->getListReference()); if (dialog.selectedNameFilter() == filters[0]) result = FrameFileIO::loadCRTDFile(filename, &tempFrames);
if (dialog.selectedNameFilter() == filters[3]) result = FrameFileIO::loadLogFile(filename, model->getListReference()); if (dialog.selectedNameFilter() == filters[1]) result = FrameFileIO::loadNativeCSVFile(filename, &tempFrames);
if (dialog.selectedNameFilter() == filters[4]) result = FrameFileIO::loadMicrochipFile(filename, model->getListReference()); if (dialog.selectedNameFilter() == filters[2]) result = FrameFileIO::loadGenericCSVFile(filename, &tempFrames);
if (dialog.selectedNameFilter() == filters[3]) result = FrameFileIO::loadLogFile(filename, &tempFrames);
if (dialog.selectedNameFilter() == filters[4]) result = FrameFileIO::loadMicrochipFile(filename, &tempFrames);
if (result) if (result)
{ {
model->insertFrames(tempFrames);
QStringList fileList = filename.split('/'); QStringList fileList = filename.split('/');
loadedFileName = fileList[fileList.length() - 1]; loadedFileName = fileList[fileList.length() - 1];
model->recalcOverwrite(); model->recalcOverwrite();
model->sendRefresh();
ui->lbNumFrames->setText(QString::number(model->rowCount())); ui->lbNumFrames->setText(QString::number(model->rowCount()));
if (ui->cbAutoScroll->isChecked()) ui->canFramesView->scrollToBottom(); if (ui->cbAutoScroll->isChecked()) ui->canFramesView->scrollToBottom();
@@ -348,7 +351,7 @@ void MainWindow::handleSaveFile()
if (dialog.exec() == QDialog::Accepted) if (dialog.exec() == QDialog::Accepted)
{ {
QVector<CANFrame> *frames = model->getListReference(); const QVector<CANFrame> *frames = model->getListReference();
filename = dialog.selectedFiles()[0]; filename = dialog.selectedFiles()[0];
if (dialog.selectedNameFilter() == filters[0]) result = FrameFileIO::saveCRTDFile(filename, frames); if (dialog.selectedNameFilter() == filters[0]) result = FrameFileIO::saveCRTDFile(filename, frames);
if (dialog.selectedNameFilter() == filters[1]) result = FrameFileIO::saveNativeCSVFile(filename, frames); if (dialog.selectedNameFilter() == filters[1]) result = FrameFileIO::saveNativeCSVFile(filename, frames);
@@ -433,7 +436,7 @@ void MainWindow::handleSaveDecoded()
void MainWindow::saveDecodedTextFile(QString filename) void MainWindow::saveDecodedTextFile(QString filename)
{ {
QFile *outFile = new QFile(filename); QFile *outFile = new QFile(filename);
QVector<CANFrame> *frames = model->getListReference(); const QVector<CANFrame> *frames = model->getListReference();
if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text)) if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text))
return; return;