MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkUSNavigationCombinedModalityPersistence.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
15
16// Microservices
17#include <usServiceReference.h>
18#include <usModuleContext.h>
19#include <usGetModuleContext.h>
20#include <usModuleContext.h>
21
22// Qt
23#include <QSettings>
24#include <QVector>
25#include <QHash>
26#include <QStringList>
27
29
31{
32 us::ModuleContext* context = us::GetModuleContext();
33
34 this->LoadStoredDevices();
35
36 // to be notified about every register and unregister of an USDevice
37 std::string filterExcludeCombinedModalities = "(&(" + us::ServiceConstants::OBJECTCLASS() + "="
38 + "org.mitk.services.UltrasoundDevice)(!(" + mitk::USDevice::GetPropertyKeys().US_PROPKEY_CLASS + "="
40 context->AddServiceListener(this, &mitk::USNavigationCombinedModalityPersistence::OnServiceEvent,
41 filterExcludeCombinedModalities);
42
43 // to be notified about every register and unregister of an NavigationDataSource
44 context->AddServiceListener(this, &mitk::USNavigationCombinedModalityPersistence::OnServiceEvent,
45 "(" + us::ServiceConstants::OBJECTCLASS() + "=" + us_service_interface_iid<mitk::NavigationDataSource>() + ")");
46}
47
49{
50 //this->StoreCurrentDevices(); //disabled because persistence is buggy
51}
52
53void mitk::USNavigationCombinedModalityPersistence::OnServiceEvent(const us::ServiceEvent event)
54{
55 if ( event.GetType() == event.REGISTERED )
56 {
57 //this->LoadStoredDevices(); //disabled because persistence is buggy
58 }
59}
60
61void mitk::USNavigationCombinedModalityPersistence::StoreCurrentDevices()
62{
63 QList<QVariant> devices;
64
65 us::ModuleContext* context = us::GetModuleContext();
66
67 // get all combined modality from the service registry
68 std::string filterOnlyCombinedModalities = "(&(" + us::ServiceConstants::OBJECTCLASS() + "=" + "org.mitk.services.UltrasoundDevice)(" + mitk::USDevice::GetPropertyKeys().US_PROPKEY_CLASS + "=" + mitk::USCombinedModality::DeviceClassIdentifier + "))";
69 std::vector<us::ServiceReference<USDevice> > services = context->GetServiceReferences<USDevice>(filterOnlyCombinedModalities);
70
71 for ( std::vector<us::ServiceReference<USDevice> >::iterator it = services.begin();
72 it != services.end(); ++it )
73 {
74 QStringList deviceStrings;
75 mitk::USCombinedModality::Pointer currentDevice = dynamic_cast<mitk::USCombinedModality*>(context->GetService(*it));
76 if ( currentDevice.IsNotNull() )
77 {
78 // save manufacturer and model string of the combined modality
79 deviceStrings.push_back(QString::fromStdString(currentDevice->GetUltrasoundDevice()->GetManufacturer()));
80 deviceStrings.push_back(QString::fromStdString(currentDevice->GetUltrasoundDevice()->GetName()));
81
82 // save name of the navigation data source
83 mitk::NavigationDataSource::Pointer navigationDataSource = currentDevice->GetNavigationDataSource();
84 if ( currentDevice.IsNull() ) { continue; }
85 deviceStrings.push_back(QString::fromStdString(navigationDataSource->GetName()));
86
87 // save manufacturer, model and comment of the ultrasound device
88 mitk::USDevice::Pointer ultrasoundDevice = currentDevice->GetUltrasoundDevice();
89 if ( ultrasoundDevice.IsNull() ) { continue; }
90 deviceStrings.push_back(QString::fromStdString(ultrasoundDevice->GetManufacturer()));
91 deviceStrings.push_back(QString::fromStdString(ultrasoundDevice->GetName()));
92 deviceStrings.push_back(QString::fromStdString(ultrasoundDevice->GetComment()));
93
94 // save calibration of the combined modality
95 deviceStrings.push_back(QString::fromStdString(currentDevice->SerializeCalibration()));
96 }
97
98 devices.push_back(deviceStrings);
99 }
100
101 // store everything in QSettings
102 QSettings settings;
103 settings.setValue("combined-modalities", devices);
104}
105
106void mitk::USNavigationCombinedModalityPersistence::LoadStoredDevices()
107{
108 // load everything from QSettings
109 QSettings settings;
110 QList<QVariant> devices = settings.value("combined-modalities").value< QList<QVariant> >();
111
112 for ( QList<QVariant>::iterator it = devices.begin();
113 it != devices.end(); ++it )
114 {
115 // get the saved strings for the combined modality (there must be at least
116 // the seven strings, which where saved before)
117 QStringList stringList = it->toStringList();
118
119 // test if the combined modality wasn't restored before
120 if (stringList.size() >= 7 && this->GetCombinedModality(stringList.at(0).toStdString(), stringList.at(1).toStdString()).IsNull())
121 {
122 // try to get matching navigation data source and ultrasound device
123 mitk::NavigationDataSource::Pointer navigationDataSource = this->GetNavigationDataSource(stringList.at(2).toStdString());
124 mitk::USDevice::Pointer usDevice = this->GetUSDevice(stringList.at(3).toStdString(), stringList.at(4).toStdString(), stringList.at(5).toStdString());
125
126 // create the combined modality if matching navigation data source and
127 // ultrasound device werde found
128 if ( navigationDataSource.IsNotNull() && usDevice.IsNotNull() )
129 {
130 mitk::USCombinedModality::Pointer combinedModality = mitk::USCombinedModality::New(usDevice, navigationDataSource, false);
131 combinedModality->DeserializeCalibration(stringList.at(6).toStdString());
132 combinedModality->GetUltrasoundDevice()->Initialize();
133 }
134 }
135 }
136}
137
138mitk::USCombinedModality::Pointer mitk::USNavigationCombinedModalityPersistence::GetCombinedModality(std::string manufacturer, std::string model)
139{
140 us::ModuleContext* context = us::GetModuleContext();
141
142 std::string filterOnlyCombinedModalities = "(&(" + us::ServiceConstants::OBJECTCLASS() + "="
143 + "org.mitk.services.UltrasoundDevice)(" + mitk::USDevice::GetPropertyKeys().US_PROPKEY_CLASS + "="
145 std::vector<us::ServiceReference<USDevice> > services = context->GetServiceReferences<USDevice>(filterOnlyCombinedModalities);
146
147 for ( std::vector<us::ServiceReference<USDevice> >::iterator it = services.begin();
148 it != services.end(); ++it )
149 {
150 mitk::USCombinedModality::Pointer currentDevice = dynamic_cast<mitk::USCombinedModality*>(context->GetService(*it));
151 if ( currentDevice.IsNotNull() )
152 {
153 if ( currentDevice->GetUltrasoundDevice()->GetManufacturer() == manufacturer && currentDevice->GetUltrasoundDevice()->GetName() == model )
154 {
155 return currentDevice;
156 }
157 }
158 }
159
160 return nullptr;
161}
162
163mitk::USDevice::Pointer mitk::USNavigationCombinedModalityPersistence::GetUSDevice(std::string manufacturer, std::string model, std::string comment)
164{
165 us::ModuleContext* context = us::GetModuleContext();
166
167 std::string filterExcludeCombinedModalities = "(&(" + us::ServiceConstants::OBJECTCLASS() + "="
168 + "org.mitk.services.UltrasoundDevice)(!(" + mitk::USDevice::GetPropertyKeys().US_PROPKEY_CLASS + "="
170 std::vector<us::ServiceReference<USDevice> > services = context->GetServiceReferences<USDevice>(filterExcludeCombinedModalities);
171
172 for ( std::vector<us::ServiceReference<USDevice> >::iterator it = services.begin();
173 it != services.end(); ++it )
174 {
175 mitk::USDevice::Pointer currentDevice = dynamic_cast<mitk::USDevice*>(context->GetService(*it));
176 if ( currentDevice.IsNotNull() )
177 {
178 if ( currentDevice->GetManufacturer() == manufacturer && currentDevice->GetName() == model
179 && currentDevice->GetComment() == comment )
180 {
181 return currentDevice;
182 }
183 }
184 }
185
186 return nullptr;
187}
188
189mitk::NavigationDataSource::Pointer mitk::USNavigationCombinedModalityPersistence::GetNavigationDataSource(std::string name)
190{
191 us::ModuleContext* context = us::GetModuleContext();
192
193 std::vector<us::ServiceReference<mitk::NavigationDataSource> > services = context->GetServiceReferences<mitk::NavigationDataSource>();
194
195 for ( std::vector<us::ServiceReference<mitk::NavigationDataSource> >::iterator it = services.begin();
196 it != services.end(); ++it )
197 {
198 mitk::NavigationDataSource::Pointer currentDevice = dynamic_cast<mitk::NavigationDataSource*>(context->GetService(*it));
199 if ( currentDevice.IsNotNull() )
200 {
201 if ( currentDevice->GetName() == name )
202 {
203 return currentDevice;
204 }
205 }
206 }
207
208 return nullptr;
209}
Combination of USDevice and NavigationDataSource. This class can be used as an ImageSource subclass....
A device holds information about it's model, make and the connected probes. It is the common super cl...
static mitk::USDevice::PropertyKeys GetPropertyKeys()
USNavigationCombinedModalityPersistence()
Restores combined modality from QSettings and registers on service events. Combined modalities are re...
~USNavigationCombinedModalityPersistence() override
The destructor stores combined modalities by calling mitk::USNavigationCombinedModalityPersistence::S...
const std::string US_PROPKEY_CLASS