1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #define QT_CLEAN_NAMESPACE /* avoid definition of INT32 and INT8 */
20 #include <inspector/View_Window.hxx>
22 #include <inspector/View_Displayer.hxx>
23 #include <inspector/View_DisplayPreview.hxx>
24 #include <inspector/View_PreviewParameters.hxx>
25 #include <inspector/View_ToolBar.hxx>
26 #include <inspector/View_Viewer.hxx>
27 #include <inspector/View_Widget.hxx>
31 #include <Standard_WarningsDisable.hxx>
33 #include <QDockWidget>
34 #include <QGridLayout>
37 #include <Standard_WarningsRestore.hxx>
39 const int DEFAULT_SPACING = 3;
41 // =======================================================================
42 // function : Constructor
44 // =======================================================================
45 View_Window::View_Window (QWidget* theParent,
46 const Handle(AIS_InteractiveContext)& theContext,
47 const bool isUseKeepView, const bool isFitAllActive)
50 QGridLayout* aViewLayout = new QGridLayout (this);
51 aViewLayout->setContentsMargins (0, 0, 0, 0);
52 aViewLayout->setSpacing (DEFAULT_SPACING);
54 myView = new View_Widget (this, theContext, isFitAllActive);
55 myViewToolBar = new View_ToolBar (this, isUseKeepView);
56 aViewLayout->addWidget (myViewToolBar->GetControl(), 0, 0, 1, 2);
57 connect (myViewToolBar, SIGNAL (contextChanged()), this, SLOT (onViewSelectorActivated()));
58 connect (myViewToolBar, SIGNAL (actionClicked (int)),
59 this, SLOT (onToolBarActionClicked (int)));
61 connect (myView, SIGNAL (checkedStateChanged(int, bool)), this, SLOT (onCheckedStateChanged (int, bool)));
63 myView->setContextMenuPolicy (Qt::CustomContextMenu);
64 connect (myView, SIGNAL (customContextMenuRequested (const QPoint&)),
65 this, SLOT (onViewContextMenuRequested (const QPoint&)));
67 myActionsToolBar = new QToolBar (this);
68 myActionsToolBar->layout()->setContentsMargins (0, 0, 0, 0);
69 myActionsToolBar->setOrientation (Qt::Vertical);
71 myActionsToolBar->addWidget (myView-> GetWidget (View_ViewActionType_FitAllId));
72 myActionsToolBar->addAction (myView->ViewAction (View_ViewActionType_DisplayModeId));
74 aViewLayout->addWidget (myActionsToolBar, 1, 0);
75 aViewLayout->addWidget (myView, 1, 1);
76 aViewLayout->setRowStretch (1, 1);
78 Handle(AIS_InteractiveContext) aContext = myView->GetViewer()->GetContext();
79 myViewToolBar->SetContext (View_ContextType_Own, aContext);
81 myDisplayer = new View_Displayer();
83 myDisplayer->KeepPresentations (true);
84 myDisplayer->SetFitAllActive (isFitAllActive);
85 connect (myView, SIGNAL (displayModeClicked()), this, SLOT (onDisplayModeChanged()));
86 onViewSelectorActivated();
89 // =======================================================================
90 // function : SetContext
92 // =======================================================================
93 void View_Window::SetContext (View_ContextType /*theType*/, const Handle(AIS_InteractiveContext)& theContext)
95 ViewToolBar()->SetContext (View_ContextType_External, theContext);
98 // =======================================================================
99 // function : SetPredefinedSize
101 // =======================================================================
102 void View_Window::SetPredefinedSize (int theDefaultWidth, int theDefaultHeight)
104 myView->SetPredefinedSize (theDefaultWidth, theDefaultHeight);
107 // =======================================================================
108 // function : SetInitProj
110 // =======================================================================
111 void View_Window::SetInitProj (const Standard_Real theVx, const Standard_Real theVy, const Standard_Real theVz)
113 myView->SetInitProj (theVx, theVy, theVz);
116 // =======================================================================
119 // =======================================================================
120 Handle(V3d_View) View_Window::View() const
122 return myView->GetViewer()->GetView();
125 // =======================================================================
126 // function : SaveState
128 // =======================================================================
129 void View_Window::SaveState (View_Window* theView, QMap<QString, QString>& theItems,
130 const QString& thePrefix)
132 QStringList aCameraDirection;
133 Standard_Real aVX, aVY, aVZ;
134 Handle(V3d_View) aView = theView->View();
138 aView->Proj (aVX, aVY, aVZ);
139 aCameraDirection << QString::number (aVX) << QString::number (aVY) << QString::number (aVZ);
141 theItems[thePrefix + "view_camera_direction"] = aCameraDirection.join (",");
143 View_PreviewParameters::SaveState (theView->Displayer()->DisplayPreview()->GetPreviewParameters(), theItems, "preview_parameters_");
144 View_ToolBar::SaveState (theView->ViewToolBar(), theItems, "view_toolbar_");
145 View_Widget::SaveState (theView->ViewWidget(), theItems, "view_widget_");
148 // =======================================================================
149 // function : RestoreState
151 // =======================================================================
152 bool View_Window::RestoreState (View_Window* theView, const QString& theKey, const QString& theValue,
153 const QString& thePrefix)
155 if (theKey == thePrefix + "view_camera_direction")
157 QStringList aValues = theValue.split (",");
158 if (aValues.size() == 3)
160 Standard_Real aVX = aValues.at (0).toDouble();
161 Standard_Real aVY = aValues.at (1).toDouble();
162 Standard_Real aVZ = aValues.at (2).toDouble();
164 theView->SetInitProj (aVX, aVY, aVZ);
168 else if (View_PreviewParameters::RestoreState (theView->Displayer()->DisplayPreview()->GetPreviewParameters(),
169 theKey, theValue, "preview_parameters_"))
173 else if (View_ToolBar::RestoreState (theView->ViewToolBar(), theKey, theValue, "view_toolbar_"))
177 else if (View_Widget::RestoreState (theView->ViewWidget(), theKey, theValue, "view_widget_"))
179 // display mode is restored
180 View_Displayer* aDisplayer = theView->Displayer();
181 if (theView->ViewWidget()->DisplayMode() != aDisplayer->DisplayMode())
182 aDisplayer->SetDisplayMode (theView->ViewWidget()->DisplayMode());
184 bool toFitAll = theView->ViewWidget()->IsActionChecked (View_ViewActionType_FitAllId);
185 if (toFitAll != aDisplayer->IsFitAllActive())
186 aDisplayer->SetFitAllActive (toFitAll);
194 // =======================================================================
195 // function : onViewSelectorActivated
197 // =======================================================================
198 void View_Window::onViewSelectorActivated()
200 View_ContextType aType = myViewToolBar->CurrentContextType();
201 bool isViewEnabled = aType == View_ContextType_Own;
203 myView->SetEnabledView (isViewEnabled);
205 Handle(AIS_InteractiveContext) aContext = myViewToolBar->CurrentContext();
206 myDisplayer->EraseAllPresentations (true);
207 emit eraseAllPerformed();
209 myDisplayer->SetContext (aContext);
212 // =======================================================================
213 // function : onToolBarActionClicked
215 // =======================================================================
216 void View_Window::onToolBarActionClicked (const int theActionId)
220 case View_ToolActionType_Trihedron:
222 myDisplayer->DisplayDefaultTrihedron (myViewToolBar->IsActionChecked (theActionId), Standard_True);
225 case View_ToolActionType_ViewCube:
227 myDisplayer->DisplayViewCube (myViewToolBar->IsActionChecked (theActionId), Standard_True);
231 case View_ToolActionType_KeepViewId:
233 myDisplayer->KeepPresentations (myViewToolBar->IsActionChecked (theActionId));
236 case View_ToolActionType_KeepViewOffId:
238 myDisplayer->KeepPresentations (!myViewToolBar->IsActionChecked (theActionId));
241 case View_ToolActionType_ClearViewId:
243 myDisplayer->EraseAllPresentations (true);
244 emit eraseAllPerformed();
252 // =======================================================================
253 // function : onCheckedStateChanged
255 // =======================================================================
256 void View_Window::onCheckedStateChanged (int theActionId, bool theState)
258 if (theActionId == View_ViewActionType_FitAllId)
259 myDisplayer->SetFitAllActive (theState);
262 // =======================================================================
263 // function : onViewContextMenuRequested
265 // =======================================================================
266 void View_Window::onViewContextMenuRequested (const QPoint& thePosition)
268 QMenu* aMenu = new QMenu (this);
269 QMenu* anOrientationSubMenu = aMenu->addMenu ("Set View Orientation");
271 for (int i = 0; i < (int)V3d_XnegYnegZneg; i++)
273 V3d_TypeOfOrientation anOrientationType = (V3d_TypeOfOrientation)i;
275 QAction* anAction = new QAction (V3d::TypeOfOrientationToString (anOrientationType), this);
276 QObject::connect (anAction, SIGNAL (triggered (bool)), this, SLOT (onSetOrientation()));
278 anOrientationSubMenu->addAction (anAction);
280 aMenu->addMenu (anOrientationSubMenu);
282 QPoint aPoint = myView->mapToGlobal (thePosition);
283 aMenu->exec (aPoint);
286 // =======================================================================
287 // function : onSetOrientation
289 // =======================================================================
290 void View_Window::onSetOrientation()
292 QAction* anAction = (QAction*)(sender());
294 TCollection_AsciiString anOrientationStr (anAction->text().toStdString().c_str());
296 V3d_TypeOfOrientation anOrientationType;
297 if (!V3d::TypeOfOrientationFromString (anOrientationStr.ToCString(), anOrientationType))
300 Handle(V3d_View) aView = myView->GetViewer()->GetView();
304 aView->SetProj (anOrientationType);
309 // =======================================================================
310 // function : onDisplayModeChanged
312 // =======================================================================
313 void View_Window::onDisplayModeChanged()
315 int aDisplayMode = myView->DisplayMode();
316 for (NCollection_DataMap<View_PresentationType, NCollection_Shared<AIS_ListOfInteractive> >::Iterator
317 anIterator(myDisplayer->GetDisplayed()); anIterator.More(); anIterator.Next())
318 myDisplayer->SetDisplayMode (aDisplayMode, anIterator.Key(), false);
319 myDisplayer->UpdateViewer();