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 == NULL)
90 if (getGraphic() != NULL)
92 blkList = getGraphic()->getBlockList();
101 blkList = data.blockSource;
104 RS_Block * blk = NULL;
108 blk = blkList->find(data.name);
121 * Updates the entity buffer of this insert entity. This method
122 * needs to be called whenever the block this insert is based on changes.
124 void RS_Insert::update()
126 RS_DEBUG->print("RS_Insert::update");
127 RS_DEBUG->print("RS_Insert::update: name: %s", data.name.toLatin1().data());
128 RS_DEBUG->print("RS_Insert::update: insertionPoint: %f/%f",
129 data.insertionPoint.x, data.insertionPoint.y);
131 if (updateEnabled == false)
136 RS_Block * blk = getBlockForInsert();
141 RS_DEBUG->print("RS_Insert::update: Block is NULL");
147 RS_DEBUG->print("RS_Insert::update: Insert is in undo list");
151 if (fabs(data.scaleFactor.x) < 1.0e-6 || fabs(data.scaleFactor.y) < 1.0e-6)
153 RS_DEBUG->print("RS_Insert::update: scale factor is 0");
159 /*Q3PtrListIterator<RS_Entity> it = createIterator();
161 while ( (e = it.current()) != NULL ) {
164 RS_DEBUG->print("RS_Insert::update: cols: %d, rows: %d", data.cols, data.rows);
165 RS_DEBUG->print("RS_Insert::update: block has %d entities", blk->count());
167 for(RS_Entity * e=blk->firstEntity(); e!=NULL; e=blk->nextEntity())
169 for(int c=0; c<data.cols; ++c)
171 RS_DEBUG->print("RS_Insert::update: col %d", c);
173 for(int r=0; r<data.rows; ++r)
175 RS_DEBUG->print("RS_Insert::update: row %d", r);
177 if (e->rtti() == RS2::EntityInsert && data.updateMode != RS2::PreviewUpdate)
179 RS_DEBUG->print("RS_Insert::update: updating sub-insert");
180 ((RS_Insert *)e)->update();
183 RS_DEBUG->print("RS_Insert::update: cloning entity");
185 RS_Entity * ne = e->clone();
187 ne->setUpdateEnabled(false);
189 ne->setVisible(getFlag(RS2::FlagVisible));
191 RS_DEBUG->print("RS_Insert::update: transforming entity");
194 RS_DEBUG->print("RS_Insert::update: move 1");
195 if (fabs(data.scaleFactor.x) > 1.0e-6 && fabs(data.scaleFactor.y) > 1.0e-6)
197 ne->move(data.insertionPoint + Vector(data.spacing.x / data.scaleFactor.x * c,
198 data.spacing.y / data.scaleFactor.y * r));
202 ne->move(data.insertionPoint);
205 // Move because of block base point:
206 RS_DEBUG->print("RS_Insert::update: move 2");
207 ne->move(blk->getBasePoint() * -1);
209 RS_DEBUG->print("RS_Insert::update: scale");
210 ne->scale(data.insertionPoint, data.scaleFactor);
212 RS_DEBUG->print("RS_Insert::update: rotate");
213 ne->rotate(data.insertionPoint, data.angle);
215 ne->setSelected(isSelected());
217 // individual entities can be on indiv. layers
218 tmpPen = ne->getPen(false);
220 // color from block (free floating):
221 if (tmpPen.getColor() == RS_Color(RS2::FlagByBlock))
222 tmpPen.setColor(getPen().getColor());
224 // line width from block (free floating):
225 if (tmpPen.getWidth() == RS2::WidthByBlock)
226 tmpPen.setWidth(getPen().getWidth());
228 // line type from block (free floating):
229 if (tmpPen.getLineType() == RS2::LineByBlock)
230 tmpPen.setLineType(getPen().getLineType());
232 // now that we've evaluated all flags, let's strip them:
233 // TODO: strip all flags (width, line type)
234 //tmpPen.setColor(tmpPen.getColor().stripFlags());
238 ne->setUpdateEnabled(true);
240 if (data.updateMode != RS2::PreviewUpdate)
242 RS_DEBUG->print("RS_Insert::update: updating new entity");
246 RS_DEBUG->print("RS_Insert::update: adding new entity");
254 RS_DEBUG->print("RS_Insert::update: OK");
257 QString RS_Insert::getName() const
262 void RS_Insert::setName(const QString & newName)
268 Vector RS_Insert::getInsertionPoint() const
270 return data.insertionPoint;
273 void RS_Insert::setInsertionPoint(const Vector & i)
275 data.insertionPoint = i;
278 Vector RS_Insert::getScale() const
280 return data.scaleFactor;
283 void RS_Insert::setScale(const Vector & s)
285 data.scaleFactor = s;
288 double RS_Insert::getAngle() const
293 void RS_Insert::setAngle(double a)
298 int RS_Insert::getCols() const
303 void RS_Insert::setCols(int c)
308 int RS_Insert::getRows() const
313 void RS_Insert::setRows(int r)
318 Vector RS_Insert::getSpacing() const
323 void RS_Insert::setSpacing(const Vector & s)
329 * Is this insert visible? (re-implementation from RS_Entity)
331 * @return true Only if the entity and the block and the layer it is on
333 * The Layer might also be NULL. In that case the layer visiblity
335 * The Block might also be NULL. In that case the block visiblity
338 bool RS_Insert::isVisible()
340 RS_Block * blk = getBlockForInsert();
350 return RS_Entity::isVisible();
353 VectorSolutions RS_Insert::getRefPoints()
355 VectorSolutions ret(data.insertionPoint);
359 Vector RS_Insert::getNearestRef(const Vector & coord, double * dist)
361 return getRefPoints().getClosest(coord, dist);
364 void RS_Insert::move(Vector offset)
366 RS_DEBUG->print("RS_Insert::move: offset: %f/%f",
368 RS_DEBUG->print("RS_Insert::move1: insertionPoint: %f/%f",
369 data.insertionPoint.x, data.insertionPoint.y);
370 data.insertionPoint.move(offset);
371 RS_DEBUG->print("RS_Insert::move2: insertionPoint: %f/%f",
372 data.insertionPoint.x, data.insertionPoint.y);
376 void RS_Insert::rotate(Vector center, double angle)
378 RS_DEBUG->print("RS_Insert::rotate1: insertionPoint: %f/%f "
380 data.insertionPoint.x, data.insertionPoint.y,
382 data.insertionPoint.rotate(center, angle);
383 data.angle = RS_Math::correctAngle(data.angle + angle);
384 RS_DEBUG->print("RS_Insert::rotate2: insertionPoint: %f/%f",
385 data.insertionPoint.x, data.insertionPoint.y);
389 void RS_Insert::scale(Vector center, Vector factor)
391 RS_DEBUG->print("RS_Insert::scale1: insertionPoint: %f/%f",
392 data.insertionPoint.x, data.insertionPoint.y);
393 data.insertionPoint.scale(center, factor);
394 data.scaleFactor.scale(Vector(0.0, 0.0), factor);
395 data.spacing.scale(Vector(0.0, 0.0), factor);
396 RS_DEBUG->print("RS_Insert::scale2: insertionPoint: %f/%f",
397 data.insertionPoint.x, data.insertionPoint.y);
401 void RS_Insert::mirror(Vector axisPoint1, Vector axisPoint2)
403 data.insertionPoint.mirror(axisPoint1, axisPoint2);
406 vec.setPolar(1.0, data.angle);
407 vec.mirror(Vector(0.0, 0.0), axisPoint2 - axisPoint1);
408 data.angle = vec.angle();
410 data.scaleFactor.y *= -1;
415 std::ostream & operator<<(std::ostream & os, const RS_Insert & i)
417 os << " Insert: " << i.getData() << std::endl;