Fraxinus  16.5.0-fx-rc6
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 #include "cxActiveData.h"
56 
57 namespace cx
58 {
59 
61  BaseWidget(parent, "CroppingWidget", "Crop"),
62  mPatientModelService(patientModelService),
63  mViewService(viewService)
64 {
65  connect(viewService.get(), &ViewService::activeLayoutChanged, this, &CroppingWidget::setupUI);
66  this->setupUI();
67 }
68 
69 void CroppingWidget::setupUI()
70 {
71  if (mInteractiveCropper)
72  return;
73 
74  mInteractiveCropper = viewService()->getCropper();
75 
76  if (!mInteractiveCropper)
77  return;
78 
79  connect(mInteractiveCropper.get(), SIGNAL(changed()), this, SLOT(cropperChangedSlot()));
80 
81  QVBoxLayout* layout = new QVBoxLayout(this);
82 
83  this->setToolTip("Interactive volume cropping");
84 
85  QGroupBox* activeGroupBox = new QGroupBox("Interactive cropper");
86  activeGroupBox->setToolTip(this->toolTip());
87  layout->addWidget(activeGroupBox);
88  QVBoxLayout* activeLayout = new QVBoxLayout(activeGroupBox);
89 
90  mUseCropperCheckBox = new QCheckBox("Use Cropper");
91  mUseCropperCheckBox->setToolTip("Turn on cropping for the active volume.");
92  connect(mUseCropperCheckBox, SIGNAL(toggled(bool)), mInteractiveCropper.get(), SLOT(useCropping(bool)));
93  activeLayout->addWidget(mUseCropperCheckBox);
94 
95  mShowBoxCheckBox = new QCheckBox("Show box");
96  mShowBoxCheckBox->setToolTip("Show crop box in 3D view. This also turns on cropping for convenience.");
97  connect(mShowBoxCheckBox, SIGNAL(toggled(bool)), mInteractiveCropper.get(), SLOT(showBoxWidget(bool)));
98  activeLayout->addWidget(mShowBoxCheckBox);
99 
100  mBoundingBoxDimensions = new QLabel("?, ?, ?");
101  mBoundingBoxDimensions->setToolTip("The dimensions of the croppers boundingbox.");
102  activeLayout->addWidget(mBoundingBoxDimensions);
103 
104  mBBWidget = new BoundingBoxWidget(this);
105  layout->addWidget(mBBWidget);
106  connect(mBBWidget, SIGNAL(changed()), this, SLOT(boxValuesChanged()));
107 
108  QPushButton* cropClipButton = new QPushButton("Create new cropped volume");
109  cropClipButton->setToolTip("Create a new volume containing only the volume inside the crop box.");
110  connect(cropClipButton, SIGNAL(clicked()), this, SLOT(cropClipButtonClickedSlot()));
111  layout->addWidget(cropClipButton);
112 
113  layout->addStretch();
114 
115  this->cropperChangedSlot();
116 }
117 
118 void CroppingWidget::boxValuesChanged()
119 {
120  mInteractiveCropper->setBoundingBox(mBBWidget->getValue());
121 }
122 
123 void CroppingWidget::cropperChangedSlot()
124 {
125  std::vector<int> dims = mInteractiveCropper->getDimensions();
126  if(dims.size() < 3)
127  return;
128 
129  QString dimensionText = "Dimensions: "+qstring_cast(dims.at(0))+", "+qstring_cast(dims.at(1))+", "+qstring_cast(dims.at(2));
130  mBoundingBoxDimensions->setText(dimensionText);
131  mUseCropperCheckBox->setChecked(mInteractiveCropper->getUseCropping());
132  mShowBoxCheckBox->setChecked(mInteractiveCropper->getShowBoxWidget());
133 
134  mBBWidget->setValue(mInteractiveCropper->getBoundingBox(), mInteractiveCropper->getMaxBoundingBox());
135 }
136 
137 ImagePtr CroppingWidget::cropClipButtonClickedSlot()
138 {
139  ActiveDataPtr activeData = mPatientModelService->getActiveData();
140  ImagePtr image = activeData->getActive<Image>();
141 
142  ImagePtr retval = cropImage(patientService(), image);
143  mPatientModelService->insertData(retval);
144 
145  this->hideOldAndShowNewVolume(image, retval);
146 
147  return retval;
148 }
149 
150 void CroppingWidget::hideOldAndShowNewVolume(ImagePtr oldImage, ImagePtr newImage)
151 {
152 // int groupNr = mViewService->getActiveGroup();//Gives -1 when we are inside the CroppingWidget
153  int groupNr = 0;
154 
155  ViewGroupDataPtr viewGroup = mViewService->getGroup(0);
156  if(!viewGroup)
157  {
158  reportWarning(QString("CroppingWidget: Hide old and show new volume failed. Can't get view group %1.").arg(groupNr));
159  return;
160  }
161  if(!viewGroup->removeData(oldImage->getUid()))
162  reportWarning(QString("CroppingWidget: Hide old and show new volume failed. Can't remove image %1 from view group %2").arg(oldImage->getUid()).arg(groupNr));
163 
164  viewGroup->addData(newImage->getUid());
165 }
166 
167 }
QString qstring_cast(const T &val)
boost::shared_ptr< class ViewGroupData > ViewGroupDataPtr
Definition: cxViewGroup.h:50
vtkImageDataPtr cropImage(vtkImageDataPtr input, IntBoundingBox3D cropbox)
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
boost::shared_ptr< class ActiveData > ActiveDataPtr
Definition: cxColorWidget.h:42
boost::shared_ptr< class ViewService > ViewServicePtr
Widget displays/edits a BoundingBox3D.
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
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:108
CroppingWidget(PatientModelServicePtr patientModelService, ViewServicePtr viewService, QWidget *parent)
cxLogicManager_EXPORT ViewServicePtr viewService()
cxLogicManager_EXPORT PatientModelServicePtr patientService()
void setValue(const DoubleBoundingBox3D &value, const DoubleBoundingBox3D &range)