fw4spl
OrganMaterialEditor.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 "uiReconstructionQt/OrganMaterialEditor.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwData/Material.hpp>
12 #include <fwData/Mesh.hpp>
13 #include <fwData/Reconstruction.hpp>
14 
15 #include <fwGuiQt/container/QtContainer.hpp>
16 
17 #include <fwRuntime/ConfigurationElement.hpp>
18 #include <fwRuntime/operations.hpp>
19 
20 #include <fwServices/IService.hpp>
21 #include <fwServices/macros.hpp>
22 #include <fwServices/op/Get.hpp>
23 
24 #include <QColor>
25 #include <QColorDialog>
26 #include <QHBoxLayout>
27 #include <QLabel>
28 #include <QPixmap>
29 #include <QPushButton>
30 #include <QSlider>
31 #include <QStyle>
32 #include <QVBoxLayout>
33 
34 namespace uiReconstructionQt
35 {
36 
39 
40 static const ::fwServices::IService::KeyType s_RECONSTRUCTION_INOUT = "reconstruction";
41 
43 {
44  //handlingEventOff();
45 }
46 
47 //------------------------------------------------------------------------------
48 
50 {
51 }
52 
53 //------------------------------------------------------------------------------
54 
56 {
58  this->create();
59  ::fwGuiQt::container::QtContainer::sptr qtContainer
60  = ::fwGuiQt::container::QtContainer::dynamicCast(this->getContainer() );
61 
62  m_colourButton = new QPushButton(tr("Color"));
63  m_colourButton->setToolTip(tr("Selected organ's color"));
64  m_colourButton->setMinimumSize(m_colourButton->sizeHint());
65 
66  QLabel* transparencyLabel = new QLabel(tr("Transparency : "));
67  m_opacitySlider = new QSlider( Qt::Horizontal);
68  m_opacitySlider->setToolTip(tr("Selected organ's opacity"));
69  m_opacitySlider->setRange(0, 100);
70  m_opacitySlider->setTickInterval(20);
71  m_opacitySlider->setTickPosition(QSlider::TicksBelow);
72  m_opacitySlider->setMinimumSize(m_opacitySlider->sizeHint());
73 
74  m_transparencyValue = new QLabel("");
75  m_transparencyValue->setMinimumSize(m_transparencyValue->sizeHint());
76 
77  QVBoxLayout* layout = new QVBoxLayout();
78  layout->addWidget( m_colourButton, 0 );
79 
80  QHBoxLayout* transparencyLayout = new QHBoxLayout( );
81  transparencyLayout->addWidget( transparencyLabel, 0);
82  transparencyLayout->addWidget( m_opacitySlider, 1 );
83  transparencyLayout->addWidget( m_transparencyValue, 0);
84  layout->addLayout( transparencyLayout, 0);
85 
86  qtContainer->setLayout( layout );
87  qtContainer->setEnabled(false);
88 
89  QObject::connect(m_opacitySlider, SIGNAL(valueChanged(int)), this, SLOT(onOpacitySlider(int)));
90  QObject::connect(m_colourButton, SIGNAL(clicked()), this, SLOT(onColorButton()));
91 
92  this->updating();
93 }
94 
95 //------------------------------------------------------------------------------
96 
98 {
100 
101  QObject::disconnect(m_opacitySlider, SIGNAL(valueChanged(int)), this, SLOT(onOpacitySlider(int)));
102  QObject::disconnect(m_colourButton, SIGNAL(clicked()), this, SLOT(onColorButton()));
103 
104  this->destroy();
105 }
106 
107 //------------------------------------------------------------------------------
108 
110 {
111  SLM_TRACE_FUNC();
112  this->initialize();
113 }
114 
115 //------------------------------------------------------------------------------
116 
118 {
119  this->refreshMaterial();
120 }
121 
122 //------------------------------------------------------------------------------
123 
124 void OrganMaterialEditor::onColorButton()
125 {
126  ::fwData::Reconstruction::sptr reconstruction = this->getInOut< ::fwData::Reconstruction >(s_RECONSTRUCTION_INOUT);
127  if (!reconstruction)
128  {
129  FW_DEPRECATED_KEY(s_RECONSTRUCTION_INOUT, "inout", "18.0");
130  reconstruction = this->getObject< ::fwData::Reconstruction >();
131  }
132  SLM_ASSERT("No Reconstruction!", reconstruction);
133 
134  ::fwData::Material::sptr material = reconstruction->getMaterial();
135  int red = material->diffuse()->red()*255;
136  int green = material->diffuse()->green()*255;
137  int blue = material->diffuse()->blue()*255;
138 
139  // Create Color choice dialog.
140  ::fwGuiQt::container::QtContainer::sptr qtContainer = ::fwGuiQt::container::QtContainer::dynamicCast(
141  this->getContainer() );
142  QWidget* const container = qtContainer->getQtContainer();
143  SLM_ASSERT("container not instanced", container);
144 
145  QColor oldColor(red, green, blue);
146  QColor color = QColorDialog::getColor(oldColor, container);
147  if(color.isValid())
148  {
149  material->diffuse()->red() = color.redF();
150  material->diffuse()->green() = color.greenF();
151  material->diffuse()->blue() = color.blueF();
152  this->materialNotification();
153  refreshMaterial();
154  }
155 
156 }
157 
158 //------------------------------------------------------------------------------
159 
160 void OrganMaterialEditor::onOpacitySlider(int value )
161 {
162  ::fwData::Reconstruction::sptr reconstruction = this->getInOut< ::fwData::Reconstruction >(s_RECONSTRUCTION_INOUT);
163  if (!reconstruction)
164  {
165  FW_DEPRECATED_KEY(s_RECONSTRUCTION_INOUT, "inout", "18.0");
166  reconstruction = this->getObject< ::fwData::Reconstruction >();
167  }
168  SLM_ASSERT("No Reconstruction!", reconstruction);
169 
170  ::fwData::Material::sptr material = reconstruction->getMaterial();
171  material->diffuse()->alpha() = value/100.0;
172  std::stringstream ss;
173  ss << value << "%";
174  m_transparencyValue->setText(QString::fromStdString(ss.str()));
175 
176  this->materialNotification();
177 }
178 
179 //------------------------------------------------------------------------------
180 
182 {
183  ::fwData::Reconstruction::csptr reconstruction = this->getInOut< ::fwData::Reconstruction >(s_RECONSTRUCTION_INOUT);
184  if (!reconstruction)
185  {
186  FW_DEPRECATED_KEY(s_RECONSTRUCTION_INOUT, "inout", "18.0");
187  reconstruction = this->getObject< ::fwData::Reconstruction >();
188  }
189  SLM_ASSERT("No Reconstruction!", reconstruction);
190 
191  ::fwGuiQt::container::QtContainer::sptr qtContainer = ::fwGuiQt::container::QtContainer::dynamicCast(
192  this->getContainer() );
193  QWidget* const container = qtContainer->getQtContainer();
194  SLM_ASSERT("container not instanced", container);
195 
196  container->setEnabled(!reconstruction->getOrganName().empty());
197 
198  ::fwData::Material::csptr material = reconstruction->getMaterial();
199  QColor materialColor = QColor(
200  material->diffuse()->red()*255,
201  material->diffuse()->green()*255,
202  material->diffuse()->blue()*255,
203  material->diffuse()->alpha()*255
204  );
205 
206  int iconSize = m_colourButton->style()->pixelMetric(QStyle::PM_LargeIconSize);
207  QPixmap pix(iconSize, iconSize);
208  pix.fill(materialColor);
209 
210  m_colourButton->setIcon(QIcon(pix));
211 
212  int a = material->diffuse()->alpha()*100;
213  m_opacitySlider->setValue( a );
214  std::stringstream ss;
215  ss << a << "%";
216  m_transparencyValue->setText(QString::fromStdString(ss.str()));
217 }
218 
219 //------------------------------------------------------------------------------
220 
222 {
223  ::fwData::Reconstruction::csptr reconstruction = this->getInOut< ::fwData::Reconstruction >(s_RECONSTRUCTION_INOUT);
224  if (!reconstruction)
225  {
226  FW_DEPRECATED_KEY(s_RECONSTRUCTION_INOUT, "inout", "18.0");
227  reconstruction = this->getObject< ::fwData::Reconstruction >();
228  }
229  SLM_ASSERT("No Reconstruction!", reconstruction);
230 
231  ::fwData::Object::ModifiedSignalType::sptr sig;
232  sig = reconstruction->getMaterial()->signal< ::fwData::Object::ModifiedSignalType >(
234  sig->asyncEmit();
235 }
236 
237 //------------------------------------------------------------------------------
238 
240 {
241  KeyConnectionsMap connections;
242 
243  //FIXME hack to support deprecated getObject()
244  if (this->getInOut< ::fwData::Reconstruction >(s_RECONSTRUCTION_INOUT))
245  {
246  connections.push(s_RECONSTRUCTION_INOUT, ::fwData::Object::s_MODIFIED_SIG, s_UPDATE_SLOT);
247  }
248 
249  return connections;
250 }
251 
252 //------------------------------------------------------------------------------
253 
254 }
#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
virtual KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
The namespace uiReconstructionQt contains several editors using Qt related on reconstruction.
This class defines a reconstruction object.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
virtual void stopping() override
Clean the UI.
Defines the service interface managing the editor service for object.
Definition: IEditor.hpp:25
virtual void starting() override
Initialize the UI.
FWGUI_API void destroy()
Stops sub-views and toobar services. Destroys view, sub-views and toolbar containers.
virtual void updating() override
Update the UI according to the material (color and transparency widgets)
void materialNotification()
Notify the material changes.
virtual UIRECONSTRUCTIONQT_API ~OrganMaterialEditor() noexcept
Destructor. Do nothing.
#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
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
virtual void configuring() override
Do nothing.
UIRECONSTRUCTIONQT_API OrganMaterialEditor() noexcept
Constructor. Do nothing.
Display a widget to change the reconstruction material (color and transparency).
void refreshMaterial()
Update the UI according to the material (color and transparency widgets)
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
FWGUI_API void initialize()
Initialize managers.