ctkCmdLineModuleObjectTreeWalker.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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
  88. {
  89. QString valProp = property("valueProperty").toString();
  90. return property(valProp);
  91. }
  92. //----------------------------------------------------------------------------
  93. void ctkCmdLineModuleObjectTreeWalker::setValue(const QVariant& value)
  94. {
  95. QVariant valProp = property("valueProperty");
  96. if (valProp.isValid())
  97. {
  98. CurrentObject->setProperty(qPrintable(valProp.toString()), value);
  99. }
  100. }
  101. //----------------------------------------------------------------------------
  102. QString ctkCmdLineModuleObjectTreeWalker::flag() const
  103. {
  104. QVariant v = property("flag");
  105. return v.isValid() ? v.toString() : QString();
  106. }
  107. //----------------------------------------------------------------------------
  108. QString ctkCmdLineModuleObjectTreeWalker::longFlag() const
  109. {
  110. QVariant v = property("longflag");
  111. return v.isValid() ? v.toString() : QString();
  112. }
  113. //----------------------------------------------------------------------------
  114. int ctkCmdLineModuleObjectTreeWalker::index() const
  115. {
  116. QVariant v = property("index");
  117. return v.isValid() ? v.toInt() : -1;
  118. }
  119. //----------------------------------------------------------------------------
  120. bool ctkCmdLineModuleObjectTreeWalker::isMultiple() const
  121. {
  122. QVariant v = property("multiple");
  123. return v.isValid() ? v.toBool() : false;
  124. }
  125. //----------------------------------------------------------------------------
  126. QVariant ctkCmdLineModuleObjectTreeWalker::property(const QString &propName) const
  127. {
  128. if (CurrentObject == 0) return QVariant();
  129. // First try to get a prefixed property
  130. QVariant res = prefixedProperty(propName);
  131. // Try to get a property with the original name
  132. if (!res.isValid()) res = CurrentObject->property(qPrintable(propName));
  133. return res;
  134. }
  135. //----------------------------------------------------------------------------
  136. ctkCmdLineModuleObjectTreeWalker::TokenType ctkCmdLineModuleObjectTreeWalker::readNext()
  137. {
  138. if (AtEnd) return NoToken;
  139. QObject* curr = 0;
  140. if (CurrentObject == 0)
  141. {
  142. curr = RootObject;
  143. if (setCurrent(curr)) return CurrentToken;
  144. }
  145. else
  146. {
  147. curr = CurrentObject;
  148. }
  149. while (true)
  150. {
  151. if (curr)
  152. {
  153. QObjectList children = curr->children();
  154. QListIterator<QObject*> i(children);
  155. i.toBack();
  156. while (i.hasPrevious())
  157. {
  158. ObjectStack.push(i.previous());
  159. }
  160. if (children.isEmpty())
  161. {
  162. curr = 0;
  163. }
  164. else
  165. {
  166. curr = ObjectStack.pop();
  167. if (setCurrent(curr)) return CurrentToken;
  168. }
  169. continue;
  170. }
  171. if (ObjectStack.isEmpty()) break;
  172. curr = ObjectStack.pop();
  173. if (setCurrent(curr)) return CurrentToken;
  174. }
  175. AtEnd = true;
  176. CurrentObject = 0;
  177. CurrentToken = NoToken;
  178. return NoToken;
  179. }
  180. //----------------------------------------------------------------------------
  181. bool ctkCmdLineModuleObjectTreeWalker::readNextExecutable()
  182. {
  183. while (!(readNext() == Executable || AtEnd));
  184. return !AtEnd;
  185. }
  186. //----------------------------------------------------------------------------
  187. bool ctkCmdLineModuleObjectTreeWalker::readNextParameterGroup()
  188. {
  189. while (!(readNext() == ParameterGroup || AtEnd));
  190. return !AtEnd;
  191. }
  192. //----------------------------------------------------------------------------
  193. bool ctkCmdLineModuleObjectTreeWalker::readNextParameter()
  194. {
  195. while (!(readNext() == Parameter || AtEnd));
  196. return !AtEnd;
  197. }
  198. //----------------------------------------------------------------------------
  199. ctkCmdLineModuleObjectTreeWalker::TokenType ctkCmdLineModuleObjectTreeWalker::tokenType() const
  200. {
  201. return CurrentToken;
  202. }
  203. //----------------------------------------------------------------------------
  204. QVariant ctkCmdLineModuleObjectTreeWalker::prefixedProperty(const QString& propName) const
  205. {
  206. if (CurrentObject == 0) return QString();
  207. QString prefixedName;
  208. switch(CurrentToken)
  209. {
  210. case ctkCmdLineModuleObjectTreeWalker::Executable: prefixedName = PREFIX_EXECUTABLE + propName;
  211. case ctkCmdLineModuleObjectTreeWalker::ParameterGroup: prefixedName = PREFIX_PARAMETER_GROUP + propName;
  212. case ctkCmdLineModuleObjectTreeWalker::Parameter: prefixedName = PREFIX_PARAMETER + propName;
  213. default: ;
  214. }
  215. return CurrentObject->property(qPrintable(prefixedName));
  216. }
  217. //----------------------------------------------------------------------------
  218. ctkCmdLineModuleObjectTreeWalker::TokenType
  219. ctkCmdLineModuleObjectTreeWalker::token(QObject* obj)
  220. {
  221. if (obj == 0) return ctkCmdLineModuleObjectTreeWalker::NoToken;
  222. QString name = obj->objectName();
  223. if (name.startsWith(PREFIX_EXECUTABLE)) return ctkCmdLineModuleObjectTreeWalker::Executable;
  224. if (name.startsWith(PREFIX_PARAMETER_GROUP)) return ctkCmdLineModuleObjectTreeWalker::ParameterGroup;
  225. if (name.startsWith(PREFIX_PARAMETER)) return ctkCmdLineModuleObjectTreeWalker::Parameter;
  226. return ctkCmdLineModuleObjectTreeWalker::NoToken;
  227. }
  228. //----------------------------------------------------------------------------
  229. bool ctkCmdLineModuleObjectTreeWalker::setCurrent(QObject* obj)
  230. {
  231. ctkCmdLineModuleObjectTreeWalker::TokenType t = token(obj);
  232. if (t != ctkCmdLineModuleObjectTreeWalker::NoToken)
  233. {
  234. CurrentObject = obj;
  235. CurrentToken = t;
  236. return true;
  237. }
  238. return false;
  239. }