1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-30 15:52:12 +01:00

Improve UAVObjects export and import file formats

Changed file format to be able to save settings and data together.
Old settings files still can be imported. Data export will also contain
settings, so we always have the system settings with the system state.

Sample file:

<!DOCTYPE UAVObjects>
<uavobjects>
    <version>
        ...
    </version>
    <data>
        <object id="0xC409985A" name="AccessoryDesired">
            <field values="0" name="AccessoryVal"/>
        </object>
        ...
    </data>
    <settings>
        <object id="0xF2875746" name="ActuatorSettings">
        ...
    </settings>
</uavobjects>
This commit is contained in:
Oleg Semyonov 2012-01-15 18:33:47 +02:00
parent 421cf89544
commit 873ff617ab
2 changed files with 119 additions and 89 deletions

View File

@ -31,6 +31,7 @@
#include <QDebug> #include <QDebug>
#include <QCheckBox> #include <QCheckBox>
#include "importsummary.h" #include "importsummary.h"
// for menu item // for menu item
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
@ -49,20 +50,18 @@
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
UAVSettingsImportExportFactory::~UAVSettingsImportExportFactory() UAVSettingsImportExportFactory::~UAVSettingsImportExportFactory()
{ {
// Do nothing // Do nothing
} }
UAVSettingsImportExportFactory::UAVSettingsImportExportFactory(QObject * parent):QObject(parent) UAVSettingsImportExportFactory::UAVSettingsImportExportFactory(QObject *parent):QObject(parent)
{ {
// Add Menu entry // Add Menu entry
Core::ActionManager* am = Core::ICore::instance()->actionManager(); Core::ActionManager *am = Core::ICore::instance()->actionManager();
Core::ActionContainer* ac = am->actionContainer(Core::Constants::M_FILE); Core::ActionContainer *ac = am->actionContainer(Core::Constants::M_FILE);
Core::Command* cmd = am->registerAction(new QAction(this), Core::Command *cmd = am->registerAction(new QAction(this),
"UAVSettingsImportExportPlugin.UAVSettingsExport", "UAVSettingsImportExportPlugin.UAVSettingsExport",
QList<int>() << QList<int>() <<
Core::Constants::C_GLOBAL_ID); Core::Constants::C_GLOBAL_ID);
@ -95,7 +94,7 @@ void UAVSettingsImportExportFactory::importUAVSettings()
{ {
// ask for file name // ask for file name
QString fileName; QString fileName;
QString filters = tr("UAVSettings XML files (*.uav);; XML files (*.xml)"); QString filters = tr("UAVObjects XML files (*.uav);; XML files (*.xml)");
fileName = QFileDialog::getOpenFileName(0, tr("Import UAV Settings"), "", filters); fileName = QFileDialog::getOpenFileName(0, tr("Import UAV Settings"), "", filters);
if (fileName.isEmpty()) { if (fileName.isEmpty()) {
return; return;
@ -103,7 +102,7 @@ void UAVSettingsImportExportFactory::importUAVSettings()
// Now open the file // Now open the file
QFile file(fileName); QFile file(fileName);
QDomDocument doc("UAVSettings"); QDomDocument doc("UAVObjects");
file.open(QFile::ReadOnly|QFile::Text); file.open(QFile::ReadOnly|QFile::Text);
if (!doc.setContent(file.readAll())) { if (!doc.setContent(file.readAll())) {
QMessageBox msgBox; QMessageBox msgBox;
@ -114,13 +113,19 @@ void UAVSettingsImportExportFactory::importUAVSettings()
return; return;
} }
file.close(); file.close();
// find the root of settings subtree
emit importAboutToBegin(); emit importAboutToBegin();
qDebug()<<"Import about to begin"; qDebug()<<"Import about to begin";
QDomElement root = doc.documentElement(); QDomElement root = doc.documentElement();
if (root.tagName() != "settings") { if (root.tagName() == "uavobjects") {
root = root.firstChildElement("settings");
}
if (root.isNull() || (root.tagName() != "settings")) {
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setText(tr("Wrong file contents.")); msgBox.setText(tr("Wrong file contents"));
msgBox.setInformativeText(tr("This file is not a correct UAVSettings file")); msgBox.setInformativeText(tr("This file does not contain correct UAVSettings"));
msgBox.setStandardButtons(QMessageBox::Ok); msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec(); msgBox.exec();
return; return;
@ -138,85 +143,86 @@ void UAVSettingsImportExportFactory::importUAVSettings()
while (!node.isNull()) { while (!node.isNull()) {
QDomElement e = node.toElement(); QDomElement e = node.toElement();
if (e.tagName() == "object") { if (e.tagName() == "object") {
// - Read each object
QString uavObjectName = e.attribute("name");
uint uavObjectID = e.attribute("id").toUInt(NULL,16);
// Sanity Check: // - Read each object
UAVObject* obj = objManager->getObject(uavObjectName); QString uavObjectName = e.attribute("name");
if (obj == NULL) { uint uavObjectID = e.attribute("id").toUInt(NULL,16);
// This object is unknown!
qDebug() << "Object unknown:" << uavObjectName << uavObjectID;
swui.addLine(uavObjectName, "Error (Object unknown)", false);
} else { // Sanity Check:
// - Update each field UAVObject *obj = objManager->getObject(uavObjectName);
// - Issue and "updated" command if (obj == NULL) {
bool error=false; // This object is unknown!
bool setError=false; qDebug() << "Object unknown:" << uavObjectName << uavObjectID;
QDomNode field = node.firstChild(); swui.addLine(uavObjectName, "Error (Object unknown)", false);
while(!field.isNull()) { } else {
QDomElement f = field.toElement(); // - Update each field
if (f.tagName() == "field") { // - Issue and "updated" command
UAVObjectField *uavfield = obj->getField(f.attribute("name")); bool error = false;
if (uavfield) { bool setError = false;
QStringList list = f.attribute("values").split(","); QDomNode field = node.firstChild();
if (list.length() == 1) { while(!field.isNull()) {
if (false == uavfield->checkValue(f.attribute("values"))) { QDomElement f = field.toElement();
qDebug() << "checkValue returned false on: " << uavObjectName << f.attribute("values"); if (f.tagName() == "field") {
setError = true; UAVObjectField *uavfield = obj->getField(f.attribute("name"));
} else if (uavfield) {
uavfield->setValue(f.attribute("values")); QStringList list = f.attribute("values").split(",");
} else { if (list.length() == 1) {
// This is an enum: if (false == uavfield->checkValue(f.attribute("values"))) {
int i=0; qDebug() << "checkValue returned false on: " << uavObjectName << f.attribute("values");
QStringList list = f.attribute("values").split(","); setError = true;
foreach (QString element, list) { } else {
if (false == uavfield->checkValue(element,i)) { uavfield->setValue(f.attribute("values"));
qDebug() << "checkValue(list) returned false on: " << uavObjectName << list; }
setError = true; } else {
} else // This is an enum:
int i = 0;
QStringList list = f.attribute("values").split(",");
foreach (QString element, list) {
if (false == uavfield->checkValue(element, i)) {
qDebug() << "checkValue(list) returned false on: " << uavObjectName << list;
setError = true;
} else {
uavfield->setValue(element,i); uavfield->setValue(element,i);
i++; }
} i++;
} }
} else { }
error = true; } else {
} error = true;
} }
field = field.nextSibling(); }
} field = field.nextSibling();
obj->updated(); }
if (error) { obj->updated();
swui.addLine(uavObjectName, "Warning (Object field unknown)", true);
} else if (uavObjectID != obj->getObjID()) { if (error) {
qDebug() << "Mismatch for Object " << uavObjectName << uavObjectID << " - " << obj->getObjID(); swui.addLine(uavObjectName, "Warning (Object field unknown)", true);
swui.addLine(uavObjectName, "Warning (ObjectID mismatch)", true); } else if (uavObjectID != obj->getObjID()) {
} else if (setError) { qDebug() << "Mismatch for Object " << uavObjectName << uavObjectID << " - " << obj->getObjID();
swui.addLine(uavObjectName, "Warning (Objects field value(s) invalid)", false); swui.addLine(uavObjectName, "Warning (ObjectID mismatch)", true);
} else } else if (setError) {
swui.addLine(uavObjectName, "OK", true); swui.addLine(uavObjectName, "Warning (Objects field value(s) invalid)", false);
} } else {
swui.addLine(uavObjectName, "OK", true);
}
}
} }
node = node.nextSibling(); node = node.nextSibling();
} }
qDebug()<<"End import"; qDebug() << "End import";
swui.exec(); swui.exec();
} }
// Create an XML document from UAVObject database // Create an XML document from UAVObject database
QString UAVSettingsImportExportFactory::createXMLDocument( QString UAVSettingsImportExportFactory::createXMLDocument(const enum storedData what, const bool fullExport)
const QString docName, const bool isSettings, const bool fullExport)
{ {
// generate an XML first (used for all export formats as a formatted data source) // generate an XML first (used for all export formats as a formatted data source)
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
UAVObjectManager *objManager = pm->getObject<UAVObjectManager>(); UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
QDomDocument doc(docName); // create an XML root
QDomElement root = doc.createElement(isSettings ? "settings" : "data"); QDomDocument doc("UAVObjects");
QDomElement root = doc.createElement("uavobjects");
doc.appendChild(root); doc.appendChild(root);
// add hardware, firmware and GCS version info // add hardware, firmware and GCS version info
@ -249,11 +255,31 @@ QString UAVSettingsImportExportFactory::createXMLDocument(
gcs.setAttribute("tag", gcsGitTag); gcs.setAttribute("tag", gcsGitTag);
versionInfo.appendChild(gcs); versionInfo.appendChild(gcs);
// create settings and/or data elements
QDomElement settings = doc.createElement("settings");
QDomElement data = doc.createElement("data");
switch (what)
{
case Settings:
root.appendChild(settings);
break;
case Data:
root.appendChild(data);
break;
case Both:
root.appendChild(data);
root.appendChild(settings);
break;
}
// iterate over settings objects // iterate over settings objects
QList< QList<UAVDataObject*> > objList = objManager->getDataObjects(); QList< QList<UAVDataObject*> > objList = objManager->getDataObjects();
foreach (QList<UAVDataObject*> list, objList) { foreach (QList<UAVDataObject*> list, objList) {
foreach (UAVDataObject* obj, list) { foreach (UAVDataObject *obj, list) {
if (obj->isSettings() == isSettings) { if (((what == Settings) && obj->isSettings()) ||
((what == Data) && !obj->isSettings()) ||
(what == Both)) {
// add each object to the XML // add each object to the XML
QDomElement o = doc.createElement("object"); QDomElement o = doc.createElement("object");
@ -292,7 +318,12 @@ QString UAVSettingsImportExportFactory::createXMLDocument(
} }
o.appendChild(f); o.appendChild(f);
} }
root.appendChild(o);
// append to the settings or data element
if (obj->isSettings())
settings.appendChild(o);
else
data.appendChild(o);
} }
} }
} }
@ -305,15 +336,15 @@ void UAVSettingsImportExportFactory::exportUAVSettings()
{ {
// ask for file name // ask for file name
QString fileName; QString fileName;
QString filters = tr("UAVSettings XML files (*.uav)"); QString filters = tr("UAVObjects XML files (*.uav)");
fileName = QFileDialog::getSaveFileName(0, tr("Save UAV Settings File As"), "", filters); fileName = QFileDialog::getSaveFileName(0, tr("Save UAVSettings File As"), "", filters);
if (fileName.isEmpty()) { if (fileName.isEmpty()) {
return; return;
} }
bool fullExport = false;
// If the filename ends with .xml, we will do a full export, otherwise, a simple export // If the filename ends with .xml, we will do a full export, otherwise, a simple export
bool fullExport = false;
if (fileName.endsWith(".xml")) { if (fileName.endsWith(".xml")) {
fullExport = true; fullExport = true;
} else if (!fileName.endsWith(".uav")) { } else if (!fileName.endsWith(".uav")) {
@ -321,7 +352,7 @@ void UAVSettingsImportExportFactory::exportUAVSettings()
} }
// generate an XML first (used for all export formats as a formatted data source) // generate an XML first (used for all export formats as a formatted data source)
QString xml = createXMLDocument("UAVSettings", true, fullExport); QString xml = createXMLDocument(Settings, fullExport);
// save file // save file
QFile file(fileName); QFile file(fileName);
@ -356,15 +387,15 @@ void UAVSettingsImportExportFactory::exportUAVData()
// ask for file name // ask for file name
QString fileName; QString fileName;
QString filters = tr("UAVData XML files (*.uav)"); QString filters = tr("UAVObjects XML files (*.uav)");
fileName = QFileDialog::getSaveFileName(0, tr("Save UAV Data File As"), "", filters); fileName = QFileDialog::getSaveFileName(0, tr("Save UAVData File As"), "", filters);
if (fileName.isEmpty()) { if (fileName.isEmpty()) {
return; return;
} }
bool fullExport = false;
// If the filename ends with .xml, we will do a full export, otherwise, a simple export // If the filename ends with .xml, we will do a full export, otherwise, a simple export
bool fullExport = false;
if (fileName.endsWith(".xml")) { if (fileName.endsWith(".xml")) {
fullExport = true; fullExport = true;
} else if (!fileName.endsWith(".uav")) { } else if (!fileName.endsWith(".uav")) {
@ -372,7 +403,7 @@ void UAVSettingsImportExportFactory::exportUAVData()
} }
// generate an XML first (used for all export formats as a formatted data source) // generate an XML first (used for all export formats as a formatted data source)
QString xml = createXMLDocument("UAVData", false, fullExport); QString xml = createXMLDocument(Both, fullExport);
// save file // save file
QFile file(fileName); QFile file(fileName);

View File

@ -38,9 +38,8 @@ public:
~UAVSettingsImportExportFactory(); ~UAVSettingsImportExportFactory();
private: private:
QString createXMLDocument(const QString docName, enum storedData { Settings, Data, Both };
const bool isSettings, QString createXMLDocument(const enum storedData, const bool fullExport);
const bool fullExport);
private slots: private slots:
void importUAVSettings(); void importUAVSettings();