dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
importexportgadgetwidget.cpp
Go to the documentation of this file.
1 
15 /*
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.
20  *
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.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
30 #include "ui_importexportgadgetwidget.h"
31 #include "utils/xmlconfig.h"
33 #include "coreplugin/icore.h"
36 #include <QtDebug>
37 #include <QSettings>
38 #include <QMessageBox>
39 #include <QFileInfo>
40 #include <QFileDialog>
41 #include <QDesktopServices>
42 #include <QUrl>
43 #include <QDir>
44 
46  : QWidget(parent)
47  , ui(new Ui::ImportExportGadgetWidget)
48 {
49  setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
50  ui->setupUi(this);
51 
52  filename = "";
53 }
54 
56 {
57  delete ui;
58 }
59 
61 {
62  QWidget::changeEvent(e);
63  switch (e->type()) {
64  case QEvent::LanguageChange:
65  ui->retranslateUi(this);
66  break;
67  default:
68  break;
69  }
70 }
71 
72 void ImportExportGadgetWidget::on_exportButton_clicked()
73 {
74  QString file = filename;
75  QString filter = tr("GCS Settings file (*.xml)");
76  file = QFileDialog::getSaveFileName(this, tr("Save GCS Settings to file .."),
77  QFileInfo(file).absoluteFilePath(), filter)
78  .trimmed();
79  if (file.isEmpty()) {
80  return;
81  }
82 
83  // Add a "XML" extension to the file in case it does not exist:
84  if (!file.toLower().endsWith(".xml"))
85  file.append(".xml");
86 
87  filename = file;
88 
89  qDebug() << "Export pressed! Write to file " << QFileInfo(file).absoluteFilePath();
90 
91  QMessageBox msgBox(this);
92  QDir dir = QFileInfo(file).absoluteDir();
93  if (!dir.exists()) {
94  msgBox.setText(tr("Can't write file ") + QFileInfo(file).absoluteFilePath()
95  + " since directory " + dir.absolutePath() + " doesn't exist!");
96  msgBox.exec();
97  return;
98  }
99  exportConfiguration(file);
100 
101  msgBox.setText(tr("The settings have been exported to ") + QFileInfo(file).absoluteFilePath());
102  msgBox.exec();
103  emit done();
104 }
105 
106 QList<Core::IConfigurablePlugin *> ImportExportGadgetWidget::getConfigurables()
107 {
109 
111  ExtensionSystem::PluginManager::instance()->plugins();
112  foreach (ExtensionSystem::PluginSpec *spec, specs) {
113  if (Core::IConfigurablePlugin *plugin =
114  dynamic_cast<Core::IConfigurablePlugin *>(spec->plugin())) {
115  qDebug() << "Configurable: " << plugin->metaObject()->className();
116  configurables.append(plugin);
117  }
118  }
119  return configurables;
120 }
121 
122 void ImportExportGadgetWidget::exportConfiguration(const QString &fileName)
123 {
124  bool doGeneral = ui->checkBoxGeneral->isChecked();
125  bool doAllGadgets = ui->checkBoxAllGadgets->isChecked();
126  bool doPlugins = ui->checkBoxPlugins->isChecked();
127 
128  QSettings::Format format = XmlConfig::XmlSettingsFormat;
129  QSettings qs(fileName, format);
130 
131  if (doGeneral) {
133  }
134  if (doAllGadgets) {
136  }
137  if (doPlugins) {
138  foreach (Core::IConfigurablePlugin *plugin, getConfigurables()) {
139  Core::ICore::instance()->saveSettings(plugin, &qs);
140  }
141  }
142 
143  qDebug() << "Export ended";
144 }
145 
146 void ImportExportGadgetWidget::writeError(const QString &msg) const
147 {
148  qWarning() << "ERROR: " << msg;
149 }
150 
151 void ImportExportGadgetWidget::on_importButton_clicked()
152 {
153  QString file = filename;
154  QString filter = tr("GCS Settings file (*.xml)");
155  file = QFileDialog::getOpenFileName(this, tr("Load GCS Settings from file .."),
156  QFileInfo(file).absoluteFilePath(), filter)
157  .trimmed();
158  if (file.isEmpty()) {
159  return;
160  }
161 
162  filename = file;
163 
164  qDebug() << "Import pressed! Read from file " << QFileInfo(file).absoluteFilePath();
165 
166  QMessageBox msgBox(this);
167  if (!QFileInfo(file).isReadable()) {
168  msgBox.setText(tr("Can't read file ") + QFileInfo(file).absoluteFilePath());
169  msgBox.exec();
170  return;
171  }
172  importConfiguration(file);
173 
174  // The new configs are added to the old configs. Things are messy now.
175  msgBox.setText(tr("The settings have been imported from ") + QFileInfo(file).absoluteFilePath()
176  + tr(". Restart the application."));
177  msgBox.exec();
178  emit done();
179 }
180 
181 void ImportExportGadgetWidget::importConfiguration(const QString &fileName)
182 {
183  bool doGeneral = ui->checkBoxGeneral->isChecked();
184  bool doAllGadgets = ui->checkBoxAllGadgets->isChecked();
185  bool doPlugins = ui->checkBoxPlugins->isChecked();
186 
187  QSettings qs(fileName, XmlConfig::XmlSettingsFormat);
188 
189  if (doAllGadgets) {
191  }
192  if (doGeneral) {
194  }
195  if (doPlugins) {
196  foreach (Core::IConfigurablePlugin *plugin, getConfigurables()) {
197  Core::ICore::instance()->readSettings(plugin, &qs);
198  }
199  }
200 
201  qDebug() << "Import ended";
202 }
203 
204 void ImportExportGadgetWidget::on_helpButton_clicked()
205 {
206  qDebug() << "Show Help";
207  QDesktopServices::openUrl(
208  QUrl("https://github.com/d-ronin/dRonin/wiki/OnlineHelp:-Import-Export"));
209 }
210 
211 void ImportExportGadgetWidget::on_resetButton_clicked()
212 {
213  QMessageBox msgBox(this);
214  msgBox.setText(tr("All your settings will be deleted!"));
215  msgBox.setInformativeText(tr("You must restart the GCS in order to activate the changes."));
216  msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
217  msgBox.setDefaultButton(QMessageBox::Ok);
218  if (msgBox.exec() == QMessageBox::Ok) {
219  qDebug() << "Reset requested!";
221  } else {
222  qDebug() << "Reset canceled!";
223  return;
224  }
225 }
226 
ImportExportGadgetWidget(QWidget *parent=nullptr)
virtual void readMainSettings(QSettings *qs, bool workspaceDiffOnly=false)=0
virtual void readSettings(IConfigurablePlugin *plugin, QSettings *qs=nullptr)=0
IPlugin * plugin() const
Definition: pluginspec.cpp:341
virtual void deleteSettings()=0
Parse log file
static ICore * instance()
Definition: coreimpl.cpp:46
Contains the information of the plugins xml description file and information about the plugin's curre...
Definition: pluginspec.h:63
static const QSettings::Format XmlSettingsFormat
Definition: xmlconfig.h:43
virtual void saveSettings(IConfigurablePlugin *plugin, QSettings *qs=nullptr)=0
Definition: icore.h:39
e
Definition: OPPlots.m:99
virtual UAVGadgetInstanceManager * uavGadgetInstanceManager() const =0
virtual void saveMainSettings(QSettings *qs)=0