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
52 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
54 //**************************************************************
55 XGUI_SelectionActivate::XGUI_SelectionActivate(ModuleBase_IWorkshop* theWorkshop)
56 : ModuleBase_ISelectionActivate(theWorkshop), myIsTrihedronActive(true)
60 //**************************************************************
61 XGUI_SelectionActivate::SelectionPlace XGUI_SelectionActivate::activeSelectionPlace() const
63 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
64 XGUI_ActiveControlSelector* anActiveSelector = aWorkshop->activeControlMgr()->activeSelector();
65 if (!anActiveSelector)
68 if (anActiveSelector->getType() == XGUI_FacesPanelSelector::Type())
74 //**************************************************************
75 void XGUI_SelectionActivate::updateSelectionModes()
78 switch (activeSelectionPlace()) {
80 myWorkshop->module()->activeSelectionModes(aModes);
83 ModuleBase_ModelWidget* anActiveWidget = myWorkshop->module()->activeWidget();
85 getSelectionModes(anActiveWidget, aModes);
87 myWorkshop->module()->activeSelectionModes(aModes); //using module modes
91 XGUI_Tools::workshop(myWorkshop)->facesPanel()->selectionModes(aModes);
92 myWorkshop->module()->moduleSelectionModes(-1/*all modes*/, aModes);
97 activateObjects(aModes, getDisplayer()->displayedObjects(), true);
100 //**************************************************************
101 void XGUI_SelectionActivate::updateSelectionFilters()
103 SelectMgr_ListOfFilter aSelectionFilters;
104 switch (activeSelectionPlace()) {
106 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
107 myWorkshop->module()->moduleSelectionFilters(aModuleSelectionFilters, aSelectionFilters);
110 case PropertyPanel: {
111 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
113 ModuleBase_ModelWidget* anActiveWidget = myWorkshop->module()->activeWidget();
115 anActiveWidget->selectionFilters(aModuleSelectionFilters, aSelectionFilters);
116 myWorkshop->module()->moduleSelectionFilters(aModuleSelectionFilters, aSelectionFilters);
120 XGUI_Tools::workshop(myWorkshop)->facesPanel()->selectionFilters(aSelectionFilters);
121 //QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
122 //myWorkshop->module()->moduleSelectionFilters(aModuleSelectionFilters, aSelectionFilters);
127 activateSelectionFilters(aSelectionFilters);
130 //**************************************************************
131 void XGUI_SelectionActivate::activateSelectionFilters
132 (const SelectMgr_ListOfFilter& theSelectionFilters)
134 XGUI_Displayer* aDisplayer = getDisplayer();
135 aDisplayer->deactivateSelectionFilters(false);
137 SelectMgr_ListIteratorOfListOfFilter aIt(theSelectionFilters);
138 for (; aIt.More(); aIt.Next()) {
139 Handle(SelectMgr_Filter) aFilter = aIt.Value();
140 if (aFilter.IsNull())
142 aDisplayer->addSelectionFilter(aFilter);
146 //**************************************************************
147 void XGUI_SelectionActivate::getSelectionModes(ModuleBase_ModelWidget* theWidget,
153 int aModuleSelectionModes = -1;
154 theWidget->selectionModes(aModuleSelectionModes, theModes);
155 myWorkshop->module()->moduleSelectionModes(aModuleSelectionModes, theModes);
158 //**************************************************************
159 QIntList XGUI_SelectionActivate::activeSelectionModes() const
162 foreach (int aMode, myActiveSelectionModes) {
163 // aMode < 9 is a Shape Enum values
164 aModes << ((aMode < 9)? AIS_Shape::SelectionType(aMode) : aMode);
169 //**************************************************************
170 bool XGUI_SelectionActivate::isActive(ObjectPtr theObject) const
172 Handle(AIS_InteractiveContext) aContext = AISContext();
173 if (aContext.IsNull() || !getDisplayer()->isVisible(theObject))
176 AISObjectPtr anObj = getDisplayedAISObject(theObject);
177 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
179 TColStd_ListOfInteger aModes;
180 aContext->ActivatedModes(anAIS, aModes);
182 return aModes.Extent() > 0;
185 //**************************************************************
186 void XGUI_SelectionActivate::activateObjects(const QIntList& theModes,
187 const QObjectPtrList& theObjList, const bool theUpdateViewer)
189 setSelectionModes(theModes);
191 Handle(AIS_InteractiveContext) aContext = AISContext();
192 // Open local context if there is no one
193 if (aContext.IsNull())
196 //aContext->UseDisplayedObjects();
197 //myUseExternalObjects = true;
199 Handle(AIS_InteractiveObject) anAISIO;
200 AIS_ListOfInteractive aPrsList;
201 AIS_ListOfInteractive aPrsListToBeDeactivated;
202 //if (aObjList.isEmpty())
205 foreach(ObjectPtr anObject, theObjList) {
206 AISObjectPtr anAISObject = getDisplayedAISObject(anObject);
207 if (!anAISObject.get())
210 Handle(AIS_InteractiveObject) aPrs = anAISObject->impl<Handle(AIS_InteractiveObject)>();
211 if (myWorkshop->module()->canActivateSelection(anObject))
212 aPrsList.Append(aPrs);
214 aPrsListToBeDeactivated.Append(aPrs);
218 // Add trihedron because it has to partisipate in selection
219 Handle(AIS_InteractiveObject) aTrihedron;
220 if (isTrihedronActive()) {
221 aTrihedron = getTrihedron();
222 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
223 aPrsList.Append(aTrihedron);
225 if (aPrsList.Extent() == 0 && aPrsListToBeDeactivated.Extent() == 0)
228 AIS_ListIteratorOfListOfInteractive aLIt;
229 bool isActivationChanged = false;
230 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()) {
231 anAISIO = aLIt.Value();
232 if (activate(anAISIO, false))
233 isActivationChanged = true;
236 for(aLIt.Initialize(aPrsListToBeDeactivated); aLIt.More(); aLIt.Next()) {
237 anAISIO = aLIt.Value();
238 deactivateAIS(anAISIO);
239 isActivationChanged = true;
243 #ifdef DEBUG_ACTIVATE_OBJECTS
244 //**************************************************************
245 QString getModeInfo(const int theMode)
247 QString anInfo = "Undefined";
249 case 0: anInfo = "SHAPE(0)"; break;
250 case 1: anInfo = "VERTEX(1)"; break;
251 case 2: anInfo = "EDGE(2)"; break;
252 case 3: anInfo = "WIRE(3)"; break;
253 case 4: anInfo = "FACE(4)"; break;
254 case 5: anInfo = "SHELL(5)"; break;
255 case 6: anInfo = "SOLID(6)"; break;
256 case 7: anInfo = "COMPSOLID(7)"; break;
257 case 8: anInfo = "COMPOUND(8)"; break;
258 case 100: anInfo = "Sel_Mode_First(100)"; break; //SketcherPrs_Tools
259 case 101: anInfo = "Sel_Constraint(101)"; break;
260 case 102: anInfo = "Sel_Dimension_All(102)"; break;
261 case 103: anInfo = "Sel_Dimension_Line(103)"; break;
262 case 104: anInfo = "Sel_Dimension_Text(104)"; break;
268 //**************************************************************
269 QString getModesInfo(const QIntList& theModes)
271 QStringList aModesInfo;
272 for (int i = 0, aSize = theModes.size(); i < aSize; i++)
273 aModesInfo.append(getModeInfo(theModes[i]));
274 return QString("[%1] = %2").arg(aModesInfo.size()).arg(aModesInfo.join(", "));
278 //**************************************************************
279 void XGUI_SelectionActivate::setSelectionModes(const QIntList& theModes)
281 // Convert shape types to selection types
283 foreach(int aType, theModes) {
284 aModes.append(getSelectionMode(aType));
287 #ifdef DEBUG_ACTIVATE_OBJECTS
289 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
290 for (; anIt != aLast; ++anIt) {
291 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
293 QString anInfoStr = anInfo.join(", ");
295 qDebug(QString("activateObjects: new modes%1, active modes%2, objects[%3] = %4").
296 arg(getModesInfo(aModes)).
297 arg(getModesInfo(myActiveSelectionModes)).
298 arg(theObjList.size()).
300 toStdString().c_str());
302 // In order to avoid doblications of selection modes
304 foreach (int aMode, aModes) {
305 if (!aNewModes.contains(aMode))
306 aNewModes.append(aMode);
308 myActiveSelectionModes = aNewModes;
311 //**************************************************************
312 void XGUI_SelectionActivate::activateOnDisplay(const Handle(AIS_InteractiveObject)& theIO,
313 const bool theUpdateViewer)
315 if (myActiveSelectionModes.size() == 0)
316 activateAIS(theIO, 0, theUpdateViewer);
318 foreach(int aMode, myActiveSelectionModes) {
319 activateAIS(theIO, aMode, theUpdateViewer);
324 //**************************************************************
325 void XGUI_SelectionActivate::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
326 const int theMode, const bool theUpdateViewer) const
328 Handle(AIS_InteractiveContext) aContext = AISContext();
329 if (!theIO.IsNull() && theIO == getTrihedron()) {
330 if (theMode != AIS_Shape::SelectionType(TopAbs_EDGE) &&
331 theMode != AIS_Shape::SelectionType(TopAbs_VERTEX))
334 if (!aContext.IsNull()) {
335 if (myWorkshop->module()) {
336 int aMode = (theMode > 8)? theMode : AIS_Shape::SelectionType(theMode);
337 aContext->Activate(theIO, theMode, false);
339 aContext->Activate(theIO, theMode, false);
341 // the fix from VPA for more suitable selection of sketcher lines
342 if (theIO->Width() > 1) {
343 double aPrecision = theIO->Width() + 2;
344 if (theMode == getSelectionMode(TopAbs_VERTEX))
345 aPrecision = ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer",
346 "point-selection-sensitivity", 12);
347 else if ((theMode == getSelectionMode(TopAbs_EDGE)) ||
348 (theMode == getSelectionMode(TopAbs_WIRE)))
349 aPrecision = theIO->Width() + ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer",
350 "edge-selection-sensitivity", 2);
351 aContext->SetSelectionSensitivity(theIO, theMode, aPrecision);
354 #ifdef DEBUG_ACTIVATE_AIS
355 ObjectPtr anObject = getObject(theIO);
356 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
357 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode)
358 .arg(anInfo).toStdString().c_str());
361 getDisplayer()->updateViewer();
365 //**************************************************************
366 void XGUI_SelectionActivate::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO,
367 const int theMode) const
369 Handle(AIS_InteractiveContext) aContext = AISContext();
370 if (!aContext.IsNull()) {
372 aContext->Deactivate(theIO);
374 aContext->Deactivate(theIO, theMode);
376 #ifdef DEBUG_DEACTIVATE_AIS
377 ObjectPtr anObject = getObject(theIO);
378 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
379 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode)
380 .arg(anInfo).toStdString().c_str());
385 //**************************************************************
386 bool XGUI_SelectionActivate::activate(const Handle(AIS_InteractiveObject)& theIO,
387 const bool theUpdateViewer) const
389 Handle(AIS_InteractiveContext) aContext = AISContext();
390 if (aContext.IsNull() || theIO.IsNull())
393 bool isActivationChanged = false;
394 // deactivate object in all modes, which are not in the list of activation
395 // It seems that after the IO deactivation the selected state of the IO's owners
396 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
397 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
398 // only if there is a difference in the current modes and the parameters modes.
399 // If the selection problem happens again, it is possible to write a test scenario and create
400 // a bug. The bug steps are the following:
401 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
402 // with clicked SHIFT select the second object.
403 // The result is the selection of the first IO is lost.
404 TColStd_ListOfInteger aTColModes;
405 aContext->ActivatedModes(theIO, aTColModes);
406 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
407 QIntList aModesActivatedForIO;
408 bool isDeactivated = false;
409 bool aHasValidMode = false;
410 for (; itr.More(); itr.Next() ) {
411 Standard_Integer aMode = itr.Value();
412 aHasValidMode = aHasValidMode || aMode != -1;
413 int aShapeMode = (aMode > 8)? aMode : AIS_Shape::SelectionType(aMode);
414 if (!myActiveSelectionModes.contains(aMode)) {
415 deactivateAIS(theIO, aMode);
416 isDeactivated = true;
419 aModesActivatedForIO.append(aMode);
423 // the selection from the previous activation modes should be cleared manually (#26172)
424 //theIO->ClearSelected();
425 #ifndef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
426 XGUI_Tools::workshop(myWorkshop)->selector()->deselectPresentation(theIO);
428 // For performance issues
429 //if (theUpdateViewer)
430 // getDisplayer()->updateViewer();
431 isActivationChanged = true;
434 // loading the interactive object allowing the decomposition
435 if (aTColModes.IsEmpty() || !aHasValidMode) {
436 aContext->Load(theIO, -1, true);
437 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
438 if (!aTrihedron.IsNull()) {
439 // Workaround for Trihedron. It should be loaded using the next Load method to
440 // add this object to myGlobal map of selection manager
441 // it is important to activate trihedron in two selection modes: edges and vertices
442 aContext->SelectionManager()->Load(theIO);
446 // trihedron AIS check should be after the AIS loading.
447 // If it is not loaded, it is steel selectable in the viewer.
448 Handle(AIS_Trihedron) aTrihedron;
449 if (!isTrihedronActive())
450 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
451 if (aTrihedron.IsNull()) {
452 // In order to clear active modes list
453 if (myActiveSelectionModes.size() == 0) {
454 activateAIS(theIO, 0, theUpdateViewer);
456 foreach(int aMode, myActiveSelectionModes) {
457 if (!aModesActivatedForIO.contains(aMode)) {
458 activateAIS(theIO, aMode, theUpdateViewer);
459 isActivationChanged = true;
464 return isActivationChanged;
467 //**************************************************************
468 void XGUI_SelectionActivate::deactivate(const ObjectPtr& theObject, const bool theUpdateViewer)
470 #ifdef DEBUG_DEACTIVATE
471 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
472 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
473 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
475 toStdString().c_str());
477 Handle(AIS_InteractiveContext) aContext = AISContext();
478 if (!aContext.IsNull() && getDisplayer()->isVisible(theObject)) {
479 AISObjectPtr anObj = getDisplayedAISObject(theObject);
480 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
482 deactivateAIS(anAIS);
483 // the selection from the previous activation modes should be cleared manually (#26172)
484 #ifndef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
485 XGUI_Tools::workshop(myWorkshop)->selector()->deselectPresentation(anAIS);
488 getDisplayer()->updateViewer();
492 /// #1136 hidden axis are selected in sketch
493 #ifdef BEFORE_TRIHEDRON_PATCH
494 //**************************************************************
495 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
496 Handle(AIS_InteractiveObject) theObject)
498 if (!theObject.IsNull())
499 theContext->Deactivate(theObject);
503 //**************************************************************
504 void XGUI_SelectionActivate::activateTrihedron(bool theIsActive)
506 myIsTrihedronActive = theIsActive;
507 if (!myIsTrihedronActive)
508 deactivateTrihedron(true);
511 //**************************************************************
512 void XGUI_SelectionActivate::deactivateTrihedron(const bool theUpdateViewer) const
514 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
515 Handle(AIS_InteractiveContext) aContext = AISContext();
516 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
517 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
519 aContext->Deactivate(aTrie);
521 /// #1136 hidden axis are selected in sketch
522 #ifdef BEFORE_TRIHEDRON_PATCH
523 deactivateObject(aContext, aTrie->XAxis());
524 deactivateObject(aContext, aTrie->YAxis());
525 deactivateObject(aContext, aTrie->Axis());
526 deactivateObject(aContext, aTrie->Position());
528 deactivateObject(aContext, aTrie->XYPlane());
529 deactivateObject(aContext, aTrie->XZPlane());
530 deactivateObject(aContext, aTrie->YZPlane());
533 getDisplayer()->updateViewer();
537 //**************************************************************
538 void XGUI_SelectionActivate::deactivateTrihedronInSelectionModes()
540 Handle(AIS_InteractiveContext) aContext = AISContext();
541 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(getTrihedron());
542 /// deactivate trihedron in selection modes
543 TColStd_ListOfInteger aTColModes;
544 aContext->ActivatedModes(aTrihedron, aTColModes);
545 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
546 for (; itr.More(); itr.Next() ) {
547 Standard_Integer aMode = itr.Value();
548 aContext->Deactivate(aTrihedron, aMode);
552 //**************************************************************
553 Handle(AIS_InteractiveContext) XGUI_SelectionActivate::AISContext() const
555 return myWorkshop->viewer()->AISContext();
558 //**************************************************************
559 XGUI_Displayer* XGUI_SelectionActivate::getDisplayer() const
561 return XGUI_Tools::workshop(myWorkshop)->displayer();
564 //**************************************************************
565 Handle(AIS_InteractiveObject) XGUI_SelectionActivate::getTrihedron() const
567 return myWorkshop->viewer()->trihedron();
570 //**************************************************************
571 AISObjectPtr XGUI_SelectionActivate::getDisplayedAISObject(ObjectPtr theObject) const
573 return getDisplayer()->getAISObject(theObject);
576 //**************************************************************
577 int XGUI_SelectionActivate::getSelectionMode(int theShapeType)
579 return (theShapeType > TopAbs_SHAPE) ? theShapeType :
580 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);