CustusX  16.12
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
45 namespace cx
46 {
47 
48 ViewWidget::ViewWidget(const QString& uid, const QString& name, QWidget *parent, Qt::WindowFlags f) :
49  inherited(parent, f)
50 {
51  mMTimeHash = 0;
52  this->setContextMenuPolicy(Qt::CustomContextMenu);
53  mZoomFactor = -1.0;
54  vtkRenderWindowPtr rw = vtkRenderWindowPtr::New();
55  mView = ViewLinkingViewWidget::create(this, rw);
56  connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(customContextMenuRequestedSlot(const QPoint &)));
57  this->SetRenderWindow(mView->getRenderWindow());
58  mView->getRenderWindow()->GetInteractor()->EnableRenderOff();
59  mView->clear();
60  disableGLHiDPI(this->winId());
61 }
62 
63 void ViewWidget::customContextMenuRequestedSlot(const QPoint& point)
64 {
65  QWidget* sender = dynamic_cast<QWidget*>(this->sender());
66  QPoint pointGlobal = sender->mapToGlobal(point);
67  emit customContextMenuRequestedInGlobalPos(pointGlobal);
68 }
69 
71 {
72  return mView;
73 }
74 
76 {
77  this->getView()->clear();
78 }
79 
81 {
82  return this->getView()->getRenderer();
83 }
84 
86 {
87  // Render is called only when mtime is changed.
88  // At least on MaxOS, this is not done automatically.
89  unsigned long hash = mView->computeTotalMTime();
90 
91  if (hash != mMTimeHash)
92  {
93  this->getRenderWindow()->Render();
94  mMTimeHash = hash;
95 
96  QString msg("During rendering of view: " + this->getView()->getName());
98  }
99 }
100 
101 void ViewWidget::resizeEvent(QResizeEvent * event)
102 {
103  inherited::resizeEvent(event);
104  QSize size = event->size();
105  vtkRenderWindowInteractor* iren = mView->getRenderWindow()->GetInteractor();
106  if (iren != NULL)
107  iren->UpdateSize(size.width(), size.height());
108  emit resized(size);
109 }
110 
111 void ViewWidget::mouseMoveEvent(QMouseEvent* event)
112 {
113  inherited::mouseMoveEvent(event);
114  emit mouseMove(event->x(), event->y(), event->buttons());
115 }
116 
117 void ViewWidget::mousePressEvent(QMouseEvent* event)
118 {
119  // special case for CustusX: when context menu is opened, mousereleaseevent is never called.
120  // this sets the render interactor in a zoom state after each menu call. This hack prevents
121  // the mouse press event in this case.
122  if ((this->contextMenuPolicy() == Qt::CustomContextMenu) && event->buttons().testFlag(Qt::RightButton))
123  return;
124 
125  inherited::mousePressEvent(event);
126  emit mousePress(event->x(), event->y(), event->buttons());
127 }
128 
129 void ViewWidget::mouseReleaseEvent(QMouseEvent* event)
130 {
131  inherited::mouseReleaseEvent(event);
132  emit mouseRelease(event->x(), event->y(), event->buttons());
133 }
134 
135 void ViewWidget::focusInEvent(QFocusEvent* event)
136 {
137  inherited::focusInEvent(event);
138  emit focusChange(event->gotFocus(), event->reason());
139 }
140 
141 void ViewWidget::wheelEvent(QWheelEvent* event)
142 {
143  inherited::wheelEvent(event);
144  emit mouseWheel(event->x(), event->y(), event->delta(), event->orientation(), event->buttons());
145 }
146 
147 void ViewWidget::showEvent(QShowEvent* event)
148 {
149  inherited::showEvent(event);
150  emit shown();
151 }
152 
153 void ViewWidget::paintEvent(QPaintEvent* event)
154 {
155  mView->setModified();
156  inherited::paintEvent(event);
157 }
158 
159 void ViewWidget::setZoomFactor(double factor)
160 {
161  if (similar(factor, mZoomFactor))
162  {
163  return;
164  }
165  mZoomFactor = factor;
166  emit resized(this->size());
167 }
168 
170 {
171  return mZoomFactor;
172 }
173 
175 {
176  return transform(this->get_vpMs().inv(), this->getViewport());
177 }
178 
180 {
181  Vector3D center_vp = this->getViewport().center();
182  double scale = mZoomFactor / this->mmPerPix(); // double zoomFactor = 0.3; // real magnification
183  Transform3D S = createTransformScale(Vector3D(scale, scale, scale));
184  Transform3D T = createTransformTranslate(center_vp);// center of viewport in viewport coordinates
185  Transform3D M_vp_w = T * S; // first scale , then translate to center.
186  return M_vp_w;
187 }
188 
192 {
193  return DoubleBoundingBox3D(0, size().width(), 0, size().height(), 0, 0);
194 }
195 
196 double ViewWidget::mmPerPix() const
197 {
198  // use mean mm/pix over entire screen. DONT use the height of the widget in mm,
199  // this is truncated to the nearest integer.
200  QDesktopWidget* desktop = dynamic_cast<QApplication*>(QApplication::instance())->desktop();
201  QWidget* screen = desktop->screen(desktop->screenNumber(this));
202  double r_h = (double) screen->heightMM() / (double) screen->geometry().height();
203  double r_w = (double) screen->widthMM() / (double) screen->geometry().width();
204  double retval = (r_h + r_w) / 2.0;
205  return retval;
206 }
207 
208 } // namespace cx
void mousePress(int x, int y, Qt::MouseButtons buttons)
virtual vtkRenderWindowPtr getRenderWindow()
Get the vtkRenderWindow used by this View.
Definition: cxViewWidget.h:60
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
void focusChange(bool gotFocus, Qt::FocusReason reason)
void mouseMove(int x, int y, Qt::MouseButtons buttons)
virtual QSize size() const
Definition: cxViewWidget.h:61
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:51
ViewWidget(const QString &uid="", const QString &name="", QWidget *parent=NULL, Qt::WindowFlags f=0)
constructor
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)