diff --git a/src/tablemodel.cpp b/src/tablemodel.cpp index 1018ff28..a7fcfcea 100644 --- a/src/tablemodel.cpp +++ b/src/tablemodel.cpp @@ -27,69 +27,70 @@ #include "dlt_protocol.h" #include "qdltoptmanager.h" +// this is done to have a basic caching mechanism in the table for the cases when the same dlt +// message is accessed multiple times in the TableModel::data function several times in a row +// TODO: replace with some kind of LRU cache to make it explicit static long int lastrow = -1; // necessary because object tablemodel can not be changed, so no member variable can be used -char buffer[DLT_VIEWER_LIST_BUFFER_SIZE]; - -void getmessage( int indexrow, long int filterposindex, unsigned int* decodeflag, QDltMsg* msg, QDltMsg* lastmsg, QDltFile* qfile, bool* success ) +void getMessage(int indexrow, QDltMsg* msg, QDltMsg* lastmsg, QDltFile* qfile, bool* success ) { - if ( indexrow == lastrow) - { - *msg = *lastmsg; - } - else - { - *success = qfile->getMsg(filterposindex, *msg); - *lastmsg = *msg; - *decodeflag = 1; - } - if ( indexrow == 0) - { - lastrow = 0; - } - else - { - lastrow = indexrow; - } -} + if (indexrow == lastrow) + { + *msg = *lastmsg; + } + else + { + auto filterposindex = qfile->getMsgFilterPos(indexrow); + *success = qfile->getMsg(filterposindex, *msg); + *lastmsg = *msg; + } + lastrow = indexrow; +} TableModel::TableModel(const QString & /*data*/, QObject *parent) - : QAbstractTableModel(parent) - { - qfile = NULL; - project = NULL; - pluginManager = NULL; - lastSearchIndex = -1; - emptyForceFlag = false; - loggingOnlyMode = false; - searchhit = -1; - lastrow = -1; - } - - TableModel::~TableModel() - { - - } + : QAbstractTableModel(parent) +{ + qfile = NULL; + project = NULL; + pluginManager = NULL; + lastSearchIndex = -1; + emptyForceFlag = false; + loggingOnlyMode = false; + searchhit = -1; + lastrow = -1; +} +TableModel::~TableModel() = default; - int TableModel::columnCount(const QModelIndex & /*parent*/) const - { - return DLT_VIEWER_COLUMN_COUNT+project->settings->showArguments; - } - +int TableModel::columnCount(const QModelIndex & /*parent*/) const +{ + return DLT_VIEWER_COLUMN_COUNT + project->settings->showArguments; +} - QVariant TableModel::data(const QModelIndex &index, int role) const +QVariant TableModel::data(const QModelIndex &index, int role) const { - QByteArray buf; static QDltMsg msg; static QDltMsg lastmsg; static QDltMsg last_decoded_msg; static unsigned int decodeflag = 0; static bool success = true; - long int filterposindex = 0; - + auto decodeMessageWithPlugin = [&] { + if (QDltSettingsManager::getInstance()->value("startup/pluginsEnabled", true).toBool()) + { + if ( decodeflag == 1 ) + { + decodeflag = 0; + pluginManager->decodeMsg(msg, !QDltOptManager::getInstance()->issilentMode()); + last_decoded_msg = msg; + } + else + { + msg = last_decoded_msg; + } + } + }; if (index.isValid() == false) { @@ -101,50 +102,34 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) return QVariant(); } - filterposindex = qfile->getMsgFilterPos(index.row()); - if (role == Qt::DisplayRole) { /* get the message with the selected item id */ - if(true == loggingOnlyMode) + if(loggingOnlyMode) { msg = QDltMsg(); } else { - getmessage( index.row(), filterposindex, &decodeflag, &msg, &lastmsg, qfile, &success); + decodeflag = (index.row() != lastrow); + getMessage(index.row(), &msg, &lastmsg, qfile, &success); - if ( success == false ) - { - if(index.column() == FieldNames::Index) + if ( success == false ) { - return QString("%1").arg(qfile->getMsgFilterPos(index.row())); - } - else if(index.column() == FieldNames::Payload) - { - qDebug() << "Corrupted message at index" << index.row(); - return QString("!!CORRUPTED MESSAGE!!"); + if(index.column() == FieldNames::Index) + { + return QString("%1").arg(qfile->getMsgFilterPos(index.row())); + } + else if(index.column() == FieldNames::Payload) + { + return QString("!!CORRUPTED MESSAGE!!"); + } + return QVariant(); } - return QVariant(); - } } - if((QDltSettingsManager::getInstance()->value("startup/pluginsEnabled", true).toBool())) - { - if ( decodeflag == 1 ) - { - decodeflag = 0; - last_decoded_msg = msg; - pluginManager->decodeMsg(msg,!QDltOptManager::getInstance()->issilentMode()); - last_decoded_msg = msg; - } - else - { - msg = last_decoded_msg; - } - } + decodeMessageWithPlugin(); - QString visu_data; switch(index.column()) { case FieldNames::Index: @@ -242,30 +227,17 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) case FieldNames::ArgCount: return QString("%1").arg(msg.getNumberOfArguments()); case FieldNames::Payload: - if( true == loggingOnlyMode) + { + if(loggingOnlyMode) { return QString("Logging only Mode! Disable in Project Settings!"); } - /* display payload */ - visu_data = msg.toStringPayload().simplified().remove(QChar::Null); - - if((QDltSettingsManager::getInstance()->value("startup/filtersEnabled", true).toBool())) - { - for(int num = 0; num < project->filter->topLevelItemCount (); num++) { - FilterItem *item = (FilterItem*)project->filter->topLevelItem(num); - if(item->checkState(0) == Qt::Checked && item->filter.enableRegexSearchReplace) { - visu_data.replace(QRegularExpression(item->filter.regex_search), item->filter.regex_replace); - } - } - } + /* display payload */ + QString payloadStr = toStringPayload(msg); /* limit size of string to 1000 characters to speed up scrolling */ - if(visu_data.size()>1000) - { - visu_data = visu_data.mid(0,1000); - } - - return visu_data; + return payloadStr.mid(0, 1000); + } case FieldNames::MessageId: return QString::asprintf(project->settings->msgIdFormat.toUtf8() ,msg.getMessageId()); default: @@ -286,51 +258,30 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) } } + long int filterposindex = qfile->getMsgFilterPos(index.row()); + if ( role == Qt::ForegroundRole ) { + decodeflag = (index.row() != lastrow); /* get message at current row */ - getmessage( index.row(), filterposindex, &decodeflag, &msg, &lastmsg, qfile, &success); // version2 + getMessage( index.row(), &msg, &lastmsg, qfile, &success); // version2 /* decode message if not already decoded */ - if((QDltSettingsManager::getInstance()->value("startup/pluginsEnabled", true).toBool())) - { - if ( decodeflag == 1 ) - { - decodeflag = 0; - last_decoded_msg = msg; - pluginManager->decodeMsg(msg,!QDltOptManager::getInstance()->issilentMode()); - last_decoded_msg = msg; - } - else - { - msg = last_decoded_msg; - } - } + decodeMessageWithPlugin(); /* Calculate background color and find optimal forground color */ + return QVariant(QBrush(DltUiUtils::optimalTextColor(getMsgBackgroundColor(msg,index.row(),filterposindex)))); } if ( role == Qt::BackgroundRole ) { + decodeflag = (index.row() != lastrow); /* get message at current row */ - getmessage( index.row(), filterposindex, &decodeflag, &msg, &lastmsg, qfile, &success); // version2 + getMessage( index.row(), &msg, &lastmsg, qfile, &success); // version2 /* decode message if not already decoded */ - if((QDltSettingsManager::getInstance()->value("startup/pluginsEnabled", true).toBool())) - { - if ( decodeflag == 1 ) - { - decodeflag = 0; - last_decoded_msg = msg; - pluginManager->decodeMsg(msg,!QDltOptManager::getInstance()->issilentMode()); - last_decoded_msg = msg; - } - else - { - msg = last_decoded_msg; - } - } + decodeMessageWithPlugin(); /* Calculate background color */ return QVariant(QBrush(getMsgBackgroundColor(msg,index.row(),filterposindex))); @@ -349,38 +300,15 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) if ( role == Qt::ToolTipRole ) { - getmessage( index.row(), filterposindex, &decodeflag, &msg, &lastmsg, qfile, &success); + decodeflag = (index.row() != lastrow); + getMessage(index.row(), &msg, &lastmsg, qfile, &success); if ( success == false ) { return QString("!!CORRUPTED MESSAGE!!"); } - if((QDltSettingsManager::getInstance()->value("startup/pluginsEnabled", true).toBool())) - { - if ( decodeflag == 1 ) - { - decodeflag = 0; - last_decoded_msg = msg; - pluginManager->decodeMsg(msg,!QDltOptManager::getInstance()->issilentMode()); - last_decoded_msg = msg; - } - else - { - msg = last_decoded_msg; - } - } + decodeMessageWithPlugin(); - QString visu_data = msg.toStringPayload().simplified().remove(QChar::Null); - if((QDltSettingsManager::getInstance()->value("startup/filtersEnabled", true).toBool())) - { - for(int num = 0; num < project->filter->topLevelItemCount (); num++) { - FilterItem *item = (FilterItem*)project->filter->topLevelItem(num); - if(item->checkState(0) == Qt::Checked && item->filter.enableRegexSearchReplace) { - visu_data.replace(QRegularExpression(item->filter.regex_search), item->filter.regex_replace); - } - } - } - - return visu_data; + return toStringPayload(msg); } return QVariant(); @@ -558,3 +486,19 @@ QColor TableModel::getMsgBackgroundColor(QDltMsg &msg,int index,long int filterp return brushColor; // this is the default background color } + +QString TableModel::toStringPayload(const QDltMsg& msg) const +{ + QString result = msg.toStringPayload().simplified().remove(QChar::Null); + if((QDltSettingsManager::getInstance()->value("startup/filtersEnabled", true).toBool())) + { + for(int num = 0; num < project->filter->topLevelItemCount (); num++) { + FilterItem *item = (FilterItem*)project->filter->topLevelItem(num); + if(item->checkState(0) == Qt::Checked && item->filter.enableRegexSearchReplace) { + result.replace(QRegularExpression(item->filter.regex_search), item->filter.regex_replace); + } + } + } + + return result; +} diff --git a/src/tablemodel.h b/src/tablemodel.h index edcb4ef2..ac26f48d 100644 --- a/src/tablemodel.h +++ b/src/tablemodel.h @@ -72,6 +72,8 @@ Q_OBJECT QColor manualMarkerColor; QList selectedMarkerRows; QColor getMsgBackgroundColor(QDltMsg &msg,int index,long int filterposindex) const; + + QString toStringPayload(const QDltMsg&) const; }; class HtmlDelegate : public QStyledItemDelegate