Files
qad/qad_blockview/blockbase.h
Бычков Андрей ba8bc27298 1
git-svn-id: svn://db.shs.com.ru/libs@1 a8b55f48-bf90-11e4-a774-851b48703e85
2015-02-28 21:28:53 +00:00

178 lines
6.3 KiB
C++

#ifndef BLOCKBASE_H
#define BLOCKBASE_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QGraphicsObject>
#include <QGraphicsEllipseItem>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>
#include <QStack>
#include <QDebug>
#include "chunkstream.h"
/// data:
/// 1002 - flag for move parent (true)
/// 1003 - flag for visualize selection (true)
/// 1004 - BlockItemPin ("pin")
/// 1005 - BlockBusItem ("connection")
/// 1006 - BlockItem ("item")
/// 1007 - BlockItem selection ("item_selection")
/// 1008 - item is NOT decor, ignore for function decors() (true)
/// 1009 - item is scene decor ("decor")
/// 1010 - BlockItem decor (src text for QGraphicsSimpleTextItem)
/// 1011 - item is BlockItem decor ("decor")
/// 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);
class BlockItemBase: public QObject
{
Q_OBJECT
Q_ENUMS(Action)
public:
enum Action {
BlockAdd = 1,
BlockMove,
BlockRemove,
BlockCopy,
BusAdd,
BusRemove,
BusPointAdd,
BusPointMove,
BusPointRemove,
BusSegmentAdd,
BusSegmentMove,
BusSegmentRemove
};
};
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