~showard314/ubuntu/natty/qtiplot/Python2.7_fix

« back to all changes in this revision

Viewing changes to qtiplot/src/ImportASCIIDialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-04-04 15:11:55 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080404151155-rjp12ziov4tryj0o
Tags: 0.9.4-1
* New upstream release.
* Refresh patches.
* Remove 04_homepage_url patch. Merged upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
    File                 : ImportASCIIDialog.cpp
3
 
    Project              : QtiPlot
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
8
 
 
9
 
 ***************************************************************************/
10
 
 
11
 
 
12
 
/***************************************************************************
13
 
 *                                                                         *
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.                                    *
18
 
 *                                                                         *
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.                           *
23
 
 *                                                                         *
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                                           *
28
 
 *                                                                         *
29
 
 ***************************************************************************/
30
 
 
31
 
#include "ImportASCIIDialog.h"
32
 
#include "ApplicationWindow.h"
33
 
#include "Table.h"
34
 
 
35
 
#include <QGridLayout>
36
 
#include <QVBoxLayout>
37
 
#include <QHBoxLayout>
 
1
/***************************************************************************
 
2
    File                 : ImportASCIIDialog.cpp
 
3
    Project              : QtiPlot
 
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
 
8
 
 
9
 ***************************************************************************/
 
10
 
 
11
 
 
12
/***************************************************************************
 
13
 *                                                                         *
 
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.                                    *
 
18
 *                                                                         *
 
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.                           *
 
23
 *                                                                         *
 
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                                           *
 
28
 *                                                                         *
 
29
 ***************************************************************************/
 
30
 
 
31
#include "ImportASCIIDialog.h"
 
32
#include "ApplicationWindow.h"
 
33
#include "Table.h"
 
34
#include "Matrix.h"
 
35
#include "MatrixModel.h"
 
36
 
 
37
#include <QLayout>
38
38
#include <QGroupBox>
39
 
#include <QPushButton>
40
 
#include <QRegExp>
41
 
#include <QMessageBox>
42
 
#include <QTemporaryFile>
43
 
#include <QTextStream>
44
 
#include <Q3TextStream>
45
 
 
46
 
ImportASCIIDialog::ImportASCIIDialog(bool import_mode_enabled, QWidget * parent, bool extended, Qt::WFlags flags )
47
 
: ExtensibleFileDialog(parent, extended, flags )
48
 
{
49
 
        setWindowTitle(tr("QtiPlot - Import ASCII File(s)"));
50
 
 
51
 
        QStringList filters;
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 );
57
 
 
58
 
        setFileMode( QFileDialog::ExistingFiles );
59
 
 
60
 
        d_current_path = QString::null;
61
 
 
62
 
        initAdvancedOptions();
63
 
        d_import_mode->setEnabled(import_mode_enabled);
64
 
        setExtensionWidget(d_advanced_options);
65
 
 
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>
 
40
#include <QRegExp>
 
41
#include <QMessageBox>
 
42
#include <QTextStream>
 
43
#include <QApplication>
 
44
#include <QProgressDialog>
 
45
#include <QStackedWidget>
 
46
#include <QHeaderView>
 
47
 
 
48
#include <gsl/gsl_math.h>
 
49
 
 
50
ImportASCIIDialog::ImportASCIIDialog(bool new_windows_only, QWidget * parent, bool extended, Qt::WFlags flags )
 
51
: ExtensibleFileDialog(parent, extended, flags )
 
52
{
 
53
        setWindowTitle(tr("QtiPlot - Import ASCII File(s)"));
 
54
 
 
55
        QStringList filters;
 
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 );
 
61
 
 
62
        setFileMode( QFileDialog::ExistingFiles );
 
63
 
 
64
        d_current_path = QString::null;
 
65
 
 
66
        initAdvancedOptions();
 
67
        setNewWindowsOnly(new_windows_only);
 
68
        setExtensionWidget(d_advanced_options);
 
69
 
 
70
        // get rembered option values
 
71
        ApplicationWindow *app = (ApplicationWindow *)parent;
 
72
        setLocale(app->locale());
 
73
 
 
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);
76
 
 
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);
85
 
 
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();
 
82
 
 
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);
 
91
 
 
92
        connect(d_import_mode, SIGNAL(currentIndexChanged(int)), this, SLOT(updateImportMode(int)));
 
93
 
 
94
        if (app->d_ASCII_import_mode < d_import_mode->count())
 
95
                d_import_mode->setCurrentIndex(app->d_ASCII_import_mode);
 
96
 
 
97
        d_preview_lines_box->setValue(app->d_preview_lines);
 
98
        d_preview_button->setChecked(app->d_ASCII_import_preview);
 
99
 
 
100
    boxEndLine->setCurrentIndex((int)app->d_ASCII_end_line);
 
101
 
 
102
        if (!app->d_ASCII_import_preview)
 
103
                d_preview_stack->hide();
 
104
 
 
105
        initPreview(d_import_mode->currentIndex());
93
106
 
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&)));
105
 
}
106
 
 
107
 
void ImportASCIIDialog::initAdvancedOptions()
108
 
{
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);
113
 
 
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);
122
 
 
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);
144
 
 
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&)));
 
118
}
 
119
 
 
120
void ImportASCIIDialog::initAdvancedOptions()
 
121
{
 
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);
 
126
 
 
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);
 
136
 
 
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);
 
158
 
 
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);
152
166
 
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);
156
 
 
157
 
        d_rename_columns = new QCheckBox(tr("Use first row to &name columns"));
158
 
        advanced_layout->addWidget(d_rename_columns, 0, 2, 1, 2);
 
170
 
 
171
        d_rename_columns = new QCheckBox(tr("Use first row to &name columns"));
 
172
        advanced_layout->addWidget(d_rename_columns, 0, 2, 1, 2);
159
173
 
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);
162
 
 
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);
171
 
 
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);
180
 
 
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);
188
 
 
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)));
 
177
 
 
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);
 
186
 
 
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);
 
195
 
 
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);
 
203
 
 
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);
 
207
 
 
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);
 
215
 
 
216
    d_read_only = new QCheckBox(tr("Import as &read-only"));
 
217
        advanced_layout->addWidget(d_read_only, 5, 2);
192
218
 
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);
196
222
 
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);
203
 
 
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);
206
229
 
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);
210
 
 
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);
217
 
#else
218
 
    d_preview_table->showComments();
219
 
        int height = d_preview_table->table()->horizontalHeader()->height();
220
 
        d_preview_table->setMinimumHeight(2*height);
221
 
#endif
222
 
        main_layout->addWidget(d_preview_table);
223
 
}
224
 
 
225
 
void ImportASCIIDialog::setColumnSeparator(const QString& sep)
226
 
{
227
 
        if (sep=="\t")
228
 
                d_column_separator->setCurrentIndex(0);
229
 
        else if (sep==" ")
230
 
                d_column_separator->setCurrentIndex(1);
231
 
        else if (sep==";\t")
232
 
                d_column_separator->setCurrentIndex(2);
233
 
        else if (sep==",\t")
234
 
                d_column_separator->setCurrentIndex(3);
235
 
        else if (sep=="; ")
236
 
                d_column_separator->setCurrentIndex(4);
237
 
        else if (sep==", ")
238
 
                d_column_separator->setCurrentIndex(5);
239
 
        else if (sep==";")
240
 
                d_column_separator->setCurrentIndex(6);
241
 
        else if (sep==",")
242
 
                d_column_separator->setCurrentIndex(7);
243
 
        else {
244
 
                QString separator = sep;
245
 
                d_column_separator->setEditText(separator.replace(" ","\\s").replace("\t","\\t"));
246
 
        }
247
 
}
248
 
 
249
 
const QString ImportASCIIDialog::columnSeparator() const
250
 
{
251
 
        QString sep = d_column_separator->currentText();
252
 
 
253
 
        if (d_simplify_spaces->isChecked())
254
 
                sep.replace(tr("TAB"), " ", Qt::CaseInsensitive);
255
 
        else
256
 
                sep.replace(tr("TAB"), "\t", Qt::CaseInsensitive);
257
 
 
258
 
        sep.replace(tr("SPACE"), " ", Qt::CaseInsensitive);
259
 
        sep.replace("\\s", " ");
260
 
        sep.replace("\\t", "\t");
261
 
 
262
 
        /* TODO
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.+-"));
266
 
        */
267
 
 
268
 
        return sep;
269
 
}
270
 
 
271
 
void ImportASCIIDialog::displayHelp()
272
 
{
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.");
280
 
 
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.");
283
 
 
284
 
        QMessageBox::about(this, tr("QtiPlot - Help"), s);
285
 
}
286
 
 
287
 
void ImportASCIIDialog::updateImportMode(int mode)
288
 
{
289
 
        if (mode == Overwrite)
290
 
                setFileMode( QFileDialog::ExistingFile );
291
 
        else
292
 
                setFileMode( QFileDialog::ExistingFiles );
293
 
}
294
 
 
295
 
void ImportASCIIDialog::closeEvent(QCloseEvent* e)
296
 
{
297
 
        ApplicationWindow *app = (ApplicationWindow *)this->parent();
298
 
        if (app){
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();
303
 
        }
304
 
 
305
 
        e->accept();
306
 
}
307
 
 
308
 
QLocale ImportASCIIDialog::decimalSeparators()
309
 
{
310
 
        QLocale locale;
311
 
    switch (boxDecimalSeparator->currentIndex()){
312
 
        case 0:
313
 
            locale = QLocale::system();
314
 
        break;
315
 
        case 1:
316
 
            locale = QLocale::c();
317
 
        break;
318
 
        case 2:
319
 
            locale = QLocale(QLocale::German);
320
 
        break;
321
 
        case 3:
322
 
            locale = QLocale(QLocale::French);
323
 
        break;
324
 
    }
325
 
        return locale;
326
 
}
327
 
 
328
 
void ImportASCIIDialog::preview()
 
232
        advanced_layout->addWidget(d_help_button, 6, 2);
 
233
 
 
234
        d_preview_table = NULL;
 
235
        d_preview_matrix = NULL;
 
236
        d_preview_stack = new QStackedWidget();
 
237
        main_layout->addWidget(d_preview_stack);
 
238
}
 
239
 
 
240
void ImportASCIIDialog::initPreview(int previewMode)
 
241
{
 
242
        if (previewMode < NewTables || previewMode > Overwrite)
 
243
                return;
 
244
 
 
245
        ApplicationWindow *app = (ApplicationWindow *)parent();
 
246
        if (!app)
 
247
                return;
 
248
 
 
249
        if (d_preview_table){
 
250
                delete d_preview_table;
 
251
                d_preview_table = NULL;
 
252
        }
 
253
 
 
254
        if (d_preview_matrix){
 
255
                delete d_preview_matrix;
 
256
                d_preview_matrix = NULL;
 
257
        }
 
258
 
 
259
        switch(previewMode){
 
260
                case NewTables:
 
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);
 
265
                break;
 
266
 
 
267
                case NewMatrices:
 
268
                        d_preview_matrix = new PreviewMatrix(app);
 
269
                        d_preview_stack->addWidget(d_preview_matrix);
 
270
                        enableTableOptions(false);
 
271
                break;
 
272
 
 
273
                case NewColumns:
 
274
                case NewRows:
 
275
                case Overwrite:
 
276
                        MdiSubWindow *w = app->activeWindow();
 
277
                        if (!w)
 
278
                                return;
 
279
 
 
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);
 
289
                        }
 
290
                break;
 
291
        }
 
292
        preview();
 
293
}
 
294
 
 
295
void ImportASCIIDialog::enableTableOptions(bool on)
 
296
{
 
297
        d_rename_columns->setEnabled(on);
 
298
        d_import_comments->setEnabled(on && d_rename_columns->isChecked());
 
299
        d_read_only->setEnabled(on);
 
300
}
 
301
 
 
302
void ImportASCIIDialog::setColumnSeparator(const QString& sep)
 
303
{
 
304
        if (sep=="\t")
 
305
                d_column_separator->setCurrentIndex(0);
 
306
        else if (sep==" ")
 
307
                d_column_separator->setCurrentIndex(1);
 
308
        else if (sep==";\t")
 
309
                d_column_separator->setCurrentIndex(2);
 
310
        else if (sep==",\t")
 
311
                d_column_separator->setCurrentIndex(3);
 
312
        else if (sep=="; ")
 
313
                d_column_separator->setCurrentIndex(4);
 
314
        else if (sep==", ")
 
315
                d_column_separator->setCurrentIndex(5);
 
316
        else if (sep==";")
 
317
                d_column_separator->setCurrentIndex(6);
 
318
        else if (sep==",")
 
319
                d_column_separator->setCurrentIndex(7);
 
320
        else {
 
321
                QString separator = sep;
 
322
                d_column_separator->setEditText(separator.replace(" ","\\s").replace("\t","\\t"));
 
323
        }
 
324
}
 
325
 
 
326
const QString ImportASCIIDialog::columnSeparator() const
 
327
{
 
328
        QString sep = d_column_separator->currentText();
 
329
 
 
330
        if (d_simplify_spaces->isChecked())
 
331
                sep.replace(tr("TAB"), " ", Qt::CaseInsensitive);
 
332
        else
 
333
                sep.replace(tr("TAB"), "\t", Qt::CaseInsensitive);
 
334
 
 
335
        sep.replace(tr("SPACE"), " ", Qt::CaseInsensitive);
 
336
        sep.replace("\\s", " ");
 
337
        sep.replace("\\t", "\t");
 
338
 
 
339
        /* TODO
 
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.+-"));
 
343
        */
 
344
 
 
345
        return sep;
 
346
}
 
347
 
 
348
void ImportASCIIDialog::displayHelp()
 
349
{
 
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.");
 
357
 
 
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.");
 
360
 
 
361
        QMessageBox::about(this, tr("QtiPlot - Help"), s);
 
362
}
 
363
 
 
364
void ImportASCIIDialog::updateImportMode(int mode)
 
365
{
 
366
        if (mode == Overwrite)
 
367
                setFileMode( QFileDialog::ExistingFile );
 
368
        else
 
369
                setFileMode( QFileDialog::ExistingFiles );
 
370
 
 
371
        initPreview(mode);
 
372
}
 
373
 
 
374
void ImportASCIIDialog::closeEvent(QCloseEvent* e)
 
375
{
 
376
        ApplicationWindow *app = (ApplicationWindow *)this->parent();
 
377
        if (app){
 
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();
 
382
        }
 
383
 
 
384
        e->accept();
 
385
}
 
386
 
 
387
QLocale ImportASCIIDialog::decimalSeparators()
 
388
{
 
389
        QLocale locale;
 
390
    switch (boxDecimalSeparator->currentIndex()){
 
391
        case 0:
 
392
            locale = QLocale::system();
 
393
        break;
 
394
        case 1:
 
395
            locale = QLocale::c();
 
396
        break;
 
397
        case 2:
 
398
            locale = QLocale(QLocale::German);
 
399
        break;
 
400
        case 3:
 
401
            locale = QLocale(QLocale::French);
 
402
        break;
 
403
    }
 
404
        return locale;
 
405
}
 
406
 
 
407
void ImportASCIIDialog::preview()
329
408
{
330
409
    if (!d_preview_button->isChecked()){
331
 
        d_preview_table->hide();
 
410
                d_preview_stack->hide();
332
411
        return;
333
 
    }
334
 
 
 
412
    } else
 
413
                d_preview_stack->show();
 
414
 
 
415
        if (d_preview_table)
 
416
                previewTable();
 
417
        else if (d_preview_matrix)
 
418
                previewMatrix();
 
419
}
 
420
 
 
421
void ImportASCIIDialog::previewTable()
 
422
{
 
423
        if (!d_preview_table)
 
424
                return;
 
425
 
 
426
        if (!d_preview_table->isVisible())
 
427
                d_preview_table->show();
 
428
 
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);
340
 
        #else
341
 
            d_preview_table->showComments();
342
 
        #endif
343
 
                if (!d_preview_table->isVisible())
344
 
                        d_preview_table->show();
345
432
        return;
346
433
    }
347
 
 
348
 
        QString fileName = d_current_path;
349
 
        QTemporaryFile tempFile;
350
 
        int rows = d_preview_lines_box->value();
351
 
        if (rows){
352
 
                QFile dataFile(fileName);
353
 
                if(tempFile.open() && dataFile.open(QIODevice::ReadOnly)){
354
 
                        Q3TextStream t(&dataFile);
355
 
                        QTextStream tt(&tempFile);
356
 
                        int i = 0;
357
 
                        while(i<rows && !t.atEnd()){
358
 
                                tt << t.readLine() + "\n";
359
 
                                i++;
360
 
                        }
361
 
                        fileName = tempFile.fileName();
362
 
                }
363
 
        }
364
 
 
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());
370
 
 
371
 
        if (d_import_dec_separators->isChecked())
 
434
 
 
435
        int importMode = d_import_mode->currentIndex();
 
436
        if (importMode == NewTables)
 
437
                importMode = Table::Overwrite;
 
438
        else
 
439
                importMode -= 2;
 
440
 
 
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());
 
447
 
 
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();
375
 
 
376
 
        tempFile.close();
377
 
}
378
 
 
379
 
void ImportASCIIDialog::changePreviewFile(const QString& path)
380
 
{
381
 
        if (path.isEmpty() || !QFileInfo(path).exists())
382
 
                return;
383
 
 
384
 
        d_current_path = path;
385
 
        preview();
 
452
}
 
453
 
 
454
void ImportASCIIDialog::previewMatrix()
 
455
{
 
456
        if (!d_preview_matrix)
 
457
                return;
 
458
 
 
459
        if (d_current_path.trimmed().isEmpty()){
 
460
                d_preview_matrix->clear();
 
461
        return;
 
462
    }
 
463
 
 
464
        int importMode = d_import_mode->currentIndex();
 
465
        if (importMode == NewMatrices)
 
466
                importMode = Matrix::Overwrite;
 
467
        else
 
468
                importMode -= 2;
 
469
 
 
470
        QLocale locale = d_preview_matrix->locale();
 
471
        if(d_import_dec_separators->isChecked())
 
472
                locale = decimalSeparators();
 
473
 
 
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();
 
479
}
 
480
 
 
481
void ImportASCIIDialog::changePreviewFile(const QString& path)
 
482
{
 
483
        if (path.isEmpty())
 
484
                return;
 
485
 
 
486
        QFileInfo fi(path);
 
487
        if (!fi.exists() || fi.isDir() || !fi.isFile())
 
488
                return;
 
489
 
 
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));
 
493
                return;
 
494
        }
 
495
 
 
496
        d_current_path = path;
 
497
        preview();
 
498
}
 
499
 
 
500
void ImportASCIIDialog::setNewWindowsOnly(bool on)
 
501
{
 
502
    if (on){
 
503
        d_import_mode->clear();
 
504
        d_import_mode->addItem(tr("New Table"));
 
505
        d_import_mode->addItem(tr("New Matrice"));
 
506
    }
 
507
 
 
508
    d_preview_button->setChecked(false);
 
509
}
 
510
 
 
511
/*****************************************************************************
 
512
 *
 
513
 * Class PreviewTable
 
514
 *
 
515
 *****************************************************************************/
 
516
 
 
517
PreviewTable::PreviewTable(int numRows, int numCols, QWidget * parent, const char * name)
 
518
:Q3Table(numRows, numCols, parent, name)
 
519
{
 
520
        setAttribute(Qt::WA_DeleteOnClose);
 
521
        setSelectionMode(Q3Table::NoSelection);
 
522
        setReadOnly(true);
 
523
        setRowMovingEnabled(false);
 
524
        setColumnMovingEnabled(false);
 
525
        verticalHeader()->setResizeEnabled(false);
 
526
 
 
527
        for (int i=0; i<numCols; i++){
 
528
                comments << "";
 
529
                col_label << QString::number(i+1);
 
530
        }
 
531
        setHeader();
 
532
#ifdef Q_OS_MAC
 
533
        setMinimumHeight(4*horizontalHeader()->height());
 
534
#else
 
535
        setMinimumHeight(2*horizontalHeader()->height());
 
536
#endif
 
537
}
 
538
 
 
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)
 
542
{
 
543
        int rows;
 
544
        QString name = MdiSubWindow::parseAsciiFile(fname, commentString, endLine, ignoredLines, maxRows, rows);
 
545
        if (name.isEmpty())
 
546
                return;
 
547
        
 
548
        QFile f(name);
 
549
        if (f.open(QIODevice::ReadOnly)){
 
550
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 
551
 
 
552
        QTextStream t(&f);
 
553
                QString s = t.readLine();//read first line
 
554
                if (simplifySpaces)
 
555
                        s = s.simplifyWhiteSpace();
 
556
                else if (stripSpaces)
 
557
                        s = s.stripWhiteSpace();
 
558
 
 
559
                QStringList line = s.split(sep);
 
560
                int cols = line.size();
 
561
 
 
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);
 
566
                        if (!allNumbers)
 
567
                                break;
 
568
                }
 
569
        if (renameCols && !allNumbers){
 
570
            rows--;
 
571
            if (importComments)
 
572
                rows--;
 
573
        }
 
574
 
 
575
        int startRow = 0, startCol = 0;
 
576
        int c = numCols();
 
577
        int r = numRows();
 
578
                switch(importMode){
 
579
                        case Table::Overwrite:
 
580
                if (numRows() != rows)
 
581
                    setNumRows(rows);
 
582
 
 
583
                if (c != cols){
 
584
                    if (c < cols)
 
585
                        addColumns(cols - c);
 
586
                    else
 
587
                        setNumCols(cols);
 
588
                }
 
589
                        break;
 
590
                        case Table::NewColumns:
 
591
                startCol = c;
 
592
                addColumns(cols);
 
593
                if (r < rows)
 
594
                    setNumRows(rows);
 
595
                        break;
 
596
                        case Table::NewRows:
 
597
                startRow = r;
 
598
                if (c < cols)
 
599
                    addColumns(cols - c);
 
600
                setNumRows(r + rows);
 
601
                        break;
 
602
                }
 
603
 
 
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;
 
608
                            if (!importComments)
 
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)))
 
614
                                                n++;
 
615
                                        s += QString::number(n);
 
616
                                }
 
617
                                col_label[aux] = s;
 
618
                        }
 
619
 
 
620
            if (importComments){//import comments
 
621
                s = t.readLine();//read 2nd line
 
622
                if (simplifySpaces)
 
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);
 
630
            }
 
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]);
 
634
            startRow++;
 
635
        }
 
636
 
 
637
        blockSignals(true);
 
638
                setHeader();
 
639
 
 
640
        QApplication::restoreOverrideCursor();
 
641
 
 
642
                int row = startRow;
 
643
                rows = numRows();
 
644
                while (!t.atEnd() && row < rows){
 
645
                    s = t.readLine();
 
646
                        if (simplifySpaces)
 
647
                                s = s.simplifyWhiteSpace();
 
648
                        else if (stripSpaces)
 
649
                                s = s.stripWhiteSpace();
 
650
                        line = s.split(sep);
 
651
                        int lc = line.size();
 
652
                        if (lc > cols) {
 
653
                                addColumns(lc - cols);
 
654
                                cols = lc;
 
655
                        }
 
656
                        for (int j=0; j<cols && j<lc; j++)
 
657
                                setText(row, startCol + j, line[j]);
 
658
 
 
659
            row++;
 
660
            qApp->processEvents(QEventLoop::ExcludeUserInput);
 
661
                }
 
662
                blockSignals(false);
 
663
                f.remove();
 
664
        }
 
665
}
 
666
 
 
667
void PreviewTable::resetHeader()
 
668
{
 
669
        for (int i=0; i<numCols(); i++){
 
670
            comments[i] = QString::null;
 
671
                col_label[i] = QString::number(i+1);
 
672
        }
 
673
}
 
674
 
 
675
void PreviewTable::clear()
 
676
{
 
677
        for (int i=0; i<numCols(); i++){
 
678
                for (int j=0; j<numRows(); j++)
 
679
                        setText(j, i, QString::null);
 
680
        }
 
681
}
 
682
 
 
683
void PreviewTable::updateDecimalSeparators(const QLocale& oldSeparators)
 
684
{
 
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));
 
691
                        }
 
692
                }
 
693
        }
 
694
}
 
695
 
 
696
void PreviewTable::setHeader()
 
697
{
 
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();
 
702
        #ifdef Q_OS_MAC
 
703
                head->setLabel(i, s.remove("\n"));
 
704
        #else
 
705
                head->setLabel(i, s.remove("\n") + "\n" + QString(lines, '_') + "\n" + comments[i]);
 
706
        #endif
 
707
        }
 
708
}
 
709
 
 
710
void PreviewTable::addColumns(int c)
 
711
{
 
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();
 
716
                        if (index>max)
 
717
                                max=index;
 
718
                }
 
719
        }
 
720
        max++;
 
721
        insertColumns(cols, c);
 
722
        for (int i=0; i<c; i++){
 
723
                comments << QString();
 
724
                col_label<< QString::number(max+i);
 
725
        }
 
726
}
 
727
 
 
728
/*****************************************************************************
 
729
 *
 
730
 * Class PreviewMatrix
 
731
 *
 
732
 *****************************************************************************/
 
733
 
 
734
PreviewMatrix::PreviewMatrix(QWidget *parent, Matrix * m):QTableView(parent)
 
735
{
 
736
        d_matrix_model = new MatrixModel(32, 32, m);
 
737
        if (!m){
 
738
                ApplicationWindow *app = (ApplicationWindow *)parent;
 
739
                if (app){
 
740
                        d_matrix_model->setLocale(app->locale());
 
741
                        d_matrix_model->setNumericFormat('f', app->d_decimal_digits);
 
742
                }
 
743
        }
 
744
        setModel(d_matrix_model);
 
745
 
 
746
        setAttribute(Qt::WA_DeleteOnClose);
 
747
        setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
 
748
    setSelectionMode(QAbstractItemView::NoSelection);
 
749
    setEditTriggers(QAbstractItemView::NoEditTriggers);
 
750
    setFocusPolicy(Qt::NoFocus);
 
751
 
 
752
    QPalette pal = palette();
 
753
        pal.setColor(QColorGroup::Base, QColor(255, 255, 128));
 
754
        setPalette(pal);
 
755
 
 
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);
 
761
 
 
762
        verticalHeader()->setMovable(false);
 
763
        verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
 
764
}
 
765
 
 
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)
 
769
{
 
770
        d_matrix_model->importASCII(fname, sep, ignoredLines, stripSpaces,
 
771
                simplifySpaces, commentString, importAs, locale, endLine, maxRows);
 
772
}
 
773
 
 
774
void PreviewMatrix::clear()
 
775
{
 
776
        d_matrix_model->clear();
 
777
        reset();
386
778
}