fw4spl
SSelectionMenuButton.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 "guiQt/editor/SSelectionMenuButton.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 <fwDataTools/fieldHelper/Image.hpp>
20 
21 #include <fwGuiQt/container/QtContainer.hpp>
22 
23 #include <fwRuntime/ConfigurationElement.hpp>
24 #include <fwRuntime/operations.hpp>
25 
26 #include <fwServices/macros.hpp>
27 
28 #include <fwTools/fwID.hpp>
29 
30 #include <boost/filesystem.hpp>
31 #include <boost/filesystem/convenience.hpp>
32 
33 #include <QAction>
34 #include <QMenu>
35 #include <QPushButton>
36 #include <QString>
37 #include <QVBoxLayout>
38 #include <QWidget>
39 
40 namespace guiQt
41 {
42 namespace editor
43 {
44 
45 fwServicesRegisterMacro( ::fwGui::editor::IEditor, ::guiQt::editor::SSelectionMenuButton );
46 
47 static const ::fwCom::Signals::SignalKeyType s_SELECTED_SIG = "selected";
48 
49 static const ::fwCom::Slots::SlotKeyType s_SET_ENABLED_SIG = "setEnabled";
50 static const ::fwCom::Slots::SlotKeyType s_SENABLE_SIG = "enable";
51 static const ::fwCom::Slots::SlotKeyType s_DISABLE_SIG = "disable";
52 
54  m_text(">"),
55  m_selection(0)
56 {
57  m_sigSelected = newSignal < SelectedSignalType >(s_SELECTED_SIG);
58 
59  newSlot(s_SET_ENABLED_SIG, &SSelectionMenuButton::setEnabled, this);
60  newSlot(s_SENABLE_SIG, &SSelectionMenuButton::enable, this);
61  newSlot(s_DISABLE_SIG, &SSelectionMenuButton::disable, this);
62 }
63 
64 //------------------------------------------------------------------------------
65 
67 {
68 }
69 
70 //------------------------------------------------------------------------------
71 
73 {
74  this->initialize();
75 
76  Configuration txtCfg = m_configuration->findConfigurationElement("text");
77  if(txtCfg)
78  {
79  m_text = txtCfg->getValue();
80  }
81  Configuration toolTipCfg = m_configuration->findConfigurationElement("toolTip");
82  if(toolTipCfg)
83  {
84  m_toolTip = toolTipCfg->getValue();
85  }
86 
87  Configuration selectedCfg = m_configuration->findConfigurationElement("selected");
88  if(selectedCfg)
89  {
90  m_selection = std::stoi(selectedCfg->getValue());
91  }
92 
93  Configuration itemsCfg = m_configuration->findConfigurationElement("items");
94  SLM_ASSERT("Missing 'items' config", itemsCfg);
95 
96  std::vector < Configuration > itemCfgs = itemsCfg->find("item");
97  SLM_ASSERT("At least two items must be defined", itemCfgs.size() >= 2);
98  for (auto itemCfg : itemCfgs)
99  {
100  SLM_ASSERT("Missing 'text' attribute", itemCfg->hasAttribute("text"));
101  SLM_ASSERT("Missing 'value' attribute", itemCfg->hasAttribute("value"));
102  std::string txt = itemCfg->getExistingAttributeValue("text");
103  std::string val = itemCfg->getExistingAttributeValue("value");
104  int value = std::stoi(val);
105  m_items.push_back(std::make_pair(value, txt));
106  }
107 }
108 
109 //------------------------------------------------------------------------------
110 
112 {
113  this->create();
114 
115  ::fwGuiQt::container::QtContainer::sptr qtContainer = ::fwGuiQt::container::QtContainer::dynamicCast(
116  this->getContainer() );
117 
118  m_dropDownButton = new QPushButton( QString::fromStdString(m_text) );
119  m_dropDownButton->setToolTip( QString::fromStdString(m_toolTip));
120 // m_dropDownButton->setMaximumWidth(40);
121 
122  m_pDropDownMenu = new QMenu();
123  m_actionGroup = new QActionGroup(m_pDropDownMenu);
124 
125  for (auto item : m_items)
126  {
127  QAction* action = new QAction(QString::fromStdString(item.second), m_pDropDownMenu);
128  action->setCheckable(true);
129  action->setData(QVariant(item.first));
130  m_actionGroup->addAction(action);
131  m_pDropDownMenu->addAction(action);
132 
133  if (item.first == m_selection)
134  {
135  action->setChecked(true);
136  }
137  }
138  QObject::connect(m_actionGroup, SIGNAL(triggered(QAction*)), this, SLOT(onSelection(QAction*)));
139  m_dropDownButton->setMenu(m_pDropDownMenu);
140 
141  QVBoxLayout* vLayout = new QVBoxLayout();
142  vLayout->addWidget( m_dropDownButton);
143  vLayout->setContentsMargins(0, 0, 0, 0);
144 
145  qtContainer->setLayout( vLayout );
146 }
147 
148 //------------------------------------------------------------------------------
149 
151 {
152  QObject::connect(m_actionGroup, SIGNAL(triggered(QAction*)), this, SLOT(onSelection(QAction*)));
153  for (QAction* action : m_actionGroup->actions())
154  {
155  m_actionGroup->removeAction(action);
156  }
157 
158  this->destroy();
159 }
160 
161 //------------------------------------------------------------------------------
162 
164 {
165 }
166 
167 //------------------------------------------------------------------------------
168 
170 {
171 
172 }
173 
174 //------------------------------------------------------------------------------
175 
176 void SSelectionMenuButton::info( std::ostream& _sstream )
177 {
178 }
179 
180 //------------------------------------------------------------------------------
181 
183 {
184  if (action->isChecked())
185  {
186  int value = action->data().toInt();
187  m_sigSelected->asyncEmit(value);
188  return;
189  }
190 }
191 
192 //------------------------------------------------------------------------------
193 
194 void SSelectionMenuButton::setEnabled(bool enabled)
195 {
196  m_dropDownButton->setEnabled(enabled);
197 }
198 
199 //------------------------------------------------------------------------------
200 
201 void SSelectionMenuButton::enable()
202 {
203  this->setEnabled(true);
204 }
205 
206 //------------------------------------------------------------------------------
207 
208 void SSelectionMenuButton::disable()
209 {
210  this->setEnabled(false);
211 }
212 
213 //------------------------------------------------------------------------------
214 
215 } // namespace editor
216 } // namespace guiQt
virtual void swapping() override
Do nothing.
Defines the service interface managing the editor service for object.
Definition: IEditor.hpp:25
virtual void starting() override
Install the layout.
FWGUI_API void destroy()
Stops sub-views and toobar services. Destroys view, sub-views and toolbar containers.
This service show a menu button. The user can select one item in the menu.
virtual void configuring() override
Configure the editor.
GUIQT_API SSelectionMenuButton() noexcept
Constructor. 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
virtual void stopping() override
Destroy the layout.
virtual GUIQT_API ~SSelectionMenuButton() noexcept
Destructor. 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
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
The namespace guiQt contains the basic services to build the application IHM with Qt...
Definition: Code.hpp:21
virtual void updating() override
Do nothing.
virtual void info(std::ostream &_sstream) override
Overrides.
void onSelection(QAction *action)
This method is called when the popup menu is clicked. Notify the selection changed.
FWGUI_API void initialize()
Initialize managers.