fw4spl
t/src/fwGuiQt/dialog/SelectorDialog.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/SelectorDialog.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwGui/registry/macros.hpp>
12 
13 #include <QApplication>
14 #include <QDialog>
15 #include <QHBoxLayout>
16 #include <QLabel>
17 #include <QListWidget>
18 #include <QPushButton>
19 #include <QVBoxLayout>
20 
22 
23 namespace fwGuiQt
24 {
25 namespace dialog
26 {
27 
28 //------------------------------------------------------------------------------
29 
30 SelectorDialog::SelectorDialog(::fwGui::GuiBaseObject::Key key) :
31  m_message(""),
32  m_title("")
33 {
34 }
35 
36 //------------------------------------------------------------------------------
37 
38 SelectorDialog::~SelectorDialog()
39 {
40 }
41 
42 //------------------------------------------------------------------------------
43 
44 void SelectorDialog::setSelections(std::vector< std::string > _selections)
45 {
46  this->m_selections = _selections;
47 }
48 
49 //------------------------------------------------------------------------------
50 
51 void SelectorDialog::setTitle(std::string _title)
52 {
53  this->m_title = _title;
54 }
55 
56 //------------------------------------------------------------------------------
57 
58 std::string SelectorDialog::show()
59 {
60  QWidget* parent = qApp->activeWindow();
61 
62  QDialog* dialog = new QDialog(parent);
63  dialog->setWindowTitle(QString::fromStdString(m_title));
64 
65  QListWidget* selectionList = new QListWidget(dialog);
66  for( std::string selection : m_selections)
67  {
68  selectionList->addItem(QString::fromStdString( selection ));
69  }
70 
71  QListWidgetItem* firstItem = selectionList->item(0);
72  selectionList->setCurrentItem(firstItem);
73 
74  QPushButton* okButton = new QPushButton(QObject::tr("Ok"));
75  QPushButton* cancelButton = new QPushButton(QObject::tr("Cancel"));
76 
77  QHBoxLayout* hLayout = new QHBoxLayout();
78  hLayout->addWidget(okButton);
79  hLayout->addWidget(cancelButton);
80 
81  for(auto customButton : m_customButtons)
82  {
83  hLayout->addWidget(customButton);
84  QObject::connect(customButton, SIGNAL(clicked()), dialog, SLOT(reject()));
85  }
86 
87  QVBoxLayout* vLayout = new QVBoxLayout();
88  if(!m_message.empty())
89  {
90  QLabel* msgText = new QLabel(QString::fromStdString(m_message), dialog);
91  vLayout->addWidget( msgText);
92  }
93  vLayout->addWidget(selectionList);
94  vLayout->addLayout(hLayout);
95 
96  dialog->setLayout(vLayout);
97  QObject::connect(okButton, SIGNAL(clicked()), dialog, SLOT(accept()));
98  QObject::connect(cancelButton, SIGNAL(clicked()), dialog, SLOT(reject()));
99  QObject::connect(selectionList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), dialog, SLOT(accept()));
100 
101  std::string selection = "";
102  if(dialog->exec())
103  {
104  selection = selectionList->currentItem()->text().toStdString();
105  }
106 
107  return selection;
108 }
109 
110 //------------------------------------------------------------------------------
111 
112 void SelectorDialog::setMessage(const std::string& msg)
113 {
114  m_message = msg;
115 }
116 
117 //------------------------------------------------------------------------------
118 
119 void SelectorDialog::addCustomButton(const std::string& label, std::function<void()> clickedFn)
120 {
121  QPushButton* button = new QPushButton( QString::fromStdString(label) );
122  m_customButtons.push_back( button );
123  QObject::connect(button, &QPushButton::clicked, clickedFn);
124 }
125 
126 //------------------------------------------------------------------------------
127 
128 } // namespace dialog
129 } // namespace fwGuiQt
virtual FWGUI_API void setSelections(std::vector< std::string > _selections) override
Set the string list that can be chosen by the selector.
virtual FWGUI_API void setMessage(const std::string &msg) override
Set the message.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
static FWGUI_API const FactoryRegistryKeyType REGISTRY_KEY
this unique key should be used for all factory for specific Selector(qt,wx,...)
FWGUI_API void setTitle(std::string title) override
Sets the selector title.
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...
Definition: WindowLevel.hpp:32
FWGUI_API std::string show() override
Show the selector and return the selection.
virtual FWGUI_API void addCustomButton(const std::string &label, std::function< void()> clickedFn) override
Add a custom button to this dialog.
SelectorDialog allowing the choice of an element among severals (_selections)