CustusX  20.03-rc1
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 #include <QGridLayout>
16 #include "cxViewUtilities.h"
17 #include "vtkRenderWindow.h"
18 #include "cxGLHelpers.h"
19 #include "cxLogger.h"
20 #include "cxMultiViewCache.h"
21 #include "cxRenderWindowFactory.h"
22 
23 namespace cx
24 {
25 
27  ViewCollectionWidget(parent)
28 {
29  mLayout = new QGridLayout(this);
30  this->setLayout(mLayout);
31  mViewCache = MultiViewCache::create(factory);
32 
34  this->initBaseLayout();
35  this->setGridMargin(4);
36  this->setGridSpacing(2);
37 }
38 
40 {
41 }
42 
43 void ViewCollectionWidgetMixed::initBaseLayout()
44 {
45  this->addWidgetToLayout(mLayout, mBaseLayout, LayoutRegion(0,0));
46  mBaseRegion = LayoutRegion(-1,-1);
47  mTotalRegion = LayoutRegion(-1,-1);
48 }
49 
51 {
52  ViewPtr view;
53  mTotalRegion = merge(region, mTotalRegion);
54 
55  if (type==View::VIEW_3D)
56  {
57  //Using cached 3D view don't work if mBaseRegion covers the 3D view region (In some cases.) CX-63
58  this->mViewCache->clearCache();
59  ViewWidget* overlay = this->mViewCache->retrieveView(this, View::VIEW_3D, mBaseLayout->getOffScreenRendering());
60  overlay->getView()->setType(type);
61  overlay->show();
62  mOverlays.push_back(overlay);
63  view = overlay->getView();
64  this->addWidgetToLayout(mLayout, overlay, region);
65  }
66  else
67  {
68  mBaseRegion = merge(region, mBaseRegion);
69  view = mBaseLayout->addView(type, region);
70 
71  // re-add the base widget with updated position in grid
72  this->addWidgetToLayout(mLayout, mBaseLayout, mBaseRegion);
73  }
74  view_utils::setStretchFactors(mLayout, mTotalRegion, 1);
75 
76  return view;
77 }
78 
80 {
81  this->clearViews();
83 }
84 
86 {
88 }
89 
90 void ViewCollectionWidgetMixed::addWidgetToLayout(QGridLayout* layout, QWidget* widget, LayoutRegion region)
91 {
92  layout->addWidget(widget,
93  region.pos.row, region.pos.col,
94  region.span.row, region.span.col);
95 }
96 
98 {
99  mViewCache->clearViews();
100 
101  for (unsigned i=0; i<mOverlays.size(); ++i)
102  {
103  mOverlays[i]->hide();
104  mLayout->removeWidget(mOverlays[i]);
105  }
106  mOverlays.clear();
107 
109  mLayout->removeWidget(mBaseLayout);
110 
111  // rebuild to default state:
112  view_utils::setStretchFactors(mLayout, mTotalRegion, 0);
113  this->initBaseLayout();
114 }
115 
117 {
119  for (unsigned i=0; i<mOverlays.size(); ++i)
120  mOverlays[i]->setModified();
121 }
122 
124 {
125  mBaseLayout->render();
126 
127  for (unsigned i=0; i<mOverlays.size(); ++i)
128  {
129  mOverlays[i]->render();
130  }
131 
132  emit rendered();
133 }
134 
136 {
137  mLayout->setSpacing(val);
139 }
140 
142 {
144  mLayout->setMargin(val);
145 }
146 
148 {
149  return mLayout->spacing();
150 }
151 
153 {
154  return mLayout->margin();
155 }
156 
157 
159 {
160  std::vector<ViewPtr> retval = mBaseLayout->getViews();
161  for (unsigned i=0; i<mOverlays.size(); ++i)
162  retval.push_back(mOverlays[i]->getView());
163  return retval;
164 }
165 
167 {
168  for (unsigned i=0; i<mOverlays.size(); ++i)
169  {
170  if (mOverlays[i]->getView()==view)
171  {
172  QPoint p = mOverlays[i]->mapToGlobal(QPoint(0,0));
173  p = this->mapFromGlobal(p);
174  return p;
175  }
176  }
177 
178  QPoint p = mBaseLayout->getPosition(view);
179  p = mBaseLayout->mapToGlobal(p);
180  p = this->mapFromGlobal(p);
181  return p;
182 }
183 
185 {
187  Qt::ContextMenuPolicy policy = enable ? Qt::CustomContextMenu : Qt::PreventContextMenu;
188  for (unsigned i=0; i<mOverlays.size(); ++i)
189  {
190  mOverlays[i]->setContextMenuPolicy(policy);
191  }
192 }
193 
194 } /* 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:46
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
virtual void setOffScreenRenderingAndClear(bool on)
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:45
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.