diff --git a/canframemodel.cpp b/canframemodel.cpp index 1aab17d..04b3f85 100644 --- a/canframemodel.cpp +++ b/canframemodel.cpp @@ -54,6 +54,38 @@ void CANFrameModel::setOverwriteMode(bool mode) overwriteDups = mode; } +void CANFrameModel::recalcOverwrite() +{ + if (!overwriteDups) return; //no need to do a thing if mode is disabled + + int lastUnique = 0; + bool found; + + beginResetModel(); + for (int i = 1; i < frames.count(); i++) + { + found = false; + for (int j = 0; j <= lastUnique; j++) + { + if (frames[i].ID == frames[j].ID) + { + frames.replace(j, frames[i]); + found = true; + break; + } + } + if (!found) + { + lastUnique++; + frames.replace(lastUnique, frames[i]); + } + } + + while (frames.count() > lastUnique) frames.removeLast(); + + endResetModel(); +} + QVariant CANFrameModel::data(const QModelIndex &index, int role) const { QString tempString; @@ -152,17 +184,41 @@ QVariant CANFrameModel::headerData(int section, Qt::Orientation orientation, void CANFrameModel::addFrame(CANFrame &frame, bool autoRefresh = false) { mutex.lock(); - if (autoRefresh) beginInsertRows(QModelIndex(), frames.count() + 1, frames.count() + 1); frame.timestamp -= timeOffset; - frames.append(frame); - if (autoRefresh) endInsertRows(); + if (!overwriteDups) + { + if (autoRefresh) beginInsertRows(QModelIndex(), frames.count() + 1, frames.count() + 1); + frames.append(frame); + if (autoRefresh) endInsertRows(); + } + else + { + bool found = false; + for (int i = 0; i < frames.count(); i++) + { + if (frames[i].ID == frame.ID) + { + if (autoRefresh) beginResetModel(); + frames.replace(i, frame); + if (autoRefresh) endResetModel(); + found = true; + break; + } + } + if (!found) + { + if (autoRefresh) beginInsertRows(QModelIndex(), frames.count() + 1, frames.count() + 1); + frames.append(frame); + if (autoRefresh) endInsertRows(); + } + } mutex.unlock(); } void CANFrameModel::sendRefresh() { - beginInsertRows(QModelIndex(), 0, frames.count()); - endInsertRows(); + beginResetModel(); + endResetModel(); } void CANFrameModel::sendRefresh(int pos) @@ -182,9 +238,17 @@ void CANFrameModel::sendBulkRefresh(int num) //otherwise it's possible that the grid is in an odd state. if (num == 0) return; if (frames.count() == 0) return; - if (num > frames.count()) num = frames.count(); - beginInsertRows(QModelIndex(), frames.count() - num, frames.count() - 1); - endInsertRows(); + + if (!overwriteDups) + { + if (num > frames.count()) num = frames.count(); + beginInsertRows(QModelIndex(), frames.count() - num, frames.count() - 1); + endInsertRows(); + } + else + { + sendRefresh(); + } } void CANFrameModel::clearFrames() @@ -199,6 +263,9 @@ void CANFrameModel::clearFrames() //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 //but, it's for a good cause. +//Implement proper "const"ness for this function. No one should be +//adding frames via this reference. Unfortunately, I've done just that in +//places like the file loading code. QVector* CANFrameModel::getListReference() { return &frames; diff --git a/canframemodel.h b/canframemodel.h index fffb73f..2fab8ab 100644 --- a/canframemodel.h +++ b/canframemodel.h @@ -30,6 +30,7 @@ public: void setInterpetMode(bool); void setOverwriteMode(bool); void normalizeTiming(); + void recalcOverwrite(); QVector *getListReference(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 65f6569..2028a45 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -317,6 +317,7 @@ void MainWindow::handleLoadFile() QStringList fileList = filename.split('/'); loadedFileName = fileList[fileList.length() - 1]; + model->recalcOverwrite(); model->sendRefresh(); ui->lbNumFrames->setText(QString::number(model->rowCount())); if (ui->cbAutoScroll->isChecked()) ui->canFramesView->scrollToBottom();