Debian Codegraph @

src/Gui/Selection/SoFCSelection.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2005 Jürgen Riegel <[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 <QString>
26 #include <Inventor/SoFullPath.h>
27 #include <Inventor/SoPickedPoint.h>
28 #include <Inventor/actions/SoGLRenderAction.h>
29 #include <Inventor/actions/SoHandleEventAction.h>
30 #include <Inventor/details/SoFaceDetail.h>
31 #include <Inventor/details/SoLineDetail.h>
32 #include <Inventor/elements/SoLazyElement.h>
33 #include <Inventor/elements/SoMaterialBindingElement.h>
34 #include <Inventor/elements/SoOverrideElement.h>
35 #include <Inventor/elements/SoWindowElement.h>
36 #include <Inventor/events/SoKeyboardEvent.h>
37 #include <Inventor/events/SoLocation2Event.h>
38 #include <Inventor/events/SoMouseButtonEvent.h>
39 #include <Inventor/misc/SoState.h>
40
41
42 #include <Base/UnitsApi.h>
43
44 #include "SoFCSelection.h"
45 #include "MainWindow.h"
46 #include "SoFCSelectionAction.h"
47 #include "SoFCUnifiedSelection.h"
48 #include "ViewParams.h"
49
50
51 using namespace Gui;
52
53 namespace Gui
54 {
55 void printPreselectionInfo(
56 const char* documentName,
57 const char* objectName,
58 const char* subElementName,
59 float x,
60 float y,
61 float z,
62 double precision
63 );
64 }
65
66 SoFullPath* Gui::SoFCSelection::currenthighlight = nullptr;
67
68
69 // *************************************************************************
70
71 SO_NODE_SOURCE(SoFCSelection)
72
73 /*!
74 Constructor.
75 */
76 SoFCSelection::SoFCSelection()
77 {
78 SO_NODE_CONSTRUCTOR(SoFCSelection);
79
80 SO_NODE_ADD_FIELD(colorHighlight, (SbColor(0.8f, 0.1f, 0.1f)));
81 SO_NODE_ADD_FIELD(colorSelection, (SbColor(0.1f, 0.8f, 0.1f)));
82 SO_NODE_ADD_FIELD(style, (EMISSIVE));
83 SO_NODE_ADD_FIELD(preselectionMode, (AUTO));
84 SO_NODE_ADD_FIELD(selectionMode, (SEL_ON));
85 SO_NODE_ADD_FIELD(selected, (NOTSELECTED));
86 SO_NODE_ADD_FIELD(documentName, (""));
87 SO_NODE_ADD_FIELD(objectName, (""));
88 SO_NODE_ADD_FIELD(subElementName, (""));
89 SO_NODE_ADD_FIELD(useNewSelection, (true));
90
91 SO_NODE_DEFINE_ENUM_VALUE(Styles, EMISSIVE);
92 SO_NODE_DEFINE_ENUM_VALUE(Styles, EMISSIVE_DIFFUSE);
93 SO_NODE_DEFINE_ENUM_VALUE(Styles, BOX);
94 SO_NODE_SET_SF_ENUM_TYPE(style, Styles);
95
96 SO_NODE_DEFINE_ENUM_VALUE(PreselectionModes, AUTO);
97 SO_NODE_DEFINE_ENUM_VALUE(PreselectionModes, ON);
98 SO_NODE_DEFINE_ENUM_VALUE(PreselectionModes, OFF);
99 SO_NODE_SET_SF_ENUM_TYPE(preselectionMode, PreselectionModes);
100
101 SO_NODE_DEFINE_ENUM_VALUE(SelectionModes, SEL_ON);
102 SO_NODE_DEFINE_ENUM_VALUE(SelectionModes, SEL_OFF);
103 SO_NODE_SET_SF_ENUM_TYPE(selectionMode, SelectionModes);
104
105 SO_NODE_DEFINE_ENUM_VALUE(Selected, NOTSELECTED);
106 SO_NODE_DEFINE_ENUM_VALUE(Selected, SELECTED);
107 SO_NODE_SET_SF_ENUM_TYPE(selected, Selected);
108
109 highlighted = false;
110 bShift = false;
111 bCtrl = false;
112
113 selected = NOTSELECTED;
114
115 useNewSelection = ViewParams::instance()->getUseNewSelection();
116 selContext = std::make_shared<SelContext>();
117 selContext2 = std::make_shared<SelContext>();
118 }
119
120 /*!
121 Destructor.
122 */
123 SoFCSelection::~SoFCSelection()
124 {
125 // If we're being deleted and we're the current highlight,
126 // NULL out that variable
127 if (currenthighlight
128 && (!currenthighlight->getTail()->isOfType(SoFCSelection::getClassTypeId()))) {
129 currenthighlight->unref();
130 currenthighlight = nullptr;
131 }
132 // delete THIS;
133 }
134
135 // doc from parent
136 void SoFCSelection::initClass()
137 {
138 SO_NODE_INIT_CLASS(SoFCSelection, SoGroup, "Group");
139 }
140
141 void SoFCSelection::finish()
142 {
143 atexit_cleanup();
144 }
145
146 /*!
147 Static method that can be used to turn off the current highlight.
148 */
149 void SoFCSelection::turnOffCurrentHighlight(SoGLRenderAction* action)
150 {
151 SoFCSelection::turnoffcurrent(action);
152 }
153
154 void SoFCSelection::doAction(SoAction* action)
155 {
156 if (useNewSelection.getValue() && action->getCurPathCode() != SoAction::OFF_PATH) {
157 if (action->getTypeId() == Gui::SoHighlightElementAction::getClassTypeId()) {
158 auto hlaction = static_cast<Gui::SoHighlightElementAction*>(action);
159 if (!hlaction->isHighlighted()) {
160 auto ctx = Gui::SoFCSelectionRoot::getActionContext(action, this, selContext, false);
161 if (ctx && ctx->isHighlighted()) {
162 ctx->highlightIndex = -1;
163 touch();
164 }
165 }
166 else {
167 auto ctx = Gui::SoFCSelectionRoot::getActionContext(action, this, selContext);
168 if (ctx) {
169 ctx->highlightColor = hlaction->getColor();
170 if (!ctx->isHighlighted()) {
171 ctx->highlightIndex = 0;
172 touch();
173 }
174 }
175 }
176 return;
177 }
178 else if (action->getTypeId() == Gui::SoSelectionElementAction::getClassTypeId()) {
179 auto selaction = static_cast<Gui::SoSelectionElementAction*>(action);
180 if (selaction->getType() == Gui::SoSelectionElementAction::All
181 || selaction->getType() == Gui::SoSelectionElementAction::Append) {
182 SelContextPtr ctx = Gui::SoFCSelectionRoot::getActionContext(action, this, selContext);
183 if (ctx) {
184 ctx->selectionColor = selaction->getColor();
185 if (!ctx->isSelectAll()) {
186 ctx->selectAll();
187 this->touch();
188 }
189 }
190 }
191 else if (selaction->getType() == Gui::SoSelectionElementAction::None
192 || selaction->getType() == Gui::SoSelectionElementAction::Remove) {
193 SelContextPtr ctx
194 = Gui::SoFCSelectionRoot::getActionContext(action, this, selContext, false);
195 if (ctx && ctx->isSelected()) {
196 ctx->selectionIndex.clear();
197 this->touch();
198 }
199 }
200 return;
201 }
202 }
203
204 if (action->getTypeId() == SoFCDocumentAction::getClassTypeId()) {
205 auto docaction = static_cast<SoFCDocumentAction*>(action);
206 this->documentName = docaction->documentName;
207 }
208
209 if (action->getTypeId() == SoFCDocumentObjectAction::getClassTypeId()) {
210 auto objaction = static_cast<SoFCDocumentObjectAction*>(action);
211 objaction->documentName = this->documentName.getValue();
212 objaction->objectName = this->objectName.getValue();
213 objaction->componentName = this->subElementName.getValue();
214 objaction->setHandled();
215 }
216
217 if (!useNewSelection.getValue()) {
218
219 if (action->getTypeId() == SoFCEnablePreselectionAction::getClassTypeId()) {
220 auto preaction = static_cast<SoFCEnablePreselectionAction*>(action);
221 if (preaction->enabled) {
222 this->preselectionMode = SoFCSelection::AUTO;
223 }
224 else {
225 this->preselectionMode = SoFCSelection::OFF;
226 }
227 }
228
229 if (action->getTypeId() == SoFCEnableSelectionAction::getClassTypeId()) {
230 auto selaction = static_cast<SoFCEnableSelectionAction*>(action);
231 if (selaction->enabled) {
232 this->selectionMode = SoFCSelection::SEL_ON;
233 }
234 else {
235 this->selectionMode = SoFCSelection::SEL_OFF;
236 if (selected.getValue() == SELECTED) {
237 this->selected = NOTSELECTED;
238 }
239 }
240 }
241
242 if (action->getTypeId() == SoFCSelectionColorAction::getClassTypeId()) {
243 auto colaction = static_cast<SoFCSelectionColorAction*>(action);
244 this->colorSelection = colaction->selectionColor;
245 }
246
247 if (action->getTypeId() == SoFCHighlightColorAction::getClassTypeId()) {
248 auto colaction = static_cast<SoFCHighlightColorAction*>(action);
249 this->colorHighlight = colaction->highlightColor;
250 }
251
252 if (selectionMode.getValue() == SEL_ON
253 && action->getTypeId() == SoFCSelectionAction::getClassTypeId()) {
254 auto selaction = static_cast<SoFCSelectionAction*>(action);
255
256 if (selaction->SelChange.Type == SelectionChanges::AddSelection
257 || selaction->SelChange.Type == SelectionChanges::RmvSelection) {
258 if (documentName.getValue() == selaction->SelChange.pDocName
259 && objectName.getValue() == selaction->SelChange.pObjectName
260 && (subElementName.getValue() == selaction->SelChange.pSubName
261 || *(selaction->SelChange.pSubName) == '\0')) {
262 if (selaction->SelChange.Type == SelectionChanges::AddSelection) {
263 if (selected.getValue() == NOTSELECTED) {
264 selected = SELECTED;
265 }
266 }
267 else {
268 if (selected.getValue() == SELECTED) {
269 selected = NOTSELECTED;
270 }
271 }
272 return;
273 }
274 }
275 else if (selaction->SelChange.Type == SelectionChanges::ClrSelection) {
276 if (documentName.getValue() == selaction->SelChange.pDocName
277 || strcmp(selaction->SelChange.pDocName, "") == 0) {
278 if (selected.getValue() == SELECTED) {
279 selected = NOTSELECTED;
280 }
281 }
282 }
283 else if (selaction->SelChange.Type == SelectionChanges::SetSelection) {
284 bool sel = Selection().isSelected(
285 documentName.getValue().getString(),
286 objectName.getValue().getString() /*,
287 subElementName.getValue().getString()*/
288 );
289 if (sel) {
290 if (selected.getValue() == NOTSELECTED) {
291 selected = SELECTED;
292 }
293 }
294 else {
295 if (selected.getValue() == SELECTED) {
296 selected = NOTSELECTED;
297 }
298 }
299 }
300 }
301 }
302
303 inherited::doAction(action);
304 }
305
306 int SoFCSelection::getPriority(const SoPickedPoint* p)
307 {
308 const SoDetail* detail = p->getDetail();
309 if (!detail) {
310 return 0;
311 }
312 if (detail->isOfType(SoFaceDetail::getClassTypeId())) {
313 return 1;
314 }
315 if (detail->isOfType(SoLineDetail::getClassTypeId())) {
316 return 2;
317 }
318 if (detail->isOfType(SoPointDetail::getClassTypeId())) {
319 return 3;
320 }
321 return 0;
322 }
323
324 const SoPickedPoint* SoFCSelection::getPickedPoint(SoHandleEventAction* action) const
325 {
326 // To identify the picking of lines in a concave area we have to
327 // get all intersection points. If we have two or more intersection
328 // points where the first is of a face and the second of a line with
329 // almost similar coordinates we use the second point, instead.
330 const SoPickedPointList& points = action->getPickedPointList();
331 if (points.getLength() == 0) {
332 return nullptr;
333 }
334 else if (points.getLength() == 1) {
335 return points[0];
336 }
337
338 const SoPickedPoint* picked = points[0];
339
340 int picked_prio = getPriority(picked);
341 const SbVec3f& picked_pt = picked->getPoint();
342
343
344 for (int i = 1; i < points.getLength(); i++) {
345 const SoPickedPoint* cur = points[i];
346 int cur_prio = getPriority(cur);
347 const SbVec3f& cur_pt = cur->getPoint();
348
349 if ((cur_prio > picked_prio) && picked_pt.equals(cur_pt, 0.01f)) {
350 picked = cur;
351 picked_prio = cur_prio;
352 }
353 }
354 return picked;
355 }
356
357 // doc from parent
358 void SoFCSelection::handleEvent(SoHandleEventAction* action)
359 {
360 if (useNewSelection.getValue()) {
361 inherited::handleEvent(action);
362 return;
363 }
364
365 static char buf[513];
366 auto mymode = static_cast<PreselectionModes>(this->preselectionMode.getValue());
367 const SoEvent* event = action->getEvent();
368
369 // mouse move events for preselection
370 if (event->isOfType(SoLocation2Event::getClassTypeId())) {
371 // NOTE: If preselection is off then we do not check for a picked point because otherwise
372 // this search may slow down extremely the system on really big data sets. In this case we
373 // just check for a picked point if the data set has been selected.
374 if (mymode == AUTO || mymode == ON) {
375 const SoPickedPoint* pp = this->getPickedPoint(action);
376 if (pp && pp->getPath()->containsPath(action->getCurPath())) {
377 if (!highlighted) {
378 if (Gui::Selection().setPreselect(
379 documentName.getValue().getString(),
380 objectName.getValue().getString(),
381 subElementName.getValue().getString(),
382 pp->getPoint()[0],
383 pp->getPoint()[1],
384 pp->getPoint()[2]
385 )) {
386 SoFCSelection::turnoffcurrent(action);
387 SoFCSelection::currenthighlight = static_cast<SoFullPath*>(
388 action->getCurPath()->copy()
389 );
390 SoFCSelection::currenthighlight->ref();
391 highlighted = true;
392 this->touch(); // force scene redraw
393 this->redrawHighlighted(action, true);
394 }
395 }
396
397 const auto& pt = pp->getPoint();
398
399 printPreselectionInfo(
400 documentName.getValue().getString(),
401 objectName.getValue().getString(),
402 subElementName.getValue().getString(),
403 pt[0],
404 pt[1],
405 pt[2],
406 1e-7
407 );
408 }
409 else { // picked point
410 if (highlighted) {
411 if (mymode == AUTO) {
412 SoFCSelection::turnoffcurrent(action);
413 }
414 // FIXME: I think we should set 'highlighted' to false whenever no point is
415 // picked else
416 highlighted = false;
417 Gui::Selection().rmvPreselect();
418 }
419 }
420 }
421 } // key press events
422 else if (event->isOfType(SoKeyboardEvent ::getClassTypeId())) {
423 auto const e = static_cast<const SoKeyboardEvent*>(event);
424 if (SoKeyboardEvent::isKeyPressEvent(e, SoKeyboardEvent::LEFT_SHIFT)
425 || SoKeyboardEvent::isKeyPressEvent(e, SoKeyboardEvent::RIGHT_SHIFT)) {
426 bShift = true;
427 }
428 if (SoKeyboardEvent::isKeyReleaseEvent(e, SoKeyboardEvent::LEFT_SHIFT)
429 || SoKeyboardEvent::isKeyReleaseEvent(e, SoKeyboardEvent::RIGHT_SHIFT)) {
430 bShift = false;
431 }
432 if (SoKeyboardEvent::isKeyPressEvent(e, SoKeyboardEvent::LEFT_CONTROL)
433 || SoKeyboardEvent::isKeyPressEvent(e, SoKeyboardEvent::RIGHT_CONTROL)) {
434 bCtrl = true;
435 }
436 if (SoKeyboardEvent::isKeyReleaseEvent(e, SoKeyboardEvent::LEFT_CONTROL)
437 || SoKeyboardEvent::isKeyReleaseEvent(e, SoKeyboardEvent::RIGHT_CONTROL)) {
438 bCtrl = false;
439 }
440 } // mouse press events for (de)selection
441 else if (event->isOfType(SoMouseButtonEvent::getClassTypeId())) {
442 auto const e = static_cast<const SoMouseButtonEvent*>(event);
443 if (SoMouseButtonEvent::isButtonReleaseEvent(e, SoMouseButtonEvent::BUTTON1)) {
444 // FIXME: Shouldn't we remove the preselection for newly selected objects?
445 // Otherwise the tree signals that an object is preselected even though it is
446 // hidden. (Werner)
447 const SoPickedPoint* pp = this->getPickedPoint(action);
448 if (pp && pp->getPath()->containsPath(action->getCurPath())) {
449 const auto& pt = pp->getPoint();
450 if (bCtrl) {
451 if (Gui::Selection().isSelected(
452 documentName.getValue().getString(),
453 objectName.getValue().getString(),
454 subElementName.getValue().getString()
455 )) {
456 Gui::Selection().rmvSelection(
457 documentName.getValue().getString(),
458 objectName.getValue().getString(),
459 subElementName.getValue().getString()
460 );
461 }
462 else {
463 Gui::Selection().addSelection(
464 documentName.getValue().getString(),
465 objectName.getValue().getString(),
466 subElementName.getValue().getString(),
467 pt[0],
468 pt[1],
469 pt[2]
470 );
471
472 if (mymode == OFF) {
473 snprintf(
474 buf,
475 512,
476 "Selected: %s.%s.%s (%g, %g, %g)",
477 documentName.getValue().getString(),
478 objectName.getValue().getString(),
479 subElementName.getValue().getString(),
480 fabs(pt[0]) > 1e-7 ? pt[0] : 0.0,
481 fabs(pt[1]) > 1e-7 ? pt[1] : 0.0,
482 fabs(pt[2]) > 1e-7 ? pt[2] : 0.0
483 );
484
485 getMainWindow()->showMessage(QString::fromLatin1(buf));
486 }
487 }
488 }
489 else { // Ctrl
490 if (!Gui::Selection().isSelected(
491 documentName.getValue().getString(),
492 objectName.getValue().getString(),
493 subElementName.getValue().getString()
494 )) {
495 Gui::Selection().clearSelection(documentName.getValue().getString());
496 Gui::Selection().addSelection(
497 documentName.getValue().getString(),
498 objectName.getValue().getString(),
499 subElementName.getValue().getString(),
500 pt[0],
501 pt[1],
502 pt[2]
503 );
504 }
505 else {
506 Gui::Selection().clearSelection(documentName.getValue().getString());
507 Gui::Selection().addSelection(
508 documentName.getValue().getString(),
509 objectName.getValue().getString(),
510 nullptr,
511 pt[0],
512 pt[1],
513 pt[2]
514 );
515 }
516
517 if (mymode == OFF) {
518 snprintf(
519 buf,
520 512,
521 "Selected: %s.%s.%s (%g, %g, %g)",
522 documentName.getValue().getString(),
523 objectName.getValue().getString(),
524 subElementName.getValue().getString(),
525 fabs(pt[0]) > 1e-7 ? pt[0] : 0.0,
526 fabs(pt[1]) > 1e-7 ? pt[1] : 0.0,
527 fabs(pt[2]) > 1e-7 ? pt[2] : 0.0
528 );
529
530 getMainWindow()->showMessage(QString::fromLatin1(buf));
531 }
532 }
533
534 action->setHandled();
535 } // picked point
536 } // mouse release
537 }
538
539 inherited::handleEvent(action);
540 }
541
542 // doc from parent
543 void SoFCSelection::GLRenderBelowPath(SoGLRenderAction* action)
544 {
545 SoState* state = action->getState();
546 SelContextPtr ctx = Gui::SoFCSelectionRoot::getRenderContext<SelContext>(this, selContext);
547 if (selContext2->checkGlobal(ctx)) {
548 ctx = selContext2;
549 }
550 if (!useNewSelection.getValue() && selContext == ctx) {
551 ctx->selectionColor = this->colorSelection.getValue();
552 ctx->highlightColor = this->colorHighlight.getValue();
553 if (this->selected.getValue() == SELECTED) {
554 ctx->selectAll();
555 }
556 else {
557 ctx->selectionIndex.clear();
558 }
559 ctx->highlightIndex = this->highlighted ? 0 : -1;
560 }
561
562 // check if preselection is active
563 if (this->setOverride(action, ctx)) {
564 inherited::GLRenderBelowPath(action);
565 state->pop();
566 }
567 else {
568 inherited::GLRenderBelowPath(action);
569 }
570 }
571
572 void SoFCSelection::GLRender(SoGLRenderAction* action)
573 {
574 SoState* state = action->getState();
575 SelContextPtr ctx = Gui::SoFCSelectionRoot::getRenderContext<SelContext>(this, selContext);
576 if (selContext2->checkGlobal(ctx)) {
577 ctx = selContext2;
578 }
579 if (!useNewSelection.getValue() && selContext == ctx) {
580 ctx->selectionColor = this->colorSelection.getValue();
581 ctx->highlightColor = this->colorHighlight.getValue();
582 if (this->selected.getValue() == SELECTED) {
583 ctx->selectAll();
584 }
585 else {
586 ctx->selectionIndex.clear();
587 }
588 ctx->highlightIndex = this->highlighted ? 0 : -1;
589 }
590
591 // check if preselection is active
592 if (this->setOverride(action, ctx)) {
593 inherited::GLRender(action);
594 state->pop();
595 }
596 else {
597 inherited::GLRender(action);
598 }
599 }
600
601 // doc from parent
602 void SoFCSelection::GLRenderInPath(SoGLRenderAction* action)
603 {
604 SelContextPtr ctx = Gui::SoFCSelectionRoot::getRenderContext<SelContext>(this, selContext);
605 if (selContext2->checkGlobal(ctx)) {
606 ctx = selContext2;
607 }
608 if (!useNewSelection.getValue() && selContext == ctx) {
609 ctx->selectionColor = this->colorSelection.getValue();
610 ctx->highlightColor = this->colorHighlight.getValue();
611 if (this->selected.getValue() == SELECTED) {
612 ctx->selectAll();
613 }
614 else {
615 ctx->selectionIndex.clear();
616 }
617 ctx->highlightIndex = this->highlighted ? 0 : -1;
618 }
619 // check if preselection is active
620 SoState* state = action->getState();
621 if (this->setOverride(action, ctx)) {
622 inherited::GLRenderInPath(action);
623 state->pop();
624 }
625 else {
626 inherited::GLRenderInPath(action);
627 }
628 }
629
630 SbBool SoFCSelection::preRender(SoGLRenderAction* action, GLint& oldDepthFunc)
631 //
632 ////////////////////////////////////////////////////////////////////////
633 {
634 // If not performing locate highlighting, just return.
635 if (preselectionMode.getValue() == OFF) {
636 return false;
637 }
638
639 SoState* state = action->getState();
640
641 // ??? prevent caching at this level - for some reason the
642 // ??? SoWindowElement::copyMatchInfo() method get called, which should
643 // ??? never be called. We are not caching this node correctly yet....
644 // SoCacheElement::invalidate(state);
645
646 SbBool drawHighlighted
647 = (preselectionMode.getValue() == ON || isHighlighted(action)
648 || selected.getValue() == SELECTED);
649
650 if (drawHighlighted) {
651 // prevent diffuse & emissive color from leaking out...
652 state->push();
653 SbColor col;
654 if (selected.getValue() == SELECTED) {
655 col = colorSelection.getValue();
656 }
657 else {
658 col = colorHighlight.getValue();
659 }
660
661 // Emissive Color
662 SoLazyElement::setEmissive(state, &col);
663 SoOverrideElement::setEmissiveColorOverride(state, this, true);
664
665 // Diffuse Color
666 if (style.getValue() == EMISSIVE_DIFFUSE) {
667 SoLazyElement::setDiffuse(state, this, 1, &col, &colorpacker);
668 SoOverrideElement::setDiffuseColorOverride(state, this, true);
669 }
670 }
671
672 // Draw on top of other things at same z-buffer depth if:
673 // [a] we're highlighted
674 // [b] this is the highlighting pass. This occurs when changing from
675 // non-hilit to lit OR VICE VERSA.
676 // Otherwise, leave it alone...
677 if (drawHighlighted || highlighted) {
678 glGetIntegerv(GL_DEPTH_FUNC, &oldDepthFunc);
679 if (oldDepthFunc != GL_LEQUAL) {
680 glDepthFunc(GL_LEQUAL);
681 }
682 }
683
684 return drawHighlighted;
685 }
686
687 /*!
688 Empty method in Coin. Can be used by subclasses to be told
689 when status change.
690 */
691 void SoFCSelection::redrawHighlighted(SoAction* action, SbBool doHighlight)
692 {
693 Q_UNUSED(action);
694 Q_UNUSED(doHighlight);
695 }
696
697 SbBool SoFCSelection::readInstance(SoInput* in, unsigned short flags)
698 {
699 // Note: The read in document name can be false, so the caller must ensure pointing to the
700 // correct document
701 SbBool ret = inherited::readInstance(in, flags);
702 return ret;
703 }
704 //
705 // update override state before rendering
706 //
707 bool SoFCSelection::setOverride(SoGLRenderAction* action, SelContextPtr ctx)
708 {
709 auto mymode = static_cast<PreselectionModes>(this->preselectionMode.getValue());
710 bool preselected = ctx && ctx->isHighlighted() && (useNewSelection.getValue() || mymode == AUTO);
711 if (!preselected && mymode != ON && (!ctx || !ctx->isSelected())) {
712 return false;
713 }
714
715 // uniqueId is returned by SoNode::getNodeId(). It is used to notify change
716 // and for render cache update. In order to update cache on selection state
717 // change, We manually change the id here by using a combined hash of the
718 // original id and context pointer.
719 auto oldId = this->uniqueId;
720 this->uniqueId ^= std::hash<void*>()(ctx.get()) + 0x9e3779b9 + (oldId << 6) + (oldId >> 2);
721
722 auto mystyle = static_cast<Styles>(this->style.getValue());
723
724 if (mystyle == SoFCSelection::BOX) {
725 if (ctx) {
726 SoFCSelectionRoot::renderBBox(
727 action,
728 this,
729 preselected ? ctx->highlightColor : ctx->selectionColor
730 );
731 }
732 this->uniqueId = oldId;
733 return false;
734 }
735
736 SoState* state = action->getState();
737 state->push();
738
739 SoMaterialBindingElement::set(state, SoMaterialBindingElement::OVERALL);
740 SoOverrideElement::setMaterialBindingOverride(state, this, true);
741
742 if (!preselected && ctx) {
743 SoLazyElement::setEmissive(state, &ctx->selectionColor);
744 }
745 else if (ctx) {
746 SoLazyElement::setEmissive(state, &ctx->highlightColor);
747 }
748 SoOverrideElement::setEmissiveColorOverride(state, this, true);
749
750 if (SoLazyElement::getLightModel(state) == SoLazyElement::BASE_COLOR
751 || mystyle == SoFCSelection::EMISSIVE_DIFFUSE) {
752 if (!preselected && ctx) {
753 SoLazyElement::setDiffuse(state, this, 1, &ctx->selectionColor, &colorpacker);
754 }
755 else if (ctx) {
756 SoLazyElement::setDiffuse(state, this, 1, &ctx->highlightColor, &colorpacker);
757 }
758 SoOverrideElement::setDiffuseColorOverride(state, this, true);
759 }
760
761 this->uniqueId = oldId;
762 return true;
763 }
764
765 // private convenience method
766 void SoFCSelection::turnoffcurrent(SoAction* action)
767 {
768 if (SoFCSelection::currenthighlight && SoFCSelection::currenthighlight->getLength()) {
769 SoNode* tail = SoFCSelection::currenthighlight->getTail();
770 if (tail->isOfType(SoFCSelection::getClassTypeId())) {
771 static_cast<SoFCSelection*>(tail)->highlighted = false;
772 static_cast<SoFCSelection*>(tail)->touch(); // force scene redraw
773 if (action) {
774 static_cast<SoFCSelection*>(tail)->redrawHighlighted(action, false);
775 }
776 }
777 }
778 if (SoFCSelection::currenthighlight) {
779 SoFCSelection::currenthighlight->unref();
780 SoFCSelection::currenthighlight = nullptr;
781 }
782 }
783
784 SbBool SoFCSelection::isHighlighted(SoAction* action)
785 //
786 ////////////////////////////////////////////////////////////////////////
787 {
788 auto actionPath = static_cast<const SoFullPath*>(action->getCurPath());
789 return (
790 currenthighlight && currenthighlight->getTail() == actionPath->getTail() && // nested SoHL!
791 *currenthighlight == *actionPath
792 );
793 }
794
795 void SoFCSelection::applySettings()
796 {
797 // TODO Some view providers got copy of this code: make them use this (2015-09-03, Fat-Zer)
798 // Note: SoFCUnifiedSelection got the same code, keep in sync or think about a way to share it
799 float transparency;
800 ParameterGrp::handle hGrp = Gui::WindowParameter::getDefaultParameter()->GetGroup("View");
801 bool enablePre = hGrp->GetBool("EnablePreselection", true);
802 bool enableSel = hGrp->GetBool("EnableSelection", true);
803 if (!enablePre) {
804 this->preselectionMode = Gui::SoFCSelection::OFF;
805 }
806 else {
807 // Search for a user defined value with the current color as default
808 SbColor highlightColor = this->colorHighlight.getValue();
809 auto highlight = (unsigned long)(highlightColor.getPackedValue());
810 highlight = hGrp->GetUnsigned("HighlightColor", highlight);
811 highlightColor.setPackedValue((uint32_t)highlight, transparency);
812 this->colorHighlight.setValue(highlightColor);
813 }
814 if (!enableSel) {
815 this->selectionMode = Gui::SoFCSelection::SEL_OFF;
816 }
817 else {
818 // Do the same with the selection color
819 SbColor selectionColor = this->colorSelection.getValue();
820 auto selection = (unsigned long)(selectionColor.getPackedValue());
821 selection = hGrp->GetUnsigned("SelectionColor", selection);
822 selectionColor.setPackedValue((uint32_t)selection, transparency);
823 this->colorSelection.setValue(selectionColor);
824 }
825 }