diff --git a/Modules/US/USModel/mitkUSDevice.cpp b/Modules/US/USModel/mitkUSDevice.cpp index e17951b99d..ba06bf0b39 100644 --- a/Modules/US/USModel/mitkUSDevice.cpp +++ b/Modules/US/USModel/mitkUSDevice.cpp @@ -1,274 +1,274 @@ /*=================================================================== 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. ===================================================================*/ #include "mitkUSDevice.h" #include "mitkUSImageMetadata.h" //Microservices #include #include #include #include "mitkModuleContext.h" mitk::USDevice::USDevice(std::string manufacturer, std::string model, bool isVideoOnly) : mitk::ImageSource() { // Initialize Members m_Metadata = mitk::USImageMetadata::New(); m_Metadata->SetDeviceManufacturer(manufacturer); m_Metadata->SetDeviceModel(model); m_Metadata->SetDeviceIsVideoOnly(isVideoOnly); m_IsActive = false; //set number of outputs this->SetNumberOfOutputs(1); //create a new output mitk::USImage::Pointer newOutput = mitk::USImage::New(); this->SetNthOutput(0,newOutput); } mitk::USDevice::USDevice(mitk::USImageMetadata::Pointer metadata, bool isVideoOnly) : mitk::ImageSource() { m_Metadata = metadata; m_IsActive = false; //set number of outputs this->SetNumberOfOutputs(1); //create a new output mitk::USImage::Pointer newOutput = mitk::USImage::New(); this->SetNthOutput(0,newOutput); } mitk::USDevice::~USDevice() { } bool mitk::USDevice::Connect() { //TODO Throw Exception is already activated before connection // Prepare connection, fail if this fails. if (! this->OnConnection()) return false; // Get Context and Module mitk::ModuleContext* context = GetModuleContext(); // Define ServiceProps ServiceProperties props; props["DeviceClass"] = this->GetDeviceClass(); std::string no = "false"; props["IsActive"] = no; - props["Class"] = this->GetClassName(); m_ServiceRegistration = context->RegisterService(this, props); return true; } bool mitk::USDevice::OnConnection() { return true; // TODO: Make Abstract } bool mitk::USDevice::Disconnect() { // Prepare connection, fail if this fails. if (! this->OnDisconnection()) return false; // Unregister m_ServiceRegistration.Unregister(); m_ServiceRegistration = 0; return true; } bool mitk::USDevice::OnDisconnection() { return true; // TODO Make Abstract } bool mitk::USDevice::Activate() { if (! this->GetIsConnected()) return false; m_IsActive = OnActivation(); ServiceProperties props; props["DeviceClass"] = this->GetDeviceClass(); std::string yes = "true"; props["IsActive"] = yes; - props["Class"] = this->GetClassName(); this->m_ServiceRegistration.SetProperties(props); return m_IsActive; } + void mitk::USDevice::Deactivate() { m_IsActive= false; ServiceProperties props; props["DeviceClass"] = this->GetDeviceClass(); std::string no = "false"; props["IsActive"] = no; - props["Class"] = this->GetClassName(); this->m_ServiceRegistration.SetProperties(props); OnDeactivation(); } bool mitk::USDevice::OnActivation() { return true; // TODO Make Abstract } void mitk::USDevice::OnDeactivation() { // TODO Make Abstract } std::string mitk::USDevice::GetDeviceClass() { return "org.mitk.Ultrasound.GenericDevice"; } void mitk::USDevice::AddProbe(mitk::USProbe::Pointer probe) { for(int i = 0; i < m_ConnectedProbes.size(); i++) { if (m_ConnectedProbes[i]->IsEqualToProbe(probe)) return; } this->m_ConnectedProbes.push_back(probe); } + void mitk::USDevice::ActivateProbe(mitk::USProbe::Pointer probe){ // currently, we may just add the probe. This behaviour must be changed, should more complicated SDK applications emerge AddProbe(probe); int index = -1; for(int i = 0; i < m_ConnectedProbes.size(); i++) { if (m_ConnectedProbes[i]->IsEqualToProbe(probe)) index = i; } // index now contains the position of the original instance of this probe m_ActiveProbe = m_ConnectedProbes[index]; } + void mitk::USDevice::DeactivateProbe(){ m_ActiveProbe = 0; } void mitk::USDevice::GenerateData() { } mitk::USImage* mitk::USDevice::GetOutput() { if (this->GetNumberOfOutputs() < 1) return NULL; return static_cast(this->ProcessObject::GetOutput(0)); } mitk::USImage* mitk::USDevice::GetOutput(unsigned int idx) { if (this->GetNumberOfOutputs() < 1) return NULL; return static_cast(this->ProcessObject::GetOutput(idx)); } void mitk::USDevice::GraftOutput(itk::DataObject *graft) { this->GraftNthOutput(0, graft); } void mitk::USDevice::GraftNthOutput(unsigned int idx, itk::DataObject *graft) { if ( idx >= this->GetNumberOfOutputs() ) { itkExceptionMacro(<<"Requested to graft output " << idx << " but this filter only has " << this->GetNumberOfOutputs() << " Outputs."); } if ( !graft ) { itkExceptionMacro(<<"Requested to graft output with a NULL pointer object" ); } itk::DataObject* output = this->GetOutput(idx); if ( !output ) { itkExceptionMacro(<<"Requested to graft output that is a NULL pointer" ); } // Call Graft on USImage to copy member data output->Graft( graft ); } itk::ProcessObject::DataObjectPointer mitk::USDevice::MakeOutput( unsigned int /*idx */) { mitk::USImage::Pointer p = mitk::USImage::New(); return static_cast(p.GetPointer()); } //########### GETTER & SETTER ##################// bool mitk::USDevice::GetIsActive() { return m_IsActive; } bool mitk::USDevice::GetIsConnected() { // a device is connected if it is registered with the service return (m_ServiceRegistration != 0); } std::string mitk::USDevice::GetDeviceManufacturer(){ return this->m_Metadata->GetDeviceManufacturer(); } std::string mitk::USDevice::GetDeviceModel(){ return this->m_Metadata->GetDeviceModel(); } std::string mitk::USDevice::GetDeviceComment(){ return this->m_Metadata->GetDeviceComment(); } bool mitk::USDevice::GetIsVideoOnly(){ return this->m_Metadata->GetDeviceIsVideoOnly(); } std::vector mitk::USDevice::GetConnectedProbes() { return m_ConnectedProbes; } diff --git a/Modules/US/USModel/mitkUSDevice.h b/Modules/US/USModel/mitkUSDevice.h index dd6e206fb3..a7bac5170d 100644 --- a/Modules/US/USModel/mitkUSDevice.h +++ b/Modules/US/USModel/mitkUSDevice.h @@ -1,263 +1,255 @@ /*=================================================================== 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 MITKUSDevice_H_HEADER_INCLUDED_ #define MITKUSDevice_H_HEADER_INCLUDED_ #include #include "mitkUSProbe.h" #include "mitkUSImageMetadata.h" #include "mitkUSImage.h" #include #include #include #include // Microservices #include #include namespace mitk { /**Documentation * \brief A device holds information about it's model, make and the connected probes. It is the * common super class for all devices and acts as an image source for mitkUSImages. It is the base class * for all US Devices, and every new device should extend it. * \ingroup US */ class MitkUS_EXPORT USDevice : public mitk::ImageSource { public: mitkClassMacro(USDevice, mitk::ImageSource); /** * \brief Enforces minimal Metadata to be set. The isVideoOnly flag indicates that this class * only handles a videostream and does not receive Metadata from the physical device itself. */ mitkNewMacro3Param(Self, std::string, std::string, bool); /** * \brief Constructs a device with the given Metadata. Make sure the Metadata contains meaningful content! * The isVideoOnly flag indicates that this class * only handles a videostream and does not receive Metadata from the physical device itself. */ mitkNewMacro2Param(Self, mitk::USImageMetadata::Pointer, bool); - /** - * \brief Returns the qualified Name of this class. Be sure to override this when inheriting from Device! - */ - virtual std::string GetClassName(){ - return "org.mitk.modules.us.USDevice"; - } - - /** * \brief Connects this device. A connected device is ready to deliver images (i.e. be Activated). A Connected Device can be active. A disconnected Device cannot be active. * Internally calls onConnect and then registers the device with the service. A device usually should * override the OnConnection() method, but never the Connect() method, since this will possibly exclude the device * from normal service management. The exact flow of events is: * 0. Check if the device is already connected. If yes, return true anyway, but don't do anything. * 1. Call OnConnection() Here, a device should establish it's connection with the hardware Afterwards, it should be ready to start transmitting images at any time. * 2. If OnConnection() returns true ("successful"), then the device is registered with the service. * 3. if not, it the method itself returns false or may throw an expection, depeneding on the device implementation. * */ bool Connect(); /** * \brief Works analogously to mitk::USDevice::Connect(). Don't override this Method, but onDisconnection instead. */ bool Disconnect(); /** * \brief Activates this device. After the activation process, the device will start to produce images. This Method will fail, if the device is not connected. */ bool Activate(); /** * \brief Deactivates this device. After the deactivation process, the device will no longer produce images, but still be connected. */ void Deactivate(); /** * \brief Add a probe to the device without connecting to it. * This should usually be done before connecting to the probe. */ virtual void AddProbe(mitk::USProbe::Pointer probe); /** * \brief Connect to a probe and activate it. The probe should be added first. * Usually, a VideoDevice will simply add a probe it wants to connect to, * but an SDK Device might require adding a probe first. */ virtual void ActivateProbe(mitk::USProbe::Pointer probe); /** * \brief Deactivates the currently active probe. */ virtual void DeactivateProbe(); /** * \brief Removes a probe from the ist of currently added probes. */ //virtual void removeProbe(mitk::USProbe::Pointer probe); /** * \brief Returns a vector containing all connected probes. */ std::vector GetConnectedProbes(); /** *\brief return the output (output with id 0) of the filter */ USImage* GetOutput(void); /** *\brief return the output with id idx of the filter */ USImage* GetOutput(unsigned int idx); /** *\brief Graft the specified DataObject onto this ProcessObject's output. * * See itk::ImageSource::GraftNthOutput for details */ virtual void GraftNthOutput(unsigned int idx, itk::DataObject *graft); /** * \brief Graft the specified DataObject onto this ProcessObject's output. * * See itk::ImageSource::Graft Output for details */ virtual void GraftOutput(itk::DataObject *graft); /** * \brief Make a DataObject of the correct type to used as the specified output. * * This method is automatically called when DataObject::DisconnectPipeline() * is called. DataObject::DisconnectPipeline, disconnects a data object * from being an output of its current source. When the data object * is disconnected, the ProcessObject needs to construct a replacement * output data object so that the ProcessObject is in a valid state. * Subclasses of USImageVideoSource that have outputs of different * data types must overwrite this method so that proper output objects * are created. */ virtual DataObjectPointer MakeOutput(unsigned int idx); //########### GETTER & SETTER ##################// /** * \brief Returns the Class of the Device. This Method must be reimplemented by every Inheriting Class. */ std::string GetDeviceClass(); /** * \brief True, if the device is currently generating image data, false otherwise. */ bool GetIsActive(); /** * \brief True, if the device is currently ready to start transmitting image data or is already * transmitting image data. A disconnected device cannot be activated. */ bool GetIsConnected(); /** * \brief Returns the currently active probe or null, if none is active */ itkGetMacro(ActiveProbe, mitk::USProbe::Pointer); std::string GetDeviceManufacturer(); std::string GetDeviceModel(); std::string GetDeviceComment(); bool GetIsVideoOnly(); protected: mitk::USProbe::Pointer m_ActiveProbe; std::vector m_ConnectedProbes; bool m_IsActive; /** * \brief Is called during the connection process. Override this method in your subclass to handle the actual connection. * Return true if successful and false if unsuccessful. Additionally, you may throw an exception to clarify what went wrong. */ virtual bool OnConnection(); /** * \brief Is called during the disconnection process. Override this method in your subclass to handle the actual disconnection. * Return true if successful and false if unsuccessful. Additionally, you may throw an exception to clarify what went wrong. */ virtual bool OnDisconnection(); /** * \brief Is called during the activation process. After this method is finsihed, the device should be generating images */ virtual bool OnActivation(); /** * \brief Is called during the disactivation process. After a call to this method the device should still be connected, but not producing images anymore. */ virtual void OnDeactivation(); /** * \brief This metadata set is privately used to imprint USImages with Metadata later. * At instantiation time, it only contains Information about the Device, * At scan time, it integrates this data with the probe information and imprints it on * the produced images. This field is intentionally hidden from outside interference. */ mitk::USImageMetadata::Pointer m_Metadata; /** * \brief Enforces minimal Metadata to be set. The isVideoOnly flag indicates that this class * only handles a videostream and does not recieve Metadata from the physical device itself. */ USDevice(std::string manufacturer, std::string model, bool isVideoOnly); /** * \brief Constructs a device with the given Metadata. Make sure the Metadata contains meaningful content! * The isVideoOnly flag indicates that this class * only handles a videostream and does not receive Metadata from the physical device itself. */ USDevice(mitk::USImageMetadata::Pointer metadata, bool isVideoOnly); virtual ~USDevice(); /** * \brief Grabs the next frame from the Video input. This method is called internally, whenever Update() is invoked by an Output. */ void GenerateData(); private: mitk::ServiceRegistration m_ServiceRegistration; }; } // namespace mitk // This is the microservice declaration. Do not meddle! US_DECLARE_SERVICE_INTERFACE(mitk::USDevice, "org.mitk.services.UltrasoundDevice") #endif \ No newline at end of file diff --git a/Modules/USUI/Qmitk/QmitkUSDeviceListWidget.cpp b/Modules/USUI/Qmitk/QmitkUSDeviceListWidget.cpp index f180ff02d2..75acd872b2 100644 --- a/Modules/USUI/Qmitk/QmitkUSDeviceListWidget.cpp +++ b/Modules/USUI/Qmitk/QmitkUSDeviceListWidget.cpp @@ -1,170 +1,167 @@ /*=================================================================== 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 QmitkUSDeviceListWidget::VIEW_ID = "org.mitk.views.QmitkUSDeviceListWidget"; QmitkUSDeviceListWidget::QmitkUSDeviceListWidget(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(); } QmitkUSDeviceListWidget::~QmitkUSDeviceListWidget() { } //////////////////// INITIALIZATION ///////////////////// void QmitkUSDeviceListWidget::CreateQtPartControl(QWidget *parent) { if (!m_Controls) { // create GUI widgets m_Controls = new Ui::QmitkUSDeviceListWidgetControls; m_Controls->setupUi(parent); this->CreateConnections(); } } void QmitkUSDeviceListWidget::CreateConnections() { if ( m_Controls ) { connect( m_Controls->m_DeviceList, SIGNAL(currentItemChanged( QListWidgetItem *, QListWidgetItem *)), this, SLOT(OnDeviceSelectionChanged()) ); } } void QmitkUSDeviceListWidget::Initialize(std::string filter) { m_Filter = filter; m_MitkUSContext->AddServiceListener(this, &QmitkUSDeviceListWidget::OnServiceEvent, m_Filter); } + ///////////////////////// Getter & Setter ///////////////////////////////// mitk::USDevice::Pointer QmitkUSDeviceListWidget::GetSelectedDevice() { return this->GetDeviceForListItem(this->m_Controls->m_DeviceList->currentItem()); } ///////////// Methods & Slots Handling Direct Interaction ///////////////// void QmitkUSDeviceListWidget::OnDeviceSelectionChanged(){ mitk::USDevice::Pointer device = this->GetDeviceForListItem(this->m_Controls->m_DeviceList->currentItem()); if (device.IsNull()) return; emit (DeviceSelected(device)); } ///////////////// Methods & Slots Handling Logic ////////////////////////// void QmitkUSDeviceListWidget::OnServiceEvent(const mitk::ServiceEvent event){ // Empty ListWidget this->m_ListContent.clear(); m_Controls->m_DeviceList->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_DeviceList->addItem(newItem); // Construct link and add to internal List for reference QmitkUSDeviceListWidget::DeviceListLink link; link.device = it->GetPointer(); link.item = newItem; m_ListContent.push_back(link); } } /////////////////////// HOUSEHOLDING CODE ///////////////////////////////// QListWidgetItem* QmitkUSDeviceListWidget::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 QmitkUSDeviceListWidget::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 QmitkUSDeviceListWidget::ConstructServiceTracker(){ -//return 0; -//} - std::vector QmitkUSDeviceListWidget::GetAllRegisteredDevices(){ //Get Service References std::list serviceRefs = m_MitkUSContext->GetServiceReferences(m_Filter); // 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