]> Shamusworld >> Repos - architektonas/blob - src/base/rs_selection.cpp
Initial import
[architektonas] / src / base / rs_selection.cpp
1
2 #include "rs_selection.h"
3
4 #include "rs_information.h"
5 #include "rs_polyline.h"
6 #include "rs_entity.h"
7 #include "rs_graphic.h"
8
9 /**
10  * Default constructor.
11  *
12  * @param container The container to which we will add
13  *        entities. Usually that's an RS_Graphic entity but
14  *        it can also be a polyline, text, ...
15  */
16 RS_Selection::RS_Selection(RS_EntityContainer & container, RS_GraphicView * graphicView)
17 {
18         this->container = &container;
19         this->graphicView = graphicView;
20         graphic = container.getGraphic();
21 }
22
23 /**
24  * Selects or deselects the given entitiy.
25  */
26 void RS_Selection::selectSingle(RS_Entity * e)
27 {
28         if (e != NULL && (e->getLayer() == NULL || e->getLayer()->isLocked() == false))
29         {
30 // Same problem as below...
31 //[WAS]#warning "!!! This is causing a segfault in the draw code !!!"
32 //              if (graphicView != NULL)
33 //                      graphicView->deleteEntity(e);
34
35                 e->toggleSelected();
36
37 //Most likely because while the painter is valid, the QPainter is not...
38 //[WAS]#warning "!!! This is causing a segfault in the draw code !!!"
39                 if (graphicView != NULL)
40 //                      graphicView->drawEntity(e);
41                         graphicView->redraw();
42         }
43 }
44
45 /**
46  * Selects all entities on visible layers.
47  */
48 void RS_Selection::selectAll(bool select)
49 {
50         if (graphicView != NULL)
51         {
52                 //graphicView->deleteEntity(container);
53         }
54
55         //container->setSelected(select);
56         for(RS_Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
57         {
58         //for (uint i=0; i<container->count(); ++i) {
59                 //RS_Entity* e = container->entityAt(i);
60
61                 if (e != NULL && e->isVisible())
62                         e->setSelected(select);
63         }
64
65         if (graphicView!=NULL)
66                 //graphicView->drawEntity(container);
67                 graphicView->redraw();
68 }
69
70 /**
71  * Selects all entities on visible layers.
72  */
73 void RS_Selection::invertSelection()
74 {
75         if (graphicView != NULL)
76         {
77                 //graphicView->deleteEntity(container);
78         }
79
80         for(RS_Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
81         {
82         //for (uint i=0; i<container->count(); ++i) {
83                 //RS_Entity* e = container->entityAt(i);
84
85                 if (e != NULL && e->isVisible())
86                         e->toggleSelected();
87         }
88
89         if (graphicView != NULL)
90                 //graphicView->drawEntity(container);
91                 graphicView->redraw();
92 }
93
94 /**
95  * Selects all entities that are completely in the given window.
96  *
97  * @param v1 First corner of the window to select.
98  * @param v2 Second corner of the window to select.
99  * @param select true: select, false: deselect
100  */
101 void RS_Selection::selectWindow(const Vector & v1, const Vector & v2, bool select, bool cross)
102 {
103         //if (graphicView!=NULL) {
104         //    graphicView->drawWindow(v1, v2, true);
105         //}
106
107         container->selectWindow(v1, v2, select, cross);
108
109         if (graphicView != NULL)
110                 //graphicView->drawWindow(v1, v2);
111                 graphicView->redraw();
112 }
113
114 /**
115  * Selects all entities that are intersected by the given line.
116  *
117  * @param v1 Startpoint of line.
118  * @param v2 Endpoint of line.
119  * @param select true: select, false: deselect
120  */
121 void RS_Selection::selectIntersected(const Vector & v1, const Vector & v2, bool select)
122 {
123         RS_Line line(NULL, RS_LineData(v1, v2));
124         bool inters;
125
126         for (RS_Entity* e=container->firstEntity(); e!=NULL;
127                         e=container->nextEntity()) {
128         //for (uint i=0; i<container->count(); ++i) {
129                 //RS_Entity* e = container->entityAt(i);
130
131                 if (e!=NULL && e->isVisible()) {
132
133                         inters = false;
134
135                         // select containers / groups:
136                         if (e->isContainer()) {
137                                 RS_EntityContainer* ec = (RS_EntityContainer*)e;
138
139                                 for (RS_Entity* e2=ec->firstEntity(RS2::ResolveAll); e2!=NULL;
140                                                 e2=ec->nextEntity(RS2::ResolveAll)) {
141
142                                         VectorSolutions sol =
143                                                 RS_Information::getIntersection(&line, e2, true);
144
145                                         if (sol.hasValid()) {
146                                                 inters = true;
147                                         }
148                                 }
149                         } else {
150
151                                 VectorSolutions sol =
152                                         RS_Information::getIntersection(&line, e, true);
153
154                                 if (sol.hasValid()) {
155                                         inters = true;
156                                 }
157                         }
158
159                         if (inters) {
160                                 if (graphicView!=NULL) {
161                                         graphicView->deleteEntity(e);
162                                 }
163
164                                 e->setSelected(select);
165
166                                 if (graphicView!=NULL) {
167                                         graphicView->drawEntity(e);
168                                 }
169                         }
170                 }
171         }
172 }
173
174 /**
175  * Selects all entities that are connected to the given entity.
176  *
177  * @param e The entity where the algorithm starts. Must be an atomic entity.
178  */
179 void RS_Selection::selectContour(RS_Entity * e)
180 {
181         if (e == NULL || !e->isAtomic())
182                 return;
183
184         bool select = !e->isSelected();
185         RS_AtomicEntity* ae = (RS_AtomicEntity*)e;
186         Vector p1 = ae->getStartpoint();
187         Vector p2 = ae->getEndpoint();
188         bool found = false;
189
190         // (de)select 1st entity:
191         if (graphicView!=NULL) {
192                 graphicView->deleteEntity(e);
193         }
194         e->setSelected(select);
195         if (graphicView!=NULL) {
196                 graphicView->drawEntity(e);
197         }
198
199         do {
200                 found = false;
201
202                 for (RS_Entity* en=container->firstEntity(); en!=NULL;
203                                 en=container->nextEntity()) {
204                 //for (uint i=0; i<container->count(); ++i) {
205                         //RS_Entity* en = container->entityAt(i);
206
207                         if (en!=NULL && en->isVisible() &&
208                                 en->isAtomic() && en->isSelected()!=select &&
209                                 (en->getLayer()==NULL || en->getLayer()->isLocked()==false)) {
210
211                                 ae = (RS_AtomicEntity*)en;
212                                 bool doit = false;
213
214                                 // startpoint connects to 1st point
215                                 if (ae->getStartpoint().distanceTo(p1)<1.0e-4) {
216                                         doit = true;
217                                         p1 = ae->getEndpoint();
218                                 }
219
220                                 // endpoint connects to 1st point
221                                 else if (ae->getEndpoint().distanceTo(p1)<1.0e-4) {
222                                         doit = true;
223                                         p1 = ae->getStartpoint();
224                                 }
225
226                                 // startpoint connects to 2nd point
227                                 else if (ae->getStartpoint().distanceTo(p2)<1.0e-4) {
228                                         doit = true;
229                                         p2 = ae->getEndpoint();
230                                 }
231
232                                 // endpoint connects to 1st point
233                                 else if (ae->getEndpoint().distanceTo(p2)<1.0e-4) {
234                                         doit = true;
235                                         p2 = ae->getStartpoint();
236                                 }
237
238                                 if (doit) {
239                                         if (graphicView!=NULL) {
240                                                 graphicView->deleteEntity(ae);
241                                         }
242                                         ae->setSelected(select);
243                                         if (graphicView!=NULL) {
244                                                 graphicView->drawEntity(ae);
245                                         }
246                                         found = true;
247                                 }
248                         }
249                 }
250         } while(found);
251 }
252
253 /**
254  * Selects all entities on the given layer.
255  */
256 void RS_Selection::selectLayer(RS_Entity * e)
257 {
258         if (e == NULL)
259                 return;
260
261         bool select = !e->isSelected();
262
263         RS_Layer * layer = e->getLayer(true);
264
265         if (layer == NULL)
266                 return;
267
268         QString layerName = layer->getName();
269         selectLayer(layerName, select);
270 }
271
272 /**
273  * Selects all entities on the given layer.
274  */
275 void RS_Selection::selectLayer(const QString & layerName, bool select)
276 {
277         for(RS_Entity * en=container->firstEntity(); en!=NULL; en=container->nextEntity())
278         {
279                 if (en != NULL && en->isVisible() && en->isSelected() != select
280                         && (en->getLayer() == NULL || en->getLayer()->isLocked() == false))
281                 {
282                         RS_Layer * l = en->getLayer(true);
283
284                         if (l != NULL && l->getName() == layerName)
285                         {
286                                 if (graphicView != NULL)
287                                         graphicView->deleteEntity(en);
288
289                                 en->setSelected(select);
290
291                                 if (graphicView != NULL)
292                                         graphicView->drawEntity(en);
293                         }
294                 }
295         }
296 }