diff --git a/Modules/Qmitk/QmitkServiceListWidget.cpp b/Modules/Qmitk/QmitkServiceListWidget.cpp index bd495e5f28..c06fd40b41 100644 --- a/Modules/Qmitk/QmitkServiceListWidget.cpp +++ b/Modules/Qmitk/QmitkServiceListWidget.cpp @@ -1,164 +1,179 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ //#define _USE_MATH_DEFINES #include // STL HEaders #include //QT headers #include //microservices #include #include "mitkModuleContext.h" #include const std::string QmitkServiceListWidget::VIEW_ID = "org.mitk.views.QmitkServiceListWidget"; QmitkServiceListWidget::QmitkServiceListWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f) { m_Controls = NULL; CreateQtPartControl(this); } QmitkServiceListWidget::~QmitkServiceListWidget() { } //////////////////// INITIALIZATION ///////////////////// void QmitkServiceListWidget::CreateQtPartControl(QWidget *parent) { if (!m_Controls) { // create GUI widgets m_Controls = new Ui::QmitkServiceListWidgetControls; m_Controls->setupUi(parent); this->CreateConnections(); } } void QmitkServiceListWidget::CreateConnections() { if ( m_Controls ) { connect( m_Controls->m_ServiceList, SIGNAL(currentItemChanged( QListWidgetItem *, QListWidgetItem *)), this, SLOT(OnDeviceSelectionChanged()) ); } } void QmitkServiceListWidget::Initialize(std::string interfaceName, std::string namingProperty, std::string filter) { m_Context = mitk::GetModuleContext(); - m_Filter = filter; + if (filter.empty()) + m_Filter = "(" + mitk::ServiceConstants::OBJECTCLASS() + "=" + interfaceName + ")"; + else + m_Filter = filter; m_Interface = interfaceName; m_NamingProperty = namingProperty; + m_Context->RemoveServiceListener(this, &QmitkServiceListWidget::OnServiceEvent); m_Context->AddServiceListener(this, &QmitkServiceListWidget::OnServiceEvent, m_Filter); + // Empty ListWidget + this->m_ListContent.clear(); + m_Controls->m_ServiceList->clear(); + + // get Services + std::list services = this->GetAllRegisteredServices(); + // Transfer them to the List + for(std::list::iterator it = services.begin(); it != services.end(); ++it) + AddServiceToList(& *it); } ///////////////////////// Getter & Setter ///////////////////////////////// template T QmitkServiceListWidget::GetSelectedService() { return this->GetDeviceForListItem(this->m_Controls->m_ServiceList->currentItem()); } ///////////// Methods & Slots Handling Direct Interaction ///////////////// - void QmitkServiceListWidget::OnServiceSelectionChanged(){ mitk::ServiceReference* ref = this->GetServiceForListItem(this->m_Controls->m_ServiceList->currentItem()); if (ref == 0) return; emit (ServiceSelected(ref)); } ///////////////// Methods & Slots Handling Logic ////////////////////////// void QmitkServiceListWidget::OnServiceEvent(const mitk::ServiceEvent event){ - // Empty ListWidget - this->m_ListContent.clear(); - m_Controls->m_ServiceList->clear(); - // get Services - std::list services = this->GetAllRegisteredServices(); - // Transfer them to the List - for(std::list::iterator it = services.begin(); it != services.end(); ++it) - { - QListWidgetItem *newItem = ConstructItemFromService(& *it); - //Add new item to QListWidget - m_Controls->m_ServiceList->addItem(newItem); - // Construct link and add to internal List for reference - QmitkServiceListWidget::ServiceListLink link; - link.service = &*it; - link.item = newItem; - m_ListContent.push_back(link); - } switch (event.GetType()) { case event.MODIFIED: emit(ServiceModified(& event.GetServiceReference())); + RemoveServiceFromList(& event.GetServiceReference()); + AddServiceToList(& event.GetServiceReference()); break; case event.REGISTERED: emit(ServiceRegistered(& event.GetServiceReference())); + AddServiceToList(& event.GetServiceReference()); break; case event.UNREGISTERING: emit(ServiceUnregistering(& event.GetServiceReference())); + RemoveServiceFromList(& event.GetServiceReference()); break; //default: // mitkThrow() << "ServiceListenerWidget recieved an unrecognized event. Please Update Implementation of QmitkServiceListWidget::OnServiceEvent()"; -} + } } /////////////////////// HOUSEHOLDING CODE ///////////////////////////////// -QListWidgetItem* QmitkServiceListWidget::ConstructItemFromService(mitk::ServiceReference* serviceRef){ - QListWidgetItem *result = new QListWidgetItem; +QListWidgetItem* QmitkServiceListWidget::AddServiceToList(mitk::ServiceReference* serviceRef){ + QListWidgetItem *newItem = new QListWidgetItem; std::string caption; //TODO allow more complex formatting if (m_NamingProperty.empty()) caption = m_Interface; else caption = serviceRef->GetProperty(m_NamingProperty).ToString(); - result->setText(caption.c_str()); + newItem->setText(caption.c_str()); + + //Add new item to QListWidget + m_Controls->m_ServiceList->addItem(newItem); + // Construct link and add to internal List for reference + QmitkServiceListWidget::ServiceListLink link; + link.service = serviceRef; + link.item = newItem; + m_ListContent.push_back(link); + return newItem; +} - return result; +bool QmitkServiceListWidget::RemoveServiceFromList(mitk::ServiceReference* serviceRef){ + for(std::vector::iterator it = m_ListContent.begin(); it != m_ListContent.end(); ++it) + if (serviceRef == it->service) + { + m_Controls->m_ServiceList->removeItemWidget(it->item); + this->m_ListContent.erase(it); + return true; + } + return false; } mitk::ServiceReference* QmitkServiceListWidget::GetServiceForListItem(QListWidgetItem* item) { for(std::vector::iterator it = m_ListContent.begin(); it != m_ListContent.end(); ++it) - { if (item == it->item) return it->service; - } return 0; } std::list QmitkServiceListWidget::GetAllRegisteredServices(){ //Get Service References return m_Context->GetServiceReferences(m_Interface, m_Filter); } diff --git a/Modules/Qmitk/QmitkServiceListWidget.h b/Modules/Qmitk/QmitkServiceListWidget.h index c977d306b2..66a550b1b1 100644 --- a/Modules/Qmitk/QmitkServiceListWidget.h +++ b/Modules/Qmitk/QmitkServiceListWidget.h @@ -1,167 +1,173 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef _QmitkServiceListWidget_H_INCLUDED #define _QmitkServiceListWidget_H_INCLUDED #include "QmitkExports.h" #include "ui_QmitkServiceListWidgetControls.h" #include //QT headers #include #include //Microservices #include "usServiceReference.h" #include "usModuleContext.h" #include "usServiceEvent.h" /** * @brief This widget provides abstraction for MicroServices. Place one in your Plugin and set it to a certain interface. * One can also specify a filter and / or a property to use for captioning of the services. It also offers functionality to be * informed of ServiceEvents and to return the sctual classes, so only a minimum of interaction with the MicroserviceInterface is required. * * @ingroup QMITK */ class QMITK_EXPORT QmitkServiceListWidget :public QWidget { //this is needed for all Qt objects that should have a MOC object (everything that derives from QObject) Q_OBJECT public: static const std::string VIEW_ID; QmitkServiceListWidget(QWidget* p = 0, Qt::WindowFlags f1 = 0); virtual ~QmitkServiceListWidget(); /* @brief This method is part of the widget an needs not to be called seperately. */ virtual void CreateQtPartControl(QWidget *parent); /* @brief This method is part of the widget an needs not to be called seperately. (Creation of the connections of main and control widget.)*/ virtual void CreateConnections(); /* * \brief Initializes the connection to the registry. The string filter is an LDAP parsable String, compare mitk::ModuleContext for examples on filtering. * interfaceName is the name of the interface that is defined in the classes header file and that is used register it it with the MicroServices. NamingProperty * is a property that will be used to caption the Items in the list. If no filter is supplied, all matching interfaces are shown. If no namingProperty is supplied, * the interfaceName will be used to caption Items in the list. */ void Initialize(std::string interfaceName, std::string namingProperty, std::string filter); /* * \brief Use this function to returns the currently selected service as a class directly. * Make sure you pass the appropriate type, or else this call will fail. */ template T GetSelectedService(); /* * \brief Returns the currently selected Service as a ServiceReference. */ mitk::ServiceReference* GetSelectedService(); /* *\brief This Function listens to ServiceRegistry changes and updates the * list of services accordingly. The user of this widget does not need to call this method, * it is instead used to recieve events from the module registry. */ void OnServiceEvent(const mitk::ServiceEvent event); signals: /* *\brief Emitted when a new Service mathing the filter is being registered. */ void ServiceRegistered(mitk::ServiceReference*); /* *\brief Emitted directly before a Service matching the filter is being unregistered. */ void ServiceUnregistering(mitk::ServiceReference*); /* *\brief Emitted when a Service matching the filter changes it's properties. */ void ServiceModified(mitk::ServiceReference*); /* *\brief Emitted the user selects a Service from the list */ void ServiceSelected(mitk::ServiceReference*); public slots: protected slots: /* \brief Called, when the selection in the list of Services changes */ void OnServiceSelectionChanged(); protected: Ui::QmitkServiceListWidgetControls* m_Controls; ///< member holding the UI elements of this widget /* * \brief Internal structure used to link ServiceReferences to their QListWidgetItems */ struct ServiceListLink { mitk::ServiceReference* service; QListWidgetItem* item; }; /* - * \brief Contains a list of currently active services and their entires in the list. This is wiped with every ServiceRegistryEvent. + * \brief Contains a list of currently active services and their entires in the list. This is wiped with every ServiceRegistryEvent. */ std::vector m_ListContent; /* - * \brief Constructs a ListItem from the given service for display in the list of services. + * \brief Constructs a ListItem from the given service, displays it, and locally stores the service. */ - QListWidgetItem* ConstructItemFromService(mitk::ServiceReference* serviceRef); + QListWidgetItem* AddServiceToList(mitk::ServiceReference* serviceRef); + + + /* + * \brief Removes the given service from the list and cleans up. Returns true if successful, false if service was not found. + */ + bool RemoveServiceFromList(mitk::ServiceReference* serviceRef); /* * \brief Returns the serviceReference corresponding to the given ListEntry or null if none was found (which really shouldn't happen). */ mitk::ServiceReference* GetServiceForListItem(QListWidgetItem* item); /* * \brief Returns a list of ServiceReferences matching the filter criteria by querying the service registry. */ std::list GetAllRegisteredServices(); private: mitk::ModuleContext* m_Context; /** \brief a filter to further narrow down the list of results **/ std::string m_Filter; /** \brief The name of the ServiceInterface that this class should list **/ std::string m_Interface; /** \brief The name of the ServiceProperty that will be displayed in the List to represent the service **/ std::string m_NamingProperty; }; #endif // _QmitkServiceListWidget_H_INCLUDED