Fraxinus  18.10
An IGT application
cxCroppingWidget.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 
12 #include "cxCroppingWidget.h"
13 #include <QVBoxLayout>
14 #include <QPushButton>
15 #include <QCheckBox>
16 #include <QGroupBox>
17 #include <QLabel>
18 #include <vtkImageData.h>
19 #include "cxStringPropertyBase.h"
21 #include "cxDefinitionStrings.h"
22 #include "cxUtilHelpers.h"
23 
25 #include "cxImageAlgorithms.h"
26 #include "cxInteractiveCropper.h"
27 #include "cxImage.h"
28 #include "cxPatientModelService.h"
29 #include "cxViewService.h"
30 #include "cxInteractiveCropper.h"
31 
32 #include "cxViewGroupData.h"
33 #include "cxReporter.h"
34 #include "cxActiveData.h"
35 
36 namespace cx
37 {
38 
39 CroppingWidget::CroppingWidget(PatientModelServicePtr patientModelService, ViewServicePtr viewService, QWidget* parent) :
40  BaseWidget(parent, "cropping_widget", "Crop"),
41  mPatientModelService(patientModelService),
42  mViewService(viewService)
43 {
44  connect(viewService.get(), &ViewService::activeLayoutChanged, this, &CroppingWidget::setupUI);
45  this->setupUI();
46 }
47 
48 void CroppingWidget::setupUI()
49 {
50  if (mInteractiveCropper)
51  return;
52 
53  mInteractiveCropper = mViewService->getCropper();
54 
55  if (!mInteractiveCropper)
56  return;
57 
58  connect(mInteractiveCropper.get(), SIGNAL(changed()), this, SLOT(cropperChangedSlot()));
59 
60  QVBoxLayout* layout = new QVBoxLayout(this);
61 
62  this->setToolTip("Interactive volume cropping");
63 
64  QGroupBox* activeGroupBox = new QGroupBox("Interactive cropper");
65  activeGroupBox->setToolTip(this->toolTip());
66  layout->addWidget(activeGroupBox);
67  QVBoxLayout* activeLayout = new QVBoxLayout(activeGroupBox);
68 
69  mUseCropperCheckBox = new QCheckBox("Use Cropper");
70  mUseCropperCheckBox->setToolTip("Turn on cropping for the active volume.");
71  connect(mUseCropperCheckBox, SIGNAL(toggled(bool)), mInteractiveCropper.get(), SLOT(useCropping(bool)));
72  activeLayout->addWidget(mUseCropperCheckBox);
73 
74  mShowBoxCheckBox = new QCheckBox("Show box");
75  mShowBoxCheckBox->setToolTip("Show crop box in 3D view. This also turns on cropping for convenience.");
76  connect(mShowBoxCheckBox, SIGNAL(toggled(bool)), mInteractiveCropper.get(), SLOT(showBoxWidget(bool)));
77  activeLayout->addWidget(mShowBoxCheckBox);
78 
79  mBoundingBoxDimensions = new QLabel("?, ?, ?");
80  mBoundingBoxDimensions->setToolTip("The dimensions of the croppers boundingbox.");
81  activeLayout->addWidget(mBoundingBoxDimensions);
82 
83  mBBWidget = new BoundingBoxWidget(this);
84  layout->addWidget(mBBWidget);
85  connect(mBBWidget, SIGNAL(changed()), this, SLOT(boxValuesChanged()));
86 
87  QPushButton* cropClipButton = new QPushButton("Create new cropped volume");
88  cropClipButton->setToolTip("Create a new volume containing only the volume inside the crop box.");
89  connect(cropClipButton, SIGNAL(clicked()), this, SLOT(cropClipButtonClickedSlot()));
90  layout->addWidget(cropClipButton);
91 
92  layout->addStretch();
93 
94  this->cropperChangedSlot();
95 }
96 
97 void CroppingWidget::boxValuesChanged()
98 {
99  mInteractiveCropper->setBoundingBox(mBBWidget->getValue());
100 }
101 
102 void CroppingWidget::cropperChangedSlot()
103 {
104  std::vector<int> dims = mInteractiveCropper->getDimensions();
105  if(dims.size() < 3)
106  return;
107 
108  QString dimensionText = "Dimensions: "+qstring_cast(dims.at(0))+", "+qstring_cast(dims.at(1))+", "+qstring_cast(dims.at(2));
109  mBoundingBoxDimensions->setText(dimensionText);
110  mUseCropperCheckBox->setChecked(mInteractiveCropper->getUseCropping());
111  mShowBoxCheckBox->setChecked(mInteractiveCropper->getShowBoxWidget());
112 
113  mBBWidget->setValue(mInteractiveCropper->getBoundingBox(), mInteractiveCropper->getMaxBoundingBox());
114 }
115 
116 ImagePtr CroppingWidget::cropClipButtonClickedSlot()
117 {
118  ActiveDataPtr activeData = mPatientModelService->getActiveData();
119  ImagePtr image = activeData->getActive<Image>();
120 
121  ImagePtr retval = cropImage(mPatientModelService, image);
122  mPatientModelService->insertData(retval);
123 
124  this->hideOldAndShowNewVolume(image, retval);
125 
126  return retval;
127 }
128 
129 void CroppingWidget::hideOldAndShowNewVolume(ImagePtr oldImage, ImagePtr newImage)
130 {
131 // int groupNr = mViewService->getActiveGroup();//Gives -1 when we are inside the CroppingWidget
132  int groupNr = 0;
133 
134  ViewGroupDataPtr viewGroup = mViewService->getGroup(0);
135  if(!viewGroup)
136  {
137  reportWarning(QString("CroppingWidget: Hide old and show new volume failed. Can't get view group %1.").arg(groupNr));
138  return;
139  }
140  if(!viewGroup->removeData(oldImage->getUid()))
141  reportWarning(QString("CroppingWidget: Hide old and show new volume failed. Can't remove image %1 from view group %2").arg(oldImage->getUid()).arg(groupNr));
142 
143  viewGroup->addData(newImage->getUid());
144 }
145 
146 }
QString qstring_cast(const T &val)
boost::shared_ptr< class ViewGroupData > ViewGroupDataPtr
Definition: cxViewGroup.h:29
vtkImageDataPtr cropImage(vtkImageDataPtr input, IntBoundingBox3D cropbox)
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
boost::shared_ptr< class ActiveData > ActiveDataPtr
Definition: cxColorWidget.h:21
boost::shared_ptr< class ViewService > ViewServicePtr
Widget displays/edits a BoundingBox3D.
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
A volumetric data set.
Definition: cxImage.h:45
DoubleBoundingBox3D getValue() const
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void activeLayoutChanged()
emitted when the active layout changes
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
CroppingWidget(PatientModelServicePtr patientModelService, ViewServicePtr viewService, QWidget *parent)
void setValue(const DoubleBoundingBox3D &value, const DoubleBoundingBox3D &range)
Namespace for all CustusX production code.