]> Shamusworld >> Repos - architektonas/blob - src/base/selection.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / base / selection.cpp
1 // selection.cpp
2 //
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
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/02/2010  Added this text. :-)
15 //
16
17 #include "selection.h"
18
19 #include "drawing.h"
20 #include "entity.h"
21 #include "graphicview.h"
22 #include "information.h"
23 #include "polyline.h"
24
25 /**
26  * Default constructor.
27  *
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, ...
31  */
32 Selection::Selection(EntityContainer & container, GraphicView * graphicView)
33 {
34         this->container = &container;
35         this->graphicView = graphicView;
36         graphic = container.getGraphic();
37 }
38
39 /**
40  * Selects or deselects the given entitiy.
41  */
42 void Selection::selectSingle(Entity * e)
43 {
44         if (e && (e->getLayer() == NULL || e->getLayer()->isLocked() == false))
45         {
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);
50
51                 e->toggleSelected();
52
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 !!!"
55                 if (graphicView)
56 //                      graphicView->drawEntity(e);
57                         graphicView->redraw();
58         }
59 }
60
61 /**
62  * Selects all entities on visible layers.
63  */
64 void Selection::selectAll(bool select)
65 {
66         if (graphicView)
67         {
68                 //graphicView->deleteEntity(container);
69         }
70
71         //container->setSelected(select);
72         for(Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
73         {
74         //for (uint i=0; i<container->count(); ++i) {
75                 //Entity* e = container->entityAt(i);
76
77                 if (e != NULL && e->isVisible())
78                         e->setSelected(select);
79         }
80
81         if (graphicView)
82                 graphicView->redraw();
83 }
84
85 void Selection::deselectAll()
86 {
87         selectAll(false);
88 }
89
90 /**
91  * Selects all entities on visible layers.
92  */
93 void Selection::invertSelection()
94 {
95         if (graphicView != NULL)
96         {
97                 //graphicView->deleteEntity(container);
98         }
99
100         for(Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
101         {
102         //for (uint i=0; i<container->count(); ++i) {
103                 //Entity* e = container->entityAt(i);
104
105                 if (e != NULL && e->isVisible())
106                         e->toggleSelected();
107         }
108
109         if (graphicView != NULL)
110                 //graphicView->drawEntity(container);
111                 graphicView->redraw();
112 }
113
114 /**
115  * Selects all entities that are completely in the given window.
116  *
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
120  */
121 void Selection::selectWindow(const Vector & v1, const Vector & v2, bool select, bool cross)
122 {
123         //if (graphicView!=NULL) {
124         //    graphicView->drawWindow(v1, v2, true);
125         //}
126
127         container->selectWindow(v1, v2, select, cross);
128
129         if (graphicView != NULL)
130                 //graphicView->drawWindow(v1, v2);
131                 graphicView->redraw();
132 }
133
134 void Selection::deselectWindow(const Vector & v1, const Vector & v2)
135 {
136         selectWindow(v1, v2, false);
137 }
138
139 /**
140  * Selects all entities that are intersected by the given line.
141  *
142  * @param v1 Startpoint of line.
143  * @param v2 Endpoint of line.
144  * @param select true: select, false: deselect
145  */
146 void Selection::selectIntersected(const Vector & v1, const Vector & v2, bool select)
147 {
148         Line line(NULL, LineData(v1, v2));
149         bool inters;
150
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);
155
156                 if (e!=NULL && e->isVisible())
157                 {
158                         inters = false;
159
160                         // select containers / groups:
161                         if (e->isContainer())
162                         {
163                                 EntityContainer * ec = (EntityContainer *)e;
164
165                                 for(Entity * e2=ec->firstEntity(RS2::ResolveAll); e2!=NULL;
166                                         e2=ec->nextEntity(RS2::ResolveAll))
167                                 {
168                                         VectorSolutions sol = Information::getIntersection(&line, e2, true);
169
170                                         if (sol.hasValid())
171                                                 inters = true;
172                                 }
173                         }
174                         else
175                         {
176                                 VectorSolutions sol = Information::getIntersection(&line, e, true);
177
178                                 if (sol.hasValid())
179                                         inters = true;
180                         }
181
182                         if (inters)
183                         {
184 #warning "!!! Old rendering path needs upgrading !!!"
185 #if 0
186                                 if (graphicView)
187                                         graphicView->deleteEntity(e);
188 #endif
189
190                                 e->setSelected(select);
191
192 #warning "!!! Old rendering path needs upgrading !!!"
193 #if 0
194                                 if (graphicView)
195                                         graphicView->drawEntity(e);
196 #endif
197                         }
198                 }
199         }
200 }
201
202 void Selection::deselectIntersected(const Vector & v1, const Vector & v2)
203 {
204         selectIntersected(v1, v2, false);
205 }
206
207 /**
208  * Selects all entities that are connected to the given entity.
209  *
210  * @param e The entity where the algorithm starts. Must be an atomic entity.
211  */
212 void Selection::selectContour(Entity * e)
213 {
214         if (!e || !e->isAtomic())
215                 return;
216
217         bool select = !e->isSelected();
218         AtomicEntity * ae = (AtomicEntity *)e;
219         Vector p1 = ae->getStartpoint();
220         Vector p2 = ae->getEndpoint();
221         bool found = false;
222
223 #warning "!!! Old rendering path needs upgrading !!!"
224 #if 0
225         // (de)select 1st entity:
226         if (graphicView)
227                 graphicView->deleteEntity(e);
228 #endif
229
230         e->setSelected(select);
231
232 #warning "!!! Old rendering path needs upgrading !!!"
233 #if 0
234         if (graphicView)
235                 graphicView->drawEntity(e);
236 #endif
237
238         do
239         {
240                 found = false;
241
242                 for(Entity * en=container->firstEntity(); en!=NULL;
243                         en=container->nextEntity())
244                 {
245                 //for (uint i=0; i<container->count(); ++i) {
246                         //Entity* en = container->entityAt(i);
247
248                         if (en!=NULL && en->isVisible() &&
249                                 en->isAtomic() && en->isSelected()!=select &&
250                                 (en->getLayer()==NULL || en->getLayer()->isLocked()==false))
251                         {
252                                 ae = (AtomicEntity *)en;
253                                 bool doit = false;
254
255                                 // startpoint connects to 1st point
256                                 if (ae->getStartpoint().distanceTo(p1)<1.0e-4) {
257                                         doit = true;
258                                         p1 = ae->getEndpoint();
259                                 }
260
261                                 // endpoint connects to 1st point
262                                 else if (ae->getEndpoint().distanceTo(p1)<1.0e-4) {
263                                         doit = true;
264                                         p1 = ae->getStartpoint();
265                                 }
266
267                                 // startpoint connects to 2nd point
268                                 else if (ae->getStartpoint().distanceTo(p2)<1.0e-4) {
269                                         doit = true;
270                                         p2 = ae->getEndpoint();
271                                 }
272
273                                 // endpoint connects to 1st point
274                                 else if (ae->getEndpoint().distanceTo(p2)<1.0e-4) {
275                                         doit = true;
276                                         p2 = ae->getStartpoint();
277                                 }
278
279                                 if (doit)
280                                 {
281 #warning "!!! Old rendering path needs upgrading !!!"
282 #if 0
283                                         if (graphicView)
284                                                 graphicView->deleteEntity(ae);
285 #endif
286
287                                         ae->setSelected(select);
288
289 #warning "!!! Old rendering path needs upgrading !!!"
290 #if 0
291                                         if (graphicView)
292                                                 graphicView->drawEntity(ae);
293 #endif
294
295                                         found = true;
296                                 }
297                         }
298                 }
299         } while(found);
300 }
301
302 /**
303  * Selects all entities on the given layer.
304  */
305 void Selection::selectLayer(Entity * e)
306 {
307         if (e == NULL)
308                 return;
309
310         bool select = !e->isSelected();
311
312         Layer * layer = e->getLayer(true);
313
314         if (layer == NULL)
315                 return;
316
317         QString layerName = layer->getName();
318         selectLayer(layerName, select);
319 }
320
321 /**
322  * Selects all entities on the given layer.
323  */
324 void Selection::selectLayer(const QString & layerName, bool select)
325 {
326         for(Entity * en=container->firstEntity(); en!=NULL; en=container->nextEntity())
327         {
328                 if (en != NULL && en->isVisible() && en->isSelected() != select
329                         && (en->getLayer() == NULL || en->getLayer()->isLocked() == false))
330                 {
331                         Layer * l = en->getLayer(true);
332
333                         if (l != NULL && l->getName() == layerName)
334                         {
335 #warning "!!! Old rendering path needs upgrading !!!"
336 #if 0
337                                 if (graphicView)
338                                         graphicView->deleteEntity(en);
339 #endif
340
341                                 en->setSelected(select);
342
343 #warning "!!! Old rendering path needs upgrading !!!"
344 #if 0
345                                 if (graphicView)
346                                         graphicView->drawEntity(en);
347 #endif
348                         }
349                 }
350         }
351 }
352
353 void Selection::deselectLayer(QString & layerName)
354 {
355         selectLayer(layerName, false);
356 }