LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / uavobjectwidgetutils / smartsavebutton.cpp
blob454743191818aacc44a3b3b25bfbe07ebbb05a69
1 /**
2 ******************************************************************************
4 * @file smartsavebutton.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup UAVObjectWidgetUtils Plugin
9 * @{
10 * @brief Utility plugin for UAVObject to Widget relation management
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "smartsavebutton.h"
28 #include "configtaskwidget.h"
30 SmartSaveButton::SmartSaveButton(ConfigTaskWidget *configTaskWidget) : configWidget(configTaskWidget)
33 void SmartSaveButton::addButtons(QPushButton *save, QPushButton *apply)
35 buttonList.insert(save, save_button);
36 buttonList.insert(apply, apply_button);
37 connect(save, SIGNAL(clicked()), this, SLOT(processClick()));
38 connect(apply, SIGNAL(clicked()), this, SLOT(processClick()));
40 void SmartSaveButton::addApplyButton(QPushButton *apply)
42 buttonList.insert(apply, apply_button);
43 connect(apply, SIGNAL(clicked()), this, SLOT(processClick()));
45 void SmartSaveButton::addSaveButton(QPushButton *save)
47 buttonList.insert(save, save_button);
48 connect(save, SIGNAL(clicked()), this, SLOT(processClick()));
50 void SmartSaveButton::processClick()
52 emit beginOp();
53 bool save = false;
54 QPushButton *button = qobject_cast<QPushButton *>(sender());
56 if (!button) {
57 return;
59 if (buttonList.value(button) == save_button) {
60 save = true;
62 processOperation(button, save);
65 void SmartSaveButton::processOperation(QPushButton *button, bool save)
67 emit preProcessOperations();
69 if (button) {
70 button->setEnabled(false);
71 button->setIcon(QIcon(":/uploader/images/system-run.svg"));
73 QTimer timer;
74 timer.setSingleShot(true);
75 bool error = false;
76 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
77 UAVObjectUtilManager *utilMngr = pm->getObject<UAVObjectUtilManager>();
78 foreach(UAVDataObject * obj, objects) {
79 UAVObject::Metadata mdata = obj->getMetadata();
81 // Should we really save this object to the board?
82 if (!configWidget->shouldObjectBeSaved(obj) || UAVObject::GetGcsAccess(mdata) == UAVObject::ACCESS_READONLY) {
83 qDebug() << obj->getName() << "was skipped.";
84 continue;
87 up_result = false;
88 current_object = obj;
89 for (int i = 0; i < 3; ++i) {
90 qDebug() << "Uploading" << obj->getName() << "to board.";
91 connect(obj, SIGNAL(transactionCompleted(UAVObject *, bool)), this, SLOT(transaction_finished(UAVObject *, bool)));
92 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
93 obj->updated();
95 timer.start(3000);
96 loop.exec();
97 if (!timer.isActive()) {
98 qDebug() << "Upload of" << obj->getName() << "timed out.";
100 timer.stop();
102 disconnect(obj, SIGNAL(transactionCompleted(UAVObject *, bool)), this, SLOT(transaction_finished(UAVObject *, bool)));
103 disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
104 if (up_result) {
105 qDebug() << "Upload of" << obj->getName() << "successful.";
106 break;
109 if (up_result == false) {
110 qDebug() << "Upload of" << obj->getName() << "failed after 3 tries.";
111 error = true;
112 continue;
115 sv_result = false;
116 current_objectID = obj->getObjID();
117 if (save && (obj->isSettingsObject())) {
118 for (int i = 0; i < 3; ++i) {
119 qDebug() << "Saving" << obj->getName() << "to board.";
120 connect(utilMngr, SIGNAL(saveCompleted(int, bool)), this, SLOT(saving_finished(int, bool)));
121 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
122 utilMngr->saveObjectToSD(obj);
124 timer.start(3000);
125 loop.exec();
126 if (!timer.isActive()) {
127 qDebug() << "Saving of" << obj->getName() << "timed out.";
129 timer.stop();
131 disconnect(utilMngr, SIGNAL(saveCompleted(int, bool)), this, SLOT(saving_finished(int, bool)));
132 disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
133 if (sv_result) {
134 qDebug() << "Saving of" << obj->getName() << "successful.";
135 break;
138 if (sv_result == false) {
139 qDebug() << "Saving of" << obj->getName() << "failed after 3 tries.";
140 error = true;
144 if (button) {
145 button->setEnabled(true);
147 if (!error) {
148 if (button) {
149 button->setIcon(QIcon(":/uploader/images/dialog-apply.svg"));
151 emit saveSuccessfull();
152 } else {
153 if (button) {
154 button->setIcon(QIcon(":/uploader/images/process-stop.svg"));
157 emit endOp();
160 void SmartSaveButton::setObjects(QList<UAVDataObject *> list)
162 objects = list;
165 void SmartSaveButton::addObject(UAVDataObject *obj)
167 Q_ASSERT(obj);
168 if (!objects.contains(obj)) {
169 objects.append(obj);
172 void SmartSaveButton::removeObject(UAVDataObject *obj)
174 if (objects.contains(obj)) {
175 objects.removeAll(obj);
178 void SmartSaveButton::removeAllObjects()
180 objects.clear();
182 void SmartSaveButton::clearObjects()
184 objects.clear();
186 void SmartSaveButton::transaction_finished(UAVObject *obj, bool result)
188 if (current_object == obj) {
189 up_result = result;
190 loop.quit();
194 void SmartSaveButton::saving_finished(int id, bool result)
196 if (id == (int)current_objectID) {
197 sv_result = result;
198 loop.quit();
202 void SmartSaveButton::enableControls(bool value)
204 foreach(QPushButton * button, buttonList.keys())
205 button->setEnabled(value);
208 void SmartSaveButton::resetIcons()
210 foreach(QPushButton * button, buttonList.keys())
211 button->setIcon(QIcon());
214 void SmartSaveButton::apply()
216 processOperation(NULL, false);
219 void SmartSaveButton::save()
221 processOperation(NULL, true);