From 46a25cc1e56c30504aed4661de96a51b443a46b6 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Mon, 29 Jun 2015 22:50:53 -0400 Subject: [PATCH] Saving of filtered frames now works. Also, saving and loading of main filter list is now implemented. --- canframemodel.cpp | 54 ++++++++++++++++++++++++++++++++ canframemodel.h | 3 ++ mainwindow.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++ mainwindow.h | 3 ++ 4 files changed, 140 insertions(+) diff --git a/canframemodel.cpp b/canframemodel.cpp index 703b256..5bdf212 100644 --- a/canframemodel.cpp +++ b/canframemodel.cpp @@ -1,5 +1,7 @@ #include "canframemodel.h" +#include + int CANFrameModel::rowCount(const QModelIndex &parent) const { return filteredFrames.count(); @@ -380,6 +382,53 @@ void CANFrameModel::insertFrames(const QVector &newFrames) endInsertRows(); } +void CANFrameModel::loadFilterFile(QString filename) +{ + QFile *inFile = new QFile(filename); + QByteArray line; + int ID; + + if (!inFile->open(QIODevice::ReadOnly | QIODevice::Text)) + return; + + filters.clear(); + + while (!inFile->atEnd()) { + line = inFile->readLine().simplified(); + if (line.length() > 2) + { + QList tokens = line.split(','); + ID = tokens[0].toInt(NULL, 16); + if (tokens[1].toUpper() == "T") filters.insert(ID, true); + else filters.insert(ID, false); + } + } + inFile->close(); + + sendRefresh(); + + emit updatedFiltersList(); +} + +void CANFrameModel::saveFilterFile(QString filename) +{ + QFile *outFile = new QFile(filename); + + if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text)) + return; + + QMap::const_iterator it; + for (it = filters.begin(); it != filters.end(); ++it) + { + outFile->write(QString::number(it.key(), 16).toUtf8()); + outFile->putChar(','); + if (it.value()) outFile->putChar('T'); + else outFile->putChar('F'); + outFile->write("\n"); + } + outFile->close(); +} + /* *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. @@ -392,6 +441,11 @@ const QVector* CANFrameModel::getListReference() const return &frames; } +const QVector* CANFrameModel::getFilteredListReference() const +{ + return &filteredFrames; +} + const QMap* CANFrameModel::getFiltersReference() const { return &filters; diff --git a/canframemodel.h b/canframemodel.h index 9a5e723..dbcd588 100644 --- a/canframemodel.h +++ b/canframemodel.h @@ -34,10 +34,13 @@ public: void setHexMode(bool); void setFilterState(int ID, bool state); void setSecondsMode(bool); + void loadFilterFile(QString filename); + void saveFilterFile(QString filename); void normalizeTiming(); void recalcOverwrite(); void insertFrames(const QVector &newFrames); const QVector *getListReference() const; //thou shalt not modify these frames externally! + const QVector *getFilteredListReference() const; //Thus saith the Lord, NO. const QMap *getFiltersReference() const; //this neither signals: diff --git a/mainwindow.cpp b/mainwindow.cpp index 8e36c89..32cf3d4 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -123,6 +123,9 @@ MainWindow::MainWindow(QWidget *parent) : connect(ui->actionFrame_Data_Analysis, SIGNAL(triggered(bool)), this, SLOT(showFrameDataAnalysis())); connect(ui->btnClearFrames, SIGNAL(clicked(bool)), this, SLOT(clearFrames())); connect(ui->actionSave_Log_File, SIGNAL(triggered(bool)), this, SLOT(handleSaveFile())); + connect(ui->actionSave_Filtered_Log_File, SIGNAL(triggered(bool)), this, SLOT(handleSaveFilteredFile())); + connect(ui->actionLoad_Filter_Definition, SIGNAL(triggered(bool)), this, SLOT(handleLoadFilters())); + connect(ui->actionSave_Filter_Definition, SIGNAL(triggered(bool)), this, SLOT(handleSaveFilters())); connect(ui->action_Playback, SIGNAL(triggered(bool)), this, SLOT(showPlaybackWindow())); connect(ui->btnBaudSet, SIGNAL(clicked(bool)), this, SLOT(changeBaudRates())); connect(ui->actionFlow_View, SIGNAL(triggered(bool)), this, SLOT(showFlowViewWindow())); @@ -451,6 +454,83 @@ void MainWindow::handleSaveFile() } } +void MainWindow::handleSaveFilteredFile() +{ + QString filename; + QFileDialog dialog(this); + bool result = false; + + QStringList filters; + filters.append(QString(tr("CRTD Logs (*.txt)"))); + filters.append(QString(tr("GVRET Logs (*.csv)"))); + filters.append(QString(tr("Generic ID/Data CSV (*.csv)"))); + filters.append(QString(tr("BusMaster Log (*.log)"))); + filters.append(QString(tr("Microchip Log (*.log)"))); + + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilters(filters); + dialog.setViewMode(QFileDialog::Detail); + dialog.setAcceptMode(QFileDialog::AcceptSave); + + if (dialog.exec() == QDialog::Accepted) + { + const QVector *frames = model->getFilteredListReference(); + filename = dialog.selectedFiles()[0]; + if (dialog.selectedNameFilter() == filters[0]) result = FrameFileIO::saveCRTDFile(filename, frames); + if (dialog.selectedNameFilter() == filters[1]) result = FrameFileIO::saveNativeCSVFile(filename, frames); + if (dialog.selectedNameFilter() == filters[2]) result = FrameFileIO::saveGenericCSVFile(filename, frames); + if (dialog.selectedNameFilter() == filters[3]) result = FrameFileIO::saveLogFile(filename, frames); + if (dialog.selectedNameFilter() == filters[4]) result = FrameFileIO::saveMicrochipFile(filename, frames); + if (result) + { + QStringList fileList = filename.split('/'); + loadedFileName = fileList[fileList.length() - 1]; + updateFileStatus(); + } + } +} + +void MainWindow::handleSaveFilters() +{ + QString filename; + QFileDialog dialog(this); + bool result = false; + + QStringList filters; + filters.append(QString(tr("Filter list (*.ftl)"))); + + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilters(filters); + dialog.setViewMode(QFileDialog::Detail); + dialog.setAcceptMode(QFileDialog::AcceptSave); + + if (dialog.exec() == QDialog::Accepted) + { + filename = dialog.selectedFiles()[0]; + if (dialog.selectedNameFilter() == filters[0]) model->saveFilterFile(filename); + } +} + +void MainWindow::handleLoadFilters() +{ + QString filename; + QFileDialog dialog(this); + + QStringList filters; + filters.append(QString(tr("Filter List (*.ftl)"))); + + dialog.setFileMode(QFileDialog::ExistingFile); + dialog.setNameFilters(filters); + dialog.setViewMode(QFileDialog::Detail); + + if (dialog.exec() == QDialog::Accepted) + { + filename = dialog.selectedFiles()[0]; + //right now there is only one file type that can be loaded here so just do it. + model->loadFilterFile(filename); + } +} + void MainWindow::handleLoadDBC() { QString filename; diff --git a/mainwindow.h b/mainwindow.h index d6807d3..397171d 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -35,8 +35,11 @@ public: private slots: void handleLoadFile(); void handleSaveFile(); + void handleSaveFilteredFile(); void handleLoadDBC(); void handleSaveDBC(); + void handleSaveFilters(); + void handleLoadFilters(); void connButtonPress(); void showGraphingWindow(); void showFrameDataAnalysis();