Salome HOME
Issue #2975: Do not select groups in all operations except operations on groups
[modules/shaper.git] / src / XGUI / XGUI_FacesPanel.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "XGUI_FacesPanel.h"
21 #include "XGUI_ObjectsBrowser.h"
22 #include "XGUI_SelectionMgr.h"
23 #include "XGUI_Tools.h"
24 #include "XGUI_Workshop.h"
25
26 #include <ModuleBase_IModule.h>
27 #include <ModuleBase_ISelection.h>
28 #include <ModuleBase_IWorkshop.h>
29 #include <ModuleBase_ListView.h>
30 #include <ModuleBase_ResultPrs.h>
31 #include <ModuleBase_Tools.h>
32 #include <ModuleBase_ViewerPrs.h>
33 #include <ModuleBase_SelectionFilterType.h>
34
35 #include <Config_PropManager.h>
36 #include <Events_Loop.h>
37 #include <GeomAlgoAPI_CompoundBuilder.h>
38
39 #include <ModelAPI_Events.h>
40
41 #include <QAction>
42 #include <QCheckBox>
43 #include <QFocusEvent>
44 #include <QGridLayout>
45 #include <QListWidget>
46 #include <QMainWindow>
47
48 static const int LayoutMargin = 3;
49
50 //********************************************************************
51 XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
52   : QDockWidget(theParent), myIsActive(false), myWorkshop(theWorkshop)
53 {
54   setWindowTitle(tr("Hide Faces"));
55   QAction* aViewAct = toggleViewAction();
56   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
57
58   QWidget* aContent = new QWidget(this);
59   QGridLayout* aMainLayout = new QGridLayout(aContent);
60   aMainLayout->setContentsMargins(LayoutMargin, LayoutMargin, LayoutMargin, LayoutMargin);
61   setWidget(aContent);
62
63   myHiddenOrTransparent = new QCheckBox(tr("Transparent"), aContent);
64   connect(myHiddenOrTransparent, SIGNAL(toggled(bool)), SLOT(onTransparencyChanged()));
65
66   myListView = new ModuleBase_ListView(aContent, "", "Hidden/transparent faces in 3D view");
67   connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
68
69   aMainLayout->addWidget(myHiddenOrTransparent, 0, 0);
70   aMainLayout->addWidget(myListView->getControl(), 1, 0);
71
72   myListView->getControl()->setFocusPolicy(Qt::StrongFocus);
73   myListView->getControl()->viewport()->installEventFilter(this);
74 }
75
76 //********************************************************************
77 void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
78 {
79   if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
80     return;
81
82   // clear internal containers
83   myListView->getControl()->clear();
84   myItems.clear();
85
86   // restore previous view of presentations
87   bool isModified = redisplayObjects(myItemObjects);
88   std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
89   isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects) || isModified;
90   if (isModified)// && isToFlushRedisplay) // flush signal immediatelly until container is filled
91     flushRedisplay();
92
93   updateProcessedObjects(myItems, myItemObjects);
94   myHiddenObjects.clear();
95   myLastItemIndex = 0; // it should be after redisplay as flag used in customize
96 }
97
98 //********************************************************************
99 bool XGUI_FacesPanel::isEmpty() const
100 {
101   return myItems.size() == 0;
102 }
103
104 //********************************************************************
105 void XGUI_FacesPanel::selectionModes(QIntList& theModes)
106 {
107   theModes.append(TopAbs_FACE);
108 }
109
110 //********************************************************************
111 void XGUI_FacesPanel::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
112 {
113   ModuleBase_IModule* aModule = myWorkshop->module();
114   QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
115
116   theSelectionFilters.Append(aModule->selectionFilter(SF_GlobalFilter));
117   theSelectionFilters.Append(aModule->selectionFilter(SF_FilterInfinite));
118   theSelectionFilters.Append(aModule->selectionFilter(SF_ResultGroupNameFilter));
119 }
120
121 //********************************************************************
122 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
123 {
124   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
125   if (theEvent->type() == QEvent::MouseButtonRelease)
126   {
127     if (myListView->getControl()->viewport() == aWidget)
128       setActivePanel(true);
129   }
130   // pass the event on to the parent class
131   return QObject::eventFilter(theObject, theEvent);
132 }
133
134 //********************************************************************
135 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
136 {
137   if (myIsActive == theIsActive)
138     return;
139
140   ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
141   myIsActive = theIsActive;
142
143   if (myIsActive) {
144     emit activated();
145     // selection should be cleared after emit of signal to do not process selection change
146     // event by the previous selector
147     // the selection is cleared by activating selection control
148     XGUI_Tools::workshop(myWorkshop)->selector()->clearSelection();
149   }
150   else
151     emit deactivated();
152 }
153
154 //********************************************************************
155 bool XGUI_FacesPanel::useTransparency() const
156 {
157   return myHiddenOrTransparent->isChecked();
158 }
159
160 //********************************************************************
161 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
162 {
163   std::set<int> anIndicesToBeRemoved;
164   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
165     anItemsIt != myItems.end(); anItemsIt++)
166   {
167     ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
168     ObjectPtr anObject = aPrs->object();
169     if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
170       continue;
171     anIndicesToBeRemoved.insert(anItemsIt.key());
172   }
173
174   // remove from myItes container
175   for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
176     aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
177   {
178     myItems.remove(*aToBeRemovedIt);
179   }
180   if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
181     updateProcessedObjects(myItems, myItemObjects);
182   myListView->removeItems(anIndicesToBeRemoved);
183
184   // remove from container of hidden objects
185   for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
186     aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
187   {
188     if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
189       myHiddenObjects.erase(*aHiddenIt);
190   }
191 }
192
193 //********************************************************************
194 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
195 {
196   switch (theActionType) {
197     //case ActionEnter:
198     //  return processEnter();
199     case ActionEscape:
200       setActivePanel(false);
201       return true;
202     case ActionDelete:
203       return processDelete();
204     //case ActionUndo:
205     //case ActionRedo:
206     default:
207       return false;
208   }
209 }
210
211 //********************************************************************
212 void XGUI_FacesPanel::processSelection()
213 {
214   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
215                                                        ModuleBase_ISelection::Viewer);
216   bool isModified = false;
217   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
218
219   std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
220   std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
221   for (int i = 0; i < aSelected.size(); i++) {
222     ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
223     ObjectPtr anObject = aPrs->object();
224     if (!anObject.get())
225       continue;
226     if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
227       continue;
228
229     Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
230       aPrs->interactive());
231     if (aResultPrs.IsNull())
232       continue;
233     QString aItemName = XGUI_Tools::generateName(aPrs);
234     if (myListView->hasItem(aItemName))
235       return;
236
237     myItems.insert(myLastItemIndex, aPrs);
238     myListView->addItem(aItemName, myLastItemIndex);
239     myLastItemIndex++;
240     isModified = true;
241
242     if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
243       anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
244     else {
245       NCollection_List<TopoDS_Shape> aListOfShapes;
246       aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
247       anObjectToShapes[anObject] = aListOfShapes;
248       anObjectToPrs[anObject] = aResultPrs;
249     }
250   }
251   for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
252     anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
253     ObjectPtr anObject = anIt->first;
254     if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
255       continue;
256     Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
257
258     if (aResultPrs->hasSubShapeVisible(anIt->second) || useTransparency()) // redisplay
259       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
260     else { // erase object because it is entirely hidden
261       anObject->setDisplayed(false);
262       myHiddenObjects.insert(anObject);
263       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
264     }
265   }
266   if (isModified) {
267     updateProcessedObjects(myItems, myItemObjects);
268     flushRedisplay();
269   }
270 }
271
272 //********************************************************************
273 bool XGUI_FacesPanel::processDelete()
274 {
275   //appendFirstSelectionInHistory();
276   QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
277
278   std::set<int> aSelectedIds;
279   myListView->getSelectedIndices(aSelectedIds);
280   if (aSelectedIds.empty())
281     return false;
282
283   bool isModified = false;
284   std::set<ObjectPtr> aRestoredObjects;
285   for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
286        anIt++) {
287     ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
288     if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
289       aRestoredObjects.insert(aPrs->object());
290     myItems.remove(*anIt);
291     isModified = true;
292   }
293   if (isModified) {
294     bool isRedisplayed = redisplayObjects(aRestoredObjects);
295     isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects)
296                     || isRedisplayed;
297     if (isRedisplayed)
298       flushRedisplay();
299     // should be after flush of redisplay to have items object to be updated
300     updateProcessedObjects(myItems, myItemObjects);
301   }
302
303   myListView->removeSelectedItems();
304   // Restore selection
305   myListView->restoreSelection(anIndices);
306   //appendSelectionInHistory();
307   return true;
308 }
309
310 //********************************************************************
311 bool XGUI_FacesPanel::redisplayObjects(
312   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
313 {
314   if (theObjects.empty())
315     return false;
316
317   bool isModified = false;
318   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
319   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
320        anIt++)
321   {
322     ObjectPtr anObject = *anIt;
323     if (!anObject->isDisplayed())
324       continue;
325     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
326     isModified = true;
327   }
328   return isModified;
329 }
330
331 //********************************************************************
332 bool XGUI_FacesPanel::displayHiddenObjects(
333   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
334   std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects)
335 {
336   if (theObjects.empty())
337     return false;
338
339   bool isModified = false;
340   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
341
342   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
343        anIt++)
344   {
345     ObjectPtr anObject = *anIt;
346     // if the object was hidden by this panel
347     if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
348       continue;
349     theHiddenObjects.erase(anObject);
350     anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
351     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
352     isModified = true;
353   }
354   return isModified;
355 }
356
357 //********************************************************************
358 bool XGUI_FacesPanel::hideEmptyObjects()
359 {
360   bool isModified = false;
361   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
362   std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
363   std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
364
365   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
366        anIt != myItems.end(); anIt++) {
367     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
368     ObjectPtr anObject = aPrs->object();
369     if (!anObject.get() || !anObject->isDisplayed())
370       continue;
371
372     Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
373       aPrs->interactive());
374     if (aResultPrs.IsNull())
375       continue;
376
377     if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
378       anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
379     else {
380       NCollection_List<TopoDS_Shape> aListOfShapes;
381       aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
382       anObjectToShapes[anObject] = aListOfShapes;
383       anObjectToPrs[anObject] = aResultPrs;
384     }
385   }
386   for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
387     anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
388     ObjectPtr anObject = anIt->first;
389     if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
390       continue;
391     Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
392
393     if (!aResultPrs->hasSubShapeVisible(anIt->second)) {
394       // erase object because it is entirely hidden
395       anObject->setDisplayed(false);
396       myHiddenObjects.insert(anObject);
397       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
398       isModified = true;
399     }
400   }
401   return isModified;
402 }
403
404 //********************************************************************
405 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
406                                              std::set<ObjectPtr>& theObjects)
407 {
408   theObjects.clear();
409   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
410        anIt != theItems.end(); anIt++) {
411     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
412     ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
413     if (anObject.get() && theObjects.find(anObject) != theObjects.end())
414       continue;
415     theObjects.insert(anObject);
416   }
417 }
418
419 //********************************************************************
420 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
421 {
422   QDockWidget::closeEvent(theEvent);
423   emit closed();
424 }
425
426 //********************************************************************
427 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
428                                       const AISObjectPtr& thePresentation)
429 {
430   if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
431     return false;
432
433   if (thePresentation.get() == NULL)
434     return false;
435
436   if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
437     return false;
438
439   // if the object is displayed, the hidden faces are collected and set to the presentation
440   bool isModified = false;
441   NCollection_List<TopoDS_Shape> aHiddenSubShapes;
442   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
443     anIt != myItems.end(); anIt++) {
444     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
445     if (aPrs.get() && aPrs->object() != theObject)
446       continue;
447     TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
448     if (!aHiddenSubShapes.Contains(aShape))
449       aHiddenSubShapes.Append(aShape);
450   }
451
452   Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
453     thePresentation->impl<Handle(AIS_InteractiveObject)>());
454   if (aResultPrs.IsNull())
455     return false;
456
457   isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
458
459   double aTransparency = !useTransparency() ? 1
460     : Config_PropManager::real("Visualization", "hidden_face_transparency");
461   isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
462
463   return isModified;
464 }
465
466 //********************************************************************
467 void XGUI_FacesPanel::onDeleteItem()
468 {
469   processDelete();
470 }
471
472 //********************************************************************
473 void XGUI_FacesPanel::onTransparencyChanged()
474 {
475   bool isModified = false;
476   if (useTransparency()) {
477     std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
478     isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects);
479   }
480   else
481     isModified = hideEmptyObjects();
482
483   isModified = redisplayObjects(myItemObjects) || isModified;
484   if (isModified)
485     flushRedisplay();
486 }
487
488 //********************************************************************
489 void XGUI_FacesPanel::onClosed()
490 {
491   setActivePanel(false);
492   reset(true);
493 }
494
495 //********************************************************************
496 void XGUI_FacesPanel::flushRedisplay() const
497 {
498   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
499   // Necessary for update visibility icons in ObjectBrowser
500   XGUI_ObjectsBrowser* anObjectBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
501   if (anObjectBrowser)
502     anObjectBrowser->updateAllIndexes();
503 }