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()->customSubShapesSelectionModes(aModes); // avoid wire selection
97 activateObjects(aModes, getDisplayer()->displayedObjects(), true);
100 //**************************************************************
101 void XGUI_SelectionActivate::updateSelectionFilters()
103 SelectMgr_ListOfFilter aSelectionFilters;
104 switch (activeSelectionPlace()) {
106 XGUI_Tools::workshop(myWorkshop)->selectionFilters(aSelectionFilters);
108 case PropertyPanel: {
109 ModuleBase_ModelWidget* anActiveWidget = myWorkshop->module()->activeWidget();
110 getSelectionFilters(anActiveWidget, aSelectionFilters);
114 //XGUI_Tools::workshop(myWorkshop)->selectionFilters(aSelectionFilters);
115 XGUI_Tools::workshop(myWorkshop)->facesPanel()->selectionFilters(aSelectionFilters);
120 activateSelectionFilters(aSelectionFilters);
123 //**************************************************************
124 void XGUI_SelectionActivate::activateSelectionFilters
125 (const SelectMgr_ListOfFilter& theSelectionFilters)
127 XGUI_Displayer* aDisplayer = getDisplayer();
128 aDisplayer->deactivateSelectionFilters(false);
130 SelectMgr_ListIteratorOfListOfFilter aIt(theSelectionFilters);
131 for (; aIt.More(); aIt.Next()) {
132 Handle(SelectMgr_Filter) aFilter = aIt.Value();
133 if (aFilter.IsNull())
135 aDisplayer->addSelectionFilter(aFilter);
139 //**************************************************************
140 void XGUI_SelectionActivate::getSelectionModes(ModuleBase_ModelWidget* theWidget,
146 bool isAdditional = false;
147 theWidget->selectionModes(theModes, isAdditional);
149 myWorkshop->module()->customSubShapesSelectionModes(theModes);
150 //theModes.append(XGUI_Tools::workshop(myWorkshop)->viewerSelectionModes());
151 //myWorkshop->module()->activeSelectionModes(theModes);
155 //**************************************************************
156 void XGUI_SelectionActivate::getSelectionFilters(ModuleBase_ModelWidget* theWidget,
157 SelectMgr_ListOfFilter& theSelectionFilters)
159 XGUI_Tools::workshop(myWorkshop)->selectionFilters(theSelectionFilters);
162 theWidget->selectionFilters(theSelectionFilters);
165 //**************************************************************
166 QIntList XGUI_SelectionActivate::activeSelectionModes() const
169 foreach (int aMode, myActiveSelectionModes) {
170 // aMode < 9 is a Shape Enum values
171 aModes << ((aMode < 9)? AIS_Shape::SelectionType(aMode) : aMode);
176 //**************************************************************
177 bool XGUI_SelectionActivate::isActive(ObjectPtr theObject) const
179 Handle(AIS_InteractiveContext) aContext = AISContext();
180 if (aContext.IsNull() || !getDisplayer()->isVisible(theObject))
183 AISObjectPtr anObj = getDisplayedAISObject(theObject);
184 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
186 TColStd_ListOfInteger aModes;
187 aContext->ActivatedModes(anAIS, aModes);
189 return aModes.Extent() > 0;
192 //**************************************************************
193 void XGUI_SelectionActivate::activateObjects(const QIntList& theModes,
194 const QObjectPtrList& theObjList, const bool theUpdateViewer)
196 setSelectionModes(theModes);
198 Handle(AIS_InteractiveContext) aContext = AISContext();
199 // Open local context if there is no one
200 if (aContext.IsNull())
203 //aContext->UseDisplayedObjects();
204 //myUseExternalObjects = true;
206 Handle(AIS_InteractiveObject) anAISIO;
207 AIS_ListOfInteractive aPrsList;
208 AIS_ListOfInteractive aPrsListToBeDeactivated;
209 //if (aObjList.isEmpty())
212 foreach(ObjectPtr anObject, theObjList) {
213 AISObjectPtr anAISObject = getDisplayedAISObject(anObject);
214 if (!anAISObject.get())
217 Handle(AIS_InteractiveObject) aPrs = anAISObject->impl<Handle(AIS_InteractiveObject)>();
218 if (myWorkshop->module()->canActivateSelection(anObject))
219 aPrsList.Append(aPrs);
221 aPrsListToBeDeactivated.Append(aPrs);
225 // Add trihedron because it has to partisipate in selection
226 Handle(AIS_InteractiveObject) aTrihedron;
227 if (isTrihedronActive()) {
228 aTrihedron = getTrihedron();
229 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
230 aPrsList.Append(aTrihedron);
232 if (aPrsList.Extent() == 0 && aPrsListToBeDeactivated.Extent() == 0)
235 AIS_ListIteratorOfListOfInteractive aLIt;
236 bool isActivationChanged = false;
237 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()) {
238 anAISIO = aLIt.Value();
239 if (activate(anAISIO, false))
240 isActivationChanged = true;
243 for(aLIt.Initialize(aPrsListToBeDeactivated); aLIt.More(); aLIt.Next()) {
244 anAISIO = aLIt.Value();
245 deactivateAIS(anAISIO);
246 isActivationChanged = true;
250 #ifdef DEBUG_ACTIVATE_OBJECTS
251 //**************************************************************
252 QString getModeInfo(const int theMode)
254 QString anInfo = "Undefined";
256 case 0: anInfo = "SHAPE(0)"; break;
257 case 1: anInfo = "VERTEX(1)"; break;
258 case 2: anInfo = "EDGE(2)"; break;
259 case 3: anInfo = "WIRE(3)"; break;
260 case 4: anInfo = "FACE(4)"; break;
261 case 5: anInfo = "SHELL(5)"; break;
262 case 6: anInfo = "SOLID(6)"; break;
263 case 7: anInfo = "COMPSOLID(7)"; break;
264 case 8: anInfo = "COMPOUND(8)"; break;
265 case 100: anInfo = "Sel_Mode_First(100)"; break; //SketcherPrs_Tools
266 case 101: anInfo = "Sel_Constraint(101)"; break;
267 case 102: anInfo = "Sel_Dimension_All(102)"; break;
268 case 103: anInfo = "Sel_Dimension_Line(103)"; break;
269 case 104: anInfo = "Sel_Dimension_Text(104)"; break;
275 //**************************************************************
276 QString getModesInfo(const QIntList& theModes)
278 QStringList aModesInfo;
279 for (int i = 0, aSize = theModes.size(); i < aSize; i++)
280 aModesInfo.append(getModeInfo(theModes[i]));
281 return QString("[%1] = %2").arg(aModesInfo.size()).arg(aModesInfo.join(", "));
285 //**************************************************************
286 void XGUI_SelectionActivate::setSelectionModes(const QIntList& theModes)
288 // Convert shape types to selection types
290 foreach(int aType, theModes) {
291 aModes.append(getSelectionMode(aType));
294 #ifdef DEBUG_ACTIVATE_OBJECTS
296 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
297 for (; anIt != aLast; ++anIt) {
298 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
300 QString anInfoStr = anInfo.join(", ");
302 qDebug(QString("activateObjects: new modes%1, active modes%2, objects[%3] = %4").
303 arg(getModesInfo(aModes)).
304 arg(getModesInfo(myActiveSelectionModes)).
305 arg(theObjList.size()).
307 toStdString().c_str());
309 // In order to avoid doblications of selection modes
311 foreach (int aMode, aModes) {
312 if (!aNewModes.contains(aMode))
313 aNewModes.append(aMode);
315 myActiveSelectionModes = aNewModes;
318 //**************************************************************
319 void XGUI_SelectionActivate::activateOnDisplay(const Handle(AIS_InteractiveObject)& theIO,
320 const bool theUpdateViewer)
322 if (myActiveSelectionModes.size() == 0)
323 activateAIS(theIO, 0, theUpdateViewer);
325 foreach(int aMode, myActiveSelectionModes) {
326 activateAIS(theIO, aMode, theUpdateViewer);
331 //**************************************************************
332 void XGUI_SelectionActivate::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
333 const int theMode, const bool theUpdateViewer) const
335 Handle(AIS_InteractiveContext) aContext = AISContext();
336 if (!theIO.IsNull() && theIO == getTrihedron()) {
337 if (theMode != AIS_Shape::SelectionType(TopAbs_EDGE) &&
338 theMode != AIS_Shape::SelectionType(TopAbs_VERTEX))
341 if (!aContext.IsNull()) {
342 if (myWorkshop->module()) {
343 int aMode = (theMode > 8)? theMode : AIS_Shape::SelectionType(theMode);
344 aContext->Activate(theIO, theMode, false);
346 aContext->Activate(theIO, theMode, false);
348 // the fix from VPA for more suitable selection of sketcher lines
349 if (theIO->Width() > 1) {
350 double aPrecision = theIO->Width() + 2;
351 if (theMode == getSelectionMode(TopAbs_VERTEX))
352 aPrecision = ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer",
353 "point-selection-sensitivity", 12);
354 else if ((theMode == getSelectionMode(TopAbs_EDGE)) ||
355 (theMode == getSelectionMode(TopAbs_WIRE)))
356 aPrecision = theIO->Width() + ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer",
357 "edge-selection-sensitivity", 2);
358 aContext->SetSelectionSensitivity(theIO, theMode, aPrecision);
361 #ifdef DEBUG_ACTIVATE_AIS
362 ObjectPtr anObject = getObject(theIO);
363 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
364 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode)
365 .arg(anInfo).toStdString().c_str());
368 getDisplayer()->updateViewer();
372 //**************************************************************
373 void XGUI_SelectionActivate::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO,
374 const int theMode) const
376 Handle(AIS_InteractiveContext) aContext = AISContext();
377 if (!aContext.IsNull()) {
379 aContext->Deactivate(theIO);
381 aContext->Deactivate(theIO, theMode);
383 #ifdef DEBUG_DEACTIVATE_AIS
384 ObjectPtr anObject = getObject(theIO);
385 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
386 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode)
387 .arg(anInfo).toStdString().c_str());
392 //**************************************************************
393 bool XGUI_SelectionActivate::activate(const Handle(AIS_InteractiveObject)& theIO,
394 const bool theUpdateViewer) const
396 Handle(AIS_InteractiveContext) aContext = AISContext();
397 if (aContext.IsNull() || theIO.IsNull())
400 bool isActivationChanged = false;
401 // deactivate object in all modes, which are not in the list of activation
402 // It seems that after the IO deactivation the selected state of the IO's owners
403 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
404 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
405 // only if there is a difference in the current modes and the parameters modes.
406 // If the selection problem happens again, it is possible to write a test scenario and create
407 // a bug. The bug steps are the following:
408 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
409 // with clicked SHIFT select the second object.
410 // The result is the selection of the first IO is lost.
411 TColStd_ListOfInteger aTColModes;
412 aContext->ActivatedModes(theIO, aTColModes);
413 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
414 QIntList aModesActivatedForIO;
415 bool isDeactivated = false;
416 bool aHasValidMode = false;
417 for (; itr.More(); itr.Next() ) {
418 Standard_Integer aMode = itr.Value();
419 aHasValidMode = aHasValidMode || aMode != -1;
420 int aShapeMode = (aMode > 8)? aMode : AIS_Shape::SelectionType(aMode);
421 if (!myActiveSelectionModes.contains(aMode)) {
422 deactivateAIS(theIO, aMode);
423 isDeactivated = true;
426 aModesActivatedForIO.append(aMode);
430 // the selection from the previous activation modes should be cleared manually (#26172)
431 //theIO->ClearSelected();
432 #ifndef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
433 XGUI_Tools::workshop(myWorkshop)->selector()->deselectPresentation(theIO);
435 // For performance issues
436 //if (theUpdateViewer)
437 // getDisplayer()->updateViewer();
438 isActivationChanged = true;
441 // loading the interactive object allowing the decomposition
442 if (aTColModes.IsEmpty() || !aHasValidMode) {
443 aContext->Load(theIO, -1, true);
444 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
445 if (!aTrihedron.IsNull()) {
446 // Workaround for Trihedron. It should be loaded using the next Load method to
447 // add this object to myGlobal map of selection manager
448 // it is important to activate trihedron in two selection modes: edges and vertices
449 aContext->SelectionManager()->Load(theIO);
453 // trihedron AIS check should be after the AIS loading.
454 // If it is not loaded, it is steel selectable in the viewer.
455 Handle(AIS_Trihedron) aTrihedron;
456 if (!isTrihedronActive())
457 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
458 if (aTrihedron.IsNull()) {
459 // In order to clear active modes list
460 if (myActiveSelectionModes.size() == 0) {
461 activateAIS(theIO, 0, theUpdateViewer);
463 foreach(int aMode, myActiveSelectionModes) {
464 if (!aModesActivatedForIO.contains(aMode)) {
465 activateAIS(theIO, aMode, theUpdateViewer);
466 isActivationChanged = true;
471 return isActivationChanged;
474 //**************************************************************
475 void XGUI_SelectionActivate::deactivate(const ObjectPtr& theObject, const bool theUpdateViewer)
477 #ifdef DEBUG_DEACTIVATE
478 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
479 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
480 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
482 toStdString().c_str());
484 Handle(AIS_InteractiveContext) aContext = AISContext();
485 if (!aContext.IsNull() && getDisplayer()->isVisible(theObject)) {
486 AISObjectPtr anObj = getDisplayedAISObject(theObject);
487 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
489 deactivateAIS(anAIS);
490 // the selection from the previous activation modes should be cleared manually (#26172)
491 #ifndef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
492 XGUI_Tools::workshop(myWorkshop)->selector()->deselectPresentation(anAIS);
495 getDisplayer()->updateViewer();
499 /// #1136 hidden axis are selected in sketch
500 #ifdef BEFORE_TRIHEDRON_PATCH
501 //**************************************************************
502 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
503 Handle(AIS_InteractiveObject) theObject)
505 if (!theObject.IsNull())
506 theContext->Deactivate(theObject);
510 //**************************************************************
511 void XGUI_SelectionActivate::activateTrihedron(bool theIsActive)
513 myIsTrihedronActive = theIsActive;
514 if (!myIsTrihedronActive)
515 deactivateTrihedron(true);
518 //**************************************************************
519 void XGUI_SelectionActivate::deactivateTrihedron(const bool theUpdateViewer) const
521 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
522 Handle(AIS_InteractiveContext) aContext = AISContext();
523 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
524 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
526 aContext->Deactivate(aTrie);
528 /// #1136 hidden axis are selected in sketch
529 #ifdef BEFORE_TRIHEDRON_PATCH
530 deactivateObject(aContext, aTrie->XAxis());
531 deactivateObject(aContext, aTrie->YAxis());
532 deactivateObject(aContext, aTrie->Axis());
533 deactivateObject(aContext, aTrie->Position());
535 deactivateObject(aContext, aTrie->XYPlane());
536 deactivateObject(aContext, aTrie->XZPlane());
537 deactivateObject(aContext, aTrie->YZPlane());
540 getDisplayer()->updateViewer();
544 //**************************************************************
545 void XGUI_SelectionActivate::deactivateTrihedronInSelectionModes()
547 Handle(AIS_InteractiveContext) aContext = AISContext();
548 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(getTrihedron());
549 /// deactivate trihedron in selection modes
550 TColStd_ListOfInteger aTColModes;
551 aContext->ActivatedModes(aTrihedron, aTColModes);
552 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
553 for (; itr.More(); itr.Next() ) {
554 Standard_Integer aMode = itr.Value();
555 aContext->Deactivate(aTrihedron, aMode);
559 //**************************************************************
560 Handle(AIS_InteractiveContext) XGUI_SelectionActivate::AISContext() const
562 return myWorkshop->viewer()->AISContext();
565 //**************************************************************
566 XGUI_Displayer* XGUI_SelectionActivate::getDisplayer() const
568 return XGUI_Tools::workshop(myWorkshop)->displayer();
571 //**************************************************************
572 Handle(AIS_InteractiveObject) XGUI_SelectionActivate::getTrihedron() const
574 return myWorkshop->viewer()->trihedron();
577 //**************************************************************
578 AISObjectPtr XGUI_SelectionActivate::getDisplayedAISObject(ObjectPtr theObject) const
580 return getDisplayer()->getAISObject(theObject);
583 //**************************************************************
584 int XGUI_SelectionActivate::getSelectionMode(int theShapeType)
586 return (theShapeType > TopAbs_SHAPE) ? theShapeType :
587 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);