Salome HOME
Activation objects redesign.
[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, myHiddenObjects, false) || isModified;
88   if (isModified)// && isToFlushRedisplay) // flush signal immediatelly until container is filled
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 void XGUI_FacesPanel::selectionModes(QIntList& theModes)
98 {
99   theModes.append(TopAbs_FACE);
100 }
101
102 //********************************************************************
103 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
104 {
105   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
106   if (theEvent->type() == QEvent::MouseButtonRelease)
107   {
108     if (myListView->getControl()->viewport() == aWidget)
109       setActivePanel(true);
110   }
111   // pass the event on to the parent class
112   return QObject::eventFilter(theObject, theEvent);
113 }
114
115 //********************************************************************
116 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
117 {
118   if (myIsActive == theIsActive)
119     return;
120
121   ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
122   myIsActive = theIsActive;
123
124   if (myIsActive)
125     emit activated();
126   else
127     emit deactivated();
128 }
129
130 //********************************************************************
131 bool XGUI_FacesPanel::useTransparency() const
132 {
133   return myHiddenOrTransparent->isChecked();
134 }
135
136 //********************************************************************
137 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
138 {
139   std::set<int> anIndicesToBeRemoved;
140   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
141     anItemsIt != myItems.end(); anItemsIt++)
142   {
143     ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
144     ObjectPtr anObject = aPrs->object();
145     if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
146       continue;
147     anIndicesToBeRemoved.insert(anItemsIt.key());
148   }
149
150   // remove from myItes container
151   for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
152     aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
153   {
154     myItems.remove(*aToBeRemovedIt);
155   }
156   if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
157     updateProcessedObjects(myItems, myItemObjects);
158   myListView->removeItems(anIndicesToBeRemoved);
159
160   // remove from container of hidden objects
161   for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
162     aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
163   {
164     if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
165       myHiddenObjects.erase(*aHiddenIt);
166   }
167 }
168
169 //********************************************************************
170 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
171 {
172   switch (theActionType) {
173     //case ActionEnter:
174     //  return processEnter();
175     case ActionEscape:
176       setActivePanel(false);
177       return true;
178     case ActionDelete:
179       return processDelete();
180     //case ActionUndo:
181     //case ActionRedo:
182     default:
183       return false;
184   }
185 }
186
187 //********************************************************************
188 void XGUI_FacesPanel::processSelection()
189 {
190   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
191                                                        ModuleBase_ISelection::Viewer);
192   bool isModified = false;
193   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
194   for (int i = 0; i < aSelected.size(); i++) {
195     ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
196     ObjectPtr anObject = aPrs->object();
197     if (!anObject.get())
198       continue;
199     if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
200       continue;
201
202     Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
203       aPrs->interactive());
204     if (aResultPrs.IsNull())
205       continue;
206
207     myItems.insert(myLastItemIndex, aPrs);
208     myListView->addItem(generateName(aPrs), myLastItemIndex);
209     myLastItemIndex++;
210     isModified = true;
211
212     if (aResultPrs->hasSubShapeVisible(ModuleBase_Tools::getSelectedShape(aPrs)) ||
213         useTransparency()) // redisplay
214       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
215     else { // erase object because it is entirely hidden
216       anObject->setDisplayed(false);
217       myHiddenObjects.insert(anObject);
218       ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
219     }
220   }
221   if (isModified)
222   {
223     updateProcessedObjects(myItems, myItemObjects);
224     Events_Loop::loop()->flush(aDispEvent);
225   }
226 }
227
228 //********************************************************************
229 bool XGUI_FacesPanel::processDelete()
230 {
231   //appendFirstSelectionInHistory();
232   QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
233
234   std::set<int> aSelectedIds;
235   myListView->getSelectedIndices(aSelectedIds);
236   if (aSelectedIds.empty())
237     return false;
238
239   bool isModified = false;
240   std::set<ObjectPtr> aRestoredObjects;
241   for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
242        anIt++) {
243     ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
244     if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
245       aRestoredObjects.insert(aPrs->object());
246     myItems.remove(*anIt);
247     isModified = true;
248   }
249   if (isModified) {
250     bool isRedisplayed = redisplayObjects(aRestoredObjects, false);
251     isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects, false)
252                     || isRedisplayed;
253     if (isRedisplayed)
254       Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
255     // should be after flush of redisplay to have items object to be updated
256     updateProcessedObjects(myItems, myItemObjects);
257   }
258
259   myListView->removeSelectedItems();
260   // Restore selection
261   myListView->restoreSelection(anIndices);
262   //appendSelectionInHistory();
263   return true;
264 }
265
266 //********************************************************************
267 bool XGUI_FacesPanel::redisplayObjects(
268   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
269   const bool isToFlushRedisplay)
270 {
271   if (theObjects.empty())
272     return false;
273
274   bool isModified = false;
275   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
276   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
277        anIt++)
278   {
279     ObjectPtr anObject = *anIt;
280     if (!anObject->isDisplayed())
281       continue;
282     ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
283     isModified = true;
284   }
285   if (isModified && isToFlushRedisplay)
286     Events_Loop::loop()->flush(aDispEvent);
287   return isModified;
288 }
289
290 //********************************************************************
291 bool XGUI_FacesPanel::displayHiddenObjects(
292   const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
293   std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects,
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() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
308       continue;
309     theHiddenObjects.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 QString XGUI_FacesPanel::generateName(const ModuleBase_ViewerPrsPtr& thePrs)
372 {
373   if (!thePrs.get() || !thePrs->object().get())
374     return "Undefined";
375
376   GeomShapePtr aContext;
377   ObjectPtr anObject = thePrs->object();
378   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
379   if (aResult.get())
380     aContext = aResult->shape();
381   else {
382     // TODO if there is this case
383   }
384
385   QString aName = anObject->data()->name().c_str();
386   if (aContext.get()) {
387     GeomShapePtr aSubShape(new GeomAPI_Shape());
388     aSubShape->setImpl(new TopoDS_Shape(ModuleBase_Tools::getSelectedShape(thePrs)));
389     if (!aSubShape->isEqual(aContext))
390       aName += QString("_%1").arg(GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape));
391   }
392   return aName;
393 }
394
395 //********************************************************************
396 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
397                                       const AISObjectPtr& thePresentation)
398 {
399   if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
400     return false;
401
402   if (thePresentation.get() == NULL)
403     return false;
404
405   if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
406     return false;
407
408   // if the object is displayed, the hidden faces are collected and set to the presentation
409   bool isModified = false;
410   NCollection_List<TopoDS_Shape> aHiddenSubShapes;
411   for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
412     anIt != myItems.end(); anIt++) {
413     ModuleBase_ViewerPrsPtr aPrs = anIt.value();
414     if (aPrs.get() && aPrs->object() != theObject)
415       continue;
416     TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
417     if (!aHiddenSubShapes.Contains(aShape))
418       aHiddenSubShapes.Append(aShape);
419   }
420
421   Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
422     thePresentation->impl<Handle(AIS_InteractiveObject)>());
423   if (aResultPrs.IsNull())
424     return false;
425
426   isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
427
428   double aTransparency = !useTransparency() ? 1
429     : Config_PropManager::real("Visualization", "hidden_face_transparency");
430   isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
431
432   return isModified;
433 }
434
435 //********************************************************************
436 void XGUI_FacesPanel::onDeleteItem()
437 {
438   processDelete();
439 }
440
441 //********************************************************************
442 void XGUI_FacesPanel::onTransparencyChanged()
443 {
444   bool isModified = false;
445   if (useTransparency()) {
446     std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
447     isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects, false);
448   }
449   else
450     isModified = hideEmptyObjects();
451
452   isModified = redisplayObjects(myItemObjects, false) || isModified;
453   if (isModified)
454     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
455
456 }
457
458 //********************************************************************
459 void XGUI_FacesPanel::onClosed()
460 {
461   setActivePanel(false);
462   reset(true);
463 }