dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
importsummary.cpp
Go to the documentation of this file.
1 
14 /*
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  * for more details.
24  *
25  * You should have received a copy of the GNU General Public License along
26  * with this program; if not, see <http://www.gnu.org/licenses/>
27  */
28 
29 // for XML object
30 #include <QDomDocument>
31 #include <QXmlQuery>
32 
33 #include <QMessageBox>
34 
35 // for Parameterized slots
36 #include <QSignalMapper>
37 #include "importsummary.h"
38 
39 ImportSummaryDialog::ImportSummaryDialog(QWidget *parent, bool quiet)
40  : QDialog(parent)
41  , ui(new Ui::ImportSummaryDialog)
42  , importedObjects(NULL)
43  , quiet(quiet)
44 {
45  ui->setupUi(this);
46 
47  setModal(true);
48 
49  setWindowTitle(tr("Import Summary"));
50 
51  ui->importSummaryList->setColumnCount(3);
52  ui->importSummaryList->setRowCount(0);
53  QStringList header;
54  header.append("Use");
55  header.append("Name");
56  header.append("Status");
57  ui->importSummaryList->setHorizontalHeaderLabels(header);
58  ui->progressBar->setValue(0);
59 
60  connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(reject()));
61  connect(ui->btnSaveToFlash, SIGNAL(clicked()), this, SLOT(doTheApplySaving()));
62 
63  // Connect the Select All/None buttons
64  QSignalMapper *signalMapper = new QSignalMapper(this);
65 
66  connect(ui->btnSelectAll, SIGNAL(clicked()), signalMapper, SLOT(map()));
67  connect(ui->btnSelectNone, SIGNAL(clicked()), signalMapper, SLOT(map()));
68 
69  signalMapper->setMapping(ui->btnSelectAll, 1);
70  signalMapper->setMapping(ui->btnSelectNone, 0);
71 
72  connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(setCheckedState(int)));
73 
74  // Connect the help button
75  connect(ui->helpButton, SIGNAL(clicked()), this, SLOT(openHelp()));
76 }
77 
79 {
80  delete ui;
81  delete importedObjects;
82 }
83 
84 /*
85  Stores the settings that were imported
86  */
88 {
89  importedObjects = objs;
90 }
91 
92 /*
93  Open the right page on the wiki
94  */
95 void ImportSummaryDialog::openHelp()
96 {
97  QDesktopServices::openUrl(
98  QUrl("https://github.com/d-ronin/dRonin/wiki/OnlineHelp:-UAV-Settings-import-export",
99  QUrl::StrictMode));
100 }
101 
102 /*
103  Adds a new line about a UAVObject along with its status
104  (whether it got saved OK or not)
105  */
106 void ImportSummaryDialog::addLine(QString uavObjectName, QString text, bool status)
107 {
108  ui->importSummaryList->setRowCount(ui->importSummaryList->rowCount() + 1);
109  int row = ui->importSummaryList->rowCount() - 1;
110  ui->importSummaryList->setCellWidget(row, 0, new QCheckBox(ui->importSummaryList));
111  QTableWidgetItem *objName = new QTableWidgetItem(uavObjectName);
112  ui->importSummaryList->setItem(row, 1, objName);
113  QCheckBox *box = dynamic_cast<QCheckBox *>(ui->importSummaryList->cellWidget(row, 0));
114  ui->importSummaryList->setItem(row, 2, new QTableWidgetItem(text));
115 
116  // Disable editability and selectability in table elements
117  ui->importSummaryList->item(row, 1)->setFlags(Qt::NoItemFlags);
118  ui->importSummaryList->item(row, 2)->setFlags(Qt::NoItemFlags);
119 
120  if (status) {
121  box->setChecked(true);
122  } else {
123  box->setChecked(false);
124  box->setEnabled(false);
125  }
126 
127  this->repaint();
128  this->showEvent(NULL);
129 }
130 
132 {
133  return ui->importSummaryList->rowCount();
134 }
135 
136 /*
137  Sets or unsets every UAVObjet in the list
138  */
139 void ImportSummaryDialog::setCheckedState(int state)
140 {
141  for (int i = 0; i < ui->importSummaryList->rowCount(); i++) {
142  QCheckBox *box = dynamic_cast<QCheckBox *>(ui->importSummaryList->cellWidget(i, 0));
143  if (box->isEnabled())
144  box->setChecked((state == 1 ? true : false));
145  }
146 }
147 
148 /*
149  Apply and saves every checked UAVObjet in the list to Flash
150  */
151 void ImportSummaryDialog::doTheApplySaving()
152 {
153  if (!importedObjects)
154  return;
155 
156  int itemCount = 0;
157  ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
158  UAVObjectManager *boardObjManager = pm->getObject<UAVObjectManager>();
159  UAVObjectUtilManager *utilManager = pm->getObject<UAVObjectUtilManager>();
160  connect(utilManager, SIGNAL(saveCompleted(int, bool)), this, SLOT(updateCompletion()));
161 
162  for (int i = 0; i < ui->importSummaryList->rowCount(); i++) {
163  QCheckBox *box = dynamic_cast<QCheckBox *>(ui->importSummaryList->cellWidget(i, 0));
164  if (box->isChecked()) {
165  ++itemCount;
166  }
167  }
168 
169  if (itemCount == 0)
170  return;
171 
172  ui->btnSaveToFlash->setEnabled(false);
173  ui->closeButton->setEnabled(false);
174 
175  ui->progressBar->setMaximum(itemCount + 1);
176  ui->progressBar->setValue(1);
177  for (int i = 0; i < ui->importSummaryList->rowCount(); i++) {
178  QString uavObjectName = ui->importSummaryList->item(i, 1)->text();
179  QCheckBox *box = dynamic_cast<QCheckBox *>(ui->importSummaryList->cellWidget(i, 0));
180  if (box->isChecked()) {
181  UAVObject *importedObj = importedObjects->getObject(uavObjectName);
182  UAVObject *boardObj = boardObjManager->getObject(uavObjectName);
183 
184  quint8 *data = new quint8[importedObj->getNumBytes()];
185  importedObj->pack(data);
186  boardObj->unpack(data);
187  delete[] data;
188 
189  boardObj->updated();
190 
191  utilManager->saveObjectToFlash(boardObj);
192 
194  repaint();
195  }
196  }
197 
198  hide();
199 
200  if (!quiet) {
201  QMessageBox msgBox(this);
202 
203  msgBox.setText(tr("Settings saved to flash."));
204 
205  msgBox.setInformativeText(tr("You must power cycle the flight controller to apply settings "
206  "and continue configuration."));
207 
208  msgBox.setStandardButtons(QMessageBox::Ok);
209  msgBox.exec();
210  }
211 
212  accept();
213 }
214 
216 {
217  ui->progressBar->setValue(ui->progressBar->value() + 1);
218  if (ui->progressBar->value() == ui->progressBar->maximum()) {
219  ui->btnSaveToFlash->setEnabled(true);
220  ui->closeButton->setEnabled(true);
221  }
222 }
223 
225 {
226  QDialog::changeEvent(e);
227  switch (e->type()) {
228  case QEvent::LanguageChange:
229  ui->retranslateUi(this);
230  break;
231  default:
232  break;
233  }
234 }
235 
236 void ImportSummaryDialog::showEvent(QShowEvent *event)
237 {
238  Q_UNUSED(event)
239  ui->importSummaryList->resizeColumnsToContents();
240  int width = ui->importSummaryList->width()
241  - (ui->importSummaryList->columnWidth(0) + ui->importSummaryList->columnWidth(2));
242  ui->importSummaryList->setColumnWidth(1, width - 15);
243 }
qint32 unpack(const quint8 *dataIn)
Definition: uavobject.cpp:270
qint32 pack(quint8 *dataOut)
Definition: uavobject.cpp:255
Core plugin system that manages the plugins, their life cycle and their registered objects...
Definition: pluginmanager.h:53
ImportSummaryDialog(QWidget *parent=nullptr, bool quiet=false)
for i
Definition: OPPlots.m:140
void setUAVOSettings(UAVObjectManager *obj)
DataFields data
void changeEvent(QEvent *e)
void updated()
Definition: uavobject.cpp:179
void showEvent(QShowEvent *event)
void addLine(QString objectName, QString text, bool status)
quint32 getNumBytes()
Definition: uavobject.cpp:155
UAVObject * getObject(const QString &name, quint32 instId=0)
e
Definition: OPPlots.m:99