CustusX  22.04-rc3
An IGT application
cxReplacableContentWidget.h
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 #ifndef CXREPLACABLECONTENTWIDGET_H
12 #define CXREPLACABLECONTENTWIDGET_H
13 
14 #include <QWidget>
15 #include <QVBoxLayout>
16 #include <boost/shared_ptr.hpp>
17 #include <iostream>
18 
19 namespace cx
20 {
21 
26 class ReplacableContentWidget : public QWidget
27 {
28 public:
29  ReplacableContentWidget(QWidget* parent) : QWidget(parent), mWidget(NULL)
30  {
31  mLayout = new QVBoxLayout(this);
32  mLayout->setMargin(0);
33  }
35  {
36  this->setWidget(boost::shared_ptr<QWidget>()); // Delete objects in correct order
37  }
38 
39  void setWidget(boost::shared_ptr<QWidget> widget)
40  {
41  this->setWidgetDontDeleteOld(widget.get());
42  mWidgetBoostPointer = widget;
43  }
44  void setWidgetDeleteOld(QWidget* widget)
45  {
46  QWidget* oldwidget = mWidget;
47  this->setWidgetDontDeleteOld(widget);
48  delete oldwidget;
49  }
50 
51  QWidget* getWidget()
52  {
53  return mWidget;
54  }
55 
56 private:
57  QVBoxLayout* mLayout;
58  QWidget* mWidget;
59  boost::shared_ptr<QWidget> mWidgetBoostPointer;
60 
61  void setWidgetDontDeleteOld(QWidget* widget)
62  {
63  if (mWidget)
64  {
65  mLayout->takeAt(0);
66  mWidget->hide();
67  mWidget->setParent(NULL);
68  }
69  mWidget = widget;
70  if (mWidget)
71  {
72  mLayout->addWidget(mWidget);
73  mWidget->show();
74  }
75  }
76 };
77 
78 }//end namespace cx
79 
80 #endif // CXREPLACABLECONTENTWIDGET_H
void setWidget(boost::shared_ptr< QWidget > widget)
void setWidgetDeleteOld(QWidget *widget)
Namespace for all CustusX production code.