ctkFlatProxyModel.cpp 11 KB

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