1
/***************************************************************************
2
File : ImportASCIIDialog.cpp
4
--------------------------------------------------------------------
5
Copyright : (C) 2006,2007 by Ion Vasilief, Knut Franke
6
Email (use @ for *) : ion_vasilief*yahoo.fr, knut.franke*gmx.de
7
Description : Import ASCII file(s) dialog
9
***************************************************************************/
12
/***************************************************************************
14
* This program is free software; you can redistribute it and/or modify *
15
* it under the terms of the GNU General Public License as published by *
16
* the Free Software Foundation; either version 2 of the License, or *
17
* (at your option) any later version. *
19
* This program is distributed in the hope that it will be useful, *
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22
* GNU General Public License for more details. *
24
* You should have received a copy of the GNU General Public License *
25
* along with this program; if not, write to the Free Software *
26
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27
* Boston, MA 02110-1301 USA *
29
***************************************************************************/
31
#include "ImportASCIIDialog.h"
32
#include "ApplicationWindow.h"
35
#include <QGridLayout>
36
#include <QVBoxLayout>
37
#include <QHBoxLayout>
1
/***************************************************************************
2
File : ImportASCIIDialog.cpp
4
--------------------------------------------------------------------
5
Copyright : (C) 2006,2007 by Ion Vasilief, Knut Franke
6
Email (use @ for *) : ion_vasilief*yahoo.fr, knut.franke*gmx.de
7
Description : Import ASCII file(s) dialog
9
***************************************************************************/
12
/***************************************************************************
14
* This program is free software; you can redistribute it and/or modify *
15
* it under the terms of the GNU General Public License as published by *
16
* the Free Software Foundation; either version 2 of the License, or *
17
* (at your option) any later version. *
19
* This program is distributed in the hope that it will be useful, *
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22
* GNU General Public License for more details. *
24
* You should have received a copy of the GNU General Public License *
25
* along with this program; if not, write to the Free Software *
26
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27
* Boston, MA 02110-1301 USA *
29
***************************************************************************/
31
#include "ImportASCIIDialog.h"
32
#include "ApplicationWindow.h"
35
#include "MatrixModel.h"
38
38
#include <QGroupBox>
39
#include <QPushButton>
41
#include <QMessageBox>
42
#include <QTemporaryFile>
43
#include <QTextStream>
44
#include <Q3TextStream>
46
ImportASCIIDialog::ImportASCIIDialog(bool import_mode_enabled, QWidget * parent, bool extended, Qt::WFlags flags )
47
: ExtensibleFileDialog(parent, extended, flags )
49
setWindowTitle(tr("QtiPlot - Import ASCII File(s)"));
52
filters << tr("All files") + " (*)";
53
filters << tr("Text files") + " (*.TXT *.txt)";
54
filters << tr("Data files") + " (*.DAT *.dat)";
55
filters << tr("Comma Separated Values") + " (*.CSV *.csv)";
56
setFilters( filters );
58
setFileMode( QFileDialog::ExistingFiles );
60
d_current_path = QString::null;
62
initAdvancedOptions();
63
d_import_mode->setEnabled(import_mode_enabled);
64
setExtensionWidget(d_advanced_options);
66
// get rembered option values
67
ApplicationWindow *app = (ApplicationWindow *)parent;
68
d_strip_spaces->setChecked(app->strip_spaces);
69
d_simplify_spaces->setChecked(app->simplify_spaces);
70
d_ignored_lines->setValue(app->ignoredLines);
71
d_rename_columns->setChecked(app->renameColumns);
39
#include <QPushButton>
41
#include <QMessageBox>
42
#include <QTextStream>
43
#include <QApplication>
44
#include <QProgressDialog>
45
#include <QStackedWidget>
46
#include <QHeaderView>
48
#include <gsl/gsl_math.h>
50
ImportASCIIDialog::ImportASCIIDialog(bool new_windows_only, QWidget * parent, bool extended, Qt::WFlags flags )
51
: ExtensibleFileDialog(parent, extended, flags )
53
setWindowTitle(tr("QtiPlot - Import ASCII File(s)"));
56
filters << tr("All files") + " (*)";
57
filters << tr("Text files") + " (*.TXT *.txt)";
58
filters << tr("Data files") + " (*.DAT *.dat)";
59
filters << tr("Comma Separated Values") + " (*.CSV *.csv)";
60
setFilters( filters );
62
setFileMode( QFileDialog::ExistingFiles );
64
d_current_path = QString::null;
66
initAdvancedOptions();
67
setNewWindowsOnly(new_windows_only);
68
setExtensionWidget(d_advanced_options);
70
// get rembered option values
71
ApplicationWindow *app = (ApplicationWindow *)parent;
72
setLocale(app->locale());
74
d_strip_spaces->setChecked(app->strip_spaces);
75
d_simplify_spaces->setChecked(app->simplify_spaces);
76
d_ignored_lines->setValue(app->ignoredLines);
77
d_rename_columns->setChecked(app->renameColumns);
72
78
setColumnSeparator(app->columnSeparator);
73
79
d_comment_string->setText(app->d_ASCII_comment_string);
74
80
d_import_comments->setChecked(app->d_ASCII_import_comments);
75
81
d_read_only->setChecked(app->d_ASCII_import_read_only);
77
if (app->d_ASCII_import_locale.name() == QLocale::c().name())
78
boxDecimalSeparator->setCurrentIndex(1);
79
else if (app->d_ASCII_import_locale.name() == QLocale(QLocale::German).name())
80
boxDecimalSeparator->setCurrentIndex(2);
81
else if (app->d_ASCII_import_locale.name() == QLocale(QLocale::French).name())
82
boxDecimalSeparator->setCurrentIndex(3);
83
boxDecimalSeparator->setEnabled(app->d_import_dec_separators);
84
d_import_dec_separators->setChecked(app->d_import_dec_separators);
86
connect(d_import_mode, SIGNAL(currentIndexChanged(int)), this, SLOT(updateImportMode(int)));
87
if (import_mode_enabled)
88
d_import_mode->setCurrentIndex(app->d_ASCII_import_mode);
89
d_preview_lines_box->setValue(app->d_preview_lines);
90
d_preview_button->setChecked(app->d_ASCII_import_preview);
91
if (!app->d_ASCII_import_preview)
92
d_preview_table->hide();
83
if (app->d_ASCII_import_locale.name() == QLocale::c().name())
84
boxDecimalSeparator->setCurrentIndex(1);
85
else if (app->d_ASCII_import_locale.name() == QLocale(QLocale::German).name())
86
boxDecimalSeparator->setCurrentIndex(2);
87
else if (app->d_ASCII_import_locale.name() == QLocale(QLocale::French).name())
88
boxDecimalSeparator->setCurrentIndex(3);
89
boxDecimalSeparator->setEnabled(app->d_import_dec_separators);
90
d_import_dec_separators->setChecked(app->d_import_dec_separators);
92
connect(d_import_mode, SIGNAL(currentIndexChanged(int)), this, SLOT(updateImportMode(int)));
94
if (app->d_ASCII_import_mode < d_import_mode->count())
95
d_import_mode->setCurrentIndex(app->d_ASCII_import_mode);
97
d_preview_lines_box->setValue(app->d_preview_lines);
98
d_preview_button->setChecked(app->d_ASCII_import_preview);
100
boxEndLine->setCurrentIndex((int)app->d_ASCII_end_line);
102
if (!app->d_ASCII_import_preview)
103
d_preview_stack->hide();
105
initPreview(d_import_mode->currentIndex());
94
107
connect(d_preview_lines_box, SIGNAL(valueChanged(int)), this, SLOT(preview()));
95
108
connect(d_rename_columns, SIGNAL(clicked()), this, SLOT(preview()));
101
114
connect(d_column_separator, SIGNAL(currentIndexChanged(int)), this, SLOT(preview()));
102
115
connect(boxDecimalSeparator, SIGNAL(currentIndexChanged(int)), this, SLOT(preview()));
103
116
connect(d_comment_string, SIGNAL(textChanged(const QString&)), this, SLOT(preview()));
104
connect(this, SIGNAL(currentChanged(const QString&)), this, SLOT(changePreviewFile(const QString&)));
107
void ImportASCIIDialog::initAdvancedOptions()
109
d_advanced_options = new QGroupBox();
110
QVBoxLayout *main_layout = new QVBoxLayout(d_advanced_options);
111
QGridLayout *advanced_layout = new QGridLayout();
112
main_layout->addLayout(advanced_layout);
114
advanced_layout->addWidget(new QLabel(tr("Import each file as: ")), 0, 0);
115
d_import_mode = new QComboBox();
116
// Important: Keep this in sync with the ImportMode enum.
117
d_import_mode->addItem(tr("New Table"));
118
d_import_mode->addItem(tr("New Columns"));
119
d_import_mode->addItem(tr("New Rows"));
120
d_import_mode->addItem(tr("Overwrite Current Table"));
121
advanced_layout->addWidget(d_import_mode, 0, 1);
123
QLabel *label_column_separator = new QLabel(tr("Separator:"));
124
advanced_layout->addWidget(label_column_separator, 1, 0);
125
d_column_separator = new QComboBox();
126
d_column_separator->addItem(tr("TAB"));
127
d_column_separator->addItem(tr("SPACE"));
128
d_column_separator->addItem(";" + tr("TAB"));
129
d_column_separator->addItem("," + tr("TAB"));
130
d_column_separator->addItem(";" + tr("SPACE"));
131
d_column_separator->addItem("," + tr("SPACE"));
132
d_column_separator->addItem(";");
133
d_column_separator->addItem(",");
134
d_column_separator->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
135
d_column_separator->setEditable( true );
136
advanced_layout->addWidget(d_column_separator, 1, 1);
137
// context-sensitive help
138
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");
139
help_column_separator += "\n"+tr("The separator must not contain the following characters: \n0-9eE.+-");
140
d_column_separator->setWhatsThis(help_column_separator);
141
label_column_separator->setToolTip(help_column_separator);
142
d_column_separator->setToolTip(help_column_separator);
143
label_column_separator->setWhatsThis(help_column_separator);
145
QLabel *label_ignore_lines = new QLabel(tr( "Ignore first" ));
146
advanced_layout->addWidget(label_ignore_lines, 2, 0);
147
d_ignored_lines = new QSpinBox();
148
d_ignored_lines->setRange( 0, 10000 );
149
d_ignored_lines->setSuffix(" " + tr("lines"));
150
d_ignored_lines->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
117
connect(this, SIGNAL(currentChanged(const QString&)), this, SLOT(changePreviewFile(const QString&)));
120
void ImportASCIIDialog::initAdvancedOptions()
122
d_advanced_options = new QGroupBox();
123
QVBoxLayout *main_layout = new QVBoxLayout(d_advanced_options);
124
QGridLayout *advanced_layout = new QGridLayout();
125
main_layout->addLayout(advanced_layout);
127
advanced_layout->addWidget(new QLabel(tr("Import each file as: ")), 0, 0);
128
d_import_mode = new QComboBox();
129
// Important: Keep this in sync with the ImportMode enum.
130
d_import_mode->addItem(tr("New Table"));
131
d_import_mode->addItem(tr("New Matrice"));
132
d_import_mode->addItem(tr("New Columns"));
133
d_import_mode->addItem(tr("New Rows"));
134
d_import_mode->addItem(tr("Overwrite Current Window"));
135
advanced_layout->addWidget(d_import_mode, 0, 1);
137
QLabel *label_column_separator = new QLabel(tr("Separator:"));
138
advanced_layout->addWidget(label_column_separator, 1, 0);
139
d_column_separator = new QComboBox();
140
d_column_separator->addItem(tr("TAB"));
141
d_column_separator->addItem(tr("SPACE"));
142
d_column_separator->addItem(";" + tr("TAB"));
143
d_column_separator->addItem("," + tr("TAB"));
144
d_column_separator->addItem(";" + tr("SPACE"));
145
d_column_separator->addItem("," + tr("SPACE"));
146
d_column_separator->addItem(";");
147
d_column_separator->addItem(",");
148
d_column_separator->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
149
d_column_separator->setEditable( true );
150
advanced_layout->addWidget(d_column_separator, 1, 1);
151
// context-sensitive help
152
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");
153
help_column_separator += "\n"+tr("The separator must not contain the following characters: \n0-9eE.+-");
154
d_column_separator->setWhatsThis(help_column_separator);
155
label_column_separator->setToolTip(help_column_separator);
156
d_column_separator->setToolTip(help_column_separator);
157
label_column_separator->setWhatsThis(help_column_separator);
159
QLabel *label_ignore_lines = new QLabel(tr( "Ignore first" ));
160
advanced_layout->addWidget(label_ignore_lines, 2, 0);
161
d_ignored_lines = new QSpinBox();
162
d_ignored_lines->setRange( 0, 10000 );
163
d_ignored_lines->setSuffix(" " + tr("lines"));
164
d_ignored_lines->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
151
165
advanced_layout->addWidget(d_ignored_lines, 2, 1);
153
167
advanced_layout->addWidget(new QLabel(tr("Ignore lines starting with")), 3, 0);
154
d_comment_string = new QLineEdit();
168
d_comment_string = new QLineEdit();
155
169
advanced_layout->addWidget(d_comment_string, 3, 1);
157
d_rename_columns = new QCheckBox(tr("Use first row to &name columns"));
158
advanced_layout->addWidget(d_rename_columns, 0, 2, 1, 2);
171
d_rename_columns = new QCheckBox(tr("Use first row to &name columns"));
172
advanced_layout->addWidget(d_rename_columns, 0, 2, 1, 2);
160
174
d_import_comments = new QCheckBox(tr("Use second row as &comments"));
161
175
advanced_layout->addWidget(d_import_comments, 1, 2, 1, 2);
163
d_strip_spaces = new QCheckBox(tr("&Remove white spaces from line ends"));
164
advanced_layout->addWidget(d_strip_spaces, 2, 2, 1, 2);
165
// context-sensitive help
166
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");
167
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.");
168
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");
169
d_strip_spaces->setWhatsThis(help_strip_spaces);
170
d_strip_spaces->setToolTip(help_strip_spaces);
172
d_simplify_spaces = new QCheckBox(tr("&Simplify white spaces" ));
173
advanced_layout->addWidget(d_simplify_spaces, 3, 2, 1, 2);
174
// context-sensitive help
175
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");
176
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");
177
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");
178
d_simplify_spaces->setWhatsThis(help_simplify_spaces);
179
d_simplify_spaces->setToolTip(help_simplify_spaces);
181
advanced_layout->addWidget(new QLabel(tr("Decimal Separators")), 4, 0);
182
boxDecimalSeparator = new QComboBox();
183
boxDecimalSeparator->addItem(tr("System Locale Setting"));
184
boxDecimalSeparator->addItem("1,000.0");
185
boxDecimalSeparator->addItem("1.000,0");
186
boxDecimalSeparator->addItem("1 000,0");
187
advanced_layout->addWidget(boxDecimalSeparator, 4, 1);
189
d_import_dec_separators = new QCheckBox(tr("Import &decimal separators"));
190
connect(d_import_dec_separators, SIGNAL(toggled(bool)), boxDecimalSeparator, SLOT(setEnabled(bool)));
191
advanced_layout->addWidget(d_import_dec_separators, 4, 2, 1, 2);
176
connect(d_rename_columns, SIGNAL(toggled(bool)), d_import_comments, SLOT(setEnabled(bool)));
178
d_strip_spaces = new QCheckBox(tr("&Remove white spaces from line ends"));
179
advanced_layout->addWidget(d_strip_spaces, 2, 2, 1, 2);
180
// context-sensitive help
181
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");
182
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.");
183
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");
184
d_strip_spaces->setWhatsThis(help_strip_spaces);
185
d_strip_spaces->setToolTip(help_strip_spaces);
187
d_simplify_spaces = new QCheckBox(tr("&Simplify white spaces" ));
188
advanced_layout->addWidget(d_simplify_spaces, 3, 2, 1, 2);
189
// context-sensitive help
190
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");
191
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");
192
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");
193
d_simplify_spaces->setWhatsThis(help_simplify_spaces);
194
d_simplify_spaces->setToolTip(help_simplify_spaces);
196
advanced_layout->addWidget(new QLabel(tr("Decimal Separators")), 4, 0);
197
boxDecimalSeparator = new QComboBox();
198
boxDecimalSeparator->addItem(tr("System Locale Setting"));
199
boxDecimalSeparator->addItem("1,000.0");
200
boxDecimalSeparator->addItem("1.000,0");
201
boxDecimalSeparator->addItem("1 000,0");
202
advanced_layout->addWidget(boxDecimalSeparator, 4, 1);
204
d_import_dec_separators = new QCheckBox(tr("Import &decimal separators"));
205
connect(d_import_dec_separators, SIGNAL(toggled(bool)), boxDecimalSeparator, SLOT(setEnabled(bool)));
206
advanced_layout->addWidget(d_import_dec_separators, 4, 2, 1, 2);
208
advanced_layout->addWidget(new QLabel(tr("Endline character")), 5, 0);
209
boxEndLine = new QComboBox();
210
boxEndLine->addItem(tr("LF (Unix)"));
211
boxEndLine->addItem(tr("CRLF (Windows)"));
212
boxEndLine->addItem(tr("CR (Mac)"));
213
connect(boxEndLine, SIGNAL(activated(int)), this, SLOT(preview()));
214
advanced_layout->addWidget(boxEndLine, 5, 1);
216
d_read_only = new QCheckBox(tr("Import as &read-only"));
217
advanced_layout->addWidget(d_read_only, 5, 2);
193
219
d_preview_button = new QCheckBox(tr("&Preview Lines"));
194
220
connect(d_preview_button, SIGNAL(clicked()), this, SLOT(preview()));
195
advanced_layout->addWidget(d_preview_button, 5, 0);
221
advanced_layout->addWidget(d_preview_button, 6, 0);
197
223
d_preview_lines_box = new QSpinBox();
198
224
d_preview_lines_box->setMaximum (INT_MAX);
199
225
d_preview_lines_box->setValue(100);
200
226
d_preview_lines_box->setSingleStep(10);
201
227
d_preview_lines_box->setSpecialValueText(tr("All"));
202
advanced_layout->addWidget(d_preview_lines_box, 5, 1);
204
d_read_only = new QCheckBox(tr("Import as &read-only"));
205
advanced_layout->addWidget(d_read_only, 5, 2);
228
advanced_layout->addWidget(d_preview_lines_box, 6, 1);
207
230
d_help_button = new QPushButton(tr("&Help"));
208
231
connect(d_help_button, SIGNAL(clicked()), this, SLOT(displayHelp()));
209
advanced_layout->addWidget(d_help_button, 5, 3);
211
d_preview_table = new Table(ScriptingLangManager::newEnv((ApplicationWindow *)parent()), 30, 2, tr("Preview"), 0, 0);
212
d_preview_table->setAttribute(Qt::WA_DeleteOnClose);
213
#if defined(Q_WS_MAC)//displaying comments on Mac leads to a crash
214
d_preview_table->showComments(false);
215
int height = d_preview_table->table()->horizontalHeader()->height();
216
d_preview_table->setMinimumHeight(4*height);
218
d_preview_table->showComments();
219
int height = d_preview_table->table()->horizontalHeader()->height();
220
d_preview_table->setMinimumHeight(2*height);
222
main_layout->addWidget(d_preview_table);
225
void ImportASCIIDialog::setColumnSeparator(const QString& sep)
228
d_column_separator->setCurrentIndex(0);
230
d_column_separator->setCurrentIndex(1);
232
d_column_separator->setCurrentIndex(2);
234
d_column_separator->setCurrentIndex(3);
236
d_column_separator->setCurrentIndex(4);
238
d_column_separator->setCurrentIndex(5);
240
d_column_separator->setCurrentIndex(6);
242
d_column_separator->setCurrentIndex(7);
244
QString separator = sep;
245
d_column_separator->setEditText(separator.replace(" ","\\s").replace("\t","\\t"));
249
const QString ImportASCIIDialog::columnSeparator() const
251
QString sep = d_column_separator->currentText();
253
if (d_simplify_spaces->isChecked())
254
sep.replace(tr("TAB"), " ", Qt::CaseInsensitive);
256
sep.replace(tr("TAB"), "\t", Qt::CaseInsensitive);
258
sep.replace(tr("SPACE"), " ", Qt::CaseInsensitive);
259
sep.replace("\\s", " ");
260
sep.replace("\\t", "\t");
263
if (sep.contains(QRegExp("[0-9.eE+-]")))
264
QMessageBox::warning(this, tr("QtiPlot - Import options error"),
265
tr("The separator must not contain the following characters: 0-9eE.+-"));
271
void ImportASCIIDialog::displayHelp()
273
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");
274
s += "\n"+tr("The separator must not contain the following characters: 0-9eE.+-") + "\n\n";
275
s += tr( "Remove white spaces from line ends" )+ ":\n";
276
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";
277
s += tr( "Simplify white spaces" )+ ":\n";
278
s += tr("By checking this option each sequence of internal whitespaces (including the TAB character) will be replaced with a single space.");
279
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.");
281
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.");
282
s +="\n"+tr("To avoid this problem you should precisely define the column separator using TAB and SPACE characters.");
284
QMessageBox::about(this, tr("QtiPlot - Help"), s);
287
void ImportASCIIDialog::updateImportMode(int mode)
289
if (mode == Overwrite)
290
setFileMode( QFileDialog::ExistingFile );
292
setFileMode( QFileDialog::ExistingFiles );
295
void ImportASCIIDialog::closeEvent(QCloseEvent* e)
297
ApplicationWindow *app = (ApplicationWindow *)this->parent();
299
app->d_extended_import_ASCII_dialog = this->isExtended();
300
app->d_ASCII_file_filter = this->selectedFilter();
301
app->d_ASCII_import_preview = d_preview_button->isChecked();
302
app->d_preview_lines = d_preview_lines_box->value();
308
QLocale ImportASCIIDialog::decimalSeparators()
311
switch (boxDecimalSeparator->currentIndex()){
313
locale = QLocale::system();
316
locale = QLocale::c();
319
locale = QLocale(QLocale::German);
322
locale = QLocale(QLocale::French);
328
void ImportASCIIDialog::preview()
232
advanced_layout->addWidget(d_help_button, 6, 2);
234
d_preview_table = NULL;
235
d_preview_matrix = NULL;
236
d_preview_stack = new QStackedWidget();
237
main_layout->addWidget(d_preview_stack);
240
void ImportASCIIDialog::initPreview(int previewMode)
242
if (previewMode < NewTables || previewMode > Overwrite)
245
ApplicationWindow *app = (ApplicationWindow *)parent();
249
if (d_preview_table){
250
delete d_preview_table;
251
d_preview_table = NULL;
254
if (d_preview_matrix){
255
delete d_preview_matrix;
256
d_preview_matrix = NULL;
261
d_preview_table = new PreviewTable(30, 2, this);
262
d_preview_table->setNumericPrecision(app->d_decimal_digits);
263
d_preview_stack->addWidget(d_preview_table);
264
enableTableOptions(true);
268
d_preview_matrix = new PreviewMatrix(app);
269
d_preview_stack->addWidget(d_preview_matrix);
270
enableTableOptions(false);
276
MdiSubWindow *w = app->activeWindow();
280
if (w->inherits("Table")){
281
d_preview_table = new PreviewTable(30, 2, this);
282
d_preview_table->setNumericPrecision(app->d_decimal_digits);
283
d_preview_stack->addWidget(d_preview_table);
284
enableTableOptions(true);
285
} else if (w->isA("Matrix")){
286
d_preview_matrix = new PreviewMatrix(app, (Matrix *)w);
287
d_preview_stack->addWidget(d_preview_matrix);
288
enableTableOptions(false);
295
void ImportASCIIDialog::enableTableOptions(bool on)
297
d_rename_columns->setEnabled(on);
298
d_import_comments->setEnabled(on && d_rename_columns->isChecked());
299
d_read_only->setEnabled(on);
302
void ImportASCIIDialog::setColumnSeparator(const QString& sep)
305
d_column_separator->setCurrentIndex(0);
307
d_column_separator->setCurrentIndex(1);
309
d_column_separator->setCurrentIndex(2);
311
d_column_separator->setCurrentIndex(3);
313
d_column_separator->setCurrentIndex(4);
315
d_column_separator->setCurrentIndex(5);
317
d_column_separator->setCurrentIndex(6);
319
d_column_separator->setCurrentIndex(7);
321
QString separator = sep;
322
d_column_separator->setEditText(separator.replace(" ","\\s").replace("\t","\\t"));
326
const QString ImportASCIIDialog::columnSeparator() const
328
QString sep = d_column_separator->currentText();
330
if (d_simplify_spaces->isChecked())
331
sep.replace(tr("TAB"), " ", Qt::CaseInsensitive);
333
sep.replace(tr("TAB"), "\t", Qt::CaseInsensitive);
335
sep.replace(tr("SPACE"), " ", Qt::CaseInsensitive);
336
sep.replace("\\s", " ");
337
sep.replace("\\t", "\t");
340
if (sep.contains(QRegExp("[0-9.eE+-]")))
341
QMessageBox::warning(this, tr("QtiPlot - Import options error"),
342
tr("The separator must not contain the following characters: 0-9eE.+-"));
348
void ImportASCIIDialog::displayHelp()
350
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");
351
s += "\n"+tr("The separator must not contain the following characters: 0-9eE.+-") + "\n\n";
352
s += tr( "Remove white spaces from line ends" )+ ":\n";
353
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";
354
s += tr( "Simplify white spaces" )+ ":\n";
355
s += tr("By checking this option each sequence of internal whitespaces (including the TAB character) will be replaced with a single space.");
356
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.");
358
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.");
359
s +="\n"+tr("To avoid this problem you should precisely define the column separator using TAB and SPACE characters.");
361
QMessageBox::about(this, tr("QtiPlot - Help"), s);
364
void ImportASCIIDialog::updateImportMode(int mode)
366
if (mode == Overwrite)
367
setFileMode( QFileDialog::ExistingFile );
369
setFileMode( QFileDialog::ExistingFiles );
374
void ImportASCIIDialog::closeEvent(QCloseEvent* e)
376
ApplicationWindow *app = (ApplicationWindow *)this->parent();
378
app->d_extended_import_ASCII_dialog = this->isExtended();
379
app->d_ASCII_file_filter = this->selectedFilter();
380
app->d_ASCII_import_preview = d_preview_button->isChecked();
381
app->d_preview_lines = d_preview_lines_box->value();
387
QLocale ImportASCIIDialog::decimalSeparators()
390
switch (boxDecimalSeparator->currentIndex()){
392
locale = QLocale::system();
395
locale = QLocale::c();
398
locale = QLocale(QLocale::German);
401
locale = QLocale(QLocale::French);
407
void ImportASCIIDialog::preview()
330
409
if (!d_preview_button->isChecked()){
331
d_preview_table->hide();
410
d_preview_stack->hide();
413
d_preview_stack->show();
417
else if (d_preview_matrix)
421
void ImportASCIIDialog::previewTable()
423
if (!d_preview_table)
426
if (!d_preview_table->isVisible())
427
d_preview_table->show();
335
429
if (d_current_path.trimmed().isEmpty()){
336
d_preview_table->clear();
430
d_preview_table->clear();
337
431
d_preview_table->resetHeader();
338
#if defined(Q_WS_MAC)//displaying comments on Mac leads to a crash
339
d_preview_table->showComments(false);
341
d_preview_table->showComments();
343
if (!d_preview_table->isVisible())
344
d_preview_table->show();
348
QString fileName = d_current_path;
349
QTemporaryFile tempFile;
350
int rows = d_preview_lines_box->value();
352
QFile dataFile(fileName);
353
if(tempFile.open() && dataFile.open(QIODevice::ReadOnly)){
354
Q3TextStream t(&dataFile);
355
QTextStream tt(&tempFile);
357
while(i<rows && !t.atEnd()){
358
tt << t.readLine() + "\n";
361
fileName = tempFile.fileName();
365
d_preview_table->resetHeader();
366
d_preview_table->importASCII(fileName, columnSeparator(), d_ignored_lines->value(),
367
d_rename_columns->isChecked(), d_strip_spaces->isChecked(),
368
d_simplify_spaces->isChecked(), d_import_comments->isChecked(), false,
369
d_comment_string->text(), d_read_only->isChecked());
371
if (d_import_dec_separators->isChecked())
435
int importMode = d_import_mode->currentIndex();
436
if (importMode == NewTables)
437
importMode = Table::Overwrite;
441
d_preview_table->resetHeader();
442
d_preview_table->importASCII(d_current_path, columnSeparator(), d_ignored_lines->value(),
443
d_rename_columns->isChecked(), d_strip_spaces->isChecked(),
444
d_simplify_spaces->isChecked(), d_import_comments->isChecked(),
445
d_comment_string->text(), (Table::ImportMode)importMode,
446
boxEndLine->currentIndex(), d_preview_lines_box->value());
448
if (d_import_dec_separators->isChecked())
372
449
d_preview_table->updateDecimalSeparators(decimalSeparators());
373
450
if (!d_preview_table->isVisible())
374
451
d_preview_table->show();
379
void ImportASCIIDialog::changePreviewFile(const QString& path)
381
if (path.isEmpty() || !QFileInfo(path).exists())
384
d_current_path = path;
454
void ImportASCIIDialog::previewMatrix()
456
if (!d_preview_matrix)
459
if (d_current_path.trimmed().isEmpty()){
460
d_preview_matrix->clear();
464
int importMode = d_import_mode->currentIndex();
465
if (importMode == NewMatrices)
466
importMode = Matrix::Overwrite;
470
QLocale locale = d_preview_matrix->locale();
471
if(d_import_dec_separators->isChecked())
472
locale = decimalSeparators();
474
d_preview_matrix->importASCII(d_current_path, columnSeparator(), d_ignored_lines->value(),
475
d_strip_spaces->isChecked(), d_simplify_spaces->isChecked(),
476
d_comment_string->text(), importMode, locale,
477
boxEndLine->currentIndex(), d_preview_lines_box->value());
478
d_preview_matrix->resizeColumnsToContents();
481
void ImportASCIIDialog::changePreviewFile(const QString& path)
487
if (!fi.exists() || fi.isDir() || !fi.isFile())
490
if (!fi.isReadable()){
491
QMessageBox::critical(this, tr("QtiPlot - File openning error"),
492
tr("You don't have the permission to open this file: <b>%1</b>").arg(path));
496
d_current_path = path;
500
void ImportASCIIDialog::setNewWindowsOnly(bool on)
503
d_import_mode->clear();
504
d_import_mode->addItem(tr("New Table"));
505
d_import_mode->addItem(tr("New Matrice"));
508
d_preview_button->setChecked(false);
511
/*****************************************************************************
515
*****************************************************************************/
517
PreviewTable::PreviewTable(int numRows, int numCols, QWidget * parent, const char * name)
518
:Q3Table(numRows, numCols, parent, name)
520
setAttribute(Qt::WA_DeleteOnClose);
521
setSelectionMode(Q3Table::NoSelection);
523
setRowMovingEnabled(false);
524
setColumnMovingEnabled(false);
525
verticalHeader()->setResizeEnabled(false);
527
for (int i=0; i<numCols; i++){
529
col_label << QString::number(i+1);
533
setMinimumHeight(4*horizontalHeader()->height());
535
setMinimumHeight(2*horizontalHeader()->height());
539
void PreviewTable::importASCII(const QString &fname, const QString &sep, int ignoredLines, bool renameCols,
540
bool stripSpaces, bool simplifySpaces, bool importComments, const QString& commentString,
541
int importMode, int endLine, int maxRows)
544
QString name = MdiSubWindow::parseAsciiFile(fname, commentString, endLine, ignoredLines, maxRows, rows);
549
if (f.open(QIODevice::ReadOnly)){
550
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
553
QString s = t.readLine();//read first line
555
s = s.simplifyWhiteSpace();
556
else if (stripSpaces)
557
s = s.stripWhiteSpace();
559
QStringList line = s.split(sep);
560
int cols = line.size();
562
bool allNumbers = true;
563
for (int i=0; i<cols; i++)
564
{//verify if the strings in the line used to rename the columns are not all numbers
565
locale().toDouble(line[i], &allNumbers);
569
if (renameCols && !allNumbers){
575
int startRow = 0, startCol = 0;
579
case Table::Overwrite:
580
if (numRows() != rows)
585
addColumns(cols - c);
590
case Table::NewColumns:
599
addColumns(cols - c);
600
setNumRows(r + rows);
604
if (renameCols && !allNumbers){//use first line to set the table header
605
for (int i = 0; i<cols; i++){
606
int aux = i + startCol;
607
col_label[aux] = QString::null;
609
comments[aux] = line[i];
610
s = line[i].replace("-","_").remove(QRegExp("\\W")).replace("_","-");
611
int n = col_label.count(s);
612
if(n){//avoid identical col names
613
while (col_label.contains(s + QString::number(n)))
615
s += QString::number(n);
620
if (importComments){//import comments
621
s = t.readLine();//read 2nd line
623
s = s.simplifyWhiteSpace();
624
else if (stripSpaces)
625
s = s.stripWhiteSpace();
626
line = s.split(sep, QString::SkipEmptyParts);
627
for (int i=0; i<line.size(); i++)
628
comments[startCol + i] = line[i];
629
qApp->processEvents(QEventLoop::ExcludeUserInput);
631
} else if (rows > 0){//put values in the first line of the table
632
for (int i = 0; i<cols; i++)
633
setText(startRow, startCol + i, line[i]);
640
QApplication::restoreOverrideCursor();
644
while (!t.atEnd() && row < rows){
647
s = s.simplifyWhiteSpace();
648
else if (stripSpaces)
649
s = s.stripWhiteSpace();
651
int lc = line.size();
653
addColumns(lc - cols);
656
for (int j=0; j<cols && j<lc; j++)
657
setText(row, startCol + j, line[j]);
660
qApp->processEvents(QEventLoop::ExcludeUserInput);
667
void PreviewTable::resetHeader()
669
for (int i=0; i<numCols(); i++){
670
comments[i] = QString::null;
671
col_label[i] = QString::number(i+1);
675
void PreviewTable::clear()
677
for (int i=0; i<numCols(); i++){
678
for (int j=0; j<numRows(); j++)
679
setText(j, i, QString::null);
683
void PreviewTable::updateDecimalSeparators(const QLocale& oldSeparators)
685
QLocale locale = ((QWidget *)parent())->locale();
686
for (int i=0; i<numCols(); i++){
687
for (int j=0; j<numRows(); j++){
688
if (!text(j, i).isEmpty()){
689
double val = oldSeparators.toDouble(text(j, i));
690
setText(j, i, locale.toString(val, 'g', d_numeric_precision));
696
void PreviewTable::setHeader()
698
Q3Header *head = horizontalHeader();
699
for (int i=0; i<numCols(); i++){
700
QString s = col_label[i];
701
int lines = columnWidth(i)/head->fontMetrics().averageCharWidth();
703
head->setLabel(i, s.remove("\n"));
705
head->setLabel(i, s.remove("\n") + "\n" + QString(lines, '_') + "\n" + comments[i]);
710
void PreviewTable::addColumns(int c)
712
int max=0, cols = numCols();
713
for (int i=0; i<cols; i++){
714
if (!col_label[i].contains(QRegExp ("\\D"))){
715
int index=col_label[i].toInt();
721
insertColumns(cols, c);
722
for (int i=0; i<c; i++){
723
comments << QString();
724
col_label<< QString::number(max+i);
728
/*****************************************************************************
730
* Class PreviewMatrix
732
*****************************************************************************/
734
PreviewMatrix::PreviewMatrix(QWidget *parent, Matrix * m):QTableView(parent)
736
d_matrix_model = new MatrixModel(32, 32, m);
738
ApplicationWindow *app = (ApplicationWindow *)parent;
740
d_matrix_model->setLocale(app->locale());
741
d_matrix_model->setNumericFormat('f', app->d_decimal_digits);
744
setModel(d_matrix_model);
746
setAttribute(Qt::WA_DeleteOnClose);
747
setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
748
setSelectionMode(QAbstractItemView::NoSelection);
749
setEditTriggers(QAbstractItemView::NoEditTriggers);
750
setFocusPolicy(Qt::NoFocus);
752
QPalette pal = palette();
753
pal.setColor(QColorGroup::Base, QColor(255, 255, 128));
756
// set header properties
757
horizontalHeader()->setMovable(false);
758
horizontalHeader()->setResizeMode(QHeaderView::Fixed);
759
for(int i=0; i<d_matrix_model->columnCount(); i++)
760
setColumnWidth(i, 100);
762
verticalHeader()->setMovable(false);
763
verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
766
void PreviewMatrix::importASCII(const QString &fname, const QString &sep, int ignoredLines,
767
bool stripSpaces, bool simplifySpaces, const QString& commentString,
768
int importAs, const QLocale& locale, int endLine, int maxRows)
770
d_matrix_model->importASCII(fname, sep, ignoredLines, stripSpaces,
771
simplifySpaces, commentString, importAs, locale, endLine, maxRows);
774
void PreviewMatrix::clear()
776
d_matrix_model->clear();