Fraxinus  16.5.0-fx-rc8
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 
42 namespace cx
43 {
44 
46  ViewCollectionWidget(parent)
47 {
48  mLayout = new QGridLayout(this);
49  this->setLayout(mLayout);
50  mViewCacheOverlay.reset(new ViewCache<ViewWidget>(this, "Overlay"));
51 
52  this->initBaseLayout();
53 }
54 
56 {
57 }
58 
59 void ViewCollectionWidgetMixed::initBaseLayout()
60 {
61  mBaseLayout = new ViewCollectionWidgetUsingViewContainer(this);
62  this->addWidgetToLayout(mLayout, mBaseLayout, LayoutRegion(0,0));
63  mBaseRegion = LayoutRegion(-1,-1);
64  mTotalRegion = LayoutRegion(-1,-1);
65 
66  this->setGridMargin(4);
67  this->setGridSpacing(2);
68 }
69 
71 {
72  ViewPtr view;
73  mTotalRegion = merge(region, mTotalRegion);
74 
75  if (type==View::VIEW_3D)
76  {
77  ViewWidget* overlay = this->mViewCacheOverlay->retrieveView();
78  overlay->getView()->setType(type);
79  overlay->show();
80  mOverlays.push_back(overlay);
81  view = overlay->getView();
82  this->addWidgetToLayout(mLayout, overlay, region);
83  }
84  else
85  {
86  mBaseRegion = merge(region, mBaseRegion);
87  view = mBaseLayout->addView(type, region);
88  // re-add the base widget with updated position in grid
89  this->addWidgetToLayout(mLayout, mBaseLayout, mBaseRegion);
90  }
91  view_utils::setStretchFactors(mLayout, mTotalRegion, 1);
92 
93  return view;
94 }
95 
96 void ViewCollectionWidgetMixed::addWidgetToLayout(QGridLayout* layout, QWidget* widget, LayoutRegion region)
97 {
98  layout->addWidget(widget,
99  region.pos.row, region.pos.col,
100  region.span.row, region.span.col);
101 }
102 
104 {
105  mViewCacheOverlay->clearUsedViews();
106 
107  for (unsigned i=0; i<mOverlays.size(); ++i)
108  {
109  mOverlays[i]->hide();
110  mLayout->removeWidget(mOverlays[i]);
111  }
112  mOverlays.clear();
113 
114  mBaseLayout->clearViews();
115  mLayout->removeWidget(mBaseLayout);
116 
117  // rebuild to default state:
118  view_utils::setStretchFactors(mLayout, mTotalRegion, 0);
119  this->addWidgetToLayout(mLayout, mBaseLayout, LayoutRegion(0,0));
120  mBaseRegion = LayoutRegion(-1,-1);
121  mTotalRegion = LayoutRegion(-1,-1);
122 }
123 
125 {
126  mBaseLayout->setModified();
127  for (unsigned i=0; i<mOverlays.size(); ++i)
128  mOverlays[i]->setModified();
129 }
130 
132 {
133  mBaseLayout->render();
134 
135  for (unsigned i=0; i<mOverlays.size(); ++i)
136  {
137  mOverlays[i]->render();
138  }
139 
140  emit rendered();
141 }
142 
144 {
145  mLayout->setSpacing(val);
146  mBaseLayout->setGridSpacing(val);
147 }
148 
150 {
151  mBaseLayout->setGridMargin(0);
152  mLayout->setMargin(val);
153 }
154 
156 {
157  return mLayout->spacing();
158 }
159 
161 {
162  return mLayout->margin();
163 }
164 
165 
167 {
168  std::vector<ViewPtr> retval = mBaseLayout->getViews();
169  for (unsigned i=0; i<mOverlays.size(); ++i)
170  retval.push_back(mOverlays[i]->getView());
171  return retval;
172 }
173 
175 {
176  for (unsigned i=0; i<mOverlays.size(); ++i)
177  {
178  if (mOverlays[i]->getView()==view)
179  {
180  QPoint p = mOverlays[i]->mapToGlobal(QPoint(0,0));
181  p = this->mapFromGlobal(p);
182  return p;
183  }
184  }
185 
186  QPoint p = mBaseLayout->getPosition(view);
187  p = mBaseLayout->mapToGlobal(p);
188  p = this->mapFromGlobal(p);
189  return p;
190 }
191 
192 } /* namespace cx */
virtual void setModified()=0
virtual std::vector< ViewPtr > getViews()=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
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 QPoint getPosition(ViewPtr view)
virtual QPoint getPosition(ViewPtr view)=0
ViewPtr addView(View::Type type, LayoutRegion region)
LayoutRegion merge(LayoutRegion a, LayoutRegion b)