1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-30 08:24:11 +01:00

GCS: UAV Settings Export: added support for *.ini files.

INI file format is to be used for compact export to publish on forums, etc.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2866 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
osnwt 2011-02-23 20:27:36 +00:00 committed by osnwt
parent bd665790c2
commit a4fe867c1d

View File

@ -24,6 +24,14 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
/*
* TODO:
* - write import functions
* - split formats into different files/classes
* - better error handling (not a lot of QMessageBoxes)
*/
#include "uavsettingsimportexport.h" #include "uavsettingsimportexport.h"
#include <QtPlugin> #include <QtPlugin>
@ -115,12 +123,13 @@ void UAVSettingsImportExportPlugin::importExport()
fileFormat = UAV; fileFormat = UAV;
} else if (fileType == "xml") { } else if (fileType == "xml") {
fileFormat = XML; fileFormat = XML;
} else if (fileType == "ini") {
fileFormat = INI;
} else { } else {
QMessageBox mb(QMessageBox::Information, QMessageBox::critical(0,
tr("UAV Settings Export"), tr("UAV Settings Export"),
tr("Unsupported export file format: '") + fileType + "'", tr("Unsupported export file format: ") + fileType,
QMessageBox::Ok); QMessageBox::Ok);
mb.exec();
return; return;
} }
@ -182,12 +191,45 @@ void UAVSettingsImportExportPlugin::importExport()
} }
// save file // save file
QString xml = doc.toString(4); if ((fileFormat == UAV) || (fileFormat == XML)) {
QFile file(fileName);
if (file.open(QIODevice::WriteOnly) &&
(file.write(doc.toString(4).toAscii()) != -1)) {
file.close();
} else {
QMessageBox::critical(0,
tr("UAV Settings Export"),
tr("Unable to save settings: ") + fileName,
QMessageBox::Ok);
return;
}
} else if (fileFormat == INI) {
if (QFile::exists(fileName) && !QFile::remove(fileName)) {
QMessageBox::critical(0,
tr("UAV Settings Export"),
tr("Unable to remove existing file: ") + fileName,
QMessageBox::Ok);
return;
}
QFile file(fileName); QSettings ini(fileName, QSettings::IniFormat);
if (file.open(QIODevice::WriteOnly)) { QDomElement docElem = doc.documentElement();
file.write(xml.toAscii()); QDomNodeList nodeList = docElem.elementsByTagName("object");
file.close(); for (int i = 0; i < nodeList.count(); i++) {
QDomElement e = nodeList.at(i).toElement();
if (!e.isNull()) {
ini.beginGroup(e.attribute("name", "undefined"));
ini.setValue("id", e.attribute("id"));
QDomNodeList n = e.elementsByTagName("field");
for (int j = 0; j < n.count(); j++) {
QDomElement f = n.at(j).toElement();
if (!f.isNull()) {
ini.setValue(f.attribute("name", "unknown"), f.attribute("values"));
}
}
ini.endGroup();
}
}
} }
} }