2 ******************************************************************************
4 * @file smartsavebutton.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
8 * @addtogroup UAVObjectWidgetUtils Plugin
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
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()
54 QPushButton
*button
= qobject_cast
<QPushButton
*>(sender());
59 if (buttonList
.value(button
) == save_button
) {
62 processOperation(button
, save
);
65 void SmartSaveButton::processOperation(QPushButton
*button
, bool save
)
67 emit
preProcessOperations();
70 button
->setEnabled(false);
71 button
->setIcon(QIcon(":/uploader/images/system-run.svg"));
74 timer
.setSingleShot(true);
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.";
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()));
97 if (!timer
.isActive()) {
98 qDebug() << "Upload of" << obj
->getName() << "timed out.";
102 disconnect(obj
, SIGNAL(transactionCompleted(UAVObject
*, bool)), this, SLOT(transaction_finished(UAVObject
*, bool)));
103 disconnect(&timer
, SIGNAL(timeout()), &loop
, SLOT(quit()));
105 qDebug() << "Upload of" << obj
->getName() << "successful.";
109 if (up_result
== false) {
110 qDebug() << "Upload of" << obj
->getName() << "failed after 3 tries.";
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
);
126 if (!timer
.isActive()) {
127 qDebug() << "Saving of" << obj
->getName() << "timed out.";
131 disconnect(utilMngr
, SIGNAL(saveCompleted(int, bool)), this, SLOT(saving_finished(int, bool)));
132 disconnect(&timer
, SIGNAL(timeout()), &loop
, SLOT(quit()));
134 qDebug() << "Saving of" << obj
->getName() << "successful.";
138 if (sv_result
== false) {
139 qDebug() << "Saving of" << obj
->getName() << "failed after 3 tries.";
145 button
->setEnabled(true);
149 button
->setIcon(QIcon(":/uploader/images/dialog-apply.svg"));
151 emit
saveSuccessfull();
154 button
->setIcon(QIcon(":/uploader/images/process-stop.svg"));
160 void SmartSaveButton::setObjects(QList
<UAVDataObject
*> list
)
165 void SmartSaveButton::addObject(UAVDataObject
*obj
)
168 if (!objects
.contains(obj
)) {
172 void SmartSaveButton::removeObject(UAVDataObject
*obj
)
174 if (objects
.contains(obj
)) {
175 objects
.removeAll(obj
);
178 void SmartSaveButton::removeAllObjects()
182 void SmartSaveButton::clearObjects()
186 void SmartSaveButton::transaction_finished(UAVObject
*obj
, bool result
)
188 if (current_object
== obj
) {
194 void SmartSaveButton::saving_finished(int id
, bool result
)
196 if (id
== (int)current_objectID
) {
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);