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