Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor tablemodel cleanup #551

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 97 additions & 153 deletions src/tablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexmucde initially I had a look to this file to understand if I can remove usage of QDltOptManager singleton in here. this is the only place it is used. Do you know how issilentmode command option relates to triggeredByUser argument of the decodeMsg? Can I simply pass false here? I know it is not used in the existing plugins, however I am not sure about third-party ones

last_decoded_msg = msg;
}
else
{
msg = last_decoded_msg;
}
}
};

if (index.isValid() == false)
{
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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)));
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/tablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Q_OBJECT
QColor manualMarkerColor;
QList<unsigned long int> selectedMarkerRows;
QColor getMsgBackgroundColor(QDltMsg &msg,int index,long int filterposindex) const;

QString toStringPayload(const QDltMsg&) const;
};

class HtmlDelegate : public QStyledItemDelegate
Expand Down