QCAD
Open Source 2D CAD
RObject.h
Go to the documentation of this file.
1 
20 #ifndef ROBJECT_H
21 #define ROBJECT_H
22 
23 #include "core_global.h"
24 
25 #include "RPropertyAttributes.h"
26 #include "RPropertyTypeId.h"
27 
28 class RDocument;
29 class RTransaction;
30 
31 #ifndef RDEFAULT_QVARIANT
32 #define RDEFAULT_QVARIANT QVariant()
33 #endif
34 
35 
49 
50  friend class RStorage;
51 
52 public:
53  typedef int Id;
54  const static Id INVALID_ID;
55  typedef int Handle;
56  const static Handle INVALID_HANDLE;
57 
62 
63  enum XYZ {
64  X, Y, Z
65  };
66 
67 public:
68  RObject(RDocument* document=NULL);
69  RObject(const RObject& other);
70  virtual ~RObject();
71 
72  static void init();
73 
74  virtual RObject* clone() const = 0;
75 
77  return document;
78  }
79 
80  const RDocument* getDocument() const {
81  return document;
82  }
83 
85  this->document = document;
86  }
87 
88  // static double variantToDouble(const QVariant& v, double defaultValue,
89  // bool ignoreError = true);
90  // static int variantToInt(const QVariant& v, int defaultValue,
91  // bool ignoreError = true);
92 
96  template<class T>
97  static bool setMember(T& variable, const QVariant& value,
98  bool condition = true) {
99 
100  if (!condition) {
101  return false;
102  }
103  variable = value.value<T> ();
104  return true;
105  }
106 
112  RObject::Id getId() const {
113  return objectId;
114  }
115 
117  return handle;
118  }
119 
120  virtual RS::EntityType getType() const {
121  return RS::ObjectUnknown;
122  }
123 
124  bool isProtected() const {
125  return protect;
126  }
127 
128  void setProtected(bool on) {
129  protect = on;
130  }
131 
132  bool isUndone() const {
133  return undone;
134  }
135 
136  virtual QSet<RPropertyTypeId> getPropertyTypeIds() const;
137  virtual QSet<RPropertyTypeId> getCustomPropertyTypeIds() const;
138 
143  virtual QPair<QVariant, RPropertyAttributes>
144  getProperty(RPropertyTypeId& propertyTypeId,
145  bool humanReadable = false,
146  bool noAttributes = false);
147 
156  virtual bool setProperty(RPropertyTypeId propertyTypeId,
157  const QVariant& value, RTransaction* transaction=NULL);
158 
163  virtual bool hasPropertyType(RPropertyTypeId propertyTypeId) {
164  return RPropertyTypeId::hasPropertyType(typeid(*this), propertyTypeId);
165  }
166 
172  virtual bool isSelectedForPropertyEditing() = 0;
173 
174  bool hasCustomProperties() const;
175  bool hasCustomProperty(const QString& title, const QString& key) const;
176 
180  bool hasCustomProperty(const QString& title, const QRegExp& key) const;
181 
182  QVariant getCustomProperty(const QString& title, const QString& key, const QVariant& defaultValue = RDEFAULT_QVARIANT) const;
183  bool getCustomBoolProperty(const QString& title, const QString& key, bool defaultValue) const;
184  void setCustomProperty(const QString& title, const QString& key, const QVariant& value);
185  void removeCustomProperty(const QString& title, const QString& key);
186  QStringList getCustomPropertyTitles() const;
187  QStringList getCustomPropertyKeys(const QString& title) const;
188  void copyCustomPropertiesFrom(RObject* other);
189 
193  QMap<QString, QVariantMap> getCustomProperties() const;
194 
195  virtual int getComplexity() const {
196  return 0;
197  }
198 
199  virtual void setAutoUpdatesBlocked(bool on) {
200  Q_UNUSED(on)
201  }
202 
203  void dump() {
204  qDebug() << *this;
205  }
206 
210  friend QDebug operator<<(QDebug dbg, const RObject& o) {
211  o.print(dbg);
212  return dbg;
213  }
214 
215 protected:
216  virtual void print(QDebug dbg) const;
217 
218  void setUndone(bool on);
219 
220  void setId(RObject::Id id) {
221  objectId = id;
222  }
223 
225  handle = h;
226  }
227 
231  static bool setMember(QString& variable, const QVariant& value, bool condition = true);
235  static bool setMember(double& variable, const QVariant& value, bool condition = true);
239  static bool setMember(int& variable, const QVariant& value, bool condition = true);
243  static bool setMember(bool& variable, const QVariant& value, bool condition = true);
247  static bool setMember(QList<double>& variable, const QVariant& value, bool condition);
251  static bool setMemberX(QList<RVector>& variable, const QVariant& value, bool condition = true);
255  static bool setMemberY(QList<RVector>& variable, const QVariant& value, bool condition = true);
259  static bool setMemberZ(QList<RVector>& variable, const QVariant& value, bool condition = true);
260 
264  static bool setMemberVector(QList<RVector>& variable, const QVariant& value, RObject::XYZ xyz);
265 
266 private:
275  Handle handle;
279  bool undone;
283  bool protect;
291  QMap<QString, QVariantMap> customProperties;
292 };
293 
297 Q_DECLARE_METATYPE(QList<QSharedPointer<RObject> >)
298 Q_DECLARE_METATYPE(QSharedPointer<RObject>)
299 Q_DECLARE_METATYPE(QSharedPointer<RObject>*)
300 typedef QMap<int, QSharedPointer<RObject> > _RMapIntObjectPointer;
301 Q_DECLARE_METATYPE(_RMapIntObjectPointer)
303 
304 #endif
RObject::Handle getHandle() const
Definition: RObject.h:116
int Handle
Definition: RObject.h:55
RDocument * document
Definition: RStorage.h:692
EntityType
Entity types used for property handling / filtering.
Definition: RS.h:76
friend QDebug operator<<(QDebug dbg, const RObject &o)
Definition: RObject.h:210
Abstract base class for all objects.
Definition: RObject.h:48
bool protect
True if this object is protected (undeletable).
Definition: RObject.h:283
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RPropertyTypeId.h:58
bool undone
True if this object has been undone (deleted).
Definition: RObject.h:279
#define QCADCORE_EXPORT
Definition: core_global.h:10
int Id
Definition: RObject.h:53
This is the abstract base class for all storage implementations.
Definition: RStorage.h:62
static RPropertyTypeId PropertyType
Definition: RObject.h:59
Id objectId
Unique ID of this object.
Definition: RObject.h:271
Transaction implementation.
Definition: RTransaction.h:74
static RPropertyTypeId PropertyHandle
Definition: RObject.h:60
virtual RS::EntityType getType() const
Definition: RObject.h:120
Unknown object.
Definition: RS.h:78
static RPropertyTypeId PropertyCustom
Definition: RObject.h:58
virtual bool hasPropertyType(RPropertyTypeId propertyTypeId)
Definition: RObject.h:163
void setId(RObject::Id id)
Definition: RObject.h:220
static const Id INVALID_ID
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RObject.h:54
QMap< int, QSharedPointer< RObject > > _RMapIntObjectPointer
Definition: RObject.h:300
void setProtected(bool on)
Definition: RObject.h:128
static bool setMember(T &variable, const QVariant &value, bool condition=true)
Definition: RObject.h:97
bool isUndone() const
Definition: RObject.h:132
void setDocument(RDocument *document)
Definition: RObject.h:84
A graphics document contains and owns entities, layers, user coordinate systems, variables, block definitions, etc.
Definition: RDocument.h:67
Q_DECLARE_METATYPE(RMath *)
virtual int getComplexity() const
Definition: RObject.h:195
void setHandle(RObject::Handle h)
Definition: RObject.h:224
const RDocument * getDocument() const
Definition: RObject.h:80
virtual void setAutoUpdatesBlocked(bool on)
Definition: RObject.h:199
static RPropertyTypeId PropertyProtected
Definition: RObject.h:61
RDocument * getDocument()
Definition: RObject.h:76
static bool hasPropertyType(const std::type_info &classInfo, RPropertyTypeId propertyTypeId)
Definition: RPropertyTypeId.cpp:211
QMap< QString, QVariantMap > customProperties
AppID -> key -> value e.g.
Definition: RObject.h:291
void init(void basePath)
Definition: AddBlockInit.js:2
RObject::Id getId() const
Definition: RObject.h:112
void dump()
Definition: RObject.h:203
bool isProtected() const
Definition: RObject.h:124
XYZ
Definition: RObject.h:63
Handle handle
Handle of this object (from DXF / DWG).
Definition: RObject.h:275
virtual void print(QDebug dbg) const
Stream operator for QDebug.
Definition: RObject.cpp:512
#define RDEFAULT_QVARIANT
Definition: RObject.h:32
static const Handle INVALID_HANDLE
Definition: RObject.h:56
RDocument * document
Definition: RObject.h:267