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_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"
36 #include "XGUI_Tools.h"
37 #include "XGUI_Workshop.h"
41 #include <QFocusEvent>
42 #include <QGridLayout>
43 #include <QListWidget>
44 #include <QMainWindow>
46 static const int LayoutMargin = 3;
48 //********************************************************************
49 XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
50 : QDockWidget(theParent), myIsActive(false), myWorkshop(theWorkshop)
52 setWindowTitle(tr("Hide Faces"));
53 QAction* aViewAct = toggleViewAction();
54 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
56 QWidget* aContent = new QWidget(this);
57 QGridLayout* aMainLayout = new QGridLayout(aContent);
58 aMainLayout->setContentsMargins(LayoutMargin, LayoutMargin, LayoutMargin, LayoutMargin);
61 myHiddenOrTransparent = new QCheckBox(tr("Transparent"), aContent);
62 connect(myHiddenOrTransparent, SIGNAL(toggled(bool)), SLOT(onTransparencyChanged()));
64 myListView = new ModuleBase_ListView(aContent, "", "Hidden/transparent faces in 3D view");
65 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
67 aMainLayout->addWidget(myHiddenOrTransparent, 0, 0);
68 aMainLayout->addWidget(myListView->getControl(), 1, 0);
70 myListView->getControl()->setFocusPolicy(Qt::StrongFocus);
71 myListView->getControl()->viewport()->installEventFilter(this);
74 //********************************************************************
75 void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
77 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
80 // clear internal containers
81 myListView->getControl()->clear();
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)
89 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
91 updateProcessedObjects(myItems, myItemObjects);
92 myHiddenObjects.clear();
93 myLastItemIndex = 0; // it should be after redisplay as flag used in customize
96 //********************************************************************
97 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
99 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
100 if (theEvent->type() == QEvent::MouseButtonRelease)
102 if (myListView->getControl()->viewport() == aWidget)
103 setActivePanel(true);
105 // pass the event on to the parent class
106 return QObject::eventFilter(theObject, theEvent);
109 //********************************************************************
110 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
112 if (myIsActive == theIsActive)
115 ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
116 myIsActive = theIsActive;
121 // selection should be activated after emit signal, that deactivates current widget(selection)
122 activateSelection(theIsActive);
126 // selection should be activated after emit signal, that deactivates current widget(selection)
127 activateSelection(theIsActive);
132 //********************************************************************
133 bool XGUI_FacesPanel::useTransparency() const
135 return myHiddenOrTransparent->isChecked();
138 //********************************************************************
139 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
141 std::set<int> anIndicesToBeRemoved;
142 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
143 anItemsIt != myItems.end(); anItemsIt++)
145 ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
146 ObjectPtr anObject = aPrs->object();
147 if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
149 anIndicesToBeRemoved.insert(anItemsIt.key());
152 // remove from myItes container
153 for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
154 aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
156 myItems.remove(*aToBeRemovedIt);
158 if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
159 updateProcessedObjects(myItems, myItemObjects);
160 myListView->removeItems(anIndicesToBeRemoved);
162 // remove from container of hidden objects
163 for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
164 aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
166 if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
167 myHiddenObjects.erase(*aHiddenIt);
171 //********************************************************************
172 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
174 switch (theActionType) {
176 // return processEnter();
178 setActivePanel(false);
181 return processDelete();
189 //********************************************************************
190 void XGUI_FacesPanel::processSelection()
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();
201 if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
204 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
205 aPrs->interactive());
206 if (aResultPrs.IsNull())
209 myItems.insert(myLastItemIndex, aPrs);
210 myListView->addItem(generateName(aPrs), myLastItemIndex);
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);
225 updateProcessedObjects(myItems, myItemObjects);
226 Events_Loop::loop()->flush(aDispEvent);
230 //********************************************************************
231 bool XGUI_FacesPanel::processDelete()
233 //appendFirstSelectionInHistory();
234 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
236 std::set<int> aSelectedIds;
237 myListView->getSelectedIndices(aSelectedIds);
238 if (aSelectedIds.empty())
241 bool isModified = false;
242 std::set<ObjectPtr> aRestoredObjects;
243 for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
245 ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
246 if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
247 aRestoredObjects.insert(aPrs->object());
248 myItems.remove(*anIt);
252 bool isRedisplayed = redisplayObjects(aRestoredObjects, false);
253 isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects, false)
256 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
257 // should be after flush of redisplay to have items object to be updated
258 updateProcessedObjects(myItems, myItemObjects);
261 myListView->removeSelectedItems();
263 myListView->restoreSelection(anIndices);
264 //appendSelectionInHistory();
268 //********************************************************************
269 bool XGUI_FacesPanel::redisplayObjects(
270 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
271 const bool isToFlushRedisplay)
273 if (theObjects.empty())
276 bool isModified = false;
277 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
278 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
281 ObjectPtr anObject = *anIt;
282 if (!anObject->isDisplayed())
284 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
287 if (isModified && isToFlushRedisplay)
288 Events_Loop::loop()->flush(aDispEvent);
292 //********************************************************************
293 bool XGUI_FacesPanel::displayHiddenObjects(
294 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
295 std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects,
296 const bool isToFlushRedisplay)
298 if (theObjects.empty())
301 bool isModified = false;
302 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
304 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
307 ObjectPtr anObject = *anIt;
308 // if the object was hidden by this panel
309 if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
311 theHiddenObjects.erase(anObject);
312 anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
313 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
317 if (isModified && isToFlushRedisplay)
318 Events_Loop::loop()->flush(aDispEvent);
322 //********************************************************************
323 bool XGUI_FacesPanel::hideEmptyObjects()
325 bool isModified = false;
326 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
327 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
328 anIt != myItems.end(); anIt++) {
329 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
330 ObjectPtr anObject = aPrs->object();
331 if (!anObject.get() || !anObject->isDisplayed())
334 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
335 aPrs->interactive());
336 if (aResultPrs.IsNull())
339 if (!aResultPrs->hasSubShapeVisible(ModuleBase_Tools::getSelectedShape(aPrs))) {
340 // erase object because it is entirely hidden
341 anObject->setDisplayed(false);
342 myHiddenObjects.insert(anObject);
343 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
350 //********************************************************************
351 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
352 std::set<ObjectPtr>& theObjects)
355 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
356 anIt != theItems.end(); anIt++) {
357 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
358 ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
359 if (anObject.get() && theObjects.find(anObject) != theObjects.end())
361 theObjects.insert(anObject);
365 //********************************************************************
366 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
368 QDockWidget::closeEvent(theEvent);
372 //********************************************************************
373 void XGUI_FacesPanel::activateSelection(bool toActivate)
375 QIntList aShapeTypes;
376 aShapeTypes.append(TopAbs_FACE);
379 myWorkshop->activateSubShapesSelection(aShapeTypes);
381 myWorkshop->deactivateSubShapesSelection();
384 activateSelectionFilters();
386 deactivateSelectionFilters();
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);