ctkModuleDescription.cpp 11 KB

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