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