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" 43 mConfigurationFilePath(absoluteConfigFilePath), mLoggingFolder(loggingFolder)
45 this->setConfigDocument(mConfigurationFilePath);
54 if (!this->isConfigFileValid())
57 QDomNode configNode = mConfigureDoc.elementsByTagName(
CONFIG_TAG).at(0);
67 for (
int i = 0; i < trackingsystemImplementationNodes.count(); ++i)
69 retval = trackingsystemImplementationNodes.at(i).toElement().attribute(
TYPE_ATTRIBUTE);
72 if (trackingsystemImplementationNodes.count() == 0)
76 else if(trackingsystemImplementationNodes.count() > 1)
78 CX_LOG_ERROR() <<
"ConfigurationFileParser::getTrackingSystemImplementation(): Config file: " << mConfigurationFilePath
79 <<
" has more the one tracking system implementation. Only one is currently supported.";
87 std::vector<ToolFileParser::TrackerInternalStructure> retval;
89 if (!this->isConfigFileValid())
93 for (
int i = 0; i < trackerNodes.count(); ++i)
96 QString trackerType = trackerNodes.at(i).toElement().attribute(
TYPE_ATTRIBUTE);
97 internalStructure.
mType = string2enum<TRACKING_SYSTEM>(trackerType);
100 retval.push_back(internalStructure);
103 if (retval.size() > 1)
105 "Config file " + mConfigurationFilePath
106 +
" has a invalid number of tracking systems, we only support 1 tracking system atm!");
113 std::vector<QString> retval;
115 if (!this->isConfigFileValid())
119 for (
int i = 0; i < toolFileNodes.count(); ++i)
121 QString absoluteToolFilePath = this->getAbsoluteToolFilePath(toolFileNodes.at(i).toElement());
122 if (absoluteToolFilePath.isEmpty())
125 retval.push_back(absoluteToolFilePath);
135 if (!this->isConfigFileValid())
143 for (
int i = 0; i < toolFileNodes.count(); ++i)
146 if (reference.contains(
"yes", Qt::CaseInsensitive))
149 retval = this->getAbsoluteToolFilePath(toolFileNodes.at(i).toElement());
157 std::vector<ToolStructure> retval;
159 if (!this->isConfigFileValid())
163 for (
int i = 0; i < toolFileNodes.count(); ++i)
166 toolStructure.
mAbsoluteToolFilePath = this->getAbsoluteToolFilePath(toolFileNodes.at(i).toElement());
171 if (reference.contains(
"yes", Qt::CaseInsensitive))
175 retval.push_back(toolStructure);
186 QString ConfigurationFileParser::convertToRelativeToolFilePath(QString configFilename, QString absoluteToolFilePath)
188 foreach (QString root,
profile()->getAllRootConfigPaths())
190 QString configPath = getToolPathFromRoot(root);
191 if (!absoluteToolFilePath.contains(configPath))
193 absoluteToolFilePath.replace(configPath,
"");
194 if (absoluteToolFilePath.startsWith(
"/"))
195 absoluteToolFilePath.remove(0, 1);
196 return absoluteToolFilePath;
200 return absoluteToolFilePath;
203 QString ConfigurationFileParser::getToolPathFromRoot(QString root)
205 return root +
"/tool/Tools/";
210 bool doSaveFile =
true;
212 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"\"1.0\""));
214 QDomElement configNode = doc.createElement(
CONFIG_TAG);
220 configNode.appendChild(trackingsystemImplementationNode);
225 QString trackingSystemName =
enum2string(it1->first);
226 if(trackingSystemName.isEmpty())
229 trackingSystemName=
"";
234 ToolStructureVector::iterator it2 = it1->second.begin();
235 for (; it2 != it1->second.end(); ++it2)
237 QString absoluteToolFilePath = it2->mAbsoluteToolFilePath;
238 QString relativeToolFilePath = convertToRelativeToolFilePath(config.
mFileName, absoluteToolFilePath);
248 if (!trackingSystemName.contains(
enum2string(toolparser.
getTool()->mTrackerType), Qt::CaseInsensitive))
250 reportWarning(
"When saving configuration, skipping tool " + relativeToolFilePath +
" of type " 251 + toolTrackerType +
" because trackingSystemName is set to " + trackingSystemName);
256 toolFileNode.appendChild(doc.createTextNode(relativeToolFilePath));
261 trackerTagNode.appendChild(toolFileNode);
263 trackingsystemImplementationNode.appendChild(trackerTagNode);
266 doc.appendChild(configNode);
272 QDir().mkpath(QFileInfo(config.
mFileName).absolutePath());
274 if (!file.open(QIODevice::WriteOnly))
276 reportWarning(
"Could not open file " + file.fileName() +
", aborting writing of config.");
280 QTextStream stream(&file);
282 reportSuccess(
"Configuration file " + file.fileName() +
" is written.");
285 CX_LOG_INFO() <<
"Changing OpenIGTLink tool files not supported. Not overwriting config file: " << file.fileName();
288 void ConfigurationFileParser::setConfigDocument(QString configAbsoluteFilePath)
290 QFile configFile(configAbsoluteFilePath);
291 if (!configFile.exists())
297 QString errorMessage;
299 if (!mConfigureDoc.setContent(&configFile, &errorMessage, &errorInLine))
301 reportError(
"Could not set the xml content of the config file " + configAbsoluteFilePath);
302 CX_LOG_ERROR() <<
"Qt error message: " << errorMessage <<
" in line: " << errorInLine;
307 bool ConfigurationFileParser::isConfigFileValid()
310 QDomNode configNode = mConfigureDoc.elementsByTagName(
CONFIG_TAG).item(0);
311 if (configNode.isNull())
319 QString ConfigurationFileParser::findXmlFileWithDirNameInPath(QString path)
323 filter << dir.dirName() +
".xml";
324 QStringList filepaths = dir.entryList(filter);
325 if (!filepaths.isEmpty())
326 return dir.absoluteFilePath(filter[0]);
330 QString ConfigurationFileParser::searchForExistingToolFilePath(QString relativeToolFilePath)
333 relativeToolFilePath.replace(
"../Tools/",
"");
335 foreach (QString root,
profile()->getAllRootConfigPaths())
337 QString configPath = this->getToolPathFromRoot(root);
338 QFileInfo guess(configPath +
"/" + relativeToolFilePath);
340 return guess.canonicalFilePath();
345 QString ConfigurationFileParser::getAbsoluteToolFilePath(QDomElement toolfileelement)
347 QString relativeToolFilePath = toolfileelement.text();
348 if (relativeToolFilePath.isEmpty())
351 QString absoluteToolFilePath = this->searchForExistingToolFilePath(relativeToolFilePath);
353 QFileInfo info(absoluteToolFilePath);
355 reportError(QString(
"Tool file %1 in configuration %2 not found. Skipping.")
356 .arg(relativeToolFilePath)
357 .arg(mConfigurationFilePath));
360 absoluteToolFilePath = this->findXmlFileWithDirNameInPath(absoluteToolFilePath);
361 return absoluteToolFilePath;
TrackersAndToolsMap mTrackersAndTools
the trackers and tools (relative path) that should be used in the config
cxResource_EXPORT ProfilePtr profile()
void reportError(QString msg)
QString getTrackingSystemImplementation()
~ConfigurationFileParser()
QString getApplicationapplication()
static QString getTemplatesAbsoluteFilePath()
QString mFileName
absolute path and filename for the new config file
ConfigurationFileParser(QString absoluteConfigFilePath, QString loggingFolder="")
QString getAbsoluteReferenceFilePath()
static void saveConfiguration(Configuration &config)
#define TRACKING_SYSTEM_IMPLEMENTATION_IGSTK
#define TRACKING_SYSTEM_IMPLEMENTATION_IGTLINK
void reportWarning(QString msg)
void reportSuccess(QString msg)
QString mTrackingSystemImplementation
static QString getRootConfigPath()
return path to root config folder. May be replaced with getExistingConfigPath()
std::vector< QString > getAbsoluteToolFilePaths()
QString mClinical_app
the clinical application this config is made for
std::vector< ToolFileParser::TrackerInternalStructure > getTrackers()
QString enum2string(const ENUM &val)
std::vector< ConfigurationFileParser::ToolStructure > getToolListWithMetaInformation()
Namespace for all CustusX production code.