Skip to content

Commit

Permalink
Merge pull request collin80#296 from bigoulours/feature/busfilter
Browse files Browse the repository at this point in the history
Feature/busfilter
  • Loading branch information
collin80 authored Feb 24, 2021
2 parents 4374798 + 85ed779 commit 52ff410
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#Build directory
build/

# Compiled Object files
*.slo
*.lo
Expand Down
51 changes: 47 additions & 4 deletions canframemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CANFrameModel::~CANFrameModel()
frames.clear();
filteredFrames.clear();
filters.clear();
busFilters.clear();
}

int CANFrameModel::rowCount(const QModelIndex &parent) const
Expand Down Expand Up @@ -171,6 +172,13 @@ void CANFrameModel::setFilterState(unsigned int ID, bool state)
sendRefresh();
}

void CANFrameModel::setBusFilterState(unsigned int BusID, bool state)
{
if (!busFilters.contains(BusID)) return;
busFilters[BusID] = state;
sendRefresh();
}

void CANFrameModel::setAllFilters(bool state)
{
QMap<int, bool>::iterator it;
Expand Down Expand Up @@ -337,7 +345,7 @@ void CANFrameModel::recalcOverwrite()

for (int i = 0; i < frames.count(); i++)
{
if (filters[frames[i].frameId()])
if (filters[frames[i].frameId()] && busFilters[frames[i].bus])
{
filteredFrames.append(frames[i]);
}
Expand Down Expand Up @@ -578,6 +586,18 @@ bool CANFrameModel::any_filters_are_configured(void)
return false;
}

bool CANFrameModel::any_busfilters_are_configured(void)
{
for (auto const &val : busFilters)
{
if (val == true)
continue;
else
return true;
}
return false;
}


void CANFrameModel::addFrame(const CANFrame& frame, bool autoRefresh = false)
{
Expand All @@ -601,10 +621,21 @@ void CANFrameModel::addFrame(const CANFrame& frame, bool autoRefresh = false)
needFilterRefresh = true;
}

//if this BusID isn't found in the busFilters list then add it and show it by default
if (!busFilters.contains(tempFrame.bus))
{
// if there are any busFilters already configured, leave the new filter disabled
if (any_busfilters_are_configured())
busFilters.insert(tempFrame.bus, false);
else
busFilters.insert(tempFrame.bus, true);
needFilterRefresh = true;
}

if (!overwriteDups)
{
frames.append(tempFrame);
if (filters[tempFrame.frameId()])
if (filters[tempFrame.frameId()] && busFilters[tempFrame.bus])
{
if (autoRefresh) beginInsertRows(QModelIndex(), filteredFrames.count(), filteredFrames.count());
tempFrame.frameCount = 1;
Expand All @@ -629,7 +660,7 @@ void CANFrameModel::addFrame(const CANFrame& frame, bool autoRefresh = false)
if (!found)
{
frames.append(tempFrame);
if (filters[tempFrame.frameId()])
if (filters[tempFrame.frameId()] && busFilters[tempFrame.bus])
{
if (autoRefresh) beginInsertRows(QModelIndex(), filteredFrames.count(), filteredFrames.count());
tempFrame.frameCount = 1;
Expand Down Expand Up @@ -676,7 +707,7 @@ void CANFrameModel::sendRefresh()
int count = frames.count();
for (int i = 0; i < count; i++)
{
if (filters[frames[i].frameId()])
if (filters[frames[i].frameId()] && busFilters[frames[i].bus])
{
tempContainer.append(frames[i]);
}
Expand Down Expand Up @@ -727,6 +758,7 @@ void CANFrameModel::clearFrames()
frames.clear();
filteredFrames.clear();
filters.clear();
busFilters.clear();
frames.reserve(preallocSize);
filteredFrames.reserve(preallocSize);
this->endResetModel();
Expand Down Expand Up @@ -758,6 +790,11 @@ void CANFrameModel::insertFrames(const QVector<CANFrame> &newFrames)
needFilterRefresh = true;
}
if (filters[newFrames[i].frameId()])
{
busFilters.insert(newFrames[i].bus, true);
needFilterRefresh = true;
}
if (filters[newFrames[i].frameId()] && busFilters[newFrames[i].bus])
{
insertedFiltered++;
filteredFrames.append(newFrames[i]);
Expand Down Expand Up @@ -796,6 +833,7 @@ void CANFrameModel::loadFilterFile(QString filename)
return;

filters.clear();
busFilters.clear();

while (!inFile->atEnd()) {
line = inFile->readLine().simplified();
Expand Down Expand Up @@ -861,3 +899,8 @@ const QMap<int, bool>* CANFrameModel::getFiltersReference() const
{
return &filters;
}

const QMap<int, bool>* CANFrameModel::getBusFiltersReference() const
{
return &busFilters;
}
4 changes: 4 additions & 0 deletions canframemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CANFrameModel: public QAbstractTableModel
void setHexMode(bool);
void setSysTimeMode(bool);
void setFilterState(unsigned int ID, bool state);
void setBusFilterState(unsigned int BusID, bool state);
void setAllFilters(bool state);
void setSecondsMode(bool);
void setTimeFormat(QString);
Expand All @@ -62,6 +63,7 @@ class CANFrameModel: public QAbstractTableModel
const QVector<CANFrame> *getListReference() const; //thou shalt not modify these frames externally!
const QVector<CANFrame> *getFilteredListReference() const; //Thus saith the Lord, NO.
const QMap<int, bool> *getFiltersReference() const; //this neither
const QMap<int, bool> *getBusFiltersReference() const; //this neither

public slots:
void addFrame(const CANFrame&, bool);
Expand All @@ -75,10 +77,12 @@ public slots:
void qSortCANFrameDesc(QVector<CANFrame>* frames, Column column, int lowerBound, int upperBound);
uint64_t getCANFrameVal(int row, Column col);
bool any_filters_are_configured(void);
bool any_busfilters_are_configured(void);

QVector<CANFrame> frames;
QVector<CANFrame> filteredFrames;
QMap<int, bool> filters;
QMap<int, bool> busFilters;
DBCHandler *dbcHandler;
QMutex mutex;
bool interpretFrames; //should we use the dbcHandler?
Expand Down
39 changes: 37 additions & 2 deletions filterutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ uint32_t FilterUtility::getGMLanSenderId(int32_t id)
return id & 0x1FFF;
}

QListWidgetItem * FilterUtility::createCheckableFilterItem(int32_t id, bool checked, QListWidget* parent)
QListWidgetItem * FilterUtility::createCheckableFilterItem(uint32_t id, bool checked, QListWidget* parent)
{
QListWidgetItem * thisItem = createFilterItem(id,parent);
thisItem->setFlags(thisItem->flags() | Qt::ItemIsUserCheckable);
Expand All @@ -48,7 +48,19 @@ QListWidgetItem * FilterUtility::createCheckableFilterItem(int32_t id, bool chec
return thisItem;
}

QListWidgetItem * FilterUtility::createFilterItem(int32_t id, QListWidget* parent)
QListWidgetItem * FilterUtility::createCheckableBusFilterItem(uint32_t id, bool checked, QListWidget* parent)
{
QListWidgetItem * thisItem = createBusFilterItem(id,parent);
thisItem->setFlags(thisItem->flags() | Qt::ItemIsUserCheckable);
if (checked)
thisItem->setCheckState(Qt::Checked);
else
thisItem->setCheckState(Qt::Unchecked);
return thisItem;
}


QListWidgetItem * FilterUtility::createFilterItem(uint32_t id, QListWidget* parent)
{
QSettings settings;
DBCHandler * dbcHandler = DBCHandler::getReference();
Expand Down Expand Up @@ -81,3 +93,26 @@ QListWidgetItem * FilterUtility::createFilterItem(int32_t id, QListWidget* paren
thisItem->setText(filterItemName);
return thisItem;
}

QListWidgetItem * FilterUtility::createBusFilterItem(uint32_t id, QListWidget* parent)
{
QSettings settings;
DBCHandler * dbcHandler = DBCHandler::getReference();
QListWidgetItem *thisItem = new QListWidgetItem(parent);
QString filterItemName = QStringLiteral("%1").arg(id);

if (settings.value("Main/FilterLabeling", false).toBool())
{
// Filter labeling (show interpreted frame names next to the CAN addr ID)
MatchingCriteria_t matchingCriteria;
DBC_MESSAGE *msg = dbcHandler->findMessageForFilter(id,&matchingCriteria);
if (msg != NULL)
{
filterItemName.append(" ");
filterItemName.append(msg->name);
}
}

thisItem->setText(filterItemName);
return thisItem;
}
6 changes: 4 additions & 2 deletions filterutility.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ class FilterUtility
{

public:
static QListWidgetItem * createFilterItem(int32_t id, QListWidget* parent=NULL); // if parent is given, add item automatically to listwidget
static QListWidgetItem * createCheckableFilterItem(int32_t id, bool checked, QListWidget* parent=NULL);
static QListWidgetItem * createFilterItem(uint32_t id, QListWidget* parent=NULL); // if parent is given, add item automatically to listwidget
static QListWidgetItem * createCheckableFilterItem(uint32_t id, bool checked, QListWidget* parent=NULL);
static QListWidgetItem * createBusFilterItem(uint32_t id, QListWidget* parent=NULL); // if parent is given, add item automatically to listwidget
static QListWidgetItem * createCheckableBusFilterItem(uint32_t id, bool checked, QListWidget* parent=NULL);

static uint32_t getIdAsInt( QListWidgetItem * item );
static QString getId( QListWidgetItem * item );
Expand Down
25 changes: 24 additions & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->actionPreferences, &QAction::triggered, this, &MainWindow::showSettingsDialog);
connect(model, &CANFrameModel::updatedFiltersList, this, &MainWindow::updateFilterList);
connect(ui->listFilters, &QListWidget::itemChanged, this, &MainWindow::filterListItemChanged);
connect(ui->listBusFilters, &QListWidget::itemChanged, this, &MainWindow::busFilterListItemChanged);
connect(ui->btnFilterAll, &QAbstractButton::clicked, this, &MainWindow::filterSetAll);
connect(ui->btnFilterNone, &QAbstractButton::clicked, this, &MainWindow::filterClearAll);
connect(ui->actionFirmware_Update, &QAction::triggered, this, &MainWindow::showFirmwareUploaderWindow);
Expand Down Expand Up @@ -503,13 +504,15 @@ void MainWindow::updateFilterList()
{
if (model == nullptr) return;
const QMap<int, bool> *filters = model->getFiltersReference();
if (filters == nullptr) return;
const QMap<int, bool> *busFilters = model->getBusFiltersReference();
if (filters == nullptr && busFilters == nullptr) return;

qDebug() << "updateFilterList called on MainWindow";

inhibitFilterUpdate = true;

ui->listFilters->clear();
ui->listBusFilters->clear();

if (filters->isEmpty()) return;

Expand All @@ -518,6 +521,13 @@ void MainWindow::updateFilterList()
{
/*QListWidgetItem *thisItem = */FilterUtility::createCheckableFilterItem(filterIter.key(), filterIter.value(), ui->listFilters);
}

if (busFilters->isEmpty()) return;

for (filterIter = busFilters->begin(); filterIter != busFilters->end(); ++filterIter)
{
QListWidgetItem *thisItem = FilterUtility::createCheckableBusFilterItem(filterIter.key(), filterIter.value(), ui->listBusFilters);
}
inhibitFilterUpdate = false;
}

Expand All @@ -534,6 +544,19 @@ void MainWindow::filterListItemChanged(QListWidgetItem *item)
model->setFilterState(ID, isSet);
}

void MainWindow::busFilterListItemChanged(QListWidgetItem *item)
{
if (inhibitFilterUpdate) return;
//qDebug() << item->text();

// strip away possible filter label
int ID = FilterUtility::getIdAsInt(item);
bool isSet = false;
if (item->checkState() == Qt::Checked) isSet = true;

model->setBusFilterState(ID, isSet);
}

void MainWindow::filterSetAll()
{
inhibitFilterUpdate = true;
Expand Down
2 changes: 2 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ private slots:
void gridDoubleClicked(QModelIndex);
void interpretToggled(bool);
void overwriteToggled(bool);
//void logReceivedFrame(CANConnection*, QVector<CANFrame>);
void tickGUIUpdate();
void toggleCapture();
void normalizeTiming();
void updateFilterList();
void filterListItemChanged(QListWidgetItem *item);
void busFilterListItemChanged(QListWidgetItem *item);
void filterSetAll();
void filterClearAll();
void headerClicked (int logicalIndex);
Expand Down
Loading

0 comments on commit 52ff410

Please sign in to comment.