dataload_csv.cpp
Go to the documentation of this file.
00001 #include "dataload_csv.h"
00002 #include <QTextStream>
00003 #include <QFile>
00004 #include <QMessageBox>
00005 #include <QDebug>
00006 #include <QProgressDialog>
00007 #include "selectlistdialog.h"
00008 
00009 DataLoadCSV::DataLoadCSV()
00010 {
00011     _extensions.push_back( "csv");
00012 }
00013 
00014 const QRegExp csv_separator("(\\,|\\;|\\ |\\t|\\|)");
00015 
00016 const std::vector<const char*> &DataLoadCSV::compatibleFileExtensions() const
00017 {
00018     return _extensions;
00019 }
00020 
00021 int DataLoadCSV::parseHeader(QFile *file,
00022                              std::vector<std::pair<bool,QString> >& ordered_names)
00023 {
00024     QTextStream inA(file);
00025 
00026     QString first_line = inA.readLine();
00027     QString second_line = inA.readLine();
00028 
00029     int linecount = 1;
00030 
00031     QStringList string_items = first_line.split(csv_separator);
00032     QStringList secondline_items = second_line.split(csv_separator);
00033 
00034     for (int i=0; i < string_items.size(); i++ )
00035     {
00036         // remove annoying prefix
00037         QStringRef field_name ( &string_items[i] );
00038         if( field_name.startsWith( "field." ) )
00039         {
00040             field_name = field_name.mid(6);
00041         }
00042 
00043         QString qname = field_name.toString();
00044         if( qname.isEmpty())
00045         {
00046             qname = QString("_Column_%1").arg(i);
00047         }
00048         ordered_names.push_back( std::make_pair(true,qname) );
00049     }
00050 
00051     for (unsigned i=0; i < ordered_names.size(); i++ )
00052     {
00053         QString field_name ( ordered_names[i].second );
00054         if( field_name.endsWith( ".name" ) && i < ordered_names.size()-1)
00055         {
00056             QString prefix =  field_name.left(field_name.size() - 5 );
00057             QString replace_name = secondline_items[i];
00058 
00059             ordered_names[i].first = false;
00060             i++;
00061             //----------------------------------------
00062 
00063             QString value_prefix =  prefix + ".value";
00064 
00065             if( value_prefix.contains("vectors3d") )
00066             {
00067                 ordered_names[i].second =  prefix + "/" + replace_name + "/x";
00068                 i++;
00069                 ordered_names[i].second =  prefix + "/" + replace_name + "/y";
00070                 i++;
00071                 ordered_names[i].second =  prefix + "/" + replace_name + "/z";
00072             }
00073             else
00074             {
00075                 while( i < ordered_names.size() &&
00076                        ordered_names[i].second.startsWith( value_prefix ) )
00077                 {
00078                     QString name = ordered_names[i].second;
00079                     QString suffix = name.right( name.size() - value_prefix.size());
00080                     ordered_names[i].second = prefix + "/" + replace_name;
00081                     if( suffix.size() > 0)
00082                     {
00083                         ordered_names[i].second.append( "/" + suffix );
00084                     }
00085                     i++;
00086                 }
00087                 i--;
00088             }
00089         }
00090     }
00091 
00092     while (!inA.atEnd())
00093     {
00094         inA.readLine();
00095         linecount++;
00096     }
00097 
00098     return linecount;
00099 }
00100 
00101 PlotDataMap DataLoadCSV::readDataFromFile(const QString &file_name,
00102                                           QString &load_configuration )
00103 {
00104     const int TIME_INDEX_NOT_DEFINED = -2;
00105 
00106     int time_index = TIME_INDEX_NOT_DEFINED;
00107 
00108     PlotDataMap plot_data;
00109 
00110     QFile file( file_name );
00111     file.open(QFile::ReadOnly);
00112 
00113     std::vector<std::pair<bool, QString> > ordered_names;
00114 
00115     int linecount = parseHeader( &file, ordered_names);
00116 
00117     file.close();
00118     file.open(QFile::ReadOnly);
00119     QTextStream inB( &file );
00120 
00121     std::vector<PlotDataPtr> plots_vector;
00122 
00123     bool interrupted = false;
00124 
00125     int tot_lines = linecount -1;
00126     linecount = 0;
00127 
00128     QProgressDialog progress_dialog;
00129     progress_dialog.setLabelText("Loading... please wait");
00130     progress_dialog.setWindowModality( Qt::ApplicationModal );
00131     progress_dialog.setRange(0, tot_lines -1);
00132     progress_dialog.setAutoClose( true );
00133     progress_dialog.setAutoReset( true );
00134     progress_dialog.show();
00135 
00136     double prev_time = -1;
00137 
00138     // remove first line (header
00139     inB.readLine();
00140 
00141     //---- build plots_vector from header  ------
00142     QStringList valid_field_names;
00143 
00144     for (unsigned i=0; i < ordered_names.size(); i++ )
00145     {
00146         bool valid = ordered_names[i].first;
00147         if( valid )
00148         {
00149             QString&   qname = ( ordered_names[i].second );
00150             std::string name = qname.toStdString();
00151 
00152             PlotDataPtr plot( new PlotData(name.c_str()) );
00153             plot_data.numeric.insert( std::make_pair( name, plot ) );
00154 
00155             valid_field_names.push_back( qname );
00156             plots_vector.push_back( plot );
00157 
00158             if (time_index == TIME_INDEX_NOT_DEFINED)
00159             {
00160                 if( load_configuration == qname )
00161                 {
00162                     time_index = valid_field_names.size() ;
00163                 }
00164             }
00165         }
00166     }
00167 
00168     if( time_index == TIME_INDEX_NOT_DEFINED)
00169     {
00170         QStringList field_names;
00171         field_names.push_back( "INDEX (auto-generated)" );
00172         field_names.append( valid_field_names );
00173 
00174         SelectFromListDialog* dialog = new SelectFromListDialog( &field_names );
00175         dialog->setWindowTitle("Select the time axis");
00176         int res = dialog->exec();
00177 
00178         if (res == QDialog::Rejected )
00179         {
00180             return PlotDataMap();
00181         }
00182 
00183         // vector is supposed to have only one element
00184         time_index = dialog->getSelectedRowNumber().at(0) -1;
00185         load_configuration = field_names.at( time_index + 1 ) ;
00186     }
00187     //-----------------
00188 
00189     while (!inB.atEnd())
00190     {
00191         QString line = inB.readLine();
00192 
00193         QStringList string_items = line.split(csv_separator);
00194         double t = linecount;
00195 
00196         if( time_index >= 0)
00197         {
00198             t = string_items[ time_index ].toDouble();
00199             if( t <= prev_time)
00200             {
00201                 QMessageBox::StandardButton reply;
00202                 reply = QMessageBox::question(0, tr("Error reading file"),
00203                                               tr("Selected time in notstrictly  monotonic. Do you want to abort?\n"
00204                                                  "(Clicking \"NO\" you continue loading)") );
00205 
00206                 interrupted = (reply == QMessageBox::Yes);
00207                 break;
00208             }
00209             prev_time = t;
00210         }
00211 
00212         int index = 0;
00213         for (int i=0; i < string_items.size(); i++ )
00214         {
00215             if( ordered_names[i].first )
00216             {
00217                 double y = string_items[i].toDouble();
00218                 PlotData::Point point( t,y );
00219                 plots_vector[index]->pushBack( point );
00220                 index++;
00221             }
00222         }
00223 
00224         if(linecount++ %100 == 0)
00225         {
00226             progress_dialog.setValue( linecount );
00227             QApplication::processEvents();
00228             if( progress_dialog.wasCanceled() ) {
00229                 interrupted = true;
00230                 break;
00231             }
00232         }
00233     }
00234     file.close();
00235 
00236     if(interrupted)
00237     {
00238         progress_dialog.cancel();
00239         plot_data.numeric.erase( plot_data.numeric.begin(), plot_data.numeric.end() );
00240     }
00241 
00242     return plot_data;
00243 }
00244 
00245 
00246 
00247 DataLoadCSV::~DataLoadCSV()
00248 {
00249 
00250 }


plotjuggler
Author(s): Davide Faconti
autogenerated on Fri Sep 1 2017 02:41:56