Salome HOME
Issue #2355 hide all faces then show solid problem
[modules/shaper.git] / src / XGUI / XGUI_FacesPanel.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "XGUI_FacesPanel.h"
22
23 #include <Config_PropManager.h>
24 #include <Events_Loop.h>
25 #include <GeomAlgoAPI_CompoundBuilder.h>
26
27 #include <ModelAPI_Events.h>
28
29 #include <ModuleBase_IModule.h>
30 #include <ModuleBase_ISelection.h>
31 #include "ModuleBase_IWorkshop.h"
32 #include "ModuleBase_ListView.h"
33 #include "ModuleBase_ResultPrs.h"
34 #include "ModuleBase_Tools.h"
35 #include "ModuleBase_ViewerPrs.h"
36
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, false);
89   std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
90   isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects, false) || isModified;
91   if (isModified)// && isToFlushRedisplay) // flush signal immediatelly until container is filled
92     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
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
235     myItems.insert(myLastItemIndex, aPrs);
236     myListView->addItem(generateName(aPrs), myLastItemIndex);
237     myLastItemIndex++;
238     isModified = true;
239
240     if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
241       anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
242     else {
243       NCollection_List<TopoDS_Shape> aListOfShapes;
244       aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
245       anObjectToShapes[anObject] = aListOfShapes;
246       anObjectToPrs[anObject] = aResultPrs;
247     }
248   }
249   for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
250     anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
251     ObjectPtr anObject = anIt->first;
252     if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
253       continue;
254     Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
255
256     if (aResultPrs->hasSubShapeVisible(anIt->second) || useTransparency()) // redisplay
257       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
258     else { // erase object because it is entirely hidden
259       anObject->setDisplayed(false);
260       myHiddenObjects.insert(anObject);
261       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
262     }
263   }
264   if (isModified)
265   {
266     updateProcessedObjects(myItems, myItemObjects);
267     Events_Loop::loop()->flush(aDispEvent);
268   }
269 }
270
271 //********************************************************************
272 bool XGUI_FacesPanel::processDelete()
273 {
274   //appendFirstSelectionInHistory();
275   QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
276
277   std::set<int> aSelectedIds;
278   myListView->getSelectedIndices(aSelectedIds);
279   if (aSelectedIds.empty())
280     return false;
281
282   bool isModified = false;
283   std::set<ObjectPtr> aRestoredObjects;
284   for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
285        anIt++) {
286     ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
287     if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
288       aRestoredObjects.insert(aPrs->object());
289     myItems.remove(*anIt);
290     isModified = true;
291   }
292   if (isModified) {
293     bool isRedisplayed = redisplayObjects(aRestoredObjects, false);
294     isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects, false)
295                     || isRedisplayed;
296     if (isRedisplayed)
297       Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
298     // should be after flush of redisplay to have items object to be updated
299     updateProcessedObjects(myItems, myItemObjects);
300   }
301
302   myListView->removeSelectedItems();
303   // Restore selection
304   myListView->restoreSelection(anIndices);
305   //appendSelectionInHistory();
306   return true;
307 }
308
309 //********************************************************************
310 bool XGUI_FacesPanel::redisplayObjects(
311   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
312   const bool isToFlushRedisplay)
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   if (isModified && isToFlushRedisplay)
329     Events_Loop::loop()->flush(aDispEvent);
330   return isModified;
331 }
332
333 //********************************************************************
334 bool XGUI_FacesPanel::displayHiddenObjects(
335   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
336   std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects,
337   const bool isToFlushRedisplay)
338 {
339   if (theObjects.empty())
340     return false;
341
342   bool isModified = false;
343   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
344
345   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
346        anIt++)
347   {
348     ObjectPtr anObject = *anIt;
349     // if the object was hidden by this panel
350     if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
351       continue;
352     theHiddenObjects.erase(anObject);
353     anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
354     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
355     isModified = true;
356   }
357
358   if (isModified && isToFlushRedisplay)
359     Events_Loop::loop()->flush(aDispEvent);
360   return isModified;
361 }
362
363 //********************************************************************
364 bool XGUI_FacesPanel::hideEmptyObjects()
365 {
366   bool isModified = false;
367   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
368   std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
369   std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
370
371   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
372        anIt != myItems.end(); anIt++) {
373     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
374     ObjectPtr anObject = aPrs->object();
375     if (!anObject.get() || !anObject->isDisplayed())
376       continue;
377
378     Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
379       aPrs->interactive());
380     if (aResultPrs.IsNull())
381       continue;
382
383     if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
384       anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
385     else {
386       NCollection_List<TopoDS_Shape> aListOfShapes;
387       aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
388       anObjectToShapes[anObject] = aListOfShapes;
389       anObjectToPrs[anObject] = aResultPrs;
390     }
391   }
392   for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
393     anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
394     ObjectPtr anObject = anIt->first;
395     if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
396       continue;
397     Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
398
399     if (!aResultPrs->hasSubShapeVisible(anIt->second)) {
400       // erase object because it is entirely hidden
401       anObject->setDisplayed(false);
402       myHiddenObjects.insert(anObject);
403       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
404       isModified = true;
405     }
406   }
407   return isModified;
408 }
409
410 //********************************************************************
411 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
412                                              std::set<ObjectPtr>& theObjects)
413 {
414   theObjects.clear();
415   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
416        anIt != theItems.end(); anIt++) {
417     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
418     ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
419     if (anObject.get() && theObjects.find(anObject) != theObjects.end())
420       continue;
421     theObjects.insert(anObject);
422   }
423 }
424
425 //********************************************************************
426 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
427 {
428   QDockWidget::closeEvent(theEvent);
429   emit closed();
430 }
431
432 //********************************************************************
433 QString XGUI_FacesPanel::generateName(const ModuleBase_ViewerPrsPtr& thePrs)
434 {
435   if (!thePrs.get() || !thePrs->object().get())
436     return "Undefined";
437
438   GeomShapePtr aContext;
439   ObjectPtr anObject = thePrs->object();
440   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
441   if (aResult.get())
442     aContext = aResult->shape();
443   else {
444     // TODO if there is this case
445   }
446
447   QString aName = anObject->data()->name().c_str();
448   if (aContext.get()) {
449     GeomShapePtr aSubShape(new GeomAPI_Shape());
450     aSubShape->setImpl(new TopoDS_Shape(ModuleBase_Tools::getSelectedShape(thePrs)));
451     if (!aSubShape->isEqual(aContext))
452       aName += QString("_%1").arg(GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape));
453   }
454   return aName;
455 }
456
457 //********************************************************************
458 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
459                                       const AISObjectPtr& thePresentation)
460 {
461   if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
462     return false;
463
464   if (thePresentation.get() == NULL)
465     return false;
466
467   if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
468     return false;
469
470   // if the object is displayed, the hidden faces are collected and set to the presentation
471   bool isModified = false;
472   NCollection_List<TopoDS_Shape> aHiddenSubShapes;
473   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
474     anIt != myItems.end(); anIt++) {
475     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
476     if (aPrs.get() && aPrs->object() != theObject)
477       continue;
478     TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
479     if (!aHiddenSubShapes.Contains(aShape))
480       aHiddenSubShapes.Append(aShape);
481   }
482
483   Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
484     thePresentation->impl<Handle(AIS_InteractiveObject)>());
485   if (aResultPrs.IsNull())
486     return false;
487
488   isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
489
490   double aTransparency = !useTransparency() ? 1
491     : Config_PropManager::real("Visualization", "hidden_face_transparency");
492   isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
493
494   return isModified;
495 }
496
497 //********************************************************************
498 void XGUI_FacesPanel::onDeleteItem()
499 {
500   processDelete();
501 }
502
503 //********************************************************************
504 void XGUI_FacesPanel::onTransparencyChanged()
505 {
506   bool isModified = false;
507   if (useTransparency()) {
508     std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
509     isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects, false);
510   }
511   else
512     isModified = hideEmptyObjects();
513
514   isModified = redisplayObjects(myItemObjects, false) || isModified;
515   if (isModified)
516     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
517
518 }
519
520 //********************************************************************
521 void XGUI_FacesPanel::onClosed()
522 {
523   setActivePanel(false);
524   reset(true);
525 }