Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 //#define _USE_MATH_DEFINES
18 #include <QmitkServiceListWidget.h>
19 
20 // STL Headers
21 #include <list>
22 
23 // microservices
24 #include <usGetModuleContext.h>
25 #include <usModuleContext.h>
26 #include <usServiceProperties.h>
27 
28 #include <mitkCommon.h>
29 
30 const std::string QmitkServiceListWidget::VIEW_ID = "org.mitk.views.QmitkServiceListWidget";
31 
32 QmitkServiceListWidget::QmitkServiceListWidget(QWidget *parent, Qt::WindowFlags f)
33  : QWidget(parent, f), m_AutomaticallySelectFirstEntry(false), m_Controls(NULL)
34 {
35  CreateQtPartControl(this);
36 }
37 
39 {
40  m_Context->RemoveServiceListener(this, &QmitkServiceListWidget::OnServiceEvent);
41 }
42 
43 void QmitkServiceListWidget::SetAutomaticallySelectFirstEntry(bool automaticallySelectFirstEntry)
44 {
45  m_AutomaticallySelectFirstEntry = automaticallySelectFirstEntry;
46 }
47 
49 
51 {
52  if (!m_Controls)
53  {
54  // create GUI widgets
55  m_Controls = new Ui::QmitkServiceListWidgetControls;
56  m_Controls->setupUi(parent);
57  this->CreateConnections();
58  }
59  m_Context = us::GetModuleContext();
60 }
61 
63 {
64  if (m_Controls)
65  {
66  connect(m_Controls->m_ServiceList,
67  SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
68  this,
70  }
71 }
72 
73 void QmitkServiceListWidget::InitPrivate(const std::string &namingProperty, const std::string &filter)
74 {
75  if (filter.empty())
76  m_Filter = "(" + us::ServiceConstants::OBJECTCLASS() + "=" + m_Interface + ")";
77  else
78  m_Filter = filter;
79  m_NamingProperty = namingProperty;
80  m_Context->RemoveServiceListener(this, &QmitkServiceListWidget::OnServiceEvent);
81  m_Context->AddServiceListener(this, &QmitkServiceListWidget::OnServiceEvent, m_Filter);
82  // Empty ListWidget
83  this->m_ListContent.clear();
84  m_Controls->m_ServiceList->clear();
85 
86  // get Services
87  std::vector<us::ServiceReferenceU> services = this->GetAllRegisteredServices();
88  // Transfer them to the List
89  for (std::vector<us::ServiceReferenceU>::iterator it = services.begin(); it != services.end(); ++it)
90  AddServiceToList(*it);
91 }
92 
94 
96 {
97  return (this->m_Controls->m_ServiceList->currentItem() != 0);
98 }
99 
101 {
102  us::ServiceReferenceU ref = this->GetServiceForListItem(this->m_Controls->m_ServiceList->currentItem());
103  if (!ref)
104  {
106  return;
107  }
108 
109  emit(ServiceSelectionChanged(ref));
110 }
111 
113 {
114  return this->GetServiceForListItem(this->m_Controls->m_ServiceList->currentItem());
115 }
116 
117 std::vector<us::ServiceReferenceU> QmitkServiceListWidget::GetAllServiceReferences()
118 {
119  std::vector<us::ServiceReferenceU> result;
120  for (size_t i = 0; i < m_ListContent.size(); i++)
121  {
122  result.push_back(m_ListContent[i].service);
123  }
124  return result;
125 }
126 
128 
130 {
131  // MITK_INFO << "ServiceEvent" << event.GetType();
132  switch (event.GetType())
133  {
135  emit(ServiceModified(event.GetServiceReference()));
136 
137  // Change service; add a new entry if service wasn't on list before
138  if (!this->ChangeServiceOnList(event.GetServiceReference()))
139  {
140  this->AddServiceToList(event.GetServiceReference());
141  }
142  break;
144  emit(ServiceRegistered(event.GetServiceReference()));
146  break;
150  break;
154  break;
155  }
156 }
157 
159 
161 {
162  QListWidgetItem *newItem = new QListWidgetItem;
163 
164  newItem->setText(this->CreateCaptionForService(serviceRef));
165 
166  // Add new item to QListWidget
167  m_Controls->m_ServiceList->addItem(newItem);
168  m_Controls->m_ServiceList->sortItems();
169 
170  // Construct link and add to internal List for reference
172  link.service = serviceRef;
173  link.item = newItem;
174  m_ListContent.push_back(link);
175 
176  // Select first entry and emit corresponding signal if the list was
177  // empty before and this feature is enabled
178  if (m_AutomaticallySelectFirstEntry && m_Controls->m_ServiceList->selectedItems().isEmpty())
179  {
180  m_Controls->m_ServiceList->setCurrentIndex(m_Controls->m_ServiceList->indexAt(QPoint(0, 0)));
182  }
183 
184  return newItem;
185 }
186 
188 {
189  for (std::vector<QmitkServiceListWidget::ServiceListLink>::iterator it = m_ListContent.begin();
190  it != m_ListContent.end();
191  ++it)
192  {
193  if (serviceRef == it->service)
194  {
195  int row = m_Controls->m_ServiceList->row(it->item);
196  QListWidgetItem *oldItem = m_Controls->m_ServiceList->takeItem(row);
197  delete oldItem;
198  this->m_ListContent.erase(it);
199  return true;
200  }
201  }
202  return false;
203 }
204 
206 {
207  for (std::vector<QmitkServiceListWidget::ServiceListLink>::iterator it = m_ListContent.begin();
208  it != m_ListContent.end();
209  ++it)
210  {
211  if (serviceRef == it->service)
212  {
213  it->item->setText(this->CreateCaptionForService(serviceRef));
214  return true;
215  }
216  }
217  return false;
218 }
219 
221 {
222  for (std::vector<QmitkServiceListWidget::ServiceListLink>::iterator it = m_ListContent.begin();
223  it != m_ListContent.end();
224  ++it)
225  if (item == it->item)
226  return it->service;
227  // Return invalid ServiceReference (will evaluate to false in bool expressions)
228  return us::ServiceReferenceU();
229 }
230 
231 std::vector<us::ServiceReferenceU> QmitkServiceListWidget::GetAllRegisteredServices()
232 {
233  // Get Service References
234  return m_Context->GetServiceReferences(m_Interface, m_Filter);
235 }
236 
238 {
239  std::string caption;
240  // TODO allow more complex formatting
241  if (m_NamingProperty.empty())
242  caption = m_Interface;
243  else
244  {
245  us::Any prop = serviceRef.GetProperty(m_NamingProperty);
246  if (prop.Empty())
247  {
248  MITK_WARN << "QmitkServiceListWidget tried to resolve property '" + m_NamingProperty +
249  "' but failed. Resorting to interface name for display.";
250  caption = m_Interface;
251  }
252  else
253  caption = prop.ToString();
254  }
255 
256  return QString::fromStdString(caption);
257 }
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
ServiceReferenceU GetServiceReference() const
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
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:23
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
void ServiceModified(us::ServiceReferenceU)
Emitted when a Service matching the filter changes it's properties, or when a service that formerly n...
std::vector< us::ServiceReferenceU > GetAllServiceReferences()
Definition: usAny.h:163
Type GetType() const
US_Core_EXPORT const std::string & OBJECTCLASS()
std::string ToString() const
Definition: usAny.h:257
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 ...
QmitkServiceListWidget(QWidget *p=0, Qt::WindowFlags f1=0)
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.
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's properties,.
QListWidgetItem * AddServiceToList(const us::ServiceReferenceU &serviceRef)
Constructs a ListItem from the given service, displays it, and locally stores the service...
bool Empty() const
Definition: usAny.h:246