1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_SelectionActivate.h"
23 #include "ModelAPI_Object.h"
25 #include "ModuleBase_IModule.h"
26 #include "ModuleBase_IViewer.h"
27 #include "ModuleBase_ModelWidget.h"
28 #include "ModuleBase_Preferences.h"
30 #include "XGUI_ActiveControlMgr.h"
31 #include "XGUI_ActiveControlSelector.h"
32 #include "XGUI_Displayer.h"
33 #include "XGUI_FacesPanel.h"
34 #include "XGUI_FacesPanelSelector.h"
35 #include "XGUI_SelectionMgr.h"
36 #include "XGUI_Tools.h"
37 #include "XGUI_Workshop.h"
39 #include <SUIT_ResourceMgr.h>
41 #include <AIS_InteractiveContext.hxx>
42 #include <AIS_Shape.hxx>
43 #include <AIS_Trihedron.hxx>
45 #include <SelectMgr_SelectionManager.hxx>
47 //#define DEBUG_ACTIVATE_OBJECTS
48 //#define DEBUG_DEACTIVATE
49 //#define DEBUG_ACTIVATE_AIS
50 //#define DEBUG_DEACTIVATE_AIS
53 #include <inspector/VInspectorAPI_CallBack.hxx>
56 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
58 //**************************************************************
59 XGUI_SelectionActivate::XGUI_SelectionActivate(ModuleBase_IWorkshop* theWorkshop)
60 : ModuleBase_ISelectionActivate(theWorkshop), myIsTrihedronActive(true)
64 //**************************************************************
65 XGUI_SelectionActivate::SelectionPlace XGUI_SelectionActivate::activeSelectionPlace() const
67 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
68 XGUI_ActiveControlSelector* anActiveSelector = aWorkshop->activeControlMgr()->activeSelector();
69 if (!anActiveSelector)
72 if (anActiveSelector->getType() == XGUI_FacesPanelSelector::Type())
78 //**************************************************************
79 void XGUI_SelectionActivate::updateSelectionModes()
82 switch (activeSelectionPlace()) {
84 myWorkshop->module()->activeSelectionModes(aModes);
87 ModuleBase_ModelWidget* anActiveWidget = myWorkshop->module()->activeWidget();
89 getSelectionModes(anActiveWidget, aModes);
91 myWorkshop->module()->activeSelectionModes(aModes); //using module modes
95 XGUI_Tools::workshop(myWorkshop)->facesPanel()->selectionModes(aModes);
96 myWorkshop->module()->moduleSelectionModes(-1/*all modes*/, aModes);
101 activateObjects(aModes, getDisplayer()->displayedObjects(), true);
104 //**************************************************************
105 void XGUI_SelectionActivate::updateSelectionFilters()
107 SelectMgr_ListOfFilter aSelectionFilters;
108 switch (activeSelectionPlace()) {
110 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
111 myWorkshop->module()->moduleSelectionFilters(aModuleSelectionFilters, aSelectionFilters);
114 case PropertyPanel: {
115 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
117 ModuleBase_ModelWidget* anActiveWidget = myWorkshop->module()->activeWidget();
119 anActiveWidget->selectionFilters(aModuleSelectionFilters, aSelectionFilters);
120 myWorkshop->module()->moduleSelectionFilters(aModuleSelectionFilters, aSelectionFilters);
124 XGUI_Tools::workshop(myWorkshop)->facesPanel()->selectionFilters(aSelectionFilters);
125 //QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
126 //myWorkshop->module()->moduleSelectionFilters(aModuleSelectionFilters, aSelectionFilters);
131 activateSelectionFilters(aSelectionFilters);
134 //**************************************************************
135 void XGUI_SelectionActivate::activateSelectionFilters
136 (const SelectMgr_ListOfFilter& theSelectionFilters)
138 XGUI_Displayer* aDisplayer = getDisplayer();
139 aDisplayer->deactivateSelectionFilters(false);
141 SelectMgr_ListIteratorOfListOfFilter aIt(theSelectionFilters);
142 for (; aIt.More(); aIt.Next()) {
143 Handle(SelectMgr_Filter) aFilter = aIt.Value();
144 if (aFilter.IsNull())
146 aDisplayer->addSelectionFilter(aFilter);
150 //**************************************************************
151 void XGUI_SelectionActivate::getSelectionModes(ModuleBase_ModelWidget* theWidget,
157 int aModuleSelectionModes = -1;
158 theWidget->selectionModes(aModuleSelectionModes, theModes);
159 myWorkshop->module()->moduleSelectionModes(aModuleSelectionModes, theModes);
162 //**************************************************************
163 QIntList XGUI_SelectionActivate::activeSelectionModes() const
166 foreach (int aMode, myActiveSelectionModes) {
167 // aMode < 9 is a Shape Enum values
168 aModes << ((aMode < 9)? AIS_Shape::SelectionType(aMode) : aMode);
173 //**************************************************************
174 bool XGUI_SelectionActivate::isActive(ObjectPtr theObject) const
176 Handle(AIS_InteractiveContext) aContext = AISContext();
177 if (aContext.IsNull() || !getDisplayer()->isVisible(theObject))
180 AISObjectPtr anObj = getDisplayedAISObject(theObject);
181 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
183 TColStd_ListOfInteger aModes;
184 aContext->ActivatedModes(anAIS, aModes);
186 return aModes.Extent() > 0;
189 //**************************************************************
190 void XGUI_SelectionActivate::activateObjects(const QIntList& theModes,
191 const QObjectPtrList& theObjList, const bool theUpdateViewer)
193 setSelectionModes(theModes);
195 Handle(AIS_InteractiveContext) aContext = AISContext();
196 // Open local context if there is no one
197 if (aContext.IsNull())
200 //aContext->UseDisplayedObjects();
201 //myUseExternalObjects = true;
203 Handle(AIS_InteractiveObject) anAISIO;
204 AIS_ListOfInteractive aPrsList;
205 AIS_ListOfInteractive aPrsListToBeDeactivated;
206 //if (aObjList.isEmpty())
209 foreach(ObjectPtr anObject, theObjList) {
210 AISObjectPtr anAISObject = getDisplayedAISObject(anObject);
211 if (!anAISObject.get())
214 Handle(AIS_InteractiveObject) aPrs = anAISObject->impl<Handle(AIS_InteractiveObject)>();
215 if (myWorkshop->module()->canActivateSelection(anObject))
216 aPrsList.Append(aPrs);
218 aPrsListToBeDeactivated.Append(aPrs);
222 // Add trihedron because it has to partisipate in selection
223 Handle(AIS_InteractiveObject) aTrihedron;
224 if (isTrihedronActive()) {
225 aTrihedron = getTrihedron();
226 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
227 aPrsList.Append(aTrihedron);
229 if (aPrsList.Extent() == 0 && aPrsListToBeDeactivated.Extent() == 0)
232 AIS_ListIteratorOfListOfInteractive aLIt;
233 bool isActivationChanged = false;
234 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()) {
235 anAISIO = aLIt.Value();
236 if (activate(anAISIO, false))
237 isActivationChanged = true;
240 for(aLIt.Initialize(aPrsListToBeDeactivated); aLIt.More(); aLIt.Next()) {
241 anAISIO = aLIt.Value();
242 deactivateAIS(anAISIO);
243 isActivationChanged = true;
247 #ifdef DEBUG_ACTIVATE_OBJECTS
248 //**************************************************************
249 QString getModeInfo(const int theMode)
251 QString anInfo = "Undefined";
253 case 0: anInfo = "SHAPE(0)"; break;
254 case 1: anInfo = "VERTEX(1)"; break;
255 case 2: anInfo = "EDGE(2)"; break;
256 case 3: anInfo = "WIRE(3)"; break;
257 case 4: anInfo = "FACE(4)"; break;
258 case 5: anInfo = "SHELL(5)"; break;
259 case 6: anInfo = "SOLID(6)"; break;
260 case 7: anInfo = "COMPSOLID(7)"; break;
261 case 8: anInfo = "COMPOUND(8)"; break;
262 case 100: anInfo = "Sel_Mode_First(100)"; break; //SketcherPrs_Tools
263 case 101: anInfo = "Sel_Constraint(101)"; break;
264 case 102: anInfo = "Sel_Dimension_All(102)"; break;
265 case 103: anInfo = "Sel_Dimension_Line(103)"; break;
266 case 104: anInfo = "Sel_Dimension_Text(104)"; break;
272 //**************************************************************
273 QString getModesInfo(const QIntList& theModes)
275 QStringList aModesInfo;
276 for (int i = 0, aSize = theModes.size(); i < aSize; i++)
277 aModesInfo.append(getModeInfo(theModes[i]));
278 return QString("[%1] = %2").arg(aModesInfo.size()).arg(aModesInfo.join(", "));
282 //**************************************************************
283 void XGUI_SelectionActivate::setSelectionModes(const QIntList& theModes)
285 // Convert shape types to selection types
287 foreach(int aType, theModes) {
288 aModes.append(getSelectionMode(aType));
291 #ifdef DEBUG_ACTIVATE_OBJECTS
293 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
294 for (; anIt != aLast; ++anIt) {
295 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
297 QString anInfoStr = anInfo.join(", ");
299 qDebug(QString("activateObjects: new modes%1, active modes%2, objects[%3] = %4").
300 arg(getModesInfo(aModes)).
301 arg(getModesInfo(myActiveSelectionModes)).
302 arg(theObjList.size()).
304 toStdString().c_str());
306 // In order to avoid doblications of selection modes
308 foreach (int aMode, aModes) {
309 if (!aNewModes.contains(aMode))
310 aNewModes.append(aMode);
312 myActiveSelectionModes = aNewModes;
315 //**************************************************************
316 void XGUI_SelectionActivate::activateOnDisplay(const Handle(AIS_InteractiveObject)& theIO,
317 const bool theUpdateViewer)
319 if (myActiveSelectionModes.size() == 0)
320 activateAIS(theIO, 0, theUpdateViewer);
322 foreach(int aMode, myActiveSelectionModes) {
323 activateAIS(theIO, aMode, theUpdateViewer);
328 //**************************************************************
329 void XGUI_SelectionActivate::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
330 const int theMode, const bool theUpdateViewer) const
332 Handle(AIS_InteractiveContext) aContext = AISContext();
333 if (!theIO.IsNull() && theIO == getTrihedron()) {
334 if (theMode != AIS_Shape::SelectionType(TopAbs_EDGE) &&
335 theMode != AIS_Shape::SelectionType(TopAbs_VERTEX))
338 if (!aContext.IsNull()) {
339 if (myWorkshop->module()) {
340 // the code is obsolete, used in additional check before activate, it was removed
341 //int aMode = (theMode > 8)? theMode : AIS_Shape::SelectionType(theMode);
342 aContext->Activate(theIO, theMode, false);
344 if (getDisplayer()->getCallBack()) getDisplayer()->getCallBack()->Activate(theIO, theMode);
348 aContext->Activate(theIO, theMode, false);
350 if (getDisplayer()->getCallBack()) getDisplayer()->getCallBack()->Activate(theIO, theMode);
353 // the fix from VPA for more suitable selection of sketcher lines
354 if (theIO->Width() > 1) {
355 double aPrecision = theIO->Width() + 2;
356 if (theMode == getSelectionMode(TopAbs_VERTEX))
357 aPrecision = ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer",
358 "point-selection-sensitivity", 12);
359 else if ((theMode == getSelectionMode(TopAbs_EDGE)) ||
360 (theMode == getSelectionMode(TopAbs_WIRE)))
361 aPrecision = theIO->Width() + ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer",
362 "edge-selection-sensitivity", 2);
363 aContext->SetSelectionSensitivity(theIO, theMode, aPrecision);
366 #ifdef DEBUG_ACTIVATE_AIS
367 ObjectPtr anObject = getObject(theIO);
368 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
369 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode)
370 .arg(anInfo).toStdString().c_str());
373 getDisplayer()->updateViewer();
377 //**************************************************************
378 void XGUI_SelectionActivate::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO,
379 const int theMode) const
381 Handle(AIS_InteractiveContext) aContext = AISContext();
382 if (!aContext.IsNull()) {
384 aContext->Deactivate(theIO);
386 aContext->Deactivate(theIO, theMode);
388 #ifdef DEBUG_DEACTIVATE_AIS
389 ObjectPtr anObject = getObject(theIO);
390 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
391 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode)
392 .arg(anInfo).toStdString().c_str());
397 //**************************************************************
398 bool XGUI_SelectionActivate::activate(const Handle(AIS_InteractiveObject)& theIO,
399 const bool theUpdateViewer) const
401 Handle(AIS_InteractiveContext) aContext = AISContext();
402 if (aContext.IsNull() || theIO.IsNull())
405 bool isActivationChanged = false;
406 // deactivate object in all modes, which are not in the list of activation
407 // It seems that after the IO deactivation the selected state of the IO's owners
408 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
409 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
410 // only if there is a difference in the current modes and the parameters modes.
411 // If the selection problem happens again, it is possible to write a test scenario and create
412 // a bug. The bug steps are the following:
413 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
414 // with clicked SHIFT select the second object.
415 // The result is the selection of the first IO is lost.
416 TColStd_ListOfInteger aTColModes;
417 aContext->ActivatedModes(theIO, aTColModes);
418 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
419 QIntList aModesActivatedForIO;
420 bool isDeactivated = false;
421 bool aHasValidMode = false;
422 for (; itr.More(); itr.Next() ) {
423 Standard_Integer aMode = itr.Value();
424 aHasValidMode = aHasValidMode || aMode != -1;
425 int aShapeMode = (aMode > 8)? aMode : AIS_Shape::SelectionType(aMode);
426 if (!myActiveSelectionModes.contains(aMode)) {
427 deactivateAIS(theIO, aMode);
428 isDeactivated = true;
431 aModesActivatedForIO.append(aMode);
435 // the selection from the previous activation modes should be cleared manually (#26172)
436 //theIO->ClearSelected();
437 #ifndef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
438 XGUI_Tools::workshop(myWorkshop)->selector()->deselectPresentation(theIO);
440 // For performance issues
441 //if (theUpdateViewer)
442 // getDisplayer()->updateViewer();
443 isActivationChanged = true;
446 // loading the interactive object allowing the decomposition
447 if (aTColModes.IsEmpty() || !aHasValidMode) {
448 aContext->Load(theIO, -1, true);
449 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
450 if (!aTrihedron.IsNull()) {
451 // Workaround for Trihedron. It should be loaded using the next Load method to
452 // add this object to myGlobal map of selection manager
453 // it is important to activate trihedron in two selection modes: edges and vertices
454 aContext->SelectionManager()->Load(theIO);
458 // trihedron AIS check should be after the AIS loading.
459 // If it is not loaded, it is steel selectable in the viewer.
460 Handle(AIS_Trihedron) aTrihedron;
461 if (!isTrihedronActive())
462 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
463 if (aTrihedron.IsNull()) {
464 // In order to clear active modes list
465 if (myActiveSelectionModes.size() == 0) {
466 activateAIS(theIO, 0, theUpdateViewer);
468 foreach(int aMode, myActiveSelectionModes) {
469 if (!aModesActivatedForIO.contains(aMode)) {
470 activateAIS(theIO, aMode, theUpdateViewer);
471 isActivationChanged = true;
476 return isActivationChanged;
479 //**************************************************************
480 void XGUI_SelectionActivate::deactivate(const ObjectPtr& theObject, const bool theUpdateViewer)
482 #ifdef DEBUG_DEACTIVATE
483 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
484 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
485 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
487 toStdString().c_str());
489 Handle(AIS_InteractiveContext) aContext = AISContext();
490 if (!aContext.IsNull() && getDisplayer()->isVisible(theObject)) {
491 AISObjectPtr anObj = getDisplayedAISObject(theObject);
492 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
494 deactivateAIS(anAIS);
495 // the selection from the previous activation modes should be cleared manually (#26172)
496 #ifndef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
497 XGUI_Tools::workshop(myWorkshop)->selector()->deselectPresentation(anAIS);
500 getDisplayer()->updateViewer();
504 /// #1136 hidden axis are selected in sketch
505 #ifdef BEFORE_TRIHEDRON_PATCH
506 //**************************************************************
507 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
508 Handle(AIS_InteractiveObject) theObject)
510 if (!theObject.IsNull())
511 theContext->Deactivate(theObject);
515 //**************************************************************
516 void XGUI_SelectionActivate::activateTrihedron(bool theIsActive)
518 myIsTrihedronActive = theIsActive;
519 if (!myIsTrihedronActive)
520 deactivateTrihedron(true);
523 //**************************************************************
524 void XGUI_SelectionActivate::deactivateTrihedron(const bool theUpdateViewer) const
526 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
527 Handle(AIS_InteractiveContext) aContext = AISContext();
528 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
529 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
531 aContext->Deactivate(aTrie);
533 /// #1136 hidden axis are selected in sketch
534 #ifdef BEFORE_TRIHEDRON_PATCH
535 deactivateObject(aContext, aTrie->XAxis());
536 deactivateObject(aContext, aTrie->YAxis());
537 deactivateObject(aContext, aTrie->Axis());
538 deactivateObject(aContext, aTrie->Position());
540 deactivateObject(aContext, aTrie->XYPlane());
541 deactivateObject(aContext, aTrie->XZPlane());
542 deactivateObject(aContext, aTrie->YZPlane());
545 getDisplayer()->updateViewer();
549 //**************************************************************
550 void XGUI_SelectionActivate::deactivateTrihedronInSelectionModes()
552 Handle(AIS_InteractiveContext) aContext = AISContext();
553 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(getTrihedron());
554 /// deactivate trihedron in selection modes
555 TColStd_ListOfInteger aTColModes;
556 aContext->ActivatedModes(aTrihedron, aTColModes);
557 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
558 for (; itr.More(); itr.Next() ) {
559 Standard_Integer aMode = itr.Value();
560 aContext->Deactivate(aTrihedron, aMode);
564 //**************************************************************
565 Handle(AIS_InteractiveContext) XGUI_SelectionActivate::AISContext() const
567 return myWorkshop->viewer()->AISContext();
570 //**************************************************************
571 XGUI_Displayer* XGUI_SelectionActivate::getDisplayer() const
573 return XGUI_Tools::workshop(myWorkshop)->displayer();
576 //**************************************************************
577 Handle(AIS_InteractiveObject) XGUI_SelectionActivate::getTrihedron() const
579 return myWorkshop->viewer()->trihedron();
582 //**************************************************************
583 AISObjectPtr XGUI_SelectionActivate::getDisplayedAISObject(ObjectPtr theObject) const
585 return getDisplayer()->getAISObject(theObject);
588 //**************************************************************
589 int XGUI_SelectionActivate::getSelectionMode(int theShapeType)
591 return (theShapeType > TopAbs_SHAPE) ? theShapeType :
592 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);