1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_FacesPanel.h"
23 #include <Config_PropManager.h>
24 #include <Events_Loop.h>
25 #include <GeomAlgoAPI_CompoundBuilder.h>
27 #include <ModelAPI_Events.h>
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"
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"
45 #include <QFocusEvent>
46 #include <QGridLayout>
47 #include <QListWidget>
48 #include <QMainWindow>
50 static const int LayoutMargin = 3;
52 //********************************************************************
53 XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
54 : QDockWidget(theParent), myIsActive(false), myWorkshop(theWorkshop)
56 setWindowTitle(tr("Hide Faces"));
57 QAction* aViewAct = toggleViewAction();
58 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
60 QWidget* aContent = new QWidget(this);
61 QGridLayout* aMainLayout = new QGridLayout(aContent);
62 aMainLayout->setContentsMargins(LayoutMargin, LayoutMargin, LayoutMargin, LayoutMargin);
65 myHiddenOrTransparent = new QCheckBox(tr("Transparent"), aContent);
66 connect(myHiddenOrTransparent, SIGNAL(toggled(bool)), SLOT(onTransparencyChanged()));
68 myListView = new ModuleBase_ListView(aContent, "", "Hidden/transparent faces in 3D view");
69 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
71 aMainLayout->addWidget(myHiddenOrTransparent, 0, 0);
72 aMainLayout->addWidget(myListView->getControl(), 1, 0);
74 myListView->getControl()->setFocusPolicy(Qt::StrongFocus);
75 myListView->getControl()->viewport()->installEventFilter(this);
78 //********************************************************************
79 void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
81 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
84 // clear internal containers
85 myListView->getControl()->clear();
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
95 updateProcessedObjects(myItems, myItemObjects);
96 myHiddenObjects.clear();
97 myLastItemIndex = 0; // it should be after redisplay as flag used in customize
100 //********************************************************************
101 bool XGUI_FacesPanel::isEmpty() const
103 return myItems.size() == 0;
106 //********************************************************************
107 void XGUI_FacesPanel::selectionModes(QIntList& theModes)
109 theModes.append(TopAbs_FACE);
112 //********************************************************************
113 void XGUI_FacesPanel::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
115 ModuleBase_IModule* aModule = myWorkshop->module();
116 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
118 theSelectionFilters.Append(aModule->selectionFilter(SF_GlobalFilter));
119 theSelectionFilters.Append(aModule->selectionFilter(SF_FilterInfinite));
120 theSelectionFilters.Append(aModule->selectionFilter(SF_ResultGroupNameFilter));
123 //********************************************************************
124 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
126 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
127 if (theEvent->type() == QEvent::MouseButtonRelease)
129 if (myListView->getControl()->viewport() == aWidget)
130 setActivePanel(true);
132 // pass the event on to the parent class
133 return QObject::eventFilter(theObject, theEvent);
136 //********************************************************************
137 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
139 if (myIsActive == theIsActive)
142 ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
143 myIsActive = theIsActive;
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();
156 //********************************************************************
157 bool XGUI_FacesPanel::useTransparency() const
159 return myHiddenOrTransparent->isChecked();
162 //********************************************************************
163 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
165 std::set<int> anIndicesToBeRemoved;
166 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
167 anItemsIt != myItems.end(); anItemsIt++)
169 ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
170 ObjectPtr anObject = aPrs->object();
171 if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
173 anIndicesToBeRemoved.insert(anItemsIt.key());
176 // remove from myItes container
177 for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
178 aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
180 myItems.remove(*aToBeRemovedIt);
182 if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
183 updateProcessedObjects(myItems, myItemObjects);
184 myListView->removeItems(anIndicesToBeRemoved);
186 // remove from container of hidden objects
187 for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
188 aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
190 if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
191 myHiddenObjects.erase(*aHiddenIt);
195 //********************************************************************
196 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
198 switch (theActionType) {
200 // return processEnter();
202 setActivePanel(false);
205 return processDelete();
213 //********************************************************************
214 void XGUI_FacesPanel::processSelection()
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);
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();
228 if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
231 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
232 aPrs->interactive());
233 if (aResultPrs.IsNull())
236 myItems.insert(myLastItemIndex, aPrs);
237 myListView->addItem(generateName(aPrs), myLastItemIndex);
241 if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
242 anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
244 NCollection_List<TopoDS_Shape> aListOfShapes;
245 aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
246 anObjectToShapes[anObject] = aListOfShapes;
247 anObjectToPrs[anObject] = aResultPrs;
250 for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
251 anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
252 ObjectPtr anObject = anIt->first;
253 if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
255 Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
257 if (aResultPrs->hasSubShapeVisible(anIt->second) || useTransparency()) // redisplay
258 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
259 else { // erase object because it is entirely hidden
260 anObject->setDisplayed(false);
261 myHiddenObjects.insert(anObject);
262 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
266 updateProcessedObjects(myItems, myItemObjects);
271 //********************************************************************
272 bool XGUI_FacesPanel::processDelete()
274 //appendFirstSelectionInHistory();
275 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
277 std::set<int> aSelectedIds;
278 myListView->getSelectedIndices(aSelectedIds);
279 if (aSelectedIds.empty())
282 bool isModified = false;
283 std::set<ObjectPtr> aRestoredObjects;
284 for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
286 ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
287 if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
288 aRestoredObjects.insert(aPrs->object());
289 myItems.remove(*anIt);
293 bool isRedisplayed = redisplayObjects(aRestoredObjects);
294 isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects)
298 // should be after flush of redisplay to have items object to be updated
299 updateProcessedObjects(myItems, myItemObjects);
302 myListView->removeSelectedItems();
304 myListView->restoreSelection(anIndices);
305 //appendSelectionInHistory();
309 //********************************************************************
310 bool XGUI_FacesPanel::redisplayObjects(
311 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
313 if (theObjects.empty())
316 bool isModified = false;
317 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
318 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
321 ObjectPtr anObject = *anIt;
322 if (!anObject->isDisplayed())
324 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
330 //********************************************************************
331 bool XGUI_FacesPanel::displayHiddenObjects(
332 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
333 std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects)
335 if (theObjects.empty())
338 bool isModified = false;
339 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
341 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
344 ObjectPtr anObject = *anIt;
345 // if the object was hidden by this panel
346 if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
348 theHiddenObjects.erase(anObject);
349 anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
350 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
356 //********************************************************************
357 bool XGUI_FacesPanel::hideEmptyObjects()
359 bool isModified = false;
360 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
361 std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
362 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
364 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
365 anIt != myItems.end(); anIt++) {
366 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
367 ObjectPtr anObject = aPrs->object();
368 if (!anObject.get() || !anObject->isDisplayed())
371 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
372 aPrs->interactive());
373 if (aResultPrs.IsNull())
376 if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
377 anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
379 NCollection_List<TopoDS_Shape> aListOfShapes;
380 aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
381 anObjectToShapes[anObject] = aListOfShapes;
382 anObjectToPrs[anObject] = aResultPrs;
385 for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
386 anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
387 ObjectPtr anObject = anIt->first;
388 if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
390 Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
392 if (!aResultPrs->hasSubShapeVisible(anIt->second)) {
393 // erase object because it is entirely hidden
394 anObject->setDisplayed(false);
395 myHiddenObjects.insert(anObject);
396 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
403 //********************************************************************
404 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
405 std::set<ObjectPtr>& theObjects)
408 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
409 anIt != theItems.end(); anIt++) {
410 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
411 ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
412 if (anObject.get() && theObjects.find(anObject) != theObjects.end())
414 theObjects.insert(anObject);
418 //********************************************************************
419 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
421 QDockWidget::closeEvent(theEvent);
425 //********************************************************************
426 QString XGUI_FacesPanel::generateName(const ModuleBase_ViewerPrsPtr& thePrs)
428 if (!thePrs.get() || !thePrs->object().get())
431 GeomShapePtr aContext;
432 ObjectPtr anObject = thePrs->object();
433 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
435 aContext = aResult->shape();
437 // TODO if there is this case
440 QString aName = anObject->data()->name().c_str();
441 if (aContext.get()) {
442 GeomShapePtr aSubShape(new GeomAPI_Shape());
443 aSubShape->setImpl(new TopoDS_Shape(ModuleBase_Tools::getSelectedShape(thePrs)));
444 if (!aSubShape->isEqual(aContext))
445 aName += QString("_%1").arg(GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape));
450 //********************************************************************
451 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
452 const AISObjectPtr& thePresentation)
454 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
457 if (thePresentation.get() == NULL)
460 if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
463 // if the object is displayed, the hidden faces are collected and set to the presentation
464 bool isModified = false;
465 NCollection_List<TopoDS_Shape> aHiddenSubShapes;
466 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
467 anIt != myItems.end(); anIt++) {
468 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
469 if (aPrs.get() && aPrs->object() != theObject)
471 TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
472 if (!aHiddenSubShapes.Contains(aShape))
473 aHiddenSubShapes.Append(aShape);
476 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
477 thePresentation->impl<Handle(AIS_InteractiveObject)>());
478 if (aResultPrs.IsNull())
481 isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
483 double aTransparency = !useTransparency() ? 1
484 : Config_PropManager::real("Visualization", "hidden_face_transparency");
485 isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
490 //********************************************************************
491 void XGUI_FacesPanel::onDeleteItem()
496 //********************************************************************
497 void XGUI_FacesPanel::onTransparencyChanged()
499 bool isModified = false;
500 if (useTransparency()) {
501 std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
502 isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects);
505 isModified = hideEmptyObjects();
507 isModified = redisplayObjects(myItemObjects) || isModified;
512 //********************************************************************
513 void XGUI_FacesPanel::onClosed()
515 setActivePanel(false);
519 //********************************************************************
520 void XGUI_FacesPanel::flushRedisplay() const
522 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
523 // Necessary for update visibility icons in ObjectBrowser
524 XGUI_ObjectsBrowser* anObjectBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
526 anObjectBrowser->updateAllIndexes();