17 #include <QTextStream>
30 #define CONFIG_TAG "configuration"
31 #define CONFIG_TRACKER_TAG "tracker"
32 #define CONFIG_TRACKER_TOOL_FILE "toolfile"
33 #define CONFIG_TRACKINGSYSTEMIMPLEMENTATION_TAG "trackingsystemimplementation"
36 #define TYPE_ATTRIBUTE "type"
37 #define CLINICAL_APP_ATTRIBUTE "clinical_app"
38 #define REFERENCE_ATTRIBUTE "reference"
39 #define OPENIGTLINK_TRANSFORM_ID_ATTRIBUTE "openigtlinktransformid"
40 #define OPENIGTLINK_IMAGE_ID_ATTRIBUTE "openigtlinkimageid"
41 #define OPENIGTLINK_APPLY_REF_TO_TOOLS_ATTRIBUTE "openigtlinkApplyRefToTools"
44 mConfigurationFilePath(absoluteConfigFilePath), mLoggingFolder(loggingFolder)
46 this->setConfigDocument(mConfigurationFilePath);
55 if (!this->isConfigFileValid())
58 QDomNode configNode = mConfigureDoc.elementsByTagName(
CONFIG_TAG).at(0);
68 for (
int i = 0; i < trackingsystemImplementationNodes.count(); ++i)
70 retval = trackingsystemImplementationNodes.at(i).toElement().attribute(
TYPE_ATTRIBUTE);
73 if (trackingsystemImplementationNodes.count() == 0)
78 else if(trackingsystemImplementationNodes.count() > 1)
80 CX_LOG_ERROR() <<
"ConfigurationFileParser::getTrackingSystemImplementation(): Config file: " << mConfigurationFilePath
81 <<
" has more the one tracking system implementation. Only one is currently supported.";
89 std::vector<ToolFileParser::TrackerInternalStructure> retval;
91 if (!this->isConfigFileValid())
95 for (
int i = 0; i < trackerNodes.count(); ++i)
98 QString trackerType = trackerNodes.at(i).toElement().attribute(
TYPE_ATTRIBUTE);
99 internalStructure.
mType = string2enum<TRACKING_SYSTEM>(trackerType);
102 retval.push_back(internalStructure);
105 if (retval.size() > 1)
107 "Config file " + mConfigurationFilePath
108 +
" has a invalid number of tracking systems, we only support 1 tracking system atm!");
115 std::vector<QString> retval;
117 if (!this->isConfigFileValid())
121 for (
int i = 0; i < toolFileNodes.count(); ++i)
123 QString absoluteToolFilePath = this->getAbsoluteToolFilePath(toolFileNodes.at(i).toElement());
124 if (absoluteToolFilePath.isEmpty())
127 retval.push_back(absoluteToolFilePath);
137 if (!this->isConfigFileValid())
145 for (
int i = 0; i < toolFileNodes.count(); ++i)
148 if (reference.contains(
"yes", Qt::CaseInsensitive))
151 retval = this->getAbsoluteToolFilePath(toolFileNodes.at(i).toElement());
159 std::vector<ToolStructure> retval;
161 if (!this->isConfigFileValid())
166 for (
int i = 0; i < toolFileNodes.count(); ++i)
169 toolStructure.
mAbsoluteToolFilePath = this->getAbsoluteToolFilePath(toolFileNodes.at(i).toElement());
175 if (reference.contains(
"yes", Qt::CaseInsensitive))
179 retval.push_back(toolStructure);
193 if (!this->isConfigFileValid())
197 for (
int i = 0; i < trackingsystemImplementationNodes.count(); ++i)
200 if (applyRef.contains(
"yes", Qt::CaseInsensitive))
205 if(trackingsystemImplementationNodes.count() > 1)
207 CX_LOG_ERROR() <<
"ConfigurationFileParser::getApplyRefToTools(): Config file: " << mConfigurationFilePath
208 <<
" has more the one tracking system implementation. Only one is currently supported.";
213 QString ConfigurationFileParser::convertToRelativeToolFilePath(QString configFilename, QString absoluteToolFilePath)
215 foreach (QString root,
profile()->getAllRootConfigPaths())
217 QString configPath = getToolPathFromRoot(root);
218 if (!absoluteToolFilePath.contains(configPath))
220 absoluteToolFilePath.replace(configPath,
"");
221 if (absoluteToolFilePath.startsWith(
"/"))
222 absoluteToolFilePath.remove(0, 1);
223 return absoluteToolFilePath;
227 return absoluteToolFilePath;
230 QString ConfigurationFileParser::getToolPathFromRoot(QString root)
232 return root +
"/tool/Tools/";
238 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"\"1.0\""));
240 QDomElement configNode = doc.createElement(
CONFIG_TAG);
249 configNode.appendChild(trackingsystemImplementationNode);
254 QString trackingSystemName =
enum2string(it1->first);
255 if(trackingSystemName.isEmpty())
258 trackingSystemName=
"";
263 ToolStructureVector::iterator it2 = it1->second.begin();
264 for (; it2 != it1->second.end(); ++it2)
266 QString absoluteToolFilePath = it2->mAbsoluteToolFilePath;
267 QString relativeToolFilePath = convertToRelativeToolFilePath(config.
mFileName, absoluteToolFilePath);
272 if (!trackingSystemName.contains(
enum2string(toolparser.
getTool()->mTrackerType), Qt::CaseInsensitive))
274 reportWarning(
"When saving configuration, skipping tool " + relativeToolFilePath +
" of type "
275 + toolTrackerType +
" because trackingSystemName is set to " + trackingSystemName);
280 toolFileNode.appendChild(doc.createTextNode(relativeToolFilePath));
281 createToolFileNode(it2, toolFileNode, toolparser);
282 trackerTagNode.appendChild(toolFileNode);
284 trackingsystemImplementationNode.appendChild(trackerTagNode);
287 doc.appendChild(configNode);
291 QDir().mkpath(QFileInfo(config.
mFileName).absolutePath());
293 if (!file.open(QIODevice::WriteOnly))
295 reportWarning(
"Could not open file " + file.fileName() +
", aborting writing of config.");
299 QTextStream stream(&file);
301 reportSuccess(
"Configuration file " + file.fileName() +
" is written.");
304 void ConfigurationFileParser::createToolFileNode(ToolStructureVector::iterator iter, QDomElement &toolFileNode,
ToolFileParser &toolparser)
308 QString transformId = iter->mOpenIGTLinkTransformId;
309 QString imageId = iter->mOpenIGTLinkImageId;
311 if(transformId.isEmpty())
312 transformId = toolparser.
getTool()->mOpenigtlinkTransformId;
313 if(imageId.isEmpty())
314 imageId = toolparser.
getTool()->mOpenigtlinkImageId;
315 if(!transformId.isEmpty())
317 if(!imageId.isEmpty())
321 void ConfigurationFileParser::setConfigDocument(QString configAbsoluteFilePath)
323 QFile configFile(configAbsoluteFilePath);
324 if (!configFile.exists())
330 QString errorMessage;
332 if (!mConfigureDoc.setContent(&configFile, &errorMessage, &errorInLine))
334 reportError(
"Could not set the xml content of the config file " + configAbsoluteFilePath);
335 CX_LOG_ERROR() <<
"Qt error message: " << errorMessage <<
" in line: " << errorInLine;
340 bool ConfigurationFileParser::isConfigFileValid()
343 QDomNode configNode = mConfigureDoc.elementsByTagName(
CONFIG_TAG).item(0);
344 if (configNode.isNull())
352 QString ConfigurationFileParser::findXmlFileWithDirNameInPath(QString path)
356 filter << dir.dirName() +
".xml";
357 QStringList filepaths = dir.entryList(filter);
358 if (!filepaths.isEmpty())
359 return dir.absoluteFilePath(filter[0]);
363 QString ConfigurationFileParser::searchForExistingToolFilePath(QString relativeToolFilePath)
366 relativeToolFilePath.replace(
"../Tools/",
"");
368 foreach (QString root,
profile()->getAllRootConfigPaths())
370 QString configPath = this->getToolPathFromRoot(root);
371 QFileInfo guess(configPath +
"/" + relativeToolFilePath);
373 return guess.canonicalFilePath();
378 QString ConfigurationFileParser::getAbsoluteToolFilePath(QDomElement toolfileelement)
380 QString relativeToolFilePath = toolfileelement.text();
381 if (relativeToolFilePath.isEmpty())
384 QString absoluteToolFilePath = this->searchForExistingToolFilePath(relativeToolFilePath);
386 QFileInfo info(absoluteToolFilePath);
388 reportError(QString(
"Tool file %1 in configuration %2 not found. Skipping.")
389 .arg(relativeToolFilePath)
390 .arg(mConfigurationFilePath));
393 absoluteToolFilePath = this->findXmlFileWithDirNameInPath(absoluteToolFilePath);
394 return absoluteToolFilePath;