]> Shamusworld >> Repos - architektonas/blob - src/base/insert.cpp
Fixed problem with MDI activation.
[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 Insert::Insert(EntityContainer * parent, const InsertData & d):
26         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 Insert::~Insert()
41 {
42 }
43
44 /*virtual*/ Entity * Insert::clone()
45 {
46         Insert * i = new 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 Insert::rtti() const
56 {
57         return RS2::EntityInsert;
58 }
59
60 /** @return Copy of data that defines the insert. **/
61 InsertData Insert::getData() const
62 {
63         return data;
64 }
65
66 /**
67  * Reimplementation of reparent. Invalidates block cache pointer.
68  */
69 /*virtual*/ void Insert::reparent(EntityContainer * parent)
70 {
71         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 Block * Insert::getBlockForInsert()
82 {
83         if (block)
84                 return block;
85
86         BlockList * blkList;
87
88         if (!data.blockSource)
89                 blkList = (GetDrawing() ? GetDrawing()->getBlockList() : NULL);
90         else
91                 blkList = data.blockSource;
92
93         Block * blk = NULL;
94
95         if (blkList)
96                 blk = blkList->find(data.name);
97
98         block = blk;
99
100         return blk;
101 }
102
103 /**
104  * Updates the entity buffer of this insert entity. This method
105  * needs to be called whenever the block this insert is based on changes.
106  */
107 void Insert::update()
108 {
109         DEBUG->print("Insert::update");
110         DEBUG->print("Insert::update: name: %s", data.name.toLatin1().data());
111         DEBUG->print("Insert::update: insertionPoint: %f/%f",
112                 data.insertionPoint.x, data.insertionPoint.y);
113
114         if (!updateEnabled)
115                 return;
116
117         clear();
118         Block * blk = getBlockForInsert();
119
120         if (!blk)
121         {
122                 DEBUG->print("Insert::update: Block is NULL");
123                 return;
124         }
125
126         if (isUndone())
127         {
128                 DEBUG->print("Insert::update: Insert is in undo list");
129                 return;
130         }
131
132         if (fabs(data.scaleFactor.x) < 1.0e-6 || fabs(data.scaleFactor.y) < 1.0e-6)
133         {
134                 DEBUG->print("Insert::update: scale factor is 0");
135                 return;
136         }
137
138         Pen tmpPen;
139
140         /*Q3PtrListIterator<Entity> it = createIterator();
141         Entity* e;
142         while ( (e = it.current()) != NULL ) {
143                 ++it;*/
144
145         DEBUG->print("Insert::update: cols: %d, rows: %d", data.cols, data.rows);
146         DEBUG->print("Insert::update: block has %d entities", blk->count());
147
148         for(Entity * e=blk->firstEntity(); e!=NULL; e=blk->nextEntity())
149         {
150                 for(int c=0; c<data.cols; ++c)
151                 {
152                         DEBUG->print("Insert::update: col %d", c);
153
154                         for(int r=0; r<data.rows; ++r)
155                         {
156                                 DEBUG->print("Insert::update: row %d", r);
157
158                                 if (e->rtti() == RS2::EntityInsert && data.updateMode != RS2::PreviewUpdate)
159                                 {
160                                         DEBUG->print("Insert::update: updating sub-insert");
161                                         ((Insert *)e)->update();
162                                 }
163
164                                 DEBUG->print("Insert::update: cloning entity");
165
166                                 Entity * ne = e->clone();
167                                 ne->initId();
168                                 ne->setUpdateEnabled(false);
169                                 ne->setParent(this);
170                                 ne->setVisible(getFlag(RS2::FlagVisible));
171
172                                 DEBUG->print("Insert::update: transforming entity");
173
174                                 // Move:
175                                 DEBUG->print("Insert::update: move 1");
176                                 if (fabs(data.scaleFactor.x) > 1.0e-6 && fabs(data.scaleFactor.y) > 1.0e-6)
177                                 {
178                                         ne->move(data.insertionPoint + Vector(data.spacing.x / data.scaleFactor.x * c, data.spacing.y / data.scaleFactor.y * r));
179                                 }
180                                 else
181                                 {
182                                         ne->move(data.insertionPoint);
183                                 }
184
185                                 // Move because of block base point:
186                                 DEBUG->print("Insert::update: move 2");
187                                 ne->move(blk->getBasePoint() * -1);
188                                 // Scale:
189                                 DEBUG->print("Insert::update: scale");
190                                 ne->scale(data.insertionPoint, data.scaleFactor);
191                                 // Rotate:
192                                 DEBUG->print("Insert::update: rotate");
193                                 ne->rotate(data.insertionPoint, data.angle);
194                                 // Select:
195                                 ne->setSelected(isSelected());
196
197                                 // individual entities can be on indiv. layers
198                                 tmpPen = ne->getPen(false);
199
200                                 // color from block (free floating):
201                                 if (tmpPen.getColor() == Color(RS2::FlagByBlock))
202                                         tmpPen.setColor(getPen().getColor());
203
204                                 // line width from block (free floating):
205                                 if (tmpPen.getWidth() == RS2::WidthByBlock)
206                                         tmpPen.setWidth(getPen().getWidth());
207
208                                 // line type from block (free floating):
209                                 if (tmpPen.getLineType() == RS2::LineByBlock)
210                                         tmpPen.setLineType(getPen().getLineType());
211
212                                 // now that we've evaluated all flags, let's strip them:
213                                 // TODO: strip all flags (width, line type)
214                                 //tmpPen.setColor(tmpPen.getColor().stripFlags());
215
216                                 ne->setPen(tmpPen);
217
218                                 ne->setUpdateEnabled(true);
219
220                                 if (data.updateMode != RS2::PreviewUpdate)
221                                 {
222                                         DEBUG->print("Insert::update: updating new entity");
223                                         ne->update();
224                                 }
225
226                                 DEBUG->print("Insert::update: adding new entity");
227                                 addEntity(ne);
228                         }
229                 }
230         }
231
232         calculateBorders();
233
234         DEBUG->print("Insert::update: OK");
235 }
236
237 QString Insert::getName() const
238 {
239         return data.name;
240 }
241
242 void Insert::setName(const QString & newName)
243 {
244         data.name = newName;
245         update();
246 }
247
248 Vector Insert::getInsertionPoint() const
249 {
250         return data.insertionPoint;
251 }
252
253 void Insert::setInsertionPoint(const Vector & i)
254 {
255         data.insertionPoint = i;
256 }
257
258 Vector Insert::getScale() const
259 {
260         return data.scaleFactor;
261 }
262
263 void Insert::setScale(const Vector & s)
264 {
265         data.scaleFactor = s;
266 }
267
268 double Insert::getAngle() const
269 {
270         return data.angle;
271 }
272
273 void Insert::setAngle(double a)
274 {
275         data.angle = a;
276 }
277
278 int Insert::getCols() const
279 {
280         return data.cols;
281 }
282
283 void Insert::setCols(int c)
284 {
285         data.cols = c;
286 }
287
288 int Insert::getRows() const
289 {
290         return data.rows;
291 }
292
293 void Insert::setRows(int r)
294 {
295         data.rows = r;
296 }
297
298 Vector Insert::getSpacing() const
299 {
300         return data.spacing;
301 }
302
303 void Insert::setSpacing(const Vector & s)
304 {
305         data.spacing = s;
306 }
307
308 /**
309  * Is this insert visible? (re-implementation from Entity)
310  *
311  * @return true Only if the entity and the block and the layer it is on
312  * are visible.
313  * The Layer might also be NULL. In that case the layer visiblity
314  * is ignored.
315  * The Block might also be NULL. In that case the block visiblity
316  * is ignored.
317  */
318 bool Insert::isVisible()
319 {
320         Block * blk = getBlockForInsert();
321
322         if (blk != NULL)
323         {
324                 if (blk->isFrozen())
325                 {
326                         return false;
327                 }
328         }
329
330         return Entity::isVisible();
331 }
332
333 VectorSolutions Insert::getRefPoints()
334 {
335         VectorSolutions ret(data.insertionPoint);
336         return ret;
337 }
338
339 Vector Insert::getNearestRef(const Vector & coord, double * dist)
340 {
341         return getRefPoints().getClosest(coord, dist);
342 }
343
344 void Insert::move(Vector offset)
345 {
346         DEBUG->print("Insert::move: offset: %f/%f",
347                 offset.x, offset.y);
348         DEBUG->print("Insert::move1: insertionPoint: %f/%f",
349                 data.insertionPoint.x, data.insertionPoint.y);
350     data.insertionPoint.move(offset);
351         DEBUG->print("Insert::move2: insertionPoint: %f/%f",
352                 data.insertionPoint.x, data.insertionPoint.y);
353     update();
354 }
355
356 void Insert::rotate(Vector center, double angle)
357 {
358         DEBUG->print("Insert::rotate1: insertionPoint: %f/%f "
359             "/ center: %f/%f",
360                 data.insertionPoint.x, data.insertionPoint.y,
361                 center.x, center.y);
362     data.insertionPoint.rotate(center, angle);
363     data.angle = Math::correctAngle(data.angle + angle);
364         DEBUG->print("Insert::rotate2: insertionPoint: %f/%f",
365                 data.insertionPoint.x, data.insertionPoint.y);
366     update();
367 }
368
369 void Insert::scale(Vector center, Vector factor)
370 {
371         DEBUG->print("Insert::scale1: insertionPoint: %f/%f",
372                 data.insertionPoint.x, data.insertionPoint.y);
373     data.insertionPoint.scale(center, factor);
374     data.scaleFactor.scale(Vector(0.0, 0.0), factor);
375     data.spacing.scale(Vector(0.0, 0.0), factor);
376         DEBUG->print("Insert::scale2: insertionPoint: %f/%f",
377                 data.insertionPoint.x, data.insertionPoint.y);
378     update();
379 }
380
381 void Insert::mirror(Vector axisPoint1, Vector axisPoint2)
382 {
383         data.insertionPoint.mirror(axisPoint1, axisPoint2);
384
385         Vector vec;
386         vec.setPolar(1.0, data.angle);
387         vec.mirror(Vector(0.0, 0.0), axisPoint2 - axisPoint1);
388         data.angle = vec.angle();
389
390         data.scaleFactor.y *= -1;
391
392     update();
393 }
394
395 std::ostream & operator<<(std::ostream & os, const Insert & i)
396 {
397     os << " Insert: " << i.getData() << std::endl;
398     return os;
399 }