CustusX  16.12
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
43 namespace cx
44 {
45 
47  ViewCollectionWidget(parent)
48 {
49  mLayout = new QGridLayout(this);
50  this->setLayout(mLayout);
51  mViewCache = MultiViewCache::create();
52 
54  this->initBaseLayout();
55  this->setGridMargin(4);
56  this->setGridSpacing(2);
57 }
58 
60 {
61 }
62 
63 void ViewCollectionWidgetMixed::initBaseLayout()
64 {
65  this->addWidgetToLayout(mLayout, mBaseLayout, LayoutRegion(0,0));
66  mBaseRegion = LayoutRegion(-1,-1);
67  mTotalRegion = LayoutRegion(-1,-1);
68 }
69 
71 {
72  ViewPtr view;
73  mTotalRegion = merge(region, mTotalRegion);
74 
75  if (type==View::VIEW_3D)
76  {
77  //Using cached 3D view don't work if mBaseRegion covers the 3D view region (In some cases.) CX-63
78  this->mViewCache->clearCache();
79  ViewWidget* overlay = this->mViewCache->retrieveView(this, View::VIEW_3D, mBaseLayout->getOffScreenRendering());
80  overlay->getView()->setType(type);
81  overlay->show();
82  mOverlays.push_back(overlay);
83  view = overlay->getView();
84  this->addWidgetToLayout(mLayout, overlay, region);
85  }
86  else
87  {
88  mBaseRegion = merge(region, mBaseRegion);
89  view = mBaseLayout->addView(type, region);
90 
91  // re-add the base widget with updated position in grid
92  this->addWidgetToLayout(mLayout, mBaseLayout, mBaseRegion);
93  }
94  view_utils::setStretchFactors(mLayout, mTotalRegion, 1);
95 
96  return view;
97 }
98 
100 {
101  this->clearViews();
103 }
104 
106 {
108 }
109 
110 void ViewCollectionWidgetMixed::addWidgetToLayout(QGridLayout* layout, QWidget* widget, LayoutRegion region)
111 {
112  layout->addWidget(widget,
113  region.pos.row, region.pos.col,
114  region.span.row, region.span.col);
115 }
116 
118 {
119  mViewCache->clearViews();
120 
121  for (unsigned i=0; i<mOverlays.size(); ++i)
122  {
123  mOverlays[i]->hide();
124  mLayout->removeWidget(mOverlays[i]);
125  }
126  mOverlays.clear();
127 
129  mLayout->removeWidget(mBaseLayout);
130 
131  // rebuild to default state:
132  view_utils::setStretchFactors(mLayout, mTotalRegion, 0);
133  this->initBaseLayout();
134 }
135 
137 {
139  for (unsigned i=0; i<mOverlays.size(); ++i)
140  mOverlays[i]->setModified();
141 }
142 
144 {
145  mBaseLayout->render();
146 
147  for (unsigned i=0; i<mOverlays.size(); ++i)
148  {
149  mOverlays[i]->render();
150  }
151 
152  emit rendered();
153 }
154 
156 {
157  mLayout->setSpacing(val);
159 }
160 
162 {
164  mLayout->setMargin(val);
165 }
166 
168 {
169  return mLayout->spacing();
170 }
171 
173 {
174  return mLayout->margin();
175 }
176 
177 
179 {
180  std::vector<ViewPtr> retval = mBaseLayout->getViews();
181  for (unsigned i=0; i<mOverlays.size(); ++i)
182  retval.push_back(mOverlays[i]->getView());
183  return retval;
184 }
185 
187 {
188  for (unsigned i=0; i<mOverlays.size(); ++i)
189  {
190  if (mOverlays[i]->getView()==view)
191  {
192  QPoint p = mOverlays[i]->mapToGlobal(QPoint(0,0));
193  p = this->mapFromGlobal(p);
194  return p;
195  }
196  }
197 
198  QPoint p = mBaseLayout->getPosition(view);
199  p = mBaseLayout->mapToGlobal(p);
200  p = this->mapFromGlobal(p);
201  return p;
202 }
203 
205 {
207  Qt::ContextMenuPolicy policy = enable ? Qt::CustomContextMenu : Qt::PreventContextMenu;
208  for (unsigned i=0; i<mOverlays.size(); ++i)
209  {
210  mOverlays[i]->setContextMenuPolicy(policy);
211  }
212 }
213 
214 } /* namespace cx */
static MultiViewCachePtr create()
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
virtual void setGridMargin(int val)=0
virtual void setGridSpacing(int val)=0
virtual void enableContextMenuForViews(bool enable)=0
virtual void setOffScreenRenderingAndClear(bool on)
ViewRepCollectionPtr getView()
void setStretchFactors(QGridLayout *layout, LayoutRegion region, int stretchFactor)
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)
LayoutRegion merge(LayoutRegion a, LayoutRegion b)