2012-02-05 21:07:19 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file smartsavebutton.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
|
|
|
* @addtogroup UAVObjectWidgetUtils Plugin
|
|
|
|
* @{
|
|
|
|
* @brief Utility plugin for UAVObject to Widget relation management
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2011-07-19 15:50:42 +02:00
|
|
|
#include "smartsavebutton.h"
|
2013-06-25 12:21:16 +02:00
|
|
|
#include "configtaskwidget.h"
|
2011-07-19 15:50:42 +02:00
|
|
|
|
2013-06-25 12:21:16 +02:00
|
|
|
smartSaveButton::smartSaveButton(ConfigTaskWidget *configTaskWidget) : configWidget(configTaskWidget)
|
2013-05-19 16:37:30 +02:00
|
|
|
{}
|
2012-02-08 21:15:14 +01:00
|
|
|
|
|
|
|
void smartSaveButton::addButtons(QPushButton *save, QPushButton *apply)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
buttonList.insert(save, save_button);
|
|
|
|
buttonList.insert(apply, apply_button);
|
|
|
|
connect(save, SIGNAL(clicked()), this, SLOT(processClick()));
|
|
|
|
connect(apply, SIGNAL(clicked()), this, SLOT(processClick()));
|
2012-02-08 21:15:14 +01:00
|
|
|
}
|
2012-02-10 17:34:07 +01:00
|
|
|
void smartSaveButton::addApplyButton(QPushButton *apply)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
buttonList.insert(apply, apply_button);
|
|
|
|
connect(apply, SIGNAL(clicked()), this, SLOT(processClick()));
|
2012-02-10 17:34:07 +01:00
|
|
|
}
|
|
|
|
void smartSaveButton::addSaveButton(QPushButton *save)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
buttonList.insert(save, save_button);
|
|
|
|
connect(save, SIGNAL(clicked()), this, SLOT(processClick()));
|
2012-02-10 17:34:07 +01:00
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
void smartSaveButton::processClick()
|
|
|
|
{
|
2011-07-26 21:35:38 +02:00
|
|
|
emit beginOp();
|
2013-05-19 16:37:30 +02:00
|
|
|
bool save = false;
|
|
|
|
QPushButton *button = qobject_cast<QPushButton *>(sender());
|
2012-02-10 17:34:07 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!button) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (buttonList.value(button) == save_button) {
|
|
|
|
save = true;
|
|
|
|
}
|
|
|
|
processOperation(button, save);
|
2012-02-10 17:34:07 +01:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void smartSaveButton::processOperation(QPushButton *button, bool save)
|
2012-02-10 17:34:07 +01:00
|
|
|
{
|
2011-07-19 15:50:42 +02:00
|
|
|
emit preProcessOperations();
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
if (button) {
|
2012-02-10 17:34:07 +01:00
|
|
|
button->setEnabled(false);
|
|
|
|
button->setIcon(QIcon(":/uploader/images/system-run.svg"));
|
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
2013-05-19 16:37:30 +02:00
|
|
|
bool error = false;
|
2011-07-19 15:50:42 +02:00
|
|
|
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjectUtilManager *utilMngr = pm->getObject<UAVObjectUtilManager>();
|
|
|
|
foreach(UAVDataObject * obj, objects) {
|
|
|
|
UAVObject::Metadata mdata = obj->getMetadata();
|
|
|
|
|
2013-06-25 22:31:10 +02:00
|
|
|
// Should we really save this object to the board?
|
2013-06-25 12:21:16 +02:00
|
|
|
if (!configWidget->shouldObjectBeSaved(obj) || UAVObject::GetGcsAccess(mdata) == UAVObject::ACCESS_READONLY) {
|
|
|
|
qDebug() << obj->getName() << "was skipped.";
|
2011-08-23 12:25:28 +02:00
|
|
|
continue;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-06-25 12:21:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
up_result = false;
|
|
|
|
current_object = obj;
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
2013-06-25 12:21:16 +02:00
|
|
|
qDebug() << "Uploading" << obj->getName() << "to board.";
|
2013-05-19 16:37:30 +02:00
|
|
|
connect(obj, SIGNAL(transactionCompleted(UAVObject *, bool)), this, SLOT(transaction_finished(UAVObject *, bool)));
|
|
|
|
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
2011-07-19 15:50:42 +02:00
|
|
|
obj->updated();
|
2013-06-25 12:21:16 +02:00
|
|
|
|
2012-05-12 22:16:36 +02:00
|
|
|
timer.start(3000);
|
2011-07-19 15:50:42 +02:00
|
|
|
loop.exec();
|
2013-06-25 12:21:16 +02:00
|
|
|
if (!timer.isActive()) {
|
2013-06-25 22:31:10 +02:00
|
|
|
qDebug() << "Upload of" << obj->getName() << "timed out.";
|
2012-07-26 14:03:27 +02:00
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
timer.stop();
|
2013-06-25 12:21:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
disconnect(obj, SIGNAL(transactionCompleted(UAVObject *, bool)), this, SLOT(transaction_finished(UAVObject *, bool)));
|
|
|
|
disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
|
|
|
if (up_result) {
|
2013-06-25 12:21:16 +02:00
|
|
|
qDebug() << "Upload of" << obj->getName() << "successful.";
|
2011-07-19 15:50:42 +02:00
|
|
|
break;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
if (up_result == false) {
|
2013-06-25 12:21:16 +02:00
|
|
|
qDebug() << "Upload of" << obj->getName() << "failed after 3 tries.";
|
2013-05-19 16:37:30 +02:00
|
|
|
error = true;
|
2011-07-19 15:50:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-06-25 12:21:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
sv_result = false;
|
|
|
|
current_objectID = obj->getObjID();
|
|
|
|
if (save && (obj->isSettings())) {
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
2013-06-25 22:31:10 +02:00
|
|
|
qDebug() << "Saving" << obj->getName() << "to board.";
|
2013-05-19 16:37:30 +02:00
|
|
|
connect(utilMngr, SIGNAL(saveCompleted(int, bool)), this, SLOT(saving_finished(int, bool)));
|
|
|
|
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
2011-08-23 12:25:28 +02:00
|
|
|
utilMngr->saveObjectToSD(obj);
|
2013-06-25 12:21:16 +02:00
|
|
|
|
2012-05-12 22:16:36 +02:00
|
|
|
timer.start(3000);
|
2011-08-23 12:25:28 +02:00
|
|
|
loop.exec();
|
2013-06-25 12:21:16 +02:00
|
|
|
if (!timer.isActive()) {
|
|
|
|
qDebug() << "Saving of" << obj->getName() << "timed out.";
|
2012-07-26 14:03:27 +02:00
|
|
|
}
|
2011-08-23 12:25:28 +02:00
|
|
|
timer.stop();
|
2013-06-25 12:21:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
disconnect(utilMngr, SIGNAL(saveCompleted(int, bool)), this, SLOT(saving_finished(int, bool)));
|
|
|
|
disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
|
|
|
if (sv_result) {
|
2013-06-25 12:21:16 +02:00
|
|
|
qDebug() << "Saving of" << obj->getName() << "successful.";
|
2011-08-23 12:25:28 +02:00
|
|
|
break;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-08-23 12:25:28 +02:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
if (sv_result == false) {
|
2013-06-25 12:21:16 +02:00
|
|
|
qDebug() << "Saving of" << obj->getName() << "failed after 3 tries.";
|
2013-05-19 16:37:30 +02:00
|
|
|
error = true;
|
2011-08-23 12:25:28 +02:00
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
if (button) {
|
2012-02-10 17:34:07 +01:00
|
|
|
button->setEnabled(true);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
if (button) {
|
2012-02-10 17:34:07 +01:00
|
|
|
button->setIcon(QIcon(":/uploader/images/dialog-apply.svg"));
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-07-26 21:35:38 +02:00
|
|
|
emit saveSuccessfull();
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
|
|
|
if (button) {
|
2012-02-10 17:34:07 +01:00
|
|
|
button->setIcon(QIcon(":/uploader/images/process-stop.svg"));
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
}
|
2011-07-26 21:35:38 +02:00
|
|
|
emit endOp();
|
2011-07-19 15:50:42 +02:00
|
|
|
}
|
|
|
|
|
2011-08-23 12:25:28 +02:00
|
|
|
void smartSaveButton::setObjects(QList<UAVDataObject *> list)
|
2011-07-19 15:50:42 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
objects = list;
|
2011-07-19 15:50:42 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void smartSaveButton::addObject(UAVDataObject *obj)
|
2011-07-19 15:50:42 +02:00
|
|
|
{
|
2012-08-15 00:02:08 +02:00
|
|
|
Q_ASSERT(obj);
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!objects.contains(obj)) {
|
2011-08-17 13:43:08 +02:00
|
|
|
objects.append(obj);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
void smartSaveButton::removeObject(UAVDataObject *obj)
|
2012-02-02 17:22:40 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (objects.contains(obj)) {
|
2012-02-02 17:22:40 +01:00
|
|
|
objects.removeAll(obj);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2012-02-02 17:22:40 +01:00
|
|
|
}
|
|
|
|
void smartSaveButton::removeAllObjects()
|
|
|
|
{
|
|
|
|
objects.clear();
|
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
void smartSaveButton::clearObjects()
|
|
|
|
{
|
|
|
|
objects.clear();
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
void smartSaveButton::transaction_finished(UAVObject *obj, bool result)
|
2011-07-19 15:50:42 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (current_object == obj) {
|
|
|
|
up_result = result;
|
2011-07-19 15:50:42 +02:00
|
|
|
loop.quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void smartSaveButton::saving_finished(int id, bool result)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (id == current_objectID) {
|
|
|
|
sv_result = result;
|
2011-07-19 15:50:42 +02:00
|
|
|
loop.quit();
|
|
|
|
}
|
|
|
|
}
|
2011-07-26 00:18:41 +02:00
|
|
|
|
|
|
|
void smartSaveButton::enableControls(bool value)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
foreach(QPushButton * button, buttonList.keys())
|
|
|
|
button->setEnabled(value);
|
2011-07-26 00:18:41 +02:00
|
|
|
}
|
2012-02-10 17:34:07 +01:00
|
|
|
|
2012-03-02 19:47:24 +01:00
|
|
|
void smartSaveButton::resetIcons()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
foreach(QPushButton * button, buttonList.keys())
|
2013-06-25 22:31:10 +02:00
|
|
|
button->setIcon(QIcon());
|
2012-03-02 19:47:24 +01:00
|
|
|
}
|
|
|
|
|
2012-02-10 17:34:07 +01:00
|
|
|
void smartSaveButton::apply()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
processOperation(NULL, false);
|
2012-02-10 17:34:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void smartSaveButton::save()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
processOperation(NULL, true);
|
2012-02-10 17:34:07 +01:00
|
|
|
}
|