]> Shamusworld >> Repos - architektonas/blob - src/base/insert.cpp
Initial removal of unnecessary rs_ prefixes from files.
[architektonas] / src / base / insert.cpp
1 // insert.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/01/2010  Added this text. :-)
15 //
16
17 #include "insert.h"
18
19 #include "block.h"
20 #include "drawing.h"
21
22 /**
23  * @param parent The graphic this block belongs to.
24  */
25 RS_Insert::RS_Insert(RS_EntityContainer * parent, const RS_InsertData & d):
26         RS_EntityContainer(parent), data(d)
27 {
28         block = NULL;
29
30         if (data.updateMode != RS2::NoUpdate)
31         {
32                 update();
33                 //calculateBorders();
34         }
35 }
36
37 /**
38  * Destructor.
39  */
40 RS_Insert::~RS_Insert()
41 {
42 }
43
44 /*virtual*/ RS_Entity * RS_Insert::clone()
45 {
46         RS_Insert * i = new RS_Insert(*this);
47 #warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
48 //      i->entities.setAutoDelete(entities.autoDelete());
49         i->initId();
50         i->detach();
51         return i;
52 }
53
54 /** @return RS2::EntityInsert */
55 /*virtual*/ RS2::EntityType RS_Insert::rtti() const
56 {
57         return RS2::EntityInsert;
58 }
59
60 /** @return Copy of data that defines the insert. **/
61 RS_InsertData RS_Insert::getData() const
62 {
63         return data;
64 }
65
66 /**
67  * Reimplementation of reparent. Invalidates block cache pointer.
68  */
69 /*virtual*/ void RS_Insert::reparent(RS_EntityContainer * parent)
70 {
71         RS_Entity::reparent(parent);
72         block = NULL;
73 }
74
75 /**
76  * @return Pointer to the block associated with this Insert or
77  *   NULL if the block couldn't be found. Blocks are requested
78  *   from the blockSource if one was supplied and otherwise from
79  *   the closest parent graphic.
80  */
81 RS_Block * RS_Insert::getBlockForInsert()
82 {
83         if (block)
84                 return block;
85
86         RS_BlockList * blkList;
87
88         if (!data.blockSource)
89         {
90                 blkList = (getGraphic() ? getGraphic()->getBlockList() : NULL);
91         }
92         else
93         {
94                 blkList = data.blockSource;
95         }
96
97         RS_Block * blk = NULL;
98
99         if (blkList)
100                 blk = blkList->find(data.name);
101
102 /*???
103         if (blk)
104         {
105         }
106 */
107
108         block = blk;
109
110         return blk;
111 }
112
113 /**
114  * Updates the entity buffer of this insert entity. This method
115  * needs to be called whenever the block this insert is based on changes.
116  */
117 void RS_Insert::update()
118 {
119         RS_DEBUG->print("RS_Insert::update");
120         RS_DEBUG->print("RS_Insert::update: name: %s", data.name.toLatin1().data());
121         RS_DEBUG->print("RS_Insert::update: insertionPoint: %f/%f",
122                 data.insertionPoint.x, data.insertionPoint.y);
123
124         if (!updateEnabled)
125                 return;
126
127         clear();
128         RS_Block * blk = getBlockForInsert();
129
130         if (!blk)
131         {
132                 RS_DEBUG->print("RS_Insert::update: Block is NULL");
133                 return;
134         }
135
136         if (isUndone())
137         {
138                 RS_DEBUG->print("RS_Insert::update: Insert is in undo list");
139                 return;
140         }
141
142         if (fabs(data.scaleFactor.x) < 1.0e-6 || fabs(data.scaleFactor.y) < 1.0e-6)
143         {
144                 RS_DEBUG->print("RS_Insert::update: scale factor is 0");
145                 return;
146         }
147
148         RS_Pen tmpPen;
149
150         /*Q3PtrListIterator<RS_Entity> it = createIterator();
151         RS_Entity* e;
152         while ( (e = it.current()) != NULL ) {
153                 ++it;*/
154
155         RS_DEBUG->print("RS_Insert::update: cols: %d, rows: %d", data.cols, data.rows);
156         RS_DEBUG->print("RS_Insert::update: block has %d entities", blk->count());
157
158         for(RS_Entity * e=blk->firstEntity(); e!=NULL; e=blk->nextEntity())
159         {
160                 for(int c=0; c<data.cols; ++c)
161                 {
162                         RS_DEBUG->print("RS_Insert::update: col %d", c);
163
164                         for(int r=0; r<data.rows; ++r)
165                         {
166                                 RS_DEBUG->print("RS_Insert::update: row %d", r);
167
168                                 if (e->rtti() == RS2::EntityInsert && data.updateMode != RS2::PreviewUpdate)
169                                 {
170                                         RS_DEBUG->print("RS_Insert::update: updating sub-insert");
171                                         ((RS_Insert *)e)->update();
172                                 }
173
174                                 RS_DEBUG->print("RS_Insert::update: cloning entity");
175
176                                 RS_Entity * ne = e->clone();
177                                 ne->initId();
178                                 ne->setUpdateEnabled(false);
179                                 ne->setParent(this);
180                                 ne->setVisible(getFlag(RS2::FlagVisible));
181
182                                 RS_DEBUG->print("RS_Insert::update: transforming entity");
183
184                                 // Move:
185                                 RS_DEBUG->print("RS_Insert::update: move 1");
186                                 if (fabs(data.scaleFactor.x) > 1.0e-6 && fabs(data.scaleFactor.y) > 1.0e-6)
187                                 {
188                                         ne->move(data.insertionPoint + Vector(data.spacing.x / data.scaleFactor.x * c, data.spacing.y / data.scaleFactor.y * r));
189                                 }
190                                 else
191                                 {
192                                         ne->move(data.insertionPoint);
193                                 }
194
195                                 // Move because of block base point:
196                                 RS_DEBUG->print("RS_Insert::update: move 2");
197                                 ne->move(blk->getBasePoint() * -1);
198                                 // Scale:
199                                 RS_DEBUG->print("RS_Insert::update: scale");
200                                 ne->scale(data.insertionPoint, data.scaleFactor);
201                                 // Rotate:
202                                 RS_DEBUG->print("RS_Insert::update: rotate");
203                                 ne->rotate(data.insertionPoint, data.angle);
204                                 // Select:
205                                 ne->setSelected(isSelected());
206
207                                 // individual entities can be on indiv. layers
208                                 tmpPen = ne->getPen(false);
209
210                                 // color from block (free floating):
211                                 if (tmpPen.getColor() == RS_Color(RS2::FlagByBlock))
212                                         tmpPen.setColor(getPen().getColor());
213
214                                 // line width from block (free floating):
215                                 if (tmpPen.getWidth() == RS2::WidthByBlock)
216                                         tmpPen.setWidth(getPen().getWidth());
217
218                                 // line type from block (free floating):
219                                 if (tmpPen.getLineType() == RS2::LineByBlock)
220                                         tmpPen.setLineType(getPen().getLineType());
221
222                                 // now that we've evaluated all flags, let's strip them:
223                                 // TODO: strip all flags (width, line type)
224                                 //tmpPen.setColor(tmpPen.getColor().stripFlags());
225
226                                 ne->setPen(tmpPen);
227
228                                 ne->setUpdateEnabled(true);
229
230                                 if (data.updateMode != RS2::PreviewUpdate)
231                                 {
232                                         RS_DEBUG->print("RS_Insert::update: updating new entity");
233                                         ne->update();
234                                 }
235
236                                 RS_DEBUG->print("RS_Insert::update: adding new entity");
237                                 addEntity(ne);
238                         }
239                 }
240         }
241
242         calculateBorders();
243
244         RS_DEBUG->print("RS_Insert::update: OK");
245 }
246
247 QString RS_Insert::getName() const
248 {
249         return data.name;
250 }
251
252 void RS_Insert::setName(const QString & newName)
253 {
254         data.name = newName;
255         update();
256 }
257
258 Vector RS_Insert::getInsertionPoint() const
259 {
260         return data.insertionPoint;
261 }
262
263 void RS_Insert::setInsertionPoint(const Vector & i)
264 {
265         data.insertionPoint = i;
266 }
267
268 Vector RS_Insert::getScale() const
269 {
270         return data.scaleFactor;
271 }
272
273 void RS_Insert::setScale(const Vector & s)
274 {
275         data.scaleFactor = s;
276 }
277
278 double RS_Insert::getAngle() const
279 {
280         return data.angle;
281 }
282
283 void RS_Insert::setAngle(double a)
284 {
285         data.angle = a;
286 }
287
288 int RS_Insert::getCols() const
289 {
290         return data.cols;
291 }
292
293 void RS_Insert::setCols(int c)
294 {
295         data.cols = c;
296 }
297
298 int RS_Insert::getRows() const
299 {
300         return data.rows;
301 }
302
303 void RS_Insert::setRows(int r)
304 {
305         data.rows = r;
306 }
307
308 Vector RS_Insert::getSpacing() const
309 {
310         return data.spacing;
311 }
312
313 void RS_Insert::setSpacing(const Vector & s)
314 {
315         data.spacing = s;
316 }
317
318 /**
319  * Is this insert visible? (re-implementation from RS_Entity)
320  *
321  * @return true Only if the entity and the block and the layer it is on
322  * are visible.
323  * The Layer might also be NULL. In that case the layer visiblity
324  * is ignored.
325  * The Block might also be NULL. In that case the block visiblity
326  * is ignored.
327  */
328 bool RS_Insert::isVisible()
329 {
330         RS_Block * blk = getBlockForInsert();
331
332         if (blk != NULL)
333         {
334                 if (blk->isFrozen())
335                 {
336                         return false;
337                 }
338         }
339
340         return RS_Entity::isVisible();
341 }
342
343 VectorSolutions RS_Insert::getRefPoints()
344 {
345         VectorSolutions ret(data.insertionPoint);
346         return ret;
347 }
348
349 Vector RS_Insert::getNearestRef(const Vector & coord, double * dist)
350 {
351         return getRefPoints().getClosest(coord, dist);
352 }
353
354 void RS_Insert::move(Vector offset)
355 {
356         RS_DEBUG->print("RS_Insert::move: offset: %f/%f",
357                 offset.x, offset.y);
358         RS_DEBUG->print("RS_Insert::move1: insertionPoint: %f/%f",
359                 data.insertionPoint.x, data.insertionPoint.y);
360     data.insertionPoint.move(offset);
361         RS_DEBUG->print("RS_Insert::move2: insertionPoint: %f/%f",
362                 data.insertionPoint.x, data.insertionPoint.y);
363     update();
364 }
365
366 void RS_Insert::rotate(Vector center, double angle)
367 {
368         RS_DEBUG->print("RS_Insert::rotate1: insertionPoint: %f/%f "
369             "/ center: %f/%f",
370                 data.insertionPoint.x, data.insertionPoint.y,
371                 center.x, center.y);
372     data.insertionPoint.rotate(center, angle);
373     data.angle = RS_Math::correctAngle(data.angle + angle);
374         RS_DEBUG->print("RS_Insert::rotate2: insertionPoint: %f/%f",
375                 data.insertionPoint.x, data.insertionPoint.y);
376     update();
377 }
378
379 void RS_Insert::scale(Vector center, Vector factor)
380 {
381         RS_DEBUG->print("RS_Insert::scale1: insertionPoint: %f/%f",
382                 data.insertionPoint.x, data.insertionPoint.y);
383     data.insertionPoint.scale(center, factor);
384     data.scaleFactor.scale(Vector(0.0, 0.0), factor);
385     data.spacing.scale(Vector(0.0, 0.0), factor);
386         RS_DEBUG->print("RS_Insert::scale2: insertionPoint: %f/%f",
387                 data.insertionPoint.x, data.insertionPoint.y);
388     update();
389 }
390
391 void RS_Insert::mirror(Vector axisPoint1, Vector axisPoint2)
392 {
393         data.insertionPoint.mirror(axisPoint1, axisPoint2);
394
395         Vector vec;
396         vec.setPolar(1.0, data.angle);
397         vec.mirror(Vector(0.0, 0.0), axisPoint2 - axisPoint1);
398         data.angle = vec.angle();
399
400         data.scaleFactor.y *= -1;
401
402     update();
403 }
404
405 std::ostream & operator<<(std::ostream & os, const RS_Insert & i)
406 {
407     os << " Insert: " << i.getData() << std::endl;
408     return os;
409 }