fw4spl
action/SCreateActivity.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2016-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 "activities/action/SCreateActivity.hpp"
8 
9 #include <fwActivities/IBuilder.hpp>
10 #include <fwActivities/IValidator.hpp>
11 
12 #include <fwCom/Signal.hpp>
13 #include <fwCom/Signal.hxx>
14 #include <fwCom/Slot.hpp>
15 #include <fwCom/Slots.hpp>
16 #include <fwCom/Slots.hxx>
17 
18 #include <fwData/Composite.hpp>
19 #include <fwData/String.hpp>
20 #include <fwData/Vector.hpp>
21 
22 #include <fwGui/dialog/MessageDialog.hpp>
23 #include <fwGui/dialog/SelectorDialog.hpp>
24 
25 #include <fwMedData/ActivitySeries.hpp>
26 
27 #include <fwRuntime/Bundle.hpp>
28 #include <fwRuntime/Convert.hpp>
29 #include <fwRuntime/operations.hpp>
30 
31 #include <fwServices/macros.hpp>
32 
33 #include <boost/foreach.hpp>
34 
35 #include <QApplication>
36 #include <QDialog>
37 #include <QHBoxLayout>
38 #include <QListWidget>
39 #include <QPushButton>
40 #include <QStandardItemModel>
41 #include <QVBoxLayout>
42 
43 Q_DECLARE_METATYPE(::fwActivities::registry::ActivityInfo)
44 
45 namespace activities
46 {
47 namespace action
48 {
49 
50 //------------------------------------------------------------------------------
51 
53 
54 //------------------------------------------------------------------------------
55 
56 const ::fwCom::Signals::SignalKeyType SCreateActivity::s_ACTIVITY_ID_SELECTED_SIG = "activityIDSelected";
57 const ::fwCom::Signals::SignalKeyType SCreateActivity::s_ACTIVITY_SELECTED_SIG = "activitySelected";
58 
59 //------------------------------------------------------------------------------
60 
61 SCreateActivity::SCreateActivity() noexcept
62 {
63  m_sigActivityIDSelected = newSignal< ActivityIDSelectedSignalType >(s_ACTIVITY_ID_SELECTED_SIG);
64  m_sigActivitySelected = newSignal< ActivitySelectedSignalType >(s_ACTIVITY_SELECTED_SIG);
65 }
66 
67 //------------------------------------------------------------------------------
68 
69 SCreateActivity::~SCreateActivity() noexcept
70 {
71 }
72 
73 //------------------------------------------------------------------------------
74 
75 void SCreateActivity::starting()
76 {
77  this->actionServiceStarting();
78 }
79 
80 //------------------------------------------------------------------------------
81 
82 void SCreateActivity::stopping()
83 {
84  this->actionServiceStopping();
85 }
86 
87 //------------------------------------------------------------------------------
88 
89 void SCreateActivity::configuring()
90 {
92  typedef ::fwServices::IService::ConfigType ConfigType;
93 
94  const ::fwServices::IService::ConfigType srvconfig = this->getConfigTree();
95 
96  if(srvconfig.count("filter") == 1 )
97  {
98  const ::fwServices::IService::ConfigType& configFilter = srvconfig.get_child("filter");
99  SLM_ASSERT("A maximum of 1 <mode> tag is allowed", configFilter.count("mode") < 2);
100 
101  const std::string mode = configFilter.get< std::string >("mode");
102  SLM_ASSERT("'" + mode + "' value for <mode> tag isn't valid. Allowed values are : 'include', 'exclude'.",
103  mode == "include" || mode == "exclude");
104  m_filterMode = mode;
105 
106  BOOST_FOREACH( const ConfigType::value_type& v, configFilter.equal_range("id") )
107  {
108  m_keys.push_back(v.second.get<std::string>(""));
109  }
110  }
111  SLM_ASSERT("A maximum of 1 <filter> tag is allowed", srvconfig.count("filter") < 2);
112 }
113 
114 //------------------------------------------------------------------------------
115 
116 ::fwActivities::registry::ActivityInfo SCreateActivity::show( const ActivityInfoContainer& infos )
117 {
118  QWidget* parent = qApp->activeWindow();
119 
120  QDialog* dialog = new QDialog(parent);
121  dialog->setWindowTitle(QString::fromStdString("Choose an activity"));
122  dialog->resize(600, 400);
123 
124  QStandardItemModel* model = new QStandardItemModel(dialog);
125  for( const ::fwActivities::registry::ActivityInfo& info : infos)
126  {
127  std::string text;
128  if(info.title.empty())
129  {
130  text = info.id;
131  }
132  else
133  {
134  text = info.title + (info.description.empty() ? "" : "\n" + info.description);
135  }
136 
137  QStandardItem* item = new QStandardItem(QIcon(info.icon.c_str()), QString::fromStdString(text));
138  item->setData(QVariant::fromValue(info));
139  item->setEditable(false);
140  model->appendRow(item);
141  }
142 
143  QListView* selectionList = new QListView();
144  selectionList->setIconSize(QSize(40, 40));
145  selectionList->setUniformItemSizes(true);
146  selectionList->setModel(model);
147 
148  QModelIndex index = model->index( 0, 0 );
149  if ( index.isValid() )
150  {
151  selectionList->selectionModel()->select( index, QItemSelectionModel::Select );
152  }
153 
154  QPushButton* okButton = new QPushButton("Ok");
155  QPushButton* cancelButton = new QPushButton("Cancel");
156 
157  QHBoxLayout* hLayout = new QHBoxLayout();
158  hLayout->addWidget(okButton);
159  hLayout->addWidget(cancelButton);
160 
161  QVBoxLayout* vLayout = new QVBoxLayout();
162  vLayout->addWidget(selectionList);
163  vLayout->addLayout(hLayout);
164 
165  dialog->setLayout(vLayout);
166  QObject::connect(okButton, SIGNAL(clicked()), dialog, SLOT(accept()));
167  QObject::connect(cancelButton, SIGNAL(clicked()), dialog, SLOT(reject()));
168  QObject::connect(selectionList, SIGNAL(doubleClicked(const QModelIndex&)), dialog, SLOT(accept()));
169 
171  if(dialog->exec())
172  {
173  QModelIndex currentIndex = selectionList->selectionModel()->currentIndex();
174  QStandardItem* item = model->itemFromIndex( currentIndex );
175  QVariant var = item->data();
176  info = var.value< ::fwActivities::registry::ActivityInfo >();
177  }
178 
179  return info;
180 }
181 
182 //------------------------------------------------------------------------------
183 
184 SCreateActivity::ActivityInfoContainer SCreateActivity::getEnabledActivities(const ActivityInfoContainer& infos)
185 {
186  ActivityInfoContainer configs;
187 
188  if(m_filterMode == "include" || m_filterMode == "exclude")
189  {
190  const bool isIncludeMode = m_filterMode == "include";
191 
192  for(auto info : infos)
193  {
194  KeysType::iterator keyIt = std::find(m_keys.begin(), m_keys.end(), info.id);
195 
196  if(keyIt != m_keys.end() && isIncludeMode)
197  {
198  configs.push_back(info);
199  }
200  else if(keyIt == m_keys.end() && !isIncludeMode)
201  {
202  configs.push_back(info);
203  }
204  }
205  }
206  else
207  {
208  configs = infos;
209  }
210 
211  return configs;
212 }
213 
214 //------------------------------------------------------------------------------
215 
216 void SCreateActivity::updating()
217 {
218  ActivityInfoContainer infos = ::fwActivities::registry::Activities::getDefault()->getInfos();
219  infos = this->getEnabledActivities(infos);
220 
221  if ( !infos.empty())
222  {
224  if((m_keys.size() == 1 && m_filterMode == "include") || (infos.size() == 1))
225  {
226  info = infos[0];
227  }
228  else
229  {
230  info = this->show( infos );
231  }
232 
233  if( !info.id.empty() )
234  {
235  m_sigActivityIDSelected->asyncEmit(info.id);
236  }
237  }
238  else
239  {
241  "No available activity for the current selection.",
242  ::fwGui::dialog::MessageDialog::WARNING);
243  }
244 }
245 
246 //------------------------------------------------------------------------------
247 
248 }
249 }
Holds Activities configuration.
Definition: Activities.hpp:175
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
static FWACTIVITIES_API Activities::sptr getDefault()
Return the default global instance of Activities.
Definition: Activities.cpp:226
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
This class defines a vector of objects.
#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
FWGUI_API void initialize()
Initialize the action.
Definition: IActionSrv.cpp:69
The namespace activities contains helpers and services allowing to launch activities.
This action launchs an activity according to the given configuration.