Debian Codegraph @

src/Gui/Selection/SoFCSelectionContext.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * Copyright (c) 2018 Zheng Lei (realthunder) <[email protected]> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ****************************************************************************/
23
24
25 #include <boost/algorithm/string/predicate.hpp>
26 #include <Inventor/elements/SoCacheElement.h>
27 #include <Inventor/misc/SoState.h>
28
29
30 #include "SoFCSelectionContext.h"
31 #include "SoFCUnifiedSelection.h"
32
33
34 using namespace Gui;
35
36 /////////////////////////////////////////////////////////////////////////////
37
38 SoFCSelectionContext::~SoFCSelectionContext()
39 {
40 if (counter) {
41 *counter -= 1;
42 }
43 }
44
45 bool SoFCSelectionContext::checkGlobal(SoFCSelectionContextPtr ctx)
46 {
47 bool sel = false;
48 bool hl = false;
49 SoFCSelectionRoot::checkSelection(sel, selectionColor, hl, highlightColor);
50 if (sel) {
51 selectionIndex.insert(-1);
52 }
53 else if (ctx && hl) {
54 selectionColor = ctx->selectionColor;
55 selectionIndex = ctx->selectionIndex;
56 }
57 else {
58 selectionIndex.clear();
59 }
60 if (hl) {
61 highlightAll();
62 }
63 else if (ctx && sel) {
64 highlightIndex = ctx->highlightIndex;
65 highlightColor = ctx->highlightColor;
66 }
67 else {
68 removeHighlight();
69 }
70 return sel || hl;
71 }
72
73 bool SoFCSelectionContext::removeIndex(int index)
74 {
75 auto it = selectionIndex.find(index);
76 if (it != selectionIndex.end()) {
77 selectionIndex.erase(it);
78 return true;
79 }
80 return false;
81 }
82
83 int SoFCSelectionContext::merge(
84 int status,
85 SoFCSelectionContextBasePtr& output,
86 SoFCSelectionContextBasePtr input,
87 SoNode*
88 )
89 {
90 auto ctx = std::dynamic_pointer_cast<SoFCSelectionContext>(input);
91 if (!ctx) {
92 return status;
93 }
94
95 if (ctx->selectionIndex.empty()) {
96 output = ctx;
97 return -1;
98 }
99
100 auto ret = std::dynamic_pointer_cast<SoFCSelectionContext>(output);
101 if (!ret) {
102 output = ctx;
103 return 0;
104 }
105
106 if (ctx->isSelectAll()) {
107 return status;
108 }
109
110 if (ret->isSelectAll()) {
111 if (!status) {
112 output = ret->copy();
113 ret = std::dynamic_pointer_cast<SoFCSelectionContext>(ret);
114 assert(ret);
115 }
116 ret->selectionIndex = ctx->selectionIndex;
117 return status;
118 }
119
120 std::vector<int> remove;
121 for (auto idx : ret->selectionIndex) {
122 if (!ctx->selectionIndex.contains(idx)) {
123 remove.push_back(idx);
124 }
125 }
126
127 for (auto idx : remove) {
128 if (!status) {
129 status = 1;
130 output = ret->copy();
131 ret = std::dynamic_pointer_cast<SoFCSelectionContext>(ret);
132 assert(ret);
133 }
134 ret->selectionIndex.erase(idx);
135 if (ret->selectionIndex.empty()) {
136 return -1;
137 }
138 }
139 return status;
140 }
141
142 /////////////////////////////////////////////////////////////////////////////////////
143
144 bool SoFCSelectionContextEx::setColors(
145 const std::map<std::string, Base::Color>& colors,
146 const std::string& element
147 )
148 {
149 std::map<int, Base::Color> tmp;
150 auto it = colors.find("");
151 if (it != colors.end()) {
152 tmp[-1] = it->second;
153 }
154 for (auto it = colors.lower_bound(element); it != colors.end(); ++it) {
155 if (!boost::starts_with(it->first, element)) {
156 break;
157 }
158 if (it->first.size() == element.size()) {
159 tmp[-1] = it->second;
160 }
161 else {
162 int idx = std::atoi(it->first.c_str() + 4);
163 if (idx > 0) {
164 idx -= 1;
165 tmp[idx] = it->second;
166 }
167 }
168 }
169 if (tmp == this->colors) {
170 return false;
171 }
172 this->colors.swap(tmp);
173 return true;
174 }
175
176 uint32_t SoFCSelectionContextEx::packColor(const Base::Color& c, bool& hasTransparency)
177 {
178 // Convert Base::Color's alpha (opacity) to transparency for Coin3D.
179 float transparency = 1.0f - c.a;
180
181 // Apply any external transparency override (e.g., from picking).
182 float final_transparency = std::max(trans0, transparency);
183
184 if (final_transparency > 0.0f) {
185 hasTransparency = true;
186 }
187
188 return SbColor(c.r, c.g, c.b).getPackedValue(final_transparency);
189 }
190
191 bool SoFCSelectionContextEx::applyColor(int idx, std::vector<uint32_t>& packedColors, bool& hasTransparency)
192 {
193 if (colors.empty()) {
194 return false;
195 }
196 auto it = colors.find(idx);
197 if (it == colors.end()) {
198 if (colors.begin()->first >= 0) {
199 return false;
200 }
201 it = colors.begin();
202 }
203 packedColors.push_back(packColor(it->second, hasTransparency));
204 return true;
205 }
206
207 bool SoFCSelectionContextEx::isSingleColor(uint32_t& color, bool& hasTransparency)
208 {
209 if (!colors.empty() && colors.begin()->first < 0) {
210 color = packColor(colors.begin()->second, hasTransparency);
211 return colors.size() == 1;
212 }
213 return false;
214 }
215
216 int SoFCSelectionContextEx::merge(
217 int status,
218 SoFCSelectionContextBasePtr& output,
219 SoFCSelectionContextBasePtr input,
220 SoNode* node
221 )
222 {
223 auto ctx = std::dynamic_pointer_cast<SoFCSelectionContextEx>(input);
224 SoFCSelectionRoot* selectionNode = dynamic_cast<SoFCSelectionRoot*>(node);
225
226 if (!ctx) {
227 if (selectionNode && selectionNode->hasColorOverride()) {
228 if (!status) {
229 status = 2;
230 }
231 else if (status == 1) {
232 status = 3;
233 }
234 }
235 return status;
236 }
237
238 int status_copy = status;
239 if (status == 2) {
240 status_copy = 0;
241 }
242 else if (status == 3) {
243 status_copy = 1;
244 }
245 status_copy = SoFCSelectionContext::merge(status_copy, output, input, node);
246 if (status_copy < 0) {
247 return status_copy;
248 }
249
250 if (status > 1) {
251 // When status>1 it means there is color override before us, all
252 // subsequent color override will be bypassed
253 if (status_copy == 1) {
254 status = 3;
255 }
256 else {
257 status = 2;
258 }
259 return status;
260 }
261
262 status = status_copy;
263 auto ret = std::dynamic_pointer_cast<SoFCSelectionContextEx>(output);
264 assert(ret);
265 for (auto& v : ctx->colors) {
266 if (ret->colors.contains(v.first)) {
267 continue;
268 }
269 if (!status) {
270 status = 1;
271 output = ret->copy();
272 ret = std::dynamic_pointer_cast<SoFCSelectionContextEx>(output);
273 assert(ret);
274 }
275 ret->colors.insert(v);
276 }
277
278 if (selectionNode && selectionNode->hasColorOverride()) {
279 if (!status) {
280 status = 2;
281 }
282 else if (status == 1) {
283 status = 3;
284 }
285 }
286 return status;
287 }
288
289 ///////////////////////////////////////////////////////////////////////
290
291 SoFCSelectionCounter::SoFCSelectionCounter()
292 : counter(std::make_shared<int>(0))
293 {}
294
295 SoFCSelectionCounter::~SoFCSelectionCounter() = default;
296
297
298 bool SoFCSelectionCounter::checkRenderCache(SoState* state)
299 {
300 if (*counter || (hasSelection && Selection().hasSelection())
301 || (hasPreselection && Selection().hasPreselection())) {
302 if (SoFCSelectionRoot::getCacheMode() != SoSeparator::OFF) {
303 SoCacheElement::invalidate(state);
304 }
305 return false;
306 }
307 if (!Selection().hasPreselection()) {
308 hasPreselection = false;
309 }
310 if (!Selection().hasSelection()) {
311 hasSelection = false;
312 }
313 return true;
314 }
315
316 void SoFCSelectionCounter::checkAction(SoHighlightElementAction* hlaction)
317 {
318 if (hlaction->isHighlighted()) {
319 hasPreselection = true;
320 }
321 }
322
323 void SoFCSelectionCounter::checkAction(SoSelectionElementAction* selaction, SoFCSelectionContextPtr ctx)
324 {
325 switch (selaction->getType()) {
326 case SoSelectionElementAction::None:
327 return;
328 case SoSelectionElementAction::All:
329 case SoSelectionElementAction::Append:
330 hasSelection = true;
331 break;
332 default:
333 break;
334 }
335 if (selaction->isSecondary()) {
336 if (ctx && !ctx->counter) {
337 *counter += 1;
338 ctx->counter = counter;
339 }
340 }
341 }