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