1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-06 21:54:15 +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,8 +50,6 @@
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
UAVSettingsImportExportFactory::~UAVSettingsImportExportFactory() UAVSettingsImportExportFactory::~UAVSettingsImportExportFactory()
{ {
// Do nothing // Do nothing
@ -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,6 +143,7 @@ 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 // - Read each object
QString uavObjectName = e.attribute("name"); QString uavObjectName = e.attribute("name");
uint uavObjectID = e.attribute("id").toUInt(NULL,16); uint uavObjectID = e.attribute("id").toUInt(NULL,16);
@ -148,7 +154,6 @@ void UAVSettingsImportExportFactory::importUAVSettings()
// This object is unknown! // This object is unknown!
qDebug() << "Object unknown:" << uavObjectName << uavObjectID; qDebug() << "Object unknown:" << uavObjectName << uavObjectID;
swui.addLine(uavObjectName, "Error (Object unknown)", false); swui.addLine(uavObjectName, "Error (Object unknown)", false);
} else { } else {
// - Update each field // - Update each field
// - Issue and "updated" command // - Issue and "updated" command
@ -165,8 +170,9 @@ void UAVSettingsImportExportFactory::importUAVSettings()
if (false == uavfield->checkValue(f.attribute("values"))) { if (false == uavfield->checkValue(f.attribute("values"))) {
qDebug() << "checkValue returned false on: " << uavObjectName << f.attribute("values"); qDebug() << "checkValue returned false on: " << uavObjectName << f.attribute("values");
setError = true; setError = true;
} else } else {
uavfield->setValue(f.attribute("values")); uavfield->setValue(f.attribute("values"));
}
} else { } else {
// This is an enum: // This is an enum:
int i = 0; int i = 0;
@ -175,8 +181,9 @@ void UAVSettingsImportExportFactory::importUAVSettings()
if (false == uavfield->checkValue(element, i)) { if (false == uavfield->checkValue(element, i)) {
qDebug() << "checkValue(list) returned false on: " << uavObjectName << list; qDebug() << "checkValue(list) returned false on: " << uavObjectName << list;
setError = true; setError = true;
} else } else {
uavfield->setValue(element,i); uavfield->setValue(element,i);
}
i++; i++;
} }
} }
@ -187,6 +194,7 @@ void UAVSettingsImportExportFactory::importUAVSettings()
field = field.nextSibling(); field = field.nextSibling();
} }
obj->updated(); obj->updated();
if (error) { if (error) {
swui.addLine(uavObjectName, "Warning (Object field unknown)", true); swui.addLine(uavObjectName, "Warning (Object field unknown)", true);
} else if (uavObjectID != obj->getObjID()) { } else if (uavObjectID != obj->getObjID()) {
@ -194,29 +202,27 @@ void UAVSettingsImportExportFactory::importUAVSettings()
swui.addLine(uavObjectName, "Warning (ObjectID mismatch)", true); swui.addLine(uavObjectName, "Warning (ObjectID mismatch)", true);
} else if (setError) { } else if (setError) {
swui.addLine(uavObjectName, "Warning (Objects field value(s) invalid)", false); swui.addLine(uavObjectName, "Warning (Objects field value(s) invalid)", false);
} else } else {
swui.addLine(uavObjectName, "OK", true); 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 UAVSettings 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 UAVData 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();