ctkModuleDescription.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 this 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. //----------------------------------------------------------------------------
  19. ctkModuleDescription::ctkModuleDescription()
  20. {
  21. }
  22. //----------------------------------------------------------------------------
  23. ctkModuleDescription::ctkModuleDescription(const ctkModuleDescription &md)
  24. : QHash<QString, QString>( QHash<QString, QString>( md ) )
  25. {
  26. this->ParameterGroups = md.ParameterGroups;
  27. this->Logo = md.Logo;
  28. }
  29. //----------------------------------------------------------------------------
  30. void ctkModuleDescription::operator=(const ctkModuleDescription &md)
  31. {
  32. QHash<QString, QString>::operator=(md);
  33. this->ParameterGroups = md.ParameterGroups;
  34. this->Logo = md.Logo;
  35. }
  36. //----------------------------------------------------------------------------
  37. QTextStream & operator<<(QTextStream &os, const ctkModuleDescription &module)
  38. {
  39. os << QHash<QString, QString>(module);
  40. //os << "Logo: " << module.GetLogo() << endl;
  41. os << "ParameterGroups: " << endl;
  42. foreach( const ctkModuleParameterGroup& it, module.parameterGroups())
  43. { os << it; }
  44. return os;
  45. }
  46. //----------------------------------------------------------------------------
  47. bool ctkModuleDescription::hasReturnParameters() const
  48. {
  49. // iterate over each parameter group
  50. QVector<ctkModuleParameterGroup>::const_iterator pgbeginit
  51. = this->ParameterGroups.begin();
  52. QVector<ctkModuleParameterGroup>::const_iterator pgendit
  53. = this->ParameterGroups.end();
  54. QVector<ctkModuleParameterGroup>::const_iterator pgit;
  55. for (pgit = pgbeginit; pgit != pgendit; ++pgit)
  56. {
  57. // iterate over each parameter in this group
  58. QVector<ctkModuleParameter>::const_iterator pbeginit
  59. = (*pgit).parameters().begin();
  60. QVector<ctkModuleParameter>::const_iterator pendit
  61. = (*pgit).parameters().end();
  62. QVector<ctkModuleParameter>::const_iterator pit;
  63. for (pit = pbeginit; pit != pendit; ++pit)
  64. {
  65. if ((*pit).isReturnParameter())
  66. {
  67. return true;
  68. }
  69. }
  70. }
  71. return false;
  72. }
  73. //----------------------------------------------------------------------------
  74. bool ctkModuleDescription::setParameterDefaultValue(const QString& name, const QString& value)
  75. {
  76. ctkModuleParameter* param = parameter( name );
  77. if ( param )
  78. {
  79. (*param)[ "Default" ] = value;
  80. return true;
  81. }
  82. return false;
  83. }
  84. //----------------------------------------------------------------------------
  85. ctkModuleParameter* ctkModuleDescription::parameter(const QString& name)
  86. {
  87. // iterate over each parameter group
  88. QVector<ctkModuleParameterGroup>::iterator pgbeginit
  89. = this->ParameterGroups.begin();
  90. QVector<ctkModuleParameterGroup>::iterator pgendit
  91. = this->ParameterGroups.end();
  92. QVector<ctkModuleParameterGroup>::iterator pgit;
  93. for (pgit = pgbeginit; pgit != pgendit; ++pgit)
  94. {
  95. // iterate over each parameter in this group
  96. QVector<ctkModuleParameter>::iterator pbeginit
  97. = (*pgit).parameters().begin();
  98. QVector<ctkModuleParameter>::iterator pendit
  99. = (*pgit).parameters().end();
  100. QVector<ctkModuleParameter>::iterator pit;
  101. for (pit = pbeginit; pit != pendit; ++pit)
  102. {
  103. if ((*pit)["Name"] == name)
  104. {
  105. return &(*pit);
  106. }
  107. }
  108. }
  109. return NULL;
  110. }
  111. //----------------------------------------------------------------------------
  112. void ctkModuleDescription ::setLogo(const QIcon& logo)
  113. {
  114. this->Logo = logo;
  115. }
  116. //----------------------------------------------------------------------------
  117. const QIcon& ctkModuleDescription::logo() const
  118. {
  119. return this->Logo;
  120. }
  121. //----------------------------------------------------------------------------
  122. bool ctkModuleDescription ::readParameterFile(const QString& filename)
  123. {
  124. bool modified = false;
  125. QFile file(filename);
  126. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  127. {
  128. std::cout << "Parameter file " << filename.toStdString( ) << " could not be opened." << endl;
  129. return false;
  130. }
  131. QTextStream in(&file);
  132. while (!in.atEnd())
  133. {
  134. QString line = in.readLine();
  135. // split the line into key: value
  136. QString key, value;
  137. line = line.trimmed();
  138. QStringList list = line.split( "=" );
  139. key = list[ 0 ].trimmed();
  140. if ( list.size() == 1 )
  141. {
  142. continue;
  143. }
  144. value = list[ 1 ].trimmed();
  145. // std::cout << "key=" << key << ", value=" << value << "!" << endl;
  146. ctkModuleParameter *param = this->parameter( key );
  147. if ( param )
  148. {
  149. if (value != (*param)["Default"] )
  150. {
  151. (*param)["Default"] = value;
  152. modified = true;
  153. // multiple="true" may have to be handled differently
  154. }
  155. }
  156. }
  157. return modified;
  158. }
  159. //----------------------------------------------------------------------------
  160. bool ctkModuleDescription::
  161. writeParameterFile(const QString& filename, bool withHandlesToBulkParameters)
  162. {
  163. QFile rtp(filename);
  164. if (!rtp.open(QIODevice::WriteOnly | QIODevice::Text))
  165. {
  166. std::cout << "Parameter file " << filename.toStdString() << " could not be opened for writing." << endl;
  167. return false;
  168. }
  169. QTextStream in(&rtp);
  170. // iterate over each parameter group
  171. QVector<ctkModuleParameterGroup>::const_iterator pgbeginit
  172. = this->ParameterGroups.begin();
  173. QVector<ctkModuleParameterGroup>::const_iterator pgendit
  174. = this->ParameterGroups.end();
  175. QVector<ctkModuleParameterGroup>::const_iterator pgit;
  176. for (pgit = pgbeginit; pgit != pgendit; ++pgit)
  177. {
  178. // iterate over each parameter in this group
  179. QVector<ctkModuleParameter>::const_iterator pbeginit
  180. = (*pgit).parameters().begin();
  181. QVector<ctkModuleParameter>::const_iterator pendit
  182. = (*pgit).parameters().end();
  183. QVector<ctkModuleParameter>::const_iterator pit;
  184. for (pit = pbeginit; pit != pendit; ++pit)
  185. {
  186. // write out all parameters or just the ones that are not bulk parameters
  187. if (withHandlesToBulkParameters
  188. || (!withHandlesToBulkParameters
  189. && ((*pit)[ "Tag" ] != "image"
  190. && (*pit)[ "Tag" ] != "geometry"
  191. && (*pit)[ "Tag" ] != "transform"
  192. && (*pit)[ "Tag" ] != "table"
  193. && (*pit)[ "Tag" ] != "measurement"
  194. && (*pit)[ "Tag" ] != "point" // point and region are special
  195. && (*pit)[ "Tag" ] != "region")))
  196. {
  197. in << (*pit)[ "Name" ] << " = " << (*pit)[ "Default" ] << endl;
  198. // multiple="true" may have to be handled differently
  199. }
  200. }
  201. }
  202. return true;
  203. }
  204. void ctkModuleDescription::addParameterGroup( const ctkModuleParameterGroup &group )
  205. {
  206. this->ParameterGroups.push_back(group);
  207. }
  208. const QVector<ctkModuleParameterGroup>& ctkModuleDescription::parameterGroups() const
  209. {
  210. return this->ParameterGroups;
  211. }
  212. QVector<ctkModuleParameterGroup>& ctkModuleDescription::parameterGroups()
  213. {
  214. return this->ParameterGroups;
  215. }
  216. void ctkModuleDescription::setParameterGroups( const QVector<ctkModuleParameterGroup>& groups )
  217. {
  218. this->ParameterGroups = groups;
  219. }