ctkModuleDescription.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. void ctkModuleDescription::addParameterGroup( ctkModuleParameterGroup* group )
  20. {
  21. Q_ASSERT(group);
  22. this->ParameterGroups.push_back(group);
  23. }
  24. //----------------------------------------------------------------------------
  25. const QVector<ctkModuleParameterGroup*>& ctkModuleDescription::parameterGroups() const
  26. {
  27. return this->ParameterGroups;
  28. }
  29. //----------------------------------------------------------------------------
  30. bool ctkModuleDescription::hasReturnParameters() const
  31. {
  32. // iterate over each parameter group
  33. foreach( const ctkModuleParameterGroup* group, this->ParameterGroups)
  34. {
  35. if (group->hasReturnParameters())
  36. {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. //----------------------------------------------------------------------------
  43. bool ctkModuleDescription::setParameterDefaultValue(const QString& name, const QString& value)
  44. {
  45. ctkModuleParameter* param = this->parameter( name );
  46. if ( param )
  47. {
  48. (*param)[ "Default" ] = value;
  49. return true;
  50. }
  51. return false;
  52. }
  53. //----------------------------------------------------------------------------
  54. ctkModuleParameterGroup* ctkModuleDescription::parameterGroup(const QString& parameterName)const
  55. {
  56. // iterate over each parameter group
  57. foreach( ctkModuleParameterGroup* group, this->ParameterGroups)
  58. {
  59. ctkModuleParameter* param = group->parameter(parameterName);
  60. if (param)
  61. {
  62. return group;
  63. }
  64. }
  65. return 0;
  66. }
  67. //----------------------------------------------------------------------------
  68. ctkModuleParameter* ctkModuleDescription::parameter(const QString& name)const
  69. {
  70. // iterate over each parameter group
  71. foreach( const ctkModuleParameterGroup* group, this->ParameterGroups)
  72. {
  73. ctkModuleParameter* param = group->parameter(name);
  74. if (param)
  75. {
  76. return param;
  77. }
  78. }
  79. return 0;
  80. }
  81. //----------------------------------------------------------------------------
  82. void ctkModuleDescription ::setIcon(const QIcon& logo)
  83. {
  84. this->Icon = logo;
  85. }
  86. //----------------------------------------------------------------------------
  87. const QIcon& ctkModuleDescription::icon() const
  88. {
  89. return this->Icon;
  90. }
  91. //----------------------------------------------------------------------------
  92. bool ctkModuleDescription ::readParameterFile(const QString& filename)
  93. {
  94. bool modified = false;
  95. QFile file(filename);
  96. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  97. {
  98. std::cout << "Parameter file " << filename.toStdString( ) << " could not be opened." << endl;
  99. return false;
  100. }
  101. QTextStream in(&file);
  102. while (!in.atEnd())
  103. {
  104. QString line = in.readLine();
  105. // split the line into key: value
  106. QString key, value;
  107. line = line.trimmed();
  108. QStringList list = line.split( "=" );
  109. key = list[ 0 ].trimmed();
  110. if ( list.size() == 1 )
  111. {
  112. continue;
  113. }
  114. value = list[ 1 ].trimmed();
  115. // std::cout << "key=" << key << ", value=" << value << "!" << endl;
  116. ctkModuleParameter *param = this->parameter( key );
  117. if ( param )
  118. {
  119. if (value != (*param)["Default"] )
  120. {
  121. (*param)["Default"] = value;
  122. modified = true;
  123. // multiple="true" may have to be handled differently
  124. }
  125. }
  126. }
  127. return modified;
  128. }
  129. //----------------------------------------------------------------------------
  130. bool ctkModuleDescription::
  131. writeParameterFile(const QString& filename, bool withHandlesToBulkParameters)const
  132. {
  133. QFile rtp(filename);
  134. if (!rtp.open(QIODevice::WriteOnly | QIODevice::Text))
  135. {
  136. std::cout << "Parameter file " << filename.toStdString() << " could not be opened for writing." << endl;
  137. return false;
  138. }
  139. QTextStream in(&rtp);
  140. // iterate over each parameter group
  141. foreach(const ctkModuleParameterGroup* group, this->ParameterGroups)
  142. {
  143. group->writeParameterFile(in, withHandlesToBulkParameters);
  144. }
  145. return true;
  146. }
  147. //----------------------------------------------------------------------------
  148. QTextStream & operator<<(QTextStream &os, const ctkModuleDescription &module)
  149. {
  150. os << QHash<QString, QString>(module);
  151. os << "Icon: " << !module.icon().isNull() << endl;
  152. os << "ParameterGroups: " << endl;
  153. foreach(const ctkModuleParameterGroup* group, module.ParameterGroups)
  154. {
  155. os << *group;
  156. }
  157. return os;
  158. }