ctkModuleDescription.cpp 7.1 KB

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