Salome HOME
c3ab2ec0b096d65e5088c121624f2a552c7b32bc
[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 void XGUI_FacesPanel::selectionModes(QIntList& theModes)
101 {
102   theModes.append(TopAbs_FACE);
103 }
104
105 //********************************************************************
106 void XGUI_FacesPanel::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
107 {
108   ModuleBase_IModule* aModule = myWorkshop->module();
109   QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
110
111   theSelectionFilters.Append(aModule->selectionFilter(SF_GlobalFilter));
112   theSelectionFilters.Append(aModule->selectionFilter(SF_FilterInfinite));
113   theSelectionFilters.Append(aModule->selectionFilter(SF_ResultGroupNameFilter));
114 }
115
116 //********************************************************************
117 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
118 {
119   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
120   if (theEvent->type() == QEvent::MouseButtonRelease)
121   {
122     if (myListView->getControl()->viewport() == aWidget)
123       setActivePanel(true);
124   }
125   // pass the event on to the parent class
126   return QObject::eventFilter(theObject, theEvent);
127 }
128
129 //********************************************************************
130 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
131 {
132   if (myIsActive == theIsActive)
133     return;
134
135   ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
136   myIsActive = theIsActive;
137
138   if (myIsActive) {
139     emit activated();
140     // selection should be cleared after emit of signal to do not process selection change
141     // event by the previous selector
142     // the selection is cleared by activating selection control
143     XGUI_Tools::workshop(myWorkshop)->selector()->clearSelection();
144   }
145   else
146     emit deactivated();
147 }
148
149 //********************************************************************
150 bool XGUI_FacesPanel::useTransparency() const
151 {
152   return myHiddenOrTransparent->isChecked();
153 }
154
155 //********************************************************************
156 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
157 {
158   std::set<int> anIndicesToBeRemoved;
159   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
160     anItemsIt != myItems.end(); anItemsIt++)
161   {
162     ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
163     ObjectPtr anObject = aPrs->object();
164     if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
165       continue;
166     anIndicesToBeRemoved.insert(anItemsIt.key());
167   }
168
169   // remove from myItes container
170   for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
171     aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
172   {
173     myItems.remove(*aToBeRemovedIt);
174   }
175   if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
176     updateProcessedObjects(myItems, myItemObjects);
177   myListView->removeItems(anIndicesToBeRemoved);
178
179   // remove from container of hidden objects
180   for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
181     aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
182   {
183     if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
184       myHiddenObjects.erase(*aHiddenIt);
185   }
186 }
187
188 //********************************************************************
189 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
190 {
191   switch (theActionType) {
192     //case ActionEnter:
193     //  return processEnter();
194     case ActionEscape:
195       setActivePanel(false);
196       return true;
197     case ActionDelete:
198       return processDelete();
199     //case ActionUndo:
200     //case ActionRedo:
201     default:
202       return false;
203   }
204 }
205
206 //********************************************************************
207 void XGUI_FacesPanel::processSelection()
208 {
209   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
210                                                        ModuleBase_ISelection::Viewer);
211   bool isModified = false;
212   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
213
214   std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
215   std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
216   for (int i = 0; i < aSelected.size(); i++) {
217     ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
218     ObjectPtr anObject = aPrs->object();
219     if (!anObject.get())
220       continue;
221     if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
222       continue;
223
224     Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
225       aPrs->interactive());
226     if (aResultPrs.IsNull())
227       continue;
228
229     myItems.insert(myLastItemIndex, aPrs);
230     myListView->addItem(generateName(aPrs), myLastItemIndex);
231     myLastItemIndex++;
232     isModified = true;
233
234     if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
235       anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
236     else {
237       NCollection_List<TopoDS_Shape> aListOfShapes;
238       aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
239       anObjectToShapes[anObject] = aListOfShapes;
240       anObjectToPrs[anObject] = aResultPrs;
241     }
242   }
243   for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator anIt = anObjectToShapes.begin();
244     anIt != anObjectToShapes.end(); anIt++) {
245     ObjectPtr anObject = anIt->first;
246     if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
247       continue;
248     Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
249
250     if (aResultPrs->hasSubShapeVisible(anIt->second) || useTransparency()) // redisplay
251       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
252     else { // erase object because it is entirely hidden
253       anObject->setDisplayed(false);
254       myHiddenObjects.insert(anObject);
255       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
256     }
257   }
258   if (isModified)
259   {
260     updateProcessedObjects(myItems, myItemObjects);
261     Events_Loop::loop()->flush(aDispEvent);
262   }
263 }
264
265 //********************************************************************
266 bool XGUI_FacesPanel::processDelete()
267 {
268   //appendFirstSelectionInHistory();
269   QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
270
271   std::set<int> aSelectedIds;
272   myListView->getSelectedIndices(aSelectedIds);
273   if (aSelectedIds.empty())
274     return false;
275
276   bool isModified = false;
277   std::set<ObjectPtr> aRestoredObjects;
278   for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
279        anIt++) {
280     ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
281     if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
282       aRestoredObjects.insert(aPrs->object());
283     myItems.remove(*anIt);
284     isModified = true;
285   }
286   if (isModified) {
287     bool isRedisplayed = redisplayObjects(aRestoredObjects, false);
288     isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects, false)
289                     || isRedisplayed;
290     if (isRedisplayed)
291       Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
292     // should be after flush of redisplay to have items object to be updated
293     updateProcessedObjects(myItems, myItemObjects);
294   }
295
296   myListView->removeSelectedItems();
297   // Restore selection
298   myListView->restoreSelection(anIndices);
299   //appendSelectionInHistory();
300   return true;
301 }
302
303 //********************************************************************
304 bool XGUI_FacesPanel::redisplayObjects(
305   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
306   const bool isToFlushRedisplay)
307 {
308   if (theObjects.empty())
309     return false;
310
311   bool isModified = false;
312   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
313   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
314        anIt++)
315   {
316     ObjectPtr anObject = *anIt;
317     if (!anObject->isDisplayed())
318       continue;
319     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
320     isModified = true;
321   }
322   if (isModified && isToFlushRedisplay)
323     Events_Loop::loop()->flush(aDispEvent);
324   return isModified;
325 }
326
327 //********************************************************************
328 bool XGUI_FacesPanel::displayHiddenObjects(
329   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
330   std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects,
331   const bool isToFlushRedisplay)
332 {
333   if (theObjects.empty())
334     return false;
335
336   bool isModified = false;
337   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
338
339   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
340        anIt++)
341   {
342     ObjectPtr anObject = *anIt;
343     // if the object was hidden by this panel
344     if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
345       continue;
346     theHiddenObjects.erase(anObject);
347     anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
348     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
349     isModified = true;
350   }
351
352   if (isModified && isToFlushRedisplay)
353     Events_Loop::loop()->flush(aDispEvent);
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 anIt = anObjectToShapes.begin();
387     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 QString XGUI_FacesPanel::generateName(const ModuleBase_ViewerPrsPtr& thePrs)
428 {
429   if (!thePrs.get() || !thePrs->object().get())
430     return "Undefined";
431
432   GeomShapePtr aContext;
433   ObjectPtr anObject = thePrs->object();
434   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
435   if (aResult.get())
436     aContext = aResult->shape();
437   else {
438     // TODO if there is this case
439   }
440
441   QString aName = anObject->data()->name().c_str();
442   if (aContext.get()) {
443     GeomShapePtr aSubShape(new GeomAPI_Shape());
444     aSubShape->setImpl(new TopoDS_Shape(ModuleBase_Tools::getSelectedShape(thePrs)));
445     if (!aSubShape->isEqual(aContext))
446       aName += QString("_%1").arg(GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape));
447   }
448   return aName;
449 }
450
451 //********************************************************************
452 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
453                                       const AISObjectPtr& thePresentation)
454 {
455   if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
456     return false;
457
458   if (thePresentation.get() == NULL)
459     return false;
460
461   if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
462     return false;
463
464   // if the object is displayed, the hidden faces are collected and set to the presentation
465   bool isModified = false;
466   NCollection_List<TopoDS_Shape> aHiddenSubShapes;
467   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
468     anIt != myItems.end(); anIt++) {
469     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
470     if (aPrs.get() && aPrs->object() != theObject)
471       continue;
472     TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
473     if (!aHiddenSubShapes.Contains(aShape))
474       aHiddenSubShapes.Append(aShape);
475   }
476
477   Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
478     thePresentation->impl<Handle(AIS_InteractiveObject)>());
479   if (aResultPrs.IsNull())
480     return false;
481
482   isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
483
484   double aTransparency = !useTransparency() ? 1
485     : Config_PropManager::real("Visualization", "hidden_face_transparency");
486   isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
487
488   return isModified;
489 }
490
491 //********************************************************************
492 void XGUI_FacesPanel::onDeleteItem()
493 {
494   processDelete();
495 }
496
497 //********************************************************************
498 void XGUI_FacesPanel::onTransparencyChanged()
499 {
500   bool isModified = false;
501   if (useTransparency()) {
502     std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
503     isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects, false);
504   }
505   else
506     isModified = hideEmptyObjects();
507
508   isModified = redisplayObjects(myItemObjects, false) || isModified;
509   if (isModified)
510     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
511
512 }
513
514 //********************************************************************
515 void XGUI_FacesPanel::onClosed()
516 {
517   setActivePanel(false);
518   reset(true);
519 }