Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkServiceListWidget.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 //#define _USE_MATH_DEFINES
14 #include <QmitkServiceListWidget.h>
15 
16 // STL Headers
17 #include <list>
18 
19 // microservices
20 #include <usGetModuleContext.h>
21 #include <usModuleContext.h>
22 #include <usServiceProperties.h>
23 
24 #include <mitkCommon.h>
25 
26 const std::string QmitkServiceListWidget::VIEW_ID = "org.mitk.views.QmitkServiceListWidget";
27 
28 QmitkServiceListWidget::QmitkServiceListWidget(QWidget *parent, Qt::WindowFlags f)
29  : QWidget(parent, f), m_AutomaticallySelectFirstEntry(false), m_Controls(nullptr)
30 {
31  CreateQtPartControl(this);
32 }
33 
35 {
36  m_Context->RemoveServiceListener(this, &QmitkServiceListWidget::OnServiceEvent);
37 }
38 
39 void QmitkServiceListWidget::SetAutomaticallySelectFirstEntry(bool automaticallySelectFirstEntry)
40 {
41  m_AutomaticallySelectFirstEntry = automaticallySelectFirstEntry;
42 }
43 
45 
47 {
48  if (!m_Controls)
49  {
50  // create GUI widgets
51  m_Controls = new Ui::QmitkServiceListWidgetControls;
52  m_Controls->setupUi(parent);
53  this->CreateConnections();
54  }
55  m_Context = us::GetModuleContext();
56 }
57 
59 {
60  if (m_Controls)
61  {
62  connect(m_Controls->m_ServiceList,
63  SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
64  this,
66  }
67 }
68 
69 void QmitkServiceListWidget::InitPrivate(const std::string &namingProperty, const std::string &filter)
70 {
71  if (filter.empty())
72  m_Filter = "(" + us::ServiceConstants::OBJECTCLASS() + "=" + m_Interface + ")";
73  else
74  m_Filter = filter;
75  m_NamingProperty = namingProperty;
76  m_Context->RemoveServiceListener(this, &QmitkServiceListWidget::OnServiceEvent);
77  m_Context->AddServiceListener(this, &QmitkServiceListWidget::OnServiceEvent, m_Filter);
78  // Empty ListWidget
79  this->m_ListContent.clear();
80  m_Controls->m_ServiceList->clear();
81 
82  // get Services
83  std::vector<us::ServiceReferenceU> services = this->GetAllRegisteredServices();
84  // Transfer them to the List
85  for (std::vector<us::ServiceReferenceU>::iterator it = services.begin(); it != services.end(); ++it)
86  AddServiceToList(*it);
87 }
88 
90 
92 {
93  return (this->m_Controls->m_ServiceList->currentItem() != nullptr);
94 }
95 
97 {
98  us::ServiceReferenceU ref = this->GetServiceForListItem(this->m_Controls->m_ServiceList->currentItem());
99  if (!ref)
100  {
102  return;
103  }
104 
105  emit(ServiceSelectionChanged(ref));
106 }
107 
109 {
110  return this->GetServiceForListItem(this->m_Controls->m_ServiceList->currentItem());
111 }
112 
113 std::vector<us::ServiceReferenceU> QmitkServiceListWidget::GetAllServiceReferences()
114 {
115  std::vector<us::ServiceReferenceU> result;
116  for (size_t i = 0; i < m_ListContent.size(); i++)
117  {
118  result.push_back(m_ListContent[i].service);
119  }
120  return result;
121 }
122 
124 
126 {
127  // MITK_INFO << "ServiceEvent" << event.GetType();
128  switch (event.GetType())
129  {
131  emit(ServiceModified(event.GetServiceReference()));
132 
133  // Change service; add a new entry if service wasn't on list before
134  if (!this->ChangeServiceOnList(event.GetServiceReference()))
135  {
136  this->AddServiceToList(event.GetServiceReference());
137  }
138  break;
140  emit(ServiceRegistered(event.GetServiceReference()));
142  break;
146  break;
150  break;
151  }
152 }
153 
155 
157 {
158  QListWidgetItem *newItem = new QListWidgetItem;
159 
160  newItem->setText(this->CreateCaptionForService(serviceRef));
161 
162  // Add new item to QListWidget
163  m_Controls->m_ServiceList->addItem(newItem);
164  m_Controls->m_ServiceList->sortItems();
165 
166  // Construct link and add to internal List for reference
168  link.service = serviceRef;
169  link.item = newItem;
170  m_ListContent.push_back(link);
171 
172  // Select first entry and emit corresponding signal if the list was
173  // empty before and this feature is enabled
174  if (m_AutomaticallySelectFirstEntry && m_Controls->m_ServiceList->selectedItems().isEmpty())
175  {
176  m_Controls->m_ServiceList->setCurrentIndex(m_Controls->m_ServiceList->indexAt(QPoint(0, 0)));
178  }
179 
180  return newItem;
181 }
182 
184 {
185  for (std::vector<QmitkServiceListWidget::ServiceListLink>::iterator it = m_ListContent.begin();
186  it != m_ListContent.end();
187  ++it)
188  {
189  if (serviceRef == it->service)
190  {
191  int row = m_Controls->m_ServiceList->row(it->item);
192  QListWidgetItem *oldItem = m_Controls->m_ServiceList->takeItem(row);
193  delete oldItem;
194  this->m_ListContent.erase(it);
195  return true;
196  }
197  }
198  return false;
199 }
200 
202 {
203  for (std::vector<QmitkServiceListWidget::ServiceListLink>::iterator it = m_ListContent.begin();
204  it != m_ListContent.end();
205  ++it)
206  {
207  if (serviceRef == it->service)
208  {
209  it->item->setText(this->CreateCaptionForService(serviceRef));
210  return true;
211  }
212  }
213  return false;
214 }
215 
217 {
218  for (std::vector<QmitkServiceListWidget::ServiceListLink>::iterator it = m_ListContent.begin();
219  it != m_ListContent.end();
220  ++it)
221  if (item == it->item)
222  return it->service;
223  // Return invalid ServiceReference (will evaluate to false in bool expressions)
224  return us::ServiceReferenceU();
225 }
226 
227 std::vector<us::ServiceReferenceU> QmitkServiceListWidget::GetAllRegisteredServices()
228 {
229  // Get Service References
230  return m_Context->GetServiceReferences(m_Interface, m_Filter);
231 }
232 
234 {
235  std::string caption;
236  // TODO allow more complex formatting
237  if (m_NamingProperty.empty())
238  caption = m_Interface;
239  else
240  {
241  us::Any prop = serviceRef.GetProperty(m_NamingProperty);
242  if (prop.Empty())
243  {
244  MITK_WARN << "QmitkServiceListWidget tried to resolve property '" + m_NamingProperty +
245  "' but failed. Resorting to interface name for display.";
246  caption = m_Interface;
247  }
248  else
249  caption = prop.ToString();
250  }
251 
252  return QString::fromStdString(caption);
253 }
us::ServiceReferenceU GetServiceForListItem(QListWidgetItem *item)
Returns the serviceReference corresponding to the given ListEntry or an invalid one if none was found...
ServiceReference< void > ServiceReferenceU
Definition: usModule.h:40
void OnServiceSelectionChanged()
Called, when the selection in the list of Services changes.
QString CreateCaptionForService(const us::ServiceReferenceU &serviceRef)
Gets string from the naming property of the service.
Ui::QmitkServiceListWidgetControls * m_Controls
member holding the UI elements of this widget
QmitkServiceListWidget(QWidget *p=nullptr, Qt::WindowFlags f1=nullptr)
bool Empty() const
Definition: usAny.h:246
std::string ToString() const
Definition: usAny.h:257
void ServiceRegistered(us::ServiceReferenceU)
Emitted when a new Service matching the filter is being registered.
virtual void CreateConnections()
This method is part of the widget an needs not to be called separately. (Creation of the connections ...
virtual void CreateQtPartControl(QWidget *parent)
This method is part of the widget an needs not to be called separately.
void InitPrivate(const std::string &namingProperty, const std::string &filter)
Finishes initialization after Initialize has been called.
void ServiceUnregistering(us::ServiceReferenceU)
Emitted directly before a Service matching the filter is being unregistered.
#define MITK_WARN
Definition: mitkLogMacros.h:19
void OnServiceEvent(const us::ServiceEvent event)
This Function listens to ServiceRegistry changes and updates the list of services accordingly...
static const std::string VIEW_ID
ServiceReferenceU GetServiceReference() const
void ServiceModified(us::ServiceReferenceU)
Emitted when a Service matching the filter changes it&#39;s properties, or when a service that formerly n...
std::vector< us::ServiceReferenceU > GetAllServiceReferences()
Definition: usAny.h:163
US_Core_EXPORT const std::string & OBJECTCLASS()
bool GetIsServiceSelected()
Will return true, if a service is currently selected and false otherwise.
std::vector< ServiceListLink > m_ListContent
Contains a list of currently active services and their entires in the list. This is wiped with every ...
bool RemoveServiceFromList(const us::ServiceReferenceU &serviceRef)
Removes the given service from the list and cleans up. Returns true if successful, false if service was not found.
Type GetType() const
us::ServiceReferenceU GetSelectedServiceReference()
Returns the currently selected Service as a ServiceReference.
void SetAutomaticallySelectFirstEntry(bool automaticallySelectFirstEntry)
Set if the first entry of the list should be selected automatically if no entry was selected before...
bool ChangeServiceOnList(const us::ServiceReferenceU &serviceRef)
Changes list entry of given service to match the changed service properties.
std::vector< us::ServiceReferenceU > GetAllRegisteredServices()
Returns a list of ServiceReferences matching the filter criteria by querying the service registry...
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void ServiceSelectionChanged(us::ServiceReferenceU)
Emitted if the user selects a Service from the list.
void ServiceModifiedEndMatch(us::ServiceReferenceU)
Emitted when a Service matching the filter changes it&#39;s properties,.
QListWidgetItem * AddServiceToList(const us::ServiceReferenceU &serviceRef)
Constructs a ListItem from the given service, displays it, and locally stores the service...