git-svn-id: svn://db.shs.com.ru/libs@161 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2017-02-02 08:52:24 +00:00
parent dfb021c228
commit 0932e6ecf4
20 changed files with 663 additions and 432 deletions

View File

@@ -3,44 +3,6 @@
#include "qvariantedit.h"
QVariant::Type typeFromLetter(const QString & l) {
if (l.isEmpty()) return QVariant::String;
QString ft = l.left(1);
if (ft == "l") return QVariant::StringList;
if (ft == "b") return QVariant::Bool;
if (ft == "n") return QVariant::Int;
if (ft == "f") return QVariant::Double;
if (ft == "c") return QVariant::Color;
if (ft == "r") return QVariant::Rect;
if (ft == "a") return QVariant::RectF;
if (ft == "p") return QVariant::Point;
if (ft == "v") return QVariant::PointF;
if (ft == "e") return (QVariant::Type)qMetaTypeId<QVariantEdit::EnumType>();
if (ft == "F") return (QVariant::Type)qMetaTypeId<QVariantEdit::FileType>();
if (ft == "D") return (QVariant::Type)qMetaTypeId<QVariantEdit::DirType>();
return QVariant::String;
}
QString uniqueName(QString n, const QStringList & names) {
if (!names.contains(n))
return n;
QString num;
while (!n.isEmpty()) {
if (n.right(1)[0].isDigit()) {
num.push_front(n.right(1));
n.chop(1);
} else break;
}
if (!n.endsWith('_')) n += '_';
int in = num.toInt() + 1;
QString nn = n + QString::number(in).rightJustified(3, '0');
while (names.contains(nn))
nn = n + QString::number(++in).rightJustified(3, '0');
return nn;
}
QDataStream & operator <<(QDataStream & s, const QGraphicsItem * item) {
if (!item) {
s << int(-1);
@@ -69,6 +31,7 @@ QDataStream & operator <<(QDataStream & s, const QGraphicsItem * item) {
s << int(5) << (ipixmap->pixmap());
} else {
s << int(-1);
return s;
}
s << (item->pos()) << (item->rotation()) << int(item->flags());
return s;
@@ -139,151 +102,3 @@ QDataStream & operator >>(QDataStream & s, QGraphicsItem *& item) {
}
return s;
}
bool PropertyStorage::isPropertyExists(const QString & _name) const {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == _name)
return true;
return false;
}
void PropertyStorage::addProperty(const PropertyStorage::Property & p) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == p.name) {
props[i] = p;
return;
}
props << p;
}
void PropertyStorage::removeProperty(const QString & _name) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == _name) {
props.removeAt(i);
return;
}
}
void PropertyStorage::removePropertiesByFlag(int flag) {
for (int i = 0; i < props.size(); ++i)
if ((props[i].flags & flag) == flag) {
props.removeAt(i);
--i;
}
}
void PropertyStorage::updateProperties(const QList<PropertyStorage::Property> & properties_, int flag_ignore) {
QVariantMap values;
foreach (const PropertyStorage::Property & p, props)
if (((p.flags & flag_ignore) != flag_ignore) || (flag_ignore == 0))
values[p.name] = p.value;
props = properties_;
for (int i = 0; i < props.size(); ++i) {
PropertyStorage::Property & p(props[i]);
if (values.contains(p.name)) {
QVariant pv = values[p.name];
if (pv.userType() == p.value.userType())
p.value = pv;
}
}
}
PropertyStorage::Property PropertyStorage::propertyByName(const QString & name) const {
foreach (const Property & p, props)
if (p.name == name)
return p;
return Property();
}
QVariant PropertyStorage::propertyValueByName(const QString & name) const {
foreach (const Property & p, props)
if (p.name == name)
return p.value;
return QVariant();
}
void PropertyStorage::setPropertyValue(const QString & name, const QVariant & value) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == name) {
props[i].value = value;
return;
}
}
void PropertyStorage::setPropertyComment(const QString & name, const QString & comment) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == name) {
props[i].comment = comment;
return;
}
}
void PropertyStorage::setPropertyFlags(const QString & name, int flags) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == name) {
props[i].flags = flags;
return;
}
}
PropertyStorage::Property PropertyStorage::parsePropertyLine(QString l) {
PropertyStorage::Property ret;
QString pn, pc, pt("s"), pv;
if (l.contains('#')) {
int i = l.indexOf('#');
pn = l.left(i).trimmed();
pc = l.right(l.length() - i - 1).trimmed();
} else {
if (l.contains('(')) {
int bs = l.indexOf('('), be = l.indexOf(')');
if (be > 0) {
pc = l.mid(bs + 1, be - bs - 1).trimmed();
l.remove(bs, be - bs + 1);
} else {
pc = l.right(l.length() - bs - 1).trimmed();
l = l.left(bs);
}
}
pn = l.trimmed();
}
if (!pc.isEmpty()) {
pt = pc.left(1);
pc = pc.remove(0, 1).trimmed();
}
if (pn.contains('=')) {
int i = pn.indexOf('=');
pv = pn.right(pn.length() - i - 1).trimmed();
pn.truncate(i);
pn = pn.trimmed();
}
ret.name = pn;
ret.comment = pc;
ret.value = QVariant(typeFromLetter(pt));
if (!pv.isEmpty()) {
//qDebug() << "set value !" << pv;
switch (ret.value.type()) {
case QVariant::Bool: pv = pv.toLower(); ret.value = (pv == "on" || pv == "true" || pv == "enable" || pv == "enabled" || pv.toInt() > 0 ? true : false); break;
case QVariant::Int: ret.value = pv.toInt(); break;
case QVariant::UInt: ret.value = pv.toUInt(); break;
case QVariant::LongLong: ret.value = pv.toLongLong(); break;
case QVariant::ULongLong: ret.value = pv.toULongLong(); break;
case QVariant::Double: ret.value = pv.toDouble(); break;
case QVariant::Color: ret.value = QColor(pv); break;
default: ret.value = pv; break;
};
}
return ret;
}

View File

@@ -9,8 +9,7 @@
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>
#include <QStack>
#include <QDebug>
#include "chunkstream.h"
#include "propertystorage.h"
/// data:
@@ -27,25 +26,6 @@
/// 1100 - flag for correct move (true)
inline qreal quantize(qreal x, qreal q = 10.f) {return qRound(x / q) * q;}
inline QPointF quantize(QPointF x, qreal q = 10.f) {return QPointF(quantize(x.x(), q), quantize(x.y(), q));}
inline qreal distPointToLine(const QPointF & lp0, const QPointF & lp1, const QPointF & p) {
QLineF a(lp0, lp1), b(lp0, p), c(lp1, p);
qreal f = qAbs(a.dx()*b.dy() - a.dy()*b.dx()) / a.length(), s = b.length() + c.length() - a.length();
return qMax(f, s);
}
inline QPointF nearestPointOnLine(const QPointF & lp0, const QPointF & lp1, const QPointF & p) {
QLineF a(lp0, lp1), b(lp0, p);
return a.pointAt(b.length() / a.length());
}
inline QRectF enlargedRect(const QRectF & r, qreal dx, qreal dy, qreal v) {
return QRectF(r.left() - v + dx, r.top() - v + dy, r.width() + v+v, r.height() + v+v);
}
QVariant::Type typeFromLetter(const QString & l);
QString uniqueName(QString n, const QStringList & names);
QDataStream & operator <<(QDataStream & s, const QGraphicsItem * item);
QDataStream & operator >>(QDataStream & s, QGraphicsItem *& item);
@@ -74,104 +54,4 @@ public:
};
class PropertyStorage {
public:
PropertyStorage() {}
struct 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) {}
QString name;
QString comment;
QVariant value;
int flags;
};
PropertyStorage(const QList<Property> & pl) {props = 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();}
const_iterator constBegin() const {return props.constBegin();}
iterator end() {return props.end();}
const_iterator end() const {return props.end();}
const_iterator constEnd() const {return props.constEnd();}
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;}
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];}
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 & p) {s << p.properties(); return s;}
inline QDataStream & operator >>(QDataStream & s, PropertyStorage & p) {s >> p.properties(); return s;}
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: p.name = cs.getData<QString>(); break;
case 2: p.comment = cs.getData<QString>(); break;
case 3: p.value = cs.getData<QVariant>(); break;
case 4: p.flags = cs.getData<int>(); break;
}
}
return s;
}
#endif // BLOCKBASE_H