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
11 * @addtogroup importexportplugin
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
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>
41 #include <QMessageBox>
43 #include <QFileDialog>
44 #include <QDesktopServices>
48 ImportExportGadgetWidget::ImportExportGadgetWidget(QWidget
*parent
) :
50 ui(new Ui::ImportExportGadgetWidget
)
52 setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::MinimumExpanding
);
58 ImportExportGadgetWidget::~ImportExportGadgetWidget()
63 void ImportExportGadgetWidget::changeEvent(QEvent
*e
)
65 QWidget::changeEvent(e
);
68 case QEvent::LanguageChange
:
69 ui
->retranslateUi(this);
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();
86 // Add a "XML" extension to the file in case it does not exist:
87 if (!file
.toLower().endsWith(".xml")) {
93 qDebug() << "Export pressed! Write to file " << QFileInfo(file
).absoluteFilePath();
96 QDir dir
= QFileInfo(file
).absoluteDir();
98 msgBox
.setText(tr("Can't write file ") + QFileInfo(file
).absoluteFilePath()
99 + " since directory " + dir
.absolutePath() + " doesn't exist!");
103 exportConfiguration(file
);
105 msgBox
.setText(tr("The settings have been exported to ") + QFileInfo(file
).absoluteFilePath());
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
);
133 Core::ICore::instance()->saveMainSettings(settings
);
136 Core::ICore::instance()->uavGadgetInstanceManager()->saveSettings(settings
);
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()) {
165 qDebug() << "Import pressed! Read from file " << QFileInfo(file
).absoluteFilePath();
168 if (!QFileInfo(file
).isReadable()) {
169 msgBox
.setText(tr("Can't read file ") + QFileInfo(file
).absoluteFilePath());
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."));
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
);
191 Core::ICore::instance()->uavGadgetInstanceManager()->readSettings(settings
);
194 Core::ICore::instance()->readMainSettings(settings
);
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()
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();
223 qDebug() << "Reset canceled!";