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 graphic = container.getGraphic();
40 * Selects or deselects the given entitiy.
42 void Selection::selectSingle(Entity * e)
44 if (e && (e->getLayer() == NULL || e->getLayer()->isLocked() == false))
46 // Same problem as below...
47 //[WAS]#warning "!!! This is causing a segfault in the draw code !!!"
48 // if (graphicView != NULL)
49 // graphicView->deleteEntity(e);
53 //Most likely because while the painter is valid, the QPainter is not...
54 //[WAS]#warning "!!! This is causing a segfault in the draw code !!!"
56 // graphicView->drawEntity(e);
57 graphicView->redraw();
62 * Selects all entities on visible layers.
64 void Selection::selectAll(bool select)
68 //graphicView->deleteEntity(container);
71 //container->setSelected(select);
72 for(Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
74 //for (uint i=0; i<container->count(); ++i) {
75 //Entity* e = container->entityAt(i);
77 if (e != NULL && e->isVisible())
78 e->setSelected(select);
82 graphicView->redraw();
85 void Selection::deselectAll()
91 * Selects all entities on visible layers.
93 void Selection::invertSelection()
95 if (graphicView != NULL)
97 //graphicView->deleteEntity(container);
100 for(Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
102 //for (uint i=0; i<container->count(); ++i) {
103 //Entity* e = container->entityAt(i);
105 if (e != NULL && e->isVisible())
109 if (graphicView != NULL)
110 //graphicView->drawEntity(container);
111 graphicView->redraw();
115 * Selects all entities that are completely in the given window.
117 * @param v1 First corner of the window to select.
118 * @param v2 Second corner of the window to select.
119 * @param select true: select, false: deselect
121 void Selection::selectWindow(const Vector & v1, const Vector & v2, bool select, bool cross)
123 //if (graphicView!=NULL) {
124 // graphicView->drawWindow(v1, v2, true);
127 container->selectWindow(v1, v2, select, cross);
129 if (graphicView != NULL)
130 //graphicView->drawWindow(v1, v2);
131 graphicView->redraw();
134 void Selection::deselectWindow(const Vector & v1, const Vector & v2)
136 selectWindow(v1, v2, false);
140 * Selects all entities that are intersected by the given line.
142 * @param v1 Startpoint of line.
143 * @param v2 Endpoint of line.
144 * @param select true: select, false: deselect
146 void Selection::selectIntersected(const Vector & v1, const Vector & v2, bool select)
148 Line line(NULL, LineData(v1, v2));
151 for (Entity* e=container->firstEntity(); e!=NULL;
152 e=container->nextEntity()) {
153 //for (uint i=0; i<container->count(); ++i) {
154 //Entity* e = container->entityAt(i);
156 if (e!=NULL && e->isVisible())
160 // select containers / groups:
161 if (e->isContainer())
163 EntityContainer * ec = (EntityContainer *)e;
165 for(Entity * e2=ec->firstEntity(RS2::ResolveAll); e2!=NULL;
166 e2=ec->nextEntity(RS2::ResolveAll))
168 VectorSolutions sol = Information::getIntersection(&line, e2, true);
176 VectorSolutions sol = Information::getIntersection(&line, e, true);
184 #warning "!!! Old rendering path needs upgrading !!!"
187 graphicView->deleteEntity(e);
190 e->setSelected(select);
192 #warning "!!! Old rendering path needs upgrading !!!"
195 graphicView->drawEntity(e);
202 void Selection::deselectIntersected(const Vector & v1, const Vector & v2)
204 selectIntersected(v1, v2, false);
208 * Selects all entities that are connected to the given entity.
210 * @param e The entity where the algorithm starts. Must be an atomic entity.
212 void Selection::selectContour(Entity * e)
214 if (!e || !e->isAtomic())
217 bool select = !e->isSelected();
218 AtomicEntity * ae = (AtomicEntity *)e;
219 Vector p1 = ae->getStartpoint();
220 Vector p2 = ae->getEndpoint();
223 #warning "!!! Old rendering path needs upgrading !!!"
225 // (de)select 1st entity:
227 graphicView->deleteEntity(e);
230 e->setSelected(select);
232 #warning "!!! Old rendering path needs upgrading !!!"
235 graphicView->drawEntity(e);
242 for(Entity * en=container->firstEntity(); en!=NULL;
243 en=container->nextEntity())
245 //for (uint i=0; i<container->count(); ++i) {
246 //Entity* en = container->entityAt(i);
248 if (en!=NULL && en->isVisible() &&
249 en->isAtomic() && en->isSelected()!=select &&
250 (en->getLayer()==NULL || en->getLayer()->isLocked()==false))
252 ae = (AtomicEntity *)en;
255 // startpoint connects to 1st point
256 if (ae->getStartpoint().distanceTo(p1)<1.0e-4) {
258 p1 = ae->getEndpoint();
261 // endpoint connects to 1st point
262 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) {
270 p2 = ae->getEndpoint();
273 // endpoint connects to 1st point
274 else if (ae->getEndpoint().distanceTo(p2)<1.0e-4) {
276 p2 = ae->getStartpoint();
281 #warning "!!! Old rendering path needs upgrading !!!"
284 graphicView->deleteEntity(ae);
287 ae->setSelected(select);
289 #warning "!!! Old rendering path needs upgrading !!!"
292 graphicView->drawEntity(ae);
303 * Selects all entities on the given layer.
305 void Selection::selectLayer(Entity * e)
310 bool select = !e->isSelected();
312 Layer * layer = e->getLayer(true);
317 QString layerName = layer->getName();
318 selectLayer(layerName, select);
322 * Selects all entities on the given layer.
324 void Selection::selectLayer(const QString & layerName, bool select)
326 for(Entity * en=container->firstEntity(); en!=NULL; en=container->nextEntity())
328 if (en != NULL && en->isVisible() && en->isSelected() != select
329 && (en->getLayer() == NULL || en->getLayer()->isLocked() == false))
331 Layer * l = en->getLayer(true);
333 if (l != NULL && l->getName() == layerName)
335 #warning "!!! Old rendering path needs upgrading !!!"
338 graphicView->deleteEntity(en);
341 en->setSelected(select);
343 #warning "!!! Old rendering path needs upgrading !!!"
346 graphicView->drawEntity(en);
353 void Selection::deselectLayer(QString & layerName)
355 selectLayer(layerName, false);