File size: 11,416 Bytes
a5ffdcd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | /****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#include "rs_selection.h"
#include "lc_containertraverser.h"
#include "lc_graphicviewport.h"
#include "qc_applicationwindow.h"
#include "qg_dialogfactory.h"
#include "rs_dialogfactory.h"
#include "rs_entitycontainer.h"
#include "rs_information.h"
#include "rs_insert.h"
#include "rs_layer.h"
#include "rs_line.h"
/**
* Default constructor.
*
* @param container The container to which we will add
* entities. Usually that's an RS_Graphic entity but
* it can also be a polyline, text, ...
*/
RS_Selection::RS_Selection(
RS_EntityContainer &container,
LC_GraphicViewport *graphicView):
m_container{&container}, m_graphic{container.getGraphic()}, m_graphicView{graphicView}{
}
/**
* Selects or deselects the given entity.
*/
void RS_Selection::selectSingle(RS_Entity *e){
if (e && (!(e->getLayer() && e->getLayer()->isLocked()))){
e->toggleSelected();
if (m_graphicView){
if (e->isSelected() && (e->rtti() == RS2::EntityInsert)){
const RS_Block *selectedBlock = dynamic_cast<RS_Insert *>(e)->getBlockForInsert();
if (selectedBlock != nullptr){
// Display the selected block as active in the block widget
QC_ApplicationWindow::getAppWindow()->showBlockActivated(selectedBlock);
// Display the selected block name
QG_DIALOGFACTORY->displayBlockName(selectedBlock->getName(), true);
}
} else {
QG_DIALOGFACTORY->displayBlockName("", false);
}
m_graphicView->notifyChanged();
}
}
}
/**
* Selects all entities on visible layers.
*/
void RS_Selection::selectAll(bool select){
if (m_graphicView){
for (auto e: *m_container) {
if (e && e->isVisible()){
e->setSelected(select);
// fixme - sand - selectAll by entity type - check whether it will not break plugin interface:
// NOTE:
// this is actually bad practice and development by side-effect.
// type to select for graphic view is set only in Doc_plugin_interface::getSelectByType
// and in general it's hardly that this flag is propertly used (as later
// RS_ActionSelectSingle with own check is invoked.
// So better just create separate function with explicit type of entity, if one will be really
// necessary.
/* if (graphicView->getTypeToSelect() == RS2::EntityType::EntityUnknown){
e->setSelected(select);
} else {
if (e->rtti() == graphicView->getTypeToSelect()){
e->setSelected(select);
}
}*/
}
}
m_graphicView->notifyChanged();
}
}
/**
* Selects all entities on visible layers.
*/
void RS_Selection::invertSelection(){
for (auto e: *m_container) {
if (e && e->isVisible()){
e->toggleSelected();
}
}
m_graphicView->notifyChanged();
}
/**
* Selects all entities that are completely in the given window.
*
* @param v1 First corner of the window to select.
* @param v2 Second corner of the window to select.
* @param select true: select, false: invertSelectionOperation
*/
void RS_Selection::selectWindow(
enum RS2::EntityType typeToSelect, const RS_Vector &v1, const RS_Vector &v2,
bool select, bool cross){
m_container->selectWindow(typeToSelect, v1, v2, select, cross);
m_graphicView->notifyChanged();
}
void RS_Selection::selectWindow(const QList<RS2::EntityType> &typesToSelect, const RS_Vector &v1, const RS_Vector &v2,
bool select, bool cross){
m_container->selectWindow(typesToSelect, v1, v2, select, cross);
m_graphicView->notifyChanged();
}
void RS_Selection::selectIntersected(RS_Entity* entity, bool select) {
if (entity->isAtomic()) {
selectIntersectedAtomic(entity, select);
}
else if (entity->isContainer()) {
selectIntersectedContainer(entity, select);
}
}
void RS_Selection::selectIntersectedContainer(RS_Entity* entity, bool select) {
auto* cont = dynamic_cast<RS_EntityContainer*>(entity);
auto containerEntities = lc::LC_ContainerTraverser{*cont, RS2::ResolveAll}.entities();
bool inters;
for (auto e : *m_container) { // fixme - iteration over ALL entities, limit area
if (e == nullptr || e == entity || !e->isVisible()) {
continue;
}
inters = false;
// select containers / groups:
if (e->isContainer()) {
auto* ec = static_cast<RS_EntityContainer*>(e);
if (entity->getParent() == e) { // case for segment of polyline
continue;
}
for (RS_Entity* e2 : lc::LC_ContainerTraverser{*ec, RS2::ResolveAll}.entities()) {
for (RS_Entity* e3 : containerEntities) {
RS_VectorSolutions sol = RS_Information::getIntersection(e3, e2, true);
if (sol.hasValid()) {
inters = true;
break;
}
}
}
}
else {
for (RS_Entity* e2 : containerEntities) {
RS_VectorSolutions sol = RS_Information::getIntersection(e2, e, true);
if (sol.hasValid()) {
inters = true;
break;
}
}
}
if (inters) {
e->setSelected(select);
}
}
m_graphicView->notifyChanged();
}
void RS_Selection::selectIntersectedAtomic(RS_Entity* entity, bool select) {
bool inters;
for (auto e: *m_container) { // fixme - iteration over ALL entities, limit area
if (e != nullptr && e->isVisible()){
inters = false;
// select containers / groups:
if (e->isContainer()){
auto *ec = static_cast<RS_EntityContainer*>(e);
if (entity->getParent() == e) { // case for segment of polyline
continue;
}
for(RS_Entity* e2: lc::LC_ContainerTraverser{*ec, RS2::ResolveAll}.entities()) {
RS_VectorSolutions sol = RS_Information::getIntersection(entity, e2, true);
if (sol.hasValid()){
inters = true;
}
}
} else {
RS_VectorSolutions sol = RS_Information::getIntersection(entity, e, true);
if (sol.hasValid()){
inters = true;
}
}
if (inters){
e->setSelected(select);
}
}
}
m_graphicView->notifyChanged();
}
/**
* Selects all entities that are intersected by the given line.
*
* @param v1 Startpoint of line.
* @param v2 Endpoint of line.
* @param select true: select, false: invertSelectionOperation
*/
void RS_Selection::selectIntersected(const RS_Vector &v1, const RS_Vector &v2, bool select){
RS_Line line{v1, v2};
selectIntersected(&line, select);
}
/**
* Selects all entities that are connected to the given entity.
*
* @param e The entity where the algorithm starts. Must be an atomic entity.
*/
void RS_Selection::selectContour(RS_Entity *e){
if (e == nullptr){
return;
}
if (!e->isAtomic()){
return;
}
bool select = !e->isSelected();
auto *ae = (RS_AtomicEntity *) e;
RS_Vector p1 = ae->getStartpoint();
RS_Vector p2 = ae->getEndpoint();
bool found = false;
// (de)select 1st entity:
e->setSelected(select);
do {// fixme - hm...iterating over all entities of drawing in cycle???? too nice for me...
found = false;
// fixme - iterating over all entities of drawing
for (auto en: *m_container) {
if (en && en->isVisible() &&
en->isAtomic() && en->isSelected() != select &&
(!(en->getLayer() && en->getLayer()->isLocked()))){
ae = (RS_AtomicEntity *) en;
bool doit = false;
// startpoint connects to 1st point
if (ae->getStartpoint().distanceTo(p1) < 1.0e-4){
doit = true;
p1 = ae->getEndpoint();
}
// endpoint connects to 1st point
else if (ae->getEndpoint().distanceTo(p1) < 1.0e-4){
doit = true;
p1 = ae->getStartpoint();
}
// startpoint connects to 2nd point
else if (ae->getStartpoint().distanceTo(p2) < 1.0e-4){
doit = true;
p2 = ae->getEndpoint();
}
// endpoint connects to 1st point
else if (ae->getEndpoint().distanceTo(p2) < 1.0e-4){
doit = true;
p2 = ae->getStartpoint();
}
if (doit){
ae->setSelected(select);
found = true;
}
}
}
} while (found);
m_graphicView->notifyChanged();
}
/**
* Selects all entities on the given layer.
*/
void RS_Selection::selectLayer(RS_Entity *e){
if (e == nullptr)
return;
bool select = !e->isSelected();
RS_Layer *layer = e->getLayer(true);
if (layer == nullptr) {
return;
}
QString layerName = layer->getName();
selectLayer(layerName, select);
}
/**
* Selects all entities on the given layer.
*/
void RS_Selection::selectLayer(const QString &layerName, bool select){
for (auto en: *m_container) {
// fixme - review and make more efficient... why check for locking upfront? Why just not use layer pointers but names?
if (en && en->isVisible() &&
en->isSelected() != select &&
(!(en->getLayer() && en->getLayer()->isLocked()))){
RS_Layer *l = en->getLayer(true);
if (l != nullptr && l->getName() == layerName){
en->setSelected(select);
}
}
}
m_graphicView->notifyChanged();
}
|