ctkCmdLineModuleObjectTreeWalker.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleObjectTreeWalker_p.h"
  16. #include <QObject>
  17. #include <QStack>
  18. #include <QVariant>
  19. namespace {
  20. static QString PREFIX_EXECUTABLE = "executable:";
  21. static QString PREFIX_PARAMETER_GROUP = "paramGroup:";
  22. static QString PREFIX_PARAMETER = "parameter:";
  23. }
  24. ctkCmdLineModuleObjectTreeWalker::ctkCmdLineModuleObjectTreeWalker(QObject *root)
  25. : RootObject(root), CurrentObject(0), CurrentToken(NoToken),
  26. AtEnd(false)
  27. {
  28. }
  29. ctkCmdLineModuleObjectTreeWalker::~ctkCmdLineModuleObjectTreeWalker()
  30. {
  31. }
  32. void ctkCmdLineModuleObjectTreeWalker::setRootObject(QObject* root)
  33. {
  34. RootObject = root;
  35. clear();
  36. }
  37. void ctkCmdLineModuleObjectTreeWalker::clear()
  38. {
  39. CurrentToken = NoToken;
  40. CurrentObject = 0;
  41. ObjectStack.clear();
  42. }
  43. bool ctkCmdLineModuleObjectTreeWalker::atEnd() const
  44. {
  45. return AtEnd || RootObject == 0;
  46. }
  47. bool ctkCmdLineModuleObjectTreeWalker::isParameterGroup() const
  48. {
  49. return CurrentToken == ParameterGroup;
  50. }
  51. bool ctkCmdLineModuleObjectTreeWalker::isParameter() const
  52. {
  53. return CurrentToken == Parameter;
  54. }
  55. QString ctkCmdLineModuleObjectTreeWalker::name() const
  56. {
  57. if (CurrentObject == 0) return QString();
  58. switch(CurrentToken)
  59. {
  60. case Executable: return CurrentObject->objectName().mid(PREFIX_EXECUTABLE.size());
  61. case ParameterGroup: return CurrentObject->objectName().mid(PREFIX_PARAMETER_GROUP.size());
  62. case Parameter: return CurrentObject->objectName().mid(PREFIX_PARAMETER.size());
  63. default: return QString();
  64. }
  65. }
  66. QString ctkCmdLineModuleObjectTreeWalker::label() const
  67. {
  68. if (CurrentObject == 0) return QString();
  69. switch(CurrentToken)
  70. {
  71. case Executable: return CurrentObject->objectName().mid(PREFIX_EXECUTABLE.size());
  72. case ParameterGroup: return property("title").toString();
  73. case Parameter: return property("label").toString();
  74. default: return QString();
  75. }
  76. }
  77. QVariant ctkCmdLineModuleObjectTreeWalker::value() const
  78. {
  79. QString valProp = property("valueProperty").toString();
  80. return property(valProp);
  81. }
  82. void ctkCmdLineModuleObjectTreeWalker::setValue(const QVariant& value)
  83. {
  84. QVariant valProp = property("valueProperty");
  85. if (valProp.isValid())
  86. {
  87. CurrentObject->setProperty(qPrintable(valProp.toString()), value);
  88. }
  89. }
  90. QString ctkCmdLineModuleObjectTreeWalker::flag() const
  91. {
  92. QVariant v = property("flag");
  93. return v.isValid() ? v.toString() : QString();
  94. }
  95. QString ctkCmdLineModuleObjectTreeWalker::longFlag() const
  96. {
  97. QVariant v = property("longflag");
  98. return v.isValid() ? v.toString() : QString();
  99. }
  100. int ctkCmdLineModuleObjectTreeWalker::index() const
  101. {
  102. QVariant v = property("index");
  103. return v.isValid() ? v.toInt() : -1;
  104. }
  105. bool ctkCmdLineModuleObjectTreeWalker::isMultiple() const
  106. {
  107. QVariant v = property("multiple");
  108. return v.isValid() ? v.toBool() : false;
  109. }
  110. QVariant ctkCmdLineModuleObjectTreeWalker::property(const QString &propName) const
  111. {
  112. if (CurrentObject == 0) return QVariant();
  113. // First try to get a prefixed property
  114. QVariant res = prefixedProperty(propName);
  115. // Try to get a property with the original name
  116. if (!res.isValid()) res = CurrentObject->property(qPrintable(propName));
  117. return res;
  118. }
  119. ctkCmdLineModuleObjectTreeWalker::TokenType ctkCmdLineModuleObjectTreeWalker::readNext()
  120. {
  121. if (AtEnd) return NoToken;
  122. QObject* curr = 0;
  123. if (CurrentObject == 0)
  124. {
  125. curr = RootObject;
  126. if (setCurrent(curr)) return CurrentToken;
  127. }
  128. else
  129. {
  130. curr = CurrentObject;
  131. }
  132. while (true)
  133. {
  134. if (curr)
  135. {
  136. QObjectList children = curr->children();
  137. QListIterator<QObject*> i(children);
  138. i.toBack();
  139. while (i.hasPrevious())
  140. {
  141. ObjectStack.push(i.previous());
  142. }
  143. if (children.isEmpty())
  144. {
  145. curr = 0;
  146. }
  147. else
  148. {
  149. curr = ObjectStack.pop();
  150. if (setCurrent(curr)) return CurrentToken;
  151. }
  152. continue;
  153. }
  154. if (ObjectStack.isEmpty()) break;
  155. curr = ObjectStack.pop();
  156. if (setCurrent(curr)) return CurrentToken;
  157. }
  158. AtEnd = true;
  159. CurrentObject = 0;
  160. CurrentToken = NoToken;
  161. return NoToken;
  162. }
  163. bool ctkCmdLineModuleObjectTreeWalker::readNextExecutable()
  164. {
  165. while (!(readNext() == Executable || AtEnd));
  166. return !AtEnd;
  167. }
  168. bool ctkCmdLineModuleObjectTreeWalker::readNextParameterGroup()
  169. {
  170. while (!(readNext() == ParameterGroup || AtEnd));
  171. return !AtEnd;
  172. }
  173. bool ctkCmdLineModuleObjectTreeWalker::readNextParameter()
  174. {
  175. while (!(readNext() == Parameter || AtEnd));
  176. return !AtEnd;
  177. }
  178. ctkCmdLineModuleObjectTreeWalker::TokenType ctkCmdLineModuleObjectTreeWalker::tokenType() const
  179. {
  180. return CurrentToken;
  181. }
  182. QVariant ctkCmdLineModuleObjectTreeWalker::prefixedProperty(const QString& propName) const
  183. {
  184. if (CurrentObject == 0) return QString();
  185. QString prefixedName;
  186. switch(CurrentToken)
  187. {
  188. case ctkCmdLineModuleObjectTreeWalker::Executable: prefixedName = PREFIX_EXECUTABLE + propName;
  189. case ctkCmdLineModuleObjectTreeWalker::ParameterGroup: prefixedName = PREFIX_PARAMETER_GROUP + propName;
  190. case ctkCmdLineModuleObjectTreeWalker::Parameter: prefixedName = PREFIX_PARAMETER + propName;
  191. default: ;
  192. }
  193. return CurrentObject->property(qPrintable(prefixedName));
  194. }
  195. ctkCmdLineModuleObjectTreeWalker::TokenType
  196. ctkCmdLineModuleObjectTreeWalker::token(QObject* obj)
  197. {
  198. if (obj == 0) return ctkCmdLineModuleObjectTreeWalker::NoToken;
  199. QString name = obj->objectName();
  200. if (name.startsWith(PREFIX_EXECUTABLE)) return ctkCmdLineModuleObjectTreeWalker::Executable;
  201. if (name.startsWith(PREFIX_PARAMETER_GROUP)) return ctkCmdLineModuleObjectTreeWalker::ParameterGroup;
  202. if (name.startsWith(PREFIX_PARAMETER)) return ctkCmdLineModuleObjectTreeWalker::Parameter;
  203. return ctkCmdLineModuleObjectTreeWalker::NoToken;
  204. }
  205. bool ctkCmdLineModuleObjectTreeWalker::setCurrent(QObject* obj)
  206. {
  207. ctkCmdLineModuleObjectTreeWalker::TokenType t = token(obj);
  208. if (t != ctkCmdLineModuleObjectTreeWalker::NoToken)
  209. {
  210. CurrentObject = obj;
  211. CurrentToken = t;
  212. return true;
  213. }
  214. return false;
  215. }