Implemented overwrite mode
This commit is contained in:
+70
-3
@@ -54,6 +54,38 @@ void CANFrameModel::setOverwriteMode(bool mode)
|
|||||||
overwriteDups = 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
|
QVariant CANFrameModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
QString tempString;
|
QString tempString;
|
||||||
@@ -152,17 +184,41 @@ QVariant CANFrameModel::headerData(int section, Qt::Orientation orientation,
|
|||||||
void CANFrameModel::addFrame(CANFrame &frame, bool autoRefresh = false)
|
void CANFrameModel::addFrame(CANFrame &frame, bool autoRefresh = false)
|
||||||
{
|
{
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
if (autoRefresh) beginInsertRows(QModelIndex(), frames.count() + 1, frames.count() + 1);
|
|
||||||
frame.timestamp -= timeOffset;
|
frame.timestamp -= timeOffset;
|
||||||
|
if (!overwriteDups)
|
||||||
|
{
|
||||||
|
if (autoRefresh) beginInsertRows(QModelIndex(), frames.count() + 1, frames.count() + 1);
|
||||||
frames.append(frame);
|
frames.append(frame);
|
||||||
if (autoRefresh) endInsertRows();
|
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();
|
mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CANFrameModel::sendRefresh()
|
void CANFrameModel::sendRefresh()
|
||||||
{
|
{
|
||||||
beginInsertRows(QModelIndex(), 0, frames.count());
|
beginResetModel();
|
||||||
endInsertRows();
|
endResetModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CANFrameModel::sendRefresh(int pos)
|
void CANFrameModel::sendRefresh(int pos)
|
||||||
@@ -182,10 +238,18 @@ void CANFrameModel::sendBulkRefresh(int num)
|
|||||||
//otherwise it's possible that the grid is in an odd state.
|
//otherwise it's possible that the grid is in an odd state.
|
||||||
if (num == 0) return;
|
if (num == 0) return;
|
||||||
if (frames.count() == 0) return;
|
if (frames.count() == 0) return;
|
||||||
|
|
||||||
|
if (!overwriteDups)
|
||||||
|
{
|
||||||
if (num > frames.count()) num = frames.count();
|
if (num > frames.count()) num = frames.count();
|
||||||
beginInsertRows(QModelIndex(), frames.count() - num, frames.count() - 1);
|
beginInsertRows(QModelIndex(), frames.count() - num, frames.count() - 1);
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sendRefresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void CANFrameModel::clearFrames()
|
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 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
|
//Is it the best C++ practice? Probably not. This breaks the MVC paradigm
|
||||||
//but, it's for a good cause.
|
//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<CANFrame>* CANFrameModel::getListReference()
|
QVector<CANFrame>* CANFrameModel::getListReference()
|
||||||
{
|
{
|
||||||
return &frames;
|
return &frames;
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ public:
|
|||||||
void setInterpetMode(bool);
|
void setInterpetMode(bool);
|
||||||
void setOverwriteMode(bool);
|
void setOverwriteMode(bool);
|
||||||
void normalizeTiming();
|
void normalizeTiming();
|
||||||
|
void recalcOverwrite();
|
||||||
QVector<CANFrame> *getListReference();
|
QVector<CANFrame> *getListReference();
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -317,6 +317,7 @@ void MainWindow::handleLoadFile()
|
|||||||
QStringList fileList = filename.split('/');
|
QStringList fileList = filename.split('/');
|
||||||
loadedFileName = fileList[fileList.length() - 1];
|
loadedFileName = fileList[fileList.length() - 1];
|
||||||
|
|
||||||
|
model->recalcOverwrite();
|
||||||
model->sendRefresh();
|
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();
|
||||||
|
|||||||
Reference in New Issue
Block a user