ctkCmdLineModuleObjectTreeWalker.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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_CONTAINER = "paramContainer:";
  23. static QString PREFIX_PARAMETER = "parameter:";
  24. }
  25. //----------------------------------------------------------------------------
  26. ctkCmdLineModuleObjectTreeWalker::ctkCmdLineModuleObjectTreeWalker(QObject *root)
  27. : RootObject(root), CurrentObject(0), CurrentToken(NoToken),
  28. AtEnd(false)
  29. {
  30. }
  31. //----------------------------------------------------------------------------
  32. ctkCmdLineModuleObjectTreeWalker::~ctkCmdLineModuleObjectTreeWalker()
  33. {
  34. }
  35. //----------------------------------------------------------------------------
  36. void ctkCmdLineModuleObjectTreeWalker::setRootObject(QObject* root)
  37. {
  38. RootObject = root;
  39. clear();
  40. }
  41. //----------------------------------------------------------------------------
  42. void ctkCmdLineModuleObjectTreeWalker::clear()
  43. {
  44. CurrentToken = NoToken;
  45. CurrentObject = 0;
  46. ObjectStack.clear();
  47. }
  48. //----------------------------------------------------------------------------
  49. bool ctkCmdLineModuleObjectTreeWalker::atEnd() const
  50. {
  51. return AtEnd || RootObject == 0;
  52. }
  53. //----------------------------------------------------------------------------
  54. bool ctkCmdLineModuleObjectTreeWalker::isParameterGroup() const
  55. {
  56. return CurrentToken == ParameterGroup;
  57. }
  58. //----------------------------------------------------------------------------
  59. bool ctkCmdLineModuleObjectTreeWalker::isParameterContainer() const
  60. {
  61. return CurrentToken == ParameterContainer;
  62. }
  63. //----------------------------------------------------------------------------
  64. bool ctkCmdLineModuleObjectTreeWalker::isParameter() const
  65. {
  66. return CurrentToken == Parameter;
  67. }
  68. //----------------------------------------------------------------------------
  69. QString ctkCmdLineModuleObjectTreeWalker::name() const
  70. {
  71. if (CurrentObject == 0) return QString();
  72. switch(CurrentToken)
  73. {
  74. case Executable: return CurrentObject->objectName().mid(PREFIX_EXECUTABLE.size());
  75. case ParameterGroup: return CurrentObject->objectName().mid(PREFIX_PARAMETER_GROUP.size());
  76. case ParameterContainer: return CurrentObject->objectName().mid(PREFIX_PARAMETER_CONTAINER.size());
  77. case Parameter: return CurrentObject->objectName().mid(PREFIX_PARAMETER.size());
  78. default: return QString();
  79. }
  80. }
  81. //----------------------------------------------------------------------------
  82. QString ctkCmdLineModuleObjectTreeWalker::label() const
  83. {
  84. if (CurrentObject == 0) return QString();
  85. switch(CurrentToken)
  86. {
  87. case Executable: return CurrentObject->objectName().mid(PREFIX_EXECUTABLE.size());
  88. case ParameterGroup: return property("title").toString();
  89. case ParameterContainer: return property("name").toString();
  90. case Parameter: return property("label").toString();
  91. default: return QString();
  92. }
  93. }
  94. //----------------------------------------------------------------------------
  95. QVariant ctkCmdLineModuleObjectTreeWalker::value(const QString &propertyName) const
  96. {
  97. QString valProp = propertyName;
  98. if (valProp.isEmpty())
  99. {
  100. valProp = property("valueProperty").toString();
  101. }
  102. return property(valProp);
  103. }
  104. //----------------------------------------------------------------------------
  105. void ctkCmdLineModuleObjectTreeWalker::setValue(const QVariant& value, const QString &propertyName)
  106. {
  107. QString valProp = propertyName;
  108. if (valProp.isEmpty())
  109. {
  110. valProp = property("valueProperty").toString();
  111. }
  112. if (!valProp.isEmpty())
  113. {
  114. CurrentObject->setProperty(qPrintable(valProp), value);
  115. }
  116. }
  117. //----------------------------------------------------------------------------
  118. QString ctkCmdLineModuleObjectTreeWalker::flag() const
  119. {
  120. QVariant v = property("flag");
  121. return v.isValid() ? v.toString() : QString();
  122. }
  123. //----------------------------------------------------------------------------
  124. QString ctkCmdLineModuleObjectTreeWalker::longFlag() const
  125. {
  126. QVariant v = property("longflag");
  127. return v.isValid() ? v.toString() : QString();
  128. }
  129. //----------------------------------------------------------------------------
  130. int ctkCmdLineModuleObjectTreeWalker::index() const
  131. {
  132. QVariant v = property("index");
  133. return v.isValid() ? v.toInt() : -1;
  134. }
  135. //----------------------------------------------------------------------------
  136. bool ctkCmdLineModuleObjectTreeWalker::isMultiple() const
  137. {
  138. QVariant v = property("multiple");
  139. return v.isValid() ? v.toBool() : false;
  140. }
  141. //----------------------------------------------------------------------------
  142. QVariant ctkCmdLineModuleObjectTreeWalker::property(const QString &propName) const
  143. {
  144. if (CurrentObject == 0) return QVariant();
  145. // First try to get a prefixed property
  146. QVariant res = prefixedProperty(propName);
  147. // Try to get a property with the original name
  148. if (!res.isValid()) res = CurrentObject->property(qPrintable(propName));
  149. return res;
  150. }
  151. //----------------------------------------------------------------------------
  152. ctkCmdLineModuleObjectTreeWalker::TokenType ctkCmdLineModuleObjectTreeWalker::readNext()
  153. {
  154. if (AtEnd) return NoToken;
  155. QObject* curr = 0;
  156. if (CurrentObject == 0)
  157. {
  158. curr = RootObject;
  159. if (setCurrent(curr)) return CurrentToken;
  160. }
  161. else
  162. {
  163. curr = CurrentObject;
  164. }
  165. while (true)
  166. {
  167. if (curr)
  168. {
  169. QObjectList children = curr->children();
  170. QListIterator<QObject*> i(children);
  171. i.toBack();
  172. while (i.hasPrevious())
  173. {
  174. ObjectStack.push(i.previous());
  175. }
  176. if (children.isEmpty())
  177. {
  178. curr = 0;
  179. }
  180. else
  181. {
  182. curr = ObjectStack.pop();
  183. if (setCurrent(curr)) return CurrentToken;
  184. }
  185. continue;
  186. }
  187. if (ObjectStack.isEmpty()) break;
  188. curr = ObjectStack.pop();
  189. if (setCurrent(curr)) return CurrentToken;
  190. }
  191. AtEnd = true;
  192. CurrentObject = 0;
  193. CurrentToken = NoToken;
  194. return NoToken;
  195. }
  196. //----------------------------------------------------------------------------
  197. bool ctkCmdLineModuleObjectTreeWalker::readNextExecutable()
  198. {
  199. while (!(readNext() == Executable || AtEnd)) ; // deliberately empty loop body
  200. return !AtEnd;
  201. }
  202. //----------------------------------------------------------------------------
  203. bool ctkCmdLineModuleObjectTreeWalker::readNextParameterGroup()
  204. {
  205. while (!(readNext() == ParameterGroup || AtEnd)) ; // deliberately empty loop body
  206. return !AtEnd;
  207. }
  208. //----------------------------------------------------------------------------
  209. bool ctkCmdLineModuleObjectTreeWalker::readNextParameterContainer()
  210. {
  211. while (!(readNext() == ParameterContainer || AtEnd)) ; // deliberately empty loop body
  212. return !AtEnd;
  213. }
  214. //----------------------------------------------------------------------------
  215. bool ctkCmdLineModuleObjectTreeWalker::readNextParameter()
  216. {
  217. while (!(readNext() == Parameter || AtEnd)) ; // deliberately empty loop body
  218. return !AtEnd;
  219. }
  220. //----------------------------------------------------------------------------
  221. ctkCmdLineModuleObjectTreeWalker::TokenType ctkCmdLineModuleObjectTreeWalker::tokenType() const
  222. {
  223. return CurrentToken;
  224. }
  225. //----------------------------------------------------------------------------
  226. QVariant ctkCmdLineModuleObjectTreeWalker::prefixedProperty(const QString& propName) const
  227. {
  228. if (CurrentObject == 0) return QString();
  229. QString prefixedName;
  230. switch(CurrentToken)
  231. {
  232. case ctkCmdLineModuleObjectTreeWalker::Executable: prefixedName = PREFIX_EXECUTABLE + propName;
  233. case ctkCmdLineModuleObjectTreeWalker::ParameterGroup: prefixedName = PREFIX_PARAMETER_GROUP + propName;
  234. case ctkCmdLineModuleObjectTreeWalker::ParameterContainer: prefixedName = PREFIX_PARAMETER_CONTAINER + propName;
  235. case ctkCmdLineModuleObjectTreeWalker::Parameter: prefixedName = PREFIX_PARAMETER + propName;
  236. default: ;
  237. }
  238. return CurrentObject->property(qPrintable(prefixedName));
  239. }
  240. //----------------------------------------------------------------------------
  241. ctkCmdLineModuleObjectTreeWalker::TokenType
  242. ctkCmdLineModuleObjectTreeWalker::token(QObject* obj)
  243. {
  244. if (obj == 0) return ctkCmdLineModuleObjectTreeWalker::NoToken;
  245. QString name = obj->objectName();
  246. if (name.startsWith(PREFIX_EXECUTABLE)) return ctkCmdLineModuleObjectTreeWalker::Executable;
  247. if (name.startsWith(PREFIX_PARAMETER_GROUP)) return ctkCmdLineModuleObjectTreeWalker::ParameterGroup;
  248. if (name.startsWith(PREFIX_PARAMETER_CONTAINER)) return ctkCmdLineModuleObjectTreeWalker::ParameterContainer;
  249. if (name.startsWith(PREFIX_PARAMETER)) return ctkCmdLineModuleObjectTreeWalker::Parameter;
  250. return ctkCmdLineModuleObjectTreeWalker::NoToken;
  251. }
  252. //----------------------------------------------------------------------------
  253. bool ctkCmdLineModuleObjectTreeWalker::setCurrent(QObject* obj)
  254. {
  255. ctkCmdLineModuleObjectTreeWalker::TokenType t = token(obj);
  256. if (t != ctkCmdLineModuleObjectTreeWalker::NoToken)
  257. {
  258. CurrentObject = obj;
  259. CurrentToken = t;
  260. return true;
  261. }
  262. return false;
  263. }