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 // clear internal containers
78 myListView->getControl()->clear();
81 // restore previous view of presentations
82 bool isModified = redisplayObjects(myItemObjects, false);
83 isModified = displayHiddenObjects(myHiddenObjects, false) || isModified;
84 if (isModified && isToFlushRedisplay)
85 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
87 updateProcessedObjects(myItems, myItemObjects);
88 myHiddenObjects.clear();
89 myLastItemIndex = 0; // it should be after redisplay as flag used in customize
92 //********************************************************************
93 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
95 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
96 if (theEvent->type() == QEvent::MouseButtonRelease)
98 if (myListView->getControl()->viewport() == aWidget)
101 // pass the event on to the parent class
102 return QObject::eventFilter(theObject, theEvent);
105 //********************************************************************
106 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
108 if (myIsActive == theIsActive)
111 ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
112 myIsActive = theIsActive;
117 // selection should be activated after emit signal, that deactivates current widget(selection)
118 activateSelection(theIsActive);
122 // selection should be activated after emit signal, that deactivates current widget(selection)
123 activateSelection(theIsActive);
128 //********************************************************************
129 bool XGUI_FacesPanel::useTransparency() const
131 return myHiddenOrTransparent->isChecked();
134 //********************************************************************
135 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
137 std::set<int> anIndicesToBeRemoved;
138 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
139 anItemsIt != myItems.end(); anItemsIt++)
141 ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
142 ObjectPtr anObject = aPrs->object();
143 if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
145 anIndicesToBeRemoved.insert(anItemsIt.key());
148 // remove from myItes container
149 for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
150 aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
152 myItems.remove(*aToBeRemovedIt);
154 if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
155 updateProcessedObjects(myItems, myItemObjects);
156 myListView->removeItems(anIndicesToBeRemoved);
158 // remove from container of hidden objects
159 for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
160 aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
162 if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
163 myHiddenObjects.erase(*aHiddenIt);
167 //********************************************************************
168 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
170 switch (theActionType) {
172 // return processEnter();
174 setActivePanel(false);
177 return processDelete();
185 //********************************************************************
186 void XGUI_FacesPanel::processSelection()
188 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
189 ModuleBase_ISelection::Viewer);
190 bool isModified = false;
191 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
192 for (int i = 0; i < aSelected.size(); i++) {
193 ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
194 ObjectPtr anObject = aPrs->object();
197 if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
200 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
201 aPrs->interactive());
202 if (aResultPrs.IsNull())
205 myItems.insert(myLastItemIndex, aPrs);
206 myListView->addItem(generateName(aPrs), myLastItemIndex);
210 if (aResultPrs->hasSubShapeVisible(ModuleBase_Tools::getSelectedShape(aPrs))) // redisplay
211 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
212 else { // erase object because it is entirely hidden
213 anObject->setDisplayed(false);
214 myHiddenObjects.insert(anObject);
215 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
220 updateProcessedObjects(myItems, myItemObjects);
221 Events_Loop::loop()->flush(aDispEvent);
225 //********************************************************************
226 bool XGUI_FacesPanel::processDelete()
228 //appendFirstSelectionInHistory();
229 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
231 std::set<int> aSelectedIds;
232 myListView->getSelectedIndices(aSelectedIds);
233 if (aSelectedIds.empty())
236 bool isModified = false;
237 std::set<ObjectPtr> aRestoredObjects;
238 for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
240 ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
241 if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
242 aRestoredObjects.insert(aPrs->object());
243 myItems.remove(*anIt);
247 bool isRedisplayed = redisplayObjects(aRestoredObjects, false);
248 isRedisplayed = displayHiddenObjects(aRestoredObjects, false) || isRedisplayed;
250 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
251 // should be after flush of redisplay to have items object to be updated
252 updateProcessedObjects(myItems, myItemObjects);
255 myListView->removeSelectedItems();
257 myListView->restoreSelection(anIndices);
258 //appendSelectionInHistory();
262 //********************************************************************
263 bool XGUI_FacesPanel::redisplayObjects(
264 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
265 const bool isToFlushRedisplay)
267 if (theObjects.empty())
270 bool isModified = false;
271 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
272 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
275 ObjectPtr anObject = *anIt;
276 if (!anObject->isDisplayed())
278 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
281 if (isModified && isToFlushRedisplay)
282 Events_Loop::loop()->flush(aDispEvent);
286 //********************************************************************
287 bool XGUI_FacesPanel::displayHiddenObjects(
288 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
289 const bool isToFlushRedisplay)
291 if (theObjects.empty())
294 bool isModified = false;
295 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
297 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
300 ObjectPtr anObject = *anIt;
301 // if the object was hidden by this panel
302 if (anObject->isDisplayed() || myHiddenObjects.find(anObject) == myHiddenObjects.end())
304 myHiddenObjects.erase(anObject);
305 anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
306 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
310 if (isModified && isToFlushRedisplay)
311 Events_Loop::loop()->flush(aDispEvent);
316 //********************************************************************
317 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
318 std::set<ObjectPtr>& theObjects)
321 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
322 anIt != theItems.end(); anIt++) {
323 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
324 ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
325 if (anObject.get() && theObjects.find(anObject) != theObjects.end())
327 theObjects.insert(anObject);
331 //********************************************************************
332 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
334 QDockWidget::closeEvent(theEvent);
338 //********************************************************************
339 void XGUI_FacesPanel::activateSelection(bool toActivate)
341 QIntList aShapeTypes;
342 aShapeTypes.append(TopAbs_FACE);
345 myWorkshop->activateSubShapesSelection(aShapeTypes);
347 myWorkshop->deactivateSubShapesSelection();
350 activateSelectionFilters();
352 deactivateSelectionFilters();
355 //********************************************************************
356 QString XGUI_FacesPanel::generateName(const ModuleBase_ViewerPrsPtr& thePrs)
358 if (!thePrs.get() || !thePrs->object().get())
361 GeomShapePtr aContext;
362 ObjectPtr anObject = thePrs->object();
363 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
365 aContext = aResult->shape();
367 // TODO if there is this case
370 QString aName = anObject->data()->name().c_str();
371 if (aContext.get()) {
372 GeomShapePtr aSubShape(new GeomAPI_Shape());
373 aSubShape->setImpl(new TopoDS_Shape(ModuleBase_Tools::getSelectedShape(thePrs)));
374 if (!aSubShape->isEqual(aContext))
375 aName += QString("_%1").arg(GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape));
380 //********************************************************************
381 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
382 const AISObjectPtr& thePresentation)
384 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
387 if (thePresentation.get() == NULL)
390 if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
393 // if the object is displayed, the hidden faces are collected and set to the presentation
394 bool isModified = false;
395 NCollection_List<TopoDS_Shape> aHiddenSubShapes;
396 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
397 anIt != myItems.end(); anIt++) {
398 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
399 if (aPrs.get() && aPrs->object() != theObject)
401 TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
402 if (!aHiddenSubShapes.Contains(aShape))
403 aHiddenSubShapes.Append(aShape);
406 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
407 thePresentation->impl<Handle(AIS_InteractiveObject)>());
408 if (aResultPrs.IsNull())
411 isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
413 double aTransparency = !useTransparency() ? 1
414 : Config_PropManager::real("Visualization", "hidden_face_transparency");
415 isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
420 //********************************************************************
421 void XGUI_FacesPanel::onDeleteItem()
426 //********************************************************************
427 void XGUI_FacesPanel::onTransparencyChanged()
429 bool isModified = redisplayObjects(myItemObjects, false);
431 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
435 //********************************************************************
436 void XGUI_FacesPanel::onClosed()
438 setActivePanel(false);