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_Widget.hxx>
22 #include <AIS_ViewController.hxx>
23 #include <Geom_Axis2Placement.hxx>
24 #include <Graphic3d_GraphicDriver.hxx>
26 #include <inspector/View_ToolButton.hxx>
27 #include <inspector/View_ViewActionType.hxx>
28 #include <inspector/View_Viewer.hxx>
30 #include <Standard_WarningsDisable.hxx>
31 #include <QColorDialog>
34 #include <QMdiSubWindow>
36 #include <QMessageBox>
37 #include <QMouseEvent>
39 #include <QRubberBand>
41 #include <QStyleFactory>
42 #include <Standard_WarningsRestore.hxx>
47 #include <WNT_Window.hxx>
48 #elif defined(HAVE_XLIB)
49 #include <Xw_Window.hxx>
50 #elif defined(__APPLE__)
51 #include <Cocoa_Window.hxx>
56 // =======================================================================
57 // function : Constructor
59 // =======================================================================
60 View_Widget::View_Widget (QWidget* theParent,
61 const Handle(AIS_InteractiveContext)& theContext,
62 const bool isFitAllActive)
63 : QWidget (theParent), myFirst (true), myDefaultWidth (-1),
64 myDefaultHeight (-1), myViewIsEnabled (true),
65 myHasInitProj (Standard_False), myInitVx (0), myInitVy (0), myInitVz (0)
67 myViewer = new View_Viewer (View_Viewer::DefaultColor());
68 if (!theContext.IsNull())
70 myViewer->InitViewer (theContext);
74 myViewer->InitViewer (myViewer->CreateStandardViewer());
76 myController = new AIS_ViewController();
78 setAttribute (Qt::WA_PaintOnScreen);
79 setAttribute (Qt::WA_NoSystemBackground);
81 setMouseTracking (true);
82 setBackgroundRole (QPalette::NoRole);
83 // set focus policy to threat QContextMenuEvent from keyboard
84 setFocusPolicy (Qt::StrongFocus);
87 ((View_ToolButton*)myFitAllAction)->SetButtonChecked (isFitAllActive);
90 // =======================================================================
91 // function : SetPredefinedSize
93 // =======================================================================
94 void View_Widget::SetPredefinedSize (int theDefaultWidth, int theDefaultHeight)
96 myDefaultWidth = theDefaultWidth;
97 myDefaultHeight = theDefaultHeight;
100 // =======================================================================
103 // =======================================================================
104 void View_Widget::Init()
106 myViewer->CreateView();
109 Aspect_Handle aWindowHandle = (Aspect_Handle)winId();
110 Handle(Aspect_Window) aWnd = new WNT_Window (aWindowHandle);
111 #elif defined (HAVE_XLIB)
112 Aspect_Drawable aWindowHandle = (Aspect_Drawable )winId();
113 Handle(Aspect_DisplayConnection) aDispConnection = myViewer->GetContext()->CurrentViewer()->Driver()->GetDisplayConnection();
114 Handle(Aspect_Window) aWnd = new Xw_Window (aDispConnection, aWindowHandle);
115 #elif defined (__APPLE__)
116 NSView* aViewHandle = (NSView*)winId();
117 Handle(Aspect_Window) aWnd = new Cocoa_Window (aViewHandle);
121 myViewer->SetWindow (aWnd);
123 myViewer->GetView()->SetBackgroundColor (View_Viewer::DefaultColor());
124 myViewer->GetView()->MustBeResized();
127 myViewer->GetView()->SetProj (myInitVx, myInitVy, myInitVz);
130 // =======================================================================
131 // function : DisplayMode
133 // =======================================================================
134 int View_Widget::DisplayMode() const
136 return myViewActions[View_ViewActionType_DisplayModeId]->isChecked() ? AIS_Shaded : AIS_WireFrame;
139 // =======================================================================
140 // function : SetDisplayMode
142 // =======================================================================
143 void View_Widget::SetDisplayMode (const int theMode)
145 myViewActions[View_ViewActionType_DisplayModeId]->setChecked ( theMode ? AIS_Shaded : AIS_WireFrame);
148 // =======================================================================
149 // function : paintEvent
151 // =======================================================================
152 void View_Widget::paintEvent (QPaintEvent* /*theEvent*/)
154 #if (QT_VERSION < 0x050000 || QT_VERSION >= 0x050700)
162 if (myViewer->GetView())
163 myViewer->GetView()->Redraw();
166 // =======================================================================
167 // function : resizeEvent
169 // =======================================================================
170 void View_Widget::resizeEvent (QResizeEvent* /*theEvent*/)
172 #if (QT_VERSION > 0x050000 && QT_VERSION < 0x050700)
179 if (myViewer->GetView())
180 myViewer->GetView()->MustBeResized();
183 // =======================================================================
184 // function : sizeHint
186 // =======================================================================
187 QSize View_Widget::sizeHint() const
189 if (myDefaultWidth > 0 && myDefaultHeight > 0)
190 return QSize (myDefaultWidth, myDefaultHeight);
192 return QWidget::sizeHint();
195 // =======================================================================
196 // function : SetEnabledView
198 // =======================================================================
199 void View_Widget::SetEnabledView (const bool theIsEnabled)
201 myViewIsEnabled = theIsEnabled;
203 if (myViewer->GetView())
204 myViewer->GetView()->SetBackgroundColor (theIsEnabled ? View_Viewer::DefaultColor()
205 : View_Viewer::DisabledColor());
206 ViewAction (View_ViewActionType_DisplayModeId)->setEnabled (theIsEnabled);
209 // =======================================================================
210 // function : SaveState
212 // =======================================================================
213 void View_Widget::SaveState (View_Widget* theWidget,
214 QMap<QString, QString>& theItems,
215 const QString& thePrefix)
217 theItems[thePrefix + "fitall"] = theWidget->ViewAction (View_ViewActionType_FitAllId)->isChecked();
218 theItems[thePrefix + "dispmode"] = QString::number (theWidget->DisplayMode());
221 // =======================================================================
222 // function : RestoreState
224 // =======================================================================
225 bool View_Widget::RestoreState (View_Widget* theWidget,
226 const QString& theKey, const QString& theValue,
227 const QString& thePrefix)
229 if (theKey == thePrefix + "fitall")
231 theWidget->SetActionChecked (View_ViewActionType_FitAllId, theValue.toInt() > 0);
233 else if (theKey == thePrefix + "dispmode")
235 theWidget->SetDisplayMode (theValue.toInt());
243 // =======================================================================
244 // function : onCheckedStateChanged
246 // =======================================================================
247 void View_Widget::onCheckedStateChanged (bool isOn)
249 QWidget* aSentByAction = (QWidget*)sender();
251 if (aSentByAction == myFitAllAction)
252 emit checkedStateChanged(View_ViewActionType_FitAllId, isOn);
255 // =======================================================================
256 // function : initViewActions
258 // =======================================================================
259 void View_Widget::initViewActions()
261 if (!myViewActions.empty())
264 myFitAllAction = new View_ToolButton (this); // action for automatic fit all
265 connect (myFitAllAction, SIGNAL (checkedStateChanged(bool)), this, SLOT (onCheckedStateChanged(bool)));
266 createAction (View_ViewActionType_FitAllId, ":/icons/view_fitall.png", tr ("Fit All"), SLOT (OnFitAll()));
267 myFitAllAction->setDefaultAction (ViewAction (View_ViewActionType_FitAllId));
269 createAction (View_ViewActionType_DisplayModeId, ":/icons/view_dm_shading.png", tr ("Display Mode"),
270 SIGNAL (displayModeClicked()), true);
273 // =======================================================================
274 // function : mousePressEvent
276 // =======================================================================
277 void View_Widget::mousePressEvent (QMouseEvent* theEvent)
279 if (myController->PressMouseButton (Graphic3d_Vec2i (theEvent->x(), theEvent->y()),
280 keyMouse (theEvent->button()),
281 keyFlag (theEvent->modifiers()),
284 myController->FlushViewEvents (myViewer->GetContext(), myViewer->GetView(), Standard_True);
288 // =======================================================================
289 // function : mouseReleaseEvent
291 // =======================================================================
292 void View_Widget::mouseReleaseEvent (QMouseEvent* theEvent)
294 if (myController->ReleaseMouseButton (Graphic3d_Vec2i (theEvent->x(), theEvent->y()),
295 keyMouse (theEvent->button()),
296 keyFlag (theEvent->modifiers()),
299 myController->FlushViewEvents (myViewer->GetContext(), myViewer->GetView(), Standard_True);
303 // =======================================================================
304 // function : mouseMoveEvent
306 // =======================================================================
307 void View_Widget::mouseMoveEvent (QMouseEvent* theEvent)
309 myController->UpdateMousePosition (Graphic3d_Vec2i (theEvent->x(), theEvent->y()),
310 keyMouse (theEvent->button()),
311 keyFlag (theEvent->modifiers()), Standard_False);
313 myController->FlushViewEvents (myViewer->GetContext(), myViewer->GetView(), Standard_True);
316 // =======================================================================
317 // function : createAction
319 // =======================================================================
320 void View_Widget::createAction (const View_ViewActionType theActionId, const QString& theIcon, const QString& theText,
321 const char* theSlot, const bool isCheckable, const QString& theToolBar,
322 const QString& theStatusBar)
324 QAction* anAction = new QAction (QIcon (theIcon), theText, this);
325 anAction->setToolTip (!theToolBar.isEmpty() ? theToolBar : theText);
326 anAction->setStatusTip (!theStatusBar.isEmpty() ? theStatusBar : theText);
328 anAction->setCheckable (true);
329 connect(anAction, SIGNAL (triggered()) , this, theSlot);
330 myViewActions[theActionId] = anAction;
333 // =======================================================================
334 // function : keyFlag
336 // =======================================================================
337 Aspect_VKeyFlags View_Widget::keyFlag (const int theModifierId)
339 switch (theModifierId)
341 case Qt::NoModifier: return Aspect_VKeyFlags_NONE;
342 case Qt::ShiftModifier: return Aspect_VKeyFlags_SHIFT;
343 case Qt::ControlModifier: return Aspect_VKeyFlags_CTRL;
346 return Aspect_VKeyFlags_NONE;
349 // =======================================================================
350 // function : keyMouse
352 // =======================================================================
353 Aspect_VKeyMouse View_Widget::keyMouse (const int theButtonId)
357 case Qt::NoButton: return Aspect_VKeyMouse_NONE;
358 case Qt::LeftButton: return Aspect_VKeyMouse_LeftButton;
359 case Qt::RightButton: return Aspect_VKeyMouse_RightButton;
360 case Qt::MidButton: return Aspect_VKeyMouse_MiddleButton;
363 return Aspect_VKeyMouse_NONE;