ctkFlatProxyModel.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. // CTK includes
  17. #include "ctkFlatProxyModel.h"
  18. // ----------------------------------------------------------------------------
  19. class ctkFlatProxyModelPrivate
  20. {
  21. Q_DECLARE_PUBLIC(ctkFlatProxyModel);
  22. protected:
  23. ctkFlatProxyModel* const q_ptr;
  24. public:
  25. ctkFlatProxyModelPrivate(ctkFlatProxyModel& object);
  26. void init();
  27. int indexLevel(const QModelIndex& index)const;
  28. int levelRowCount(const QModelIndex& index)const;
  29. int nextLevelRowCount(const QModelIndex& index)const;
  30. int rowCount(const QModelIndex& sourceIndex, int depth = 0)const;
  31. QModelIndex sourceParent(const QModelIndex& index)const;
  32. QModelIndex grandChild(const QModelIndex& parent, int& row, int depth)const;
  33. int StartFlattenLevel;
  34. int EndFlattenLevel;
  35. int HideLevel;
  36. };
  37. // ----------------------------------------------------------------------------
  38. // Methods ctkFlatProxyModelPrivate
  39. // ----------------------------------------------------------------------------
  40. ctkFlatProxyModelPrivate::ctkFlatProxyModelPrivate(ctkFlatProxyModel &object)
  41. : q_ptr(&object)
  42. {
  43. this->StartFlattenLevel = -1;
  44. this->EndFlattenLevel = -1;
  45. this->HideLevel = -1;
  46. }
  47. // ----------------------------------------------------------------------------
  48. void ctkFlatProxyModelPrivate::init()
  49. {
  50. }
  51. // ----------------------------------------------------------------------------
  52. int ctkFlatProxyModelPrivate::indexLevel(const QModelIndex& index)const
  53. {
  54. int level = -1;
  55. QModelIndex parent = index;
  56. while (parent.isValid())
  57. {
  58. parent = parent.parent();
  59. ++level;
  60. }
  61. return level;
  62. }
  63. // ----------------------------------------------------------------------------
  64. int ctkFlatProxyModelPrivate::levelRowCount(const QModelIndex& sourceIndex)const
  65. {
  66. Q_Q(const ctkFlatProxyModel);
  67. if (!sourceIndex.isValid()
  68. || this->StartFlattenLevel > this->indexLevel(sourceIndex)
  69. || this->EndFlattenLevel < this->indexLevel(sourceIndex))
  70. {
  71. return 0;
  72. }
  73. int previousRows = 0;
  74. for (int row = 0; row != sourceIndex.row() ; ++row)
  75. {
  76. previousRows += q->sourceModel()->rowCount(sourceIndex.sibling(row, sourceIndex.column()));
  77. }
  78. return previousRows + this->levelRowCount(sourceIndex.parent());
  79. }
  80. // ----------------------------------------------------------------------------
  81. int ctkFlatProxyModelPrivate::nextLevelRowCount(const QModelIndex& sourceIndex)const
  82. {
  83. Q_Q(const ctkFlatProxyModel);
  84. if (!sourceIndex.isValid()
  85. || this->StartFlattenLevel > this->indexLevel(sourceIndex)
  86. || this->EndFlattenLevel < this->indexLevel(sourceIndex))
  87. {
  88. return q->sourceModel()->rowCount(sourceIndex);
  89. }
  90. int rowCount = 0;
  91. QModelIndex sibling = sourceIndex.sibling(0, sourceIndex.column());
  92. for (int row = 0; sibling.isValid() ; ++row)
  93. {
  94. sibling = sourceIndex.sibling(row, sourceIndex.column());
  95. rowCount += q->sourceModel()->rowCount(sibling);
  96. }
  97. return rowCount;
  98. }
  99. // ----------------------------------------------------------------------------
  100. int ctkFlatProxyModelPrivate::rowCount(const QModelIndex& sourceIndex, int depth)const
  101. {
  102. Q_Q(const ctkFlatProxyModel);
  103. int rows = 0;
  104. if (depth < 0)
  105. {
  106. rows = 1;
  107. }
  108. else
  109. {
  110. --depth;
  111. for (int row = 0; row < q->sourceModel()->rowCount(sourceIndex); ++row)
  112. {
  113. QModelIndex child = q->sourceModel()->index(row, 0, sourceIndex);
  114. rows += this->rowCount(child, depth);
  115. }
  116. }
  117. return rows;
  118. }
  119. // ----------------------------------------------------------------------------
  120. QModelIndex ctkFlatProxyModelPrivate
  121. ::sourceParent(const QModelIndex& index)const
  122. {
  123. Q_Q(const ctkFlatProxyModel);
  124. QModelIndexList sourceIndexes;
  125. sourceIndexes << QModelIndex();
  126. QMap<int, int> rowCountsPerLevel;
  127. while (!sourceIndexes.isEmpty())
  128. {
  129. QModelIndex sourceIndex = sourceIndexes.takeFirst();
  130. const int rowCount = q->sourceModel()->rowCount(sourceIndex);
  131. for (int row = 0; row < rowCount; ++row)
  132. {
  133. QModelIndex child = q->sourceModel()->index(row, 0, sourceIndex);
  134. if (child.internalPointer() == index.internalPointer())
  135. {
  136. return sourceIndex;
  137. }
  138. else
  139. {
  140. sourceIndexes << child;
  141. }
  142. }
  143. }
  144. Q_ASSERT(false);
  145. return QModelIndex();
  146. }
  147. // ----------------------------------------------------------------------------
  148. QModelIndex ctkFlatProxyModelPrivate
  149. ::grandChild(const QModelIndex& parent, int& row, int depth)const
  150. {
  151. Q_Q(const ctkFlatProxyModel);
  152. const int rowCount = q->sourceModel()->rowCount(parent);
  153. if (depth > 0)
  154. {
  155. for (int i = 0; i < rowCount; ++i)
  156. {
  157. QModelIndex child = q->sourceModel()->index(i, 0, parent);
  158. QModelIndex found = this->grandChild(child, row, depth - 1);
  159. if (found.isValid())
  160. {
  161. return found;
  162. }
  163. }
  164. }
  165. else
  166. {
  167. if (row < rowCount)
  168. {
  169. QModelIndex sourceIndex = q->sourceModel()->index(
  170. row, 0, parent);
  171. return sourceIndex;
  172. }
  173. else
  174. {
  175. row -= rowCount;
  176. }
  177. }
  178. return QModelIndex();
  179. }
  180. // ----------------------------------------------------------------------------
  181. // Methods ctkFlatProxyModel
  182. // ----------------------------------------------------------------------------
  183. ctkFlatProxyModel::ctkFlatProxyModel(QObject *parentObject)
  184. : Superclass(parentObject)
  185. , d_ptr(new ctkFlatProxyModelPrivate(*this))
  186. {
  187. Q_D(ctkFlatProxyModel);
  188. d->init();
  189. }
  190. // ----------------------------------------------------------------------------
  191. ctkFlatProxyModel::~ctkFlatProxyModel()
  192. {
  193. }
  194. // ----------------------------------------------------------------------------
  195. int ctkFlatProxyModel::startFlattenLevel() const
  196. {
  197. Q_D(const ctkFlatProxyModel);
  198. return d->StartFlattenLevel;
  199. }
  200. // ----------------------------------------------------------------------------
  201. void ctkFlatProxyModel::setStartFlattenLevel(int level)
  202. {
  203. Q_D(ctkFlatProxyModel);
  204. d->StartFlattenLevel = level;
  205. Q_ASSERT( d->StartFlattenLevel <= d->EndFlattenLevel);
  206. }
  207. // ----------------------------------------------------------------------------
  208. int ctkFlatProxyModel::endFlattenLevel() const
  209. {
  210. Q_D(const ctkFlatProxyModel);
  211. return d->EndFlattenLevel;
  212. }
  213. // ----------------------------------------------------------------------------
  214. void ctkFlatProxyModel::setEndFlattenLevel(int level)
  215. {
  216. Q_D(ctkFlatProxyModel);
  217. d->EndFlattenLevel = level;
  218. Q_ASSERT( d->EndFlattenLevel >= d->EndFlattenLevel);
  219. }
  220. // ----------------------------------------------------------------------------
  221. int ctkFlatProxyModel::hideLevel()const
  222. {
  223. Q_D(const ctkFlatProxyModel);
  224. return d->HideLevel;
  225. }
  226. // ----------------------------------------------------------------------------
  227. void ctkFlatProxyModel::setHideLevel(int level)
  228. {
  229. Q_D(ctkFlatProxyModel);
  230. d->HideLevel = level;
  231. }
  232. // ----------------------------------------------------------------------------
  233. QModelIndex ctkFlatProxyModel::mapFromSource( const QModelIndex& sourceIndex ) const
  234. {
  235. Q_D(const ctkFlatProxyModel);
  236. if (!sourceIndex.isValid())
  237. {
  238. return QModelIndex();
  239. }
  240. int level = d->indexLevel(sourceIndex);
  241. if (d->HideLevel != -1
  242. && level >= d->HideLevel)
  243. {
  244. return QModelIndex();
  245. }
  246. if (d->EndFlattenLevel != -1
  247. && level <= d->EndFlattenLevel)
  248. {
  249. return QModelIndex();
  250. }
  251. int row = sourceIndex.row();
  252. if (d->EndFlattenLevel != -1)
  253. {
  254. row += d->levelRowCount(sourceIndex.parent());
  255. }
  256. return this->createIndex(row, sourceIndex.column(),
  257. sourceIndex.internalPointer());
  258. }
  259. // ----------------------------------------------------------------------------
  260. QModelIndex ctkFlatProxyModel::mapToSource( const QModelIndex& proxyIndex ) const
  261. {
  262. Q_D(const ctkFlatProxyModel);
  263. if (!proxyIndex.isValid())
  264. {
  265. return QModelIndex();
  266. }
  267. QModelIndex sourceParent = d->sourceParent(proxyIndex);
  268. int level = d->indexLevel(sourceParent);
  269. int levelRowCount = 0;
  270. if ((d->StartFlattenLevel != -1 || d->EndFlattenLevel != -1) &&
  271. (d->StartFlattenLevel != -1 || level >= d->StartFlattenLevel) &&
  272. (d->EndFlattenLevel != -1 || level <= d->EndFlattenLevel))
  273. {
  274. levelRowCount = d->levelRowCount(sourceParent);
  275. }
  276. QModelIndex sourceIndex = this->sourceModel()->index(
  277. proxyIndex.row() - levelRowCount, proxyIndex.column(), sourceParent);
  278. Q_ASSERT(sourceIndex.isValid());
  279. return sourceIndex;
  280. }
  281. // ----------------------------------------------------------------------------
  282. QModelIndex ctkFlatProxyModel::index(int row, int column, const QModelIndex &parent) const
  283. {
  284. Q_D(const ctkFlatProxyModel);
  285. if (row < 0 || column < 0)
  286. {
  287. return QModelIndex();
  288. }
  289. QModelIndex sourceParent = this->mapToSource(parent); // parent is already mapped at this point
  290. int sourceRow = row;
  291. QModelIndex sourceGrandChild = d->grandChild(
  292. sourceParent, sourceRow, qMax(0, d->EndFlattenLevel - d->indexLevel(sourceParent)));
  293. return this->createIndex(row, column, sourceGrandChild.internalPointer());
  294. }
  295. // ----------------------------------------------------------------------------
  296. QModelIndex ctkFlatProxyModel::parent(const QModelIndex &child) const
  297. {
  298. if (!child.isValid())
  299. {
  300. return QModelIndex();
  301. }
  302. QModelIndex sourceChild = this->mapToSource(child);
  303. QModelIndex sourceParent = sourceChild.parent();
  304. QModelIndex proxyParent = this->mapFromSource(sourceParent);
  305. return proxyParent;
  306. }
  307. // ----------------------------------------------------------------------------
  308. int ctkFlatProxyModel::rowCount(const QModelIndex &parent) const
  309. {
  310. Q_D(const ctkFlatProxyModel);
  311. QModelIndex sourceParent = this->mapToSource(parent);
  312. int sourceParentLevel = d->indexLevel(sourceParent);
  313. int depth = 0;
  314. if (sourceParentLevel >= d->StartFlattenLevel &&
  315. sourceParentLevel <= d->EndFlattenLevel)
  316. {
  317. depth = d->EndFlattenLevel - d->StartFlattenLevel;
  318. }
  319. return d->rowCount(sourceParent, depth);
  320. }
  321. // ----------------------------------------------------------------------------
  322. int ctkFlatProxyModel::columnCount(const QModelIndex &parent) const
  323. {
  324. QModelIndex proxyChild = this->index(0, 0, parent);
  325. if (parent.isValid() && !proxyChild.internalPointer())
  326. {
  327. Q_ASSERT(!parent.isValid() || proxyChild.internalPointer());
  328. }
  329. QModelIndex sourceChild = this->mapToSource(proxyChild);
  330. QModelIndex sourceParent = sourceChild.parent();
  331. return this->sourceModel()->columnCount(sourceParent);
  332. }