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 #ifndef RDEFAULT_QSTRING
36 #define RDEFAULT_QSTRING QString()
37 #endif
38 
39 
53 
54  friend class RStorage;
55 
56 public:
57  typedef int Id;
58  const static Id INVALID_ID;
59  typedef int Handle;
60  const static Handle INVALID_HANDLE;
61 
66 
67  enum XYZ {
68  X, Y, Z
69  };
70 
71 public:
72  RObject(RDocument* document=NULL);
73  RObject(const RObject& other);
74  virtual ~RObject();
75 
76  static void init();
77 
78  virtual RS::EntityType getType() const = 0;
79 
80  virtual RObject* clone() const = 0;
81 
83  return document;
84  }
85 
86  const RDocument* getDocument() const {
87  return document;
88  }
89 
91  this->document = document;
92  }
93 
94  // static double variantToDouble(const QVariant& v, double defaultValue,
95  // bool ignoreError = true);
96  // static int variantToInt(const QVariant& v, int defaultValue,
97  // bool ignoreError = true);
98 
102  template<class T>
103  static bool setMember(T& variable, const QVariant& value,
104  bool condition = true) {
105 
106  if (!condition) {
107  return false;
108  }
109  variable = value.value<T> ();
110  return true;
111  }
112 
118  RObject::Id getId() const {
119  return objectId;
120  }
121 
123  return handle;
124  }
125 
126  bool isProtected() const {
127  return protect;
128  }
129 
130  void setProtected(bool on) {
131  protect = on;
132  }
133 
134  bool isUndone() const {
135  return undone;
136  }
137 
138  virtual QSet<RPropertyTypeId> getPropertyTypeIds() const;
139  virtual QSet<RPropertyTypeId> getCustomPropertyTypeIds() const;
140 
145  virtual QPair<QVariant, RPropertyAttributes>
146  getProperty(RPropertyTypeId& propertyTypeId,
147  bool humanReadable = false,
148  bool noAttributes = false);
149 
158  virtual bool setProperty(RPropertyTypeId propertyTypeId,
159  const QVariant& value, RTransaction* transaction=NULL);
160 
165  virtual bool hasPropertyType(RPropertyTypeId propertyTypeId) {
166  return RPropertyTypeId::hasPropertyType(typeid(*this), propertyTypeId);
167  }
168 
174  virtual bool isSelectedForPropertyEditing() = 0;
175 
176  bool hasCustomProperties() const;
177  bool hasCustomProperty(const QString& title, const QString& key) const;
178 
182  bool hasCustomProperty(const QString& title, const QRegExp& key) const;
183 
184  QVariant getCustomProperty(const QString& title, const QString& key, const QVariant& defaultValue = RDEFAULT_QVARIANT) const;
185  double getCustomDoubleProperty(const QString& title, const QString& key, double defaultValue) const;
186  int getCustomIntProperty(const QString& title, const QString& key, int defaultValue) const;
187  bool getCustomBoolProperty(const QString& title, const QString& key, bool defaultValue) const;
188  void setCustomProperty(const QString& title, const QString& key, const QVariant& value);
189  void removeCustomProperty(const QString& title, const QString& key);
190  QStringList getCustomPropertyTitles() const;
191  QStringList getCustomPropertyKeys(const QString& title) const;
192  void copyCustomPropertiesFrom(RObject* other, const QString& title = RDEFAULT_QSTRING);
193 
197  QMap<QString, QVariantMap> getCustomProperties() const;
198 
199  virtual int getComplexity() const {
200  return 0;
201  }
202 
203  virtual void setAutoUpdatesBlocked(bool on) {
204  Q_UNUSED(on)
205  }
206 
207  void dump() {
208  qDebug() << *this;
209  }
210 
214  friend QDebug operator<<(QDebug dbg, const RObject& o) {
215  o.print(dbg);
216  return dbg;
217  }
218 
219 protected:
220  virtual void print(QDebug dbg) const;
221 
222  void setUndone(bool on);
223 
224  void setId(RObject::Id id) {
225  objectId = id;
226  }
227 
229  handle = h;
230  }
231 
235  static bool setMember(QString& variable, const QVariant& value, bool condition = true);
239  static bool setMember(double& variable, const QVariant& value, bool condition = true);
243  static bool setMember(int& variable, const QVariant& value, bool condition = true);
247  static bool setMember(bool& variable, const QVariant& value, bool condition = true);
251  static bool setMember(QList<double>& variable, const QVariant& value, bool condition);
255  static bool setMemberX(QList<RVector>& variable, const QVariant& value, bool condition = true);
259  static bool setMemberY(QList<RVector>& variable, const QVariant& value, bool condition = true);
263  static bool setMemberZ(QList<RVector>& variable, const QVariant& value, bool condition = true);
264 
268  static bool setMemberVector(QList<RVector>& variable, const QVariant& value, RObject::XYZ xyz);
269 
270 private:
279  Handle handle;
283  bool undone;
287  bool protect;
295  QMap<QString, QVariantMap> customProperties;
296 };
297 
301 Q_DECLARE_METATYPE(QList<QSharedPointer<RObject> >)
302 Q_DECLARE_METATYPE(QSharedPointer<RObject>)
303 Q_DECLARE_METATYPE(QSharedPointer<RObject>*)
304 typedef QMap<int, QSharedPointer<RObject> > _RMapIntObjectPointer;
305 Q_DECLARE_METATYPE(_RMapIntObjectPointer)
307 
308 #endif
RObject::Handle getHandle() const
Definition: RObject.h:122
int Handle
Definition: RObject.h:59
RDocument * document
Definition: RStorage.h:723
EntityType
Entity types used for property handling / filtering.
Definition: RS.h:76
friend QDebug operator<<(QDebug dbg, const RObject &o)
Definition: RObject.h:214
Abstract base class for all objects.
Definition: RObject.h:52
bool protect
True if this object is protected (undeletable).
Definition: RObject.h:287
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RPropertyTypeId.h:58
bool undone
True if this object has been undone (deleted).
Definition: RObject.h:283
#define QCADCORE_EXPORT
Definition: core_global.h:10
int Id
Definition: RObject.h:57
This is the abstract base class for all storage implementations.
Definition: RStorage.h:62
static RPropertyTypeId PropertyType
Definition: RObject.h:63
Id objectId
Unique ID of this object.
Definition: RObject.h:275
Transaction implementation.
Definition: RTransaction.h:74
static RPropertyTypeId PropertyHandle
Definition: RObject.h:64
static RPropertyTypeId PropertyCustom
Definition: RObject.h:62
virtual bool hasPropertyType(RPropertyTypeId propertyTypeId)
Definition: RObject.h:165
void setId(RObject::Id id)
Definition: RObject.h:224
static const Id INVALID_ID
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RObject.h:58
QMap< int, QSharedPointer< RObject > > _RMapIntObjectPointer
Definition: RObject.h:304
void setProtected(bool on)
Definition: RObject.h:130
static bool setMember(T &variable, const QVariant &value, bool condition=true)
Definition: RObject.h:103
bool isUndone() const
Definition: RObject.h:134
void setDocument(RDocument *document)
Definition: RObject.h:90
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:199
void setHandle(RObject::Handle h)
Definition: RObject.h:228
const RDocument * getDocument() const
Definition: RObject.h:86
virtual void setAutoUpdatesBlocked(bool on)
Definition: RObject.h:203
static RPropertyTypeId PropertyProtected
Definition: RObject.h:65
RDocument * getDocument()
Definition: RObject.h:82
static bool hasPropertyType(const std::type_info &classInfo, RPropertyTypeId propertyTypeId)
Definition: RPropertyTypeId.cpp:211
#define RDEFAULT_QSTRING
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RDebug.h:48
QMap< QString, QVariantMap > customProperties
AppID -> key -> value e.g.
Definition: RObject.h:295
void init(void basePath)
Definition: AddBlockInit.js:2
RObject::Id getId() const
Definition: RObject.h:118
void dump()
Definition: RObject.h:207
bool isProtected() const
Definition: RObject.h:126
XYZ
Definition: RObject.h:67
Handle handle
Handle of this object (from DXF / DWG).
Definition: RObject.h:279
virtual void print(QDebug dbg) const
Stream operator for QDebug.
Definition: RObject.cpp:562
#define RDEFAULT_QVARIANT
Definition: RObject.h:32
static const Handle INVALID_HANDLE
Definition: RObject.h:60
RDocument * document
Definition: RObject.h:271