ctkFlatProxyModelTest.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  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.txt
  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. // Qt includes
  15. #include <QDebug>
  16. #include <QApplication>
  17. #include <QEventLoop>
  18. #include <QTreeView>
  19. #include <QStandardItem>
  20. #include <QStandardItemModel>
  21. #include <QTimer>
  22. // CTK includes
  23. #include "ctkFlatProxyModel.h"
  24. #include "ctkModelTester.h"
  25. #include "ctkTest.h"
  26. // ----------------------------------------------------------------------------
  27. class ctkFlatProxyModelTester: public QObject
  28. {
  29. Q_OBJECT
  30. private slots:
  31. void testModel();
  32. void testModel_data();
  33. private:
  34. QStandardItem* createItem(const QString& name, QVariant& children)const;
  35. };
  36. // ----------------------------------------------------------------------------
  37. void ctkFlatProxyModelTester::testModel_data()
  38. {
  39. QTest::addColumn<QVariant >("hashModel");
  40. QTest::addColumn<int>("startHideLevel");
  41. QTest::addColumn<int>("endFlattenLevel");
  42. QTest::addColumn<int>("level0ExpectedRowCount");
  43. QTest::addColumn<int>("level1ExpectedRowCount");
  44. // -\ top
  45. // +--\ item1
  46. // | +--\ subItem1
  47. // | | +--\ subSubItem1
  48. // | | | +-- leaf1
  49. // | | +--\ subSubItem2
  50. // | | | +-- leaf2
  51. // | | +--\ subSubItem3
  52. // | | +-- leaf3
  53. // | +--\ subItem2
  54. // | | +--\ subSubItem1
  55. // | | | +-- leaf1
  56. // | | +--\ subSubItem2
  57. // | | | +-- leaf2
  58. // | | +--\ subSubItem3
  59. // | | +-- leaf3
  60. // | +--\ subItem3
  61. // | +--\ subSubItem1
  62. // | | +-- leaf1
  63. // | +--\ subSubItem2
  64. // | | +-- leaf2
  65. // | +--\ subSubItem3
  66. // | +-- leaf3
  67. // +--\ item2
  68. // | +--\ subItem1
  69. // | | +--\ subSubItem1
  70. // | | | +-- leaf1
  71. // | | +--\ subSubItem2
  72. // | | | +-- leaf2
  73. // | | +--\ subSubItem3
  74. // | | +-- leaf3
  75. // | +--\ subItem2
  76. // | | +--\ subSubItem1
  77. // | | | +-- leaf1
  78. // | | +--\ subSubItem2
  79. // | | | +-- leaf2
  80. // | | +--\ subSubItem3
  81. // | | +-- leaf3
  82. // | +--\ subItem3
  83. // | +--\ subSubItem1
  84. // | | +-- leaf1
  85. // | +--\ subSubItem2
  86. // | | +-- leaf2
  87. // | +--\ subSubItem3
  88. // | +-- leaf3
  89. // +--\ item3
  90. // +--\ subItem1
  91. // | +--\ subSubItem1
  92. // | | +-- leaf1
  93. // | +--\ subSubItem2
  94. // | | +-- leaf2
  95. // | +--\ subSubItem3
  96. // | +-- leaf3
  97. // +--\ subItem2
  98. // | +--\ subSubItem1
  99. // | | +-- leaf1
  100. // | +--\ subSubItem2
  101. // | | +-- leaf2
  102. // | +--\ subSubItem3
  103. // | +-- leaf3
  104. // +--\ subItem3
  105. // +--\ subSubItem1
  106. // | +-- leaf1
  107. // +--\ subSubItem2
  108. // | +-- leaf2
  109. // +--\ subSubItem3
  110. // +-- leaf3
  111. QMap<QString, QVariant> subSubModel;
  112. subSubModel["subSubItem1"] = QString("leaf1");
  113. subSubModel["subSubItem2"] = QString("leaf2");
  114. QMap<QString, QVariant> subModel;
  115. subModel["subItem1"] = subSubModel;
  116. subModel["subItem2"] = subSubModel;
  117. subModel["subItem3"] = subSubModel;
  118. QMap<QString, QVariant> model;
  119. model["item1"] = subModel;
  120. model["item2"] = subModel;
  121. model["item3"] = subModel;
  122. model["item4"] = subModel;
  123. // -\ top
  124. // +--\ subItem1 (item1)
  125. // | +--\ subSubItem1
  126. // | | +-- leaf1
  127. // | +--\ subSubItem2
  128. // | | +-- leaf2
  129. // | +--\ subSubItem3
  130. // | +-- leaf3
  131. // +--\ subItem2 (item1)
  132. // | +--\ subSubItem1
  133. // | | +-- leaf1
  134. // | +--\ subSubItem2
  135. // | | +-- leaf2
  136. // | +--\ subSubItem3
  137. // | +-- leaf3
  138. // +--\ subItem3 (item1)
  139. // | +--\ subSubItem1
  140. // | | +-- leaf1
  141. // | +--\ subSubItem2
  142. // | | +-- leaf2
  143. // | +--\ subSubItem3
  144. // | +-- leaf3
  145. // +--\ subItem1 (item2)
  146. // | +--\ subSubItem1
  147. // | | +-- leaf1
  148. // | +--\ subSubItem2
  149. // | | +-- leaf2
  150. // | +--\ subSubItem3
  151. // | +-- leaf3
  152. // +--\ subItem2 (item2)
  153. // | +--\ subSubItem1
  154. // | | +-- leaf1
  155. // | +--\ subSubItem2
  156. // | | +-- leaf2
  157. // | +--\ subSubItem3
  158. // | +-- leaf3
  159. // +--\ subItem3 (item2)
  160. // | +--\ subSubItem1
  161. // | | +-- leaf1
  162. // | +--\ subSubItem2
  163. // | | +-- leaf2
  164. // | +--\ subSubItem3
  165. // | +-- leaf3
  166. // +--\ subItem1 (item3)
  167. // | +--\ subSubItem1
  168. // | | +-- leaf1
  169. // | +--\ subSubItem2
  170. // | | +-- leaf2
  171. // | +--\ subSubItem3
  172. // | +-- leaf3
  173. // +--\ subItem2 (item3)
  174. // | +--\ subSubItem1
  175. // | | +-- leaf1
  176. // | +--\ subSubItem2
  177. // | | +-- leaf2
  178. // | +--\ subSubItem3
  179. // | +-- leaf3
  180. // +--\ subItem3 (item3)
  181. // +--\ subSubItem1
  182. // | +-- leaf1
  183. // +--\ subSubItem2
  184. // | +-- leaf2
  185. // +--\ subSubItem3
  186. // +-- leaf3
  187. QTest::newRow("flatten level 0") << QVariant(model) << -1 << 0 << 12 << 2;
  188. // Don't work yet.
  189. //QTest::newRow("flatten level 1") << QVariant(model) << -1<< 1 << 24 << 1;
  190. //QTest::newRow("flatten level 0, hide 2") << QVariant(model) << 0 << 0 << 12 << 2;
  191. //QTest::newRow("flatten level 1, hide 2") << QVariant(model) << 0 << 1 << 12 << 2;
  192. }
  193. // ----------------------------------------------------------------------------
  194. QStandardItem* ctkFlatProxyModelTester
  195. ::createItem(const QString& name, QVariant& children)const
  196. {
  197. QStandardItem* item = new QStandardItem(name);
  198. if (children.canConvert<QString>())
  199. {
  200. QStandardItem* leaf = new QStandardItem(children.toString());
  201. item->appendRow(leaf);
  202. return item;
  203. }
  204. QMap<QString, QVariant> hash = children.toMap();
  205. QMap<QString, QVariant>::iterator i = hash.begin();
  206. for ( ; i != hash.end(); ++i)
  207. {
  208. QStandardItem* childItem = this->createItem(i.key(), i.value());
  209. item->appendRow(childItem);
  210. }
  211. return item;
  212. }
  213. // ----------------------------------------------------------------------------
  214. void ctkFlatProxyModelTester::testModel()
  215. {
  216. QStandardItemModel model;
  217. QFETCH(QVariant, hashModel);
  218. QFETCH(int, endFlattenLevel);
  219. QFETCH(int, startHideLevel);
  220. QFETCH(int, level0ExpectedRowCount);
  221. QFETCH(int, level1ExpectedRowCount);
  222. QMap<QString, QVariant> hash = hashModel.toMap();
  223. QMap<QString, QVariant>::iterator i = hash.begin();
  224. for ( ; i != hash.end(); ++i)
  225. {
  226. QStandardItem* childItem = this->createItem(i.key(), i.value());
  227. model.appendRow(childItem);
  228. }
  229. ctkFlatProxyModel flattenModel;
  230. flattenModel.setEndFlattenLevel(endFlattenLevel);
  231. flattenModel.setHideLevel(startHideLevel);
  232. flattenModel.setSourceModel(&model);
  233. QCOMPARE( flattenModel.rowCount(QModelIndex()), level0ExpectedRowCount);
  234. QModelIndex subIndex1 = flattenModel.index(0,0, QModelIndex());
  235. QCOMPARE( flattenModel.rowCount(subIndex1), level1ExpectedRowCount);
  236. ctkModelTester tester;
  237. tester.setTestDataEnabled(false);
  238. tester.setModel(&flattenModel);
  239. /*
  240. QTreeView view(0);
  241. view.setModel(&flattenModel);
  242. view.show();
  243. QEventLoop eventLoop;
  244. eventLoop.exec();
  245. */
  246. }
  247. // ----------------------------------------------------------------------------
  248. CTK_TEST_MAIN(ctkFlatProxyModelTest)
  249. #include "moc_ctkFlatProxyModelTest.cpp"