]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_FacesPanel.cpp
Salome HOME
6c1e40370672d9f413acc44158bc470b25fd28d1
[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     // the selection is cleared by activating selection control
140     XGUI_Tools::workshop(myWorkshop)->selector()->clearSelection();
141     emit activated();
142   }
143   else
144     emit deactivated();
145 }
146
147 //********************************************************************
148 bool XGUI_FacesPanel::useTransparency() const
149 {
150   return myHiddenOrTransparent->isChecked();
151 }
152
153 //********************************************************************
154 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
155 {
156   std::set<int> anIndicesToBeRemoved;
157   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
158     anItemsIt != myItems.end(); anItemsIt++)
159   {
160     ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
161     ObjectPtr anObject = aPrs->object();
162     if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
163       continue;
164     anIndicesToBeRemoved.insert(anItemsIt.key());
165   }
166
167   // remove from myItes container
168   for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
169     aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
170   {
171     myItems.remove(*aToBeRemovedIt);
172   }
173   if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
174     updateProcessedObjects(myItems, myItemObjects);
175   myListView->removeItems(anIndicesToBeRemoved);
176
177   // remove from container of hidden objects
178   for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
179     aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
180   {
181     if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
182       myHiddenObjects.erase(*aHiddenIt);
183   }
184 }
185
186 //********************************************************************
187 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
188 {
189   switch (theActionType) {
190     //case ActionEnter:
191     //  return processEnter();
192     case ActionEscape:
193       setActivePanel(false);
194       return true;
195     case ActionDelete:
196       return processDelete();
197     //case ActionUndo:
198     //case ActionRedo:
199     default:
200       return false;
201   }
202 }
203
204 //********************************************************************
205 void XGUI_FacesPanel::processSelection()
206 {
207   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
208                                                        ModuleBase_ISelection::Viewer);
209   bool isModified = false;
210   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
211   for (int i = 0; i < aSelected.size(); i++) {
212     ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
213     ObjectPtr anObject = aPrs->object();
214     if (!anObject.get())
215       continue;
216     if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
217       continue;
218
219     Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
220       aPrs->interactive());
221     if (aResultPrs.IsNull())
222       continue;
223
224     myItems.insert(myLastItemIndex, aPrs);
225     myListView->addItem(generateName(aPrs), myLastItemIndex);
226     myLastItemIndex++;
227     isModified = true;
228
229     if (aResultPrs->hasSubShapeVisible(ModuleBase_Tools::getSelectedShape(aPrs)) ||
230         useTransparency()) // redisplay
231       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
232     else { // erase object because it is entirely hidden
233       anObject->setDisplayed(false);
234       myHiddenObjects.insert(anObject);
235       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
236     }
237   }
238   if (isModified)
239   {
240     updateProcessedObjects(myItems, myItemObjects);
241     Events_Loop::loop()->flush(aDispEvent);
242   }
243 }
244
245 //********************************************************************
246 bool XGUI_FacesPanel::processDelete()
247 {
248   //appendFirstSelectionInHistory();
249   QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
250
251   std::set<int> aSelectedIds;
252   myListView->getSelectedIndices(aSelectedIds);
253   if (aSelectedIds.empty())
254     return false;
255
256   bool isModified = false;
257   std::set<ObjectPtr> aRestoredObjects;
258   for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
259        anIt++) {
260     ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
261     if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
262       aRestoredObjects.insert(aPrs->object());
263     myItems.remove(*anIt);
264     isModified = true;
265   }
266   if (isModified) {
267     bool isRedisplayed = redisplayObjects(aRestoredObjects, false);
268     isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects, false)
269                     || isRedisplayed;
270     if (isRedisplayed)
271       Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
272     // should be after flush of redisplay to have items object to be updated
273     updateProcessedObjects(myItems, myItemObjects);
274   }
275
276   myListView->removeSelectedItems();
277   // Restore selection
278   myListView->restoreSelection(anIndices);
279   //appendSelectionInHistory();
280   return true;
281 }
282
283 //********************************************************************
284 bool XGUI_FacesPanel::redisplayObjects(
285   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
286   const bool isToFlushRedisplay)
287 {
288   if (theObjects.empty())
289     return false;
290
291   bool isModified = false;
292   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
293   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
294        anIt++)
295   {
296     ObjectPtr anObject = *anIt;
297     if (!anObject->isDisplayed())
298       continue;
299     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
300     isModified = true;
301   }
302   if (isModified && isToFlushRedisplay)
303     Events_Loop::loop()->flush(aDispEvent);
304   return isModified;
305 }
306
307 //********************************************************************
308 bool XGUI_FacesPanel::displayHiddenObjects(
309   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
310   std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects,
311   const bool isToFlushRedisplay)
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
319   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
320        anIt++)
321   {
322     ObjectPtr anObject = *anIt;
323     // if the object was hidden by this panel
324     if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
325       continue;
326     theHiddenObjects.erase(anObject);
327     anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
328     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
329     isModified = true;
330   }
331
332   if (isModified && isToFlushRedisplay)
333     Events_Loop::loop()->flush(aDispEvent);
334   return isModified;
335 }
336
337 //********************************************************************
338 bool XGUI_FacesPanel::hideEmptyObjects()
339 {
340   bool isModified = false;
341   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
342   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
343        anIt != myItems.end(); anIt++) {
344     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
345     ObjectPtr anObject = aPrs->object();
346     if (!anObject.get() || !anObject->isDisplayed())
347       continue;
348
349     Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
350       aPrs->interactive());
351     if (aResultPrs.IsNull())
352       continue;
353
354     if (!aResultPrs->hasSubShapeVisible(ModuleBase_Tools::getSelectedShape(aPrs))) {
355       // erase object because it is entirely hidden
356       anObject->setDisplayed(false);
357       myHiddenObjects.insert(anObject);
358       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
359       isModified = true;
360     }
361   }
362   return isModified;
363 }
364
365 //********************************************************************
366 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
367                                              std::set<ObjectPtr>& theObjects)
368 {
369   theObjects.clear();
370   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
371        anIt != theItems.end(); anIt++) {
372     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
373     ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
374     if (anObject.get() && theObjects.find(anObject) != theObjects.end())
375       continue;
376     theObjects.insert(anObject);
377   }
378 }
379
380 //********************************************************************
381 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
382 {
383   QDockWidget::closeEvent(theEvent);
384   emit closed();
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, myHiddenObjects, 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 }