Skip to content

Commit

Permalink
Background thread for export. (#584)
Browse files Browse the repository at this point in the history
Improve progress bar for Export.

Signed-off-by: Alexander Wenzel <[email protected]>
  • Loading branch information
alexmucde authored Nov 19, 2024
1 parent d52eced commit 7d5ac32
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 97 deletions.
20 changes: 8 additions & 12 deletions commander/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,38 +122,34 @@ int main(int argc, char *argv[])

if(opt.get_convertionmode()==e_DLT)
{
QFile output(opt.getConvertDestFile());
qDebug() << "### Convert to DLT";
QDltExporter exporter(1,0,0,opt.getDelimiter());
QDltExporter exporter(&dltFile,opt.getConvertDestFile(),0,QDltExporter::FormatDlt,QDltExporter::SelectionFiltered,0,1,0,0,opt.getDelimiter());
qDebug() << "Commandline DLT convert to " << opt.getConvertDestFile();
exporter.exportMessages(&dltFile,&output,0,QDltExporter::FormatDlt,QDltExporter::SelectionFiltered);
exporter.exportMessages();
qDebug() << "DLT export to DLT file format done";
}
if(opt.get_convertionmode()==e_ASCI)
{
QFile output(opt.getConvertDestFile());
qDebug() << "### Convert to ASCII";
QDltExporter exporter(1,0,0,opt.getDelimiter());
QDltExporter exporter(&dltFile,opt.getConvertDestFile(),0,QDltExporter::FormatAscii,QDltExporter::SelectionFiltered,0,1,0,0,opt.getDelimiter());
qDebug() << "Commandline ASCII convert to " << opt.getConvertDestFile();
exporter.exportMessages(&dltFile,&output,0,QDltExporter::FormatAscii,QDltExporter::SelectionFiltered);
exporter.exportMessages();
qDebug() << "DLT export ASCII done";
}
if(opt.get_convertionmode()==e_CSV)
{
QFile output(opt.getConvertDestFile());
qDebug() << "### Convert to CSV";
QDltExporter exporter(1,0,0,opt.getDelimiter());
QDltExporter exporter(&dltFile,opt.getConvertDestFile(),0,QDltExporter::FormatCsv,QDltExporter::SelectionFiltered,0,1,0,0,opt.getDelimiter());
qDebug() << "Commandline ASCII convert to " << opt.getConvertDestFile();
exporter.exportMessages(&dltFile,&output,0,QDltExporter::FormatCsv,QDltExporter::SelectionFiltered);
exporter.exportMessages();
qDebug() << "DLT export CSV done";
}
if(opt.get_convertionmode()==e_UTF8)
{
QFile output(opt.getConvertDestFile());
qDebug() << "### Convert to UTF8";
QDltExporter exporter(1,0,0,opt.getDelimiter());
QDltExporter exporter(&dltFile,opt.getConvertDestFile(),0,QDltExporter::FormatUTF8,QDltExporter::SelectionFiltered,0,1,0,0,opt.getDelimiter());
qDebug() << "Commandline UTF8 convert to " << opt.getConvertDestFile();
exporter.exportMessages(&dltFile,&output,0,QDltExporter::FormatUTF8,QDltExporter::SelectionFiltered);
exporter.exportMessages();
qDebug() << "DLT export UTF8 done";
}
}
Expand Down
77 changes: 40 additions & 37 deletions qdlt/qdltexporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,33 @@
#include "fieldnames.h"
#include "qdltoptmanager.h"

QDltExporter::QDltExporter(int _automaticTimeSettings,qlonglong _utcOffset,int _dst,char _delimiter,QObject *parent) :
QObject(parent)
QDltExporter::QDltExporter(QDltFile *from, QString outputfileName, QDltPluginManager *pluginManager,
QDltExporter::DltExportFormat exportFormat,
QDltExporter::DltExportSelection exportSelection, QModelIndexList *selection, int _automaticTimeSettings,qlonglong _utcOffset,int _dst,char _delimiter,QObject *parent) :
QThread(parent)
{
size = 0;
from = NULL;
to = NULL;
pluginManager = NULL;
selection = NULL;
exportFormat = FormatDlt;
exportSelection = SelectionAll;
starting_index=0;
stoping_index=0;
automaticTimeSettings=_automaticTimeSettings;
utcOffset=_utcOffset;
dst=_dst;
delimiter=_delimiter;

this->from = from;
to.setFileName(outputfileName);
this->pluginManager = pluginManager;
this->selection = selection;
this->exportFormat = exportFormat;
this->exportSelection = exportSelection;
this->selection = selection;
}

void QDltExporter::run()
{
QString result;
exportMessages();
emit resultReady(result);
}

QString QDltExporter::escapeCSVValue(QString arg)
Expand All @@ -31,7 +42,7 @@ QString QDltExporter::escapeCSVValue(QString arg)
return retval;
}

bool QDltExporter::writeCSVHeader(QFile *file)
bool QDltExporter::writeCSVHeader()
{
QString header = QString("\"%1\"")+delimiter+QString("\"%2\"")+delimiter+QString("\"%3\"")+delimiter+QString("\"%4\"")+delimiter+QString("\"%5\"")+delimiter+QString("\"%6\"")+delimiter+QString("\"%7\"")+delimiter+QString("\"%8\"")+delimiter+QString("\"%9\"")+delimiter+QString("\"%10\"")+delimiter+QString("\"%11\"")+delimiter+QString("\"%12\"")+delimiter+QString("\"%13\"\n");
header = header.arg(FieldNames::getName(FieldNames::Index))
Expand All @@ -47,10 +58,10 @@ bool QDltExporter::writeCSVHeader(QFile *file)
.arg(FieldNames::getName(FieldNames::Mode))
.arg(FieldNames::getName(FieldNames::ArgCount))
.arg(FieldNames::getName(FieldNames::Payload));
return file->write(header.toLatin1().constData()) < 0 ? false : true;
return to.write(header.toLatin1().constData()) < 0 ? false : true;
}

void QDltExporter::writeCSVLine(int index, QFile *to, QDltMsg msg)
void QDltExporter::writeCSVLine(int index, QDltMsg msg)
{
QString text("");

Expand All @@ -74,10 +85,10 @@ void QDltExporter::writeCSVLine(int index, QFile *to, QDltMsg msg)
text += escapeCSVValue(payload);
text += "\n";

to->write(text.toLatin1().constData());
to.write(text.toLatin1().constData());
}

bool QDltExporter::start()
bool QDltExporter::startExport()
{
/* Sort the selection list and create Row list */
if(exportSelection == QDltExporter::SelectionSelected && selection != NULL)
Expand All @@ -97,11 +108,11 @@ bool QDltExporter::start()
exportFormat == QDltExporter::FormatUTF8 ||
exportFormat == QDltExporter::FormatCsv)
{
if(!to->open(QIODevice::WriteOnly | QIODevice::Text))
if(!to.open(QIODevice::WriteOnly | QIODevice::Text))
{
if (QDltOptManager::getInstance()->issilentMode())
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to->fileName());
qDebug() << QString("ERROR - cannot open the export file %1").arg(to.fileName());
}
//else
//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
Expand All @@ -111,11 +122,11 @@ bool QDltExporter::start()
}
else if((exportFormat == QDltExporter::FormatDlt)||(exportFormat == QDltExporter::FormatDltDecoded))
{
if(!to->open(QIODevice::WriteOnly))
if(!to.open(QIODevice::WriteOnly))
{
if (QDltOptManager::getInstance()->issilentMode() )
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to->fileName());
qDebug() << QString("ERROR - cannot open the export file %1").arg(to.fileName());
}
//else
//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
Expand All @@ -128,11 +139,11 @@ bool QDltExporter::start()
if(exportFormat == QDltExporter::FormatCsv)
{
/* Write the first line of CSV file */
if(!writeCSVHeader(to))
if(!writeCSVHeader())
{
if(QDltOptManager::getInstance()->issilentMode())
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to->fileName());
qDebug() << QString("ERROR - cannot open the export file %1").arg(to.fileName());
}
//else
//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
Expand Down Expand Up @@ -175,7 +186,7 @@ bool QDltExporter::finish()
exportFormat == QDltExporter::FormatDltDecoded)
{
/* close output file */
to->close();
to.close();
}
else if (exportFormat == QDltExporter::FormatClipboard ||
exportFormat == QDltExporter::FormatClipboardPayloadOnly ||
Expand Down Expand Up @@ -245,7 +256,7 @@ bool QDltExporter::exportMsg(unsigned long int num, QDltMsg &msg, QByteArray &bu
{
if((exportFormat == QDltExporter::FormatDlt)||(exportFormat == QDltExporter::FormatDltDecoded))
{
to->write(buf);
to.write(buf);
}
else if(exportFormat == QDltExporter::FormatAscii ||
exportFormat == QDltExporter::FormatUTF8 ||
Expand Down Expand Up @@ -290,9 +301,9 @@ bool QDltExporter::exportMsg(unsigned long int num, QDltMsg &msg, QByteArray &bu
{
if(exportFormat == QDltExporter::FormatAscii)
/* write to file */
to->write(text.toLatin1().constData());
to.write(text.toLatin1().constData());
else if (exportFormat == QDltExporter::FormatUTF8)
to->write(text.toUtf8().constData());
to.write(text.toUtf8().constData());
else if(exportFormat == QDltExporter::FormatClipboard ||
exportFormat == QDltExporter::FormatClipboardPayloadOnly)
clipboardString += text;
Expand All @@ -304,11 +315,11 @@ bool QDltExporter::exportMsg(unsigned long int num, QDltMsg &msg, QByteArray &bu
else if(exportFormat == QDltExporter::FormatCsv)
{
if(exportSelection == QDltExporter::SelectionAll)
writeCSVLine(num, to, msg);
writeCSVLine(num, msg);
else if(exportSelection == QDltExporter::SelectionFiltered)
writeCSVLine(from->getMsgFilterPos(num), to, msg);
writeCSVLine(from->getMsgFilterPos(num), msg);
else if(exportSelection == QDltExporter::SelectionSelected)
writeCSVLine(from->getMsgFilterPos(selectedRows[num]), to, msg);
writeCSVLine(from->getMsgFilterPos(selectedRows[num]), msg);
else
return false;
}
Expand Down Expand Up @@ -350,9 +361,7 @@ void QDltExporter::exportMessageRange(unsigned long start, unsigned long stop)
this->stoping_index=stop;
}

void QDltExporter::exportMessages(QDltFile *from, QFile *to, QDltPluginManager *pluginManager,
QDltExporter::DltExportFormat exportFormat,
QDltExporter::DltExportSelection exportSelection, QModelIndexList *selection)
void QDltExporter::exportMessages()
{
QDltMsg msg;
QByteArray buf;
Expand All @@ -363,17 +372,11 @@ void QDltExporter::exportMessages(QDltFile *from, QFile *to, QDltPluginManager *
int exportErrors=0;
int exportCounter=0;
int startFinishError=0;
this->from = from;
this->to = to;
clipboardString.clear();
this->pluginManager = pluginManager;
this->selection = selection;
this->exportFormat = exportFormat;
this->exportSelection = exportSelection;
unsigned long int starting = 0;
unsigned long int stoping = this->size;
/* start export */
if(false == start())
if(false == startExport())
{
qDebug() << "DLT Export start() failed";
startFinishError++;
Expand Down Expand Up @@ -406,7 +409,7 @@ void QDltExporter::exportMessages(QDltFile *from, QFile *to, QDltPluginManager *
if(percent>=progressCounter)
{
progressCounter += 1;
emit progress("MF4:",2,percent); // every 1%
emit progress("Exp:",2,percent); // every 1%
if((percent>0) && ((percent%10)==0))
qDebug() << "Exported:" << percent << "%"; // every 10%
}
Expand Down
22 changes: 13 additions & 9 deletions qdlt/qdltexporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define QDLTEXPORTER_H

#include <QObject>
#include <QThread>
#include <QFile>
#include <QModelIndexList>

Expand All @@ -10,7 +11,7 @@
#include "qdltmsg.h"
#include "qdltpluginmanager.h"

class QDLT_EXPORT QDltExporter : public QObject
class QDLT_EXPORT QDltExporter : public QThread
{
Q_OBJECT

Expand All @@ -34,16 +35,16 @@ class QDLT_EXPORT QDltExporter : public QObject
* \param file outputfile to write to
* \return True if writing was succesfull, false if error occured
*/
bool writeCSVHeader(QFile *file);
bool writeCSVHeader();

/* Write the message out to an open file
* \param index True index to QDltFile of the message
* \param to File to write to
* \param msg msg to get the data from
*/
void writeCSVLine(int index, QFile *to, QDltMsg msg);
void writeCSVLine(int index, QDltMsg msg);

bool start();
bool startExport();
bool finish();
bool getMsg(unsigned long int num, QDltMsg &msg, QByteArray &buf);
bool exportMsg(unsigned long int num, QDltMsg &msg,QByteArray &buf);
Expand All @@ -53,7 +54,11 @@ class QDLT_EXPORT QDltExporter : public QObject
/* Default QT constructor.
* Please pass a window as a parameter to parent dialogs correctly.
*/
explicit QDltExporter(int _automaticTimeSettings,qlonglong _utcOffset,int _dst,char _delimiter,QObject *parent = 0);
explicit QDltExporter(QDltFile *from, QString outputfileName, QDltPluginManager *pluginManager,
QDltExporter::DltExportFormat exportFormat,
QDltExporter::DltExportSelection exportSelection, QModelIndexList *selection, int _automaticTimeSettings,qlonglong _utcOffset,int _dst,char _delimiter,QObject *parent = 0);

void run() override;

/* Export some messages from QDltFile to a CSV file.
* \param from QDltFile to pull messages from
Expand All @@ -63,16 +68,15 @@ class QDLT_EXPORT QDltExporter : public QObject
* \param exportSelection
* \param selection Limit export to these messages. Leave to NULL to export everything,
*/
void exportMessages(QDltFile *from, QFile *to, QDltPluginManager *pluginManager,
QDltExporter::DltExportFormat exportFormat,
QDltExporter::DltExportSelection exportSelection, QModelIndexList *selection = 0);
void exportMessages();

void exportMessageRange(unsigned long start, unsigned long stop);

signals:

void clipboard(QString text);
void progress(QString name,int status, int progress);
void resultReady(const QString &s);

public slots:

Expand All @@ -81,7 +85,7 @@ public slots:
unsigned long int starting_index;
unsigned long int stoping_index;
QDltFile *from;
QFile *to;
QFile to;
QString clipboardString;
QDltPluginManager *pluginManager;
QModelIndexList *selection;
Expand Down
Loading

0 comments on commit 7d5ac32

Please sign in to comment.