Salome HOME
Add tutorial help page.
[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     QString aItemName = XGUI_Tools::generateName(aPrs);
236     if (myListView->hasItem(aItemName))
237       return;
238
239     myItems.insert(myLastItemIndex, aPrs);
240     myListView->addItem(aItemName, myLastItemIndex);
241     myLastItemIndex++;
242     isModified = true;
243
244     if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
245       anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
246     else {
247       NCollection_List<TopoDS_Shape> aListOfShapes;
248       aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
249       anObjectToShapes[anObject] = aListOfShapes;
250       anObjectToPrs[anObject] = aResultPrs;
251     }
252   }
253   for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
254     anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
255     ObjectPtr anObject = anIt->first;
256     if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
257       continue;
258     Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
259
260     if (aResultPrs->hasSubShapeVisible(anIt->second) || useTransparency()) // redisplay
261       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
262     else { // erase object because it is entirely hidden
263       anObject->setDisplayed(false);
264       myHiddenObjects.insert(anObject);
265       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
266     }
267   }
268   if (isModified) {
269     updateProcessedObjects(myItems, myItemObjects);
270     flushRedisplay();
271   }
272 }
273
274 //********************************************************************
275 bool XGUI_FacesPanel::processDelete()
276 {
277   //appendFirstSelectionInHistory();
278   QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
279
280   std::set<int> aSelectedIds;
281   myListView->getSelectedIndices(aSelectedIds);
282   if (aSelectedIds.empty())
283     return false;
284
285   bool isModified = false;
286   std::set<ObjectPtr> aRestoredObjects;
287   for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
288        anIt++) {
289     ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
290     if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
291       aRestoredObjects.insert(aPrs->object());
292     myItems.remove(*anIt);
293     isModified = true;
294   }
295   if (isModified) {
296     bool isRedisplayed = redisplayObjects(aRestoredObjects);
297     isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects)
298                     || isRedisplayed;
299     if (isRedisplayed)
300       flushRedisplay();
301     // should be after flush of redisplay to have items object to be updated
302     updateProcessedObjects(myItems, myItemObjects);
303   }
304
305   myListView->removeSelectedItems();
306   // Restore selection
307   myListView->restoreSelection(anIndices);
308   //appendSelectionInHistory();
309   return true;
310 }
311
312 //********************************************************************
313 bool XGUI_FacesPanel::redisplayObjects(
314   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
315 {
316   if (theObjects.empty())
317     return false;
318
319   bool isModified = false;
320   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
321   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
322        anIt++)
323   {
324     ObjectPtr anObject = *anIt;
325     if (!anObject->isDisplayed())
326       continue;
327     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
328     isModified = true;
329   }
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 {
338   if (theObjects.empty())
339     return false;
340
341   bool isModified = false;
342   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
343
344   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
345        anIt++)
346   {
347     ObjectPtr anObject = *anIt;
348     // if the object was hidden by this panel
349     if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
350       continue;
351     theHiddenObjects.erase(anObject);
352     anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
353     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
354     isModified = true;
355   }
356   return isModified;
357 }
358
359 //********************************************************************
360 bool XGUI_FacesPanel::hideEmptyObjects()
361 {
362   bool isModified = false;
363   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
364   std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
365   std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
366
367   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
368        anIt != myItems.end(); anIt++) {
369     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
370     ObjectPtr anObject = aPrs->object();
371     if (!anObject.get() || !anObject->isDisplayed())
372       continue;
373
374     Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
375       aPrs->interactive());
376     if (aResultPrs.IsNull())
377       continue;
378
379     if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
380       anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
381     else {
382       NCollection_List<TopoDS_Shape> aListOfShapes;
383       aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
384       anObjectToShapes[anObject] = aListOfShapes;
385       anObjectToPrs[anObject] = aResultPrs;
386     }
387   }
388   for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
389     anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
390     ObjectPtr anObject = anIt->first;
391     if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
392       continue;
393     Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
394
395     if (!aResultPrs->hasSubShapeVisible(anIt->second)) {
396       // erase object because it is entirely hidden
397       anObject->setDisplayed(false);
398       myHiddenObjects.insert(anObject);
399       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
400       isModified = true;
401     }
402   }
403   return isModified;
404 }
405
406 //********************************************************************
407 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
408                                              std::set<ObjectPtr>& theObjects)
409 {
410   theObjects.clear();
411   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
412        anIt != theItems.end(); anIt++) {
413     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
414     ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
415     if (anObject.get() && theObjects.find(anObject) != theObjects.end())
416       continue;
417     theObjects.insert(anObject);
418   }
419 }
420
421 //********************************************************************
422 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
423 {
424   QDockWidget::closeEvent(theEvent);
425   emit closed();
426 }
427
428 //********************************************************************
429 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
430                                       const AISObjectPtr& thePresentation)
431 {
432   if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
433     return false;
434
435   if (thePresentation.get() == NULL)
436     return false;
437
438   if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
439     return false;
440
441   // if the object is displayed, the hidden faces are collected and set to the presentation
442   bool isModified = false;
443   NCollection_List<TopoDS_Shape> aHiddenSubShapes;
444   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
445     anIt != myItems.end(); anIt++) {
446     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
447     if (aPrs.get() && aPrs->object() != theObject)
448       continue;
449     TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
450     if (!aHiddenSubShapes.Contains(aShape))
451       aHiddenSubShapes.Append(aShape);
452   }
453
454   Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
455     thePresentation->impl<Handle(AIS_InteractiveObject)>());
456   if (aResultPrs.IsNull())
457     return false;
458
459   isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
460
461   double aTransparency = !useTransparency() ? 1
462     : Config_PropManager::real("Visualization", "hidden_face_transparency");
463   isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
464
465   return isModified;
466 }
467
468 //********************************************************************
469 void XGUI_FacesPanel::onDeleteItem()
470 {
471   processDelete();
472 }
473
474 //********************************************************************
475 void XGUI_FacesPanel::onTransparencyChanged()
476 {
477   bool isModified = false;
478   if (useTransparency()) {
479     std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
480     isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects);
481   }
482   else
483     isModified = hideEmptyObjects();
484
485   isModified = redisplayObjects(myItemObjects) || isModified;
486   if (isModified)
487     flushRedisplay();
488 }
489
490 //********************************************************************
491 void XGUI_FacesPanel::onClosed()
492 {
493   setActivePanel(false);
494   reset(true);
495 }
496
497 //********************************************************************
498 void XGUI_FacesPanel::flushRedisplay() const
499 {
500   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
501   // Necessary for update visibility icons in ObjectBrowser
502   XGUI_ObjectsBrowser* anObjectBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
503   if (anObjectBrowser)
504     anObjectBrowser->updateAllIndexes();
505 }