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