| File: | librecad/src/lib/engine/document/selection/lc_selectedset.cpp |
| Warning: | line 74, column 21 Value stored to 'e' during its initialization is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * ******************************************************************************** |
| 3 | * This file is part of the LibreCAD project, a 2D CAD program |
| 4 | * |
| 5 | * Copyright (C) 2025 LibreCAD.org |
| 6 | * Copyright (C) 2025 sand1024 |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version 2 |
| 11 | * of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | * ******************************************************************************** |
| 22 | */ |
| 23 | |
| 24 | #include "lc_selectedset.h" |
| 25 | |
| 26 | #include "rs_entity.h" |
| 27 | #include "rs_layer.h" |
| 28 | #include <utility> |
| 29 | |
| 30 | LC_SelectedSet::LC_SelectedSet() = default; |
| 31 | |
| 32 | LC_SelectedSet::PreparedRemoval LC_SelectedSet::prepareRemoval(const std::unordered_set<RS_Entity*> &candidates) const { |
| 33 | PreparedRemoval prepared; |
| 34 | prepared.remaining.reserve(m_entitiesList.size()); |
| 35 | prepared.removed.reserve(m_entitiesList.size()); |
| 36 | for (auto *entity:std::as_const(m_entitiesList)) { |
| 37 | if (candidates.find(entity)!=candidates.end()) prepared.removed.push_back(entity); |
| 38 | else prepared.remaining.push_back(entity); |
| 39 | } |
| 40 | return prepared; |
| 41 | } |
| 42 | |
| 43 | bool LC_SelectedSet::commitRemoval(PreparedRemoval &&prepared) noexcept { |
| 44 | if (prepared.removed.empty()) return false; |
| 45 | m_entitiesList.swap(prepared.remaining); |
| 46 | for (auto *entity:std::as_const(prepared.removed)) |
| 47 | if (entity) entity->RS_Entity::setSelectionFlag(false); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Tells the listeners that still listen (see LC_SelectedSetListener::selectedSetDestroyed()): a dock |
| 53 | * attached to the selection keeps its drawing, which this is the last part of. |
| 54 | */ |
| 55 | LC_SelectedSet::~LC_SelectedSet() { |
| 56 | m_entitiesList.clear(); |
| 57 | m_listeners.drain([](LC_SelectedSetListener* listener) { |
| 58 | listener->selectedSetDestroyed(); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | void LC_SelectedSet::clear() { |
| 63 | for (const auto e: std::as_const(m_entitiesList)) { |
| 64 | e->setSelectionFlag(false); |
| 65 | } |
| 66 | m_entitiesList.clear(); |
| 67 | fireSelectionChanged(); |
| 68 | } |
| 69 | |
| 70 | #define DEBUG_UNIQUE_SELECTION |
| 71 | |
| 72 | void LC_SelectedSet::add(RS_Entity* entity) { |
| 73 | #ifdef DEBUG_UNIQUE_SELECTION |
| 74 | for (const auto e : std::as_const(m_entitiesList)) { |
Value stored to 'e' during its initialization is never read | |
| 75 | Q_ASSERT_X(e != entity, "LC_SelectedSet::add()", "Entity not unique in selection")static_cast<void>(false && (e != entity)); |
| 76 | } |
| 77 | #endif |
| 78 | m_entitiesList.push_back(entity); |
| 79 | fireSelectionChanged(); |
| 80 | } |
| 81 | |
| 82 | void LC_SelectedSet::remove(RS_Entity* entity) { |
| 83 | entity->setSelectionFlag(false); |
| 84 | m_entitiesList.removeOne(entity); |
| 85 | fireSelectionChanged(); |
| 86 | } |
| 87 | |
| 88 | void LC_SelectedSet::replaceBy(QList<RS_Entity*>& entities) { |
| 89 | for (const auto e: std::as_const(m_entitiesList)) { |
| 90 | e->setSelectionFlag(false); |
| 91 | } |
| 92 | m_entitiesList.clear(); |
| 93 | m_entitiesList.append(entities); |
| 94 | // need to update flag again, as the same entity might be in current selection list (so it will be un-selected) and |
| 95 | // be added in new list (so it should be selected). |
| 96 | for (const auto e: std::as_const(entities)) { |
| 97 | e->setSelectionFlag(true); |
| 98 | } |
| 99 | fireSelectionChanged(); |
| 100 | } |
| 101 | |
| 102 | void LC_SelectedSet::addListener(LC_SelectedSetListener* listener) { |
| 103 | // added only once; the listener removes itself when it is destroyed |
| 104 | m_listeners.add(listener); |
| 105 | } |
| 106 | |
| 107 | void LC_SelectedSet::removeListener(LC_SelectedSetListener* listener) { |
| 108 | m_listeners.remove(listener); |
| 109 | } |
| 110 | |
| 111 | void LC_SelectedSet::fireSelectionChanged() { |
| 112 | if (m_silentMode == 0) { |
| 113 | for (const auto l : std::as_const(m_listeners)) { |
| 114 | l->selectionChanged(); |
| 115 | } |
| 116 | } |
| 117 | else{ |
| 118 | m_changedInSilent = true; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void LC_SelectedSet::cleanup() { |
| 123 | if (m_entitiesList.empty()) { |
| 124 | return; |
| 125 | } |
| 126 | QList<RS_Entity*> validEntities; |
| 127 | for (const auto e: std::as_const(m_entitiesList)) { |
| 128 | bool valid = false; |
| 129 | if (e->isSet(RS2::FlagSelected) && e->isAlive()) { |
| 130 | const RS_Layer* layer = e->getLayerResolved(); |
| 131 | if (!layer->isLocked() && !layer->isFrozen()) { // also clear selection for locked and freezed layers |
| 132 | valid = true; |
| 133 | } |
| 134 | } |
| 135 | if (valid) { |
| 136 | validEntities.append(e); |
| 137 | } |
| 138 | else { |
| 139 | e->setSelectionFlag(false); |
| 140 | } |
| 141 | } |
| 142 | const auto validEntitiesCount = validEntities.count(); |
| 143 | const auto currentEntitiesCount = m_entitiesList.count(); |
| 144 | if (validEntitiesCount != currentEntitiesCount) { |
| 145 | m_entitiesList.clear(); |
| 146 | m_entitiesList.append(validEntities); |
| 147 | fireSelectionChanged(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | bool LC_SelectedSet::collectSelectedEntities(QList<RS_Entity*>& list) { |
| 152 | bool cleanupNeeded = false; |
| 153 | for (const auto e: std::as_const(m_entitiesList)) { |
| 154 | if (e != nullptr) { |
| 155 | if (e->isSet(RS2::FlagSelected) && e->isNotSet(RS2::FlagDeleted)) { |
| 156 | list.append(e); |
| 157 | } |
| 158 | else { |
| 159 | cleanupNeeded = true; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | if (cleanupNeeded) { |
| 164 | cleanup(); |
| 165 | } |
| 166 | return !list.isEmpty(); |
| 167 | } |
| 168 | |
| 169 | bool LC_SelectedSet::collectSelectedEntities(QList<RS_Entity*>& list, const QList<RS2::EntityType>&types) { |
| 170 | const bool specificTypesNeeded = !types.empty(); |
| 171 | bool cleanupNeeded = false; |
| 172 | for (const auto e: std::as_const(m_entitiesList)) { |
| 173 | if (e != nullptr) { |
| 174 | if (e->isSet(RS2::FlagSelected) && e->isNotSet(RS2::FlagDeleted)) { |
| 175 | if (specificTypesNeeded) { |
| 176 | RS2::EntityType type = e->rtti(); |
| 177 | if (types.count(type) > 0) { |
| 178 | list.append(e); |
| 179 | } |
| 180 | } |
| 181 | else { |
| 182 | list.append(e); |
| 183 | } |
| 184 | } |
| 185 | else { |
| 186 | cleanupNeeded = true; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | if (cleanupNeeded) { |
| 191 | cleanup(); |
| 192 | } |
| 193 | return !list.isEmpty(); |
| 194 | } |
| 195 | |
| 196 | bool LC_SelectedSet::hasSelection() { |
| 197 | bool cleanupNeeded = false; |
| 198 | bool hasSelection = false; |
| 199 | for (const auto e: std::as_const(m_entitiesList)) { |
| 200 | if (e != nullptr) { |
| 201 | if (e->isSet(RS2::FlagSelected) && e->isNotSet(RS2::FlagDeleted)) { |
| 202 | hasSelection = true; |
| 203 | break; |
| 204 | } |
| 205 | cleanupNeeded = true; |
| 206 | } |
| 207 | } |
| 208 | if (cleanupNeeded) { |
| 209 | cleanup(); |
| 210 | } |
| 211 | return hasSelection; |
| 212 | } |
| 213 | |
| 214 | void LC_SelectedSet::disableListeners() { |
| 215 | m_silentMode++; |
| 216 | } |
| 217 | |
| 218 | bool LC_SelectedSet::enableListeners() { |
| 219 | m_silentMode --; |
| 220 | if (m_silentMode == 0) { |
| 221 | if (m_changedInSilent) { |
| 222 | m_changedInSilent = false; // fixme - review what to return! |
| 223 | fireSelectionChanged(); |
| 224 | return true; |
| 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | return false; |
| 229 | } |