fw4spl
t/src/fwGuiQt/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 "fwGuiQt/dialog/MessageDialog.hpp"
8 
9 #include <fwGui/registry/macros.hpp>
10 
11 #include <boost/assign/list_of.hpp>
12 
13 #include <QApplication>
14 #include <QMessageBox>
15 #include <QPushButton>
16 #include <QVector>
17 
18 fwGuiRegisterMacro( ::fwGuiQt::dialog::MessageDialog, ::fwGui::dialog::IMessageDialog::REGISTRY_KEY );
19 
20 namespace fwGuiQt
21 {
22 namespace dialog
23 {
24 
25 //------------------------------------------------------------------------------
26 
27 typedef const std::map< ::fwGui::dialog::IMessageDialog::Icons, QMessageBox::Icon> MessageDialogQtIconsType;
28 MessageDialogQtIconsType messageDialogQtIcons =
29  ::boost::assign::map_list_of(::fwGui::dialog::IMessageDialog::NONE, QMessageBox::NoIcon )
30  (::fwGui::dialog::IMessageDialog::QUESTION, QMessageBox::Question )
31  (::fwGui::dialog::IMessageDialog::INFO, QMessageBox::Information)
32  (::fwGui::dialog::IMessageDialog::WARNING, QMessageBox::Warning )
33  (::fwGui::dialog::IMessageDialog::CRITICAL, QMessageBox::Critical );
34 
35 typedef const std::map< ::fwGui::dialog::IMessageDialog::Buttons,
36  QMessageBox::StandardButtons> MessageDialogQtButtonType;
37 MessageDialogQtButtonType messageDialogQtButton =
38  ::boost::assign::map_list_of(::fwGui::dialog::IMessageDialog::OK, QMessageBox::Ok )
39  (::fwGui::dialog::IMessageDialog::CANCEL, QMessageBox::Cancel)
40  (::fwGui::dialog::IMessageDialog::YES, QMessageBox::Yes )
41  (::fwGui::dialog::IMessageDialog::NO, QMessageBox::No );
42 
43 //------------------------------------------------------------------------------
44 
45 MessageDialog::MessageDialog(::fwGui::GuiBaseObject::Key key) :
46  m_buttons(::fwGui::dialog::IMessageDialog::NOBUTTON),
47  m_defaultButton(::fwGui::dialog::IMessageDialog::NOBUTTON),
48  m_icon(::fwGui::dialog::IMessageDialog::NONE)
49 {
50 }
51 
52 //------------------------------------------------------------------------------
53 
55 {
56 }
57 
58 //------------------------------------------------------------------------------
59 
60 void MessageDialog::setTitle( const std::string& title )
61 {
62  m_title = title;
63 }
64 
65 //------------------------------------------------------------------------------
66 
67 void MessageDialog::setMessage( const std::string& msg )
68 {
69  m_message = msg;
70 }
71 
72 //------------------------------------------------------------------------------
73 
75 {
76  m_icon = icon;
77 }
78 
79 //------------------------------------------------------------------------------
80 
82 {
83  m_buttons = (::fwGui::dialog::IMessageDialog::Buttons) ( m_buttons | button );
84 }
85 
86 //------------------------------------------------------------------------------
87 
88 void MessageDialog::addCustomButton(const std::string& label, std::function<void()> clickedFn)
89 {
90  QPushButton* button = new QPushButton( QString::fromStdString(label) );
91  m_customButtons.push_back( button );
92  QObject::connect(button, &QPushButton::clicked, clickedFn);
93 }
94 
95 //-----------------------------------------------------------------------------
96 
98 {
99  m_defaultButton = button;
100 }
101 
102 //------------------------------------------------------------------------------
103 
104 ::fwGui::dialog::IMessageDialog::Buttons MessageDialog::show()
105 {
106  MessageDialogQtIconsType::const_iterator iterIcon = messageDialogQtIcons.find(m_icon);
107  SLM_ASSERT("Unknown Icon", iterIcon != messageDialogQtIcons.end());
108 
109  QMessageBox::Icon icon = iterIcon->second;
110  QString title = QString::fromStdString(m_title);
111  QString text = QString::fromStdString(m_message);
112  QMessageBox::StandardButtons buttons = QMessageBox::NoButton;
113 
114  for(MessageDialogQtButtonType::value_type button : messageDialogQtButton)
115  {
116  if ( m_buttons & button.first)
117  {
118  buttons |= button.second;
119  }
120  }
121 
122  QMessageBox box(icon, title, text, buttons, qApp->activeWindow());
123 
124  for(auto customButton : m_customButtons)
125  {
126  box.addButton(customButton, QMessageBox::ActionRole);
127  }
128 
129  MessageDialogQtButtonType::const_iterator iter = messageDialogQtButton.find(m_defaultButton);
130  if(iter != messageDialogQtButton.end())
131  {
132  box.setDefaultButton(QMessageBox::StandardButton(static_cast<int>(iter->second)));
133  }
134 
135  ::fwGui::dialog::IMessageDialog::Buttons result = ::fwGui::dialog::IMessageDialog::NOBUTTON;
136 
137  box.exec();
138 
139  for(MessageDialogQtButtonType::value_type button : messageDialogQtButton)
140  {
141  if ( box.standardButton( box.clickedButton() ) == button.second)
142  {
143  result = button.first;
144  break;
145  }
146  }
147  return result;
148 }
149 
150 //------------------------------------------------------------------------------
151 
152 } // namespace dialog
153 } // namespace fwGuiQt
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
virtual FWGUI_API void setMessage(const std::string &msg) override
Set the message.
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...
Definition: WindowLevel.hpp:32
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.
#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 FWGUI_API ~MessageDialog()
Destructor. Do nothing.
virtual FWGUI_API void setIcon(IMessageDialog::Icons icon) override
Set the icon (CRITICAL, WARNING, INFO or QUESTION)
virtual FWGUI_API void setDefaultButton(IMessageDialog::Buttons button) override
Set the default button.
Defines the generic message box for IHM.
virtual FWGUI_API void setTitle(const std::string &title) override
Set the title of the message box.