File: View_Widget.cxx

package info (click to toggle)
opencascade 7.9.2%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 301,924 kB
  • sloc: cpp: 1,523,264; tcl: 10,159; cs: 5,173; java: 1,554; sh: 1,342; ansic: 827; xml: 699; makefile: 30; javascript: 22
file content (402 lines) | stat: -rw-r--r-- 13,541 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Created on: 2017-06-16
// Created by: Natalia ERMOLAEVA
// Copyright (c) 2017 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.

#if !defined _WIN32
  #define QT_CLEAN_NAMESPACE /* avoid definition of INT32 and INT8 */
#endif

#include <inspector/View_Widget.hxx>

#include <AIS_DisplayMode.hxx>
#include <AIS_ViewController.hxx>
#include <Geom_Axis2Placement.hxx>
#include <Graphic3d_GraphicDriver.hxx>

#include <inspector/View_ToolButton.hxx>
#include <inspector/View_ViewActionType.hxx>
#include <inspector/View_Viewer.hxx>

#include <Standard_WarningsDisable.hxx>
#include <QColorDialog>
#include <QCursor>
#include <QFileInfo>
#include <QMdiSubWindow>
#include <QMenu>
#include <QMessageBox>
#include <QMouseEvent>
#include <QPainter>
#include <QRubberBand>
#include <QStatusBar>
#include <QStyleFactory>
#include <Standard_WarningsRestore.hxx>

#include <stdio.h>

#ifdef _WIN32
  #include <WNT_Window.hxx>
#elif defined(HAVE_XLIB)
  #include <Xw_Window.hxx>
#elif defined(__APPLE__)
  #include <Cocoa_Window.hxx>
#else

#endif

// =======================================================================
// function :  Constructor
// purpose :
// =======================================================================
View_Widget::View_Widget(QWidget*                              theParent,
                         const Handle(AIS_InteractiveContext)& theContext,
                         const bool                            isFitAllActive)
    : QWidget(theParent),
      myFirst(true),
      myDefaultWidth(-1),
      myDefaultHeight(-1),
      myViewIsEnabled(true),
      myHasInitProj(Standard_False),
      myInitVx(0),
      myInitVy(0),
      myInitVz(0)
{
  myViewer = new View_Viewer(View_Viewer::DefaultColor());
  if (!theContext.IsNull())
  {
    myViewer->InitViewer(theContext);
  }
  else
  {
    myViewer->InitViewer(myViewer->CreateStandardViewer());
  }
  myController = new AIS_ViewController();

  setAttribute(Qt::WA_PaintOnScreen);
  setAttribute(Qt::WA_NoSystemBackground);

  setMouseTracking(true);
  setBackgroundRole(QPalette::NoRole);
  // set focus policy to threat QContextMenuEvent from keyboard
  setFocusPolicy(Qt::StrongFocus);

  initViewActions();
  ((View_ToolButton*)myFitAllAction)->SetButtonChecked(isFitAllActive);
}

// =======================================================================
// function : SetPredefinedSize
// purpose :
// =======================================================================
void View_Widget::SetPredefinedSize(int theDefaultWidth, int theDefaultHeight)
{
  myDefaultWidth  = theDefaultWidth;
  myDefaultHeight = theDefaultHeight;
}

// =======================================================================
// function : Init
// purpose :
// =======================================================================
void View_Widget::Init()
{
  myViewer->CreateView();

#ifdef _WIN32
  Aspect_Handle         aWindowHandle = (Aspect_Handle)winId();
  Handle(Aspect_Window) aWnd          = new WNT_Window(aWindowHandle);
#elif defined(HAVE_XLIB)
  Aspect_Drawable                  aWindowHandle = (Aspect_Drawable)winId();
  Handle(Aspect_DisplayConnection) aDispConnection =
    myViewer->GetContext()->CurrentViewer()->Driver()->GetDisplayConnection();
  Handle(Aspect_Window) aWnd = new Xw_Window(aDispConnection, aWindowHandle);
#elif defined(__APPLE__)
  NSView*               aViewHandle = (NSView*)winId();
  Handle(Aspect_Window) aWnd        = new Cocoa_Window(aViewHandle);
#else
  //
#endif
  myViewer->SetWindow(aWnd);

  myViewer->GetView()->SetBackgroundColor(View_Viewer::DefaultColor());
  myViewer->GetView()->MustBeResized();

  if (myHasInitProj)
    myViewer->GetView()->SetProj(myInitVx, myInitVy, myInitVz);
}

// =======================================================================
// function : DisplayMode
// purpose :
// =======================================================================
int View_Widget::DisplayMode() const
{
  return myViewActions[View_ViewActionType_DisplayModeId]->isChecked() ? AIS_Shaded : AIS_WireFrame;
}

// =======================================================================
// function : SetDisplayMode
// purpose :
// =======================================================================
void View_Widget::SetDisplayMode(const int theMode)
{
  myViewActions[View_ViewActionType_DisplayModeId]->setChecked(theMode ? AIS_Shaded
                                                                       : AIS_WireFrame);
}

// =======================================================================
// function : paintEvent
// purpose :
// =======================================================================
void View_Widget::paintEvent(QPaintEvent* /*theEvent*/)
{
#if (QT_VERSION < 0x050000 || QT_VERSION >= 0x050700)
  if (myFirst)
  {
    Init();
    myFirst = false;
  }
#endif

  if (myViewer->GetView())
    myViewer->GetView()->Redraw();
}

// =======================================================================
// function : resizeEvent
// purpose :
// =======================================================================
void View_Widget::resizeEvent(QResizeEvent* /*theEvent*/)
{
#if (QT_VERSION > 0x050000 && QT_VERSION < 0x050700)
  if (myFirst)
  {
    Init();
    myFirst = false;
  }
#endif
  if (myViewer->GetView())
    myViewer->GetView()->MustBeResized();
}

// =======================================================================
// function : sizeHint
// purpose :
// =======================================================================
QSize View_Widget::sizeHint() const
{
  if (myDefaultWidth > 0 && myDefaultHeight > 0)
    return QSize(myDefaultWidth, myDefaultHeight);

  return QWidget::sizeHint();
}

// =======================================================================
// function : SetEnabledView
// purpose :
// =======================================================================
void View_Widget::SetEnabledView(const bool theIsEnabled)
{
  myViewIsEnabled = theIsEnabled;

  if (myViewer->GetView())
    myViewer->GetView()->SetBackgroundColor(theIsEnabled ? View_Viewer::DefaultColor()
                                                         : View_Viewer::DisabledColor());
  ViewAction(View_ViewActionType_DisplayModeId)->setEnabled(theIsEnabled);
}

// =======================================================================
// function : SaveState
// purpose :
// =======================================================================
void View_Widget::SaveState(View_Widget*            theWidget,
                            QMap<QString, QString>& theItems,
                            const QString&          thePrefix)
{
  theItems[thePrefix + "fitall"] = theWidget->ViewAction(View_ViewActionType_FitAllId)->isChecked();
  theItems[thePrefix + "dispmode"] = QString::number(theWidget->DisplayMode());
}

// =======================================================================
// function : RestoreState
// purpose :
// =======================================================================
bool View_Widget::RestoreState(View_Widget*   theWidget,
                               const QString& theKey,
                               const QString& theValue,
                               const QString& thePrefix)
{
  if (theKey == thePrefix + "fitall")
  {
    theWidget->SetActionChecked(View_ViewActionType_FitAllId, theValue.toInt() > 0);
  }
  else if (theKey == thePrefix + "dispmode")
  {
    theWidget->SetDisplayMode(theValue.toInt());
  }
  else
    return false;

  return true;
}

// =======================================================================
// function : onCheckedStateChanged
// purpose :
// =======================================================================
void View_Widget::onCheckedStateChanged(bool isOn)
{
  QWidget* aSentByAction = (QWidget*)sender();

  if (aSentByAction == myFitAllAction)
    emit checkedStateChanged(View_ViewActionType_FitAllId, isOn);
}

// =======================================================================
// function : initViewActions
// purpose :
// =======================================================================
void View_Widget::initViewActions()
{
  if (!myViewActions.empty())
    return;

  myFitAllAction = new View_ToolButton(this); // action for automatic fit all
  connect(myFitAllAction,
          SIGNAL(checkedStateChanged(bool)),
          this,
          SLOT(onCheckedStateChanged(bool)));
  createAction(View_ViewActionType_FitAllId,
               ":/icons/view_fitall.png",
               tr("Fit All"),
               SLOT(OnFitAll()));
  myFitAllAction->setDefaultAction(ViewAction(View_ViewActionType_FitAllId));

  createAction(View_ViewActionType_DisplayModeId,
               ":/icons/view_dm_shading.png",
               tr("Display Mode"),
               SIGNAL(displayModeClicked()),
               true);
}

// =======================================================================
// function : mousePressEvent
// purpose :
// =======================================================================
void View_Widget::mousePressEvent(QMouseEvent* theEvent)
{
  if (myController->PressMouseButton(Graphic3d_Vec2i(theEvent->x(), theEvent->y()),
                                     keyMouse(theEvent->button()),
                                     keyFlag(theEvent->modifiers()),
                                     Standard_False))
  {
    myController->FlushViewEvents(myViewer->GetContext(), myViewer->GetView(), Standard_True);
  }
}

// =======================================================================
// function : mouseReleaseEvent
// purpose :
// =======================================================================
void View_Widget::mouseReleaseEvent(QMouseEvent* theEvent)
{
  if (myController->ReleaseMouseButton(Graphic3d_Vec2i(theEvent->x(), theEvent->y()),
                                       keyMouse(theEvent->button()),
                                       keyFlag(theEvent->modifiers()),
                                       Standard_False))
  {
    myController->FlushViewEvents(myViewer->GetContext(), myViewer->GetView(), Standard_True);
  }
}

// =======================================================================
// function : mouseMoveEvent
// purpose :
// =======================================================================
void View_Widget::mouseMoveEvent(QMouseEvent* theEvent)
{
  if (myViewer->GetView().IsNull())
  {
    return;
  }

  myController->UpdateMousePosition(Graphic3d_Vec2i(theEvent->x(), theEvent->y()),
                                    keyMouse(theEvent->button()),
                                    keyFlag(theEvent->modifiers()),
                                    Standard_False);

  myController->FlushViewEvents(myViewer->GetContext(), myViewer->GetView(), Standard_True);
}

// =======================================================================
// function : createAction
// purpose :
// =======================================================================
void View_Widget::createAction(const View_ViewActionType theActionId,
                               const QString&            theIcon,
                               const QString&            theText,
                               const char*               theSlot,
                               const bool                isCheckable,
                               const QString&            theToolBar,
                               const QString&            theStatusBar)
{
  QAction* anAction = new QAction(QIcon(theIcon), theText, this);
  anAction->setToolTip(!theToolBar.isEmpty() ? theToolBar : theText);
  anAction->setStatusTip(!theStatusBar.isEmpty() ? theStatusBar : theText);
  if (isCheckable)
    anAction->setCheckable(true);
  connect(anAction, SIGNAL(triggered()), this, theSlot);
  myViewActions[theActionId] = anAction;
}

// =======================================================================
// function : keyFlag
// purpose :
// =======================================================================
Aspect_VKeyFlags View_Widget::keyFlag(const int theModifierId)
{
  switch (theModifierId)
  {
    case Qt::NoModifier:
      return Aspect_VKeyFlags_NONE;
    case Qt::ShiftModifier:
      return Aspect_VKeyFlags_SHIFT;
    case Qt::ControlModifier:
      return Aspect_VKeyFlags_CTRL;
    default:
      break;
  }
  return Aspect_VKeyFlags_NONE;
}

// =======================================================================
// function : keyMouse
// purpose :
// =======================================================================
Aspect_VKeyMouse View_Widget::keyMouse(const int theButtonId)
{
  switch (theButtonId)
  {
    case Qt::NoButton:
      return Aspect_VKeyMouse_NONE;
    case Qt::LeftButton:
      return Aspect_VKeyMouse_LeftButton;
    case Qt::RightButton:
      return Aspect_VKeyMouse_RightButton;
    case Qt::MidButton:
      return Aspect_VKeyMouse_MiddleButton;
    default:
      break;
  }
  return Aspect_VKeyMouse_NONE;
}