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