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"
|
|
|
|
|
2012-02-08 21:15:14 +01:00
|
|
|
smartSaveButton::smartSaveButton()
|
2011-07-19 15:50:42 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2012-02-08 21:15:14 +01:00
|
|
|
|
|
|
|
void smartSaveButton::addButtons(QPushButton *save, QPushButton *apply)
|
|
|
|
{
|
|
|
|
buttonList.insert(save,save_button);
|
|
|
|
buttonList.insert(apply,apply_button);
|
|
|
|
connect(save,SIGNAL(clicked()),this,SLOT(processClick()));
|
|
|
|
connect(apply,SIGNAL(clicked()),this,SLOT(processClick()));
|
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
void smartSaveButton::processClick()
|
|
|
|
{
|
2011-07-26 21:35:38 +02:00
|
|
|
emit beginOp();
|
2011-07-19 15:50:42 +02:00
|
|
|
bool save=false;
|
2012-02-08 21:15:14 +01:00
|
|
|
QPushButton *button=qobject_cast<QPushButton *>(sender());
|
|
|
|
if(!button)
|
|
|
|
return;
|
|
|
|
if(buttonList.value(button)==save_button)
|
2011-07-19 15:50:42 +02:00
|
|
|
save=true;
|
|
|
|
emit preProcessOperations();
|
|
|
|
button->setEnabled(false);
|
|
|
|
button->setIcon(QIcon(":/uploader/images/system-run.svg"));
|
|
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
bool error=false;
|
|
|
|
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
|
|
|
UAVObjectUtilManager* utilMngr = pm->getObject<UAVObjectUtilManager>();
|
2011-08-23 12:25:28 +02:00
|
|
|
foreach(UAVDataObject * obj,objects)
|
2011-07-19 15:50:42 +02:00
|
|
|
{
|
2011-08-23 12:25:28 +02:00
|
|
|
UAVObject::Metadata mdata= obj->getMetadata();
|
|
|
|
if(mdata.gcsAccess==UAVObject::ACCESS_READONLY)
|
|
|
|
continue;
|
2011-07-19 15:50:42 +02:00
|
|
|
up_result=false;
|
|
|
|
current_object=obj;
|
|
|
|
for(int i=0;i<3;++i)
|
|
|
|
{
|
|
|
|
|
|
|
|
connect(obj,SIGNAL(transactionCompleted(UAVObject*,bool)),this,SLOT(transaction_finished(UAVObject*, bool)));
|
|
|
|
connect(&timer,SIGNAL(timeout()),&loop,SLOT(quit()));
|
|
|
|
obj->updated();
|
|
|
|
timer.start(1000);
|
2011-08-23 12:25:28 +02:00
|
|
|
//qDebug()<<"begin loop";
|
2011-07-19 15:50:42 +02:00
|
|
|
loop.exec();
|
2011-08-23 12:25:28 +02:00
|
|
|
//qDebug()<<"end loop";
|
2011-07-19 15:50:42 +02:00
|
|
|
timer.stop();
|
|
|
|
disconnect(obj,SIGNAL(transactionCompleted(UAVObject*,bool)),this,SLOT(transaction_finished(UAVObject*, bool)));
|
|
|
|
disconnect(&timer,SIGNAL(timeout()),&loop,SLOT(quit()));
|
|
|
|
if(up_result)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(up_result==false)
|
|
|
|
{
|
2011-09-27 23:10:39 +02:00
|
|
|
qDebug()<<"Object upload error:"<<obj->getName();
|
2011-07-19 15:50:42 +02:00
|
|
|
error=true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sv_result=false;
|
|
|
|
current_objectID=obj->getObjID();
|
2011-08-23 12:25:28 +02:00
|
|
|
if(save && (obj->isSettings()))
|
2011-07-19 15:50:42 +02:00
|
|
|
{
|
2011-08-23 12:25:28 +02:00
|
|
|
for(int i=0;i<3;++i)
|
|
|
|
{
|
2011-09-27 23:10:39 +02:00
|
|
|
qDebug()<<"try to save:"<<obj->getName();
|
2011-08-23 12:25:28 +02:00
|
|
|
connect(utilMngr,SIGNAL(saveCompleted(int,bool)),this,SLOT(saving_finished(int,bool)));
|
|
|
|
connect(&timer,SIGNAL(timeout()),&loop,SLOT(quit()));
|
|
|
|
utilMngr->saveObjectToSD(obj);
|
|
|
|
timer.start(1000);
|
|
|
|
loop.exec();
|
|
|
|
timer.stop();
|
|
|
|
disconnect(utilMngr,SIGNAL(saveCompleted(int,bool)),this,SLOT(saving_finished(int,bool)));
|
|
|
|
disconnect(&timer,SIGNAL(timeout()),&loop,SLOT(quit()));
|
|
|
|
if(sv_result)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(sv_result==false)
|
|
|
|
{
|
2011-09-27 23:10:39 +02:00
|
|
|
qDebug()<<"failed to save:"<<obj->getName();
|
2011-08-23 12:25:28 +02:00
|
|
|
error=true;
|
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
button->setEnabled(true);
|
|
|
|
if(!error)
|
|
|
|
{
|
|
|
|
button->setIcon(QIcon(":/uploader/images/dialog-apply.svg"));
|
2011-07-26 21:35:38 +02:00
|
|
|
emit saveSuccessfull();
|
2011-07-19 15:50:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
button->setIcon(QIcon(":/uploader/images/process-stop.svg"));
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
objects=list;
|
|
|
|
}
|
|
|
|
|
2011-08-23 12:25:28 +02:00
|
|
|
void smartSaveButton::addObject(UAVDataObject * obj)
|
2011-07-19 15:50:42 +02:00
|
|
|
{
|
2011-08-17 13:43:08 +02:00
|
|
|
if(!objects.contains(obj))
|
|
|
|
objects.append(obj);
|
2011-07-19 15:50:42 +02:00
|
|
|
}
|
2012-02-02 17:22:40 +01:00
|
|
|
void smartSaveButton::removeObject(UAVDataObject * obj)
|
|
|
|
{
|
|
|
|
if(objects.contains(obj))
|
|
|
|
objects.removeAll(obj);
|
|
|
|
}
|
|
|
|
void smartSaveButton::removeAllObjects()
|
|
|
|
{
|
|
|
|
objects.clear();
|
|
|
|
}
|
2011-07-19 15:50:42 +02:00
|
|
|
void smartSaveButton::clearObjects()
|
|
|
|
{
|
|
|
|
objects.clear();
|
|
|
|
}
|
|
|
|
void smartSaveButton::transaction_finished(UAVObject* obj, bool result)
|
|
|
|
{
|
|
|
|
if(current_object==obj)
|
|
|
|
{
|
|
|
|
up_result=result;
|
|
|
|
loop.quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void smartSaveButton::saving_finished(int id, bool result)
|
|
|
|
{
|
|
|
|
if(id==current_objectID)
|
|
|
|
{
|
|
|
|
sv_result=result;
|
2011-08-23 12:25:28 +02:00
|
|
|
//qDebug()<<"saving_finished 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)
|
|
|
|
{
|
2012-02-08 21:15:14 +01:00
|
|
|
foreach(QPushButton * button,buttonList.keys())
|
|
|
|
button->setEnabled(value);
|
2011-07-26 00:18:41 +02:00
|
|
|
}
|