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