246 lines
7.0 KiB
C++
Raw Normal View History

/*
2021-02-20 12:13:46 +01:00
Copyright 2006-2021 The QElectroTech Team
2020-10-17 20:25:30 +02:00
This file is part of QElectroTech.
2020-10-17 20:25:30 +02:00
QElectroTech 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 2 of the License, or
(at your option) any later version.
2020-10-17 20:25:30 +02:00
QElectroTech 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.
2020-10-17 20:25:30 +02:00
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "propertiesinterface.h"
2020-10-19 11:07:04 +02:00
#include <QDebug>
#include "../qetxml.h"
2020-10-19 11:07:04 +02:00
/*!
* Available property types
*/
namespace {
2021-03-04 19:18:28 +01:00
const QString userPropertiesS = "userProperties";
}
2020-07-19 20:05:13 +02:00
/**
2020-10-17 20:25:30 +02:00
@brief PropertiesInterface::PropertiesInterface
2020-07-19 20:05:13 +02:00
*/
2021-03-04 19:18:28 +01:00
PropertiesInterface::PropertiesInterface(const QString &tagname):
mTagName(tagname)
{
}
2020-07-19 20:05:13 +02:00
/**
2020-10-17 20:25:30 +02:00
@brief PropertiesInterface::~PropertiesInterface
2020-07-19 20:05:13 +02:00
*/
PropertiesInterface::~PropertiesInterface()
{
}
2021-03-04 19:18:28 +01:00
void PropertiesInterface::setTagName(const QString& tagname)
{
mTagName = tagname;
}
2021-03-04 21:25:04 +01:00
QString PropertiesInterface::tagName() const
{
return mTagName;
}
2021-03-04 19:18:28 +01:00
QDomElement PropertiesInterface::toXml (QDomDocument &xml_document) const
{
QDomElement element = xml_document.createElement(mTagName);
toXmlPriv(element);
propertiesToXml(element);
return element;
}
bool PropertiesInterface::fromXml (const QDomElement &xml_element)
{
if (!fromXmlPriv(xml_element))
return false;
if (!propertiesFromXml(xml_element))
return false;
2021-03-04 21:25:04 +01:00
return true;
2021-03-04 19:18:28 +01:00
}
bool PropertiesInterface::valideXml(QDomElement& element) {
2020-10-17 20:25:30 +02:00
qDebug(QString("ValideXml() is not implemented. File: %1, Line: %2").arg(__FILE__).arg(__LINE__).toStdString().data());
return false;
}
/**
2020-10-17 20:25:30 +02:00
Permet de convertir une chaine de caracteres ("n", "s", "e" ou "w")
en orientation. Si la chaine fait plusieurs caracteres, seul le
premier est pris en compte. En cas d'incoherence, Qet::North est
retourne.
@param s Chaine de caractere cense representer une orientation
@return l'orientation designee par la chaine de caractere
*/
Qet::Orientation PropertiesInterface::orientationFromString(const QString &s) {
2020-10-17 20:25:30 +02:00
QChar c = s[0];
// in some cases/ old projects? (affuteuse_250h.qet) numbers instead of characters are
// used for the orientation
if (c == 'e' || c == '1') return(Qet::East);
else if (c == 's' || c == '2') return(Qet::South);
else if (c == 'w' || c == '3') return (Qet::West);
else return(Qet::North); // c == '0'
}
/**
2020-10-17 20:25:30 +02:00
@param o une orientation
@return une chaine de caractere representant l'orientation
*/
QString PropertiesInterface::orientationToString(Qet::Orientation o) {
2020-10-17 20:25:30 +02:00
QString ret;
switch(o) {
case Qet::North: ret = "n"; break;
case Qet::East : ret = "e"; break;
case Qet::South: ret = "s"; break;
case Qet::West : ret = "w"; break;
}
return(ret);
}
2021-03-04 19:18:28 +01:00
void PropertiesInterface::deleteUserProperties()
{
properties.clear();
}
int PropertiesInterface::userPropertiesCount() const
{
return properties.count();
}
void PropertiesInterface::setUserProperty(const QString& key, const QVariant& value)
{
properties[key] = value;
}
bool PropertiesInterface::existUserProperty(const QString& key) const
{
return properties.contains(key);
}
QVariant PropertiesInterface::userPropertyValue(const QString& key)
{
if (!existUserProperty(key))
return QVariant();
return properties[key];
}
2021-03-04 19:18:28 +01:00
void PropertiesInterface::propertiesToXml(QDomElement& e) const
2021-03-04 21:25:04 +01:00
{
if (properties.count() == 0)
return;
2021-03-04 19:18:28 +01:00
QDomDocument doc = e.ownerDocument();
auto up = doc.createElement(userPropertiesS);
for (auto i = properties.begin(); i != properties.end(); ++i)
{
auto type = i.value().type();
switch(type) {
case QVariant::Type::String:
up.appendChild(QETXML::createXmlProperty(i.key(), i.value().toString())); break;
2021-03-04 19:18:28 +01:00
case QVariant::Type::Int:
up.appendChild(QETXML::createXmlProperty(i.key(), i.value().toInt())); break;
2021-03-04 19:18:28 +01:00
case QVariant::Type::Double:
up.appendChild(QETXML::createXmlProperty(i.key(), i.value().toDouble())); break;
2021-03-04 19:18:28 +01:00
case QVariant::Type::Bool:
up.appendChild(QETXML::createXmlProperty(i.key(), i.value().toBool())); break;
2021-03-04 21:25:04 +01:00
case QVariant::Type::Color:
up.appendChild(QETXML::createXmlProperty(i.key(), QColor(i.value().value<QColor>()))); break;
2021-03-04 19:18:28 +01:00
default:
break;
}
}
e.appendChild(up);
}
/*!
* \brief PropertiesInterface::propertiesFromXml
* Read all user properties from the DomElement& e
* \param e
* \return
*/
bool PropertiesInterface::propertiesFromXml(const QDomElement& e)
{
QDomNodeList l = e.childNodes();
for (int i=0; i < l.count(); i++)
{
QDomElement userProperties = l.at(i).toElement();
if (userProperties.tagName() != userPropertiesS)
continue;
QDomElement userProperty;
for (int up_index = 0; up_index < userProperties.childNodes().length(); up_index++)
{
userProperty = userProperties.childNodes().at(up_index).toElement();
QString name = userProperty.attribute("name");
QString type = userProperty.attribute("type");
QString value = userProperty.attribute("value");
if (type == QETXML::integerS)
2021-03-04 19:18:28 +01:00
{
int i;
if (QETXML::propertyInteger(value, &i) == QETXML::PropertyFlags::Success)
2021-03-04 19:18:28 +01:00
properties[name] = QVariant(i);
else
return false;
}
else if (type == QETXML::doubleS)
2021-03-04 19:18:28 +01:00
{
double d;
if (QETXML::propertyDouble(value, &d) == QETXML::PropertyFlags::Success)
2021-03-04 19:18:28 +01:00
properties[name] = QVariant(d);
else
return false;
}
else if (type == QETXML::boolS)
2021-03-04 19:18:28 +01:00
{
bool b;
if (QETXML::propertyBool(value, &b) == QETXML::PropertyFlags::Success)
2021-03-04 19:18:28 +01:00
properties[name] = QVariant(b);
else
return false;
}
else if (type == QETXML::uuidS)
2021-03-04 19:18:28 +01:00
{
QUuid u;
if (QETXML::propertyUuid(value, &u) == QETXML::PropertyFlags::Success)
2021-03-04 19:18:28 +01:00
properties[name] = QVariant(u);
else
return false;
}
else if (type == QETXML::colorS)
2021-03-04 19:18:28 +01:00
{
QColor c;
if (QETXML::propertyColor(value, &c) == QETXML::PropertyFlags::Success)
2021-03-04 19:18:28 +01:00
properties[name] = QVariant(c);
else
return false;
}
else if (type == QETXML::stringS)
2021-03-04 19:18:28 +01:00
{
properties[name] = QVariant(value);
}
else
{
qDebug() << "Not a valid property type!";
}
}
}
2021-03-04 21:25:04 +01:00
return true;
2021-03-04 19:18:28 +01:00
}