Marble

RoutingProfileSettingsDialog.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2010 Niko Sams <niko.sams@gmail.com>
4//
5
6#include "RoutingProfileSettingsDialog.h"
7
8#include <QStandardItemModel>
9
10#include "MarbleDebug.h"
11#include "MarbleGlobal.h"
12#include "PluginManager.h"
13#include "RoutingProfilesModel.h"
14
15#include "ui_RoutingProfileSettingsDialog.h"
16
17namespace Marble
18{
19
20RoutingProfileSettingsDialog::RoutingProfileSettingsDialog(const PluginManager *pluginManager, RoutingProfilesModel *profilesModel, QWidget *parent)
21 : QDialog(parent)
22 , m_profilesModel(profilesModel)
23 , m_dialog(nullptr)
24 , m_dialogLayout(nullptr)
25{
26 m_ui = new Ui_RoutingProfileSettingsDialog();
27 m_ui->setupUi(this);
28 bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
29 if (smallScreen) {
30 setMinimumHeight(480);
31 m_ui->services->setMinimumWidth(280);
32 m_ui->buttonBox->hide();
33 }
34
35 QList<RoutingRunnerPlugin *> allPlugins = pluginManager->routingRunnerPlugins();
36 for (RoutingRunnerPlugin *plugin : allPlugins) {
37 m_plugins << plugin;
38 RoutingRunnerPlugin::ConfigWidget *configWidget = plugin->configWidget();
39 if (configWidget) {
40 m_configWidgets.insert(plugin, configWidget);
41 m_ui->settingsStack->addWidget(configWidget);
42 }
43 }
44
45 m_servicesModel = new QStandardItemModel;
46 m_ui->services->setModel(m_servicesModel);
47 connect(m_ui->services->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), SLOT(updateConfigWidget()), Qt::QueuedConnection);
48 connect(m_servicesModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), SLOT(updateConfigWidget()));
49
50 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
51 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
52
53 connect(m_ui->configureButton, &QAbstractButton::clicked, this, &RoutingProfileSettingsDialog::openConfigDialog);
54}
55
56RoutingProfileSettingsDialog::~RoutingProfileSettingsDialog()
57{
58 qDeleteAll(m_configWidgets);
59 delete m_ui;
60}
61
62void RoutingProfileSettingsDialog::updateConfigWidget()
63{
64 QModelIndex current = m_ui->services->selectionModel()->currentIndex();
65
66 if (!current.isValid()) {
67 m_ui->settingsStack->setEnabled(false);
68 return;
69 }
70
71 RoutingRunnerPlugin *plugin = m_plugins.at(current.row());
72 QWidget *configWidget = m_configWidgets[plugin];
73 if (configWidget) {
74 bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
75 m_ui->settingsStack->setCurrentWidget(smallScreen ? m_ui->configurePage : configWidget);
76 m_ui->descriptionLabel->setText(plugin->description());
77 m_ui->statusLabel->setText(plugin->statusMessage());
78 QStandardItem *item = m_servicesModel->invisibleRootItem()->child(current.row());
79 m_ui->settingsStack->setEnabled(item->checkState() == Qt::Checked);
80 } else {
81 m_ui->settingsStack->setEnabled(false);
82 m_ui->noConfigDescriptionLabel->setText(plugin->description());
83 m_ui->noConfigStatusLabel->setText(plugin->statusMessage());
84 m_ui->settingsStack->setCurrentWidget(m_ui->noConfigAvailablePage);
85 }
86}
87
88void RoutingProfileSettingsDialog::editProfile(int profileIndex)
89{
90 QList<RoutingProfile> profiles = m_profilesModel->profiles();
91 m_ui->name->setText(profiles.at(profileIndex).name());
92
93 m_servicesModel->clear();
94 for (RoutingRunnerPlugin *plugin : m_plugins) {
95 auto item = new QStandardItem(plugin->guiString());
96 item->setCheckable(true);
97 if (profiles[profileIndex].pluginSettings().contains(plugin->nameId())) {
99 QHash<QString, QVariant> settings = profiles[profileIndex].pluginSettings()[plugin->nameId()];
100 mDebug() << settings;
101 if (m_configWidgets[plugin]) {
102 m_configWidgets[plugin]->loadSettings(settings);
103 }
104 }
105 m_servicesModel->invisibleRootItem()->appendRow(item);
106 }
107 m_ui->settingsStack->setCurrentWidget(m_ui->selectServicePage);
108 m_ui->settingsStack->setEnabled(false);
109
110 bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
111 // Small screen profile (like maemo 5) does not have the "buttonBox"
112 if (exec() != QDialog::Accepted && !smallScreen) {
113 return;
114 }
115
116 m_profilesModel->setProfileName(profileIndex, m_ui->name->text());
117
119 for (int i = 0; i < m_servicesModel->invisibleRootItem()->rowCount(); ++i) {
120 QStandardItem *item = m_servicesModel->invisibleRootItem()->child(i);
121 if (item->checkState() == Qt::Checked) {
123 if (m_configWidgets[m_plugins[i]]) {
124 settings = m_configWidgets[m_plugins[i]]->settings();
125 }
126 mDebug() << i << m_plugins[i]->nameId() << settings;
127 pluginSettings.insert(m_plugins[i]->nameId(), settings);
128 } else {
129 pluginSettings.remove(m_plugins[i]->nameId());
130 }
131 }
132 m_profilesModel->setProfilePluginSettings(profileIndex, pluginSettings);
133}
134
135void RoutingProfileSettingsDialog::openConfigDialog()
136{
137 QModelIndex current = m_ui->services->selectionModel()->currentIndex();
138 if (current.isValid()) {
139 RoutingRunnerPlugin *plugin = m_plugins.at(current.row());
140
141 if (!m_dialog) {
142 m_dialog = new QDialog(this);
143
144 m_dialogLayout = new QHBoxLayout();
145 m_dialogLayout->addWidget(m_configWidgets[plugin]);
146
147 m_dialog->setLayout(m_dialogLayout);
148 m_dialog->setMinimumHeight(480);
149 } else {
150 m_dialogLayout->insertWidget(0, m_configWidgets[plugin]);
151 }
152
153 m_configWidgets[plugin]->show();
154 m_dialog->setWindowTitle(plugin->guiString());
155 m_dialog->exec();
156 m_configWidgets[plugin]->hide();
157 m_dialogLayout->removeWidget(m_configWidgets[plugin]);
158 }
159}
160
161}
162
163#include "moc_RoutingProfileSettingsDialog.cpp"
Binds a QML item to a specific geodetic location in screen coordinates.
void clicked(bool checked)
virtual void accept()
virtual void reject()
iterator insert(const Key &key, const T &value)
bool remove(const Key &key)
const_reference at(qsizetype i) const const
void clear()
bool isValid() const const
int row() const const
Qt::CheckState checkState() const const
QStandardItem * child(int row, int column) const const
void setCheckState(Qt::CheckState state)
void setCheckable(bool checkable)
void setEnabled(bool enabled)
QueuedConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 12:02:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.