fw4spl
src/fwGui/dialog/MessageDialog.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 "fwGui/dialog/MessageDialog.hpp"
8 
9 #include <fwServices/registry/ActiveWorkers.hpp>
10 
11 #include <functional>
12 
13 namespace fwGui
14 {
15 namespace dialog
16 {
17 
18 //-----------------------------------------------------------------------------
19 
21  const std::string& title, const std::string& message, ::fwGui::dialog::IMessageDialog::Icons icon)
22 {
23  ::fwGui::dialog::MessageDialog messageBox(title, message, icon);
24  messageBox.addButton(::fwGui::dialog::IMessageDialog::OK);
25  return messageBox.show();
26 }
27 
28 //-----------------------------------------------------------------------------
29 
31 {
32  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>( std::function<void()>([&]
33  {
34  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(IMessageDialog::REGISTRY_KEY);
35  m_implementation = ::fwGui::dialog::IMessageDialog::dynamicCast(guiObj);
36  })).wait();
37 }
38 
39 //-----------------------------------------------------------------------------
40 
42  const std::string& title, const std::string& message, ::fwGui::dialog::IMessageDialog::Icons icon)
43 {
44  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>( std::function<void()>([&]
45  {
46  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(IMessageDialog::REGISTRY_KEY);
47  m_implementation = ::fwGui::dialog::IMessageDialog::dynamicCast(guiObj);
48 
50  {
51  m_implementation->setTitle(title);
52  m_implementation->setMessage(message);
53  m_implementation->setIcon(icon);
54  }
55  })).wait();
56 }
57 
58 //-----------------------------------------------------------------------------
59 
61 {
62 }
63 
64 //-----------------------------------------------------------------------------
65 
66 void MessageDialog::setTitle( const std::string& title )
67 {
68  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>( std::function<void()>([&]
69  {
71  {
72  m_implementation->setTitle(title);
73  }
74  })).wait();
75 }
76 
77 //-----------------------------------------------------------------------------
78 
79 void MessageDialog::setMessage( const std::string& msg )
80 {
81  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>( std::function<void()>([&]
82  {
84  {
85  m_implementation->setMessage(msg);
86  }
87  })).wait();
88 }
89 
90 //-----------------------------------------------------------------------------
91 
93 {
94  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>( std::function<void()>([&]
95  {
97  {
98  m_implementation->setIcon(icon);
99  }
100  })).wait();
101 }
102 
103 //-----------------------------------------------------------------------------
104 
106 {
107  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>( std::function<void()>([&]
108  {
109  if(m_implementation)
110  {
111  m_implementation->addButton(button);
112  }
113  } )).wait();
114 }
115 
116 //-----------------------------------------------------------------------------
117 
119 {
120  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>( std::function<void()>([&]
121  {
122  if(m_implementation)
123  {
124  m_implementation->setDefaultButton(button);
125  }
126  })).wait();
127 }
128 
129 //-----------------------------------------------------------------------------
130 
131 void MessageDialog::addCustomButton(const std::string& label, std::function<void()> clickedFn)
132 {
133  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>( std::function<void()>([&]
134  {
135  if(m_implementation)
136  {
137  m_implementation->addCustomButton(label, clickedFn);
138  }
139  })).wait();
140 }
141 
142 //-----------------------------------------------------------------------------
143 
145 {
146  if(m_implementation)
147  {
148  typedef ::fwGui::dialog::IMessageDialog::Buttons R;
149 
150  std::function<R()> func = std::bind(&IMessageDialog::show, m_implementation);
151  std::shared_future<R> f = ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<R>(func);
152  f.wait();
153 
154  return f.get();
155  }
156  else
157  {
158  return ::fwGui::dialog::IMessageDialog::NOBUTTON;
159  }
160 }
161 
162 //-----------------------------------------------------------------------------
163 
164 } //namespace dialog
165 } // namespace fwGui
FWGUI_API MessageDialog()
Constructor. Create the implementation of the specific message box.
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
virtual FWGUI_API void setMessage(const std::string &msg) override
Set the message.
Defines the generic message box for IHM. Use the Delegate design pattern.
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
virtual FWGUI_API void addCustomButton(const std::string &label, std::function< void()> clickedFn) override
Add a custom button to this dialog.
virtual FWGUI_API void addButton(IMessageDialog::Buttons button) override
Add a button (OK, YES_NO, YES, NO, CANCEL)
virtual FWGUI_API IMessageDialog::Buttons show() override
Show the message box and return the clicked button.
virtual FWGUI_API ~MessageDialog()
Destructor. Do nothing.
virtual FWGUI_API void setIcon(IMessageDialog::Icons icon) override
Set the icon (CRITICAL, WARNING, INFO or QUESTION)
static FWSERVICES_API::fwThread::Worker::sptr getDefaultWorker()
Get the default registered worker.
virtual FWGUI_API void setDefaultButton(IMessageDialog::Buttons button) override
Set the default button.
virtual FWGUI_API Buttons show()=0
Show the message box and return the clicked button.
::fwGui::dialog::IMessageDialog::sptr m_implementation
Implementation of a message box in a specific IHM (wx/Qt)
virtual FWGUI_API void setTitle(const std::string &title) override
Set the title of the message box.