Fraxinus  17.12-rc2
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxViewWidget.h"
34 
35 #include <QResizeEvent>
36 #include <QApplication>
37 #include <QDesktopWidget>
38 #include "vtkRenderWindow.h"
39 #include "cxBoundingBox3D.h"
41 #include "cxTypeConversions.h"
42 #include "cxGLHelpers.h"
43 #include "cxOSXHelper.h"
44 #include "cxRenderWindowFactory.h"
45 
46 namespace cx
47 {
48 
49 ViewWidget::ViewWidget(RenderWindowFactoryPtr factory, const QString& uid, const QString& name, QWidget *parent, Qt::WindowFlags f) :
50  inherited(parent, f)
51 {
52  mMTimeHash = 0;
53  this->setContextMenuPolicy(Qt::CustomContextMenu);
54  mZoomFactor = -1.0;
55  vtkRenderWindowPtr rw = factory->getRenderWindow(uid);
56  mView = ViewLinkingViewWidget::create(this, rw);
57  connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(customContextMenuRequestedSlot(const QPoint &)));
58  vtkRenderWindowPtr renderWindow = mView->getRenderWindow();
59  this->SetRenderWindow(renderWindow);
60  mView->getRenderWindow()->GetInteractor()->EnableRenderOff();
61  mView->clear();
62  disableGLHiDPI(this->winId());
63 }
64 
65 void ViewWidget::customContextMenuRequestedSlot(const QPoint& point)
66 {
67  QWidget* sender = dynamic_cast<QWidget*>(this->sender());
68  QPoint pointGlobal = sender->mapToGlobal(point);
69  emit customContextMenuRequestedInGlobalPos(pointGlobal);
70 }
71 
73 {
74  return mView;
75 }
76 
78 {
79  this->getView()->clear();
80 }
81 
83 {
84  return this->getView()->getRenderer();
85 }
86 
88 {
89  // Render is called only when mtime is changed.
90  // At least on MaxOS, this is not done automatically.
91  unsigned long hash = mView->computeTotalMTime();
92 
93  if (hash != mMTimeHash)
94  {
95  this->getRenderWindow()->Render();
96  mMTimeHash = hash;
97 
98  QString msg("During rendering of view: " + this->getView()->getName());
100  }
101 }
102 
103 void ViewWidget::resizeEvent(QResizeEvent * event)
104 {
105  inherited::resizeEvent(event);
106  QSize size = event->size();
107  vtkRenderWindowInteractor* iren = mView->getRenderWindow()->GetInteractor();
108  if (iren != NULL)
109  iren->UpdateSize(size.width(), size.height());
110  emit resized(size);
111 }
112 
113 void ViewWidget::mouseMoveEvent(QMouseEvent* event)
114 {
115  inherited::mouseMoveEvent(event);
116  emit mouseMove(event->x(), event->y(), event->buttons());
117 }
118 
119 void ViewWidget::mousePressEvent(QMouseEvent* event)
120 {
121  // special case for CustusX: when context menu is opened, mousereleaseevent is never called.
122  // this sets the render interactor in a zoom state after each menu call. This hack prevents
123  // the mouse press event in this case.
124  if ((this->contextMenuPolicy() == Qt::CustomContextMenu) && event->buttons().testFlag(Qt::RightButton))
125  return;
126 
127  inherited::mousePressEvent(event);
128  emit mousePress(event->x(), event->y(), event->buttons());
129 }
130 
131 void ViewWidget::mouseReleaseEvent(QMouseEvent* event)
132 {
133  inherited::mouseReleaseEvent(event);
134  emit mouseRelease(event->x(), event->y(), event->buttons());
135 }
136 
137 void ViewWidget::focusInEvent(QFocusEvent* event)
138 {
139  inherited::focusInEvent(event);
140  emit focusChange(event->gotFocus(), event->reason());
141 }
142 
143 void ViewWidget::wheelEvent(QWheelEvent* event)
144 {
145  inherited::wheelEvent(event);
146  emit mouseWheel(event->x(), event->y(), event->delta(), event->orientation(), event->buttons());
147 }
148 
149 void ViewWidget::showEvent(QShowEvent* event)
150 {
151  inherited::showEvent(event);
152  emit shown();
153 }
154 
155 void ViewWidget::paintEvent(QPaintEvent* event)
156 {
157  mView->setModified();
158  inherited::paintEvent(event);
159 }
160 
161 void ViewWidget::setZoomFactor(double factor)
162 {
163  if (similar(factor, mZoomFactor))
164  {
165  return;
166  }
167  mZoomFactor = factor;
168  emit resized(this->size());
169 }
170 
172 {
173  return mZoomFactor;
174 }
175 
177 {
178  return transform(this->get_vpMs().inv(), this->getViewport());
179 }
180 
182 {
183  Vector3D center_vp = this->getViewport().center();
184  double scale = mZoomFactor / this->mmPerPix(); // double zoomFactor = 0.3; // real magnification
185  Transform3D S = createTransformScale(Vector3D(scale, scale, scale));
186  Transform3D T = createTransformTranslate(center_vp);// center of viewport in viewport coordinates
187  Transform3D M_vp_w = T * S; // first scale , then translate to center.
188  return M_vp_w;
189 }
190 
194 {
195  return DoubleBoundingBox3D(0, size().width(), 0, size().height(), 0, 0);
196 }
197 
198 double ViewWidget::mmPerPix() const
199 {
200  // use mean mm/pix over entire screen. DONT use the height of the widget in mm,
201  // this is truncated to the nearest integer.
202  QDesktopWidget* desktop = dynamic_cast<QApplication*>(QApplication::instance())->desktop();
203  QWidget* screen = desktop->screen(desktop->screenNumber(this));
204  double r_h = (double) screen->heightMM() / (double) screen->geometry().height();
205  double r_w = (double) screen->widthMM() / (double) screen->geometry().width();
206  double retval = (r_h + r_w) / 2.0;
207  return retval;
208 }
209 
210 } // namespace cx
void mousePress(int x, int y, Qt::MouseButtons buttons)
virtual vtkRenderWindowPtr getRenderWindow()
Get the vtkRenderWindow used by this View.
Definition: cxViewWidget.h:61
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:62
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:50
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:63
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.