Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxViewContainer.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 "cxViewContainer.h"
34 
35 #include <QResizeEvent>
36 #include "vtkRenderWindow.h"
37 #include "vtkRenderer.h"
38 #include <QGridLayout>
39 #include "cxViewUtilities.h"
40 #include "cxViewContainerItem.h"
41 #include "cxTypeConversions.h"
42 #include "cxGLHelpers.h"
43 #include "cxOSXHelper.h"
44 
45 namespace cx
46 {
47 
48 ViewContainer::ViewContainer(QWidget *parent, Qt::WindowFlags f) :
49  QVTKWidget(parent, f),
50  mMouseEventTarget(NULL),
51  mRenderWindow(NULL)
52 {
53  this->setContextMenuPolicy(Qt::CustomContextMenu);
54  connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(customContextMenuRequestedSlot(const QPoint &)));
55  mMTimeHash = 0;
56  mMouseEventTarget = NULL;
57  this->setLayout(new QGridLayout);
58  disableGLHiDPI(this->winId());
59 }
60 
62 {
63 }
64 
69 {
70  QLayoutItem *item;
71  while ((item = getGridLayout()->takeAt(0)) != 0)
72  {
73  ViewItem* viewItem = dynamic_cast<ViewItem*>(item);
74  delete viewItem;
75  }
76  view_utils::setStretchFactors(this->getGridLayout(), LayoutRegion(0, 0, 10, 10), 0);
77  this->setModified();
78  mMouseEventTarget = NULL;
79 }
80 
85 {
86  return dynamic_cast<QGridLayout*>(layout());
87 }
88 
89 void ViewContainer::paintEvent(QPaintEvent* event)
90 {
91  inherited_widget::paintEvent(event);
92  this->setModified();
93 }
94 
96 {
97  if (this->getGridLayout())
98  {
99  for (int i = 0; i < this->getGridLayout()->count(); ++i)
100  {
101  this->getViewItem(i)->getView()->setModified();
102  }
103  }
104  mMTimeHash = 0;
105 }
106 
108 {
109  return dynamic_cast<ViewItem*>(this->getGridLayout()->itemAt(index));
110 }
111 
116 ViewItem *ViewContainer::addView(QString uid, LayoutRegion region, QString name)
117 {
118  this->initializeRenderWindow();
119 
120  // Create a viewItem for this view
121  ViewItem *item = new ViewItem(uid, name, this, mRenderWindow, QRect());
122  if (getGridLayout())
123  getGridLayout()->addItem(item,
124  region.pos.row, region.pos.col,
125  region.span.row, region.span.col);
126  view_utils::setStretchFactors(this->getGridLayout(), region, 1);
127 
128  return item;
129 }
130 
131 void ViewContainer::initializeRenderWindow()
132 {
133  if (mRenderWindow)
134  return;
135 
136  mRenderWindow = vtkRenderWindowPtr::New();
137  this->SetRenderWindow(mRenderWindow);
138  mRenderWindow->GetInteractor()->EnableRenderOff();
139 
140  this->addBackgroundRenderer();
141 }
142 
143 void ViewContainer::addBackgroundRenderer()
144 {
145  vtkRendererPtr renderer = vtkRendererPtr::New();
146  mRenderWindow->AddRenderer(renderer);
147  renderer->SetViewport(0,0,1,1);
148  QColor background = palette().color(QPalette::Background);
149  renderer->SetBackground(background.redF(), background.greenF(), background.blueF());
150 }
151 
152 void ViewContainer::customContextMenuRequestedSlot(const QPoint& point)
153 {
154  ViewItem* item = this->findViewItem(point);
155  if (!item)
156  return;
157 
158  QWidget* sender = dynamic_cast<QWidget*>(this->sender());
159  QPoint pointGlobal = sender->mapToGlobal(point);
160 
161  item->customContextMenuRequestedGlobalSlot(pointGlobal);
162 }
163 
164 void ViewContainer::mouseMoveEvent(QMouseEvent* event)
165 {
166  inherited_widget::mouseMoveEvent(event);
167 
168  if (mMouseEventTarget)
169  {
170  QPoint p = this->convertToItemSpace(event->pos(), mMouseEventTarget);
171  mMouseEventTarget->mouseMoveSlot(p.x(), p.y(), event->buttons());
172  }
173 }
174 
175 void ViewContainer::mousePressEvent(QMouseEvent* event)
176 {
177  // special case for CustusX: when context menu is opened, mousereleaseevent is never called.
178  // this sets the render interactor in a zoom state after each menu call. This hack prevents
179  // the mouse press event in this case.
180  // NOTE: this doesnt seem to be the case in this class - investigate
181  if ((this->contextMenuPolicy() == Qt::CustomContextMenu) && event->buttons().testFlag(Qt::RightButton))
182  return;
183 
184  inherited_widget::mousePressEvent(event);
185 
186  mMouseEventTarget = this->findViewItem(event->pos());
187  if (mMouseEventTarget)
188  {
189  QPoint p = this->convertToItemSpace(event->pos(), mMouseEventTarget);
190  mMouseEventTarget->mousePressSlot(p.x(), p.y(), event->buttons());
191  }
192 }
193 
194 QPoint ViewContainer::convertToItemSpace(const QPoint &pos, ViewItem* item) const
195 {
196  QRect r = item->geometry();
197  QPoint retval(pos.x() - r.left(), pos.y() - r.top());
198  return retval;
199 }
200 
201 ViewItem* ViewContainer::findViewItem(const QPoint &pos)
202 {
203  for (int i = 0; getGridLayout() && i < getGridLayout()->count(); ++i)
204  {
205  ViewItem *item = this->getViewItem(i);
206  QRect r = item->geometry();
207  if (r.contains(pos))
208  return item;
209  }
210  return NULL;
211 }
212 
213 void ViewContainer::mouseReleaseEvent(QMouseEvent* event)
214 {
215  inherited_widget::mouseReleaseEvent(event);
216 
217  if (mMouseEventTarget)
218  {
219  QPoint p = this->convertToItemSpace(event->pos(), mMouseEventTarget);
220  mMouseEventTarget->mouseReleaseSlot(p.x(), p.y(), event->buttons());
221  mMouseEventTarget = NULL;
222  }
223 }
224 
225 void ViewContainer::focusInEvent(QFocusEvent* event)
226 {
227  inherited_widget::focusInEvent(event);
228 }
229 
230 void ViewContainer::wheelEvent(QWheelEvent* event)
231 {
232  inherited_widget::wheelEvent(event);
233 
234  ViewItem *item = this->findViewItem(event->pos());
235  if (item)
236  {
237  QPoint p = this->convertToItemSpace(event->pos(), item);
238  item->mouseWheelSlot(p.x(), p.y(),
239  event->delta(), event->orientation(), event->buttons());
240  }
241 }
242 
243 void ViewContainer::showEvent(QShowEvent* event)
244 {
245  inherited_widget::showEvent(event);
246 }
247 
249 {
250  // First, calculate if anything has changed
251  long hash = 0;
252  for (int i = 0; getGridLayout() && i < getGridLayout()->count(); ++i)
253  {
254  ViewItem *item = this->getViewItem(i);
255  hash += item->getView()->computeTotalMTime();
256  }
257  // Then, if anything has changed, render everything anew
258  if (hash != mMTimeHash)
259  {
260  this->doRender();
261  mMTimeHash = hash;
262 
263  QString msg("During rendering of ViewContainer");
265  }
266 }
267 
269 {
270  this->getRenderWindow()->Render();
271 }
272 
273 void ViewContainer::resizeEvent( QResizeEvent *event)
274 {
275  inherited_widget::resizeEvent(event);
276  this->setModified();
277  this->getGridLayout()->update();
278 }
279 
280 
281 } /* namespace cx */
virtual void doRender()
cstring_cast_Placeholder cstring_cast(const T &val)
LayoutPosition span
size of region
Definition: cxLayoutData.h:67
void mouseReleaseSlot(int x, int y, Qt::MouseButtons buttons)
#define report_gl_error_text(text)
Definition: cxGLHelpers.h:51
ViewContainer(QWidget *parent=NULL, Qt::WindowFlags f=0)
ViewItem * addView(QString uid, LayoutRegion region, QString name="")
void mousePressSlot(int x, int y, Qt::MouseButtons buttons)
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
ViewItem * getViewItem(int index)
vtkRenderWindowPtr getRenderWindow()
unsigned long mMTimeHash
sum of all MTimes in objects rendered
void mouseMoveSlot(int x, int y, Qt::MouseButtons buttons)
void setStretchFactors(QGridLayout *layout, LayoutRegion region, int stretchFactor)
virtual void setModified()
void renderAll()
Use this function to render all views at once. Do not call render on each view.
virtual void clear()
LayoutPosition pos
start position of region
Definition: cxLayoutData.h:66
virtual QGridLayout * getGridLayout()
ViewRepCollectionPtr getView()
vtkRenderWindowPtr mRenderWindow
ViewItem * mMouseEventTarget