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_Displayer.h"
37 #include "XGUI_Tools.h"
38 #include "XGUI_Workshop.h"
42 #include <QFocusEvent>
43 #include <QGridLayout>
44 #include <QListWidget>
45 #include <QMainWindow>
47 static const int LayoutMargin = 3;
49 //********************************************************************
50 XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
51 : QDockWidget(theParent), myIsActive(false), myWorkshop(theWorkshop)
53 setWindowTitle(tr("Hide Faces"));
54 QAction* aViewAct = toggleViewAction();
55 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
57 QWidget* aContent = new QWidget(this);
58 QGridLayout* aMainLayout = new QGridLayout(aContent);
59 aMainLayout->setContentsMargins(LayoutMargin, LayoutMargin, LayoutMargin, LayoutMargin);
62 myHiddenOrTransparent = new QCheckBox(tr("Transparent"), aContent);
63 myListView = new ModuleBase_ListView(aContent, "", "Hidden/transparent faces in 3D view");
64 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
66 aMainLayout->addWidget(myHiddenOrTransparent, 0, 0);
67 aMainLayout->addWidget(myListView->getControl(), 1, 0);
69 myListView->getControl()->setFocusPolicy(Qt::StrongFocus);
70 myListView->getControl()->viewport()->installEventFilter(this);
73 //********************************************************************
74 void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
76 // restore presentation state
77 bool isModified = false;
78 std::set<ObjectPtr> aRestoredObjects;
79 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
80 anIt != myItems.end(); anIt++) {
81 if (aRestoredObjects.find(anIt.value()->object()) == aRestoredObjects.end())
82 aRestoredObjects.insert(anIt.value()->object());
84 // clear internal containers
85 myListView->getControl()->clear();
89 isModified = redisplayObjects(aRestoredObjects, isToFlushRedisplay);
91 if (isToFlushRedisplay && isModified)
92 XGUI_Tools::workshop(myWorkshop)->displayer()->updateViewer();
95 //********************************************************************
96 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
98 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
99 if (theEvent->type() == QEvent::MouseButtonRelease)
101 if (myListView->getControl()->viewport() == aWidget)
102 setActivePanel(true);
104 // pass the event on to the parent class
105 return QObject::eventFilter(theObject, theEvent);
108 //********************************************************************
109 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
111 if (myIsActive == theIsActive)
114 ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
115 myIsActive = theIsActive;
120 // selection should be activated after emit signal, that deactivates current widget(selection)
121 activateSelection(theIsActive);
125 // selection should be activated after emit signal, that deactivates current widget(selection)
126 activateSelection(theIsActive);
131 //********************************************************************
132 bool XGUI_FacesPanel::useTransparency() const
134 return myHiddenOrTransparent->isChecked();
137 //********************************************************************
138 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
140 std::set<int> anIndicesToBeRemoved;
141 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
142 anItemsIt != myItems.end(); anItemsIt++)
144 ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
145 ObjectPtr anObject = aPrs->object();
146 if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
148 anIndicesToBeRemoved.insert(anItemsIt.key());
151 // remove from myItes container
152 for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
153 aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
154 myItems.remove(*aToBeRemovedIt);
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 for (int i = 0; i < aSelected.size(); i++) {
192 ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
193 ObjectPtr anObject = aPrs->object();
197 if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
200 myItems.insert(myLastItemIndex, aPrs);
201 myListView->addItem(generateName(aPrs), myLastItemIndex);
202 isModified = hideFace(myLastItemIndex) || isModified;
208 XGUI_Tools::workshop(myWorkshop)->displayer()->updateViewer();
211 //********************************************************************
212 bool XGUI_FacesPanel::processDelete()
214 //appendFirstSelectionInHistory();
215 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
217 std::set<int> aSelectedIds;
218 myListView->getSelectedIndices(aSelectedIds);
219 if (aSelectedIds.empty())
222 bool isModified = false;
223 std::set<ObjectPtr> aRestoredObjects;
224 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
225 for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
227 ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
228 if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
229 aRestoredObjects.insert(aPrs->object());
230 myItems.remove(*anIt);
232 myListView->removeSelectedItems();
234 isModified = redisplayObjects(aRestoredObjects, true) || isModified;
236 XGUI_Tools::workshop(myWorkshop)->displayer()->updateViewer();
239 myListView->restoreSelection(anIndices);
240 //appendSelectionInHistory();
244 //********************************************************************
245 bool XGUI_FacesPanel::redisplayObjects(
246 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
247 const bool isToFlushRedisplay)
249 bool isModified = false;
250 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
251 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
253 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
256 ObjectPtr anObject = *anIt;
257 if (!anObject->isDisplayed()) {
258 // if the object was hidden by this panel
259 if (myHiddenObjects.find(anObject) != myHiddenObjects.end())
260 myHiddenObjects.erase(anObject);
261 anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
262 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
264 //isModified = aDisplayer->display(anObject, false) || isModified;
267 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
271 if (isToFlushRedisplay)
272 Events_Loop::loop()->flush(aDispEvent);
276 //********************************************************************
277 bool XGUI_FacesPanel::hideFace(const int theIndex)
279 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
281 if (!myItems.contains(theIndex))
284 ModuleBase_ViewerPrsPtr aPrs = myItems[theIndex];
286 AISObjectPtr aAISObj = aDisplayer->getAISObject(aPrs->object());
287 if (aAISObj.get() == NULL)
289 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
290 aAISObj->impl<Handle(AIS_InteractiveObject)>());
291 if (aResultPrs.IsNull())
293 // set shape hidden to check whether the presentation should be erased from the viewer
294 bool isModified = false;
295 if (aResultPrs->hasSubShapeVisible(ModuleBase_Tools::getSelectedShape(aPrs)))
296 isModified = aDisplayer->redisplay(aPrs->object(), false) || isModified;
299 ObjectPtr anObject = aPrs->object();
300 myHiddenObjects.insert(anObject);
301 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
302 anObject->setDisplayed(false);
303 isModified = aDisplayer->erase(anObject, false) || isModified;
304 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
305 Events_Loop::loop()->flush(aDispEvent);
310 //********************************************************************
311 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
313 QDockWidget::closeEvent(theEvent);
317 //********************************************************************
318 void XGUI_FacesPanel::activateSelection(bool toActivate)
320 QIntList aShapeTypes;
321 aShapeTypes.append(TopAbs_FACE);
324 myWorkshop->activateSubShapesSelection(aShapeTypes);
326 myWorkshop->deactivateSubShapesSelection();
329 activateSelectionFilters();
331 deactivateSelectionFilters();
334 //********************************************************************
335 QString XGUI_FacesPanel::generateName(const ModuleBase_ViewerPrsPtr& thePrs)
337 if (!thePrs.get() || !thePrs->object().get())
340 GeomShapePtr aContext;
341 ObjectPtr anObject = thePrs->object();
342 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
344 aContext = aResult->shape();
346 // TODO if there is this case
349 QString aName = anObject->data()->name().c_str();
350 if (aContext.get()) {
351 GeomShapePtr aSubShape(new GeomAPI_Shape());
352 aSubShape->setImpl(new TopoDS_Shape(ModuleBase_Tools::getSelectedShape(thePrs)));
353 if (!aSubShape->isEqual(aContext))
354 aName += QString("_%1").arg(GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape));
359 //********************************************************************
360 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject, const bool isDisplayed)
362 if (isDisplayed && myItems.isEmpty())
365 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
367 AISObjectPtr aAISObj = aDisplayer->getAISObject(theObject);
368 if (aAISObj.get() == NULL)
370 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
371 aAISObj->impl<Handle(AIS_InteractiveObject)>());
372 if (aResultPrs.IsNull())
375 // if the object is displayed, the hidden faces are collected and set to the presentation
376 bool isModified = false;
377 NCollection_List<TopoDS_Shape> aHiddenSubShapes;
378 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
379 anIt != myItems.end(); anIt++) {
380 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
381 if (aPrs.get() && aPrs->object() != theObject)
383 TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
384 if (!aHiddenSubShapes.Contains(aShape))
385 aHiddenSubShapes.Append(aShape);
387 isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
388 double aTransparency = !useTransparency() ? 1
389 : Config_PropManager::real("Visualization", "hidden_face_transparency");
390 aResultPrs->setHiddenSubShapeTransparency(aTransparency);
395 //********************************************************************
396 void XGUI_FacesPanel::onDeleteItem()
401 //********************************************************************
402 void XGUI_FacesPanel::onClosed()