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