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<RView::Id> queryAllViews() const;
109  QSet<RLinetype::Id> queryAllLinetypes() const;
110 
111  QSet<REntity::Id> queryLayerEntities(RLayer::Id layerId, bool allBlocks = false) const;
112  bool hasBlockEntities(RBlock::Id blockId) const;
113  QSet<REntity::Id> queryBlockEntities(RBlock::Id blockId) const;
114  QSet<REntity::Id> queryLayerBlockEntities(RLayer::Id layerId, RBlock::Id blockId) const;
115  QSet<REntity::Id> queryChildEntities(REntity::Id parentId, RS::EntityType type = RS::EntityAll) const;
116  bool hasChildEntities(REntity::Id parentId) const;
117  QSet<REntity::Id> queryBlockReferences(RBlock::Id blockId) const;
118  QSet<REntity::Id> queryAllBlockReferences() const;
119 
120  QSet<REntity::Id> queryContainedEntities(
121  const RBox& box
122  );
123 
124  QSet<REntity::Id> queryInfiniteEntities();
125  QSet<REntity::Id> queryIntersectedEntitiesXY(
126  const RBox& box,
127  bool checkBoundingBoxOnly=false,
128  bool includeLockedLayers=true,
129  RBlock::Id blockId = RBlock::INVALID_ID,
130  const QList<RS::EntityType>& filter = RDEFAULT_QLIST_RS_ENTITYTYPE,
131  bool selectedOnly = false
132  );
133 
134  QMap<REntity::Id, QSet<int> > queryIntersectedShapesXY(
135  const RBox& box,
136  bool checkBoundingBoxOnly=false,
137  bool includeLockedLayers=true,
138  RBlock::Id blockId = RBlock::INVALID_ID,
139  const QList<RS::EntityType>& filter = RDEFAULT_QLIST_RS_ENTITYTYPE,
140  bool selectedOnly = false
141  );
142 
143  QSet<REntity::Id> queryContainedEntitiesXY(
144  const RBox& box
145  );
146 
147  QSet<REntity::Id> querySelectedEntities();
148  QSet<RObject::Id> queryPropertyEditorObjects();
149 
150  QSharedPointer<RDocumentVariables> queryDocumentVariables() const;
151  QSharedPointer<RDocumentVariables> queryDocumentVariablesDirect() const;
152  QSharedPointer<RObject> queryObject(RObject::Id objectId) const;
153  QSharedPointer<RObject> queryObjectDirect(RObject::Id objectId) const;
154  QSharedPointer<RObject> queryObjectByHandle(RObject::Handle objectHandle) const;
155  QSharedPointer<REntity> queryEntity(REntity::Id entityId) const;
156  QSharedPointer<REntity> queryEntityDirect(REntity::Id entityId) const;
157  QSharedPointer<RUcs> queryUcs(RUcs::Id ucsId) const;
158  QSharedPointer<RUcs> queryUcs(const QString& ucsName) const;
159  QSharedPointer<RLayer> queryLayer(RLayer::Id layerId) const;
160  QSharedPointer<RLayer> queryLayerDirect(RLayer::Id layerId) const;
161  QSharedPointer<RLayer> queryLayer(const QString& layerName) const;
162  QSharedPointer<RLayout> queryLayout(RLayout::Id layoutId) const;
163  QSharedPointer<RLayout> queryLayoutDirect(RLayout::Id layoutId) const;
164  QSharedPointer<RLayout> queryLayout(const QString& layoutName) const;
165  QSharedPointer<RBlock> queryBlock(RBlock::Id blockId) const;
166  QSharedPointer<RBlock> queryBlockDirect(RBlock::Id blockId) const;
167  QSharedPointer<RBlock> queryBlock(const QString& blockName) const;
168  QSharedPointer<RView> queryView(RView::Id viewId) const;
169  QSharedPointer<RView> queryView(const QString& viewName) const;
170  QSharedPointer<RLinetype> queryLinetype(RLinetype::Id linetypeId) const;
171  QSharedPointer<RLinetype> queryLinetype(const QString& linetypeName) const;
172 
173  int countSelectedEntities() const;
174 
175  void clearSelection(
176  QSet<REntity::Id>* affectedEntities=NULL
177  );
178 
179  void selectAllEntites(QSet<REntity::Id>* affectedEntities = NULL);
180 
181  void selectEntity(
182  REntity::Id entityId,
183  bool add=false,
184  QSet<REntity::Id>* affectedEntities=NULL
185  );
186  void deselectEntity(
187  REntity::Id entityId,
188  QSet<REntity::Id>* affectedEntities=NULL
189  );
190  void selectEntities(
191  const QSet<REntity::Id>& entityId,
192  bool add=false,
193  QSet<REntity::Id>* affectedEntities=NULL
194  );
195  bool deselectEntities(
196  const QSet<REntity::Id>& entityId,
197  QSet<REntity::Id>* affectedEntities=NULL
198  );
199  bool isSelected(REntity::Id entityId);
200  bool isLayerLocked(RLayer::Id layerId) const;
201  bool isEntityEditable(REntity::Id entityId) const;
202  //bool isEntityLayerLocked(REntity::Id entityId) const;
203  bool isLayerFrozen(RLayer::Id layerId) const;
204  bool isBlockFrozen(RBlock::Id blockId) const;
205  bool isEntityLayerFrozen(REntity::Id entityId) const;
206 
207  bool hasSelection() const;
208 
209  RBox getBoundingBox(bool ignoreHiddenLayers = true, bool ignoreEmpty = false) const;
210  RBox getSelectionBox() const;
211 
212  //void addToSpatialIndex(RObject& object, bool isNew = false);
213  void clearSpatialIndices();
214  virtual void rebuildSpatialIndex();
215  //void addToSpatialIndex(QSharedPointer<REntity> entity);
216  void addToSpatialIndex(QSharedPointer<REntity> entity);
217 
218  bool blockContainsReferences(RBlock::Id blockId, RBlock::Id referencedBlockId);
219 
220  void removeBlockFromSpatialIndex(RBlock::Id blockId);
221  bool addBlockToSpatialIndex(RBlock::Id blockId, RObject::Id ignoreBlockId);
222  virtual void removeFromSpatialIndex(QSharedPointer<REntity> entity, const QList<RBox>& boundingBoxes = RDEFAULT_QLIST_RBOX);
223  //virtual void removeFromSpatialIndex2(QSharedPointer<REntity> entity);
224 
225  void updateAllEntities();
226 
227  QList<RTransaction> undo();
228  QList<RTransaction> redo();
229  bool isUndoAvailable() const;
230  bool isRedoAvailable() const;
231  void startTransactionGroup();
232  int getTransactionGroup() const;
233  void resetTransactionStack();
234 
235  void setFileName(const QString& fn);
236  QString getFileName() const;
237 
238  void setFileVersion(const QString& fv);
239  QString getFileVersion() const;
240 
241  QSharedPointer<RLayer> queryCurrentLayer();
242 // RTransaction setCurrentLayer(RLayer::Id layerId);
243 // RTransaction setCurrentLayer(const QString& layerName);
244 // void setCurrentLayer(RTransaction& transaction, RLayer::Id layerId);
245 // void setCurrentLayer(RTransaction& transaction, const QString& layerName);
246  void setCurrentLayer(RLayer::Id layerId, RTransaction* transaction=NULL);
247  void setCurrentLayer(const QString& layerName, RTransaction* transaction=NULL);
248  RLayer::Id getCurrentLayerId() const;
249  QString getCurrentLayerName() const;
250 
251  void setCurrentColor(const RColor& color);
252  RColor getCurrentColor() const;
253 
254  void setCurrentLineweight(RLineweight::Lineweight lw);
255  RLineweight::Lineweight getCurrentLineweight() const;
256 
257  void setCurrentLinetype(RLinetype::Id ltId);
258  void setCurrentLinetype(const QString& name);
259  void setCurrentLinetypePattern(const RLinetypePattern& p);
260  RLinetype::Id getCurrentLinetypeId() const;
261  RLinetypePattern getCurrentLinetypePattern() const;
262 
263  QSharedPointer<RBlock> queryCurrentBlock();
264  void setCurrentBlock(RBlock::Id blockId);
265  void setCurrentBlock(const QString& blockName);
266  RBlock::Id getCurrentBlockId() const;
267  QString getCurrentBlockName() const;
268 
269 // void setSelectedBlock(RBlock::Id blockId);
270 // void setSelectedBlock(const QString& blockName);
271 // RBlock::Id getSelectedBlockId() const;
272 
273  QSharedPointer<RView> queryCurrentView();
274  void setCurrentView(RView::Id viewId);
275  void setCurrentView(const QString& viewName);
276  RView::Id getCurrentViewId() const;
277 
278  QString getTempBlockName() const;
279  QString getBlockName(RBlock::Id blockId) const;
280  QSet<QString> getBlockNames(const QString& rxStr = RDEFAULT_QSTRING) const;
281  QString getLayerName(RLayer::Id layerId) const;
282  QSet<QString> getLayerNames(const QString& rxStr = RDEFAULT_QSTRING) const;
283  QString getLayoutName(RLayout::Id layoutId) const;
284  QSet<QString> getViewNames() const;
285 
286  bool hasLayer(const QString& layerName) const;
287  bool hasBlock(const QString& blockName) const;
288  bool hasView(const QString& viewName) const;
289  bool hasLinetype(const QString& linetypeName) const;
290 
291  RLayer::Id getLayerId(const QString& layerName) const;
292  RBlock::Id getBlockId(const QString& blockName) const;
293  RBlock::Id getModelSpaceBlockId() const;
294 
295  RLinetype::Id getLinetypeId(const QString& linetypeName) const;
297  return linetypeByLayerId;
298  }
300  return linetypeByBlockId;
301  }
302  QString getLinetypeName(RLinetype::Id linetypeId) const;
303  QString getLinetypeDescription(RLinetype::Id linetypeId) const;
304  QString getLinetypeLabel(RLinetype::Id linetypeId) const;
305  QSet<QString> getLinetypeNames() const;
306  QList<RLinetypePattern> getLinetypePatterns() const;
307  bool isByLayer(RLinetype::Id linetypeId) const;
308  bool isByBlock(RLinetype::Id linetypeId) const;
309 
310  RLineweight::Lineweight getMaxLineweight() const;
311 
312  QStringList getVariables() const;
313  void setVariable(const QString& key, const QVariant& value, bool overwrite=true);
314  QVariant getVariable(const QString& key, const QVariant& defaultValue = RDEFAULT_QVARIANT, bool useSettings = false) const;
315  bool hasVariable(const QString& key) const;
316  void removeVariable(const QString& key);
317 
318  void setKnownVariable(RS::KnownVariable key, const QVariant& value, RTransaction* transaction = NULL);
319  void setKnownVariable(RS::KnownVariable key, const RVector& value, RTransaction* transaction = NULL);
320  QVariant getKnownVariable(RS::KnownVariable key, const QVariant& defaultValue = RDEFAULT_QVARIANT) const;
321 
322  void setUnit(RS::Unit unit, RTransaction* transaction = NULL);
323  RS::Unit getUnit() const;
324 
325  void setMeasurement(RS::Measurement m, RTransaction* transaction = NULL);
326  RS::Measurement getMeasurement() const;
327 
328  bool isMetric() const;
329 
330  void setDimensionFont(const QString& f, RTransaction* transaction = NULL);
331  QString getDimensionFont() const;
332 
333  void setLinetypeScale(double v, RTransaction* transaction = NULL);
334  double getLinetypeScale() const;
335 
336  QString formatLinear(double value);
337  QString formatAngle(double value);
338 
339  RS::LinearFormat getLinearFormat();
340  int getLinearPrecision();
341  bool showLeadingZeroes();
342  bool showTrailingZeroes();
343  bool showLeadingZeroesAngle();
344  bool showTrailingZeroesAngle();
345  RS::AngleFormat getAngleFormat();
346  int getAnglePrecision();
347  QChar getDecimalSeparator();
348 
349  bool isModified() const;
350  void setModified(bool m);
351 
352  void copyVariablesFrom(const RDocument& other);
353 
354  /*
355  void copyToDocument(const RVector& reference, RDocument& other,
356  bool selectionOnly, bool clear, RTransaction& transaction);
357  void copyToDocument(REntity& entity, const RVector& reference,
358  RDocument& other, RTransaction& transaction);
359  */
360 
361  static RDocument& getClipboard();
362 
366  friend QDebug operator<<(QDebug dbg, RDocument& d);
367 
368  void dump();
369 
370 protected:
372 
373 private:
374  QString fileName;
375  QString fileVersion;
376 
378  // spatial index used to spawn new spatial indices for each block:
381  // map of spatial indices (per block):
382  QMap<RBlock::Id, RSpatialIndex*> spatialIndicesByBlock;
384  //RBlock::Id modelSpaceBlockId;
387 };
388 
391 
392 #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:379
QString fileVersion
Definition: RDocument.h:375
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:70
AngleFormat
Angle format for printing angles.
Definition: RS.h:257
QDebug operator<<(QDebug dbg, const RBox &b)
Stream operator for QDebug.
Definition: RBox.cpp:402
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RLinetypePattern.h:45
Measurement
Enum of supported measurement systems.
Definition: RS.h:219
RLinetype::Id linetypeByLayerId
Definition: RDocument.h:385
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:296
#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:273
#define RMAXDOUBLE
Definition: RMath.h:63
#define RDEFAULT_QLIST_RS_ENTITYTYPE
Definition: RDocument.h:52
KnownVariable
Well established document variables.
Definition: RS.h:304
QString fileName
Definition: RDocument.h:374
void getClipboard()
Definition: library.js:771
static RDocument * clipboard
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RDocument.h:371
RStorage & storage
Definition: RDocument.h:377
QMap< RBlock::Id, RSpatialIndex * > spatialIndicesByBlock
Definition: RDocument.h:382
bool disableSpatialIndicesByBlock
Definition: RDocument.h:380
All entities (for filters)
Definition: RS.h:86
RLinetype::Id getLinetypeByBlockId() const
Definition: RDocument.h:299
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:228
RTransactionStack transactionStack
Definition: RDocument.h:383
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RRequireHeap.h:30
RLinetype::Id linetypeByBlockId
Definition: RDocument.h:386
Abstract base class for spatial index implementations.
Definition: RSpatialIndex.h:69