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