2 #include "rs_selection.h"
4 #include "rs_information.h"
5 #include "rs_polyline.h"
7 #include "rs_graphic.h"
10 * Default constructor.
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, ...
16 RS_Selection::RS_Selection(RS_EntityContainer & container, RS_GraphicView * graphicView)
18 this->container = &container;
19 this->graphicView = graphicView;
20 graphic = container.getGraphic();
24 * Selects or deselects the given entitiy.
26 void RS_Selection::selectSingle(RS_Entity * e)
28 if (e != NULL && (e->getLayer() == NULL || e->getLayer()->isLocked() == false))
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);
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();
46 * Selects all entities on visible layers.
48 void RS_Selection::selectAll(bool select)
50 if (graphicView != NULL)
52 //graphicView->deleteEntity(container);
55 //container->setSelected(select);
56 for(RS_Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
58 //for (uint i=0; i<container->count(); ++i) {
59 //RS_Entity* e = container->entityAt(i);
61 if (e != NULL && e->isVisible())
62 e->setSelected(select);
65 if (graphicView!=NULL)
66 //graphicView->drawEntity(container);
67 graphicView->redraw();
71 * Selects all entities on visible layers.
73 void RS_Selection::invertSelection()
75 if (graphicView != NULL)
77 //graphicView->deleteEntity(container);
80 for(RS_Entity * e=container->firstEntity(); e!=NULL; e=container->nextEntity())
82 //for (uint i=0; i<container->count(); ++i) {
83 //RS_Entity* e = container->entityAt(i);
85 if (e != NULL && e->isVisible())
89 if (graphicView != NULL)
90 //graphicView->drawEntity(container);
91 graphicView->redraw();
95 * Selects all entities that are completely in the given window.
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
101 void RS_Selection::selectWindow(const Vector & v1, const Vector & v2, bool select, bool cross)
103 //if (graphicView!=NULL) {
104 // graphicView->drawWindow(v1, v2, true);
107 container->selectWindow(v1, v2, select, cross);
109 if (graphicView != NULL)
110 //graphicView->drawWindow(v1, v2);
111 graphicView->redraw();
115 * Selects all entities that are intersected by the given line.
117 * @param v1 Startpoint of line.
118 * @param v2 Endpoint of line.
119 * @param select true: select, false: deselect
121 void RS_Selection::selectIntersected(const Vector & v1, const Vector & v2, bool select)
123 RS_Line line(NULL, RS_LineData(v1, v2));
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);
131 if (e!=NULL && e->isVisible()) {
135 // select containers / groups:
136 if (e->isContainer()) {
137 RS_EntityContainer* ec = (RS_EntityContainer*)e;
139 for (RS_Entity* e2=ec->firstEntity(RS2::ResolveAll); e2!=NULL;
140 e2=ec->nextEntity(RS2::ResolveAll)) {
142 VectorSolutions sol =
143 RS_Information::getIntersection(&line, e2, true);
145 if (sol.hasValid()) {
151 VectorSolutions sol =
152 RS_Information::getIntersection(&line, e, true);
154 if (sol.hasValid()) {
160 if (graphicView!=NULL) {
161 graphicView->deleteEntity(e);
164 e->setSelected(select);
166 if (graphicView!=NULL) {
167 graphicView->drawEntity(e);
175 * Selects all entities that are connected to the given entity.
177 * @param e The entity where the algorithm starts. Must be an atomic entity.
179 void RS_Selection::selectContour(RS_Entity * e)
181 if (e == NULL || !e->isAtomic())
184 bool select = !e->isSelected();
185 RS_AtomicEntity* ae = (RS_AtomicEntity*)e;
186 Vector p1 = ae->getStartpoint();
187 Vector p2 = ae->getEndpoint();
190 // (de)select 1st entity:
191 if (graphicView!=NULL) {
192 graphicView->deleteEntity(e);
194 e->setSelected(select);
195 if (graphicView!=NULL) {
196 graphicView->drawEntity(e);
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);
207 if (en!=NULL && en->isVisible() &&
208 en->isAtomic() && en->isSelected()!=select &&
209 (en->getLayer()==NULL || en->getLayer()->isLocked()==false)) {
211 ae = (RS_AtomicEntity*)en;
214 // startpoint connects to 1st point
215 if (ae->getStartpoint().distanceTo(p1)<1.0e-4) {
217 p1 = ae->getEndpoint();
220 // endpoint connects to 1st point
221 else if (ae->getEndpoint().distanceTo(p1)<1.0e-4) {
223 p1 = ae->getStartpoint();
226 // startpoint connects to 2nd point
227 else if (ae->getStartpoint().distanceTo(p2)<1.0e-4) {
229 p2 = ae->getEndpoint();
232 // endpoint connects to 1st point
233 else if (ae->getEndpoint().distanceTo(p2)<1.0e-4) {
235 p2 = ae->getStartpoint();
239 if (graphicView!=NULL) {
240 graphicView->deleteEntity(ae);
242 ae->setSelected(select);
243 if (graphicView!=NULL) {
244 graphicView->drawEntity(ae);
254 * Selects all entities on the given layer.
256 void RS_Selection::selectLayer(RS_Entity * e)
261 bool select = !e->isSelected();
263 RS_Layer * layer = e->getLayer(true);
268 QString layerName = layer->getName();
269 selectLayer(layerName, select);
273 * Selects all entities on the given layer.
275 void RS_Selection::selectLayer(const QString & layerName, bool select)
277 for(RS_Entity * en=container->firstEntity(); en!=NULL; en=container->nextEntity())
279 if (en != NULL && en->isVisible() && en->isSelected() != select
280 && (en->getLayer() == NULL || en->getLayer()->isLocked() == false))
282 RS_Layer * l = en->getLayer(true);
284 if (l != NULL && l->getName() == layerName)
286 if (graphicView != NULL)
287 graphicView->deleteEntity(en);
289 en->setSelected(select);
291 if (graphicView != NULL)
292 graphicView->drawEntity(en);