QCAD
Open Source 2D CAD
RDocument.h
Go to the documentation of this file.
1 
20 #ifndef RDOCUMENT_H
21 #define RDOCUMENT_H
22 
23 #include "core_global.h"
24 
25 #include <QString>
26 #include <QSharedPointer>
27 
28 #include "RBlock.h"
29 #include "RBlockReferenceEntity.h"
30 #include "RDocumentVariables.h"
31 #include "REntity.h"
32 #include "RNonCopyable.h"
33 #include "RRequireHeap.h"
34 #include "RSpatialIndex.h"
35 #include "RTransactionStack.h"
36 #include "RUcs.h"
37 #include "RLayer.h"
38 #include "RLinetype.h"
39 #include "RView.h"
40 
41 class RVector;
42 class RStorage;
43 
44 #ifndef RDEFAULT_QVARIANT
45 #define RDEFAULT_QVARIANT QVariant()
46 #endif
47 
48 #ifndef RDEFAULT_QLIST_RBOX
49 #define RDEFAULT_QLIST_RBOX QList<RBox>()
50 #endif
51 
52 #define RDEFAULT_QLIST_RS_ENTITYTYPE QList<RS::EntityType>()
53 
54 
68 public:
69  RDocument(RStorage& storage, RSpatialIndex& spatialIndex);
70  virtual ~RDocument();
71 
72  void init();
73  void initLinetypes(RTransaction* transaction=NULL);
74  QList<QSharedPointer<RObject> > getDefaultLinetypes();
75 
76  RStorage& getStorage();
77  const RStorage& getStorage() const;
78  RSpatialIndex& getSpatialIndex();
79  RSpatialIndex* getSpatialIndexForBlock(RBlock::Id blockId);
80  RSpatialIndex* getSpatialIndexForCurrentBlock();
81  RTransactionStack& getTransactionStack();
82 
83  void clear();
84 
85  REntity::Id queryClosestXY(
86  const RVector& wcsPosition,
87  double range,
88  bool draft,
89  double strictRange = RMAXDOUBLE,
90  bool includeLockedLayers = true,
91  bool selectedOnly = false
92  );
93  REntity::Id queryClosestXY(
94  QSet<REntity::Id>& candidates,
95  const RVector& wcsPosition,
96  double range,
97  bool draft,
98  double strictRange = RMAXDOUBLE
99  );
100 
101  QSet<RObject::Id> queryAllObjects() const;
102  QSet<REntity::Id> queryAllVisibleEntities() const;
103  QSet<REntity::Id> queryAllEntities(bool undone = false, bool allBlocks = false, RS::EntityType type = RS::EntityAll) const;
104  QSet<REntity::Id> queryAllEntities(bool undone, bool allBlocks, QList<RS::EntityType> types) const;
105  QSet<RUcs::Id> queryAllUcs() const;
106  QSet<RLayer::Id> queryAllLayers() const;
107  QSet<RBlock::Id> queryAllBlocks() const;
108  QSet<RBlock::Id> queryAllLayoutBlocks() const;
109  QSet<RView::Id> queryAllViews() const;
110  QSet<RLinetype::Id> queryAllLinetypes() const;
111 
112  QSet<REntity::Id> queryLayerEntities(RLayer::Id layerId, bool allBlocks = false) const;
113  bool hasBlockEntities(RBlock::Id blockId) const;
114  QSet<REntity::Id> queryBlockEntities(RBlock::Id blockId) const;
115  QSet<REntity::Id> queryLayerBlockEntities(RLayer::Id layerId, RBlock::Id blockId) const;
116  QSet<REntity::Id> queryChildEntities(REntity::Id parentId, RS::EntityType type = RS::EntityAll) const;
117  bool hasChildEntities(REntity::Id parentId) const;
118  QSet<REntity::Id> queryBlockReferences(RBlock::Id blockId) const;
119  QSet<REntity::Id> queryAllBlockReferences() const;
120 
121  QSet<REntity::Id> queryContainedEntities(
122  const RBox& box
123  );
124 
125  QSet<REntity::Id> queryInfiniteEntities();
126  QSet<REntity::Id> queryIntersectedEntitiesXY(
127  const RBox& box,
128  bool checkBoundingBoxOnly=false,
129  bool includeLockedLayers=true,
130  RBlock::Id blockId = RBlock::INVALID_ID,
131  const QList<RS::EntityType>& filter = RDEFAULT_QLIST_RS_ENTITYTYPE,
132  bool selectedOnly = false
133  );
134 
135  QMap<REntity::Id, QSet<int> > queryIntersectedShapesXY(
136  const RBox& box,
137  bool checkBoundingBoxOnly=false,
138  bool includeLockedLayers=true,
139  RBlock::Id blockId = RBlock::INVALID_ID,
140  const QList<RS::EntityType>& filter = RDEFAULT_QLIST_RS_ENTITYTYPE,
141  bool selectedOnly = false
142  );
143 
144  QSet<REntity::Id> queryContainedEntitiesXY(
145  const RBox& box
146  );
147 
148  QSet<REntity::Id> querySelectedEntities();
149  QSet<RObject::Id> queryPropertyEditorObjects();
150 
151  QSharedPointer<RDocumentVariables> queryDocumentVariables() const;
152  QSharedPointer<RDocumentVariables> queryDocumentVariablesDirect() const;
153  QSharedPointer<RObject> queryObject(RObject::Id objectId) const;
154  QSharedPointer<RObject> queryObjectDirect(RObject::Id objectId) const;
155  QSharedPointer<RObject> queryObjectByHandle(RObject::Handle objectHandle) const;
156  QSharedPointer<REntity> queryEntity(REntity::Id entityId) const;
157  QSharedPointer<REntity> queryEntityDirect(REntity::Id entityId) const;
158  QSharedPointer<RUcs> queryUcs(RUcs::Id ucsId) const;
159  QSharedPointer<RUcs> queryUcs(const QString& ucsName) const;
160  QSharedPointer<RLayer> queryLayer(RLayer::Id layerId) const;
161  QSharedPointer<RLayer> queryLayerDirect(RLayer::Id layerId) const;
162  QSharedPointer<RLayer> queryLayer(const QString& layerName) const;
163  QSharedPointer<RLayout> queryLayout(RLayout::Id layoutId) const;
164  QSharedPointer<RLayout> queryLayoutDirect(RLayout::Id layoutId) const;
165  QSharedPointer<RLayout> queryLayout(const QString& layoutName) const;
166  QSharedPointer<RBlock> queryBlock(RBlock::Id blockId) const;
167  QSharedPointer<RBlock> queryBlockDirect(RBlock::Id blockId) const;
168  QSharedPointer<RBlock> queryBlockDirect(const QString& blockName) const;
169  QSharedPointer<RBlock> queryBlock(const QString& blockName) const;
170  QSharedPointer<RView> queryView(RView::Id viewId) const;
171  QSharedPointer<RView> queryView(const QString& viewName) const;
172  QSharedPointer<RLinetype> queryLinetype(RLinetype::Id linetypeId) const;
173  QSharedPointer<RLinetype> queryLinetype(const QString& linetypeName) const;
174 
175  int countSelectedEntities() const;
176 
177  void clearSelection(
178  QSet<REntity::Id>* affectedEntities=NULL
179  );
180 
181  void selectAllEntites(QSet<REntity::Id>* affectedEntities = NULL);
182 
183  void selectEntity(
184  REntity::Id entityId,
185  bool add=false,
186  QSet<REntity::Id>* affectedEntities=NULL
187  );
188  void deselectEntity(
189  REntity::Id entityId,
190  QSet<REntity::Id>* affectedEntities=NULL
191  );
192  void selectEntities(
193  const QSet<REntity::Id>& entityId,
194  bool add=false,
195  QSet<REntity::Id>* affectedEntities=NULL
196  );
197  bool deselectEntities(
198  const QSet<REntity::Id>& entityId,
199  QSet<REntity::Id>* affectedEntities=NULL
200  );
201  bool isSelected(REntity::Id entityId);
202  bool isLayerLocked(RLayer::Id layerId) const;
203  bool isLayerLocked(const RLayer& layer) const;
204  bool isParentLayerLocked(RLayer::Id layerId) const;
205  bool isParentLayerLocked(const RLayer& layer) const;
206  bool isEntityEditable(REntity::Id entityId) const;
207  //bool isEntityLayerLocked(REntity::Id entityId) const;
208  bool isLayerOff(RLayer::Id layerId) const;
209  bool isLayerOff(const RLayer& layer) const;
210  bool isLayerOffOrFrozen(RLayer::Id layerId) const;
211  bool isLayerOffOrFrozen(const RLayer& layer) const;
212  bool isLayerFrozen(RLayer::Id layerId) const;
213  bool isLayerFrozen(const RLayer& layer) const;
214  bool isParentLayerFrozen(RLayer::Id layerId) const;
215  bool isParentLayerFrozen(const RLayer& layer) const;
216  bool isBlockFrozen(RBlock::Id blockId) const;
217  bool isEntityLayerFrozen(REntity::Id entityId) const;
218 
219  bool hasSelection() const;
220 
221  RBox getBoundingBox(bool ignoreHiddenLayers = true, bool ignoreEmpty = false) const;
222  RBox getSelectionBox() const;
223 
224  //void addToSpatialIndex(RObject& object, bool isNew = false);
225  void clearSpatialIndices();
226  virtual void rebuildSpatialIndex();
227  //void addToSpatialIndex(QSharedPointer<REntity> entity);
228  void addToSpatialIndex(QSharedPointer<REntity> entity);
229 
230  bool blockContainsReferences(RBlock::Id blockId, RBlock::Id referencedBlockId);
231 
232  void removeBlockFromSpatialIndex(RBlock::Id blockId);
233  bool addBlockToSpatialIndex(RBlock::Id blockId, RObject::Id ignoreBlockId);
234  virtual void removeFromSpatialIndex(QSharedPointer<REntity> entity, const QList<RBox>& boundingBoxes = RDEFAULT_QLIST_RBOX);
235  //virtual void removeFromSpatialIndex2(QSharedPointer<REntity> entity);
236 
237  void updateAllEntities();
238 
239  QList<RTransaction> undo();
240  QList<RTransaction> redo();
241  bool isUndoAvailable() const;
242  bool isRedoAvailable() const;
243  void startTransactionGroup();
244  int getTransactionGroup() const;
245  void resetTransactionStack();
246 
247  void setFileName(const QString& fn);
248  QString getFileName() const;
249 
250  void setFileVersion(const QString& fv);
251  QString getFileVersion() const;
252 
253  QSharedPointer<RLayer> queryCurrentLayer();
254 // RTransaction setCurrentLayer(RLayer::Id layerId);
255 // RTransaction setCurrentLayer(const QString& layerName);
256 // void setCurrentLayer(RTransaction& transaction, RLayer::Id layerId);
257 // void setCurrentLayer(RTransaction& transaction, const QString& layerName);
258  void setCurrentLayer(RLayer::Id layerId, RTransaction* transaction=NULL);
259  void setCurrentLayer(const QString& layerName, RTransaction* transaction=NULL);
260  RLayer::Id getCurrentLayerId() const;
261  QString getCurrentLayerName() const;
262 
263  void setCurrentColor(const RColor& color);
264  RColor getCurrentColor() const;
265 
266  void setCurrentLineweight(RLineweight::Lineweight lw);
267  RLineweight::Lineweight getCurrentLineweight() const;
268 
269  void setCurrentLinetype(RLinetype::Id ltId);
270  void setCurrentLinetype(const QString& name);
271  void setCurrentLinetypePattern(const RLinetypePattern& p);
272  RLinetype::Id getCurrentLinetypeId() const;
273  RLinetypePattern getCurrentLinetypePattern() const;
274 
275  QSharedPointer<RBlock> queryCurrentBlock();
276  void setCurrentBlock(RBlock::Id blockId);
277  void setCurrentBlock(const QString& blockName);
278  RBlock::Id getCurrentBlockId() const;
279  QString getCurrentBlockName() const;
280 
281 // void setSelectedBlock(RBlock::Id blockId);
282 // void setSelectedBlock(const QString& blockName);
283 // RBlock::Id getSelectedBlockId() const;
284 
285  QSharedPointer<RView> queryCurrentView();
286  void setCurrentView(RView::Id viewId);
287  void setCurrentView(const QString& viewName);
288  RView::Id getCurrentViewId() const;
289 
290  QString getTempBlockName() const;
291  QString getBlockName(RBlock::Id blockId) const;
292  QSet<QString> getBlockNames(const QString& rxStr = RDEFAULT_QSTRING) const;
293  QString getLayerName(RLayer::Id layerId) const;
294  QSet<QString> getLayerNames(const QString& rxStr = RDEFAULT_QSTRING) const;
295  QString getLayoutName(RLayout::Id layoutId) const;
296  QSet<QString> getViewNames() const;
297 
298  bool hasLayer(const QString& layerName) const;
299  bool hasBlock(const QString& blockName) const;
300  bool hasView(const QString& viewName) const;
301  bool hasLinetype(const QString& linetypeName) const;
302 
303  RLayer::Id getLayerId(const QString& layerName) const;
304  RLayer::Id getLayer0Id() const;
305  RBlock::Id getBlockId(const QString& blockName) const;
306  RBlock::Id getModelSpaceBlockId() const;
307 
308  RLinetype::Id getLinetypeId(const QString& linetypeName) const;
310  return linetypeByLayerId;
311  }
313  return linetypeByBlockId;
314  }
315  QString getLinetypeName(RLinetype::Id linetypeId) const;
316  QString getLinetypeDescription(RLinetype::Id linetypeId) const;
317  QString getLinetypeLabel(RLinetype::Id linetypeId) const;
318  QSet<QString> getLinetypeNames() const;
319  QList<RLinetypePattern> getLinetypePatterns() const;
320  bool isByLayer(RLinetype::Id linetypeId) const;
321  bool isByBlock(RLinetype::Id linetypeId) const;
322 
323  RLineweight::Lineweight getMaxLineweight() const;
324 
325  QStringList getVariables() const;
326  void setVariable(const QString& key, const QVariant& value, bool overwrite=true);
327  QVariant getVariable(const QString& key, const QVariant& defaultValue = RDEFAULT_QVARIANT, bool useSettings = false) const;
328  bool hasVariable(const QString& key) const;
329  void removeVariable(const QString& key);
330 
331  void setKnownVariable(RS::KnownVariable key, const QVariant& value, RTransaction* transaction = NULL);
332  void setKnownVariable(RS::KnownVariable key, const RVector& value, RTransaction* transaction = NULL);
333  QVariant getKnownVariable(RS::KnownVariable key, const QVariant& defaultValue = RDEFAULT_QVARIANT) const;
334 
335  void setUnit(RS::Unit unit, RTransaction* transaction = NULL);
336  RS::Unit getUnit() const;
337 
338  void setMeasurement(RS::Measurement m, RTransaction* transaction = NULL);
339  RS::Measurement getMeasurement() const;
340 
341  bool isMetric() const;
342 
343  void setDimensionFont(const QString& f, RTransaction* transaction = NULL);
344  QString getDimensionFont() const;
345 
346  void setLinetypeScale(double v, RTransaction* transaction = NULL);
347  double getLinetypeScale() const;
348 
349  QString formatLinear(double value);
350  QString formatAngle(double value);
351 
352  void setLinearFormat(RS::LinearFormat f);
353  RS::LinearFormat getLinearFormat() const;
354  int getLinearPrecision();
355  bool showLeadingZeroes();
356  bool showTrailingZeroes();
357  bool showLeadingZeroesAngle();
358  bool showTrailingZeroesAngle();
359  RS::AngleFormat getAngleFormat();
360  int getAnglePrecision();
361  char getDecimalSeparator();
362 
363  bool isModified() const;
364  QDateTime getLastModifiedDateTime() const;
365  QString getLastModified() const;
366  void setModified(bool m);
367 
368  void copyVariablesFrom(const RDocument& other);
369 
370  /*
371  void copyToDocument(const RVector& reference, RDocument& other,
372  bool selectionOnly, bool clear, RTransaction& transaction);
373  void copyToDocument(REntity& entity, const RVector& reference,
374  RDocument& other, RTransaction& transaction);
375  */
376 
377  static RDocument& getClipboard();
378 
382  friend QDebug operator<<(QDebug dbg, RDocument& d);
383 
384  void dump();
385 
386 protected:
388 
389 private:
390  QString fileName;
391  QString fileVersion;
392 
394  // spatial index used to spawn new spatial indices for each block:
397  // map of spatial indices (per block):
398  QMap<RBlock::Id, RSpatialIndex*> spatialIndicesByBlock;
400  //RBlock::Id modelSpaceBlockId;
403 };
404 
407 
408 #endif
Represents a box e.g.
Definition: RBox.h:43
int Handle
Definition: RObject.h:59
Represents a 3d vector (x/y/z).
Definition: RVector.h:46
EntityType
Entity types used for property handling / filtering.
Definition: RS.h:76
RSpatialIndex & spatialIndex
Definition: RDocument.h:395
QString fileVersion
Definition: RDocument.h:391
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RNonCopyable.h:32
#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
Transaction implementation.
Definition: RTransaction.h:74
AngleFormat
Angle format for printing angles.
Definition: RS.h:280
QDebug operator<<(QDebug dbg, const RBox &b)
Stream operator for QDebug.
Definition: RBox.cpp:447
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RLinetypePattern.h:45
Measurement
Enum of supported measurement systems.
Definition: RS.h:242
RLinetype::Id linetypeByLayerId
Definition: RDocument.h:401
static const Id INVALID_ID
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RObject.h:58
Lineweight
Definition: RLineweight.h:42
Color.
Definition: RColor.h:42
RLinetype::Id getLinetypeByLayerId() const
Definition: RDocument.h:309
#define RDEFAULT_QLIST_RBOX
Definition: RDocument.h:49
A graphics document contains and owns entities, layers, user coordinate systems, variables, block definitions, etc.
Definition: RDocument.h:67
Q_DECLARE_METATYPE(RMath *)
#define RDEFAULT_QVARIANT
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RTextLabel.h:30
LinearFormat
Format for length values.
Definition: RS.h:296
#define RMAXDOUBLE
Definition: RMath.h:63
#define RDEFAULT_QLIST_RS_ENTITYTYPE
Definition: RDocument.h:52
KnownVariable
Well established document variables.
Definition: RS.h:327
QString fileName
Definition: RDocument.h:390
void getClipboard()
Definition: library.js:225
static RDocument * clipboard
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RDocument.h:387
RStorage & storage
Definition: RDocument.h:393
QMap< RBlock::Id, RSpatialIndex * > spatialIndicesByBlock
Definition: RDocument.h:398
bool disableSpatialIndicesByBlock
Definition: RDocument.h:396
All entities (for filters)
Definition: RS.h:88
RLinetype::Id getLinetypeByBlockId() const
Definition: RDocument.h:312
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RTransactionStack.h:37
#define RDEFAULT_QSTRING
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RDebug.h:48
void init(void basePath)
Definition: AddBlockInit.js:2
Unit
Enum of supported units (values correspond to DXF spec).
Definition: RS.h:251
RTransactionStack transactionStack
Definition: RDocument.h:399
Represents a layer in a drawing.
Definition: RLayer.h:48
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RRequireHeap.h:30
RLinetype::Id linetypeByBlockId
Definition: RDocument.h:402
Abstract base class for spatial index implementations.
Definition: RSpatialIndex.h:69