ctkCmdLineModuleObjectTreeWalker.cpp 8.7 KB

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