fw4spl
SSignalShortcut.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 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/SSignalShortcut.hpp"
8 
9 #include <fwCom/Signal.hxx>
10 
11 #include <fwCore/base.hpp>
12 
13 #include <fwGui/container/fwContainer.hpp>
14 #include <fwGui/GuiRegistry.hpp>
15 #include <fwGui/IGuiContainerSrv.hpp>
16 
17 #include <fwGuiQt/container/QtContainer.hpp>
18 
19 #include <fwServices/macros.hpp>
20 #include <fwServices/op/Get.hpp>
21 
22 #include <QKeySequence>
23 #include <QWidget>
24 
25 #include <memory>
26 
27 namespace guiQt
28 {
29 
30 static const ::fwCom::Signals::SignalKeyType s_ACTIVATED_SIG = "activated";
31 
32 fwServicesRegisterMacro( ::fwServices::IService, ::guiQt::SSignalShortcut );
33 
34 //-----------------------------------------------------------------------------
35 
37  m_shortcut(""),
38  m_sid(""),
39  m_wid(""),
40  m_shortcutObject(nullptr)
41 {
42  newSignal< ActivatedShortcutSignalType >(s_ACTIVATED_SIG);
43 }
44 
45 //-----------------------------------------------------------------------------
46 
48 {
49 }
50 
51 //-----------------------------------------------------------------------------
52 
54 {
55  const auto configTree = this->getConfigTree();
56  const auto configShortcut = configTree.get_child("config.<xmlattr>");
57  m_shortcut = configShortcut.get<std::string>("shortcut", m_shortcut);
58  OSLM_ASSERT("Shortcut must not be empty", m_shortcut != "");
59 
60  m_wid = configShortcut.get<std::string>("wid", m_wid);
61  m_sid = configShortcut.get<std::string>("sid", m_sid);
62  OSLM_ASSERT("Either The wid or sid attribute must be specified for SSignalShortcut", m_wid != "" || m_sid != "");
63 }
64 
65 //-----------------------------------------------------------------------------
66 
68 {
69  ::fwGui::container::fwContainer::sptr fwc = nullptr;
70 
71  // Either get the container via a service id
72  if(m_sid != "")
73  {
74  bool sidExists = ::fwTools::fwID::exist(m_sid);
75 
76  if(sidExists)
77  {
78  ::fwServices::IService::sptr service = ::fwServices::get( m_sid );
79  ::fwGui::IGuiContainerSrv::sptr containerSrv = ::fwGui::IGuiContainerSrv::dynamicCast(service);
80  fwc = containerSrv->getContainer();
81  }
82  else
83  {
84  OSLM_ERROR("Invalid service id " << m_sid);
85  }
86  }
87  // or a window id
88  else if(m_wid != "")
89  {
91  if(!fwc)
92  {
93  OSLM_ERROR("Invalid window id " << m_wid);
94  }
95  }
96 
97  if(fwc != nullptr)
98  {
99  ::fwGuiQt::container::QtContainer::sptr qtc =
100  std::dynamic_pointer_cast< ::fwGuiQt::container::QtContainer >(fwc);
101  if(qtc != nullptr)
102  {
103 
104  if(!m_shortcutObject)
105  {
106  // Get the associated widget to use as parent for the shortcut
107  QWidget* widget = qtc->getQtContainer();
108  // Create a key sequence from the string and its associated QShortcut
109  QKeySequence shortcutSequence = QKeySequence(QString::fromStdString(m_shortcut));
110  m_shortcutObject = new QShortcut(shortcutSequence, widget);
111  }
112 
113  // Connect the activated signal to the onActivation method of this class
114  QObject::connect(m_shortcutObject, SIGNAL(activated()), this, SLOT(onActivation()));
115  }
116  }
117  else
118  {
119  OSLM_ERROR("Cannot setup shortcut " << m_shortcut << " on invalid "
120  << (m_wid != "" ? "wid " + m_wid : "sid " + m_sid));
121  }
122 }
123 
124 //-----------------------------------------------------------------------------
125 
127 {
128  if(m_shortcutObject)
129  {
130  // Connect the activated signal to the onActivation method of this class
131  QObject::disconnect(m_shortcutObject, SIGNAL(activated()), this, SLOT(onActivation()));
132  }
133 }
134 
135 //-----------------------------------------------------------------------------
137 {
138 
139 }
140 
141 //------------------------------------------------------------------------------
142 
143 void SSignalShortcut::onActivation()
144 {
145  this->signal<ActivatedShortcutSignalType>(s_ACTIVATED_SIG)->asyncEmit();
146 }
147 
148 } // namespace gui
Base class for all services.
Definition: IService.hpp:61
static FWGUI_API::fwGui::container::fwContainer::sptr getWIDContainer(std::string wid)
Returns fwContainer associate with window ID, null if not found.
#define OSLM_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:310
static FWTOOLS_API bool exist(IDType _id)
Definition: fwID.cpp:33
virtual GUIQT_API void configuring() override
This method configures the service.
GUIQT_API SSignalShortcut() noexcept
Constructor. Do nothing.
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
This service sends a signal when the associated shortcut is activated.
virtual GUIQT_API void stopping() override
This method deletes the eventFilter.
virtual GUIQT_API void starting() override
This method enables the eventFilter.
virtual GUIQT_API ~SSignalShortcut() noexcept
Destructor. Do nothing.
virtual GUIQT_API void updating() override
This method does nothing.
The namespace guiQt contains the basic services to build the application IHM with Qt...
Definition: Code.hpp:21
Defines the QWidget container for UI.
Definition: QtContainer.hpp:35
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247