Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON based Mapper configuration #2157

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# Copyright 2012-2014 Thomas Schöps
# Copyright 2012-2018 Kai Pastor
# Copyright 2012-2023 Kai Pastor
#
# This file is part of OpenOrienteering.
#
Expand Down Expand Up @@ -204,6 +204,7 @@ set(Mapper_Common_SRCS
gui/widgets/template_list_widget.cpp
gui/widgets/text_browser.cpp
gui/widgets/toast.cpp
gui/widgets/json_config_widget.cpp

templates/paint_on_template_feature.cpp
templates/paint_on_template_tool.cpp
Expand Down Expand Up @@ -266,6 +267,7 @@ set(Mapper_Common_SRCS
util/translation_util.cpp
util/util.cpp
util/xml_stream_util.cpp
util/json_config.cpp
)

# Extra header to be shown in the IDE or to be translated
Expand Down
4 changes: 3 additions & 1 deletion src/gui/georeferencing_dialog.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2012, 2013 Thomas Schöps
* Copyright 2012-2020 Kai Pastor
* Copyright 2012-2023 Kai Pastor
*
* This file is part of OpenOrienteering.
*
Expand Down Expand Up @@ -78,6 +78,7 @@
#include "gui/widgets/crs_selector.h"
#include "gui/util_gui.h"
#include "util/backports.h" // IWYU pragma: keep
#include "util/json_config.h"
#include "util/scoped_signals_blocker.h"


Expand Down Expand Up @@ -448,6 +449,7 @@ void GeoreferencingDialog::requestDeclination(bool no_confirm)

QUrlQuery query;
QDate today = QDate::currentDate();
query.addQueryItem(QString::fromLatin1("key"), JSONConfiguration::getConfigValue(QLatin1String("DeclLookupKey")).toString());
query.addQueryItem(QString::fromLatin1("lat1"), QString::number(latlon.latitude()));
query.addQueryItem(QString::fromLatin1("lon1"), QString::number(latlon.longitude()));
query.addQueryItem(QString::fromLatin1("startYear"), QString::number(today.year()));
Expand Down
73 changes: 71 additions & 2 deletions src/gui/widgets/general_settings_page.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2012, 2013 Jan Dalheimer
* Copyright 2012-2017 Kai Pastor
* Copyright 2012-2023 Kai Pastor
*
* This file is part of OpenOrienteering.
*
Expand Down Expand Up @@ -46,13 +46,15 @@
#include <QList>
#include <QLocale>
#include <QMessageBox>
#include <QPixmap>
#include <QScreen>
#include <QSettings> // IWYU pragma: keep
#include <QSignalBlocker>
#include <QSize>
#include <QSpacerItem>
#include <QSpinBox>
#include <QStringList>
#include <QStyle>
#include <QTextCodec>
#include <QToolButton>
#include <QVBoxLayout>
Expand All @@ -64,13 +66,16 @@
#include "gui/widgets/home_screen_widget.h"
#include "gui/widgets/settings_page.h"
#include "util/translation_util.h"
#include "gui/widgets/json_config_widget.h"
#include "util/json_config.h"


namespace OpenOrienteering {

GeneralSettingsPage::GeneralSettingsPage(QWidget* parent)
: SettingsPage(parent)
, translation_file(getSetting(Settings::General_TranslationFile).toString())
, json_config_instance(JSONConfiguration::getInstance())
{
auto layout = new QFormLayout(this);

Expand Down Expand Up @@ -167,14 +172,46 @@ GeneralSettingsPage::GeneralSettingsPage(QWidget* parent)
encoding_box->setCompleter(completer);
layout->addRow(tr("8-bit encoding:"), encoding_box);

layout->addItem(Util::SpacerItem::create(this));
layout->addRow(Util::Headline::create(tr("JSON configuration")));

auto json_widget = new QWidget();
auto json_layout = new QHBoxLayout(json_widget);
json_layout->setContentsMargins({});
layout->addRow(tr("JSON file:"), json_widget);

json_configuration_edit = new QLineEdit();
json_configuration_edit->setEnabled(false);
json_layout->addWidget(json_configuration_edit);

auto json_file_button = new QToolButton();
if (Settings::mobileModeEnforced())
{
json_file_button->setVisible(false);
}
else
{
json_file_button->setIcon(QIcon(QLatin1String(":/images/settings.png")));
}

json_status = new QLabel();
json_layout->addWidget(json_status);

QIcon icon = style()->standardIcon(QStyle::SP_DialogOkButton);
json_ok_pixmap = new QPixmap(icon.pixmap(json_file_button->iconSize()));
icon = style()->standardIcon(QStyle::SP_MessageBoxWarning);
json_error_pixmap = new QPixmap(icon.pixmap(json_file_button->iconSize()));

json_layout->addWidget(json_file_button);

updateWidgets();

connect(language_file_button, &QAbstractButton::clicked, this, &GeneralSettingsPage::openTranslationFileDialog);
connect(ppi_calculate_button, &QAbstractButton::clicked, this, &GeneralSettingsPage::openPPICalculationDialog);
connect(encoding_box, &QComboBox::currentTextChanged, this, &GeneralSettingsPage::encodingChanged);
connect(autosave_check, &QAbstractButton::toggled, autosave_interval_edit, &QWidget::setEnabled);
connect(autosave_check, &QAbstractButton::toggled, layout->labelForField(autosave_interval_edit), &QWidget::setEnabled);

connect(json_file_button, &QAbstractButton::clicked, this, &GeneralSettingsPage::openJSONFileDialog);
}

GeneralSettingsPage::~GeneralSettingsPage() = default;
Expand Down Expand Up @@ -241,6 +278,8 @@ void GeneralSettingsPage::apply()
if (!autosave_check->isChecked())
interval = -interval;
setSetting(Settings::General_AutosaveInterval, interval);

setSetting(Settings::General_JSONConfigurationFile, json_configuration_edit->text());
}

void GeneralSettingsPage::reset()
Expand Down Expand Up @@ -298,6 +337,24 @@ void GeneralSettingsPage::updateWidgets()
{
encoding_box->setCurrentText(QString::fromLatin1(encoding));
}
json_configuration_edit->setText(getSetting(Settings::General_JSONConfigurationFile).toString());
updateJSON();
}

void GeneralSettingsPage::updateJSON()
{
const auto json_edit_filename = json_configuration_edit->text();
if (!json_edit_filename.isEmpty())
{
if (json_config_instance.getJSONFilename() != json_edit_filename)
{
json_config_instance.loadConfig(json_edit_filename);
}
json_status->setVisible(true);
json_status->setPixmap(json_config_instance.isLoadedConfigValid() ? *json_ok_pixmap : *json_error_pixmap);
}
else
json_status->setVisible(false);
}

// slot
Expand Down Expand Up @@ -337,6 +394,18 @@ void GeneralSettingsPage::encodingChanged(const QString& input)
}
}

// slot
void GeneralSettingsPage::openJSONFileDialog()
{
JSONConfigWidget dialog(this);
dialog.setWindowModality(Qt::WindowModal);
dialog.exec();
const auto json_filename = json_config_instance.getJSONFilename();
if (!json_filename.isEmpty())
json_configuration_edit->setText(json_filename);
updateJSON();
}

// slot
void GeneralSettingsPage::openTranslationFileDialog()
{
Expand Down
16 changes: 15 additions & 1 deletion src/gui/widgets/general_settings_page.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2012, 2013 Jan Dalheimer
* Copyright 2013-2016 Kai Pastor
* Copyright 2013-2023 Kai Pastor
*
* This file is part of OpenOrienteering.
*
Expand Down Expand Up @@ -31,12 +31,17 @@ class QCheckBox;
class QComboBox;
class QDoubleSpinBox;
class QEvent;
class QLabel;
class QLineEdit;
class QPixmap;
class QSpinBox;
class QWidget;


namespace OpenOrienteering {

class JSONConfiguration;

class GeneralSettingsPage : public SettingsPage
{
Q_OBJECT
Expand All @@ -60,12 +65,15 @@ Q_OBJECT

void updateWidgets();

void updateJSON();

/**
* This event filter stops LanguageChange events.
*/
bool eventFilter(QObject* watched, QEvent* event) override;

private slots:
void openJSONFileDialog();
void openTranslationFileDialog();

void openPPICalculationDialog();
Expand All @@ -76,6 +84,12 @@ private slots:
QString translation_file;
QString last_encoding_input;
QString last_matching_completition;
QString json_configuration_file;

JSONConfiguration& json_config_instance; // the single instance of the JSONConfiguration object
QLineEdit* json_configuration_edit;
QLabel* json_status;
QPixmap* json_ok_pixmap, *json_error_pixmap;

QComboBox* language_box;

Expand Down
Loading