0029674: Improvements in Inspector tool
[occt.git] / tools / VInspector / VInspector_Window.cxx
1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement. 
15
16 #include <inspector/VInspector_Window.hxx>
17
18 #include <AIS_Shape.hxx>
19
20 #include <inspector/TreeModel_ColumnType.hxx>
21 #include <inspector/TreeModel_ContextMenu.hxx>
22 #include <inspector/TreeModel_Tools.hxx>
23
24 #include <inspector/ViewControl_MessageDialog.hxx>
25 #include <inspector/ViewControl_Tools.hxx>
26
27 #include <inspector/VInspector_ItemPresentableObject.hxx>
28 #include <inspector/VInspector_ToolBar.hxx>
29 #include <inspector/VInspector_Tools.hxx>
30 #include <inspector/VInspector_ViewModel.hxx>
31 #include <inspector/VInspector_CallBack.hxx>
32 #include <inspector/VInspector_Communicator.hxx>
33 #include <inspector/VInspector_ItemEntityOwner.hxx>
34 #include <inspector/VInspector_ItemPresentableObject.hxx>
35 #include <inspector/VInspector_ToolBar.hxx>
36 #include <inspector/VInspector_Tools.hxx>
37 #include <inspector/VInspector_ViewModel.hxx>
38 #include <inspector/VInspector_ViewModelHistory.hxx>
39
40 #include <inspector/ViewControl_TreeView.hxx>
41
42 #include <inspector/View_Widget.hxx>
43 #include <inspector/View_Window.hxx>
44
45 #include <Standard_WarningsDisable.hxx>
46 #include <QApplication>
47 #include <QDockWidget>
48 #include <QHeaderView>
49 #include <QGridLayout>
50 #include <QItemSelectionModel>
51 #include <QMainWindow>
52 #include <QMenu>
53 #include <QMessageBox>
54 #include <QToolButton>
55 #include <QTreeView>
56 #include <QWidget>
57 #include <Standard_WarningsRestore.hxx>
58
59 const int VINSPECTOR_DEFAULT_WIDTH  = 1250;
60 const int VINSPECTOR_DEFAULT_HEIGHT = 800;
61
62 const int VINSPECTOR_DEFAULT_POSITION_X = 200;
63 const int VINSPECTOR_DEFAULT_POSITION_Y = 60;
64
65 const int VINSPECTOR_DEFAULT_VIEW_WIDTH = 400;
66 const int VINSPECTOR_DEFAULT_VIEW_HEIGHT = 1000;
67
68 const int VINSPECTOR_DEFAULT_HISTORY_VIEW_WIDTH = 400;
69 const int VINSPECTOR_DEFAULT_HISTORY_VIEW_HEIGHT = 50;
70
71 const int VINSPECTOR_DEFAULT_VIEW_POSITION_X = 200 + 900 + 100; // TINSPECTOR_DEFAULT_POSITION_X + TINSPECTOR_DEFAULT_WIDTH + 100
72 const int VINSPECTOR_DEFAULT_VIEW_POSITION_Y = 60; // TINSPECTOR_DEFAULT_POSITION_Y + 50
73
74 // =======================================================================
75 // function : Constructor
76 // purpose :
77 // =======================================================================
78 VInspector_Window::VInspector_Window()
79 : myParent (0), myExportToShapeViewDialog (0), myViewWindow (0)
80 {
81   myMainWindow = new QMainWindow (0);
82
83   QWidget* aCentralWidget = new QWidget (myMainWindow);
84   QGridLayout* aParentLay = new QGridLayout (aCentralWidget);
85   aParentLay->setContentsMargins (0, 0, 0, 0);
86   aParentLay->setSpacing(0);
87
88   // restore state of tool bar: on the bottom of the window
89   myToolBar = new VInspector_ToolBar(aCentralWidget);
90   connect (myToolBar, SIGNAL (actionClicked (int)), this, SLOT (onToolBarActionClicked (int)));
91   aParentLay->addWidget (myToolBar->GetControl(), 0, 0);
92
93   // tree view
94   myTreeView = new QTreeView (aCentralWidget);
95   myTreeView->setSelectionBehavior (QAbstractItemView::SelectRows);
96   myTreeView->setSelectionMode (QAbstractItemView::ExtendedSelection);
97   myTreeView->header()->setStretchLastSection (true);
98   myTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
99   VInspector_ViewModel* aTreeModel = new VInspector_ViewModel (myTreeView);
100   myTreeView->setModel (aTreeModel);
101   // hide Visibility column
102   TreeModel_HeaderSection anItem = aTreeModel->GetHeaderItem ((int)TreeModel_ColumnType_Visibility);
103   anItem.SetIsHidden (true);
104   aTreeModel->SetHeaderItem ((int)TreeModel_ColumnType_Visibility, anItem);
105
106   connect (myTreeView, SIGNAL(customContextMenuRequested(const QPoint&)),
107            this, SLOT (onTreeViewContextMenuRequested(const QPoint&)));
108   new TreeModel_ContextMenu (myTreeView);
109
110   QItemSelectionModel* aSelModel = new QItemSelectionModel (myTreeView->model(), myTreeView);
111   myTreeView->setSelectionModel (aSelModel);
112   connect (aSelModel, SIGNAL (selectionChanged (const QItemSelection&, const QItemSelection&)),
113            this, SLOT (onSelectionChanged (const QItemSelection&, const QItemSelection&)));
114
115   aParentLay->addWidget(myTreeView, 1, 0);
116   aParentLay->setRowStretch (1, 1);
117   myMainWindow->setCentralWidget (aCentralWidget);
118
119   myHistoryView = new ViewControl_TreeView (myMainWindow);
120   myHistoryView->setSelectionBehavior (QAbstractItemView::SelectRows);
121   ((ViewControl_TreeView*)myHistoryView)->SetPredefinedSize (QSize (VINSPECTOR_DEFAULT_HISTORY_VIEW_WIDTH,
122                                                                     VINSPECTOR_DEFAULT_HISTORY_VIEW_HEIGHT));
123   myHistoryView->setContextMenuPolicy (Qt::CustomContextMenu);
124   myHistoryView->header()->setStretchLastSection (true);
125   new TreeModel_ContextMenu (myHistoryView);
126
127   myHistoryView->setSelectionMode (QAbstractItemView::ExtendedSelection);
128   VInspector_ViewModelHistory* aHistoryModel = new VInspector_ViewModelHistory (myHistoryView);
129   myHistoryView->setModel (aHistoryModel);
130
131   QItemSelectionModel* aSelectionModel = new QItemSelectionModel (aHistoryModel);
132   myHistoryView->setSelectionModel (aSelectionModel);
133   connect (aSelectionModel, SIGNAL (selectionChanged (const QItemSelection&, const QItemSelection&)),
134     this, SLOT (onHistoryViewSelectionChanged (const QItemSelection&, const QItemSelection&)));
135
136   anItem = aHistoryModel->GetHeaderItem (0);
137   TreeModel_Tools::UseVisibilityColumn (myHistoryView, false);
138   // hide Visibility column
139   anItem = aHistoryModel->GetHeaderItem ((int)TreeModel_ColumnType_Visibility);
140   anItem.SetIsHidden (true);
141   aHistoryModel->SetHeaderItem ((int)TreeModel_ColumnType_Visibility, anItem);
142
143   QModelIndex aParentIndex = myHistoryView->model()->index (0, 0);
144   myHistoryView->setExpanded (aParentIndex, true);
145
146   QDockWidget* aHistoryDockWidget = new QDockWidget (tr ("HistoryView"), myMainWindow);
147   aHistoryDockWidget->setObjectName (aHistoryDockWidget->windowTitle());
148   aHistoryDockWidget->setTitleBarWidget (new QWidget(myMainWindow));
149   aHistoryDockWidget->setWidget (myHistoryView);
150   myMainWindow->addDockWidget (Qt::BottomDockWidgetArea, aHistoryDockWidget);
151
152   myMainWindow->resize (450, 800);
153   myMainWindow->move (60, 20);
154
155   myMainWindow->resize (VINSPECTOR_DEFAULT_WIDTH, VINSPECTOR_DEFAULT_HEIGHT);
156   myMainWindow->move (VINSPECTOR_DEFAULT_POSITION_X, VINSPECTOR_DEFAULT_POSITION_Y);
157 }
158
159 // =======================================================================
160 // function : SetParent
161 // purpose :
162 // =======================================================================
163 void VInspector_Window::SetParent (void* theParent)
164 {
165   myParent = (QWidget*)theParent;
166   if (!myParent)
167     return;
168
169   QLayout* aLayout = myParent->layout();
170   if (aLayout)
171     aLayout->addWidget (GetMainWindow());
172 }
173
174 // =======================================================================
175 // function : FillActionsMenu
176 // purpose :
177 // =======================================================================
178 void VInspector_Window::FillActionsMenu (void* theMenu)
179 {
180   QMenu* aMenu = (QMenu*)theMenu;
181   QList<QDockWidget*> aDockwidgets = myMainWindow->findChildren<QDockWidget*>();
182   for (QList<QDockWidget*>::iterator it = aDockwidgets.begin(); it != aDockwidgets.end(); ++it)
183   {
184     QDockWidget* aDockWidget = *it;
185     if (aDockWidget->parentWidget() == myMainWindow)
186       aMenu->addAction (aDockWidget->toggleViewAction());
187   }
188 }
189
190 // =======================================================================
191 // function : GetPreferences
192 // purpose :
193 // =======================================================================
194 void VInspector_Window::GetPreferences (TInspectorAPI_PreferencesDataMap& theItem)
195 {
196   theItem.Clear();
197   theItem.Bind ("geometry",  TreeModel_Tools::ToString (myMainWindow->saveState()).toStdString().c_str());
198
199   QMap<QString, QString> anItems;
200   TreeModel_Tools::SaveState (myTreeView, anItems);
201   for (QMap<QString, QString>::const_iterator anItemsIt = anItems.begin(); anItemsIt != anItems.end(); anItemsIt++)
202   {
203     theItem.Bind (anItemsIt.key().toStdString().c_str(), anItemsIt.value().toStdString().c_str());
204   }
205
206   anItems.clear();
207   TreeModel_Tools::SaveState (myHistoryView, anItems, "history_view_");
208   for (QMap<QString, QString>::const_iterator anItemsIt = anItems.begin(); anItemsIt != anItems.end(); anItemsIt++)
209     theItem.Bind (anItemsIt.key().toStdString().c_str(), anItemsIt.value().toStdString().c_str());
210 }
211
212 // =======================================================================
213 // function : SetPreferences
214 // purpose :
215 // =======================================================================
216 void VInspector_Window::SetPreferences (const TInspectorAPI_PreferencesDataMap& theItem)
217 {
218   if (theItem.IsEmpty())
219   {
220     TreeModel_Tools::SetDefaultHeaderSections (myTreeView);
221     TreeModel_Tools::SetDefaultHeaderSections (myHistoryView);
222     return;
223   }
224
225   for (TInspectorAPI_IteratorOfPreferencesDataMap anItemIt (theItem); anItemIt.More(); anItemIt.Next())
226   {
227     if (anItemIt.Key().IsEqual ("geometry"))
228       myMainWindow->restoreState (TreeModel_Tools::ToByteArray (anItemIt.Value().ToCString()));
229     else if (TreeModel_Tools::RestoreState (myTreeView, anItemIt.Key().ToCString(), anItemIt.Value().ToCString()))
230       continue;
231     else if (TreeModel_Tools::RestoreState (myHistoryView, anItemIt.Key().ToCString(), anItemIt.Value().ToCString(),
232                                             "history_view_"))
233       continue;
234   }
235 }
236
237 // =======================================================================
238 // function : UpdateContent
239 // purpose :
240 // =======================================================================
241 void VInspector_Window::UpdateContent()
242 {
243   TCollection_AsciiString aName = "TKVInspector";
244
245   bool isModelUpdated = false;
246   if (myParameters->FindParameters (aName))
247     isModelUpdated = Init (myParameters->Parameters (aName));
248   if (myParameters->FindFileNames (aName))
249   {
250     for (NCollection_List<TCollection_AsciiString>::Iterator aFileNamesIt (myParameters->FileNames (aName));
251          aFileNamesIt.More(); aFileNamesIt.Next())
252          isModelUpdated = OpenFile (aFileNamesIt.Value()) || isModelUpdated;
253
254     NCollection_List<TCollection_AsciiString> aNames;
255     myParameters->SetFileNames (aName, aNames);
256   }
257   if (!isModelUpdated)
258     UpdateTreeModel();
259
260   // make AIS_InteractiveObject selected selected if exist in select parameters
261   NCollection_List<Handle(Standard_Transient)> anObjects;
262   VInspector_ViewModel* aViewModel = dynamic_cast<VInspector_ViewModel*>(myTreeView->model());
263   if (aViewModel && myParameters->GetSelectedObjects(aName, anObjects))
264   {
265     QItemSelectionModel* aSelectionModel = myTreeView->selectionModel();
266     aSelectionModel->clear();
267     for (NCollection_List<Handle(Standard_Transient)>::Iterator aParamsIt (anObjects);
268          aParamsIt.More(); aParamsIt.Next())
269     {
270       Handle(Standard_Transient) anObject = aParamsIt.Value();
271       Handle(AIS_InteractiveObject) aPresentation = Handle(AIS_InteractiveObject)::DownCast (anObject);
272       if (aPresentation.IsNull())
273         continue;
274
275       QModelIndex aPresentationIndex = aViewModel->FindIndex (aPresentation);
276       if (!aPresentationIndex.isValid())
277         continue;
278        aSelectionModel->select (aPresentationIndex, QItemSelectionModel::Select);
279        myTreeView->scrollTo (aPresentationIndex);
280     }
281   }
282 }
283
284 // =======================================================================
285 // function : Init
286 // purpose :
287 // =======================================================================
288 bool VInspector_Window::Init (const NCollection_List<Handle(Standard_Transient)>& theParameters)
289 {
290   Handle(AIS_InteractiveContext) aContext;
291   Handle(VInspector_CallBack) aCallBack;
292
293   for (NCollection_List<Handle(Standard_Transient)>::Iterator aParamsIt (theParameters); aParamsIt.More(); aParamsIt.Next())
294   {
295     Handle(Standard_Transient) anObject = aParamsIt.Value();
296     if (aContext.IsNull())
297       aContext = Handle(AIS_InteractiveContext)::DownCast (anObject);
298
299     if (aCallBack.IsNull())
300       aCallBack = Handle(VInspector_CallBack)::DownCast (anObject);
301   }
302   if (aContext.IsNull())
303     return false;
304   VInspector_ViewModel* aViewModel = dynamic_cast<VInspector_ViewModel*> (myTreeView->model());
305   if (aViewModel && aViewModel->GetContext() == aContext)
306     UpdateTreeModel();
307   else
308     SetContext (aContext);
309
310   if (!aCallBack.IsNull() && aCallBack != myCallBack)
311   {
312     myCallBack = aCallBack;
313   }
314   return true;
315 }
316
317 // =======================================================================
318 // function : SetContext
319 // purpose :
320 // =======================================================================
321 void VInspector_Window::SetContext (const Handle(AIS_InteractiveContext)& theContext)
322 {
323   VInspector_ViewModel* aViewModel = dynamic_cast<VInspector_ViewModel*> (myTreeView->model());
324   aViewModel->SetContext (theContext);
325   myTreeView->setExpanded (aViewModel->index (0, 0), true);
326
327   if (!myCallBack.IsNull())
328     myCallBack->SetContext (theContext);
329 }
330
331 // =======================================================================
332 // function : OpenFile
333 // purpose :
334 // =======================================================================
335 bool VInspector_Window::OpenFile(const TCollection_AsciiString& theFileName)
336 {
337   VInspector_ViewModel* aViewModel = dynamic_cast<VInspector_ViewModel*> (myTreeView->model());
338   if (!aViewModel)
339     return false;
340
341   Handle(AIS_InteractiveContext) aContext = aViewModel->GetContext();
342   bool isModelUpdated = false;
343   if (aContext.IsNull())
344   {
345     aContext = createView();
346     SetContext (aContext);
347     isModelUpdated = true;
348   }
349
350   TopoDS_Shape aShape = VInspector_Tools::ReadShape (theFileName);
351   if (aShape.IsNull())
352     return isModelUpdated;
353
354   Handle(AIS_Shape) aPresentation = new AIS_Shape (aShape);
355   aContext->Display (aPresentation, false);
356   aContext->Load (aPresentation, -1/*selection mode*/, true);
357   aContext->UpdateCurrentViewer();
358
359   UpdateTreeModel();
360   myTreeView->setExpanded (aViewModel->index (0, 0), true);
361   return true;
362 }
363
364 // =======================================================================
365 // function : onTreeViewContextMenuRequested
366 // purpose :
367 // =======================================================================
368 void VInspector_Window::onTreeViewContextMenuRequested(const QPoint& thePosition)
369 {
370   QMenu* aMenu = new QMenu (GetMainWindow());
371   aMenu->addAction (ViewControl_Tools::CreateAction (tr ("Export to ShapeView"), SLOT (onExportToShapeView()), GetMainWindow(), this));
372   aMenu->addAction (ViewControl_Tools::CreateAction (tr ("Show"), SLOT (onShow()), GetMainWindow(), this));
373   aMenu->addAction (ViewControl_Tools::CreateAction (tr ("Hide"), SLOT (onHide()), GetMainWindow(), this));
374   QPoint aPoint = myTreeView->mapToGlobal (thePosition);
375   aMenu->exec(aPoint);
376 }
377
378 // =======================================================================
379 // function : onToolBarActionClicked
380 // purpose :
381 // =======================================================================
382 void VInspector_Window::onToolBarActionClicked (const int theActionId)
383 {
384   VInspector_ViewModel* aViewModel = dynamic_cast<VInspector_ViewModel*> (myTreeView->model());
385   if (!aViewModel)
386     return;
387
388   switch (theActionId)
389   {
390     case VInspector_ToolActionType_UpdateId:
391     {
392       UpdateTreeModel();
393       break;
394     }
395     case VInspector_ToolActionType_SelectPresentationsId:
396     {
397       bool isChecked = myToolBar->GetToolButton((VInspector_ToolActionType)theActionId)->isChecked();
398       NCollection_List<Handle(AIS_InteractiveObject)> aPresentationsForViewer;
399       if (isChecked)
400         aPresentationsForViewer = VInspector_ItemPresentableObject::GetSelectedPresentations(myTreeView->selectionModel());
401       Handle(AIS_InteractiveContext) aContext = aViewModel->GetContext();
402       VInspector_Tools::AddOrRemovePresentations(aContext, aPresentationsForViewer);
403       UpdateTreeModel();
404       break;
405     }
406     case VInspector_ToolActionType_SelectOwnersId:
407     {
408       NCollection_List<Handle(SelectBasics_EntityOwner)> anOwnersForViewer;
409       if (myToolBar->GetToolButton((VInspector_ToolActionType)theActionId)->isChecked())
410         VInspector_ViewModel::GetSelectedOwners(myTreeView->selectionModel(), anOwnersForViewer);
411       VInspector_Tools::AddOrRemoveSelectedShapes(aViewModel->GetContext(), anOwnersForViewer);
412       UpdateTreeModel();
413       break;
414     }
415     default:
416       break;
417   }
418 }
419
420 // =======================================================================
421 // function : onSelectionChanged
422 // purpose :
423 // =======================================================================
424 void VInspector_Window::onSelectionChanged (const QItemSelection&,
425                                             const QItemSelection&)
426 {
427   QApplication::setOverrideCursor (Qt::WaitCursor);
428
429   if (myToolBar->GetToolButton(VInspector_ToolActionType_SelectPresentationsId)->isChecked())
430     onToolBarActionClicked(VInspector_ToolActionType_SelectPresentationsId);
431   else if (myToolBar->GetToolButton(VInspector_ToolActionType_SelectOwnersId)->isChecked())
432     onToolBarActionClicked(VInspector_ToolActionType_SelectOwnersId);
433
434   QApplication::restoreOverrideCursor();
435 }
436
437 // =======================================================================
438 // function : onHistoryViewSelectionChanged
439 // purpose :
440 // =======================================================================
441 void VInspector_Window::onHistoryViewSelectionChanged (const QItemSelection& theSelected,
442                                                        const QItemSelection&)
443 {
444   VInspector_ViewModelHistory* aHistoryModel = dynamic_cast<VInspector_ViewModelHistory*> (myHistoryView->model());
445   if (!aHistoryModel)
446     return;
447
448   if (theSelected.size() == 0)
449     return;
450
451   QModelIndexList aSelectedIndices = theSelected.indexes();
452   QStringList aPointers = aHistoryModel->GetSelectedPointers(aSelectedIndices.first());
453
454   VInspector_ViewModel* aTreeModel = dynamic_cast<VInspector_ViewModel*> (myTreeView->model());
455   if (!aTreeModel)
456     return;
457
458   QModelIndexList anIndices = aTreeModel->FindPointers (aPointers);
459   QItemSelectionModel* aSelectionModel = myTreeView->selectionModel();
460   aSelectionModel->clear();
461   for (int anIndicesId = 0, aSize = anIndices.size(); anIndicesId < aSize; anIndicesId++)
462   {
463     QModelIndex anIndex = anIndices[anIndicesId];
464     myTreeView->setExpanded (aTreeModel->parent (anIndex), true);
465     aSelectionModel->select (anIndex, QItemSelectionModel::Select);
466   }
467 }
468
469 // =======================================================================
470 // function : onExportToShapeView
471 // purpose :
472 // =======================================================================
473 void VInspector_Window::onExportToShapeView()
474 {
475   NCollection_List<Handle(AIS_InteractiveObject)> aSelectedPresentations =
476             VInspector_ItemPresentableObject::GetSelectedPresentations(myTreeView->selectionModel());
477   if (aSelectedPresentations.Extent() <= 0)
478     return;
479
480   TCollection_AsciiString aPluginName ("TKShapeView");
481   NCollection_List<Handle(Standard_Transient)> aParameters;
482   if (myParameters->FindParameters (aPluginName))
483     aParameters = myParameters->Parameters (aPluginName);
484
485   NCollection_List<TCollection_AsciiString> anItemNames;
486   if (myParameters->FindSelectedNames (aPluginName))
487     anItemNames = myParameters->GetSelectedNames (aPluginName);
488
489   QStringList anExportedPointers;
490   for (NCollection_List<Handle(AIS_InteractiveObject)>::Iterator anIOIt (aSelectedPresentations); anIOIt.More(); anIOIt.Next())
491   {
492     Handle(AIS_Shape) aShapePresentation = Handle(AIS_Shape)::DownCast (anIOIt.Value());
493     if (aShapePresentation.IsNull())
494       continue;
495
496     const TopoDS_Shape& aShape = aShapePresentation->Shape();
497     if (aShape.IsNull())
498       continue;
499     aParameters.Append (aShape.TShape());
500     anItemNames.Append (TInspectorAPI_PluginParameters::ParametersToString(aShape));
501     anExportedPointers.append (VInspector_Tools::GetPointerInfo (aShape.TShape(), true).ToCString());
502   }
503   if (anExportedPointers.empty())
504     return;
505
506   TCollection_AsciiString aPluginShortName = aPluginName.SubString (3, aPluginName.Length());
507   QString aMessage = QString ("TShape %1 is sent to %2.")
508     .arg (anExportedPointers.join(", "))
509     .arg (aPluginShortName.ToCString());
510   QString aQuestion = QString ("Would you like to activate %1 immediately?\n")
511     .arg (aPluginShortName.ToCString()).toStdString().c_str();
512   if (!myExportToShapeViewDialog)
513     myExportToShapeViewDialog = new ViewControl_MessageDialog (myParent, aMessage, aQuestion);
514   else
515     myExportToShapeViewDialog->SetInformation (aMessage);
516   myExportToShapeViewDialog->Start();
517
518   myParameters->SetSelectedNames (aPluginName, anItemNames);
519   myParameters->SetParameters (aPluginName, aParameters, myExportToShapeViewDialog->IsAccepted());
520 }
521
522 // =======================================================================
523 // function : onShow
524 // purpose :
525 // =======================================================================
526 void VInspector_Window::onShow()
527 {
528   displaySelectedPresentations (true);
529 }
530
531 // =======================================================================
532 // function : onHide
533 // purpose :
534 // =======================================================================
535 void VInspector_Window::onHide()
536 {
537   displaySelectedPresentations (false);
538 }
539
540 // =======================================================================
541 // function : UpdateTreeModel
542 // purpose :
543 // =======================================================================
544 void VInspector_Window::UpdateTreeModel()
545 {
546   VInspector_ViewModel* aViewModel = dynamic_cast<VInspector_ViewModel*> (myTreeView->model());
547   if (aViewModel)
548     aViewModel->UpdateTreeModel();
549 }
550
551 // =======================================================================
552 // function : displaySelectedPresentations
553 // purpose :
554 // =======================================================================
555 void VInspector_Window::displaySelectedPresentations(const bool theToDisplay)
556 {
557   VInspector_ViewModel* aViewModel = dynamic_cast<VInspector_ViewModel*> (myTreeView->model());
558   if (!aViewModel)
559     return;
560
561   Handle(AIS_InteractiveContext) aContext = aViewModel->GetContext();
562   if (aContext.IsNull())
563     return;
564
565   NCollection_List<Handle(AIS_InteractiveObject)> aSelectedPresentations =
566     VInspector_ItemPresentableObject::GetSelectedPresentations(myTreeView->selectionModel());
567   if (aSelectedPresentations.Extent() <= 0)
568     return;
569
570   for (NCollection_List<Handle(AIS_InteractiveObject)>::Iterator anIOIt(aSelectedPresentations); anIOIt.More(); anIOIt.Next())
571   {
572     Handle(AIS_InteractiveObject) aPresentation = Handle(AIS_Shape)::DownCast(anIOIt.Value());
573     if (aPresentation.IsNull())
574       continue;
575     if (theToDisplay) {
576       aContext->Display(aPresentation, false);
577       aContext->Load(aPresentation, -1, true);
578     }
579     else
580       aContext->Erase(aPresentation, false);
581   }
582   aContext->UpdateCurrentViewer();
583
584   // the order of objects returned by AIS_InteractiveContext is changed because the processed object is moved from
585   // Erased to Displayed container or back
586   QItemSelectionModel* aSelectionModel = myTreeView->selectionModel();
587   aSelectionModel->clear();
588
589   UpdateTreeModel();
590 }
591
592 // =======================================================================
593 // function : createView
594 // purpose :
595 // =======================================================================
596 Handle(AIS_InteractiveContext) VInspector_Window::createView()
597 {
598   myViewWindow = new View_Window (0);
599   myViewWindow->GetView()->SetPredefinedSize (VINSPECTOR_DEFAULT_VIEW_WIDTH, VINSPECTOR_DEFAULT_VIEW_HEIGHT);
600   myViewWindow->move (VINSPECTOR_DEFAULT_VIEW_POSITION_X, VINSPECTOR_DEFAULT_VIEW_POSITION_Y);
601   myViewWindow->show();
602
603   return myViewWindow->GetView()->GetViewer()->GetContext();
604 }