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_SelectionMgr.h"
38 #include "XGUI_SelectionFilterType.h"
39 #include "XGUI_Tools.h"
40 #include "XGUI_Workshop.h"
44 #include <QFocusEvent>
45 #include <QGridLayout>
46 #include <QListWidget>
47 #include <QMainWindow>
49 static const int LayoutMargin = 3;
51 //********************************************************************
52 XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
53 : QDockWidget(theParent), myIsActive(false), myWorkshop(theWorkshop)
55 setWindowTitle(tr("Hide Faces"));
56 QAction* aViewAct = toggleViewAction();
57 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
59 QWidget* aContent = new QWidget(this);
60 QGridLayout* aMainLayout = new QGridLayout(aContent);
61 aMainLayout->setContentsMargins(LayoutMargin, LayoutMargin, LayoutMargin, LayoutMargin);
64 myHiddenOrTransparent = new QCheckBox(tr("Transparent"), aContent);
65 connect(myHiddenOrTransparent, SIGNAL(toggled(bool)), SLOT(onTransparencyChanged()));
67 myListView = new ModuleBase_ListView(aContent, "", "Hidden/transparent faces in 3D view");
68 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
70 aMainLayout->addWidget(myHiddenOrTransparent, 0, 0);
71 aMainLayout->addWidget(myListView->getControl(), 1, 0);
73 myListView->getControl()->setFocusPolicy(Qt::StrongFocus);
74 myListView->getControl()->viewport()->installEventFilter(this);
77 //********************************************************************
78 void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
80 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
83 // clear internal containers
84 myListView->getControl()->clear();
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));
94 updateProcessedObjects(myItems, myItemObjects);
95 myHiddenObjects.clear();
96 myLastItemIndex = 0; // it should be after redisplay as flag used in customize
99 //********************************************************************
100 void XGUI_FacesPanel::selectionModes(QIntList& theModes)
102 theModes.append(TopAbs_FACE);
105 //********************************************************************
106 void XGUI_FacesPanel::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
108 ModuleBase_IModule* aModule = myWorkshop->module();
109 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
111 theSelectionFilters.Append(aModule->selectionFilter(SF_GlobalFilter));
112 theSelectionFilters.Append(aModule->selectionFilter(SF_FilterInfinite));
113 theSelectionFilters.Append(aModule->selectionFilter(SF_ResultGroupNameFilter));
116 //********************************************************************
117 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
119 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
120 if (theEvent->type() == QEvent::MouseButtonRelease)
122 if (myListView->getControl()->viewport() == aWidget)
123 setActivePanel(true);
125 // pass the event on to the parent class
126 return QObject::eventFilter(theObject, theEvent);
129 //********************************************************************
130 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
132 if (myIsActive == theIsActive)
135 ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
136 myIsActive = theIsActive;
140 // selection should be cleared after emit of signal to do not process selection change
141 // event by the previous selector
142 // the selection is cleared by activating selection control
143 XGUI_Tools::workshop(myWorkshop)->selector()->clearSelection();
149 //********************************************************************
150 bool XGUI_FacesPanel::useTransparency() const
152 return myHiddenOrTransparent->isChecked();
155 //********************************************************************
156 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
158 std::set<int> anIndicesToBeRemoved;
159 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
160 anItemsIt != myItems.end(); anItemsIt++)
162 ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
163 ObjectPtr anObject = aPrs->object();
164 if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
166 anIndicesToBeRemoved.insert(anItemsIt.key());
169 // remove from myItes container
170 for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
171 aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
173 myItems.remove(*aToBeRemovedIt);
175 if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
176 updateProcessedObjects(myItems, myItemObjects);
177 myListView->removeItems(anIndicesToBeRemoved);
179 // remove from container of hidden objects
180 for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
181 aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
183 if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
184 myHiddenObjects.erase(*aHiddenIt);
188 //********************************************************************
189 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
191 switch (theActionType) {
193 // return processEnter();
195 setActivePanel(false);
198 return processDelete();
206 //********************************************************************
207 void XGUI_FacesPanel::processSelection()
209 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
210 ModuleBase_ISelection::Viewer);
211 bool isModified = false;
212 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
213 for (int i = 0; i < aSelected.size(); i++) {
214 ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
215 ObjectPtr anObject = aPrs->object();
218 if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
221 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
222 aPrs->interactive());
223 if (aResultPrs.IsNull())
226 myItems.insert(myLastItemIndex, aPrs);
227 myListView->addItem(generateName(aPrs), myLastItemIndex);
231 if (aResultPrs->hasSubShapeVisible(ModuleBase_Tools::getSelectedShape(aPrs)) ||
232 useTransparency()) // redisplay
233 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
234 else { // erase object because it is entirely hidden
235 anObject->setDisplayed(false);
236 myHiddenObjects.insert(anObject);
237 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
242 updateProcessedObjects(myItems, myItemObjects);
243 Events_Loop::loop()->flush(aDispEvent);
247 //********************************************************************
248 bool XGUI_FacesPanel::processDelete()
250 //appendFirstSelectionInHistory();
251 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
253 std::set<int> aSelectedIds;
254 myListView->getSelectedIndices(aSelectedIds);
255 if (aSelectedIds.empty())
258 bool isModified = false;
259 std::set<ObjectPtr> aRestoredObjects;
260 for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
262 ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
263 if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
264 aRestoredObjects.insert(aPrs->object());
265 myItems.remove(*anIt);
269 bool isRedisplayed = redisplayObjects(aRestoredObjects, false);
270 isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects, false)
273 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
274 // should be after flush of redisplay to have items object to be updated
275 updateProcessedObjects(myItems, myItemObjects);
278 myListView->removeSelectedItems();
280 myListView->restoreSelection(anIndices);
281 //appendSelectionInHistory();
285 //********************************************************************
286 bool XGUI_FacesPanel::redisplayObjects(
287 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
288 const bool isToFlushRedisplay)
290 if (theObjects.empty())
293 bool isModified = false;
294 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
295 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
298 ObjectPtr anObject = *anIt;
299 if (!anObject->isDisplayed())
301 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
304 if (isModified && isToFlushRedisplay)
305 Events_Loop::loop()->flush(aDispEvent);
309 //********************************************************************
310 bool XGUI_FacesPanel::displayHiddenObjects(
311 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
312 std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects,
313 const bool isToFlushRedisplay)
315 if (theObjects.empty())
318 bool isModified = false;
319 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();
324 ObjectPtr anObject = *anIt;
325 // if the object was hidden by this panel
326 if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
328 theHiddenObjects.erase(anObject);
329 anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
330 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
334 if (isModified && isToFlushRedisplay)
335 Events_Loop::loop()->flush(aDispEvent);
339 //********************************************************************
340 bool XGUI_FacesPanel::hideEmptyObjects()
342 bool isModified = false;
343 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
344 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
345 anIt != myItems.end(); anIt++) {
346 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
347 ObjectPtr anObject = aPrs->object();
348 if (!anObject.get() || !anObject->isDisplayed())
351 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
352 aPrs->interactive());
353 if (aResultPrs.IsNull())
356 if (!aResultPrs->hasSubShapeVisible(ModuleBase_Tools::getSelectedShape(aPrs))) {
357 // erase object because it is entirely hidden
358 anObject->setDisplayed(false);
359 myHiddenObjects.insert(anObject);
360 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
367 //********************************************************************
368 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
369 std::set<ObjectPtr>& theObjects)
372 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
373 anIt != theItems.end(); anIt++) {
374 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
375 ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
376 if (anObject.get() && theObjects.find(anObject) != theObjects.end())
378 theObjects.insert(anObject);
382 //********************************************************************
383 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
385 QDockWidget::closeEvent(theEvent);
389 //********************************************************************
390 QString XGUI_FacesPanel::generateName(const ModuleBase_ViewerPrsPtr& thePrs)
392 if (!thePrs.get() || !thePrs->object().get())
395 GeomShapePtr aContext;
396 ObjectPtr anObject = thePrs->object();
397 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
399 aContext = aResult->shape();
401 // TODO if there is this case
404 QString aName = anObject->data()->name().c_str();
405 if (aContext.get()) {
406 GeomShapePtr aSubShape(new GeomAPI_Shape());
407 aSubShape->setImpl(new TopoDS_Shape(ModuleBase_Tools::getSelectedShape(thePrs)));
408 if (!aSubShape->isEqual(aContext))
409 aName += QString("_%1").arg(GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape));
414 //********************************************************************
415 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
416 const AISObjectPtr& thePresentation)
418 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
421 if (thePresentation.get() == NULL)
424 if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
427 // if the object is displayed, the hidden faces are collected and set to the presentation
428 bool isModified = false;
429 NCollection_List<TopoDS_Shape> aHiddenSubShapes;
430 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
431 anIt != myItems.end(); anIt++) {
432 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
433 if (aPrs.get() && aPrs->object() != theObject)
435 TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
436 if (!aHiddenSubShapes.Contains(aShape))
437 aHiddenSubShapes.Append(aShape);
440 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
441 thePresentation->impl<Handle(AIS_InteractiveObject)>());
442 if (aResultPrs.IsNull())
445 isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
447 double aTransparency = !useTransparency() ? 1
448 : Config_PropManager::real("Visualization", "hidden_face_transparency");
449 isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
454 //********************************************************************
455 void XGUI_FacesPanel::onDeleteItem()
460 //********************************************************************
461 void XGUI_FacesPanel::onTransparencyChanged()
463 bool isModified = false;
464 if (useTransparency()) {
465 std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
466 isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects, false);
469 isModified = hideEmptyObjects();
471 isModified = redisplayObjects(myItemObjects, false) || isModified;
473 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
477 //********************************************************************
478 void XGUI_FacesPanel::onClosed()
480 setActivePanel(false);