Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxViewWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxViewWidget.h"
13 
14 #include <QResizeEvent>
15 #include <QApplication>
16 #include <QDesktopWidget>
17 #include "vtkRenderWindow.h"
18 #include "cxBoundingBox3D.h"
20 #include "cxTypeConversions.h"
21 #include "cxGLHelpers.h"
22 #include "cxOSXHelper.h"
23 #include "cxRenderWindowFactory.h"
24 
25 namespace cx
26 {
27 
28 ViewWidget::ViewWidget(RenderWindowFactoryPtr factory, const QString& uid, const QString& name, QWidget *parent, Qt::WindowFlags f) :
29  inherited(parent, f)
30 {
31  mMTimeHash = 0;
32  this->setContextMenuPolicy(Qt::CustomContextMenu);
33  mZoomFactor = -1.0;
34  vtkRenderWindowPtr rw = factory->getRenderWindow(uid);
35  mView = ViewLinkingViewWidget::create(this, rw);
36  connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(customContextMenuRequestedSlot(const QPoint &)));
37  vtkRenderWindowPtr renderWindow = mView->getRenderWindow();
38  this->SetRenderWindow(renderWindow);
39  mView->getRenderWindow()->GetInteractor()->EnableRenderOff();
40  mView->clear();
41  disableGLHiDPI(this->winId());
42 }
43 
44 void ViewWidget::customContextMenuRequestedSlot(const QPoint& point)
45 {
46  QWidget* sender = dynamic_cast<QWidget*>(this->sender());
47  QPoint pointGlobal = sender->mapToGlobal(point);
48  emit customContextMenuRequestedInGlobalPos(pointGlobal);
49 }
50 
52 {
53  return mView;
54 }
55 
57 {
58  this->getView()->clear();
59 }
60 
62 {
63  return this->getView()->getRenderer();
64 }
65 
67 {
68  // Render is called only when mtime is changed.
69  // At least on MaxOS, this is not done automatically.
70  unsigned long hash = mView->computeTotalMTime();
71 
72  if (hash != mMTimeHash)
73  {
74  this->getRenderWindow()->Render();
75  mMTimeHash = hash;
76 
77  QString msg("During rendering of view: " + this->getView()->getName());
79  }
80 }
81 
82 void ViewWidget::resizeEvent(QResizeEvent * event)
83 {
84  inherited::resizeEvent(event);
85  QSize size = event->size();
86  vtkRenderWindowInteractor* iren = mView->getRenderWindow()->GetInteractor();
87  if (iren != NULL)
88  iren->UpdateSize(size.width(), size.height());
89  emit resized(size);
90 }
91 
92 void ViewWidget::mouseMoveEvent(QMouseEvent* event)
93 {
94  inherited::mouseMoveEvent(event);
95  emit mouseMove(event->x(), event->y(), event->buttons());
96 }
97 
98 void ViewWidget::mousePressEvent(QMouseEvent* event)
99 {
100  // special case for CustusX: when context menu is opened, mousereleaseevent is never called.
101  // this sets the render interactor in a zoom state after each menu call. This hack prevents
102  // the mouse press event in this case.
103  if ((this->contextMenuPolicy() == Qt::CustomContextMenu) && event->buttons().testFlag(Qt::RightButton))
104  return;
105 
106  inherited::mousePressEvent(event);
107  emit mousePress(event->x(), event->y(), event->buttons());
108 }
109 
110 void ViewWidget::mouseReleaseEvent(QMouseEvent* event)
111 {
112  inherited::mouseReleaseEvent(event);
113  emit mouseRelease(event->x(), event->y(), event->buttons());
114 }
115 
116 void ViewWidget::focusInEvent(QFocusEvent* event)
117 {
118  inherited::focusInEvent(event);
119  emit focusChange(event->gotFocus(), event->reason());
120 }
121 
122 void ViewWidget::wheelEvent(QWheelEvent* event)
123 {
124  inherited::wheelEvent(event);
125  emit mouseWheel(event->x(), event->y(), event->delta(), event->orientation(), event->buttons());
126 }
127 
128 void ViewWidget::showEvent(QShowEvent* event)
129 {
130  inherited::showEvent(event);
131  emit shown();
132 }
133 
134 void ViewWidget::paintEvent(QPaintEvent* event)
135 {
136  mView->setModified();
137  inherited::paintEvent(event);
138 }
139 
140 void ViewWidget::setZoomFactor(double factor)
141 {
142  if (similar(factor, mZoomFactor))
143  {
144  return;
145  }
146  mZoomFactor = factor;
147  emit resized(this->size());
148 }
149 
151 {
152  return mZoomFactor;
153 }
154 
156 {
157  return transform(this->get_vpMs().inv(), this->getViewport());
158 }
159 
161 {
162  Vector3D center_vp = this->getViewport().center();
163  double scale = mZoomFactor / this->mmPerPix(); // double zoomFactor = 0.3; // real magnification
164  Transform3D S = createTransformScale(Vector3D(scale, scale, scale));
165  Transform3D T = createTransformTranslate(center_vp);// center of viewport in viewport coordinates
166  Transform3D M_vp_w = T * S; // first scale , then translate to center.
167  return M_vp_w;
168 }
169 
173 {
174  return DoubleBoundingBox3D(0, size().width(), 0, size().height(), 0, 0);
175 }
176 
177 double ViewWidget::mmPerPix() const
178 {
179  // use mean mm/pix over entire screen. DONT use the height of the widget in mm,
180  // this is truncated to the nearest integer.
181  QDesktopWidget* desktop = dynamic_cast<QApplication*>(QApplication::instance())->desktop();
182  QWidget* screen = desktop->screen(desktop->screenNumber(this));
183  double r_h = (double) screen->heightMM() / (double) screen->geometry().height();
184  double r_w = (double) screen->widthMM() / (double) screen->geometry().width();
185  double retval = (r_h + r_w) / 2.0;
186  return retval;
187 }
188 
189 } // namespace cx
void mousePress(int x, int y, Qt::MouseButtons buttons)
virtual vtkRenderWindowPtr getRenderWindow()
Get the vtkRenderWindow used by this View.
Definition: cxViewWidget.h:40
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
void focusChange(bool gotFocus, Qt::FocusReason reason)
void mouseMove(int x, int y, Qt::MouseButtons buttons)
ViewWidget(RenderWindowFactoryPtr factory, const QString &uid="", const QString &name="", QWidget *parent=NULL, Qt::WindowFlags f=0)
constructor
virtual QSize size() const
Definition: cxViewWidget.h:41
Transform3D createTransformScale(const Vector3D &scale_)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
static ViewRepCollectionPtr create(ViewWidget *base, vtkRenderWindowPtr renderWindow)
vtkSmartPointer< class vtkRenderWindow > vtkRenderWindowPtr
cstring_cast_Placeholder cstring_cast(const T &val)
virtual Transform3D get_vpMs() const
virtual vtkRendererPtr getRenderer()
Get the renderer used by this View.
void mouseWheel(int x, int y, int delta, int orientation, Qt::MouseButtons buttons)
#define report_gl_error_text(text)
Definition: cxGLHelpers.h:29
void resized(QSize size)
virtual DoubleBoundingBox3D getViewport() const
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
virtual double getZoomFactor() const
ViewRepCollectionPtr getView()
void mouseRelease(int x, int y, Qt::MouseButtons buttons)
void customContextMenuRequestedInGlobalPos(const QPoint &)
Transform3D createTransformTranslate(const Vector3D &translation)
boost::shared_ptr< class ViewRepCollection > ViewRepCollectionPtr
virtual ~ViewWidget()
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
virtual DoubleBoundingBox3D getViewport_s() const
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
virtual double mmPerPix() const
Vector3D center() const
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
virtual void setZoomFactor(double factor)
boost::shared_ptr< class RenderWindowFactory > RenderWindowFactoryPtr
Namespace for all CustusX production code.