1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XGUI_SelectionActivate.h"
22 #include "ModelAPI_Object.h"
24 #include "ModuleBase_IModule.h"
25 #include "ModuleBase_IViewer.h"
26 #include "ModuleBase_ModelWidget.h"
27 #include "ModuleBase_Preferences.h"
29 #include "XGUI_ActiveControlMgr.h"
30 #include "XGUI_ActiveControlSelector.h"
31 #include "XGUI_Displayer.h"
32 #include "XGUI_FacesPanel.h"
33 #include "XGUI_FacesPanelSelector.h"
34 #include "XGUI_SelectionMgr.h"
35 #include "XGUI_Tools.h"
36 #include "XGUI_Workshop.h"
38 #include <SUIT_ResourceMgr.h>
40 #include <AIS_InteractiveContext.hxx>
41 #include <AIS_Shape.hxx>
42 #include <AIS_Trihedron.hxx>
44 #include <SelectMgr_SelectionManager.hxx>
46 //#define DEBUG_ACTIVATE_OBJECTS
47 //#define DEBUG_DEACTIVATE
48 //#define DEBUG_ACTIVATE_AIS
49 //#define DEBUG_DEACTIVATE_AIS
52 #include <inspector/VInspectorAPI_CallBack.hxx>
55 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
57 //**************************************************************
58 XGUI_SelectionActivate::XGUI_SelectionActivate(ModuleBase_IWorkshop* theWorkshop)
59 : ModuleBase_ISelectionActivate(theWorkshop), myIsTrihedronActive(true)
63 //**************************************************************
64 XGUI_SelectionActivate::SelectionPlace XGUI_SelectionActivate::activeSelectionPlace() const
66 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
67 XGUI_ActiveControlSelector* anActiveSelector = aWorkshop->activeControlMgr()->activeSelector();
68 if (!anActiveSelector)
71 if (anActiveSelector->getType() == XGUI_FacesPanelSelector::Type())
77 //**************************************************************
78 void XGUI_SelectionActivate::updateSelectionModes()
81 switch (activeSelectionPlace()) {
83 myWorkshop->module()->activeSelectionModes(aModes);
86 ModuleBase_ModelWidget* anActiveWidget = myWorkshop->module()->activeWidget();
88 getSelectionModes(anActiveWidget, aModes);
90 myWorkshop->module()->activeSelectionModes(aModes); //using module modes
94 XGUI_Tools::workshop(myWorkshop)->facesPanel()->selectionModes(aModes);
95 myWorkshop->module()->moduleSelectionModes(-1/*all modes*/, aModes);
100 activateObjects(aModes, getDisplayer()->displayedObjects(), true);
103 //**************************************************************
104 void XGUI_SelectionActivate::updateSelectionFilters()
106 SelectMgr_ListOfFilter aSelectionFilters;
107 switch (activeSelectionPlace()) {
109 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
110 myWorkshop->module()->moduleSelectionFilters(aModuleSelectionFilters, aSelectionFilters);
113 case PropertyPanel: {
114 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
116 ModuleBase_ModelWidget* anActiveWidget = myWorkshop->module()->activeWidget();
118 anActiveWidget->selectionFilters(aModuleSelectionFilters, aSelectionFilters);
119 myWorkshop->module()->moduleSelectionFilters(aModuleSelectionFilters, aSelectionFilters);
123 XGUI_Tools::workshop(myWorkshop)->facesPanel()->selectionFilters(aSelectionFilters);
124 //QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
125 //myWorkshop->module()->moduleSelectionFilters(aModuleSelectionFilters, aSelectionFilters);
130 activateSelectionFilters(aSelectionFilters);
133 //**************************************************************
134 void XGUI_SelectionActivate::activateSelectionFilters
135 (const SelectMgr_ListOfFilter& theSelectionFilters)
137 XGUI_Displayer* aDisplayer = getDisplayer();
138 aDisplayer->deactivateSelectionFilters(false);
140 SelectMgr_ListIteratorOfListOfFilter aIt(theSelectionFilters);
141 for (; aIt.More(); aIt.Next()) {
142 Handle(SelectMgr_Filter) aFilter = aIt.Value();
143 if (aFilter.IsNull())
145 aDisplayer->addSelectionFilter(aFilter);
149 //**************************************************************
150 void XGUI_SelectionActivate::getSelectionModes(ModuleBase_ModelWidget* theWidget,
156 int aModuleSelectionModes = -1;
157 theWidget->selectionModes(aModuleSelectionModes, theModes);
158 myWorkshop->module()->moduleSelectionModes(aModuleSelectionModes, theModes);
161 //**************************************************************
162 QIntList XGUI_SelectionActivate::activeSelectionModes() const
165 foreach (int aMode, myActiveSelectionModes) {
166 // aMode < 9 is a Shape Enum values
167 aModes << ((aMode < 9)? AIS_Shape::SelectionType(aMode) : aMode);
172 //**************************************************************
173 bool XGUI_SelectionActivate::isActive(ObjectPtr theObject) const
175 Handle(AIS_InteractiveContext) aContext = AISContext();
176 if (aContext.IsNull() || !getDisplayer()->isVisible(theObject))
179 AISObjectPtr anObj = getDisplayedAISObject(theObject);
180 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
182 TColStd_ListOfInteger aModes;
183 aContext->ActivatedModes(anAIS, aModes);
185 return aModes.Extent() > 0;
188 //**************************************************************
189 void XGUI_SelectionActivate::activateObjects(const QIntList& theModes,
190 const QObjectPtrList& theObjList, const bool theUpdateViewer)
192 setSelectionModes(theModes);
194 Handle(AIS_InteractiveContext) aContext = AISContext();
195 // Open local context if there is no one
196 if (aContext.IsNull())
199 //aContext->UseDisplayedObjects();
200 //myUseExternalObjects = true;
202 Handle(AIS_InteractiveObject) anAISIO;
203 AIS_ListOfInteractive aPrsList;
204 AIS_ListOfInteractive aPrsListToBeDeactivated;
205 //if (aObjList.isEmpty())
208 foreach(ObjectPtr anObject, theObjList) {
209 AISObjectPtr anAISObject = getDisplayedAISObject(anObject);
210 if (!anAISObject.get())
213 Handle(AIS_InteractiveObject) aPrs = anAISObject->impl<Handle(AIS_InteractiveObject)>();
214 if (myWorkshop->module()->canActivateSelection(anObject))
215 aPrsList.Append(aPrs);
217 aPrsListToBeDeactivated.Append(aPrs);
221 // Add trihedron because it has to partisipate in selection
222 Handle(AIS_InteractiveObject) aTrihedron;
223 if (isTrihedronActive()) {
224 aTrihedron = getTrihedron();
225 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
226 aPrsList.Append(aTrihedron);
228 if (aPrsList.Extent() == 0 && aPrsListToBeDeactivated.Extent() == 0)
231 AIS_ListIteratorOfListOfInteractive aLIt;
233 bool isActivationChanged = false;
235 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()) {
236 anAISIO = aLIt.Value();
237 if (activate(anAISIO, false)) {
239 isActivationChanged = true;
244 for(aLIt.Initialize(aPrsListToBeDeactivated); aLIt.More(); aLIt.Next()) {
245 anAISIO = aLIt.Value();
246 deactivateAIS(anAISIO);
248 isActivationChanged = true;
253 #ifdef DEBUG_ACTIVATE_OBJECTS
254 //**************************************************************
255 QString getModeInfo(const int theMode)
257 QString anInfo = "Undefined";
259 case 0: anInfo = "SHAPE(0)"; break;
260 case 1: anInfo = "VERTEX(1)"; break;
261 case 2: anInfo = "EDGE(2)"; break;
262 case 3: anInfo = "WIRE(3)"; break;
263 case 4: anInfo = "FACE(4)"; break;
264 case 5: anInfo = "SHELL(5)"; break;
265 case 6: anInfo = "SOLID(6)"; break;
266 case 7: anInfo = "COMPSOLID(7)"; break;
267 case 8: anInfo = "COMPOUND(8)"; break;
268 case 100: anInfo = "Sel_Mode_First(100)"; break; //SketcherPrs_Tools
269 case 101: anInfo = "Sel_Constraint(101)"; break;
270 case 102: anInfo = "Sel_Dimension_All(102)"; break;
271 case 103: anInfo = "Sel_Dimension_Line(103)"; break;
272 case 104: anInfo = "Sel_Dimension_Text(104)"; break;
278 //**************************************************************
279 QString getModesInfo(const QIntList& theModes)
281 QStringList aModesInfo;
282 for (int i = 0, aSize = theModes.size(); i < aSize; i++)
283 aModesInfo.append(getModeInfo(theModes[i]));
284 return QString("[%1] = %2").arg(aModesInfo.size()).arg(aModesInfo.join(", "));
288 //**************************************************************
289 void XGUI_SelectionActivate::setSelectionModes(const QIntList& theModes)
291 // Convert shape types to selection types
293 foreach(int aType, theModes) {
294 aModes.append(getSelectionMode(aType));
297 #ifdef DEBUG_ACTIVATE_OBJECTS
299 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
300 for (; anIt != aLast; ++anIt) {
301 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
303 QString anInfoStr = anInfo.join(", ");
305 qDebug(QString("activateObjects: new modes%1, active modes%2, objects[%3] = %4").
306 arg(getModesInfo(aModes)).
307 arg(getModesInfo(myActiveSelectionModes)).
308 arg(theObjList.size()).
310 toStdString().c_str());
312 // In order to avoid doblications of selection modes
314 foreach (int aMode, aModes) {
315 if (!aNewModes.contains(aMode))
316 aNewModes.append(aMode);
318 myActiveSelectionModes = aNewModes;
321 //**************************************************************
322 void XGUI_SelectionActivate::activateOnDisplay(const Handle(AIS_InteractiveObject)& theIO,
323 const bool theUpdateViewer)
325 if (myActiveSelectionModes.size() == 0)
326 activateAIS(theIO, 0, theUpdateViewer);
328 foreach(int aMode, myActiveSelectionModes) {
329 activateAIS(theIO, aMode, theUpdateViewer);
334 //**************************************************************
335 void XGUI_SelectionActivate::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
336 const int theMode, const bool theUpdateViewer) const
338 Handle(AIS_InteractiveContext) aContext = AISContext();
339 if (!theIO.IsNull() && theIO == getTrihedron()) {
340 if (theMode != AIS_Shape::SelectionType(TopAbs_EDGE) &&
341 theMode != AIS_Shape::SelectionType(TopAbs_VERTEX))
344 if (!aContext.IsNull()) {
345 if (myWorkshop->module()) {
346 // the code is obsolete, used in additional check before activate, it was removed
347 //int aMode = (theMode > 8)? theMode : AIS_Shape::SelectionType(theMode);
348 aContext->Activate(theIO, theMode, false);
350 if (getDisplayer()->getCallBack()) getDisplayer()->getCallBack()->Activate(theIO, theMode);
354 aContext->Activate(theIO, theMode, false);
356 if (getDisplayer()->getCallBack()) getDisplayer()->getCallBack()->Activate(theIO, theMode);
359 // the fix from VPA for more suitable selection of sketcher lines
360 if (theIO->Width() > 1) {
361 double aPrecision = theIO->Width() + 2;
362 if (theMode == getSelectionMode(TopAbs_VERTEX))
363 aPrecision = ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer",
364 "point-selection-sensitivity", 12);
365 else if ((theMode == getSelectionMode(TopAbs_EDGE)) ||
366 (theMode == getSelectionMode(TopAbs_WIRE)))
367 aPrecision = theIO->Width() + ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer",
368 "edge-selection-sensitivity", 2);
371 aContext->SetSelectionSensitivity(theIO, theMode, aPrecision);
374 #ifdef DEBUG_ACTIVATE_AIS
375 ObjectPtr anObject = getObject(theIO);
376 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
377 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode)
378 .arg(anInfo).toStdString().c_str());
381 getDisplayer()->updateViewer();
385 //**************************************************************
386 void XGUI_SelectionActivate::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO,
387 const int theMode) const
389 Handle(AIS_InteractiveContext) aContext = AISContext();
390 if (!aContext.IsNull()) {
392 aContext->Deactivate(theIO);
394 aContext->Deactivate(theIO, theMode);
396 #ifdef DEBUG_DEACTIVATE_AIS
397 ObjectPtr anObject = getObject(theIO);
398 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
399 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode)
400 .arg(anInfo).toStdString().c_str());
405 //**************************************************************
406 bool XGUI_SelectionActivate::activate(const Handle(AIS_InteractiveObject)& theIO,
407 const bool theUpdateViewer) const
409 Handle(AIS_InteractiveContext) aContext = AISContext();
410 if (aContext.IsNull() || theIO.IsNull())
413 bool isActivationChanged = false;
414 // deactivate object in all modes, which are not in the list of activation
415 // It seems that after the IO deactivation the selected state of the IO's owners
416 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
417 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
418 // only if there is a difference in the current modes and the parameters modes.
419 // If the selection problem happens again, it is possible to write a test scenario and create
420 // a bug. The bug steps are the following:
421 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
422 // with clicked SHIFT select the second object.
423 // The result is the selection of the first IO is lost.
424 TColStd_ListOfInteger aTColModes;
425 aContext->ActivatedModes(theIO, aTColModes);
426 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
427 QIntList aModesActivatedForIO;
428 bool isDeactivated = false;
429 bool aHasValidMode = false;
430 for (; itr.More(); itr.Next() ) {
431 Standard_Integer aMode = itr.Value();
432 aHasValidMode = aHasValidMode || aMode != -1;
433 //int aShapeMode = (aMode > 8)? aMode : AIS_Shape::SelectionType(aMode);
434 if (!myActiveSelectionModes.contains(aMode)) {
435 deactivateAIS(theIO, aMode);
436 isDeactivated = true;
439 aModesActivatedForIO.append(aMode);
443 // the selection from the previous activation modes should be cleared manually (#26172)
444 //theIO->ClearSelected();
445 #ifndef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
446 XGUI_Tools::workshop(myWorkshop)->selector()->deselectPresentation(theIO);
448 // For performance issues
449 //if (theUpdateViewer)
450 // getDisplayer()->updateViewer();
451 isActivationChanged = true;
454 // loading the interactive object allowing the decomposition
455 if (aTColModes.IsEmpty() || !aHasValidMode) {
456 aContext->Load(theIO);
457 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
458 if (!aTrihedron.IsNull()) {
459 // Workaround for Trihedron. It should be loaded using the next Load method to
460 // add this object to myGlobal map of selection manager
461 // it is important to activate trihedron in two selection modes: edges and vertices
462 aContext->SelectionManager()->Load(theIO);
466 // trihedron AIS check should be after the AIS loading.
467 // If it is not loaded, it is steel selectable in the viewer.
468 Handle(AIS_Trihedron) aTrihedron;
469 if (!isTrihedronActive())
470 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
471 if (aTrihedron.IsNull()) {
472 // In order to clear active modes list
473 if (myActiveSelectionModes.size() == 0) {
474 activateAIS(theIO, 0, theUpdateViewer);
476 foreach(int aMode, myActiveSelectionModes) {
477 if (!aModesActivatedForIO.contains(aMode)) {
478 activateAIS(theIO, aMode, theUpdateViewer);
479 isActivationChanged = true;
484 return isActivationChanged;
487 //**************************************************************
488 void XGUI_SelectionActivate::deactivate(const ObjectPtr& theObject, const bool theUpdateViewer)
490 #ifdef DEBUG_DEACTIVATE
491 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
492 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
493 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
495 toStdString().c_str());
497 Handle(AIS_InteractiveContext) aContext = AISContext();
498 if (!aContext.IsNull() && getDisplayer()->isVisible(theObject)) {
499 AISObjectPtr anObj = getDisplayedAISObject(theObject);
500 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
502 deactivateAIS(anAIS);
503 // the selection from the previous activation modes should be cleared manually (#26172)
504 #ifndef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
505 XGUI_Tools::workshop(myWorkshop)->selector()->deselectPresentation(anAIS);
508 getDisplayer()->updateViewer();
512 /// #1136 hidden axis are selected in sketch
513 #ifdef BEFORE_TRIHEDRON_PATCH
514 //**************************************************************
515 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
516 Handle(AIS_InteractiveObject) theObject)
518 if (!theObject.IsNull())
519 theContext->Deactivate(theObject);
523 //**************************************************************
524 void XGUI_SelectionActivate::activateTrihedron(bool theIsActive)
526 myIsTrihedronActive = theIsActive;
527 if (!myIsTrihedronActive)
528 deactivateTrihedron(true);
531 //**************************************************************
532 void XGUI_SelectionActivate::deactivateTrihedron(const bool theUpdateViewer) const
534 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
535 Handle(AIS_InteractiveContext) aContext = AISContext();
536 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
537 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
539 aContext->Deactivate(aTrie);
541 /// #1136 hidden axis are selected in sketch
542 #ifdef BEFORE_TRIHEDRON_PATCH
543 deactivateObject(aContext, aTrie->XAxis());
544 deactivateObject(aContext, aTrie->YAxis());
545 deactivateObject(aContext, aTrie->Axis());
546 deactivateObject(aContext, aTrie->Position());
548 deactivateObject(aContext, aTrie->XYPlane());
549 deactivateObject(aContext, aTrie->XZPlane());
550 deactivateObject(aContext, aTrie->YZPlane());
553 getDisplayer()->updateViewer();
557 //**************************************************************
558 void XGUI_SelectionActivate::deactivateTrihedronInSelectionModes()
560 Handle(AIS_InteractiveContext) aContext = AISContext();
561 if (!aContext.IsNull()) {
562 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(getTrihedron());
563 /// deactivate trihedron in selection modes
564 TColStd_ListOfInteger aTColModes;
565 aContext->ActivatedModes(aTrihedron, aTColModes);
566 TColStd_ListIteratorOfListOfInteger itr(aTColModes);
567 for (; itr.More(); itr.Next()) {
568 Standard_Integer aMode = itr.Value();
569 aContext->Deactivate(aTrihedron, aMode);
574 //**************************************************************
575 Handle(AIS_InteractiveContext) XGUI_SelectionActivate::AISContext() const
577 return myWorkshop->viewer()->AISContext();
580 //**************************************************************
581 XGUI_Displayer* XGUI_SelectionActivate::getDisplayer() const
583 return XGUI_Tools::workshop(myWorkshop)->displayer();
586 //**************************************************************
587 Handle(AIS_InteractiveObject) XGUI_SelectionActivate::getTrihedron() const
589 return myWorkshop->viewer()->trihedron();
592 //**************************************************************
593 AISObjectPtr XGUI_SelectionActivate::getDisplayedAISObject(ObjectPtr theObject) const
595 return getDisplayer()->getAISObject(theObject);
598 //**************************************************************
599 int XGUI_SelectionActivate::getSelectionMode(int theShapeType)
601 return (theShapeType > TopAbs_SHAPE) ? theShapeType :
602 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);