ctkCmdLineModuleDescription.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 Brigham and Women's Hospital (BWH) All Rights Reserved.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use d file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =============================================================================*/
  14. #include "ctkCmdLineModuleDescription.h"
  15. #include <iostream>
  16. #include "QFile"
  17. #include "QTextStream"
  18. struct ctkCmdLineModuleDescriptionPrivate
  19. {
  20. ~ctkCmdLineModuleDescriptionPrivate()
  21. {
  22. qDeleteAll(ParameterGroups);
  23. }
  24. QString Title;
  25. QString Category;
  26. QString Description;
  27. QString Version;
  28. QString DocumentationURL;
  29. QString License;
  30. QString Acknowledgements;
  31. QString Contributor;
  32. QString Type;
  33. QString Target;
  34. QString Location;
  35. QString AlternativeType;
  36. QString AlternativeTarget;
  37. QString AlternativeLocation;
  38. QIcon Logo;
  39. QList<ctkCmdLineModuleParameterGroup*> ParameterGroups;
  40. //ModuleProcessInformation ProcessInformation;
  41. };
  42. //----------------------------------------------------------------------------
  43. ctkCmdLineModuleDescription::ctkCmdLineModuleDescription()
  44. : d_ptr(new ctkCmdLineModuleDescriptionPrivate)
  45. {
  46. }
  47. //----------------------------------------------------------------------------
  48. ctkCmdLineModuleDescription::~ctkCmdLineModuleDescription()
  49. {
  50. delete d_ptr;
  51. }
  52. //----------------------------------------------------------------------------
  53. void ctkCmdLineModuleDescription::setCategory(const QString& cat)
  54. {
  55. Q_D(ctkCmdLineModuleDescription);
  56. d->Category = cat;
  57. }
  58. //----------------------------------------------------------------------------
  59. QString ctkCmdLineModuleDescription::category() const
  60. {
  61. Q_D(const ctkCmdLineModuleDescription);
  62. return d->Category;
  63. }
  64. //----------------------------------------------------------------------------
  65. void ctkCmdLineModuleDescription::setTitle(const QString& title)
  66. {
  67. Q_D(ctkCmdLineModuleDescription);
  68. d->Title = title;
  69. }
  70. //----------------------------------------------------------------------------
  71. QString ctkCmdLineModuleDescription::title() const
  72. {
  73. Q_D(const ctkCmdLineModuleDescription);
  74. return d->Title;
  75. }
  76. //----------------------------------------------------------------------------
  77. void ctkCmdLineModuleDescription::setDescription(const QString& description)
  78. {
  79. Q_D(ctkCmdLineModuleDescription);
  80. d->Description = description;
  81. }
  82. //----------------------------------------------------------------------------
  83. QString ctkCmdLineModuleDescription::description() const
  84. {
  85. Q_D(const ctkCmdLineModuleDescription);
  86. return d->Description;
  87. }
  88. //----------------------------------------------------------------------------
  89. void ctkCmdLineModuleDescription::setVersion(const QString& version)
  90. {
  91. Q_D(ctkCmdLineModuleDescription);
  92. d->Version = version;
  93. }
  94. //----------------------------------------------------------------------------
  95. QString ctkCmdLineModuleDescription::version() const
  96. {
  97. Q_D(const ctkCmdLineModuleDescription);
  98. return d->Version;
  99. }
  100. //----------------------------------------------------------------------------
  101. void ctkCmdLineModuleDescription::setDocumentationURL(const QString& documentationURL)
  102. {
  103. Q_D(ctkCmdLineModuleDescription);
  104. d->DocumentationURL = documentationURL;
  105. }
  106. //----------------------------------------------------------------------------
  107. QString ctkCmdLineModuleDescription::documentationURL() const
  108. {
  109. Q_D(const ctkCmdLineModuleDescription);
  110. return d->DocumentationURL;
  111. }
  112. //----------------------------------------------------------------------------
  113. void ctkCmdLineModuleDescription::setLicense(const QString& license)
  114. {
  115. Q_D(ctkCmdLineModuleDescription);
  116. d->License = license;
  117. }
  118. //----------------------------------------------------------------------------
  119. QString ctkCmdLineModuleDescription::license() const
  120. {
  121. Q_D(const ctkCmdLineModuleDescription);
  122. return d->License;
  123. }
  124. //----------------------------------------------------------------------------
  125. void ctkCmdLineModuleDescription::setAcknowledgements(const QString& acknowledgements)
  126. {
  127. Q_D(ctkCmdLineModuleDescription);
  128. d->Acknowledgements = acknowledgements;
  129. }
  130. //----------------------------------------------------------------------------
  131. QString ctkCmdLineModuleDescription::acknowledgements() const
  132. {
  133. Q_D(const ctkCmdLineModuleDescription);
  134. return d->Acknowledgements;
  135. }
  136. //----------------------------------------------------------------------------
  137. void ctkCmdLineModuleDescription::setContributor(const QString& contributor)
  138. {
  139. Q_D(ctkCmdLineModuleDescription);
  140. d->Contributor = contributor;
  141. }
  142. //----------------------------------------------------------------------------
  143. QString ctkCmdLineModuleDescription::contributor() const
  144. {
  145. Q_D(const ctkCmdLineModuleDescription);
  146. return d->Contributor;
  147. }
  148. //----------------------------------------------------------------------------
  149. void ctkCmdLineModuleDescription::setLocation(const QString& target)
  150. {
  151. Q_D(ctkCmdLineModuleDescription);
  152. d->Location = target;
  153. }
  154. //----------------------------------------------------------------------------
  155. QString ctkCmdLineModuleDescription::location() const
  156. {
  157. Q_D(const ctkCmdLineModuleDescription);
  158. return d->Location;
  159. }
  160. //----------------------------------------------------------------------------
  161. void ctkCmdLineModuleDescription::setLogo(const QIcon& logo)
  162. {
  163. Q_D(ctkCmdLineModuleDescription);
  164. d->Logo = logo;
  165. }
  166. //----------------------------------------------------------------------------
  167. QIcon ctkCmdLineModuleDescription::logo() const
  168. {
  169. Q_D(const ctkCmdLineModuleDescription);
  170. return d->Logo;
  171. }
  172. //----------------------------------------------------------------------------
  173. void ctkCmdLineModuleDescription::addParameterGroup(ctkCmdLineModuleParameterGroup* group)
  174. {
  175. Q_D(ctkCmdLineModuleDescription);
  176. d->ParameterGroups.push_back(group);
  177. }
  178. //----------------------------------------------------------------------------
  179. QList<ctkCmdLineModuleParameterGroup*> ctkCmdLineModuleDescription::parameterGroups() const
  180. {
  181. Q_D(const ctkCmdLineModuleDescription);
  182. return d->ParameterGroups;
  183. }
  184. //----------------------------------------------------------------------------
  185. void ctkCmdLineModuleDescription::setParameterGroups(const QList<ctkCmdLineModuleParameterGroup*>& groups)
  186. {
  187. Q_D(ctkCmdLineModuleDescription);
  188. d->ParameterGroups = groups;
  189. }
  190. //----------------------------------------------------------------------------
  191. bool ctkCmdLineModuleDescription::hasParameter(const QString& name) const
  192. {
  193. Q_D(const ctkCmdLineModuleDescription);
  194. // iterate over each parameter group
  195. foreach(const ctkCmdLineModuleParameterGroup* group, d->ParameterGroups)
  196. {
  197. if (group->hasParameter(name)) return true;
  198. }
  199. return false;
  200. }
  201. //----------------------------------------------------------------------------
  202. ctkCmdLineModuleParameter* ctkCmdLineModuleDescription::parameter(const QString& name) const
  203. {
  204. Q_D(const ctkCmdLineModuleDescription);
  205. foreach(const ctkCmdLineModuleParameterGroup* group, d->ParameterGroups)
  206. {
  207. ctkCmdLineModuleParameter* param = group->parameter(name);
  208. if (param) return param;
  209. }
  210. return 0;
  211. }
  212. //----------------------------------------------------------------------------
  213. bool ctkCmdLineModuleDescription::hasReturnParameters() const
  214. {
  215. Q_D(const ctkCmdLineModuleDescription);
  216. // iterate over each parameter group
  217. foreach(const ctkCmdLineModuleParameterGroup* group, d->ParameterGroups)
  218. {
  219. if (group->hasReturnParameters()) return true;
  220. }
  221. return false;
  222. }
  223. //----------------------------------------------------------------------------
  224. bool ctkCmdLineModuleDescription::setParameterDefaultValue(const QString& name,
  225. const QString& value)
  226. {
  227. ctkCmdLineModuleParameter* param = parameter(name);
  228. if (param)
  229. {
  230. param->setDefaultValue(value);
  231. return true;
  232. }
  233. return false;
  234. }
  235. //----------------------------------------------------------------------------
  236. bool ctkCmdLineModuleDescription ::readParameterFile(const QString& filename)
  237. {
  238. bool modified = false;
  239. QFile file(filename);
  240. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  241. {
  242. std::cout << "Parameter file " << filename.toStdString( ) << " could not be opened." << '\n';
  243. return false;
  244. }
  245. QTextStream in(&file);
  246. while (!in.atEnd())
  247. {
  248. QString line = in.readLine();
  249. // split the line into key: value
  250. QString key, value;
  251. line = line.trimmed();
  252. QStringList list = line.split( "=" );
  253. key = list[ 0 ].trimmed();
  254. if ( list.size() == 1 )
  255. {
  256. continue;
  257. }
  258. value = list[ 1 ].trimmed();
  259. // std::cout << "key=" << key << ", value=" << value << "!" << endl;
  260. ctkCmdLineModuleParameter* param = this->parameter(key);
  261. if (param)
  262. {
  263. if (value != param->defaultValue())
  264. {
  265. param->setDefaultValue(value);
  266. modified = true;
  267. // multiple="true" may have to be handled differently
  268. }
  269. }
  270. }
  271. return modified;
  272. }
  273. //----------------------------------------------------------------------------
  274. bool ctkCmdLineModuleDescription::
  275. writeParameterFile(const QString& filename, bool withHandlesToBulkParameters)const
  276. {
  277. Q_D(const ctkCmdLineModuleDescription);
  278. QFile rtp(filename);
  279. if (!rtp.open(QIODevice::WriteOnly | QIODevice::Text))
  280. {
  281. std::cout << "Parameter file " << filename.toStdString() << " could not be opened for writing." << '\n';
  282. return false;
  283. }
  284. QTextStream in(&rtp);
  285. // iterate over each parameter group
  286. foreach(const ctkCmdLineModuleParameterGroup* group, d->ParameterGroups)
  287. {
  288. group->writeParameterFile(in, withHandlesToBulkParameters);
  289. }
  290. return true;
  291. }
  292. //----------------------------------------------------------------------------
  293. QTextStream & operator<<(QTextStream &os, const ctkCmdLineModuleDescription &module)
  294. {
  295. os << "Title: " << module.title() << '\n';
  296. os << "Category: " << module.category() << '\n';
  297. os << "Description: " << module.description() << '\n';
  298. os << "Version: " << module.version() << '\n';
  299. os << "DocumentationURL: " << module.documentationURL() << '\n';
  300. os << "License: " << module.license() << '\n';
  301. os << "Contributor: " << module.contributor() << '\n';
  302. os << "Acknowledgements: " << module.acknowledgements() << '\n';
  303. os << "Location: " << module.location() << '\n';
  304. //os << "Logo: " << module.GetLogo() << '\n';
  305. //os << "ProcessInformation: " << '\n'
  306. // << *(module.GetProcessInformation());
  307. os << "ParameterGroups: " << '\n';
  308. foreach(const ctkCmdLineModuleParameterGroup* group, module.parameterGroups())
  309. {
  310. os << *group;
  311. }
  312. return os;
  313. }