]> Shamusworld >> Repos - architektonas/blob - src/base/rs_selection.cpp
Fixed thumbnail rendering in LibraryWidget and DXF detection.
[architektonas] / src / base / rs_selection.cpp
1 // rs_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 "rs_selection.h"
18
19 #include "drawing.h"
20 #include "rs_entity.h"
21 #include "graphicview.h"
22 #include "rs_information.h"
23 #include "rs_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 RS_Selection::RS_Selection(RS_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 RS_Selection::selectSingle(RS_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 != NULL)
56 //                      graphicView->drawEntity(e);
57                         graphicView->redraw();
58         }
59 }
60
61 /**
62  * Selects all entities on visible layers.
63  */
64 void RS_Selection::selectAll(bool select)
65 {
66         if (graphicView != NULL)
67         {
68                 //graphicView->deleteEntity(container);
69         }
70
71         //container->setSelected(select);
72         for(RS_Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
73         {
74         //for (uint i=0; i<container->count(); ++i) {
75                 //RS_Entity* e = container->entityAt(i);
76
77                 if (e != NULL && e->isVisible())
78                         e->setSelected(select);
79         }
80
81         if (graphicView!=NULL)
82                 //graphicView->drawEntity(container);
83                 graphicView->redraw();
84 }
85
86 void RS_Selection::deselectAll()
87 {
88         selectAll(false);
89 }
90
91 /**
92  * Selects all entities on visible layers.
93  */
94 void RS_Selection::invertSelection()
95 {
96         if (graphicView != NULL)
97         {
98                 //graphicView->deleteEntity(container);
99         }
100
101         for(RS_Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
102         {
103         //for (uint i=0; i<container->count(); ++i) {
104                 //RS_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 RS_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 RS_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 RS_Selection::selectIntersected(const Vector & v1, const Vector & v2, bool select)
148 {
149         RS_Line line(NULL, RS_LineData(v1, v2));
150         bool inters;
151
152         for (RS_Entity* e=container->firstEntity(); e!=NULL;
153                         e=container->nextEntity()) {
154         //for (uint i=0; i<container->count(); ++i) {
155                 //RS_Entity* e = container->entityAt(i);
156
157                 if (e!=NULL && e->isVisible())
158                 {
159                         inters = false;
160
161                         // select containers / groups:
162                         if (e->isContainer())
163                         {
164                                 RS_EntityContainer * ec = (RS_EntityContainer *)e;
165
166                                 for(RS_Entity * e2=ec->firstEntity(RS2::ResolveAll); e2!=NULL;
167                                         e2=ec->nextEntity(RS2::ResolveAll))
168                                 {
169                                         VectorSolutions sol = RS_Information::getIntersection(&line, e2, true);
170
171                                         if (sol.hasValid())
172                                                 inters = true;
173                                 }
174                         }
175                         else
176                         {
177                                 VectorSolutions sol = RS_Information::getIntersection(&line, e, true);
178
179                                 if (sol.hasValid())
180                                         inters = true;
181                         }
182
183                         if (inters)
184                         {
185 #warning "!!! Old rendering path needs upgrading !!!"
186 #if 0
187                                 if (graphicView)
188                                         graphicView->deleteEntity(e);
189 #endif
190
191                                 e->setSelected(select);
192
193 #warning "!!! Old rendering path needs upgrading !!!"
194 #if 0
195                                 if (graphicView)
196                                         graphicView->drawEntity(e);
197 #endif
198                         }
199                 }
200         }
201 }
202
203 void RS_Selection::deselectIntersected(const Vector & v1, const Vector & v2)
204 {
205         selectIntersected(v1, v2, false);
206 }
207
208 /**
209  * Selects all entities that are connected to the given entity.
210  *
211  * @param e The entity where the algorithm starts. Must be an atomic entity.
212  */
213 void RS_Selection::selectContour(RS_Entity * e)
214 {
215         if (!e || !e->isAtomic())
216                 return;
217
218         bool select = !e->isSelected();
219         RS_AtomicEntity * ae = (RS_AtomicEntity *)e;
220         Vector p1 = ae->getStartpoint();
221         Vector p2 = ae->getEndpoint();
222         bool found = false;
223
224 #warning "!!! Old rendering path needs upgrading !!!"
225 #if 0
226         // (de)select 1st entity:
227         if (graphicView)
228                 graphicView->deleteEntity(e);
229 #endif
230
231         e->setSelected(select);
232
233 #warning "!!! Old rendering path needs upgrading !!!"
234 #if 0
235         if (graphicView)
236                 graphicView->drawEntity(e);
237 #endif
238
239         do
240         {
241                 found = false;
242
243                 for(RS_Entity * en=container->firstEntity(); en!=NULL;
244                         en=container->nextEntity())
245                 {
246                 //for (uint i=0; i<container->count(); ++i) {
247                         //RS_Entity* en = container->entityAt(i);
248
249                         if (en!=NULL && en->isVisible() &&
250                                 en->isAtomic() && en->isSelected()!=select &&
251                                 (en->getLayer()==NULL || en->getLayer()->isLocked()==false))
252                         {
253                                 ae = (RS_AtomicEntity *)en;
254                                 bool doit = false;
255
256                                 // startpoint connects to 1st point
257                                 if (ae->getStartpoint().distanceTo(p1)<1.0e-4) {
258                                         doit = true;
259                                         p1 = ae->getEndpoint();
260                                 }
261
262                                 // endpoint connects to 1st point
263                                 else if (ae->getEndpoint().distanceTo(p1)<1.0e-4) {
264                                         doit = true;
265                                         p1 = ae->getStartpoint();
266                                 }
267
268                                 // startpoint connects to 2nd point
269                                 else if (ae->getStartpoint().distanceTo(p2)<1.0e-4) {
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                                         doit = true;
277                                         p2 = ae->getStartpoint();
278                                 }
279
280                                 if (doit)
281                                 {
282 #warning "!!! Old rendering path needs upgrading !!!"
283 #if 0
284                                         if (graphicView)
285                                                 graphicView->deleteEntity(ae);
286 #endif
287
288                                         ae->setSelected(select);
289
290 #warning "!!! Old rendering path needs upgrading !!!"
291 #if 0
292                                         if (graphicView)
293                                                 graphicView->drawEntity(ae);
294 #endif
295
296                                         found = true;
297                                 }
298                         }
299                 }
300         } while(found);
301 }
302
303 /**
304  * Selects all entities on the given layer.
305  */
306 void RS_Selection::selectLayer(RS_Entity * e)
307 {
308         if (e == NULL)
309                 return;
310
311         bool select = !e->isSelected();
312
313         RS_Layer * layer = e->getLayer(true);
314
315         if (layer == NULL)
316                 return;
317
318         QString layerName = layer->getName();
319         selectLayer(layerName, select);
320 }
321
322 /**
323  * Selects all entities on the given layer.
324  */
325 void RS_Selection::selectLayer(const QString & layerName, bool select)
326 {
327         for(RS_Entity * en=container->firstEntity(); en!=NULL; en=container->nextEntity())
328         {
329                 if (en != NULL && en->isVisible() && en->isSelected() != select
330                         && (en->getLayer() == NULL || en->getLayer()->isLocked() == false))
331                 {
332                         RS_Layer * l = en->getLayer(true);
333
334                         if (l != NULL && l->getName() == layerName)
335                         {
336 #warning "!!! Old rendering path needs upgrading !!!"
337 #if 0
338                                 if (graphicView)
339                                         graphicView->deleteEntity(en);
340 #endif
341
342                                 en->setSelected(select);
343
344 #warning "!!! Old rendering path needs upgrading !!!"
345 #if 0
346                                 if (graphicView)
347                                         graphicView->drawEntity(en);
348 #endif
349                         }
350                 }
351         }
352 }
353
354 void RS_Selection::deselectLayer(QString & layerName)
355 {
356         selectLayer(layerName, false);
357 }