AsciiOptionsWidget.cpp 12.3 KB
Newer Older
1
/***************************************************************************
2 3 4
    File                 : AsciiOptionsWidget.h
    Project              : LabPlot
    Description          : widget providing options for the import of ascii data
5
    --------------------------------------------------------------------
Stefan Gerlach's avatar
Stefan Gerlach committed
6
    Copyright            : (C) 2009-2017 Stefan Gerlach (stefan.gerlach@uni.kn)
7
    Copyright            : (C) 2009-2019 Alexander Semke (alexander.semke@web.de)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *  This program is free software; you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation; either version 2 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the Free Software           *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 *   Boston, MA  02110-1301  USA                                           *
 *                                                                         *
 ***************************************************************************/
Stefan Gerlach's avatar
Stefan Gerlach committed
29

30
#include "AsciiOptionsWidget.h"
Stefan Gerlach's avatar
Stefan Gerlach committed
31 32
#include "backend/datasources/filters/AbstractFileFilter.h"
#include "backend/datasources/filters/AsciiFilter.h"
Stefan Gerlach's avatar
Stefan Gerlach committed
33
#include "backend/lib/macros.h"
Stefan Gerlach's avatar
Stefan Gerlach committed
34

Pino Toscano's avatar
Pino Toscano committed
35
#include <KLocalizedString>
Stefan Gerlach's avatar
Stefan Gerlach committed
36 37
#include <KSharedConfig>
#include <KConfigGroup>
38

39 40 41
/*!
\class AsciiOptionsWidget
\brief Widget providing options for the import of ascii data
42

43 44
\ingroup kdefrontend
*/
45
AsciiOptionsWidget::AsciiOptionsWidget(QWidget* parent) : QWidget(parent) {
Stefan Gerlach's avatar
Stefan Gerlach committed
46
	ui.setupUi(parent);
47

Stefan Gerlach's avatar
Stefan Gerlach committed
48 49
	ui.cbSeparatingCharacter->addItems(AsciiFilter::separatorCharacters());
	ui.cbCommentCharacter->addItems(AsciiFilter::commentCharacters());
50 51
	ui.cbDecimalSeparator->addItem(i18n("Point '.'"));
	ui.cbDecimalSeparator->addItem(i18n("Comma ','"));
Stefan Gerlach's avatar
Stefan Gerlach committed
52 53
	ui.cbDateTimeFormat->addItems(AbstractColumn::dateTimeFormats());

54 55
	const QString textNumberFormatShort = i18n("This option determines how the imported strings have to be converted to numbers.");
	const QString textNumberFormat = textNumberFormatShort + "<br><br>" + i18n(
56
	                                     "When point character is used for the decimal separator, the valid number representations are:"
57 58 59 60 61
	                                     "<ul>"
	                                     "<li>1234.56</li>"
	                                     "<li>1,234.56</li>"
	                                     "<li>etc.</li>"
	                                     "</ul>"
62
	                                     "For comma as the decimal separator, the valid number representations are:"
63 64 65 66 67 68
	                                     "<ul>"
	                                     "<li>1234,56</li>"
	                                     "<li>1.234,56</li>"
	                                     "<li>etc.</li>"
	                                     "</ul>"
	                                 );
69

70 71 72 73
	ui.lDecimalSeparator->setToolTip(textNumberFormatShort);
	ui.lDecimalSeparator->setWhatsThis(textNumberFormat);
	ui.cbDecimalSeparator->setToolTip(textNumberFormatShort);
	ui.cbDecimalSeparator->setWhatsThis(textNumberFormat);
74 75 76

	const QString textDateTimeFormatShort = i18n("This option determines how the imported strings have to be converted to calendar date, i.e. year, month, and day numbers in the Gregorian calendar and to time.");
	const QString textDateTimeFormat = textDateTimeFormatShort + "<br><br>" + i18n(
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
	                                       "Expressions that may be used for the date part of format string:"
	                                       "<table>"
	                                       "<tr><td>d</td><td>the day as number without a leading zero (1 to 31).</td></tr>"
	                                       "<tr><td>dd</td><td>the day as number with a leading zero (01 to 31).</td></tr>"
	                                       "<tr><td>ddd</td><td>the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses the system locale to localize the name.</td></tr>"
	                                       "<tr><td>dddd</td><td>the long localized day name (e.g. 'Monday' to 'Sunday'). Uses the system locale to localize the name.</td></tr>"
	                                       "<tr><td>M</td><td>the month as number without a leading zero (1 to 12).</td></tr>"
	                                       "<tr><td>MM</td><td>the month as number with a leading zero (01 to 12).</td></tr>"
	                                       "<tr><td>MMM</td><td>the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses the system locale to localize the name.</td></tr>"
	                                       "<tr><td>MMMM</td><td>the long localized month name (e.g. 'January' to 'December'). Uses the system locale to localize the name.</td></tr>"
	                                       "<tr><td>yy</td><td>the year as two digit number (00 to 99).</td></tr>"
	                                       "<tr><td>yyyy</td><td>the year as four digit number. If the year is negative, a minus sign is prepended in addition.</td></tr>"
	                                       "</table><br><br>"
	                                       "Expressions that may be used for the time part of the format string:"
	                                       "<table>"
	                                       "<tr><td>h</td><td>the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)</td></tr>"
	                                       "<tr><td>hh</td><td>the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)</td></tr>"
	                                       "<tr><td>H</td><td>the hour without a leading zero (0 to 23, even with AM/PM display)</td></tr>"
	                                       "<tr><td>HH</td><td>the hour with a leading zero (00 to 23, even with AM/PM display)</td></tr>"
	                                       "<tr><td>m</td><td>the minute without a leading zero (0 to 59)</td></tr>"
	                                       "<tr><td>mm</td><td>the minute with a leading zero (00 to 59)</td></tr>"
	                                       "<tr><td>s</td><td>the second without a leading zero (0 to 59)</td></tr>"
	                                       "<tr><td>ss</td><td>the second with a leading zero (00 to 59)</td></tr>"
	                                       "<tr><td>z</td><td>the milliseconds without leading zeroes (0 to 999)</td></tr>"
	                                       "<tr><td>zzz</td><td>the milliseconds with leading zeroes (000 to 999)</td></tr>"
	                                       "<tr><td>AP or A</td><td>interpret as an AM/PM time. AP must be either 'AM' or 'PM'.</td></tr>"
	                                       "<tr><td>ap or a</td><td>Interpret as an AM/PM time. ap must be either 'am' or 'pm'.</td></tr>"
	                                       "</table><br><br>"
	                                       "Examples are:"
	                                       "<table>"
	                                       "<tr><td>dd.MM.yyyy</td><td>20.07.1969</td></tr>"
	                                       "<tr><td>ddd MMMM d yy</td><td>Sun July 20 69</td></tr>"
	                                       "<tr><td>'The day is' dddd</td><td>The day is Sunday</td></tr>"
	                                       "</table>");
111 112 113 114 115

	ui.lDateTimeFormat->setToolTip(textDateTimeFormatShort);
	ui.lDateTimeFormat->setWhatsThis(textDateTimeFormat);
	ui.cbDateTimeFormat->setToolTip(textDateTimeFormatShort);
	ui.cbDateTimeFormat->setWhatsThis(textDateTimeFormat);
Stefan Gerlach's avatar
Stefan Gerlach committed
116

117
	connect(ui.chbHeader, &QCheckBox::stateChanged, this, &AsciiOptionsWidget::headerChanged);
Stefan Gerlach's avatar
Stefan Gerlach committed
118 119
}

120 121 122 123
void AsciiOptionsWidget::showAsciiHeaderOptions(bool visible) {
	DEBUG("AsciiOptionsWidget::showAsciiHeaderOptions(" << visible << ")");
	ui.chbHeader->setVisible(visible);
	if (visible) {
124 125 126 127 128
		ui.lVectorNames->setVisible(!ui.chbHeader->isChecked());
		ui.kleVectorNames->setVisible(!ui.chbHeader->isChecked());
	} else {
		ui.lVectorNames->setVisible(false);
		ui.kleVectorNames->setVisible(false);
129
	}
Stefan Gerlach's avatar
Stefan Gerlach committed
130 131
}

132 133 134 135
void AsciiOptionsWidget::showTimestampOptions(bool visible) {
	ui.chbCreateTimestamp->setVisible(visible);
}

Stefan Gerlach's avatar
Stefan Gerlach committed
136
/*!
137 138
  Shows a text field for the vector names if the option "Use the first row..." was not selected.
  Hides it otherwise.
Stefan Gerlach's avatar
Stefan Gerlach committed
139
*/
Stefan Gerlach's avatar
Stefan Gerlach committed
140
void AsciiOptionsWidget::headerChanged(int state) {
141
	bool visible = (state != Qt::Checked);
142 143
	ui.kleVectorNames->setVisible(visible);
	ui.lVectorNames->setVisible(visible);
144
}
Stefan Gerlach's avatar
Stefan Gerlach committed
145

Stefan Gerlach's avatar
Stefan Gerlach committed
146 147 148 149
void AsciiOptionsWidget::applyFilterSettings(AsciiFilter* filter) const {
	Q_ASSERT(filter);
	filter->setCommentCharacter( ui.cbCommentCharacter->currentText() );
	filter->setSeparatingCharacter( ui.cbSeparatingCharacter->currentText() );
150 151 152 153 154 155 156

	QLocale::Language lang;
	if (ui.cbDecimalSeparator->currentIndex() == 0)
		lang = QLocale::Language::C;
	else
		lang = QLocale::Language::German;
	filter->setNumberFormat(lang);
Stefan Gerlach's avatar
Stefan Gerlach committed
157
	filter->setDateTimeFormat(ui.cbDateTimeFormat->currentText());
158
	filter->setCreateIndexEnabled( ui.chbCreateIndex->isChecked() );
159
	filter->setCreateTimestampEnabled( ui.chbCreateTimestamp->isChecked() );
Stefan Gerlach's avatar
Stefan Gerlach committed
160
	filter->setSimplifyWhitespacesEnabled( ui.chbSimplifyWhitespaces->isChecked() );
161
	filter->setNaNValueToZero( ui.chbConvertNaNToZero->isChecked() );
162
	filter->setRemoveQuotesEnabled( ui.chbRemoveQuotes->isChecked() );
Stefan Gerlach's avatar
Stefan Gerlach committed
163
	filter->setSkipEmptyParts( ui.chbSkipEmptyParts->isChecked() );
164 165
	filter->setVectorNames( ui.kleVectorNames->text() );
	filter->setHeaderEnabled( ui.chbHeader->isChecked() );
Stefan Gerlach's avatar
Stefan Gerlach committed
166 167
}

168 169 170 171
void AsciiOptionsWidget::setSeparatingCharacter(QLatin1Char character) {
	ui.cbSeparatingCharacter->setCurrentItem(QString(character));
}

Stefan Gerlach's avatar
Stefan Gerlach committed
172
void AsciiOptionsWidget::loadSettings() const {
173
	KConfigGroup conf(KSharedConfig::openConfig(), "ImportAscii");
174

175
	ui.cbCommentCharacter->setCurrentText(conf.readEntry("CommentCharacter", "#"));
Stefan Gerlach's avatar
Stefan Gerlach committed
176
	ui.cbSeparatingCharacter->setCurrentItem(conf.readEntry("SeparatingCharacter", "auto"));
177 178 179 180 181

	const QChar decimalSeparator = QLocale().decimalPoint();
	int index = (decimalSeparator == '.') ? 0 : 1;
	ui.cbDecimalSeparator->setCurrentIndex(conf.readEntry("DecimalSeparator", index));

182
	ui.cbDateTimeFormat->setCurrentItem(conf.readEntry("DateTimeFormat", "yyyy-MM-dd hh:mm:ss.zzz"));
183
	ui.chbCreateIndex->setChecked(conf.readEntry("CreateIndex", false));
184
	ui.chbCreateTimestamp->setChecked(conf.readEntry("CreateTimestamp", true));
Stefan Gerlach's avatar
Stefan Gerlach committed
185
	ui.chbSimplifyWhitespaces->setChecked(conf.readEntry("SimplifyWhitespaces", true));
186
	ui.chbConvertNaNToZero->setChecked(conf.readEntry("ConvertNaNToZero", false));
187
	ui.chbRemoveQuotes->setChecked(conf.readEntry("RemoveQuotes", false));
Stefan Gerlach's avatar
Stefan Gerlach committed
188 189 190 191 192 193
	ui.chbSkipEmptyParts->setChecked(conf.readEntry("SkipEmptyParts", false));
	ui.chbHeader->setChecked(conf.readEntry("UseFirstRow", true));
	ui.kleVectorNames->setText(conf.readEntry("Names", ""));
}

void AsciiOptionsWidget::saveSettings() {
194
	KConfigGroup conf(KSharedConfig::openConfig(), "ImportAscii");
Stefan Gerlach's avatar
Stefan Gerlach committed
195 196 197

	conf.writeEntry("CommentCharacter", ui.cbCommentCharacter->currentText());
	conf.writeEntry("SeparatingCharacter", ui.cbSeparatingCharacter->currentText());
198
	conf.writeEntry("DecimalSeparator", ui.cbDecimalSeparator->currentIndex());
Stefan Gerlach's avatar
Stefan Gerlach committed
199
	conf.writeEntry("DateTimeFormat", ui.cbDateTimeFormat->currentText());
200
	conf.writeEntry("CreateIndex", ui.chbCreateIndex->isChecked());
201
	conf.writeEntry("CreateTimestamp", ui.chbCreateTimestamp->isChecked());
Stefan Gerlach's avatar
Stefan Gerlach committed
202
	conf.writeEntry("SimplifyWhitespaces", ui.chbSimplifyWhitespaces->isChecked());
203
	conf.writeEntry("ConvertNaNToZero", ui.chbConvertNaNToZero->isChecked());
204
	conf.writeEntry("RemoveQuotes", ui.chbRemoveQuotes->isChecked());
Stefan Gerlach's avatar
Stefan Gerlach committed
205 206 207
	conf.writeEntry("SkipEmptyParts", ui.chbSkipEmptyParts->isChecked());
	conf.writeEntry("UseFirstRow", ui.chbHeader->isChecked());
	conf.writeEntry("Names", ui.kleVectorNames->text());
Stefan Gerlach's avatar
Stefan Gerlach committed
208
}