fw4spl
ImageInfo.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "uiImageQt/ImageInfo.hpp"
8 
9 #include <fwCom/Slot.hpp>
10 #include <fwCom/Slot.hxx>
11 #include <fwCom/Slots.hpp>
12 #include <fwCom/Slots.hxx>
13 
14 #include <fwCore/base.hpp>
15 
16 #include <fwData/Image.hpp>
17 
18 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
19 #include <fwDataTools/helper/ImageGetter.hpp>
20 
21 #include <fwGuiQt/container/QtContainer.hpp>
22 
23 #include <fwMath/IntrasecTypes.hpp>
24 
25 #include <fwServices/IService.hpp>
26 #include <fwServices/macros.hpp>
27 
28 #include <QHBoxLayout>
29 #include <QLabel>
30 #include <QWidget>
31 
32 namespace uiImageQt
33 {
34 
35 fwServicesRegisterMacro( ::fwGui::editor::IEditor, ::uiImageQt::ImageInfo, ::fwData::Image );
36 
37 static const ::fwCom::Slots::SlotKeyType s_GET_INTERACTION_SLOT = "getInteraction";
38 
39 static const ::fwServices::IService::KeyType s_IMAGE_INPUT = "image";
40 
41 ImageInfo::ImageInfo() noexcept
42 {
43  newSlot(s_GET_INTERACTION_SLOT, &ImageInfo::getInteraction, this);
44 }
45 
46 //------------------------------------------------------------------------------
47 
48 ImageInfo::~ImageInfo() noexcept
49 {
50 }
51 
52 //------------------------------------------------------------------------------
53 
55 {
57 
58  ::fwGuiQt::container::QtContainer::sptr qtContainer
59  = ::fwGuiQt::container::QtContainer::dynamicCast(this->getContainer() );
60 
61  QHBoxLayout* hLayout = new QHBoxLayout();
62 
63  QLabel* staticText = new QLabel( QObject::tr("intensity:"));
64  hLayout->addWidget( staticText, 0, Qt::AlignVCenter );
65 
66  m_valueText = new QLineEdit();
67  m_valueText->setReadOnly(true);
68  hLayout->addWidget( m_valueText, 1, Qt::AlignVCenter );
69 
70  qtContainer->setLayout( hLayout );
71 }
72 
73 //------------------------------------------------------------------------------
74 
76 {
77  this->destroy();
78 }
79 
80 //------------------------------------------------------------------------------
81 
83 {
85 }
86 
87 //------------------------------------------------------------------------------
88 
90 {
91  ::fwData::Image::csptr image = this->getInput< ::fwData::Image >(s_IMAGE_INPUT);
92  if (!image)
93  {
94  FW_DEPRECATED_KEY(s_IMAGE_INPUT, "in", "18.0");
95  image = this->getObject< ::fwData::Image >();
96  }
97  SLM_ASSERT("The input '" + s_IMAGE_INPUT + "' is not defined", image);
99  m_valueText->setEnabled(imageIsValid);
100 }
101 
102 //------------------------------------------------------------------------------
103 
104 void ImageInfo::getInteraction(::fwDataTools::PickingInfo info)
105 {
106  if (info.m_eventId == ::fwDataTools::PickingInfo::Event::MOUSE_MOVE)
107  {
108  ::fwData::Image::csptr image = this->getInput< ::fwData::Image >(s_IMAGE_INPUT);
109  if (!image)
110  {
111  FW_DEPRECATED_KEY(s_IMAGE_INPUT, "in", "18.0");
112  image = this->getObject< ::fwData::Image >();
113  }
114  SLM_ASSERT("The input '" + s_IMAGE_INPUT + "' is not defined", image);
115 
117  m_valueText->setEnabled(imageIsValid);
118  if (imageIsValid)
119  {
120  const double* point = info.m_worldPos;
121  const ::fwData::Image::SizeType size = image->getSize();
122 
123  if (point[0] < 0 || point[1] < 0 || point[2] < 0)
124  {
125  SLM_ERROR("The received coordinates are not in image space, maybe you used the wrong picker "
126  "interactor (see ::visuVTKAdaptor::SImagePickerInteractor)");
127  return;
128  }
129 
130  const ::fwData::Image::SizeType coords =
131  {{ static_cast< ::fwData::Image::SizeType::value_type >(point[0]),
132  static_cast< ::fwData::Image::SizeType::value_type >(point[1]),
133  static_cast< ::fwData::Image::SizeType::value_type >(point[2])}};
134 
135  bool isInside = (coords[0] < size[0] && coords[1] < size[1]);
136  if (image->getNumberOfDimensions() < 3)
137  {
138  isInside = (isInside && coords[2] == 0);
139  }
140  else
141  {
142  isInside = (isInside && coords[2] < size[2]);
143  }
144 
145  if (!isInside)
146  {
147  SLM_ERROR("The received coordinates are not in image space, maybe you used the wrong picker "
148  "interactor (see ::visuVTKAdaptor::SImagePickerInteractor)");
149  return;
150  }
151 
152  ::fwDataTools::helper::ImageGetter imageHelper(image);
153 
154  const std::string intensity = imageHelper.getPixelAsString(coords[0], coords[1], coords[2] );
155  m_valueText->setText(QString::fromStdString(intensity));
156  }
157  }
158 }
159 
160 //------------------------------------------------------------------------------
161 
162 void ImageInfo::info( std::ostream& _sstream )
163 {
164  _sstream << "Image Info Editor";
165 }
166 
167 //------------------------------------------------------------------------------
168 
170 {
171  KeyConnectionsMap connections;
172 
173  connections.push(s_IMAGE_INPUT, ::fwData::Image::s_MODIFIED_SIG, s_UPDATE_SLOT);
174  connections.push(s_IMAGE_INPUT, ::fwData::Image::s_BUFFER_MODIFIED_SIG, s_UPDATE_SLOT);
175 
176  return connections;
177 }
178 
179 //------------------------------------------------------------------------------
180 }
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
double m_worldPos[3]
Position clicked in world coordinates.
Definition: PickingInfo.hpp:44
virtual void stopping() override
Stops editor.
Definition: ImageInfo.cpp:75
FWDATATOOLS_API const std::string getPixelAsString(SizeType::value_type x, SizeType::value_type y, SizeType::value_type z) const
Helpers for 3D images.
Definition: ImageGetter.cpp:67
Defines the service interface managing the editor service for object.
Definition: IEditor.hpp:25
FWGUI_API void destroy()
Stops sub-views and toobar services. Destroys view, sub-views and toolbar containers.
virtual void configuring() override
Initializes the editor.
Definition: ImageInfo.cpp:82
virtual void starting() override
Starts editor.
Definition: ImageInfo.cpp:54
The namespace uiImageQt contains several editors on image written with Qt. This namespace is included...
Definition: ImageInfo.hpp:23
#define SLM_ERROR(message)
Definition: spyLog.hpp:272
virtual void info(std::ostream &_sstream) override
Write information in a stream.
Definition: ImageInfo.cpp:162
ImageInfo service allows to display image pixel information when it receives the mouse cursor coordin...
Definition: ImageInfo.hpp:45
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
virtual KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: ImageInfo.cpp:169
Structure to store picking information.
Definition: PickingInfo.hpp:20
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
Defines an helper to modify an fwData::Image by adding few medical fields and create in parallel the ...
Definition: ImageGetter.hpp:23
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_BUFFER_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
static FWDATATOOLS_API bool checkImageValidity(::fwData::Image::csptr _pImg)
Check if the image is valid.
This class defines an image.
virtual void updating() override
Check if the image is valid, if not the editor is disabled.
Definition: ImageInfo.cpp:89
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
FWGUI_API void initialize()
Initialize managers.
Event m_eventId
Mouse event.
Definition: PickingInfo.hpp:50