Fraxinus  17.12-rc2
An IGT application
cxViewCollectionWidgetMixed.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 
34 #include <QGridLayout>
37 #include "cxViewUtilities.h"
38 #include "vtkRenderWindow.h"
39 #include "cxGLHelpers.h"
40 #include "cxLogger.h"
41 #include "cxMultiViewCache.h"
42 #include "cxRenderWindowFactory.h"
43 
44 namespace cx
45 {
46 
48  ViewCollectionWidget(parent)
49 {
50  mLayout = new QGridLayout(this);
51  this->setLayout(mLayout);
52  mViewCache = MultiViewCache::create(factory);
53 
55  this->initBaseLayout();
56  this->setGridMargin(4);
57  this->setGridSpacing(2);
58 }
59 
61 {
62 }
63 
64 void ViewCollectionWidgetMixed::initBaseLayout()
65 {
66  this->addWidgetToLayout(mLayout, mBaseLayout, LayoutRegion(0,0));
67  mBaseRegion = LayoutRegion(-1,-1);
68  mTotalRegion = LayoutRegion(-1,-1);
69 }
70 
72 {
73  ViewPtr view;
74  mTotalRegion = merge(region, mTotalRegion);
75 
76  if (type==View::VIEW_3D)
77  {
78  //Using cached 3D view don't work if mBaseRegion covers the 3D view region (In some cases.) CX-63
79  this->mViewCache->clearCache();
80  ViewWidget* overlay = this->mViewCache->retrieveView(this, View::VIEW_3D, mBaseLayout->getOffScreenRendering());
81  overlay->getView()->setType(type);
82  overlay->show();
83  mOverlays.push_back(overlay);
84  view = overlay->getView();
85  this->addWidgetToLayout(mLayout, overlay, region);
86  }
87  else
88  {
89  mBaseRegion = merge(region, mBaseRegion);
90  view = mBaseLayout->addView(type, region);
91 
92  // re-add the base widget with updated position in grid
93  this->addWidgetToLayout(mLayout, mBaseLayout, mBaseRegion);
94  }
95  view_utils::setStretchFactors(mLayout, mTotalRegion, 1);
96 
97  return view;
98 }
99 
101 {
102  this->clearViews();
104 }
105 
107 {
109 }
110 
111 void ViewCollectionWidgetMixed::addWidgetToLayout(QGridLayout* layout, QWidget* widget, LayoutRegion region)
112 {
113  layout->addWidget(widget,
114  region.pos.row, region.pos.col,
115  region.span.row, region.span.col);
116 }
117 
119 {
120  mViewCache->clearViews();
121 
122  for (unsigned i=0; i<mOverlays.size(); ++i)
123  {
124  mOverlays[i]->hide();
125  mLayout->removeWidget(mOverlays[i]);
126  }
127  mOverlays.clear();
128 
130  mLayout->removeWidget(mBaseLayout);
131 
132  // rebuild to default state:
133  view_utils::setStretchFactors(mLayout, mTotalRegion, 0);
134  this->initBaseLayout();
135 }
136 
138 {
140  for (unsigned i=0; i<mOverlays.size(); ++i)
141  mOverlays[i]->setModified();
142 }
143 
145 {
146  mBaseLayout->render();
147 
148  for (unsigned i=0; i<mOverlays.size(); ++i)
149  {
150  mOverlays[i]->render();
151  }
152 
153  emit rendered();
154 }
155 
157 {
158  mLayout->setSpacing(val);
160 }
161 
163 {
165  mLayout->setMargin(val);
166 }
167 
169 {
170  return mLayout->spacing();
171 }
172 
174 {
175  return mLayout->margin();
176 }
177 
178 
180 {
181  std::vector<ViewPtr> retval = mBaseLayout->getViews();
182  for (unsigned i=0; i<mOverlays.size(); ++i)
183  retval.push_back(mOverlays[i]->getView());
184  return retval;
185 }
186 
188 {
189  for (unsigned i=0; i<mOverlays.size(); ++i)
190  {
191  if (mOverlays[i]->getView()==view)
192  {
193  QPoint p = mOverlays[i]->mapToGlobal(QPoint(0,0));
194  p = this->mapFromGlobal(p);
195  return p;
196  }
197  }
198 
199  QPoint p = mBaseLayout->getPosition(view);
200  p = mBaseLayout->mapToGlobal(p);
201  p = this->mapFromGlobal(p);
202  return p;
203 }
204 
206 {
208  Qt::ContextMenuPolicy policy = enable ? Qt::CustomContextMenu : Qt::PreventContextMenu;
209  for (unsigned i=0; i<mOverlays.size(); ++i)
210  {
211  mOverlays[i]->setContextMenuPolicy(policy);
212  }
213 }
214 
215 } /* namespace cx */
virtual void setModified()=0
virtual std::vector< ViewPtr > getViews()=0
virtual void setOffScreenRenderingAndClear(bool on)=0
virtual void render()=0
virtual void clearViews()=0
LayoutPosition span
size of region
Definition: cxLayoutData.h:67
boost::shared_ptr< class View > ViewPtr
ViewCollectionWidgetMixed(RenderWindowFactoryPtr factory, QWidget *parent)
virtual void setGridMargin(int val)=0
virtual void setGridSpacing(int val)=0
virtual void enableContextMenuForViews(bool enable)=0
ViewRepCollectionPtr getView()
void setStretchFactors(QGridLayout *layout, LayoutRegion region, int stretchFactor)
static MultiViewCachePtr create(RenderWindowFactoryPtr factory)
virtual std::vector< ViewPtr > getViews()
LayoutPosition pos
start position of region
Definition: cxLayoutData.h:66
virtual ViewPtr addView(View::Type type, LayoutRegion region)=0
virtual bool getOffScreenRendering() const =0
virtual QPoint getPosition(ViewPtr view)
virtual void enableContextMenuForViews(bool enable)
virtual QPoint getPosition(ViewPtr view)=0
ViewPtr addView(View::Type type, LayoutRegion region)
boost::shared_ptr< class RenderWindowFactory > RenderWindowFactoryPtr
LayoutRegion merge(LayoutRegion a, LayoutRegion b)
Namespace for all CustusX production code.