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. :-)
17 #include "rs_insert.h"
23 * @param parent The graphic this block belongs to.
25 RS_Insert::RS_Insert(RS_EntityContainer * parent, const RS_InsertData & d):
26 RS_EntityContainer(parent), data(d)
30 if (data.updateMode != RS2::NoUpdate)
40 RS_Insert::~RS_Insert()
44 /*virtual*/ RS_Entity * RS_Insert::clone()
46 RS_Insert * i = new RS_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 RS_Insert::rtti() const
57 return RS2::EntityInsert;
60 /** @return Copy of data that defines the insert. **/
61 RS_InsertData RS_Insert::getData() const
67 * Reimplementation of reparent. Invalidates block cache pointer.
69 /*virtual*/ void RS_Insert::reparent(RS_EntityContainer * parent)
71 RS_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 RS_Block * RS_Insert::getBlockForInsert()
86 RS_BlockList * blkList;
88 if (!data.blockSource)
90 blkList = (getGraphic() ? getGraphic()->getBlockList() : NULL);
94 blkList = data.blockSource;
97 RS_Block * blk = NULL;
100 blk = blkList->find(data.name);
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.
117 void RS_Insert::update()
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);
128 RS_Block * blk = getBlockForInsert();
132 RS_DEBUG->print("RS_Insert::update: Block is NULL");
138 RS_DEBUG->print("RS_Insert::update: Insert is in undo list");
142 if (fabs(data.scaleFactor.x) < 1.0e-6 || fabs(data.scaleFactor.y) < 1.0e-6)
144 RS_DEBUG->print("RS_Insert::update: scale factor is 0");
150 /*Q3PtrListIterator<RS_Entity> it = createIterator();
152 while ( (e = it.current()) != NULL ) {
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());
158 for(RS_Entity * e=blk->firstEntity(); e!=NULL; e=blk->nextEntity())
160 for(int c=0; c<data.cols; ++c)
162 RS_DEBUG->print("RS_Insert::update: col %d", c);
164 for(int r=0; r<data.rows; ++r)
166 RS_DEBUG->print("RS_Insert::update: row %d", r);
168 if (e->rtti() == RS2::EntityInsert && data.updateMode != RS2::PreviewUpdate)
170 RS_DEBUG->print("RS_Insert::update: updating sub-insert");
171 ((RS_Insert *)e)->update();
174 RS_DEBUG->print("RS_Insert::update: cloning entity");
176 RS_Entity * ne = e->clone();
178 ne->setUpdateEnabled(false);
180 ne->setVisible(getFlag(RS2::FlagVisible));
182 RS_DEBUG->print("RS_Insert::update: transforming entity");
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)
188 ne->move(data.insertionPoint + Vector(data.spacing.x / data.scaleFactor.x * c, data.spacing.y / data.scaleFactor.y * r));
192 ne->move(data.insertionPoint);
195 // Move because of block base point:
196 RS_DEBUG->print("RS_Insert::update: move 2");
197 ne->move(blk->getBasePoint() * -1);
199 RS_DEBUG->print("RS_Insert::update: scale");
200 ne->scale(data.insertionPoint, data.scaleFactor);
202 RS_DEBUG->print("RS_Insert::update: rotate");
203 ne->rotate(data.insertionPoint, data.angle);
205 ne->setSelected(isSelected());
207 // individual entities can be on indiv. layers
208 tmpPen = ne->getPen(false);
210 // color from block (free floating):
211 if (tmpPen.getColor() == RS_Color(RS2::FlagByBlock))
212 tmpPen.setColor(getPen().getColor());
214 // line width from block (free floating):
215 if (tmpPen.getWidth() == RS2::WidthByBlock)
216 tmpPen.setWidth(getPen().getWidth());
218 // line type from block (free floating):
219 if (tmpPen.getLineType() == RS2::LineByBlock)
220 tmpPen.setLineType(getPen().getLineType());
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());
228 ne->setUpdateEnabled(true);
230 if (data.updateMode != RS2::PreviewUpdate)
232 RS_DEBUG->print("RS_Insert::update: updating new entity");
236 RS_DEBUG->print("RS_Insert::update: adding new entity");
244 RS_DEBUG->print("RS_Insert::update: OK");
247 QString RS_Insert::getName() const
252 void RS_Insert::setName(const QString & newName)
258 Vector RS_Insert::getInsertionPoint() const
260 return data.insertionPoint;
263 void RS_Insert::setInsertionPoint(const Vector & i)
265 data.insertionPoint = i;
268 Vector RS_Insert::getScale() const
270 return data.scaleFactor;
273 void RS_Insert::setScale(const Vector & s)
275 data.scaleFactor = s;
278 double RS_Insert::getAngle() const
283 void RS_Insert::setAngle(double a)
288 int RS_Insert::getCols() const
293 void RS_Insert::setCols(int c)
298 int RS_Insert::getRows() const
303 void RS_Insert::setRows(int r)
308 Vector RS_Insert::getSpacing() const
313 void RS_Insert::setSpacing(const Vector & s)
319 * Is this insert visible? (re-implementation from RS_Entity)
321 * @return true Only if the entity and the block and the layer it is on
323 * The Layer might also be NULL. In that case the layer visiblity
325 * The Block might also be NULL. In that case the block visiblity
328 bool RS_Insert::isVisible()
330 RS_Block * blk = getBlockForInsert();
340 return RS_Entity::isVisible();
343 VectorSolutions RS_Insert::getRefPoints()
345 VectorSolutions ret(data.insertionPoint);
349 Vector RS_Insert::getNearestRef(const Vector & coord, double * dist)
351 return getRefPoints().getClosest(coord, dist);
354 void RS_Insert::move(Vector offset)
356 RS_DEBUG->print("RS_Insert::move: offset: %f/%f",
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);
366 void RS_Insert::rotate(Vector center, double angle)
368 RS_DEBUG->print("RS_Insert::rotate1: insertionPoint: %f/%f "
370 data.insertionPoint.x, data.insertionPoint.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);
379 void RS_Insert::scale(Vector center, Vector factor)
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);
391 void RS_Insert::mirror(Vector axisPoint1, Vector axisPoint2)
393 data.insertionPoint.mirror(axisPoint1, axisPoint2);
396 vec.setPolar(1.0, data.angle);
397 vec.mirror(Vector(0.0, 0.0), axisPoint2 - axisPoint1);
398 data.angle = vec.angle();
400 data.scaleFactor.y *= -1;
405 std::ostream & operator<<(std::ostream & os, const RS_Insert & i)
407 os << " Insert: " << i.getData() << std::endl;