fw4spl
SSignalButton.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2015-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 "guiQt/editor/SSignalButton.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 #include <fwCom/Slot.hpp>
13 #include <fwCom/Slot.hxx>
14 #include <fwCom/Slots.hpp>
15 #include <fwCom/Slots.hxx>
16 
17 #include <fwCore/base.hpp>
18 
19 #include <fwGuiQt/container/QtContainer.hpp>
20 
21 #include <fwRuntime/ConfigurationElement.hpp>
22 #include <fwRuntime/operations.hpp>
23 
24 #include <fwServices/macros.hpp>
25 
26 #include <fwTools/fwID.hpp>
27 
28 #include <QVBoxLayout>
29 
30 #include <string>
31 
32 namespace guiQt
33 {
34 namespace editor
35 {
36 
37 static const ::fwCom::Signals::SignalKeyType s_CLICKED_SIG = "clicked";
38 static const ::fwCom::Signals::SignalKeyType s_TOGGLED_SIG = "toggled";
39 
40 static const ::fwCom::Slots::SlotKeyType s_SET_CHECKED_SLOT = "setChecked";
41 
42 fwServicesRegisterMacro( ::fwGui::editor::IEditor, ::guiQt::editor::SSignalButton );
43 
44 //-----------------------------------------------------------------------------
45 
47  m_checkable(false),
48  m_checkAtStart(false),
49  m_iconWidth(0),
50  m_iconHeight(0)
51 {
52  m_sigClicked = newSignal< ClickedSignalType >(s_CLICKED_SIG);
53  m_sigToggled = newSignal< ToggledSignalType >(s_TOGGLED_SIG);
54 
55  newSlot(s_SET_CHECKED_SLOT, &SSignalButton::setChecked, this);
56 }
57 
58 //-----------------------------------------------------------------------------
59 
61 {
62 }
63 
64 //-----------------------------------------------------------------------------
65 
67 {
68  this->create();
69 
70  ::fwGuiQt::container::QtContainer::sptr qtContainer = ::fwGuiQt::container::QtContainer::dynamicCast(
71  this->getContainer() );
72 
73  QVBoxLayout* layout = new QVBoxLayout();
74  m_button = new QPushButton(QString::fromStdString(m_text));
75  layout->addWidget(m_button);
76  qtContainer->setLayout(layout);
77 
78  if (!m_icon.empty())
79  {
80  m_button->setIcon(QIcon(QString::fromStdString(m_icon.string())));
81  }
82  if (m_iconWidth > 0 && m_iconHeight > 0)
83  {
84  m_button->setIconSize(QSize(m_iconWidth, m_iconHeight));
85  }
86 
87  if (m_checkable)
88  {
89  m_button->setCheckable(true);
90 
91  if (m_checkAtStart)
92  {
93  m_button->setChecked(true);
94  if (!m_text2.empty())
95  {
96  m_button->setText(QString::fromStdString(m_text2));
97  }
98  if (!m_icon2.empty())
99  {
100  m_button->setIcon(QIcon(QString::fromStdString(m_icon2.string())));
101  }
102  }
103  }
104 
105  QObject::connect(m_button.data(), &QPushButton::clicked, this, &SSignalButton::onClicked );
106  QObject::connect(m_button.data(), &QPushButton::toggled, this, &SSignalButton::onToggled );
107 }
108 
109 //-----------------------------------------------------------------------------
110 
112 {
113  this->destroy();
114 }
115 
116 //-----------------------------------------------------------------------------
117 
119 {
120  this->initialize();
121 
122  ::fwRuntime::ConfigurationElement::sptr config = m_configuration->findConfigurationElement("config");
123  SLM_ASSERT("'config' tag is missing", config);
124 
125  ::fwRuntime::ConfigurationElement::sptr checkableCfg = config->findConfigurationElement("checkable");
126  if (checkableCfg)
127  {
128  SLM_ASSERT("'checkable' value must be 'true' or 'false'",
129  checkableCfg->getValue() == "true" || checkableCfg->getValue() == "false");
130  m_checkable = (checkableCfg->getValue() == "true");
131  }
132 
133  ::fwRuntime::ConfigurationElement::sptr txtCfg = config->findConfigurationElement("text");
134  if (txtCfg)
135  {
136  m_text = txtCfg->getValue();
137  }
138  ::fwRuntime::ConfigurationElement::sptr iconCfg = config->findConfigurationElement("icon");
139  if (iconCfg)
140  {
141  m_icon = ::fwRuntime::getBundleResourceFilePath(iconCfg->getValue());
142  }
143 
144  ::fwRuntime::ConfigurationElement::sptr txt2Cfg = config->findConfigurationElement("text2");
145  if (txt2Cfg)
146  {
147  SLM_ASSERT("Button must be 'checkable' in order to defined 'text2'", m_checkable);
148  SLM_ASSERT("'text' tag must be defined in order to specify 'text2'", !m_text.empty());
149  m_text2 = txt2Cfg->getValue();
150  }
151 
152  ::fwRuntime::ConfigurationElement::sptr icon2Cfg = config->findConfigurationElement("icon2");
153  if (icon2Cfg)
154  {
155  SLM_ASSERT("Button must be 'checkable' in order to defined 'icon2'", m_checkable);
156  SLM_ASSERT("'icon' tag must be defined in order to specify 'icon2'", iconCfg);
157  m_icon2 = ::fwRuntime::getBundleResourceFilePath(icon2Cfg->getValue());
158  }
159 
160  ::fwRuntime::ConfigurationElement::sptr checkedCfg = config->findConfigurationElement("checked");
161  if (checkedCfg)
162  {
163  SLM_ASSERT("Button must be 'checkable' in order to defined 'checked'", m_checkable);
164  SLM_ASSERT("'checked' value must be 'true' or 'false'",
165  checkedCfg->getValue() == "true" || checkedCfg->getValue() == "false");
166  m_checkAtStart = (checkedCfg->getValue() == "true");
167  }
168 
169  ::fwRuntime::ConfigurationElement::sptr widthCfg = config->findConfigurationElement("iconWidth");
170  if (widthCfg)
171  {
172  m_iconWidth = std::stoi(widthCfg->getValue());
173  }
174 
175  ::fwRuntime::ConfigurationElement::sptr heightCfg = config->findConfigurationElement("iconHeight");
176  if (heightCfg)
177  {
178  m_iconHeight = std::stoi(heightCfg->getValue());
179  }
180 }
181 
182 //-----------------------------------------------------------------------------
183 
184 void SSignalButton::onClicked()
185 {
186  m_sigClicked->asyncEmit();
187 }
188 
189 //-----------------------------------------------------------------------------
190 
191 void SSignalButton::onToggled(bool toogled)
192 {
193  this->setChecked(toogled);
194  m_sigToggled->asyncEmit(toogled);
195 }
196 
197 //-----------------------------------------------------------------------------
198 
199 void SSignalButton::setChecked(bool checked)
200 {
201  if (checked)
202  {
203  if (!m_text2.empty())
204  {
205  m_button->setText(QString::fromStdString(m_text2));
206  }
207  if (!m_icon2.empty())
208  {
209  m_button->setIcon(QIcon(QString::fromStdString(m_icon2.string())));
210  }
211  }
212  else
213  {
214  if (!m_text.empty())
215  {
216  m_button->setText(QString::fromStdString(m_text));
217  }
218  if (!m_icon.empty())
219  {
220  m_button->setIcon(QIcon(QString::fromStdString(m_icon.string())));
221  }
222  }
223 }
224 
225 //-----------------------------------------------------------------------------
226 
228 {
229 
230 }
231 
232 //-----------------------------------------------------------------------------
233 
234 void SSignalButton::info( std::ostream& _sstream )
235 {
236 }
237 
238 //-----------------------------------------------------------------------------
239 
240 } // namespace editor
241 } // namespace gui
This editor shows a button and send a signal when it is clicked.
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 GUIQT_API void stopping() override
This method launches the IEditor::stopping method.
virtual GUIQT_API ~SSignalButton() noexcept
Destructor. Do nothing.
GUIQT_API SSignalButton() noexcept
Constructor. Do nothing.
virtual GUIQT_API void configuring() override
This method is used to configure the class parameters.
#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 GUIQT_API void updating() override
This method is used to update services. Do nothing.
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
virtual GUIQT_API void info(std::ostream &_sstream) override
This method is used to give information about the service. Do nothing.
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
virtual GUIQT_API void starting() override
This method launches the IEditor::starting method.
The namespace guiQt contains the basic services to build the application IHM with Qt...
Definition: Code.hpp:21
FWGUI_API void initialize()
Initialize managers.