Skip to content

Commit

Permalink
Added UI to select ISO 8601 date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
briha committed Mar 31, 2024
1 parent 38db02d commit 39b741f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
35 changes: 29 additions & 6 deletions plotjuggler_plugins/DataLoadCSV/dataload_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,19 @@ DataLoadCSV::DataLoadCSV()
[this]() { emit _ui->buttonBox->accepted(); });

connect(_ui->checkBoxDateFormat, &QCheckBox::toggled, this,
[this](bool checked) { _ui->lineEditDateFormat->setEnabled(checked); });
[this](bool checked) {
_ui->radioCustomDate->setEnabled(checked);
_ui->radioIso8601Date->setEnabled(checked);
_ui->lineEditDateFormat->setEnabled(_ui->radioCustomDate->isChecked());
});

connect(_ui->radioCustomDate, &QRadioButton::clicked, this, [this](bool checked){
_ui->lineEditDateFormat->setEnabled(checked);
});

connect(_ui->radioIso8601Date, &QRadioButton::clicked, this, [this](bool checked){
_ui->lineEditDateFormat->setEnabled(!checked);
});

connect(_ui->dateTimeHelpButton, &QPushButton::clicked, this,
[this]() { _dateTime_dialog->show(); });
Expand Down Expand Up @@ -490,6 +502,7 @@ bool DataLoadCSV::readDataFromFile(FileLoadInfo* info, PlotDataMapRef& plot_data
double prev_time = std::numeric_limits<double>::lowest();
bool parse_date_format = _ui->checkBoxDateFormat->isChecked();
QString format_string = _ui->lineEditDateFormat->text();
bool parse_iso_8601 = _ui->radioIso8601Date->isChecked();

auto ParseTimestamp = [&](QString str, bool& is_number) {
QString str_trimmed = str.trimmed();
Expand Down Expand Up @@ -530,9 +543,9 @@ bool DataLoadCSV::readDataFromFile(FileLoadInfo* info, PlotDataMapRef& plot_data
static QLocale locale_with_comma(QLocale::German);
val = locale_with_comma.toDouble(str_trimmed, &is_number);
}
if (!is_number && parse_date_format)
if (!is_number && parse_date_format && (!format_string.isEmpty() || parse_iso_8601))
{
QDateTime ts = !format_string.isEmpty() ? QDateTime::fromString(str_trimmed, format_string) : QDateTime::fromString(str_trimmed, Qt::ISODateWithMs);
QDateTime ts = parse_iso_8601 ? QDateTime::fromString(str_trimmed, Qt::ISODateWithMs) : QDateTime::fromString(str_trimmed, format_string);
is_number = ts.isValid();
if (is_number)
{
Expand All @@ -551,9 +564,9 @@ bool DataLoadCSV::readDataFromFile(FileLoadInfo* info, PlotDataMapRef& plot_data
static QLocale locale_with_comma(QLocale::German);
val = locale_with_comma.toDouble(str_trimmed, &is_number);
}
if (!is_number && parse_date_format && !format_string.isEmpty())
if (!is_number && parse_date_format && (!format_string.isEmpty() || parse_iso_8601))
{
QDateTime ts = QDateTime::fromString(str_trimmed, format_string);
QDateTime ts = parse_iso_8601 ? QDateTime::fromString(str_trimmed, Qt::ISODateWithMs) : QDateTime::fromString(str_trimmed, format_string);
is_number = ts.isValid();
if (is_number)
{
Expand Down Expand Up @@ -779,12 +792,16 @@ bool DataLoadCSV::xmlSaveState(QDomDocument& doc, QDomElement& parent_element) c
elem.setAttribute("time_axis", _default_time_axis.c_str());
elem.setAttribute("delimiter", _ui->comboBox->currentIndex());

QString date_format;
if (_ui->checkBoxDateFormat->isChecked())
{
elem.setAttribute("date_format", _ui->lineEditDateFormat->text());
}

if (_ui->radioIso8601Date->isChecked())
{
elem.setAttribute("date_format_iso", "ISO8601");
}

parent_element.appendChild(elem);
return true;
}
Expand Down Expand Up @@ -822,5 +839,11 @@ bool DataLoadCSV::xmlLoadState(const QDomElement& parent_element)
_ui->checkBoxDateFormat->setChecked(true);
_ui->lineEditDateFormat->setText(elem.attribute("date_format"));
}
if (elem.hasAttribute("date_format_iso"))
{
_ui->radioIso8601Date->setChecked(true);
} else {
_ui->radioCustomDate->setChecked(true);
}
return true;
}
17 changes: 17 additions & 0 deletions plotjuggler_plugins/DataLoadCSV/dataload_csv.ui
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioIso8601Date">
<property name="text">
<string> ISO 8601</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioCustomDate">
<property name="text">
<string>Custom</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditDateFormat">
<property name="enabled">
Expand Down

0 comments on commit 39b741f

Please sign in to comment.