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