From a738e787febb0fa629948c69bfe39afc16ce0992 Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Mon, 19 Jun 2017 10:53:55 +0200 Subject: [PATCH] fix for issue #46 (load csv files) --- include/PlotJuggler/dataloader_base.h | 2 - plugins/DataLoadCSV/DataLoadCSV.pro | 44 -------- plugins/DataLoadCSV/dataload_csv.cpp | 155 +++++++++++++------------- 3 files changed, 77 insertions(+), 124 deletions(-) delete mode 100644 plugins/DataLoadCSV/DataLoadCSV.pro diff --git a/include/PlotJuggler/dataloader_base.h b/include/PlotJuggler/dataloader_base.h index 18c2eb9de..bf87d9403 100644 --- a/include/PlotJuggler/dataloader_base.h +++ b/include/PlotJuggler/dataloader_base.h @@ -7,8 +7,6 @@ #include #include "PlotJuggler/plotdata.h" -enum { TIME_INDEX_NOT_DEFINED = -2 }; - class DataLoader{ public: diff --git a/plugins/DataLoadCSV/DataLoadCSV.pro b/plugins/DataLoadCSV/DataLoadCSV.pro deleted file mode 100644 index f4af44332..000000000 --- a/plugins/DataLoadCSV/DataLoadCSV.pro +++ /dev/null @@ -1,44 +0,0 @@ -#------------------------------------------------- -# -# Project created by QtCreator 2016-04-02T12:24:58 -# -#------------------------------------------------- - -TARGET = $$qtLibraryTarget(DataLoadCSV) - -TEMPLATE = lib -CONFIG += plugin c++11 - -QT += gui widgets - - -SOURCES += dataload_csv.cpp \ - selectxaxisdialog.cpp - -HEADERS += dataload_csv.h\ - selectxaxisdialog.h - -FORMS += selectxaxisdialog.ui - -INCLUDEPATH += $$PWD/../../common \ - $$PWD/../../qwt/src - -unix { - target.path = /usr/lib - INSTALLS += target -} - -DESTDIR = $$OUT_PWD/../../bin/plugins - -win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../lib/ -lcommon -else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../lib/ -lcommond -else:unix: LIBS += -L$$OUT_PWD/../../lib/ -lcommon - -INCLUDEPATH += $$PWD/../../common -DEPENDPATH += $$PWD/../../common - -win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../lib/libcommon.a -else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../lib/libcommond.a -else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../lib/common.lib -else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../lib/commond.lib -else:unix: PRE_TARGETDEPS += $$OUT_PWD/../../lib/libcommon.a diff --git a/plugins/DataLoadCSV/dataload_csv.cpp b/plugins/DataLoadCSV/dataload_csv.cpp index 525237067..90312fd05 100644 --- a/plugins/DataLoadCSV/dataload_csv.cpp +++ b/plugins/DataLoadCSV/dataload_csv.cpp @@ -39,6 +39,10 @@ int DataLoadCSV::parseHeader(QFile *file, } QString qname = field_name.toString(); + if( qname.isEmpty()) + { + qname = QString("_Column_%1").arg(i); + } ordered_names.push_back( std::make_pair(true,qname) ); } @@ -95,6 +99,8 @@ int DataLoadCSV::parseHeader(QFile *file, PlotDataMap DataLoadCSV::readDataFromFile(const QString &file_name, QString &load_configuration ) { + const int TIME_INDEX_NOT_DEFINED = -2; + int time_index = TIME_INDEX_NOT_DEFINED; PlotDataMap plot_data; @@ -126,107 +132,100 @@ PlotDataMap DataLoadCSV::readDataFromFile(const QString &file_name, progress_dialog.show(); double prev_time = -1; - bool first_line = true; - while (!inB.atEnd()) - { - QString line = inB.readLine(); + // remove first line (header + inB.readLine(); - QStringList string_items = line.split(','); - QStringList valid_field_names; + //---- build plots_vector from header ------ + QStringList valid_field_names; - if( first_line ) + for (unsigned i=0; i < ordered_names.size(); i++ ) + { + bool valid = ordered_names[i].first; + if( valid ) { - for (unsigned i=0; i < ordered_names.size(); i++ ) - { - bool valid = ordered_names[i].first; - if( valid ) - { - QString& qname = ( ordered_names[i].second ); - std::string name = qname.toStdString(); + QString& qname = ( ordered_names[i].second ); + std::string name = qname.toStdString(); - PlotDataPtr plot( new PlotData(name.c_str()) ); - plot_data.numeric.insert( std::make_pair( name, plot ) ); + PlotDataPtr plot( new PlotData(name.c_str()) ); + plot_data.numeric.insert( std::make_pair( name, plot ) ); - valid_field_names.push_back( qname ); + valid_field_names.push_back( qname ); + plots_vector.push_back( plot ); - plots_vector.push_back( plot ); - - if (time_index == TIME_INDEX_NOT_DEFINED) - { - if( load_configuration== qname ) - { - time_index = valid_field_names.size() ; - } - } + if (time_index == TIME_INDEX_NOT_DEFINED) + { + if( load_configuration == qname ) + { + time_index = valid_field_names.size() ; } } + } + } - if( load_configuration.compare( "INDEX (auto-generated)" ) == 0) - { - time_index = -1; - } + if( time_index == TIME_INDEX_NOT_DEFINED) + { + QStringList field_names; + field_names.push_back( "INDEX (auto-generated)" ); + field_names.append( valid_field_names ); - if( time_index == TIME_INDEX_NOT_DEFINED) - { - QStringList field_names; - field_names.push_back( "INDEX (auto-generated)" ); - field_names.append( valid_field_names ); + SelectFromListDialog* dialog = new SelectFromListDialog( &field_names ); + dialog->setWindowTitle("Select the time axis"); + int res = dialog->exec(); - SelectFromListDialog* dialog = new SelectFromListDialog( &field_names ); - dialog->setWindowTitle("Select the time axis"); - int res = dialog->exec(); + if (res == QDialog::Rejected ) + { + return PlotDataMap(); + } - if (res == QDialog::Rejected ) - { - return PlotDataMap(); - } + // vector is supposed to have only one element + time_index = dialog->getSelectedRowNumber().at(0) -1; + load_configuration = field_names.at( time_index + 1 ) ; + } + //----------------- - time_index = dialog->getSelectedRowNumber().at(0) -1; // vector is supposed to have only one element - load_configuration = field_names.at( time_index + 1 ) ; - } + while (!inB.atEnd()) + { + QString line = inB.readLine(); - first_line = false; - } - else{ - double t = linecount; + QStringList string_items = line.split(','); + double t = linecount; - if( time_index >= 0) + if( time_index >= 0) + { + t = string_items[ time_index ].toDouble(); + if( t <= prev_time) { - t = string_items[ time_index].toDouble(); - if( t <= prev_time) - { - QMessageBox::StandardButton reply; - reply = QMessageBox::question(0, tr("Error reading file"), - tr("Selected time in notstrictly monotonic. Do you want to abort?\n" - "(Clicking \"NO\" you continue loading)") ); + QMessageBox::StandardButton reply; + reply = QMessageBox::question(0, tr("Error reading file"), + tr("Selected time in notstrictly monotonic. Do you want to abort?\n" + "(Clicking \"NO\" you continue loading)") ); - interrupted = (reply == QMessageBox::Yes); - break; - } - prev_time = t; + interrupted = (reply == QMessageBox::Yes); + break; } + prev_time = t; + } - int index = 0; - for (int i=0; i < string_items.size(); i++ ) + int index = 0; + for (int i=0; i < string_items.size(); i++ ) + { + if( ordered_names[i].first ) { - if( ordered_names[i].first ) - { - double y = string_items[i].toDouble(); - PlotData::Point point( t,y ); - plots_vector[index]->pushBack( point ); - index++; - } + double y = string_items[i].toDouble(); + PlotData::Point point( t,y ); + plots_vector[index]->pushBack( point ); + index++; } + } - if(linecount++ %100 == 0) - { - progress_dialog.setValue( linecount ); - QApplication::processEvents(); - if( progress_dialog.wasCanceled() ) { - interrupted = true; - break; - } + if(linecount++ %100 == 0) + { + progress_dialog.setValue( linecount ); + QApplication::processEvents(); + if( progress_dialog.wasCanceled() ) { + interrupted = true; + break; } } }