Skip to content

Commit

Permalink
fix for issue #46 (load csv files)
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Jun 19, 2017
1 parent 007776f commit a738e78
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 124 deletions.
2 changes: 0 additions & 2 deletions include/PlotJuggler/dataloader_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <functional>
#include "PlotJuggler/plotdata.h"

enum { TIME_INDEX_NOT_DEFINED = -2 };

class DataLoader{

public:
Expand Down
44 changes: 0 additions & 44 deletions plugins/DataLoadCSV/DataLoadCSV.pro

This file was deleted.

155 changes: 77 additions & 78 deletions plugins/DataLoadCSV/dataload_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit a738e78

Please sign in to comment.