Files
antimicrox/xmlconfigreader.cpp
2012-12-30 12:04:41 -06:00

137 lines
3.1 KiB
C++

#include <QDebug>
#include <QDir>
#include "xmlconfigreader.h"
XMLConfigReader::XMLConfigReader(QObject *parent) :
QObject(parent)
{
fileName = QString(PadderCommon::configPath + "/dudeman.xml");
configFile = new QFile(fileName);
xml = new QXmlStreamReader();
joystick = 0;
}
void XMLConfigReader::setJoystick(Joystick *joystick)
{
this->joystick = joystick;
}
void XMLConfigReader::setFileName(QString filename)
{
QFile *temp = new QFile(filename);
if (temp->exists())
{
configFile = temp;
}
else
{
delete temp;
temp = 0;
}
}
void XMLConfigReader::configJoystick(Joystick *joystick)
{
this->joystick = joystick;
read();
}
bool XMLConfigReader::read()
{
if (configFile->exists() && joystick)
{
xml->clear();
if (!configFile->isOpen())
{
configFile->open(QFile::ReadOnly | QFile::Text);
xml->setDevice(configFile);
}
xml->readNextStartElement();
if (xml->name() != "joystick")
{
xml->raiseError("Root node is not a joystick");
}
else
{
xml->readNextStartElement();
}
while (!xml->atEnd())
{
if (xml->name() == "button" && xml->isStartElement())
{
int index = xml->attributes().value("index").toString().toInt();
JoyButton *button = joystick->getJoyButton(index-1);
if (button)
{
button->readConfig(xml);
}
else
{
xml->skipCurrentElement();
}
}
else if (xml->name() == "axis" && xml->isStartElement())
{
int index = xml->attributes().value("index").toString().toInt();
JoyAxis *axis = joystick->getJoyAxis(index-1);
if (axis)
{
axis->readConfig(xml);
}
else
{
xml->skipCurrentElement();
}
}
else if (xml->name() == "dpad" && xml->isStartElement())
{
int index = xml->attributes().value("index").toString().toInt();
JoyDPad *dpad = joystick->getJoyDPad(index-1);
if (dpad)
{
dpad->readConfig(xml);
}
else
{
xml->skipCurrentElement();
}
}
else
{
// If none of the above, skip the element
xml->skipCurrentElement();
}
xml->readNextStartElement();
}
configFile->close();
}
return !xml->error();
}
XMLConfigReader::~XMLConfigReader()
{
if (configFile)
{
if (configFile->isOpen())
{
configFile->close();
}
delete configFile;
configFile = 0;
}
if (xml)
{
delete xml;
xml = 0;
}
}