Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / importexport / importexportgadgetwidget.cpp
blobe94ba6f89b5cb848cb2f99fcee4feb45b7867585
1 /**
2 ******************************************************************************
4 * @file importexportgadgetwidget.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015
6 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
7 * @see The GNU Public License (GPL) Version 3
8 * @brief Widget for Import/Export Plugin
9 * @addtogroup GCSPlugins GCS Plugins
10 * @{
11 * @addtogroup importexportplugin
12 * @{
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "importexportgadgetwidget.h"
32 #include "ui_importexportgadgetwidget.h"
33 #include "utils/xmlconfig.h"
34 #include "coreplugin/uavgadgetinstancemanager.h"
35 #include "coreplugin/icore.h"
36 #include <extensionsystem/pluginmanager.h>
37 #include <extensionsystem/pluginspec.h>
39 #include <QDebug>
40 #include <QSettings>
41 #include <QMessageBox>
42 #include <QFileInfo>
43 #include <QFileDialog>
44 #include <QDesktopServices>
45 #include <QUrl>
46 #include <QDir>
48 ImportExportGadgetWidget::ImportExportGadgetWidget(QWidget *parent) :
49 QWidget(parent),
50 ui(new Ui::ImportExportGadgetWidget)
52 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
53 ui->setupUi(this);
55 filename = "";
58 ImportExportGadgetWidget::~ImportExportGadgetWidget()
60 delete ui;
63 void ImportExportGadgetWidget::changeEvent(QEvent *e)
65 QWidget::changeEvent(e);
67 switch (e->type()) {
68 case QEvent::LanguageChange:
69 ui->retranslateUi(this);
70 break;
71 default:
72 break;
76 void ImportExportGadgetWidget::on_exportButton_clicked()
78 QString file = filename;
79 QString filter = tr("GCS Settings file (*.xml)");
81 file = QFileDialog::getSaveFileName(this, tr("Save GCS Settings to file..."), QFileInfo(file).absoluteFilePath(), filter).trimmed();
82 if (file.isEmpty()) {
83 return;
86 // Add a "XML" extension to the file in case it does not exist:
87 if (!file.toLower().endsWith(".xml")) {
88 file.append(".xml");
91 filename = file;
93 qDebug() << "Export pressed! Write to file " << QFileInfo(file).absoluteFilePath();
95 QMessageBox msgBox;
96 QDir dir = QFileInfo(file).absoluteDir();
97 if (!dir.exists()) {
98 msgBox.setText(tr("Can't write file ") + QFileInfo(file).absoluteFilePath()
99 + " since directory " + dir.absolutePath() + " doesn't exist!");
100 msgBox.exec();
101 return;
103 exportConfiguration(file);
105 msgBox.setText(tr("The settings have been exported to ") + QFileInfo(file).absoluteFilePath());
106 msgBox.exec();
107 emit done();
110 QList<Core::IConfigurablePlugin *> ImportExportGadgetWidget::getConfigurables()
112 QList<Core::IConfigurablePlugin *> configurables;
114 QList<ExtensionSystem::PluginSpec *> specs = ExtensionSystem::PluginManager::instance()->plugins();
115 foreach(ExtensionSystem::PluginSpec * spec, specs) {
116 if (Core::IConfigurablePlugin * plugin = dynamic_cast<Core::IConfigurablePlugin *>(spec->plugin())) {
117 qDebug() << "Configurable: " << plugin->metaObject()->className();
118 configurables.append(plugin);
121 return configurables;
124 void ImportExportGadgetWidget::exportConfiguration(const QString & fileName)
126 bool doGeneral = ui->checkBoxGeneral->isChecked();
127 bool doAllGadgets = ui->checkBoxAllGadgets->isChecked();
128 bool doPlugins = ui->checkBoxPlugins->isChecked();
130 QSettings settings(fileName, XmlConfig::XmlFormat);
132 if (doGeneral) {
133 Core::ICore::instance()->saveMainSettings(settings);
135 if (doAllGadgets) {
136 Core::ICore::instance()->uavGadgetInstanceManager()->saveSettings(settings);
138 if (doPlugins) {
139 foreach(Core::IConfigurablePlugin * plugin, getConfigurables()) {
140 Core::ICore::instance()->saveSettings(plugin, settings);
144 qDebug() << "Export ended";
148 void ImportExportGadgetWidget::writeError(const QString & msg) const
150 qWarning() << "ERROR: " << msg;
153 void ImportExportGadgetWidget::on_importButton_clicked()
155 QString file = filename;
156 QString filter = tr("GCS Settings file (*.xml)");
158 file = QFileDialog::getOpenFileName(this, tr("Load GCS Settings from file .."), QFileInfo(file).absoluteFilePath(), filter).trimmed();
159 if (file.isEmpty()) {
160 return;
163 filename = file;
165 qDebug() << "Import pressed! Read from file " << QFileInfo(file).absoluteFilePath();
167 QMessageBox msgBox;
168 if (!QFileInfo(file).isReadable()) {
169 msgBox.setText(tr("Can't read file ") + QFileInfo(file).absoluteFilePath());
170 msgBox.exec();
171 return;
173 importConfiguration(file);
175 // The new configs are added to the old configs. Things are messy now.
176 msgBox.setText(tr("The settings have been imported from ") + QFileInfo(file).absoluteFilePath()
177 + tr(". Restart the application."));
178 msgBox.exec();
179 emit done();
182 void ImportExportGadgetWidget::importConfiguration(const QString & fileName)
184 bool doGeneral = ui->checkBoxGeneral->isChecked();
185 bool doAllGadgets = ui->checkBoxAllGadgets->isChecked();
186 bool doPlugins = ui->checkBoxPlugins->isChecked();
188 QSettings settings(fileName, XmlConfig::XmlFormat);
190 if (doAllGadgets) {
191 Core::ICore::instance()->uavGadgetInstanceManager()->readSettings(settings);
193 if (doGeneral) {
194 Core::ICore::instance()->readMainSettings(settings);
196 if (doPlugins) {
197 foreach(Core::IConfigurablePlugin * plugin, getConfigurables()) {
198 Core::ICore::instance()->readSettings(plugin, settings);
202 qDebug() << "Import ended";
205 void ImportExportGadgetWidget::on_helpButton_clicked()
207 QDesktopServices::openUrl(QUrl(QString(WIKI_URL_ROOT) + QString("Import+and+Export+Settings")));
211 void ImportExportGadgetWidget::on_resetButton_clicked()
213 QMessageBox msgBox;
215 msgBox.setText(tr("All your settings will be deleted!"));
216 msgBox.setInformativeText(tr("You must restart the GCS in order to activate the changes."));
217 msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
218 msgBox.setDefaultButton(QMessageBox::Ok);
219 if (msgBox.exec() == QMessageBox::Ok) {
220 qDebug() << "Reset requested!";
221 Core::ICore::instance()->deleteSettings();
222 } else {
223 qDebug() << "Reset canceled!";
224 return;
230 * @}
231 * @}