Files
qad/libs/utils/propertystorage.h
2022-12-14 14:14:33 +03:00

163 lines
5.5 KiB
C++

/*
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PROPERTYSTORAGE_H
#define PROPERTYSTORAGE_H
#include "chunkstream.h"
#include <QDebug>
#include <QLineF>
#include <QPointF>
#include <QRectF>
#include <QStringList>
#include <QVariant>
class QAD_UTILS_EXPORT PropertyStorage {
public:
PropertyStorage() {}
struct QAD_UTILS_EXPORT Property {
Property(const QString & n = QString(), const QString & c = QString(), const QVariant & v = QVariant(), int f = 0)
: name(n)
, comment(c)
, value(v)
, flags(f) {}
bool toBool() const { return value.toBool(); }
int toInt() const { return value.toInt(); }
float toFloat() const { return value.toFloat(); }
double toDouble() const { return value.toDouble(); }
QString toString() const { return value.toString(); }
QString name;
QString comment;
QVariant value;
int flags;
};
PropertyStorage(const QList<Property> & pl) { props = pl; }
PropertyStorage(const QVariantMap & pl);
typedef QList<Property>::const_iterator const_iterator;
typedef QList<Property>::iterator iterator;
iterator begin() { return props.begin(); }
const_iterator begin() const { return props.begin(); }
iterator end() { return props.end(); }
const_iterator end() const { return props.end(); }
int count() const { return props.count(); }
int length() const { return props.length(); }
int size() const { return props.size(); }
bool isEmpty() const { return props.isEmpty(); }
Property & first() { return props.first(); }
const Property & first() const { return props.first(); }
Property & front() { return props.front(); }
const Property & front() const { return props.front(); }
Property & last() { return props.last(); }
const Property & last() const { return props.last(); }
Property & back() { return props.back(); }
const Property & back() const { return props.back(); }
void removeFirst() { props.removeFirst(); }
void removeLast() { props.removeLast(); }
void removeAt(int i) { props.removeAt(i); }
Property value(int i) const { return props.value(i); }
Property value(int i, const Property & defaultValue) const { return props.value(i, defaultValue); }
void clear() { props.clear(); }
PropertyStorage copy() const { return PropertyStorage(*this); }
int propertiesCount() const { return props.size(); }
QList<Property> & properties() { return props; }
const QList<Property> & properties() const { return props; }
const PropertyStorage & propertyStorage() const { return *this; }
QVariantMap toVariantMap() const;
bool isPropertyExists(const QString & _name) const;
void clearProperties() { props.clear(); }
void addProperty(const Property & p);
void addProperty(const QString & _name, const QVariant & _def_value, const QString & _comment = QString(), int _flags = 0) {
addProperty(Property(_name, _comment, _def_value, _flags));
}
void removeProperty(const QString & _name);
void removePropertiesByFlag(int flag);
void updateProperties(const QList<Property> & properties_, int flag_ignore = 0);
Property propertyByName(const QString & name) const;
QVariant propertyValueByName(const QString & name) const;
void setPropertyValue(const QString & name, const QVariant & value);
void setPropertyComment(const QString & name, const QString & comment);
void setPropertyFlags(const QString & name, int flags);
PropertyStorage & operator<<(const PropertyStorage::Property & p) {
props << p;
return *this;
}
PropertyStorage & operator<<(const QList<Property> & p) {
props << p;
return *this;
}
PropertyStorage & operator<<(const PropertyStorage & p) {
props << p.props;
return *this;
}
Property & operator[](int i) { return props[i]; }
const Property & operator[](int i) const { return props[i]; }
Property & operator[](const QString & name);
const Property operator[](const QString & name) const;
static Property parsePropertyLine(QString l);
protected:
QList<Property> props;
};
inline QDebug operator<<(QDebug s, const PropertyStorage::Property & p) {
s.nospace() << p.name << " (0x" << QString::number(p.flags, 16) << ") = " << p.value;
return s.space();
}
inline QDataStream & operator<<(QDataStream & s, const PropertyStorage::Property & p) {
ChunkStream cs;
cs << cs.chunk(1, p.name) << cs.chunk(2, p.comment) << cs.chunk(3, p.value) << cs.chunk(4, p.flags);
s << cs.data();
return s;
}
inline QDataStream & operator>>(QDataStream & s, PropertyStorage::Property & p) {
ChunkStream cs(s);
while (!cs.atEnd()) {
switch (cs.read()) {
case 1: cs.get(p.name); break;
case 2: cs.get(p.comment); break;
case 3: cs.get(p.value); break;
case 4: cs.get(p.flags); break;
}
}
return s;
}
inline QDataStream & operator<<(QDataStream & s, const PropertyStorage & p) {
s << p.properties();
return s;
}
inline QDataStream & operator>>(QDataStream & s, PropertyStorage & p) {
s >> p.properties();
return s;
}
#endif // PROPERTYSTORAGE_H