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
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 06/01/2010 Added this text. :-)
23 * @param parent The graphic this block belongs to.
25 Insert::Insert(EntityContainer * parent, const InsertData & d):
26 EntityContainer(parent), data(d)
30 if (data.updateMode != RS2::NoUpdate)
44 /*virtual*/ Entity * Insert::clone()
46 Insert * i = new Insert(*this);
47 #warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
48 // i->entities.setAutoDelete(entities.autoDelete());
54 /** @return RS2::EntityInsert */
55 /*virtual*/ RS2::EntityType Insert::rtti() const
57 return RS2::EntityInsert;
60 /** @return Copy of data that defines the insert. **/
61 InsertData Insert::getData() const
67 * Reimplementation of reparent. Invalidates block cache pointer.
69 /*virtual*/ void Insert::reparent(EntityContainer * parent)
71 Entity::reparent(parent);
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.
81 Block * Insert::getBlockForInsert()
88 if (!data.blockSource)
89 blkList = (GetDrawing() ? GetDrawing()->getBlockList() : NULL);
91 blkList = data.blockSource;
96 blk = blkList->find(data.name);
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.
107 void Insert::update()
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);
118 Block * blk = getBlockForInsert();
122 DEBUG->print("Insert::update: Block is NULL");
128 DEBUG->print("Insert::update: Insert is in undo list");
132 if (fabs(data.scaleFactor.x) < 1.0e-6 || fabs(data.scaleFactor.y) < 1.0e-6)
134 DEBUG->print("Insert::update: scale factor is 0");
140 /*Q3PtrListIterator<Entity> it = createIterator();
142 while ( (e = it.current()) != NULL ) {
145 DEBUG->print("Insert::update: cols: %d, rows: %d", data.cols, data.rows);
146 DEBUG->print("Insert::update: block has %d entities", blk->count());
148 for(Entity * e=blk->firstEntity(); e!=NULL; e=blk->nextEntity())
150 for(int c=0; c<data.cols; ++c)
152 DEBUG->print("Insert::update: col %d", c);
154 for(int r=0; r<data.rows; ++r)
156 DEBUG->print("Insert::update: row %d", r);
158 if (e->rtti() == RS2::EntityInsert && data.updateMode != RS2::PreviewUpdate)
160 DEBUG->print("Insert::update: updating sub-insert");
161 ((Insert *)e)->update();
164 DEBUG->print("Insert::update: cloning entity");
166 Entity * ne = e->clone();
168 ne->setUpdateEnabled(false);
170 ne->setVisible(getFlag(RS2::FlagVisible));
172 DEBUG->print("Insert::update: transforming entity");
175 DEBUG->print("Insert::update: move 1");
176 if (fabs(data.scaleFactor.x) > 1.0e-6 && fabs(data.scaleFactor.y) > 1.0e-6)
178 ne->move(data.insertionPoint + Vector(data.spacing.x / data.scaleFactor.x * c, data.spacing.y / data.scaleFactor.y * r));
182 ne->move(data.insertionPoint);
185 // Move because of block base point:
186 DEBUG->print("Insert::update: move 2");
187 ne->move(blk->getBasePoint() * -1);
189 DEBUG->print("Insert::update: scale");
190 ne->scale(data.insertionPoint, data.scaleFactor);
192 DEBUG->print("Insert::update: rotate");
193 ne->rotate(data.insertionPoint, data.angle);
195 ne->setSelected(isSelected());
197 // individual entities can be on indiv. layers
198 tmpPen = ne->getPen(false);
200 // color from block (free floating):
201 if (tmpPen.getColor() == Color(RS2::FlagByBlock))
202 tmpPen.setColor(getPen().getColor());
204 // line width from block (free floating):
205 if (tmpPen.getWidth() == RS2::WidthByBlock)
206 tmpPen.setWidth(getPen().getWidth());
208 // line type from block (free floating):
209 if (tmpPen.getLineType() == RS2::LineByBlock)
210 tmpPen.setLineType(getPen().getLineType());
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());
218 ne->setUpdateEnabled(true);
220 if (data.updateMode != RS2::PreviewUpdate)
222 DEBUG->print("Insert::update: updating new entity");
226 DEBUG->print("Insert::update: adding new entity");
234 DEBUG->print("Insert::update: OK");
237 QString Insert::getName() const
242 void Insert::setName(const QString & newName)
248 Vector Insert::getInsertionPoint() const
250 return data.insertionPoint;
253 void Insert::setInsertionPoint(const Vector & i)
255 data.insertionPoint = i;
258 Vector Insert::getScale() const
260 return data.scaleFactor;
263 void Insert::setScale(const Vector & s)
265 data.scaleFactor = s;
268 double Insert::getAngle() const
273 void Insert::setAngle(double a)
278 int Insert::getCols() const
283 void Insert::setCols(int c)
288 int Insert::getRows() const
293 void Insert::setRows(int r)
298 Vector Insert::getSpacing() const
303 void Insert::setSpacing(const Vector & s)
309 * Is this insert visible? (re-implementation from Entity)
311 * @return true Only if the entity and the block and the layer it is on
313 * The Layer might also be NULL. In that case the layer visiblity
315 * The Block might also be NULL. In that case the block visiblity
318 bool Insert::isVisible()
320 Block * blk = getBlockForInsert();
330 return Entity::isVisible();
333 VectorSolutions Insert::getRefPoints()
335 VectorSolutions ret(data.insertionPoint);
339 Vector Insert::getNearestRef(const Vector & coord, double * dist)
341 return getRefPoints().getClosest(coord, dist);
344 void Insert::move(Vector offset)
346 DEBUG->print("Insert::move: offset: %f/%f",
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);
356 void Insert::rotate(Vector center, double angle)
358 DEBUG->print("Insert::rotate1: insertionPoint: %f/%f "
360 data.insertionPoint.x, data.insertionPoint.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);
369 void Insert::scale(Vector center, Vector factor)
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);
381 void Insert::mirror(Vector axisPoint1, Vector axisPoint2)
383 data.insertionPoint.mirror(axisPoint1, axisPoint2);
386 vec.setPolar(1.0, data.angle);
387 vec.mirror(Vector(0.0, 0.0), axisPoint2 - axisPoint1);
388 data.angle = vec.angle();
390 data.scaleFactor.y *= -1;
395 std::ostream & operator<<(std::ostream & os, const Insert & i)
397 os << " Insert: " << i.getData() << std::endl;