diff --git a/Modules/USUI/Qmitk/QmitkUSDeviceManagerWidget.cpp b/Modules/USUI/Qmitk/QmitkUSDeviceManagerWidget.cpp index 64fbc1df1a..7cd4cc9daf 100644 --- a/Modules/USUI/Qmitk/QmitkUSDeviceManagerWidget.cpp +++ b/Modules/USUI/Qmitk/QmitkUSDeviceManagerWidget.cpp @@ -1,174 +1,184 @@ /*=================================================================== 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 #include //QT headers #include //mitk headers //itk headers //microservices #include #include #include const std::string QmitkUSDeviceManagerWidget::VIEW_ID = "org.mitk.views.QmitkUSDeviceManagerWidget"; QmitkUSDeviceManagerWidget::QmitkUSDeviceManagerWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f) { m_Controls = NULL; CreateQtPartControl(this); // get ModuleContext mitk::Module* mitkUS = mitk::ModuleRegistry::GetModule("MitkUS"); m_MitkUSContext = mitkUS->GetModuleContext(); //ServiceTracker* tracker = new ServiceTracker(m_MitkUSContext, this); // Register this Widget as a listener for Registry changes. // If devices are registered, unregistered or changed, notifications will go there std::string filter = "("; filter += mitk::ServiceConstants::OBJECTCLASS(); filter += "="; //filter += us_service_interface_iid(); filter += "org.mitk.services.UltrasoundDevice)"; m_MitkUSContext->AddServiceListener(this, &QmitkUSDeviceManagerWidget::OnServiceEvent ,filter); } QmitkUSDeviceManagerWidget::~QmitkUSDeviceManagerWidget() { } //////////////////// INITIALIZATION ///////////////////// void QmitkUSDeviceManagerWidget::CreateQtPartControl(QWidget *parent) { if (!m_Controls) { // create GUI widgets m_Controls = new Ui::QmitkUSDeviceManagerWidgetControls; m_Controls->setupUi(parent); this->CreateConnections(); } } void QmitkUSDeviceManagerWidget::CreateConnections() { if ( m_Controls ) { connect( m_Controls->m_BtnActivate, SIGNAL(clicked()), this, SLOT(OnClickedActivateDevice()) ); connect( m_Controls->m_BtnDisconnect, SIGNAL(clicked()), this, SLOT(OnClickedDisconnectDevice()) ); + connect( m_Controls->m_ConnectedDevices, SIGNAL(currentItemChanged( QListWidgetItem *, QListWidgetItem *)), this, SLOT(OnDeviceSelectionChanged()) ); } } ///////////// Methods & Slots Handling Direct Interaction ///////////////// void QmitkUSDeviceManagerWidget::OnClickedActivateDevice(){ MITK_INFO << "Activated Device"; mitk::USDevice::Pointer device = this->GetDeviceForListItem(this->m_Controls->m_ConnectedDevices->currentItem()); - device->Activate(); + if (device.IsNull()) return; + if (device->GetIsActive()) device->Deactivate(); + else device->Activate(); } void QmitkUSDeviceManagerWidget::OnClickedDisconnectDevice(){ MITK_INFO << "Disconnected Device"; mitk::USDevice::Pointer device = this->GetDeviceForListItem(this->m_Controls->m_ConnectedDevices->currentItem()); + if (device.IsNull()) return; device->Disconnect(); } +void QmitkUSDeviceManagerWidget::OnDeviceSelectionChanged(){ + mitk::USDevice::Pointer device = this->GetDeviceForListItem(this->m_Controls->m_ConnectedDevices->currentItem()); + if (device.IsNull()) return; + if (device->GetIsActive()) m_Controls->m_BtnActivate->setText("Deactivate"); + else m_Controls->m_BtnActivate->setText("Activate"); +} ///////////////// Methods & Slots Handling Logic ////////////////////////// void QmitkUSDeviceManagerWidget::OnServiceEvent(const mitk::ServiceEvent event){ // Empty ListWidget this->m_ListContent.clear(); m_Controls->m_ConnectedDevices->clear(); // get Active Devices std::vector devices = this->GetAllRegisteredDevices(); // Transfer them to the List for(std::vector::iterator it = devices.begin(); it != devices.end(); ++it) { QListWidgetItem *newItem = ConstructItemFromDevice(it->GetPointer()); //Add new item to QListWidget m_Controls->m_ConnectedDevices->addItem(newItem); // Construct Link and add to internal List for reference QmitkUSDeviceManagerWidget::DeviceListLink link; link.device = it->GetPointer(); link.item = newItem; m_ListContent.push_back(link); } } /////////////////////// HOUSEHOLDING CODE ///////////////////////////////// QListWidgetItem* QmitkUSDeviceManagerWidget::ConstructItemFromDevice(mitk::USDevice::Pointer device){ QListWidgetItem *result = new QListWidgetItem; std::string text = device->GetDeviceManufacturer() + "|" + device->GetDeviceModel(); if (device->GetIsActive()) { result->foreground().setColor(Qt::blue); text += "|(ON)"; } else text += "|(OFF)"; result->setText(text.c_str()); return result; } mitk::USDevice::Pointer QmitkUSDeviceManagerWidget::GetDeviceForListItem(QListWidgetItem* item) { for(std::vector::iterator it = m_ListContent.begin(); it != m_ListContent.end(); ++it) { if (item == it->item) return it->device; } return 0; } //mitk::ServiceTracker QmitkUSDeviceManagerWidget::ConstructServiceTracker(){ //return 0; //} std::vector QmitkUSDeviceManagerWidget::GetAllRegisteredDevices(){ //Get Service References std::list serviceRefs = m_MitkUSContext->GetServiceReferences(); // Convert Service References to US Devices std::vector* result = new std::vector; std::list::const_iterator iterator; for (iterator = serviceRefs.begin(); iterator != serviceRefs.end(); ++iterator) { mitk::USDevice::Pointer device = m_MitkUSContext->GetService(*iterator); if (device) result->push_back(device); } return *result; } \ No newline at end of file diff --git a/Modules/USUI/Qmitk/QmitkUSDeviceManagerWidget.h b/Modules/USUI/Qmitk/QmitkUSDeviceManagerWidget.h index 03dc726819..392c20d096 100644 --- a/Modules/USUI/Qmitk/QmitkUSDeviceManagerWidget.h +++ b/Modules/USUI/Qmitk/QmitkUSDeviceManagerWidget.h @@ -1,137 +1,142 @@ /*=================================================================== 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 _QmitkUSDeviceManagerWidget_H_INCLUDED #define _QmitkUSDeviceManagerWidget_H_INCLUDED #include "mitkUSUIExports.h" #include "ui_QmitkUSDeviceManagerWidgetControls.h" #include "mitkUSDevice.h" #include //QT headers #include #include //mitk header //Microservices #include "usServiceReference.h" #include "usModuleContext.h" #include "usServiceEvent.h" /** * @brief TODO * * @ingroup USUI */ class MitkUSUI_EXPORT QmitkUSDeviceManagerWidget :public QWidget //, public mitk::ServiceTrackerCustomizer<> // this extension is necessary if one wants to use ServiceTracking instead of filtering { //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; QmitkUSDeviceManagerWidget(QWidget* p = 0, Qt::WindowFlags f1 = 0); virtual ~QmitkUSDeviceManagerWidget(); /* @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 This Function listens to ServiceRegistry changes and updates the * list of devices accordingly. */ void OnServiceEvent(const mitk::ServiceEvent event); signals: /* \brief Sent, when the user clicks "Activate Device" */ void USDeviceActivated(); public slots: protected slots: /* \brief Called, when the button "Activate Device" was clicked */ void OnClickedActivateDevice(); /* \brief Called, when the button "Disconnect Device" was clicked */ void OnClickedDisconnectDevice(); + /* + \brief Called, when the selection in the devicelist changes + */ + void OnDeviceSelectionChanged(); + protected: Ui::QmitkUSDeviceManagerWidgetControls* m_Controls; ///< member holding the UI elements of this widget /* * \brief Internal Structure used to link devices to their QListWidget Items */ struct DeviceListLink { mitk::USDevice::Pointer device; QListWidgetItem* item; }; /* * \brief Contains a list of currently active devices and their entires in the list. This is wiped with every ServiceRegistryEvent. */ std::vector m_ListContent; /* * \brief Constructs a ListItem from the given device for display in the list of active devices. */ QListWidgetItem* ConstructItemFromDevice(mitk::USDevice::Pointer device); /* * \brief Returns the device corresponding to the given ListEntry or null if none was found (which really shouldnt happen). */ mitk::USDevice::Pointer GetDeviceForListItem(QListWidgetItem* item); //mitk::ServiceTracker ConstructServiceTracker(); /* * \brief Returns a List of US Devices that are currently connected by querying the service registry. */ std::vector GetAllRegisteredDevices(); private: mitk::ModuleContext* m_MitkUSContext; }; #endif // _QmitkUSDeviceManagerWidget_H_INCLUDED