scidavis  2.3.0
About: SciDAVis is a free application for Scientific Data Analysis and Visualization (a fork off of QtiPlot).
  Fossies Dox: scidavis-2.3.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

ImportASCIIDialog.cpp
Go to the documentation of this file.
1 
14 
33 #include "ImportASCIIDialog.h"
34 #include "ApplicationWindow.h"
35 
36 #include <QGridLayout>
37 #include <QVBoxLayout>
38 #include <QHBoxLayout>
39 #include <QGroupBox>
40 #include <QPushButton>
41 #include <QRegExp>
42 #include <QMessageBox>
43 #include <QCloseEvent>
44 
45 ImportASCIIDialog::ImportASCIIDialog(bool import_mode_enabled, QWidget * parent, bool extended, Qt::WindowFlags flags )
46 : ExtensibleFileDialog(parent, extended, flags )
47 {
48  setWindowTitle(tr("Import ASCII File(s)"));
49 
50  QStringList filters;
51  filters << tr("All files") + " (*)";
52  filters << tr("Text files") + " (*.TXT *.txt)";
53  filters << tr("Data files") + " (*.DAT *.dat)";
54  filters << tr("Comma Separated Values") + " (*.CSV *.csv)";
55  setNameFilters( filters );
56 
57  setFileMode( QFileDialog::ExistingFiles );
58 
60  d_import_mode->setEnabled(import_mode_enabled);
62 
63  // get rembered option values
64  ApplicationWindow *app = (ApplicationWindow *)parent;
65  d_strip_spaces->setChecked(app->strip_spaces);
66  d_simplify_spaces->setChecked(app->simplify_spaces);
67  d_ignored_lines->setValue(app->ignoredLines);
68  d_rename_columns->setChecked(app->renameColumns);
70 
71  if (app->d_ASCII_import_locale.name() == QLocale::c().name())
72  boxDecimalSeparator->setCurrentIndex(1);
73  else if (app->d_ASCII_import_locale.name() == QLocale(QLocale::German).name())
74  boxDecimalSeparator->setCurrentIndex(2);
75  else if (app->d_ASCII_import_locale.name() == QLocale(QLocale::French).name())
76  boxDecimalSeparator->setCurrentIndex(3);
77  boxDecimalSeparator->setEnabled(app->d_convert_to_numeric);
79 
80  connect(d_import_mode, SIGNAL(currentIndexChanged(int)), this, SLOT(updateImportMode(int)));
81 }
82 
84 {
85  d_advanced_options = new QGroupBox();
86  QVBoxLayout *main_layout = new QVBoxLayout(d_advanced_options);
87  QGridLayout *advanced_layout = new QGridLayout();
88  main_layout->addLayout(advanced_layout);
89 
90  advanced_layout->addWidget(new QLabel(tr("Import each file as: ")), 0, 0);
91  d_import_mode = new QComboBox();
92  // Important: Keep this in sync with the ImportMode enum.
93  d_import_mode->addItem(tr("New Table"));
94  d_import_mode->addItem(tr("New Columns"));
95  d_import_mode->addItem(tr("New Rows"));
96  d_import_mode->addItem(tr("Overwrite Current Table"));
97  advanced_layout->addWidget(d_import_mode, 0, 1);
98 
99  QLabel *label_column_separator = new QLabel(tr("Separator:"));
100  advanced_layout->addWidget(label_column_separator, 1, 0);
101  d_column_separator = new QComboBox();
102  d_column_separator->addItem(tr("TAB"));
103  d_column_separator->addItem(tr("SPACE"));
104  d_column_separator->addItem(";" + tr("TAB"));
105  d_column_separator->addItem("," + tr("TAB"));
106  d_column_separator->addItem(";" + tr("SPACE"));
107  d_column_separator->addItem("," + tr("SPACE"));
108  d_column_separator->addItem(";");
109  d_column_separator->addItem(",");
110  d_column_separator->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
111  d_column_separator->setEditable( true );
112  advanced_layout->addWidget(d_column_separator, 1, 1);
113  // context-sensitive help
114  QString help_column_separator = tr("The column separator can be customized. \nThe following special codes can be used:\n\\t for a TAB character \n\\s for a SPACE");
115  help_column_separator += "\n"+tr("The separator must not contain the following characters: \n0-9eE.+-");
116  d_column_separator->setWhatsThis(help_column_separator);
117  label_column_separator->setToolTip(help_column_separator);
118  d_column_separator->setToolTip(help_column_separator);
119  label_column_separator->setWhatsThis(help_column_separator);
120 
121  QLabel *label_ignore_lines = new QLabel(tr( "Ignore first" ));
122  advanced_layout->addWidget(label_ignore_lines, 2, 0);
123  d_ignored_lines = new QSpinBox();
124  d_ignored_lines->setRange( 0, 10000 );
125  d_ignored_lines->setSuffix(" " + tr("lines"));
126  d_ignored_lines->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
127  advanced_layout->addWidget(d_ignored_lines, 2, 1);
128 
129  d_rename_columns = new QCheckBox(tr("Use first row to &name columns"));
130  advanced_layout->addWidget(d_rename_columns, 0, 2, 1, 2);
131 
132  d_strip_spaces = new QCheckBox(tr("&Remove white spaces from line ends"));
133  advanced_layout->addWidget(d_strip_spaces, 1, 2, 1, 2);
134  // context-sensitive help
135  QString help_strip_spaces = tr("By checking this option all white spaces will be \nremoved from the beginning and the end of \nthe lines in the ASCII file.","when translating this check the what's this functions and tool tips to place the '\\n's correctly");
136  help_strip_spaces +="\n\n"+tr("Warning: checking this option leads to column \noverlaping if the columns in the ASCII file don't \nhave the same number of rows.");
137  help_strip_spaces +="\n"+tr("To avoid this problem you should precisely \ndefine the column separator using TAB and \nSPACE characters.","when translating this check the what's this functions and tool tips to place the '\\n's correctly");
138  d_strip_spaces->setWhatsThis(help_strip_spaces);
139  d_strip_spaces->setToolTip(help_strip_spaces);
140 
141  d_simplify_spaces = new QCheckBox(tr("&Simplify white spaces" ));
142  advanced_layout->addWidget(d_simplify_spaces, 2, 2, 1, 2);
143  // context-sensitive help
144  QString help_simplify_spaces = tr("By checking this option all white spaces will be \nremoved from the beginning and the end of the \nlines and each sequence of internal \nwhitespaces (including the TAB character) will \nbe replaced with a single space.","when translating this check the what's this functions and tool tips to place the '\\n's correctly");
145  help_simplify_spaces +="\n\n"+tr("Warning: checking this option leads to column \noverlaping if the columns in the ASCII file don't \nhave the same number of rows.","when translating this check the what's this functions and tool tips to place the '\\n's correctly");
146  help_simplify_spaces +="\n"+tr("To avoid this problem you should precisely \ndefine the column separator using TAB and \nSPACE characters.","when translating this check the what's this functions and tool tips to place the '\\n's correctly");
147  d_simplify_spaces->setWhatsThis(help_simplify_spaces);
148  d_simplify_spaces->setToolTip(help_simplify_spaces);
149 
150  d_convert_to_numeric = new QCheckBox(tr("&Numeric data"));
151  advanced_layout->addWidget(d_convert_to_numeric, 3, 0, 1, 2);
152 
153  advanced_layout->addWidget(new QLabel(tr("Decimal Separators")), 3, 1);
154  boxDecimalSeparator = new QComboBox();
155  boxDecimalSeparator->addItem(tr("default") + " (" + QLocale::system().toString(1000.0, 'f', 1) +")");
156  boxDecimalSeparator->addItem(QLocale::c().toString(1000.0, 'f', 1));
157  boxDecimalSeparator->addItem(QLocale(QLocale::German).toString(1000.0, 'f', 1));
158  boxDecimalSeparator->addItem(QLocale(QLocale::French).toString(1000.0, 'f', 1));
159  connect(d_convert_to_numeric, SIGNAL(toggled(bool)), boxDecimalSeparator, SLOT(setEnabled(bool)));
160  advanced_layout->addWidget(boxDecimalSeparator, 3, 2);
161 
162  QHBoxLayout *meta_options_layout = new QHBoxLayout();
163  d_remember_options = new QCheckBox(tr("Re&member the above options"));
164  meta_options_layout->addWidget(d_remember_options);
165  d_help_button = new QPushButton(tr("&Help"));
166  connect(d_help_button, SIGNAL(clicked()), this, SLOT(displayHelp()));
167  meta_options_layout->addStretch();
168  meta_options_layout->addWidget(d_help_button);
169  main_layout->addLayout(meta_options_layout);
170 }
171 
173 {
174  if (sep=="\t")
175  d_column_separator->setCurrentIndex(0);
176  else if (sep==" ")
177  d_column_separator->setCurrentIndex(1);
178  else if (sep==";\t")
179  d_column_separator->setCurrentIndex(2);
180  else if (sep==",\t")
181  d_column_separator->setCurrentIndex(3);
182  else if (sep=="; ")
183  d_column_separator->setCurrentIndex(4);
184  else if (sep==", ")
185  d_column_separator->setCurrentIndex(5);
186  else if (sep==";")
187  d_column_separator->setCurrentIndex(6);
188  else if (sep==",")
189  d_column_separator->setCurrentIndex(7);
190  else
191  {
192  QString separator = sep;
193  d_column_separator->setEditText(separator.replace(" ","\\s").replace("\t","\\t"));
194  }
195 }
196 
198 {
199  QString sep = d_column_separator->currentText();
200 
201  if (d_simplify_spaces->isChecked())
202  sep.replace(tr("TAB"), " ", Qt::CaseInsensitive);
203  else
204  sep.replace(tr("TAB"), "\t", Qt::CaseInsensitive);
205 
206  sep.replace(tr("SPACE"), " ", Qt::CaseInsensitive);
207  sep.replace("\\s", " ");
208  sep.replace("\\t", "\t");
209 
210  /* TODO
211  if (sep.contains(QRegExp("[0-9.eE+-]")))
212  QMessageBox::warning(this, tr("Import options error"),
213  tr("The separator must not contain the following characters: 0-9eE.+-"));
214  */
215 
216  return sep;
217 }
218 
220 {
221  QString s = tr("The column separator can be customized. The following special codes can be used:\n\\t for a TAB character \n\\s for a SPACE");
222  s += "\n"+tr("The separator must not contain the following characters: 0-9eE.+-") + "\n\n";
223  s += tr( "Remove white spaces from line ends" )+ ":\n";
224  s += tr("By checking this option all white spaces will be removed from the beginning and the end of the lines in the ASCII file.") + "\n\n";
225  s += tr( "Simplify white spaces" )+ ":\n";
226  s += tr("By checking this option each sequence of internal whitespaces (including the TAB character) will be replaced with a single space.");
227  s += tr("By checking this option all white spaces will be removed from the beginning and the end of the lines and each sequence of internal whitespaces (including the TAB character) will be replaced with a single space.");
228 
229  s +="\n\n"+tr("Warning: using these two last options leads to column overlaping if the columns in the ASCII file don't have the same number of rows.");
230  s +="\n"+tr("To avoid this problem you should precisely define the column separator using TAB and SPACE characters.");
231 
232  QMessageBox::about(this, tr("Help"), s);
233 }
234 
236 {
237  if (mode == Overwrite)
238  setFileMode( QFileDialog::ExistingFile );
239  else
240  setFileMode( QFileDialog::ExistingFiles );
241 }
242 
243 void ImportASCIIDialog::closeEvent(QCloseEvent* e)
244 {
245  ApplicationWindow *app = (ApplicationWindow *)this->parent();
246  if (app){
248  app->d_ASCII_file_filter = this->selectedNameFilter();
249  }
250 
251  e->accept();
252 }
253 
255 {
256  QLocale locale;
257  switch (boxDecimalSeparator->currentIndex()){
258  case 0:
259  locale = QLocale::system();
260  break;
261  case 1:
262  locale = QLocale::c();
263  break;
264  case 2:
265  locale = QLocale(QLocale::German);
266  break;
267  case 3:
268  locale = QLocale(QLocale::French);
269  break;
270  }
271  return locale;
272 }
ImportASCIIDialog::d_ignored_lines
QSpinBox * d_ignored_lines
Definition: ImportASCIIDialog.h:112
ApplicationWindow::ignoredLines
int ignoredLines
Definition: ApplicationWindow.h:993
ApplicationWindow::d_ASCII_file_filter
QString d_ASCII_file_filter
Last selected filter in import ASCII dialog.
Definition: ApplicationWindow.h:922
ImportASCIIDialog::updateImportMode
void updateImportMode(int mode)
For Overwrite mode, allow only one file to be selected.
Definition: ImportASCIIDialog.cpp:235
ApplicationWindow::d_convert_to_numeric
bool d_convert_to_numeric
Definition: ApplicationWindow.h:923
ImportASCIIDialog::d_rename_columns
QCheckBox * d_rename_columns
Definition: ImportASCIIDialog.h:113
ImportASCIIDialog::d_simplify_spaces
QCheckBox * d_simplify_spaces
Definition: ImportASCIIDialog.h:113
toString
S toString(const QString &x)
Deal with conversion between QString and std::string/std::wstring in a generic way.
ImportASCIIDialog::columnSeparator
const QString columnSeparator() const
Return the selected column separator.
Definition: ImportASCIIDialog.cpp:197
ApplicationWindow::renameColumns
bool renameColumns
Definition: ApplicationWindow.h:994
ExtensibleFileDialog::setExtensionWidget
void setExtensionWidget(QWidget *extension)
Set the extension widget to be displayed when the user presses the toggle button.
Definition: ExtensibleFileDialog.cpp:66
ImportASCIIDialog::d_import_mode
QComboBox * d_import_mode
Definition: ImportASCIIDialog.h:111
ImportASCIIDialog::d_help_button
QPushButton * d_help_button
Definition: ImportASCIIDialog.h:109
python-sipcmd.flags
list flags
Definition: python-sipcmd.py:61
ExtensibleFileDialog
QFileDialog plus generic extension support.
Definition: ExtensibleFileDialog.h:45
ApplicationWindow
SciDAVis's main window.
Definition: ApplicationWindow.h:122
ImportASCIIDialog::d_advanced_options
QGroupBox * d_advanced_options
Container widget for all advanced options.
Definition: ImportASCIIDialog.h:107
ImportASCIIDialog::boxDecimalSeparator
QComboBox * boxDecimalSeparator
Definition: ImportASCIIDialog.h:111
ApplicationWindow::d_extended_import_ASCII_dialog
bool d_extended_import_ASCII_dialog
Definition: ApplicationWindow.h:929
ImportASCIIDialog::d_remember_options
QCheckBox * d_remember_options
Definition: ImportASCIIDialog.h:108
ApplicationWindow::columnSeparator
QString columnSeparator
Definition: ApplicationWindow.h:991
ImportASCIIDialog::d_column_separator
QComboBox * d_column_separator
Definition: ImportASCIIDialog.h:111
ExtensibleFileDialog::isExtended
bool isExtended()
Definition: ExtensibleFileDialog.h:61
ApplicationWindow::simplify_spaces
bool simplify_spaces
Definition: ApplicationWindow.h:994
name
char * name()
Definition: exp_saturation.c:45
ImportASCIIDialog.h
ImportASCIIDialog::decimalSeparators
QLocale decimalSeparators()
Returns the locale chosen for conversion to numeric format.
Definition: ImportASCIIDialog.cpp:254
ApplicationWindow::d_ASCII_import_locale
QLocale d_ASCII_import_locale
Locale used to specify the decimal separators in imported ASCII files.
Definition: ApplicationWindow.h:920
ImportASCIIDialog::displayHelp
void displayHelp()
Display help for advanced options.
Definition: ImportASCIIDialog.cpp:219
ImportASCIIDialog::initAdvancedOptions
void initAdvancedOptions()
Initialise d_advanced_options and everything it contains.
Definition: ImportASCIIDialog.cpp:83
ImportASCIIDialog::setColumnSeparator
void setColumnSeparator(const QString &sep)
Set the selected columns separator.
Definition: ImportASCIIDialog.cpp:172
ApplicationWindow.h
ImportASCIIDialog::ImportASCIIDialog
ImportASCIIDialog(bool importModeEnabled, QWidget *parent=0, bool extended=true, Qt::WindowFlags flags=0)
Constructor.
Definition: ImportASCIIDialog.cpp:45
ImportASCIIDialog::closeEvent
void closeEvent(QCloseEvent *)
Definition: ImportASCIIDialog.cpp:243
ImportASCIIDialog::d_strip_spaces
QCheckBox * d_strip_spaces
Definition: ImportASCIIDialog.h:113
ImportASCIIDialog::d_convert_to_numeric
QCheckBox * d_convert_to_numeric
Definition: ImportASCIIDialog.h:108
ApplicationWindow::strip_spaces
bool strip_spaces
Definition: ApplicationWindow.h:994
ImportASCIIDialog::Overwrite
@ Overwrite
replace content of current table with the selected file (like importing a single file in previous ver...
Definition: ImportASCIIDialog.h:57