dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
configplugin.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 #include "configplugin.h"
28 #include "configgadgetfactory.h"
29 #include <QtPlugin>
30 #include <QMainWindow>
31 #include <QStringList>
32 #include <QTimer>
34 #include "calibration.h"
35 #include "objectpersistence.h"
36 
37 ConfigPlugin::ConfigPlugin()
38 {
39  // Do nothing
40 }
41 
42 ConfigPlugin::~ConfigPlugin()
43 {
44  removeObject(this);
45 }
46 
47 bool ConfigPlugin::initialize(const QStringList &args, QString *errMsg)
48 {
49  Q_UNUSED(args);
50  Q_UNUSED(errMsg);
51  cf = new ConfigGadgetFactory(this);
53 
54  Calibration *cal = new Calibration();
56 
57  // Add Menu entry to erase all settings
60 
61  // Command to erase all settings from the board
62  cmd = am->registerAction(new QAction(this), "ConfigPlugin.EraseAll",
64  cmd->action()->setText(tr("Erase all settings from board..."));
65 
66  ac->menu()->addSeparator();
67  ac->appendGroup("Utilities");
68  ac->addAction(cmd, "Utilities");
69 
70  connect(cmd->action(), &QAction::triggered, this, &ConfigPlugin::eraseAllSettings);
71 
72  // *********************
73  // Listen to autopilot connection events
74  ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
75  TelemetryManager *telMngr = pm->getObject<TelemetryManager>();
76  connect(telMngr, &TelemetryManager::connected, this, &ConfigPlugin::onAutopilotConnect);
77  connect(telMngr, &TelemetryManager::disconnected, this, &ConfigPlugin::onAutopilotDisconnect);
78 
79  cmd->action()->setEnabled(false);
80 
81  addObject(this);
82 
83  return true;
84 }
85 
89 UAVObjectManager *ConfigPlugin::getObjectManager()
90 {
91  ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
92  UAVObjectManager *objMngr = pm->getObject<UAVObjectManager>();
93  Q_ASSERT(objMngr);
94  return objMngr;
95 }
96 
97 void ConfigPlugin::extensionsInitialized() {}
98 
99 void ConfigPlugin::shutdown()
100 {
101  // Do nothing
102 }
103 
107 void ConfigPlugin::onAutopilotConnect()
108 {
109  // erase action
110  cmd->action()->setEnabled(true);
111 
112  auto pm = ExtensionSystem::PluginManager::instance();
113  auto uavoUtilManager = pm->getObject<UAVObjectUtilManager>();
114 
115  if (uavoUtilManager->firmwareHashMatchesGcs()) {
116  // check if the board has been configured for flight
117  // if not, let's pop setup wizard automatically
118  if (!uavoUtilManager->boardConfigured())
119  Q_EMIT launchSetupWizard();
120  } else {
125  }
126 }
127 
131 void ConfigPlugin::onAutopilotDisconnect()
132 {
133  cmd->action()->setEnabled(false);
134 }
135 
139 void ConfigPlugin::eraseAllSettings()
140 {
141  QMessageBox msgBox(dynamic_cast<QWidget *>(Core::ICore::instance()->mainWindow()));
142  msgBox.setText(tr("Are you sure you want to erase all board settings?."));
143  msgBox.setInformativeText(tr("All settings stored in your board flash will be deleted."));
144  msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
145  msgBox.setDefaultButton(QMessageBox::Ok);
146  if (msgBox.exec() != QMessageBox::Ok)
147  return;
148 
149  settingsErased = false;
150 
151  // TODO: Replace the second and third [in eraseDone()] pop-up dialogs with a progress indicator,
152  // counter, or infinite chain of `......` tied to the original dialog box
153  msgBox.setText(tr("Settings will now erase."));
154  msgBox.setInformativeText(tr("Press <OK> and then please wait until a completion box appears. "
155  "This can take up to %1 seconds.")
156  .arg(FLASH_ERASE_TIMEOUT_MS / 1000));
157  msgBox.setStandardButtons(QMessageBox::Ok);
158  msgBox.exec();
159 
160  ObjectPersistence *objper = ObjectPersistence::GetInstance(getObjectManager());
161  Q_ASSERT(objper);
162 
163  connect(objper, &UAVObject::objectUpdated, this, &ConfigPlugin::eraseDone);
164 
165  ObjectPersistence::DataFields data = objper->getData();
166  data.Operation = ObjectPersistence::OPERATION_FULLERASE;
167 
168  // No need for manual updated event, this is triggered by setData
169  // based on UAVO meta data
170  objper->setData(data);
171  objper->updated();
172  QTimer::singleShot(FLASH_ERASE_TIMEOUT_MS, this, &ConfigPlugin::eraseFailed);
173 }
174 
175 void ConfigPlugin::eraseFailed()
176 {
177  if (settingsErased)
178  return;
179 
180  ObjectPersistence *objper = ObjectPersistence::GetInstance(getObjectManager());
181 
182  disconnect(objper, &UAVObject::objectUpdated, this, &ConfigPlugin::eraseDone);
183 
184  (void)QMessageBox::critical(
185  dynamic_cast<QWidget *>(Core::ICore::instance()->mainWindow()),
186  tr("Error erasing settings"),
187  tr("Power-cycle your board after removing all blades. Settings might be inconsistent."),
188  QMessageBox::Ok);
189 }
190 
191 void ConfigPlugin::eraseDone(UAVObject *obj)
192 {
193  Q_UNUSED(obj)
194  QMessageBox msgBox(dynamic_cast<QWidget *>(Core::ICore::instance()->mainWindow()));
195  ObjectPersistence *objper = ObjectPersistence::GetInstance(getObjectManager());
196  ObjectPersistence::DataFields data = objper->getData();
197  Q_ASSERT(obj->getInstID() == objper->getInstID());
198 
199  if (data.Operation != ObjectPersistence::OPERATION_COMPLETED) {
200  return;
201  }
202 
203  disconnect(objper, &UAVObject::objectUpdated, this, &ConfigPlugin::eraseDone);
204  if (data.Operation == ObjectPersistence::OPERATION_COMPLETED) {
205  settingsErased = true;
206  msgBox.setText(tr("Settings are now erased."));
207  msgBox.setInformativeText(tr("Please ensure that the status LED is flashing regularly and "
208  "then power-cycle your board."));
209  } else {
210  msgBox.setText(tr("Error trying to erase settings."));
211  msgBox.setInformativeText(tr(
212  "Power-cycle your board after removing all blades. Settings might be inconsistent."));
213  }
214  msgBox.setStandardButtons(QMessageBox::Ok);
215  msgBox.setDefaultButton(QMessageBox::Ok);
216  msgBox.exec();
217 }
void addObject(QObject *obj)
Definition: iplugin.cpp:291
void launchSetupWizard()
virtual QAction * action() const =0
virtual Command * registerAction(QAction *action, const QString &id, const QList< int > &context)=0
Makes an action known to the system under the specified string id.
const char *const M_TOOLS
Definition: coreconstants.h:83
The Calibration class is a UI free algorithm that can be connected to any interfaces. As such it only communicates with the UI via signals and slots, but has no direct handles to any particular controls or widgets.
Definition: calibration.h:46
virtual ActionContainer * actionContainer(const QString &id) const =0
Returns the IActionContainter object that is know to the system under the given string id...
Core plugin system that manages the plugins, their life cycle and their registered objects...
Definition: pluginmanager.h:53
DataFields getData()
virtual ActionManager * actionManager() const =0
Returns the application's action manager.
void objectUpdated(UAVObject *obj)
Signal sent whenever any field of the object is updated.
DataFields data
virtual QMenu * menu() const =0
const int C_GLOBAL_ID
Definition: coreconstants.h:90
static ICore * instance()
Definition: coreimpl.cpp:46
virtual void appendGroup(const QString &group)=0
virtual void addAction(Core::Command *action, const QString &group=QString())=0
void removeObject(QObject *obj)
Definition: iplugin.cpp:317
The action manager is responsible for registration of menus and menu items and keyboard shortcuts...
Definition: actionmanager.h:47
Gui-less support class for calibration.
void(NAME)
void addAutoReleasedObject(QObject *obj)
Definition: iplugin.cpp:306