fw4spl
ImageTransparency.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/ImageTransparency.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 
13 #include <fwCore/base.hpp>
14 
15 #include <fwData/Boolean.hpp>
16 #include <fwData/Image.hpp>
17 
18 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
19 
20 #include <fwGuiQt/container/QtContainer.hpp>
21 
22 #include <fwMath/IntrasecTypes.hpp>
23 
24 #include <fwServices/IService.hpp>
25 #include <fwServices/macros.hpp>
26 
27 #include <QHBoxLayout>
28 #include <QLabel>
29 #include <QWidget>
30 
31 namespace uiImageQt
32 {
33 
34 fwServicesRegisterMacro( ::fwGui::editor::IEditor, ::uiImageQt::ImageTransparency, ::fwData::Image );
35 
36 static const ::fwServices::IService::KeyType s_IMAGE_INOUT = "image";
37 
38 ImageTransparency::ImageTransparency() noexcept
39 {
40 }
41 
42 //------------------------------------------------------------------------------
43 
44 ImageTransparency::~ImageTransparency() noexcept
45 {
46 }
47 
48 //------------------------------------------------------------------------------
49 
51 {
54 
55  ::fwGuiQt::container::QtContainer::sptr qtContainer = ::fwGuiQt::container::QtContainer::dynamicCast(
56  this->getContainer() );
57 
58  QHBoxLayout* hLayout = new QHBoxLayout();
59 
60  QLabel* staticText = new QLabel( QObject::tr("Transparency: "));
61  hLayout->addWidget( staticText, 0, Qt::AlignVCenter );
62 
63  m_valueSlider = new QSlider( Qt::Horizontal );
64  hLayout->addWidget( m_valueSlider, 1, Qt::AlignVCenter );
65  m_valueSlider->setRange(0, 100);
66  m_valueSlider->setMinimumWidth(100);
67 
68  m_valueCheckBox = new QCheckBox( QObject::tr("visible"));
69  m_action = new QAction(m_valueCheckBox);
70  m_action->setCheckable(true);
71  if (!m_shortcut.empty())
72  {
73  m_action->setShortcut(QKeySequence(QString::fromStdString(m_shortcut)));
74  }
75  m_valueCheckBox->addAction(m_action);
76  hLayout->addWidget( m_valueCheckBox, 0, Qt::AlignVCenter );
77 
78  qtContainer->setLayout( hLayout );
79 
80  QObject::connect(m_valueSlider, SIGNAL(valueChanged(int)), this, SLOT(onModifyTransparency(int)));
81  QObject::connect(m_valueCheckBox, SIGNAL(stateChanged(int)), this, SLOT(onModifyVisibility(int)));
82  QObject::connect(m_action, SIGNAL(triggered(bool)), this, SLOT(onModifyVisibility(bool)));
83 
84  this->updating();
85 }
86 
87 //------------------------------------------------------------------------------
88 
90 {
92  QObject::disconnect(m_valueSlider, SIGNAL(valueChanged(int)), this, SLOT(onModifyTransparency(int)));
93  QObject::disconnect(m_valueCheckBox, SIGNAL(stateChanged(int)), this, SLOT(onModifyVisibility(int)));
94  QObject::disconnect(m_action, SIGNAL(triggered(bool)), this, SLOT(onModifyVisibility(bool)));
95 
96  this->destroy();
97 }
98 
99 //------------------------------------------------------------------------------
100 
102 {
103  SLM_TRACE_FUNC();
105 
106  //<shortcut value="X"/>
107  std::vector < ConfigurationType > vectCfg = m_configuration->find("shortcut");
108  if(!vectCfg.empty())
109  {
110  ConfigurationType config = vectCfg.at(0);
111  SLM_ASSERT("Missing attribute value", config->hasAttribute("value"));
112  m_shortcut = config->getAttributeValue("value");
113  }
114 }
115 
116 //------------------------------------------------------------------------------
117 
119 {
120  ::fwData::Image::sptr img = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
121  if (!img)
122  {
123  FW_DEPRECATED_KEY(s_IMAGE_INOUT, "inout", "18.0");
124  img = this->getObject< ::fwData::Image >();
125  }
126 
128  m_valueSlider->setEnabled(imageIsValid);
129  m_valueCheckBox->setEnabled(imageIsValid);
130  if (imageIsValid)
131  {
132  QObject::disconnect(m_valueSlider, SIGNAL(valueChanged(int)), this, SLOT(onModifyTransparency(int)));
133  QObject::disconnect(m_valueCheckBox, SIGNAL(stateChanged(int)), this, SLOT(onModifyVisibility(int)));
134  QObject::disconnect(m_action, SIGNAL(triggered(bool)), this, SLOT(onModifyVisibility(bool)));
135 
136  if(img->getField( "TRANSPARENCY" ) )
137  {
138  ::fwData::Integer::sptr transparency = img->getField< ::fwData::Integer >( "TRANSPARENCY" );
139  m_valueSlider->setValue( *transparency );
140  }
141  else
142  {
143  img->setField( "TRANSPARENCY", ::fwData::Integer::New(0) );
144  m_valueSlider->setValue( 0 );
145  }
146  if(img->getField( "VISIBILITY" ) )
147  {
148  ::fwData::Boolean::sptr visible = img->getField< ::fwData::Boolean >( "VISIBILITY" );
149  m_valueCheckBox->setChecked( *visible );
150  m_action->setChecked(*visible);
151  }
152  else
153  {
154  img->setField( "VISIBILITY", ::fwData::Boolean::New(true) );
155  m_valueCheckBox->setChecked( true );
156  m_action->setChecked(true);
157  }
158  QObject::connect(m_valueSlider, SIGNAL(valueChanged(int)), this, SLOT(onModifyTransparency(int)));
159  QObject::connect(m_valueCheckBox, SIGNAL(stateChanged(int)), this, SLOT(onModifyVisibility(int)));
160  QObject::connect(m_action, SIGNAL(triggered(bool)), this, SLOT(onModifyVisibility(bool)));
161  }
162 }
163 
164 //------------------------------------------------------------------------------
165 
167 {
168  this->updating();
169 }
170 
171 //------------------------------------------------------------------------------
172 
173 void ImageTransparency::info( std::ostream& _sstream )
174 {
175  _sstream << "Image Features Editor";
176 }
177 
178 //------------------------------------------------------------------------------
179 
181 {
182  SLM_TRACE_FUNC();
183  ::fwData::Image::sptr img = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
184  if (!img)
185  {
186  FW_DEPRECATED_KEY(s_IMAGE_INOUT, "inout", "18.0");
187  img = this->getObject< ::fwData::Image >();
188  }
189  img->setField( "TRANSPARENCY", ::fwData::Integer::New(value) );
190 
191  auto sig = img->signal< ::fwData::Image::TransparencyModifiedSignalType >(
193  {
194  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
195  sig->asyncEmit();
196  }
197 }
198 
199 //------------------------------------------------------------------------------
200 
202 {
203  SLM_TRACE_FUNC();
204  m_valueCheckBox->setCheckState(value ? Qt::Checked : Qt::Unchecked);
205  this->notifyVisibility(value);
206 }
207 
208 //------------------------------------------------------------------------------
209 
211 {
212  SLM_TRACE_FUNC();
213  m_action->setChecked(value == Qt::Checked);
214 
215  this->notifyVisibility(value == Qt::Checked);
216 }
217 
218 //------------------------------------------------------------------------------
219 
220 void ImageTransparency::notifyVisibility(bool isVisible)
221 {
222  ::fwData::Image::sptr img = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
223  if (!img)
224  {
225  FW_DEPRECATED_KEY(s_IMAGE_INOUT, "inout", "18.0");
226  img = this->getObject< ::fwData::Image >();
227  }
228  img->setField( "VISIBILITY", ::fwData::Boolean::New(isVisible) );
229 
231  {
232  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
233  sig->asyncEmit(isVisible);
234  }
235 }
236 
237 //------------------------------------------------------------------------------
238 
240 {
241  KeyConnectionsType connections;
242  connections.push_back( std::make_pair( ::fwData::Image::s_MODIFIED_SIG, s_UPDATE_SLOT ) );
243  connections.push_back( std::make_pair( ::fwData::Image::s_VISIBILITY_MODIFIED_SIG, s_UPDATE_SLOT ) );
244  connections.push_back( std::make_pair( ::fwData::Image::s_TRANSPARENCY_MODIFIED_SIG, s_UPDATE_SLOT ) );
245  connections.push_back( std::make_pair( ::fwData::Image::s_BUFFER_MODIFIED_SIG, s_UPDATE_SLOT ) );
246 
247  return connections;
248 }
249 
250 //------------------------------------------------------------------------------
251 
253 {
254  KeyConnectionsMap connections;
255 
256  //FIXME hack to support deprecated getObject()
257  if (this->getInOut< ::fwData::Image >(s_IMAGE_INOUT))
258  {
259  connections.push(s_IMAGE_INOUT, ::fwData::Image::s_MODIFIED_SIG, s_UPDATE_SLOT);
260  connections.push(s_IMAGE_INOUT, ::fwData::Image::s_VISIBILITY_MODIFIED_SIG, s_UPDATE_SLOT);
261  connections.push(s_IMAGE_INOUT, ::fwData::Image::s_TRANSPARENCY_MODIFIED_SIG, s_UPDATE_SLOT);
262  connections.push(s_IMAGE_INOUT, ::fwData::Image::s_BUFFER_MODIFIED_SIG, s_UPDATE_SLOT);
263  }
264 
265  return connections;
266 }
267 
268 //------------------------------------------------------------------------------
269 
270 }
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_TRANSPARENCY_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
virtual void info(std::ostream &_sstream) override
Write information in a stream.
ImageTransparency service allows to change image transparency.
Class allowing to block a Connection.
Definition: Connection.hpp:20
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
This class contains an integer value. Integer object is essentially used as a field in other objects...
Definition: Integer.hpp:24
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 swapping() override
Swap the service from associated object to another object.
The namespace uiImageQt contains several editors on image written with Qt. This namespace is included...
Definition: ImageInfo.hpp:23
virtual void stopping() override
Stops editor.
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
virtual UIIMAGEQT_API KeyConnectionsType getObjSrvConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
virtual KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_VISIBILITY_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
#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
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
::fwCom::helper::SigSlotConnection::KeyConnectionsType KeyConnectionsType
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: IService.hpp:449
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
virtual void configuring() override
Configure the service before starting. Apply the configuration to service.
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.
void onModifyVisibility(bool value)
This method is called when the visibility value change using action shortcut.
void onModifyTransparency(int value)
This method is called when the transparency value change moving slider.
This class defines an image.
virtual void starting() override
Starts editor.
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
virtual void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
FWGUI_API void initialize()
Initialize managers.
This class contains a boolean value.