From b7e77d42cf6c2011c81d38aba984ca1e0bd3f396 Mon Sep 17 00:00:00 2001 From: Viktor Kopp Date: Sun, 29 Sep 2024 20:55:05 +0200 Subject: [PATCH 1/6] Introduce toStringPayload to avoid code duplication in table model Signed-off-by: Viktor Kopp --- src/tablemodel.cpp | 55 +++++++++++++++++++--------------------------- src/tablemodel.h | 2 ++ 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/tablemodel.cpp b/src/tablemodel.cpp index 1018ff28..d7b0230c 100644 --- a/src/tablemodel.cpp +++ b/src/tablemodel.cpp @@ -144,7 +144,6 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) } } - QString visu_data; switch(index.column()) { case FieldNames::Index: @@ -242,30 +241,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: @@ -369,18 +355,7 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) } } - 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 +533,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 From e36d7081ecf68e3786c16304ca429b66123d25a2 Mon Sep 17 00:00:00 2001 From: Viktor Kopp Date: Sun, 29 Sep 2024 21:18:07 +0200 Subject: [PATCH 2/6] Introduce decodeMessageWithPlugin-lambda to avoid code duplication in table model Signed-off-by: Viktor Kopp --- src/tablemodel.cpp | 75 ++++++++++++---------------------------------- 1 file changed, 19 insertions(+), 56 deletions(-) diff --git a/src/tablemodel.cpp b/src/tablemodel.cpp index d7b0230c..501587d0 100644 --- a/src/tablemodel.cpp +++ b/src/tablemodel.cpp @@ -90,6 +90,21 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) 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) { @@ -129,20 +144,7 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) } } - 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(); switch(index.column()) { @@ -278,20 +280,7 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) getmessage( index.row(), filterposindex, &decodeflag, &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)))); @@ -303,20 +292,7 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) getmessage( index.row(), filterposindex, &decodeflag, &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))); @@ -340,20 +316,7 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) { 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(); return toStringPayload(msg); } From 213d08160edaed939801efa931f48feb5109afca Mon Sep 17 00:00:00 2001 From: Viktor Kopp Date: Sun, 29 Sep 2024 21:29:02 +0200 Subject: [PATCH 3/6] rm redundant code in tablemodel Signed-off-by: Viktor Kopp --- src/tablemodel.cpp | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/src/tablemodel.cpp b/src/tablemodel.cpp index 501587d0..a59bfd95 100644 --- a/src/tablemodel.cpp +++ b/src/tablemodel.cpp @@ -28,29 +28,21 @@ #include "qdltoptmanager.h" 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, long int filterposindex, unsigned int* decodeflag, 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 + { + *success = qfile->getMsg(filterposindex, *msg); + *lastmsg = *msg; + *decodeflag = 1; + } + + lastrow = indexrow; } @@ -67,10 +59,7 @@ TableModel::TableModel(const QString & /*data*/, QObject *parent) lastrow = -1; } - TableModel::~TableModel() - { - - } +TableModel::~TableModel() = default; int TableModel::columnCount(const QModelIndex & /*parent*/) const From 1928635efeee534b769ff08c61178bb0659231db Mon Sep 17 00:00:00 2001 From: Viktor Kopp Date: Sun, 29 Sep 2024 22:16:22 +0200 Subject: [PATCH 4/6] Get rid of decodeflag-argument getmessage-function Rename getmessage -> getMessage Signed-off-by: Viktor Kopp --- src/tablemodel.cpp | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/tablemodel.cpp b/src/tablemodel.cpp index a59bfd95..4eaf1caf 100644 --- a/src/tablemodel.cpp +++ b/src/tablemodel.cpp @@ -29,7 +29,7 @@ static long int lastrow = -1; // necessary because object tablemodel can not be changed, so no member variable can be used -void getmessage(int indexrow, long int filterposindex, unsigned int* decodeflag, QDltMsg* msg, QDltMsg* lastmsg, QDltFile* qfile, bool* success ) +void getMessage(int indexrow, long int filterposindex, QDltMsg* msg, QDltMsg* lastmsg, QDltFile* qfile, bool* success ) { if (indexrow == lastrow) { @@ -39,7 +39,6 @@ void getmessage(int indexrow, long int filterposindex, unsigned int* decodeflag, { *success = qfile->getMsg(filterposindex, *msg); *lastmsg = *msg; - *decodeflag = 1; } lastrow = indexrow; @@ -70,7 +69,6 @@ TableModel::~TableModel() = default; QVariant TableModel::data(const QModelIndex &index, int role) const { - QByteArray buf; static QDltMsg msg; static QDltMsg lastmsg; static QDltMsg last_decoded_msg; @@ -110,27 +108,27 @@ TableModel::~TableModel() = default; 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(), filterposindex, &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(); - } } decodeMessageWithPlugin(); @@ -265,8 +263,9 @@ TableModel::~TableModel() = default; 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(), filterposindex, &msg, &lastmsg, qfile, &success); // version2 /* decode message if not already decoded */ decodeMessageWithPlugin(); @@ -277,8 +276,9 @@ TableModel::~TableModel() = default; 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(), filterposindex, &msg, &lastmsg, qfile, &success); // version2 /* decode message if not already decoded */ decodeMessageWithPlugin(); @@ -300,7 +300,8 @@ TableModel::~TableModel() = default; if ( role == Qt::ToolTipRole ) { - getmessage( index.row(), filterposindex, &decodeflag, &msg, &lastmsg, qfile, &success); + decodeflag = (index.row() != lastrow); + getMessage( index.row(), filterposindex, &msg, &lastmsg, qfile, &success); if ( success == false ) { return QString("!!CORRUPTED MESSAGE!!"); From 53829a763065807302aaa166922fb14480ad72a3 Mon Sep 17 00:00:00 2001 From: Viktor Kopp Date: Sun, 29 Sep 2024 22:23:45 +0200 Subject: [PATCH 5/6] Get rid of filterposindex-argument of getMessage-function Signed-off-by: Viktor Kopp --- src/tablemodel.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/tablemodel.cpp b/src/tablemodel.cpp index 4eaf1caf..403a347f 100644 --- a/src/tablemodel.cpp +++ b/src/tablemodel.cpp @@ -29,7 +29,7 @@ static long int lastrow = -1; // necessary because object tablemodel can not be changed, so no member variable can be used -void getMessage(int indexrow, long int filterposindex, QDltMsg* msg, QDltMsg* lastmsg, QDltFile* qfile, bool* success ) +void getMessage(int indexrow, QDltMsg* msg, QDltMsg* lastmsg, QDltFile* qfile, bool* success ) { if (indexrow == lastrow) { @@ -37,6 +37,7 @@ void getMessage(int indexrow, long int filterposindex, QDltMsg* msg, QDltMsg* la } else { + auto filterposindex = qfile->getMsgFilterPos(indexrow); *success = qfile->getMsg(filterposindex, *msg); *lastmsg = *msg; } @@ -75,8 +76,6 @@ TableModel::~TableModel() = default; static unsigned int decodeflag = 0; static bool success = true; - long int filterposindex = 0; - auto decodeMessageWithPlugin = [&] { if (QDltSettingsManager::getInstance()->value("startup/pluginsEnabled", true).toBool()) { @@ -103,8 +102,6 @@ TableModel::~TableModel() = default; return QVariant(); } - filterposindex = qfile->getMsgFilterPos(index.row()); - if (role == Qt::DisplayRole) { /* get the message with the selected item id */ @@ -115,7 +112,7 @@ TableModel::~TableModel() = default; else { decodeflag = (index.row() != lastrow); - getMessage(index.row(), filterposindex, &msg, &lastmsg, qfile, &success); + getMessage(index.row(), &msg, &lastmsg, qfile, &success); if ( success == false ) { @@ -261,16 +258,19 @@ TableModel::~TableModel() = default; } } + long int filterposindex = qfile->getMsgFilterPos(index.row()); + if ( role == Qt::ForegroundRole ) { decodeflag = (index.row() != lastrow); /* get message at current row */ - getMessage( index.row(), filterposindex, &msg, &lastmsg, qfile, &success); // version2 + getMessage( index.row(), &msg, &lastmsg, qfile, &success); // version2 /* decode message if not already decoded */ decodeMessageWithPlugin(); /* Calculate background color and find optimal forground color */ + return QVariant(QBrush(DltUiUtils::optimalTextColor(getMsgBackgroundColor(msg,index.row(),filterposindex)))); } @@ -278,7 +278,7 @@ TableModel::~TableModel() = default; { decodeflag = (index.row() != lastrow); /* get message at current row */ - getMessage( index.row(), filterposindex, &msg, &lastmsg, qfile, &success); // version2 + getMessage( index.row(), &msg, &lastmsg, qfile, &success); // version2 /* decode message if not already decoded */ decodeMessageWithPlugin(); @@ -301,7 +301,7 @@ TableModel::~TableModel() = default; if ( role == Qt::ToolTipRole ) { decodeflag = (index.row() != lastrow); - getMessage( index.row(), filterposindex, &msg, &lastmsg, qfile, &success); + getMessage(index.row(), &msg, &lastmsg, qfile, &success); if ( success == false ) { return QString("!!CORRUPTED MESSAGE!!"); From 1ebb9380f53a39ac8798a257c0f664b6e45c2a1c Mon Sep 17 00:00:00 2001 From: Viktor Kopp Date: Sun, 29 Sep 2024 22:44:50 +0200 Subject: [PATCH 6/6] Add todo comment and adjust formatting Signed-off-by: Viktor Kopp --- src/tablemodel.cpp | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/tablemodel.cpp b/src/tablemodel.cpp index 403a347f..a7fcfcea 100644 --- a/src/tablemodel.cpp +++ b/src/tablemodel.cpp @@ -27,6 +27,9 @@ #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 void getMessage(int indexrow, QDltMsg* msg, QDltMsg* lastmsg, QDltFile* qfile, bool* success ) @@ -45,30 +48,27 @@ void getMessage(int indexrow, QDltMsg* msg, QDltMsg* lastmsg, QDltFile* qfile, b 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; - } + : 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 { static QDltMsg msg; static QDltMsg lastmsg;