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