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 isParentLayerLocked(RLayer::Id layerId) const;
204  bool isEntityEditable(REntity::Id entityId) const;
205  //bool isEntityLayerLocked(REntity::Id entityId) const;
206  bool isLayerFrozen(RLayer::Id layerId) const;
207  bool isParentLayerFrozen(RLayer::Id layerId) const;
208  bool isBlockFrozen(RBlock::Id blockId) const;
209  bool isEntityLayerFrozen(REntity::Id entityId) const;
210 
211  bool hasSelection() const;
212 
213  RBox getBoundingBox(bool ignoreHiddenLayers = true, bool ignoreEmpty = false) const;
214  RBox getSelectionBox() const;
215 
216  //void addToSpatialIndex(RObject& object, bool isNew = false);
217  void clearSpatialIndices();
218  virtual void rebuildSpatialIndex();
219  //void addToSpatialIndex(QSharedPointer<REntity> entity);
220  void addToSpatialIndex(QSharedPointer<REntity> entity);
221 
222  bool blockContainsReferences(RBlock::Id blockId, RBlock::Id referencedBlockId);
223 
224  void removeBlockFromSpatialIndex(RBlock::Id blockId);
225  bool addBlockToSpatialIndex(RBlock::Id blockId, RObject::Id ignoreBlockId);
226  virtual void removeFromSpatialIndex(QSharedPointer<REntity> entity, const QList<RBox>& boundingBoxes = RDEFAULT_QLIST_RBOX);
227  //virtual void removeFromSpatialIndex2(QSharedPointer<REntity> entity);
228 
229  void updateAllEntities();
230 
231  QList<RTransaction> undo();
232  QList<RTransaction> redo();
233  bool isUndoAvailable() const;
234  bool isRedoAvailable() const;
235  void startTransactionGroup();
236  int getTransactionGroup() const;
237  void resetTransactionStack();
238 
239  void setFileName(const QString& fn);
240  QString getFileName() const;
241 
242  void setFileVersion(const QString& fv);
243  QString getFileVersion() const;
244 
245  QSharedPointer<RLayer> queryCurrentLayer();
246 // RTransaction setCurrentLayer(RLayer::Id layerId);
247 // RTransaction setCurrentLayer(const QString& layerName);
248 // void setCurrentLayer(RTransaction& transaction, RLayer::Id layerId);
249 // void setCurrentLayer(RTransaction& transaction, const QString& layerName);
250  void setCurrentLayer(RLayer::Id layerId, RTransaction* transaction=NULL);
251  void setCurrentLayer(const QString& layerName, RTransaction* transaction=NULL);
252  RLayer::Id getCurrentLayerId() const;
253  QString getCurrentLayerName() const;
254 
255  void setCurrentColor(const RColor& color);
256  RColor getCurrentColor() const;
257 
258  void setCurrentLineweight(RLineweight::Lineweight lw);
259  RLineweight::Lineweight getCurrentLineweight() const;
260 
261  void setCurrentLinetype(RLinetype::Id ltId);
262  void setCurrentLinetype(const QString& name);
263  void setCurrentLinetypePattern(const RLinetypePattern& p);
264  RLinetype::Id getCurrentLinetypeId() const;
265  RLinetypePattern getCurrentLinetypePattern() const;
266 
267  QSharedPointer<RBlock> queryCurrentBlock();
268  void setCurrentBlock(RBlock::Id blockId);
269  void setCurrentBlock(const QString& blockName);
270  RBlock::Id getCurrentBlockId() const;
271  QString getCurrentBlockName() const;
272 
273 // void setSelectedBlock(RBlock::Id blockId);
274 // void setSelectedBlock(const QString& blockName);
275 // RBlock::Id getSelectedBlockId() const;
276 
277  QSharedPointer<RView> queryCurrentView();
278  void setCurrentView(RView::Id viewId);
279  void setCurrentView(const QString& viewName);
280  RView::Id getCurrentViewId() const;
281 
282  QString getTempBlockName() const;
283  QString getBlockName(RBlock::Id blockId) const;
284  QSet<QString> getBlockNames(const QString& rxStr = RDEFAULT_QSTRING) const;
285  QString getLayerName(RLayer::Id layerId) const;
286  QSet<QString> getLayerNames(const QString& rxStr = RDEFAULT_QSTRING) const;
287  QString getLayoutName(RLayout::Id layoutId) const;
288  QSet<QString> getViewNames() const;
289 
290  bool hasLayer(const QString& layerName) const;
291  bool hasBlock(const QString& blockName) const;
292  bool hasView(const QString& viewName) const;
293  bool hasLinetype(const QString& linetypeName) const;
294 
295  RLayer::Id getLayerId(const QString& layerName) const;
296  RBlock::Id getBlockId(const QString& blockName) const;
297  RBlock::Id getModelSpaceBlockId() const;
298 
299  RLinetype::Id getLinetypeId(const QString& linetypeName) const;
301  return linetypeByLayerId;
302  }
304  return linetypeByBlockId;
305  }
306  QString getLinetypeName(RLinetype::Id linetypeId) const;
307  QString getLinetypeDescription(RLinetype::Id linetypeId) const;
308  QString getLinetypeLabel(RLinetype::Id linetypeId) const;
309  QSet<QString> getLinetypeNames() const;
310  QList<RLinetypePattern> getLinetypePatterns() const;
311  bool isByLayer(RLinetype::Id linetypeId) const;
312  bool isByBlock(RLinetype::Id linetypeId) const;
313 
314  RLineweight::Lineweight getMaxLineweight() const;
315 
316  QStringList getVariables() const;
317  void setVariable(const QString& key, const QVariant& value, bool overwrite=true);
318  QVariant getVariable(const QString& key, const QVariant& defaultValue = RDEFAULT_QVARIANT, bool useSettings = false) const;
319  bool hasVariable(const QString& key) const;
320  void removeVariable(const QString& key);
321 
322  void setKnownVariable(RS::KnownVariable key, const QVariant& value, RTransaction* transaction = NULL);
323  void setKnownVariable(RS::KnownVariable key, const RVector& value, RTransaction* transaction = NULL);
324  QVariant getKnownVariable(RS::KnownVariable key, const QVariant& defaultValue = RDEFAULT_QVARIANT) const;
325 
326  void setUnit(RS::Unit unit, RTransaction* transaction = NULL);
327  RS::Unit getUnit() const;
328 
329  void setMeasurement(RS::Measurement m, RTransaction* transaction = NULL);
330  RS::Measurement getMeasurement() const;
331 
332  bool isMetric() const;
333 
334  void setDimensionFont(const QString& f, RTransaction* transaction = NULL);
335  QString getDimensionFont() const;
336 
337  void setLinetypeScale(double v, RTransaction* transaction = NULL);
338  double getLinetypeScale() const;
339 
340  QString formatLinear(double value);
341  QString formatAngle(double value);
342 
343  void setLinearFormat(RS::LinearFormat f);
344  RS::LinearFormat getLinearFormat() const;
345  int getLinearPrecision();
346  bool showLeadingZeroes();
347  bool showTrailingZeroes();
348  bool showLeadingZeroesAngle();
349  bool showTrailingZeroesAngle();
350  RS::AngleFormat getAngleFormat();
351  int getAnglePrecision();
352  QChar getDecimalSeparator();
353 
354  bool isModified() const;
355  QDateTime getLastModifiedDateTime() const;
356  QString getLastModified() const;
357  void setModified(bool m);
358 
359  void copyVariablesFrom(const RDocument& other);
360 
361  /*
362  void copyToDocument(const RVector& reference, RDocument& other,
363  bool selectionOnly, bool clear, RTransaction& transaction);
364  void copyToDocument(REntity& entity, const RVector& reference,
365  RDocument& other, RTransaction& transaction);
366  */
367 
368  static RDocument& getClipboard();
369 
373  friend QDebug operator<<(QDebug dbg, RDocument& d);
374 
375  void dump();
376 
377 protected:
379 
380 private:
381  QString fileName;
382  QString fileVersion;
383 
385  // spatial index used to spawn new spatial indices for each block:
388  // map of spatial indices (per block):
389  QMap<RBlock::Id, RSpatialIndex*> spatialIndicesByBlock;
391  //RBlock::Id modelSpaceBlockId;
394 };
395 
398 
399 #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:386
QString fileVersion
Definition: RDocument.h:382
Copyright (c) 2011-2016 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:260
QDebug operator<<(QDebug dbg, const RBox &b)
Stream operator for QDebug.
Definition: RBox.cpp:447
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RLinetypePattern.h:45
Measurement
Enum of supported measurement systems.
Definition: RS.h:222
RLinetype::Id linetypeByLayerId
Definition: RDocument.h:392
static const Id INVALID_ID
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RObject.h:58
Lineweight
Definition: RLineweight.h:42
Color.
Definition: RColor.h:42
RLinetype::Id getLinetypeByLayerId() const
Definition: RDocument.h:300
#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-2016 by Andrew Mustun.
Definition: RTextLabel.h:30
LinearFormat
Format for length values.
Definition: RS.h:276
#define RMAXDOUBLE
Definition: RMath.h:63
#define RDEFAULT_QLIST_RS_ENTITYTYPE
Definition: RDocument.h:52
KnownVariable
Well established document variables.
Definition: RS.h:307
QString fileName
Definition: RDocument.h:381
void getClipboard()
Definition: library.js:71
static RDocument * clipboard
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RDocument.h:378
RStorage & storage
Definition: RDocument.h:384
QMap< RBlock::Id, RSpatialIndex * > spatialIndicesByBlock
Definition: RDocument.h:389
bool disableSpatialIndicesByBlock
Definition: RDocument.h:387
All entities (for filters)
Definition: RS.h:88
RLinetype::Id getLinetypeByBlockId() const
Definition: RDocument.h:303
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RTransactionStack.h:37
#define RDEFAULT_QSTRING
Copyright (c) 2011-2016 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:231
RTransactionStack transactionStack
Definition: RDocument.h:390
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RRequireHeap.h:30
RLinetype::Id linetypeByBlockId
Definition: RDocument.h:393
Abstract base class for spatial index implementations.
Definition: RSpatialIndex.h:69