diff --git a/src/kdefrontend/datasources/JsonOptionsWidget.cpp b/src/kdefrontend/datasources/JsonOptionsWidget.cpp index e4202b48f..bec5e1532 100644 --- a/src/kdefrontend/datasources/JsonOptionsWidget.cpp +++ b/src/kdefrontend/datasources/JsonOptionsWidget.cpp @@ -1,196 +1,202 @@ #include "ImportFileWidget.h" #include "JsonOptionsWidget.h" #include "QJsonModel.h" #include "backend/datasources/filters/AbstractFileFilter.h" #include "backend/datasources/filters/JsonFilter.h" #include #include #include #include + /*! \class JsonOptionsWidget \brief Widget providing options for the import of json data \ingroup kdefrontend */ -JsonOptionsWidget::JsonOptionsWidget(QWidget* parent, ImportFileWidget* fileWidget) : QWidget(parent), m_fileWidget(fileWidget) { +JsonOptionsWidget::JsonOptionsWidget(QWidget* parent, ImportFileWidget* fileWidget) : QWidget(parent), + m_fileWidget(fileWidget), + m_model(new QJsonModel()), + m_lastItem(nullptr) { + ui.setupUi(parent); - m_model = new QJsonModel(); - m_lastItem = nullptr; ui.tvJson->setModel(m_model); + ui.tvJson->header()->setSectionResizeMode(QHeaderView::ResizeToContents); + ui.tvJson->setAlternatingRowColors(true); + ui.cbDataRowType->addItems(JsonFilter::dataRowTypes()); ui.cbNumberFormat->addItems(AbstractFileFilter::numberFormats()); ui.cbDateTimeFormat->addItems(AbstractColumn::dateTimeFormats()); connect(ui.chbUseRootEl, &QCheckBox::stateChanged, [=](int state) { ui.tvJson->setEnabled(state == 0); }); connect(ui.tvJson, &QAbstractItemView::clicked, this, &JsonOptionsWidget::indexChanged); connect(m_fileWidget, SIGNAL(fileNameChanged()), this, SLOT(updateContent())); setTooltips(); } void JsonOptionsWidget::applyFilterSettings(JsonFilter* filter) const { Q_ASSERT(filter); filter->setDataContainerType(getCurrentType()); //TODO: change this after implementation other row types filter->setDataRowType(ui.cbDataRowType->currentIndex() == 0 ? QJsonValue::Array : QJsonValue::Object); filter->setNumberFormat( QLocale::Language(ui.cbNumberFormat->currentIndex())); filter->setDateTimeFormat(ui.cbDateTimeFormat->currentText()); filter->setCreateIndexEnabled(ui.chbCreateIndex->isChecked()); filter->setNaNValueToZero(ui.chbConvertNaNToZero->isChecked()); } void JsonOptionsWidget::clear() { m_model->clear(); } void JsonOptionsWidget::loadSettings() const { KConfigGroup conf(KSharedConfig::openConfig(), "ImportJson"); //ui.leDataContainerName->setText(conf.readEntry("DataContainerName", "")); //ui.cbDataContainerType->setCurrentIndex(conf.readEntry("DataContainerType", (int)JsonFilter::Array)); //TODO: change this after implementation other row types ui.cbDataRowType->setCurrentIndex(conf.readEntry("DataRowType", 0)); ui.cbNumberFormat->setCurrentIndex(conf.readEntry("NumberFormat", (int)QLocale::AnyLanguage)); ui.cbDateTimeFormat->setCurrentItem(conf.readEntry("DateTimeFormat", "yyyy-MM-dd hh:mm:ss.zzz")); ui.chbCreateIndex->setChecked(conf.readEntry("CreateIndex", false)); ui.chbConvertNaNToZero->setChecked(conf.readEntry("ConvertNaNToZero", false)); } void JsonOptionsWidget::saveSettings() { KConfigGroup conf(KSharedConfig::openConfig(), "ImportJson"); // conf.writeEntry("DataContainerName", ui.leDataContainerName->text()); // conf.writeEntry("DataContainerType", ui.cbDataContainerType->currentIndex()); conf.writeEntry("DataRowType", ui.cbDataRowType->currentIndex()); conf.writeEntry("NumberFormat", ui.cbNumberFormat->currentIndex()); conf.writeEntry("DateTimeFormat", ui.cbDateTimeFormat->currentText()); conf.writeEntry("CreateIndex", ui.chbCreateIndex->isChecked()); conf.writeEntry("ConvertNaNToZero", ui.chbConvertNaNToZero->isChecked()); } void JsonOptionsWidget::updateContent() { if(m_fileWidget->currentFileType() == LiveDataSource::Json){ KFilterDev device(m_fileWidget->fileName()); if (!device.open(QIODevice::ReadOnly)) return; if (device.atEnd() && !device.isSequential()) // empty file return; m_model->loadJson(device.readAll()); } } QJsonDocument JsonOptionsWidget::selectedJson() const { if(ui.chbUseRootEl->isChecked() || ui.tvJson->currentIndex().internalPointer() == nullptr) return m_model->json(); else { QJsonDocument data = QJsonDocument::fromVariant(m_model->genJsonByIndex(ui.tvJson->currentIndex()).toVariant()); if (data.isEmpty()) data = m_model->json(); return data; } } void JsonOptionsWidget::indexChanged() { if(ui.tvJson->currentIndex().internalPointer() != nullptr) m_lastItem = static_cast(ui.tvJson->currentIndex().internalPointer()); else m_lastItem = nullptr; m_fileWidget->refreshPreview(); } void JsonOptionsWidget::setTooltips() { const QString textNumberFormatShort = i18n("This option determines how the imported strings have to be converted to numbers."); const QString textNumberFormat = textNumberFormatShort + "

" + i18n( "For 'C Format', a period is used for the decimal point character and comma is used for the thousands group separator. " "Valid number representations are:" "
    " "
  • 1234.56
  • " "
  • 1,234.56
  • " "
  • etc.
  • " "
" "When using 'System locale', the system settings will be used. " "E.g., for the German local the valid number representations are:" "
    " "
  • 1234,56
  • " "
  • 1.234,56
  • " "
  • etc.
  • " "
" ); ui.lNumberFormat->setToolTip(textNumberFormatShort); ui.lNumberFormat->setWhatsThis(textNumberFormat); ui.cbNumberFormat->setToolTip(textNumberFormatShort); ui.cbNumberFormat->setWhatsThis(textNumberFormat); 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 + "

" + i18n( "Expressions that may be used for the date part of format string:" "" "" "" "" "" "" "" "" "" "" "" "
dthe day as number without a leading zero (1 to 31).
ddthe day as number with a leading zero (01 to 31).
dddthe abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses the system locale to localize the name.
ddddthe long localized day name (e.g. 'Monday' to 'Sunday'). Uses the system locale to localize the name.
Mthe month as number without a leading zero (1 to 12).
MMthe month as number with a leading zero (01 to 12).
MMMthe abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses the system locale to localize the name.
MMMMthe long localized month name (e.g. 'January' to 'December'). Uses the system locale to localize the name.
yythe year as two digit number (00 to 99).
yyyythe year as four digit number. If the year is negative, a minus sign is prepended in addition.


" "Expressions that may be used for the time part of the format string:" "" "" "" "" "" "" "" "" "" "" "" "" "" "
hthe hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hhthe hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
Hthe hour without a leading zero (0 to 23, even with AM/PM display)
HHthe hour with a leading zero (00 to 23, even with AM/PM display)
mthe minute without a leading zero (0 to 59)
mmthe minute with a leading zero (00 to 59)
sthe second without a leading zero (0 to 59)
ssthe second with a leading zero (00 to 59)
zthe milliseconds without leading zeroes (0 to 999)
zzzthe milliseconds with leading zeroes (000 to 999)
AP or Ainterpret as an AM/PM time. AP must be either 'AM' or 'PM'.
ap or aInterpret as an AM/PM time. ap must be either 'am' or 'pm'.


" "Examples are:" "" "" "" "" "
dd.MM.yyyy20.07.1969
ddd MMMM d yySun July 20 69
'The day is' ddddThe day is Sunday
"); ui.lDateTimeFormat->setToolTip(textDateTimeFormatShort); ui.lDateTimeFormat->setWhatsThis(textDateTimeFormat); ui.cbDateTimeFormat->setToolTip(textDateTimeFormatShort); ui.cbDateTimeFormat->setWhatsThis(textDateTimeFormat); } JsonFilter::DataContainerType JsonOptionsWidget::getCurrentType() const { if(ui.chbUseRootEl->isChecked() || m_lastItem == nullptr) { QJsonDocument doc = m_model->json(); if(doc.isArray()) return JsonFilter::Array; else return JsonFilter::Object; } else{ switch(m_lastItem->type()){ case QJsonValue::Array: return JsonFilter::Array; case QJsonValue::Object: return JsonFilter::Object; default: DEBUG("Undefined value type: " << m_lastItem->type()); return JsonFilter::Object; } } -} \ No newline at end of file +}