fw4spl
t/src/fwGuiQt/dialog/MultiSelectorDialog.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
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/MultiSelectorDialog.hpp"
8 
9 #include <fwCore/base.hpp>
10 #include <fwGui/registry/macros.hpp>
11 
12 #include <QApplication>
13 #include <QHBoxLayout>
14 #include <QLabel>
15 #include <QListWidget>
16 #include <QPushButton>
17 #include <QPushButton>
18 #include <QVBoxLayout>
19 
21 
22 namespace fwGuiQt
23 {
24 namespace dialog
25 {
26 
27 //------------------------------------------------------------------------------
28 
29 MultiSelectorDialog::MultiSelectorDialog(::fwGui::GuiBaseObject::Key key)
30  : m_message(""),
31  m_title("")
32 {
33 }
34 
35 //------------------------------------------------------------------------------
36 
37 MultiSelectorDialog::~MultiSelectorDialog()
38 {
39 }
40 
41 //------------------------------------------------------------------------------
42 
43 void MultiSelectorDialog::setSelections(Selections _selections)
44 {
45  this->m_selections = _selections;
46 }
47 
48 //------------------------------------------------------------------------------
49 
50 void MultiSelectorDialog::setTitle(std::string _title)
51 {
52  this->m_title = _title;
53 }
54 
55 //------------------------------------------------------------------------------
56 
57 ::fwGui::dialog::IMultiSelectorDialog::Selections MultiSelectorDialog::show()
58 {
59  QWidget *parent = qApp->activeWindow();
60 
61  QDialog* dialog = new QDialog(parent);
62  dialog->setWindowTitle(QString::fromStdString(m_title));
63 
64  QListWidget *selectionList = new QListWidget(dialog);
65  for( Selections::value_type selection : m_selections)
66  {
67  QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(selection.first), selectionList);
68  item->setCheckState( (selection.second ? Qt::Checked : Qt::Unchecked) );
69  selectionList->addItem(item);
70  }
71 
72  QListWidgetItem* firstItem = selectionList->item(0);
73  selectionList->setCurrentItem(firstItem);
74 
75  QPushButton* okButton = new QPushButton(tr("Ok"));
76  QPushButton* cancelButton = new QPushButton(tr("Cancel"));
77 
78  QHBoxLayout *hLayout = new QHBoxLayout();
79  hLayout->addWidget(okButton);
80  hLayout->addWidget(cancelButton);
81 
82  QVBoxLayout *vLayout = new QVBoxLayout();
83  if(!m_message.empty())
84  {
85  QLabel* msgText = new QLabel(QString::fromStdString(m_message), dialog);
86  vLayout->addWidget( msgText);
87  }
88  vLayout->addWidget(selectionList);
89  vLayout->addLayout(hLayout);
90 
91  dialog->setLayout(vLayout);
92  QObject::connect(okButton, SIGNAL(clicked()), dialog, SLOT(accept()));
93  QObject::connect(cancelButton, SIGNAL(clicked()), dialog, SLOT(reject()));
94  QObject::connect(selectionList, SIGNAL(itemDoubleClicked( QListWidgetItem * )), dialog, SLOT(accept()));
95 
96  Selections selections;
97  if(dialog->exec())
98  {
99  int indexItem = 0;
100  for( Selections::value_type selection : m_selections)
101  {
102  selections[selection.first] = (selectionList->item(indexItem)->checkState() == Qt::Checked);
103  indexItem++;
104  }
105  }
106  return selections;
107 }
108 
109 //------------------------------------------------------------------------------
110 
111 void MultiSelectorDialog::setMessage(const std::string &msg)
112 {
113  m_message = msg;
114 }
115 
116 //------------------------------------------------------------------------------
117 
118 } // namespace dialog
119 } // namespace fwGuiQt
120 
121 
MultiSelectorDialog allowing the choice of an element among severals (_selections) ...
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
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 Selections show() override
Show the selector and return the selection.
static FWGUI_API const FactoryRegistryKeyType REGISTRY_KEY
this unique key should be used for all factory for specific Selector(qt,wx,...)
virtual FWGUI_API void setMessage(const std::string &msg) override
Set the message.
virtual FWGUI_API void setSelections(Selections _selections) override
Set the string list that can be chosen by the selector.