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/02/2010 Added this text. :-)
17 #include "selection.h"
21 #include "graphicview.h"
22 #include "information.h"
26 * Default constructor.
28 * @param container The container to which we will add
29 * entities. Usually that's an Drawing entity but
30 * it can also be a polyline, text, ...
32 Selection::Selection(EntityContainer & container, GraphicView * graphicView)
34 this->container = &container;
35 this->graphicView = graphicView;
36 #warning "!!! Need to rename graphic to drawing !!!"
37 graphic = container.GetDrawing();
41 * Selects or deselects the given entitiy.
43 void Selection::selectSingle(Entity * e)
45 if (e && (e->getLayer() == NULL || e->getLayer()->isLocked() == false))
47 // Same problem as below...
48 //[WAS]#warning "!!! This is causing a segfault in the draw code !!!"
49 // if (graphicView != NULL)
50 // graphicView->deleteEntity(e);
54 //Most likely because while the painter is valid, the QPainter is not...
55 //[WAS]#warning "!!! This is causing a segfault in the draw code !!!"
57 // graphicView->drawEntity(e);
58 graphicView->redraw();
63 * Selects all entities on visible layers.
65 void Selection::selectAll(bool select)
69 //graphicView->deleteEntity(container);
72 //container->setSelected(select);
73 for(Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
75 //for (uint i=0; i<container->count(); ++i) {
76 //Entity* e = container->entityAt(i);
78 if (e != NULL && e->isVisible())
79 e->setSelected(select);
83 graphicView->redraw();
86 void Selection::deselectAll()
92 * Selects all entities on visible layers.
94 void Selection::invertSelection()
96 if (graphicView != NULL)
98 //graphicView->deleteEntity(container);
101 for(Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
103 //for (uint i=0; i<container->count(); ++i) {
104 //Entity* e = container->entityAt(i);
106 if (e != NULL && e->isVisible())
110 if (graphicView != NULL)
111 //graphicView->drawEntity(container);
112 graphicView->redraw();
116 * Selects all entities that are completely in the given window.
118 * @param v1 First corner of the window to select.
119 * @param v2 Second corner of the window to select.
120 * @param select true: select, false: deselect
122 void Selection::selectWindow(const Vector & v1, const Vector & v2, bool select, bool cross)
124 //if (graphicView!=NULL) {
125 // graphicView->drawWindow(v1, v2, true);
128 container->selectWindow(v1, v2, select, cross);
130 if (graphicView != NULL)
131 //graphicView->drawWindow(v1, v2);
132 graphicView->redraw();
135 void Selection::deselectWindow(const Vector & v1, const Vector & v2)
137 selectWindow(v1, v2, false);
141 * Selects all entities that are intersected by the given line.
143 * @param v1 Startpoint of line.
144 * @param v2 Endpoint of line.
145 * @param select true: select, false: deselect
147 void Selection::selectIntersected(const Vector & v1, const Vector & v2, bool select)
149 Line line(NULL, LineData(v1, v2));
152 for(Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
154 if (e && e->isVisible())
158 // select containers / groups:
159 if (e->isContainer())
161 EntityContainer * ec = (EntityContainer *)e;
163 for(Entity * e2=ec->firstEntity(RS2::ResolveAll); e2!=NULL;
164 e2=ec->nextEntity(RS2::ResolveAll))
166 VectorSolutions sol = Information::getIntersection(&line, e2, true);
174 VectorSolutions sol = Information::getIntersection(&line, e, true);
182 #warning "!!! Old rendering path needs upgrading !!!"
185 graphicView->deleteEntity(e);
188 e->setSelected(select);
190 #warning "!!! Old rendering path needs upgrading !!!"
193 graphicView->drawEntity(e);
200 void Selection::deselectIntersected(const Vector & v1, const Vector & v2)
202 selectIntersected(v1, v2, false);
206 * Selects all entities that are connected to the given entity.
208 * @param e The entity where the algorithm starts. Must be an atomic entity.
210 void Selection::selectContour(Entity * e)
212 if (!e || !e->isAtomic())
215 bool select = !e->isSelected();
216 AtomicEntity * ae = (AtomicEntity *)e;
217 Vector p1 = ae->getStartpoint();
218 Vector p2 = ae->getEndpoint();
221 #warning "!!! Old rendering path needs upgrading !!!"
223 // (de)select 1st entity:
225 graphicView->deleteEntity(e);
228 e->setSelected(select);
230 #warning "!!! Old rendering path needs upgrading !!!"
233 graphicView->drawEntity(e);
240 for(Entity * en=container->firstEntity(); en!=NULL;
241 en=container->nextEntity())
243 //for (uint i=0; i<container->count(); ++i) {
244 //Entity* en = container->entityAt(i);
246 if (en && en->isVisible() && en->isAtomic()
247 && en->isSelected() != select && (en->getLayer() == NULL
248 || en->getLayer()->isLocked() == false))
250 ae = (AtomicEntity *)en;
253 // startpoint connects to 1st point
254 if (ae->getStartpoint().distanceTo(p1) < 1.0e-4)
257 p1 = ae->getEndpoint();
260 // endpoint connects to 1st point
261 else if (ae->getEndpoint().distanceTo(p1) < 1.0e-4)
264 p1 = ae->getStartpoint();
267 // startpoint connects to 2nd point
268 else if (ae->getStartpoint().distanceTo(p2) < 1.0e-4)
271 p2 = ae->getEndpoint();
274 // endpoint connects to 1st point
275 else if (ae->getEndpoint().distanceTo(p2) < 1.0e-4)
278 p2 = ae->getStartpoint();
283 #warning "!!! Old rendering path needs upgrading !!!"
286 graphicView->deleteEntity(ae);
289 ae->setSelected(select);
291 #warning "!!! Old rendering path needs upgrading !!!"
294 graphicView->drawEntity(ae);
306 * Selects all entities on the given layer.
308 void Selection::selectLayer(Entity * e)
313 bool select = !e->isSelected();
315 Layer * layer = e->getLayer(true);
320 QString layerName = layer->getName();
321 selectLayer(layerName, select);
325 * Selects all entities on the given layer.
327 void Selection::selectLayer(const QString & layerName, bool select)
329 for(Entity * en=container->firstEntity(); en!=NULL; en=container->nextEntity())
331 if (en != NULL && en->isVisible() && en->isSelected() != select
332 && (en->getLayer() == NULL || en->getLayer()->isLocked() == false))
334 Layer * l = en->getLayer(true);
336 if (l != NULL && l->getName() == layerName)
338 #warning "!!! Old rendering path needs upgrading !!!"
341 graphicView->deleteEntity(en);
344 en->setSelected(select);
346 #warning "!!! Old rendering path needs upgrading !!!"
349 graphicView->drawEntity(en);
356 void Selection::deselectLayer(QString & layerName)
358 selectLayer(layerName, false);