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