• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • itemviews
kcategorizedview.cpp
Go to the documentation of this file.
1 
32 #include "kcategorizedview.h"
33 #include "kcategorizedview_p.h"
34 
35 #include <math.h> // trunc on C99 compliant systems
36 #include <kdefakes.h> // trunc for not C99 compliant systems
37 
38 #include <QPainter>
39 #include <QScrollBar>
40 #include <QPaintEvent>
41 
42 #include "kcategorydrawer.h"
43 #include "kcategorizedsortfilterproxymodel.h"
44 
45 //BEGIN: Private part
46 
47 struct KCategorizedView::Private::Item
48 {
49  Item()
50  : topLeft(QPoint())
51  , size(QSize())
52  {
53  }
54 
55  QPoint topLeft;
56  QSize size;
57 };
58 
59 struct KCategorizedView::Private::Block
60 {
61  Block()
62  : topLeft(QPoint())
63  , height(-1)
64  , firstIndex(QModelIndex())
65  , quarantineStart(QModelIndex())
66  , items(QList<Item>())
67  , outOfQuarantine(false)
68  , alternate(false)
69  , collapsed(false)
70  {
71  }
72 
73  bool operator!=(const Block &rhs) const
74  {
75  return firstIndex != rhs.firstIndex;
76  }
77 
78  static bool lessThan(const Block &left, const Block &right)
79  {
80  Q_ASSERT(left.firstIndex.isValid());
81  Q_ASSERT(right.firstIndex.isValid());
82  return left.firstIndex.row() < right.firstIndex.row();
83  }
84 
85  QPoint topLeft;
86  int height;
87  QPersistentModelIndex firstIndex;
88  // if we have n elements on this block, and we inserted an element at position i. The quarantine
89  // will start at index (i, column, parent). This means that for all elements j where i <= j <= n, the
90  // visual rect position of item j will have to be recomputed (cannot use the cached point). The quarantine
91  // will only affect the current block, since the rest of blocks can be affected only in the way
92  // that the whole block will have different offset, but items will keep the same relative position
93  // in terms of their parent blocks.
94  QPersistentModelIndex quarantineStart;
95  QList<Item> items;
96 
97  // this affects the whole block, not items separately. items contain the topLeft point relative
98  // to the block. Because of insertions or removals a whole block can be moved, so the whole block
99  // will enter in quarantine, what is faster than moving all items in absolute terms.
100  bool outOfQuarantine;
101 
102  // should we alternate its color ? is just a hint, could not be used
103  bool alternate;
104  bool collapsed;
105 };
106 
107 KCategorizedView::Private::Private(KCategorizedView *q)
108  : q(q)
109  , proxyModel(0)
110  , categoryDrawer(0)
111  , categoryDrawerV2(0)
112  , categoryDrawerV3(0)
113  , categorySpacing(5)
114  , alternatingBlockColors(false)
115  , collapsibleBlocks(false)
116  , hoveredBlock(new Block())
117  , hoveredIndex(QModelIndex())
118  , pressedPosition(QPoint())
119  , rubberBandRect(QRect())
120 {
121 }
122 
123 KCategorizedView::Private::~Private()
124 {
125  delete hoveredBlock;
126 }
127 
128 bool KCategorizedView::Private::isCategorized() const
129 {
130  return proxyModel && categoryDrawer && proxyModel->isCategorizedModel();
131 }
132 
133 QStyleOptionViewItemV4 KCategorizedView::Private::blockRect(const QModelIndex &representative)
134 {
135  QStyleOptionViewItemV4 option(q->viewOptions());
136  const int height = categoryDrawer->categoryHeight(representative, option);
137  const QString categoryDisplay = representative.data(KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
138  QPoint pos = blockPosition(categoryDisplay);
139  pos.ry() -= height;
140  option.rect.setTopLeft(pos);
141  option.rect.setWidth(viewportWidth() + categoryDrawer->leftMargin() + categoryDrawer->rightMargin());
142  option.rect.setHeight(height + blockHeight(categoryDisplay));
143  option.rect = mapToViewport(option.rect);
144 
145  return option;
146 }
147 
148 QPair<QModelIndex, QModelIndex> KCategorizedView::Private::intersectingIndexesWithRect(const QRect &_rect) const
149 {
150  const int rowCount = proxyModel->rowCount();
151 
152  const QRect rect = _rect.normalized();
153 
154  // binary search to find out the top border
155  int bottom = 0;
156  int top = rowCount - 1;
157  while (bottom <= top) {
158  const int middle = (bottom + top) / 2;
159  const QModelIndex index = proxyModel->index(middle, q->modelColumn(), q->rootIndex());
160  const QRect itemRect = q->visualRect(index);
161  if (itemRect.bottomRight().y() <= rect.topLeft().y()) {
162  bottom = middle + 1;
163  } else {
164  top = middle - 1;
165  }
166  }
167 
168  const QModelIndex bottomIndex = proxyModel->index(bottom, q->modelColumn(), q->rootIndex());
169 
170  // binary search to find out the bottom border
171  bottom = 0;
172  top = rowCount - 1;
173  while (bottom <= top) {
174  const int middle = (bottom + top) / 2;
175  const QModelIndex index = proxyModel->index(middle, q->modelColumn(), q->rootIndex());
176  const QRect itemRect = q->visualRect(index);
177  if (itemRect.topLeft().y() <= rect.bottomRight().y()) {
178  bottom = middle + 1;
179  } else {
180  top = middle - 1;
181  }
182  }
183 
184  const QModelIndex topIndex = proxyModel->index(top, q->modelColumn(), q->rootIndex());
185 
186  return qMakePair(bottomIndex, topIndex);
187 }
188 
189 QPoint KCategorizedView::Private::blockPosition(const QString &category)
190 {
191  Block &block = blocks[category];
192 
193  if (block.outOfQuarantine && !block.topLeft.isNull()) {
194  return block.topLeft;
195  }
196 
197  QPoint res(categorySpacing, 0);
198 
199  const QModelIndex index = block.firstIndex;
200 
201  for (QHash<QString, Private::Block>::Iterator it = blocks.begin(); it != blocks.end(); ++it) {
202  Block &block = *it;
203  const QModelIndex categoryIndex = block.firstIndex;
204  if (index.row() < categoryIndex.row()) {
205  continue;
206  }
207  res.ry() += categoryDrawer->categoryHeight(categoryIndex, q->viewOptions()) + categorySpacing;
208  if (index.row() == categoryIndex.row()) {
209  continue;
210  }
211  res.ry() += blockHeight(it.key());
212  }
213 
214  block.outOfQuarantine = true;
215  block.topLeft = res;
216 
217  return res;
218 }
219 
220 int KCategorizedView::Private::blockHeight(const QString &category)
221 {
222  Block &block = blocks[category];
223 
224  if (block.collapsed) {
225  return 0;
226  }
227 
228  if (block.height > -1) {
229  return block.height;
230  }
231 
232  const QModelIndex firstIndex = block.firstIndex;
233  const QModelIndex lastIndex = proxyModel->index(firstIndex.row() + block.items.count() - 1, q->modelColumn(), q->rootIndex());
234  const QRect topLeft = q->visualRect(firstIndex);
235  QRect bottomRight = q->visualRect(lastIndex);
236 
237  if (hasGrid()) {
238  bottomRight.setHeight(qMax(bottomRight.height(), q->gridSize().height()));
239  } else {
240  if (!q->uniformItemSizes()) {
241  bottomRight.setHeight(highestElementInLastRow(block) + q->spacing() * 2);
242  }
243  }
244 
245  const int height = bottomRight.bottomRight().y() - topLeft.topLeft().y() + 1;
246  block.height = height;
247 
248  return height;
249 }
250 
251 int KCategorizedView::Private::viewportWidth() const
252 {
253  return q->viewport()->width() - categorySpacing * 2 - categoryDrawer->leftMargin() - categoryDrawer->rightMargin();
254 }
255 
256 void KCategorizedView::Private::regenerateAllElements()
257 {
258  for (QHash<QString, Block>::Iterator it = blocks.begin(); it != blocks.end(); ++it) {
259  Block &block = *it;
260  block.outOfQuarantine = false;
261  block.quarantineStart = block.firstIndex;
262  block.height = -1;
263  }
264 }
265 
266 void KCategorizedView::Private::rowsInserted(const QModelIndex &parent, int start, int end)
267 {
268  if (!isCategorized()) {
269  return;
270  }
271 
272  for (int i = start; i <= end; ++i) {
273  const QModelIndex index = proxyModel->index(i, q->modelColumn(), parent);
274 
275  Q_ASSERT(index.isValid());
276 
277  const QString category = categoryForIndex(index);
278 
279  Block &block = blocks[category];
280 
281  //BEGIN: update firstIndex
282  // save as firstIndex in block if
283  // - it forced the category creation (first element on this category)
284  // - it is before the first row on that category
285  const QModelIndex firstIndex = block.firstIndex;
286  if (!firstIndex.isValid() || index.row() < firstIndex.row()) {
287  block.firstIndex = index;
288  }
289  //END: update firstIndex
290 
291  Q_ASSERT(block.firstIndex.isValid());
292 
293  const int firstIndexRow = block.firstIndex.row();
294 
295  block.items.insert(index.row() - firstIndexRow, Private::Item());
296  block.height = -1;
297 
298  q->visualRect(index);
299  q->viewport()->update();
300  }
301 
302  //BEGIN: update the items that are in quarantine in affected categories
303  {
304  const QModelIndex lastIndex = proxyModel->index(end, q->modelColumn(), parent);
305  const QString category = categoryForIndex(lastIndex);
306  Private::Block &block = blocks[category];
307  block.quarantineStart = block.firstIndex;
308  }
309  //END: update the items that are in quarantine in affected categories
310 
311  //BEGIN: mark as in quarantine those categories that are under the affected ones
312  {
313  const QModelIndex firstIndex = proxyModel->index(start, q->modelColumn(), parent);
314  const QString category = categoryForIndex(firstIndex);
315  const QModelIndex firstAffectedCategory = blocks[category].firstIndex;
316  //BEGIN: order for marking as alternate those blocks that are alternate
317  QList<Block> blockList = blocks.values();
318  qSort(blockList.begin(), blockList.end(), Block::lessThan);
319  QList<int> firstIndexesRows;
320  foreach (const Block &block, blockList) {
321  firstIndexesRows << block.firstIndex.row();
322  }
323  //END: order for marking as alternate those blocks that are alternate
324  for (QHash<QString, Private::Block>::Iterator it = blocks.begin(); it != blocks.end(); ++it) {
325  Private::Block &block = *it;
326  if (block.firstIndex.row() > firstAffectedCategory.row()) {
327  block.outOfQuarantine = false;
328  block.alternate = firstIndexesRows.indexOf(block.firstIndex.row()) % 2;
329  } else if (block.firstIndex.row() == firstAffectedCategory.row()) {
330  block.alternate = firstIndexesRows.indexOf(block.firstIndex.row()) % 2;
331  }
332  }
333  }
334  //END: mark as in quarantine those categories that are under the affected ones
335 }
336 
337 QRect KCategorizedView::Private::mapToViewport(const QRect &rect) const
338 {
339  const int dx = -q->horizontalOffset();
340  const int dy = -q->verticalOffset();
341  return rect.adjusted(dx, dy, dx, dy);
342 }
343 
344 QRect KCategorizedView::Private::mapFromViewport(const QRect &rect) const
345 {
346  const int dx = q->horizontalOffset();
347  const int dy = q->verticalOffset();
348  return rect.adjusted(dx, dy, dx, dy);
349 }
350 
351 int KCategorizedView::Private::highestElementInLastRow(const Block &block) const
352 {
353  //Find the highest element in the last row
354  const QModelIndex lastIndex = proxyModel->index(block.firstIndex.row() + block.items.count() - 1, q->modelColumn(), q->rootIndex());
355  const QRect prevRect = q->visualRect(lastIndex);
356  int res = prevRect.height();
357  QModelIndex prevIndex = proxyModel->index(lastIndex.row() - 1, q->modelColumn(), q->rootIndex());
358  if (!prevIndex.isValid()) {
359  return res;
360  }
361  Q_FOREVER {
362  const QRect tempRect = q->visualRect(prevIndex);
363  if (tempRect.topLeft().y() < prevRect.topLeft().y()) {
364  break;
365  }
366  res = qMax(res, tempRect.height());
367  if (prevIndex == block.firstIndex) {
368  break;
369  }
370  prevIndex = proxyModel->index(prevIndex.row() - 1, q->modelColumn(), q->rootIndex());
371  }
372 
373  return res;
374 }
375 
376 bool KCategorizedView::Private::hasGrid() const
377 {
378  const QSize gridSize = q->gridSize();
379  return gridSize.isValid() && !gridSize.isNull();
380 }
381 
382 QString KCategorizedView::Private::categoryForIndex(const QModelIndex &index) const
383 {
384  const QModelIndex categoryIndex = index.model()->index(index.row(), proxyModel->sortColumn(), index.parent());
385  return categoryIndex.data(KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
386 }
387 
388 void KCategorizedView::Private::leftToRightVisualRect(const QModelIndex &index, Item &item,
389  const Block &block, const QPoint &blockPos) const
390 {
391  const int firstIndexRow = block.firstIndex.row();
392 
393  if (hasGrid()) {
394  const int relativeRow = index.row() - firstIndexRow;
395  const int maxItemsPerRow = qMax(viewportWidth() / q->gridSize().width(), 1);
396  if (q->layoutDirection() == Qt::LeftToRight) {
397  item.topLeft.rx() = (relativeRow % maxItemsPerRow) * q->gridSize().width() + blockPos.x() + categoryDrawer->leftMargin();
398  } else {
399  item.topLeft.rx() = viewportWidth() - ((relativeRow % maxItemsPerRow) + 1) * q->gridSize().width() + categoryDrawer->leftMargin() + categorySpacing;
400  }
401  item.topLeft.ry() = (relativeRow / maxItemsPerRow) * q->gridSize().height();
402  } else {
403  if (q->uniformItemSizes()) {
404  const int relativeRow = index.row() - firstIndexRow;
405  const QSize itemSize = q->sizeHintForIndex(index);
406  const int maxItemsPerRow = qMax((viewportWidth() - q->spacing()) / (itemSize.width() + q->spacing()), 1);
407  if (q->layoutDirection() == Qt::LeftToRight) {
408  item.topLeft.rx() = (relativeRow % maxItemsPerRow) * itemSize.width() + blockPos.x() + categoryDrawer->leftMargin();
409  } else {
410  item.topLeft.rx() = viewportWidth() - (relativeRow % maxItemsPerRow) * itemSize.width() + categoryDrawer->leftMargin() + categorySpacing;
411  }
412  item.topLeft.ry() = (relativeRow / maxItemsPerRow) * itemSize.height();
413  } else {
414  const QSize currSize = q->sizeHintForIndex(index);
415  if (index != block.firstIndex) {
416  const int viewportW = viewportWidth() - q->spacing();
417  QModelIndex prevIndex = proxyModel->index(index.row() - 1, q->modelColumn(), q->rootIndex());
418  QRect prevRect = q->visualRect(prevIndex);
419  prevRect = mapFromViewport(prevRect);
420  if ((prevRect.bottomRight().x() + 1) + currSize.width() - blockPos.x() + q->spacing() > viewportW) {
421  // we have to check the whole previous row, and see which one was the
422  // highest.
423  Q_FOREVER {
424  prevIndex = proxyModel->index(prevIndex.row() - 1, q->modelColumn(), q->rootIndex());
425  const QRect tempRect = q->visualRect(prevIndex);
426  if (tempRect.topLeft().y() < prevRect.topLeft().y()) {
427  break;
428  }
429  if (tempRect.bottomRight().y() > prevRect.bottomRight().y()) {
430  prevRect = tempRect;
431  }
432  if (prevIndex == block.firstIndex) {
433  break;
434  }
435  }
436  if (q->layoutDirection() == Qt::LeftToRight) {
437  item.topLeft.rx() = categoryDrawer->leftMargin() + blockPos.x() + q->spacing();
438  } else {
439  item.topLeft.rx() = viewportWidth() - currSize.width() + categoryDrawer->leftMargin() + categorySpacing;
440  }
441  item.topLeft.ry() = (prevRect.bottomRight().y() + 1) + q->spacing() - blockPos.y();
442  } else {
443  if (q->layoutDirection() == Qt::LeftToRight) {
444  item.topLeft.rx() = (prevRect.bottomRight().x() + 1) + q->spacing();
445  } else {
446  item.topLeft.rx() = (prevRect.bottomLeft().x() - 1) - q->spacing() - item.size.width() + categoryDrawer->leftMargin() + categorySpacing;
447  }
448  item.topLeft.ry() = prevRect.topLeft().y() - blockPos.y();
449  }
450  } else {
451  if (q->layoutDirection() == Qt::LeftToRight) {
452  item.topLeft.rx() = blockPos.x() + categoryDrawer->leftMargin() + q->spacing();
453  } else {
454  item.topLeft.rx() = viewportWidth() - currSize.width() + categoryDrawer->leftMargin() + categorySpacing;
455  }
456  item.topLeft.ry() = q->spacing();
457  }
458  }
459  }
460  item.size = q->sizeHintForIndex(index);
461 }
462 
463 void KCategorizedView::Private::topToBottomVisualRect(const QModelIndex &index, Item &item,
464  const Block &block, const QPoint &blockPos) const
465 {
466  const int firstIndexRow = block.firstIndex.row();
467 
468  if (hasGrid()) {
469  const int relativeRow = index.row() - firstIndexRow;
470  item.topLeft.rx() = blockPos.x() + categoryDrawer->leftMargin();
471  item.topLeft.ry() = relativeRow * q->gridSize().height();
472  } else {
473  if (q->uniformItemSizes()) {
474  const int relativeRow = index.row() - firstIndexRow;
475  const QSize itemSize = q->sizeHintForIndex(index);
476  item.topLeft.rx() = blockPos.x() + categoryDrawer->leftMargin();
477  item.topLeft.ry() = relativeRow * itemSize.height();
478  } else {
479  if (index != block.firstIndex) {
480  QModelIndex prevIndex = proxyModel->index(index.row() - 1, q->modelColumn(), q->rootIndex());
481  QRect prevRect = q->visualRect(prevIndex);
482  prevRect = mapFromViewport(prevRect);
483  item.topLeft.rx() = blockPos.x() + categoryDrawer->leftMargin() + q->spacing();
484  item.topLeft.ry() = (prevRect.bottomRight().y() + 1) + q->spacing() - blockPos.y();
485  } else {
486  item.topLeft.rx() = blockPos.x() + categoryDrawer->leftMargin() + q->spacing();
487  item.topLeft.ry() = q->spacing();
488  }
489  }
490  }
491  item.size = q->sizeHintForIndex(index);
492  item.size.setWidth(viewportWidth());
493 }
494 
495 void KCategorizedView::Private::_k_slotCollapseOrExpandClicked(QModelIndex)
496 {
497 }
498 
499 //END: Private part
500 
501 //BEGIN: Public part
502 
503 KCategorizedView::KCategorizedView(QWidget *parent)
504  : QListView(parent)
505  , d(new Private(this))
506 {
507 }
508 
509 KCategorizedView::~KCategorizedView()
510 {
511  delete d;
512 }
513 
514 void KCategorizedView::setModel(QAbstractItemModel *model)
515 {
516  if (d->proxyModel == model) {
517  return;
518  }
519 
520  d->blocks.clear();
521 
522  if (d->proxyModel) {
523  disconnect(d->proxyModel, SIGNAL(layoutChanged()), this, SLOT(slotLayoutChanged()));
524  }
525 
526  d->proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model);
527 
528  if (d->proxyModel) {
529  connect(d->proxyModel, SIGNAL(layoutChanged()), this, SLOT(slotLayoutChanged()));
530  }
531 
532  QListView::setModel(model);
533 
534  // if the model already had information inserted, update our data structures to it
535  if (model->rowCount()) {
536  slotLayoutChanged();
537  }
538 }
539 
540 void KCategorizedView::setGridSize(const QSize &size)
541 {
542  setGridSizeOwn(size);
543 }
544 
545 void KCategorizedView::setGridSizeOwn(const QSize &size)
546 {
547  d->regenerateAllElements();
548  QListView::setGridSize(size);
549 }
550 
551 QRect KCategorizedView::visualRect(const QModelIndex &index) const
552 {
553  if (!d->isCategorized()) {
554  return QListView::visualRect(index);
555  }
556 
557  if (!index.isValid()) {
558  return QRect();
559  }
560 
561  const QString category = d->categoryForIndex(index);
562 
563  if (!d->blocks.contains(category)) {
564  return QRect();
565  }
566 
567  Private::Block &block = d->blocks[category];
568  const int firstIndexRow = block.firstIndex.row();
569 
570  Q_ASSERT(block.firstIndex.isValid());
571 
572  if (index.row() - firstIndexRow < 0 || index.row() - firstIndexRow >= block.items.count()) {
573  return QRect();
574  }
575 
576  const QPoint blockPos = d->blockPosition(category);
577 
578  Private::Item &ritem = block.items[index.row() - firstIndexRow];
579 
580  if (ritem.topLeft.isNull() || (block.quarantineStart.isValid() &&
581  index.row() >= block.quarantineStart.row())) {
582  if (flow() == LeftToRight) {
583  d->leftToRightVisualRect(index, ritem, block, blockPos);
584  } else {
585  d->topToBottomVisualRect(index, ritem, block, blockPos);
586  }
587 
588  //BEGIN: update the quarantine start
589  const bool wasLastIndex = (index.row() == (block.firstIndex.row() + block.items.count() - 1));
590  if (index.row() == block.quarantineStart.row()) {
591  if (wasLastIndex) {
592  block.quarantineStart = QModelIndex();
593  } else {
594  const QModelIndex nextIndex = d->proxyModel->index(index.row() + 1, modelColumn(), rootIndex());
595  block.quarantineStart = nextIndex;
596  }
597  }
598  //END: update the quarantine start
599  }
600 
601  // we get now the absolute position through the relative position of the parent block. do not
602  // save this on ritem, since this would override the item relative position in block terms.
603  Private::Item item(ritem);
604  item.topLeft.ry() += blockPos.y();
605 
606  const QSize sizeHint = item.size;
607 
608  if (d->hasGrid()) {
609  const QSize sizeGrid = gridSize();
610  const QSize resultingSize = sizeHint.boundedTo(sizeGrid);
611  QRect res(item.topLeft.x() + ((sizeGrid.width() - resultingSize.width()) / 2),
612  item.topLeft.y(), resultingSize.width(), resultingSize.height());
613  if (block.collapsed) {
614  // we can still do binary search, while we "hide" items. We move those items in collapsed
615  // blocks to the left and set a 0 height.
616  res.setLeft(-resultingSize.width());
617  res.setHeight(0);
618  }
619  return d->mapToViewport(res);
620  }
621 
622  QRect res(item.topLeft.x(), item.topLeft.y(), sizeHint.width(), sizeHint.height());
623  if (block.collapsed) {
624  // we can still do binary search, while we "hide" items. We move those items in collapsed
625  // blocks to the left and set a 0 height.
626  res.setLeft(-sizeHint.width());
627  res.setHeight(0);
628  }
629  return d->mapToViewport(res);
630 }
631 
632 KCategoryDrawer *KCategorizedView::categoryDrawer() const
633 {
634  return d->categoryDrawer;
635 }
636 
637 void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer)
638 {
639  if (d->categoryDrawerV2) {
640  disconnect(d->categoryDrawerV2, SIGNAL(collapseOrExpandClicked(QModelIndex)),
641  this, SLOT(_k_slotCollapseOrExpandClicked(QModelIndex)));
642  }
643 
644  d->categoryDrawer = categoryDrawer;
645  d->categoryDrawerV2 = dynamic_cast<KCategoryDrawerV2*>(categoryDrawer);
646  d->categoryDrawerV3 = dynamic_cast<KCategoryDrawerV3*>(categoryDrawer);
647 
648  if (d->categoryDrawerV2) {
649  connect(d->categoryDrawerV2, SIGNAL(collapseOrExpandClicked(QModelIndex)),
650  this, SLOT(_k_slotCollapseOrExpandClicked(QModelIndex)));
651  }
652 }
653 
654 int KCategorizedView::categorySpacing() const
655 {
656  return d->categorySpacing;
657 }
658 
659 void KCategorizedView::setCategorySpacing(int categorySpacing)
660 {
661  if (d->categorySpacing == categorySpacing) {
662  return;
663  }
664 
665  d->categorySpacing = categorySpacing;
666 
667  for (QHash<QString, Private::Block>::Iterator it = d->blocks.begin(); it != d->blocks.end(); ++it) {
668  Private::Block &block = *it;
669  block.outOfQuarantine = false;
670  }
671 }
672 
673 bool KCategorizedView::alternatingBlockColors() const
674 {
675  return d->alternatingBlockColors;
676 }
677 
678 void KCategorizedView::setAlternatingBlockColors(bool enable)
679 {
680  d->alternatingBlockColors = enable;
681 }
682 
683 bool KCategorizedView::collapsibleBlocks() const
684 {
685  return d->collapsibleBlocks;
686 }
687 
688 void KCategorizedView::setCollapsibleBlocks(bool enable)
689 {
690  d->collapsibleBlocks = enable;
691 }
692 
693 QModelIndexList KCategorizedView::block(const QString &category)
694 {
695  QModelIndexList res;
696  const Private::Block &block = d->blocks[category];
697  if (block.height == -1) {
698  return res;
699  }
700  QModelIndex current = block.firstIndex;
701  const int first = current.row();
702  for (int i = 1; i <= block.items.count(); ++i) {
703  if (current.isValid()) {
704  res << current;
705  }
706  current = d->proxyModel->index(first + i, modelColumn(), rootIndex());
707  }
708  return res;
709 }
710 
711 QModelIndexList KCategorizedView::block(const QModelIndex &representative)
712 {
713  return block(representative.data(KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString());
714 }
715 
716 QModelIndex KCategorizedView::indexAt(const QPoint &point) const
717 {
718  if (!d->isCategorized()) {
719  return QListView::indexAt(point);
720  }
721 
722  const int rowCount = d->proxyModel->rowCount();
723  if (!rowCount) {
724  return QModelIndex();
725  }
726 
727  // Binary search that will try to spot if there is an index under point
728  int bottom = 0;
729  int top = rowCount - 1;
730  while (bottom <= top) {
731  const int middle = (bottom + top) / 2;
732  const QModelIndex index = d->proxyModel->index(middle, modelColumn(), rootIndex());
733  QRect rect = visualRect(index);
734  const int verticalOff = verticalOffset();
735  int horizontalOff = horizontalOffset();
736  if (layoutDirection() == Qt::RightToLeft) {
737  horizontalOff *= -1;
738  }
739  rect.topLeft().ry() += verticalOff;
740  rect.topLeft().rx() += horizontalOff;
741  rect.bottomRight().ry() += verticalOff;
742  rect.bottomRight().rx() += horizontalOff;
743  if (rect.contains(point)) {
744  if (index.model()->flags(index) & Qt::ItemIsEnabled) {
745  return index;
746  }
747  return QModelIndex();
748  }
749  bool directionCondition;
750  if (layoutDirection() == Qt::LeftToRight) {
751  directionCondition = point.x() > rect.bottomRight().x();
752  } else {
753  directionCondition = point.x() < rect.bottomLeft().x();
754  }
755  if (point.y() > rect.bottomRight().y() ||
756  (point.y() > rect.topLeft().y() && point.y() < rect.bottomRight().y() && directionCondition)) {
757  bottom = middle + 1;
758  } else {
759  top = middle - 1;
760  }
761  }
762  return QModelIndex();
763 }
764 
765 void KCategorizedView::reset()
766 {
767  d->blocks.clear();
768  QListView::reset();
769 }
770 
771 void KCategorizedView::paintEvent(QPaintEvent *event)
772 {
773  if (!d->isCategorized()) {
774  QListView::paintEvent(event);
775  return;
776  }
777 
778  const QPair<QModelIndex, QModelIndex> intersecting = d->intersectingIndexesWithRect(viewport()->rect().intersected(event->rect()));
779 
780  QPainter p(viewport());
781  p.save();
782 
783  Q_ASSERT(selectionModel()->model() == d->proxyModel);
784 
785  //BEGIN: draw categories
786  QHash<QString, Private::Block>::ConstIterator it(d->blocks.constBegin());
787  while (it != d->blocks.constEnd()) {
788  const Private::Block &block = *it;
789  const QModelIndex categoryIndex = d->proxyModel->index(block.firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
790  QStyleOptionViewItemV4 option(viewOptions());
791  option.features |= d->alternatingBlockColors && block.alternate ? QStyleOptionViewItemV4::Alternate
792  : QStyleOptionViewItemV4::None;
793  option.state |= !d->collapsibleBlocks || !block.collapsed ? QStyle::State_Open
794  : QStyle::State_None;
795  const int height = d->categoryDrawer->categoryHeight(categoryIndex, option);
796  QPoint pos = d->blockPosition(it.key());
797  pos.ry() -= height;
798  option.rect.setTopLeft(pos);
799  option.rect.setWidth(d->viewportWidth() + d->categoryDrawer->leftMargin() + d->categoryDrawer->rightMargin());
800  option.rect.setHeight(height + d->blockHeight(it.key()));
801  option.rect = d->mapToViewport(option.rect);
802  if (!option.rect.intersects(viewport()->rect())) {
803  ++it;
804  continue;
805  }
806  d->categoryDrawer->drawCategory(categoryIndex, d->proxyModel->sortRole(), option, &p);
807  ++it;
808  }
809  //END: draw categories
810 
811  if (intersecting.first.isValid() && intersecting.second.isValid()) {
812  //BEGIN: draw items
813  int i = intersecting.first.row();
814  int indexToCheckIfBlockCollapsed = i;
815  QModelIndex categoryIndex;
816  QString category;
817  Private::Block *block = 0;
818  while (i <= intersecting.second.row()) {
819  //BEGIN: first check if the block is collapsed. if so, we have to skip the item painting
820  if (i == indexToCheckIfBlockCollapsed) {
821  categoryIndex = d->proxyModel->index(i, d->proxyModel->sortColumn(), rootIndex());
822  category = categoryIndex.data(KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
823  block = &d->blocks[category];
824  indexToCheckIfBlockCollapsed = block->firstIndex.row() + block->items.count();
825  if (block->collapsed) {
826  i = indexToCheckIfBlockCollapsed;
827  continue;
828  }
829  }
830  //END: first check if the block is collapsed. if so, we have to skip the item painting
831 
832  Q_ASSERT(block);
833 
834  const bool alternateItem = (i - block->firstIndex.row()) % 2;
835 
836  const QModelIndex index = d->proxyModel->index(i, modelColumn(), rootIndex());
837  const Qt::ItemFlags flags = d->proxyModel->flags(index);
838  QStyleOptionViewItemV4 option(viewOptions());
839  option.rect = visualRect(index);
840  option.widget = this;
841  option.features |= wordWrap() ? QStyleOptionViewItemV2::WrapText
842  : QStyleOptionViewItemV2::None;
843  option.features |= alternatingRowColors() && alternateItem ? QStyleOptionViewItemV4::Alternate
844  : QStyleOptionViewItemV4::None;
845  if (flags & Qt::ItemIsSelectable) {
846  option.state |= selectionModel()->isSelected(index) ? QStyle::State_Selected
847  : QStyle::State_None;
848  } else {
849  option.state &= ~QStyle::State_Selected;
850  }
851  option.state |= (index == currentIndex()) ? QStyle::State_HasFocus
852  : QStyle::State_None;
853  if (!(flags & Qt::ItemIsEnabled)) {
854  option.state &= ~QStyle::State_Enabled;
855  } else {
856  option.state |= (index == d->hoveredIndex) ? QStyle::State_MouseOver
857  : QStyle::State_None;
858  }
859 
860  itemDelegate(index)->paint(&p, option, index);
861  ++i;
862  }
863  //END: draw items
864  }
865 
866  //BEGIN: draw selection rect
867  if (isSelectionRectVisible() && d->rubberBandRect.isValid()) {
868  QStyleOptionRubberBand opt;
869  opt.initFrom(this);
870  opt.shape = QRubberBand::Rectangle;
871  opt.opaque = false;
872  opt.rect = d->mapToViewport(d->rubberBandRect).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
873  p.save();
874  style()->drawControl(QStyle::CE_RubberBand, &opt, &p);
875  p.restore();
876  }
877  //END: draw selection rect
878 
879  p.restore();
880 }
881 
882 void KCategorizedView::resizeEvent(QResizeEvent *event)
883 {
884  d->regenerateAllElements();
885  QListView::resizeEvent(event);
886 }
887 
888 void KCategorizedView::setSelection(const QRect &rect,
889  QItemSelectionModel::SelectionFlags flags)
890 {
891  if (!d->isCategorized()) {
892  QListView::setSelection(rect, flags);
893  return;
894  }
895 
896  if (rect.topLeft() == rect.bottomRight()) {
897  const QModelIndex index = indexAt(rect.topLeft());
898  selectionModel()->select(index, flags);
899  return;
900  }
901 
902  const QPair<QModelIndex, QModelIndex> intersecting = d->intersectingIndexesWithRect(rect);
903 
904  QItemSelection selection;
905 
906  //TODO: think of a faster implementation
907  QModelIndex firstIndex;
908  QModelIndex lastIndex;
909  for (int i = intersecting.first.row(); i <= intersecting.second.row(); ++i) {
910  const QModelIndex index = d->proxyModel->index(i, modelColumn(), rootIndex());
911  const bool visualRectIntersects = visualRect(index).intersects(rect);
912  if (firstIndex.isValid()) {
913  if (visualRectIntersects) {
914  lastIndex = index;
915  } else {
916  selection << QItemSelectionRange(firstIndex, lastIndex);
917  firstIndex = QModelIndex();
918  }
919  } else if (visualRectIntersects) {
920  firstIndex = index;
921  lastIndex = index;
922  }
923  }
924 
925  if (firstIndex.isValid()) {
926  selection << QItemSelectionRange(firstIndex, lastIndex);
927  }
928 
929  selectionModel()->select(selection, flags);
930 }
931 
932 void KCategorizedView::mouseMoveEvent(QMouseEvent *event)
933 {
934  QListView::mouseMoveEvent(event);
935  d->hoveredIndex = indexAt(event->pos());
936  const SelectionMode itemViewSelectionMode = selectionMode();
937  if (state() == DragSelectingState && isSelectionRectVisible() && itemViewSelectionMode != SingleSelection
938  && itemViewSelectionMode != NoSelection) {
939  QRect rect(d->pressedPosition, event->pos() + QPoint(horizontalOffset(), verticalOffset()));
940  rect = rect.normalized();
941  update(rect.united(d->rubberBandRect));
942  d->rubberBandRect = rect;
943  }
944  if (!d->categoryDrawerV2) {
945  return;
946  }
947  QHash<QString, Private::Block>::ConstIterator it(d->blocks.constBegin());
948  while (it != d->blocks.constEnd()) {
949  const Private::Block &block = *it;
950  const QModelIndex categoryIndex = d->proxyModel->index(block.firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
951  QStyleOptionViewItemV4 option(viewOptions());
952  const int height = d->categoryDrawer->categoryHeight(categoryIndex, option);
953  QPoint pos = d->blockPosition(it.key());
954  pos.ry() -= height;
955  option.rect.setTopLeft(pos);
956  option.rect.setWidth(d->viewportWidth() + d->categoryDrawer->leftMargin() + d->categoryDrawer->rightMargin());
957  option.rect.setHeight(height + d->blockHeight(it.key()));
958  option.rect = d->mapToViewport(option.rect);
959  const QPoint mousePos = viewport()->mapFromGlobal(QCursor::pos());
960  if (option.rect.contains(mousePos)) {
961  if (d->categoryDrawerV3 && d->hoveredBlock->height != -1 && *d->hoveredBlock != block) {
962  const QModelIndex categoryIndex = d->proxyModel->index(d->hoveredBlock->firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
963  const QStyleOptionViewItemV4 option = d->blockRect(categoryIndex);
964  d->categoryDrawerV3->mouseLeft(categoryIndex, option.rect);
965  *d->hoveredBlock = block;
966  d->hoveredCategory = it.key();
967  viewport()->update(option.rect);
968  } else if (d->hoveredBlock->height == -1) {
969  *d->hoveredBlock = block;
970  d->hoveredCategory = it.key();
971  } else if (d->categoryDrawerV3) {
972  d->categoryDrawerV3->mouseMoved(categoryIndex, option.rect, event);
973  } else {
974  d->categoryDrawerV2->mouseButtonMoved(categoryIndex, event);
975  }
976  viewport()->update(option.rect);
977  return;
978  }
979  ++it;
980  }
981  if (d->categoryDrawerV3 && d->hoveredBlock->height != -1) {
982  const QModelIndex categoryIndex = d->proxyModel->index(d->hoveredBlock->firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
983  const QStyleOptionViewItemV4 option = d->blockRect(categoryIndex);
984  d->categoryDrawerV3->mouseLeft(categoryIndex, option.rect);
985  *d->hoveredBlock = Private::Block();
986  d->hoveredCategory = QString();
987  viewport()->update(option.rect);
988  }
989 }
990 
991 void KCategorizedView::mousePressEvent(QMouseEvent *event)
992 {
993  if (event->button() == Qt::LeftButton) {
994  d->pressedPosition = event->pos();
995  d->pressedPosition.rx() += horizontalOffset();
996  d->pressedPosition.ry() += verticalOffset();
997  }
998  if (!d->categoryDrawerV2) {
999  QListView::mousePressEvent(event);
1000  return;
1001  }
1002  QHash<QString, Private::Block>::ConstIterator it(d->blocks.constBegin());
1003  while (it != d->blocks.constEnd()) {
1004  const Private::Block &block = *it;
1005  const QModelIndex categoryIndex = d->proxyModel->index(block.firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
1006  const QStyleOptionViewItemV4 option = d->blockRect(categoryIndex);
1007  const QPoint mousePos = viewport()->mapFromGlobal(QCursor::pos());
1008  if (option.rect.contains(mousePos)) {
1009  if (d->categoryDrawerV3) {
1010  d->categoryDrawerV3->mouseButtonPressed(categoryIndex, option.rect, event);
1011  } else {
1012  d->categoryDrawerV2->mouseButtonPressed(categoryIndex, event);
1013  }
1014  viewport()->update(option.rect);
1015  if (!event->isAccepted()) {
1016  QListView::mousePressEvent(event);
1017  }
1018  return;
1019  }
1020  ++it;
1021  }
1022  QListView::mousePressEvent(event);
1023 }
1024 
1025 void KCategorizedView::mouseReleaseEvent(QMouseEvent *event)
1026 {
1027  d->pressedPosition = QPoint();
1028  d->rubberBandRect = QRect();
1029  if (!d->categoryDrawerV2) {
1030  QListView::mouseReleaseEvent(event);
1031  return;
1032  }
1033  QHash<QString, Private::Block>::ConstIterator it(d->blocks.constBegin());
1034  while (it != d->blocks.constEnd()) {
1035  const Private::Block &block = *it;
1036  const QModelIndex categoryIndex = d->proxyModel->index(block.firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
1037  const QStyleOptionViewItemV4 option = d->blockRect(categoryIndex);
1038  const QPoint mousePos = viewport()->mapFromGlobal(QCursor::pos());
1039  if (option.rect.contains(mousePos)) {
1040  if (d->categoryDrawerV3) {
1041  d->categoryDrawerV3->mouseButtonReleased(categoryIndex, option.rect, event);
1042  } else {
1043  d->categoryDrawerV2->mouseButtonReleased(categoryIndex, event);
1044  }
1045  viewport()->update(option.rect);
1046  if (!event->isAccepted()) {
1047  QListView::mouseReleaseEvent(event);
1048  }
1049  return;
1050  }
1051  ++it;
1052  }
1053  QListView::mouseReleaseEvent(event);
1054 }
1055 
1056 void KCategorizedView::leaveEvent(QEvent *event)
1057 {
1058  QListView::leaveEvent(event);
1059  if (d->hoveredIndex.isValid()) {
1060  viewport()->update(visualRect(d->hoveredIndex));
1061  d->hoveredIndex = QModelIndex();
1062  }
1063  if (d->categoryDrawerV3 && d->hoveredBlock->height != -1) {
1064  const QModelIndex categoryIndex = d->proxyModel->index(d->hoveredBlock->firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
1065  const QStyleOptionViewItemV4 option = d->blockRect(categoryIndex);
1066  d->categoryDrawerV3->mouseLeft(categoryIndex, option.rect);
1067  *d->hoveredBlock = Private::Block();
1068  d->hoveredCategory = QString();
1069  viewport()->update(option.rect);
1070  }
1071 }
1072 
1073 void KCategorizedView::startDrag(Qt::DropActions supportedActions)
1074 {
1075  QListView::startDrag(supportedActions);
1076 }
1077 
1078 void KCategorizedView::dragMoveEvent(QDragMoveEvent *event)
1079 {
1080  QListView::dragMoveEvent(event);
1081  d->hoveredIndex = indexAt(event->pos());
1082 }
1083 
1084 void KCategorizedView::dragEnterEvent(QDragEnterEvent *event)
1085 {
1086  QListView::dragEnterEvent(event);
1087 }
1088 
1089 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent *event)
1090 {
1091  QListView::dragLeaveEvent(event);
1092 }
1093 
1094 void KCategorizedView::dropEvent(QDropEvent *event)
1095 {
1096  QListView::dropEvent(event);
1097 }
1098 
1099 //TODO: improve se we take into account collapsed blocks
1100 //TODO: take into account when there is no grid and no uniformItemSizes
1101 QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction,
1102  Qt::KeyboardModifiers modifiers)
1103 {
1104  if (!d->isCategorized()) {
1105  return QListView::moveCursor(cursorAction, modifiers);
1106  }
1107 
1108  const QModelIndex current = currentIndex();
1109  const QRect currentRect = visualRect(current);
1110  if (!current.isValid()) {
1111  const int rowCount = d->proxyModel->rowCount(rootIndex());
1112  if (!rowCount) {
1113  return QModelIndex();
1114  }
1115  return d->proxyModel->index(0, modelColumn(), rootIndex());
1116  }
1117 
1118  switch (cursorAction) {
1119  case MoveLeft: {
1120  if (!current.row()) {
1121  return QModelIndex();
1122  }
1123  const QModelIndex previous = d->proxyModel->index(current.row() - 1, modelColumn(), rootIndex());
1124  const QRect previousRect = visualRect(previous);
1125  if (previousRect.top() == currentRect.top()) {
1126  return previous;
1127  }
1128 
1129  return QModelIndex();
1130  }
1131  case MoveRight: {
1132  if (current.row() == d->proxyModel->rowCount() - 1) {
1133  return QModelIndex();
1134  }
1135  const QModelIndex next = d->proxyModel->index(current.row() + 1, modelColumn(), rootIndex());
1136  const QRect nextRect = visualRect(next);
1137  if (nextRect.top() == currentRect.top()) {
1138  return next;
1139  }
1140 
1141  return QModelIndex();
1142  }
1143  case MoveDown: {
1144  if (d->hasGrid() || uniformItemSizes()) {
1145  const QModelIndex current = currentIndex();
1146  const QSize itemSize = d->hasGrid() ? gridSize()
1147  : sizeHintForIndex(current);
1148  const Private::Block &block = d->blocks[d->categoryForIndex(current)];
1149  const int maxItemsPerRow = qMax(d->viewportWidth() / itemSize.width(), 1);
1150  const bool canMove = current.row() + maxItemsPerRow < block.firstIndex.row() +
1151  block.items.count();
1152 
1153  if (canMove) {
1154  return d->proxyModel->index(current.row() + maxItemsPerRow, modelColumn(), rootIndex());
1155  }
1156 
1157  const int currentRelativePos = (current.row() - block.firstIndex.row()) % maxItemsPerRow;
1158  const QModelIndex nextIndex = d->proxyModel->index(block.firstIndex.row() + block.items.count(), modelColumn(), rootIndex());
1159 
1160  if (!nextIndex.isValid()) {
1161  return QModelIndex();
1162  }
1163 
1164  const Private::Block &nextBlock = d->blocks[d->categoryForIndex(nextIndex)];
1165 
1166  if (nextBlock.items.count() <= currentRelativePos) {
1167  return QModelIndex();
1168  }
1169 
1170  if (currentRelativePos < (block.items.count() % maxItemsPerRow)) {
1171  return d->proxyModel->index(nextBlock.firstIndex.row() + currentRelativePos, modelColumn(), rootIndex());
1172  }
1173 
1174  return QModelIndex();
1175  }
1176  }
1177  case MoveUp: {
1178  if (d->hasGrid() || uniformItemSizes()) {
1179  const QModelIndex current = currentIndex();
1180  const QSize itemSize = d->hasGrid() ? gridSize()
1181  : sizeHintForIndex(current);
1182  const Private::Block &block = d->blocks[d->categoryForIndex(current)];
1183  const int maxItemsPerRow = qMax(d->viewportWidth() / itemSize.width(), 1);
1184  const bool canMove = current.row() - maxItemsPerRow >= block.firstIndex.row();
1185 
1186  if (canMove) {
1187  return d->proxyModel->index(current.row() - maxItemsPerRow, modelColumn(), rootIndex());
1188  }
1189 
1190  const int currentRelativePos = (current.row() - block.firstIndex.row()) % maxItemsPerRow;
1191  const QModelIndex prevIndex = d->proxyModel->index(block.firstIndex.row() - 1, modelColumn(), rootIndex());
1192 
1193  if (!prevIndex.isValid()) {
1194  return QModelIndex();
1195  }
1196 
1197  const Private::Block &prevBlock = d->blocks[d->categoryForIndex(prevIndex)];
1198 
1199  if (prevBlock.items.count() <= currentRelativePos) {
1200  return QModelIndex();
1201  }
1202 
1203  const int remainder = prevBlock.items.count() % maxItemsPerRow;
1204  if (currentRelativePos < remainder) {
1205  return d->proxyModel->index(prevBlock.firstIndex.row() + prevBlock.items.count() - remainder + currentRelativePos, modelColumn(), rootIndex());
1206  }
1207 
1208  return QModelIndex();
1209  }
1210  }
1211  default:
1212  break;
1213  }
1214 
1215  return QModelIndex();
1216 }
1217 
1218 void KCategorizedView::rowsAboutToBeRemoved(const QModelIndex &parent,
1219  int start,
1220  int end)
1221 {
1222  if (!d->isCategorized()) {
1223  QListView::rowsAboutToBeRemoved(parent, start, end);
1224  return;
1225  }
1226 
1227  *d->hoveredBlock = Private::Block();
1228  d->hoveredCategory = QString();
1229 
1230  if (end - start + 1 == d->proxyModel->rowCount()) {
1231  d->blocks.clear();
1232  QListView::rowsAboutToBeRemoved(parent, start, end);
1233  return;
1234  }
1235 
1236  // Removal feels a bit more complicated than insertion. Basically we can consider there are
1237  // 3 different cases when going to remove items. (*) represents an item, Items between ([) and
1238  // (]) are the ones which are marked for removal.
1239  //
1240  // - 1st case:
1241  // ... * * * * * * [ * * * ...
1242  //
1243  // The items marked for removal are the last part of this category. No need to mark any item
1244  // of this category as in quarantine, because no special offset will be pushed to items at
1245  // the right because of any changes (since the removed items are those on the right most part
1246  // of the category).
1247  //
1248  // - 2nd case:
1249  // ... * * * * * * ] * * * ...
1250  //
1251  // The items marked for removal are the first part of this category. We have to mark as in
1252  // quarantine all items in this category. Absolutely all. All items will have to be moved to
1253  // the left (or moving up, because rows got a different offset).
1254  //
1255  // - 3rd case:
1256  // ... * * [ * * * * ] * * ...
1257  //
1258  // The items marked for removal are in between of this category. We have to mark as in
1259  // quarantine only those items that are at the right of the end of the removal interval,
1260  // (starting on "]").
1261  //
1262  // It hasn't been explicitly said, but when we remove, we have to mark all blocks that are
1263  // located under the top most affected category as in quarantine (the block itself, as a whole),
1264  // because such a change can force it to have a different offset (note that items themselves
1265  // contain relative positions to the block, so marking the block as in quarantine is enough).
1266  //
1267  // Also note that removal implicitly means that we have to update correctly firstIndex of each
1268  // block, and in general keep updated the internal information of elements.
1269 
1270  QStringList listOfCategoriesMarkedForRemoval;
1271 
1272  QString lastCategory;
1273  int alreadyRemoved = 0;
1274  for (int i = start; i <= end; ++i) {
1275  const QModelIndex index = d->proxyModel->index(i, modelColumn(), parent);
1276 
1277  Q_ASSERT(index.isValid());
1278 
1279  const QString category = d->categoryForIndex(index);
1280 
1281  if (lastCategory != category) {
1282  lastCategory = category;
1283  alreadyRemoved = 0;
1284  }
1285 
1286  Private::Block &block = d->blocks[category];
1287  block.items.removeAt(i - block.firstIndex.row() - alreadyRemoved);
1288  ++alreadyRemoved;
1289 
1290  if (!block.items.count()) {
1291  listOfCategoriesMarkedForRemoval << category;
1292  }
1293 
1294  block.height = -1;
1295 
1296  viewport()->update();
1297  }
1298 
1299  //BEGIN: update the items that are in quarantine in affected categories
1300  {
1301  const QModelIndex lastIndex = d->proxyModel->index(end, modelColumn(), parent);
1302  const QString category = d->categoryForIndex(lastIndex);
1303  Private::Block &block = d->blocks[category];
1304  if (block.items.count() && start <= block.firstIndex.row() && end >= block.firstIndex.row()) {
1305  block.firstIndex = d->proxyModel->index(end + 1, modelColumn(), parent);
1306  }
1307  block.quarantineStart = block.firstIndex;
1308  }
1309  //END: update the items that are in quarantine in affected categories
1310 
1311  Q_FOREACH (const QString &category, listOfCategoriesMarkedForRemoval) {
1312  d->blocks.remove(category);
1313  }
1314 
1315  //BEGIN: mark as in quarantine those categories that are under the affected ones
1316  {
1317  //BEGIN: order for marking as alternate those blocks that are alternate
1318  QList<Private::Block> blockList = d->blocks.values();
1319  qSort(blockList.begin(), blockList.end(), Private::Block::lessThan);
1320  QList<int> firstIndexesRows;
1321  foreach (const Private::Block &block, blockList) {
1322  firstIndexesRows << block.firstIndex.row();
1323  }
1324  //END: order for marking as alternate those blocks that are alternate
1325  for (QHash<QString, Private::Block>::Iterator it = d->blocks.begin(); it != d->blocks.end(); ++it) {
1326  Private::Block &block = *it;
1327  if (block.firstIndex.row() > start) {
1328  block.outOfQuarantine = false;
1329  block.alternate = firstIndexesRows.indexOf(block.firstIndex.row()) % 2;
1330  } else if (block.firstIndex.row() == start) {
1331  block.alternate = firstIndexesRows.indexOf(block.firstIndex.row()) % 2;
1332  }
1333  }
1334  }
1335  //END: mark as in quarantine those categories that are under the affected ones
1336 
1337  QListView::rowsAboutToBeRemoved(parent, start, end);
1338 }
1339 
1340 void KCategorizedView::updateGeometries()
1341 {
1342  const int oldVerticalOffset = verticalOffset();
1343  const Qt::ScrollBarPolicy verticalP = verticalScrollBarPolicy(), horizontalP = horizontalScrollBarPolicy();
1344 
1345  //BEGIN bugs 213068, 287847 ------------------------------------------------------------
1346  /*
1347  * QListView::updateGeometries() has it's own opinion on whether the scrollbars should be visible (valid range) or not
1348  * and triggers a (sometimes additionally timered) resize through ::layoutChildren()
1349  * http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/itemviews/qlistview.cpp#line1499
1350  * (the comment above the main block isn't all accurate, layoutChldren is called regardless of the policy)
1351  *
1352  * As a result QListView and KCategorizedView occasionally started a race on the scrollbar visibility, effectively blocking the UI
1353  * So we prevent QListView from having an own opinion on the scrollbar visibility by
1354  * fixing it before calling the baseclass QListView::updateGeometries()
1355  *
1356  * Since the implicit show/hide by the followin range setting will cause further resizes if the policy is Qt::ScrollBarAsNeeded
1357  * we keep it static until we're done, then restore the original value and ultimately change the scrollbar visibility ourself.
1358  */
1359  if (d->isCategorized()) { // important! - otherwise we'd pollute the setting if the view is initially not categorized
1360  setVerticalScrollBarPolicy((verticalP == Qt::ScrollBarAlwaysOn || verticalScrollBar()->isVisibleTo(this)) ?
1361  Qt::ScrollBarAlwaysOn : Qt::ScrollBarAlwaysOff);
1362  setHorizontalScrollBarPolicy((horizontalP == Qt::ScrollBarAlwaysOn || horizontalScrollBar()->isVisibleTo(this)) ?
1363  Qt::ScrollBarAlwaysOn : Qt::ScrollBarAlwaysOff);
1364  }
1365  //END bugs 213068, 287847 --------------------------------------------------------------
1366 
1367  QListView::updateGeometries();
1368 
1369  if (!d->isCategorized()) {
1370  return;
1371  }
1372 
1373  const int rowCount = d->proxyModel->rowCount();
1374  if (!rowCount) {
1375  verticalScrollBar()->setRange(0, 0);
1376  // unconditional, see function end todo
1377  horizontalScrollBar()->setRange(0, 0);
1378  return;
1379  }
1380 
1381  const QModelIndex lastIndex = d->proxyModel->index(rowCount - 1, modelColumn(), rootIndex());
1382  Q_ASSERT(lastIndex.isValid());
1383  QRect lastItemRect = visualRect(lastIndex);
1384 
1385  if (d->hasGrid()) {
1386  lastItemRect.setSize(lastItemRect.size().expandedTo(gridSize()));
1387  } else {
1388  if (uniformItemSizes()) {
1389  QSize itemSize = sizeHintForIndex(lastIndex);
1390  itemSize.setHeight(itemSize.height() + spacing());
1391  lastItemRect.setSize(itemSize);
1392  } else {
1393  QSize itemSize = sizeHintForIndex(lastIndex);
1394  const QString category = d->categoryForIndex(lastIndex);
1395  itemSize.setHeight(d->highestElementInLastRow(d->blocks[category]) + spacing());
1396  lastItemRect.setSize(itemSize);
1397  }
1398  }
1399 
1400  const int bottomRange = lastItemRect.bottomRight().y() + verticalOffset() - viewport()->height();
1401 
1402  if (verticalScrollMode() == ScrollPerItem) {
1403  verticalScrollBar()->setSingleStep(lastItemRect.height());
1404  const int rowsPerPage = qMax(viewport()->height() / lastItemRect.height(), 1);
1405  verticalScrollBar()->setPageStep(rowsPerPage * lastItemRect.height());
1406  }
1407 
1408  verticalScrollBar()->setRange(0, bottomRange);
1409  verticalScrollBar()->setValue(oldVerticalOffset);
1410 
1411  //TODO: also consider working with the horizontal scroll bar. since at this level I am not still
1412  // supporting "top to bottom" flow, there is no real problem. If I support that someday
1413  // (think how to draw categories), we would have to take care of the horizontal scroll bar too.
1414  // In theory, as KCategorizedView has been designed, there is no need of horizontal scroll bar.
1415  horizontalScrollBar()->setRange(0, 0);
1416 
1417  //BEGIN bugs 213068, 287847 ------------------------------------------------------------
1418  // restoring values from above ...
1419  setVerticalScrollBarPolicy(verticalP);
1420  setHorizontalScrollBarPolicy(horizontalP);
1421  // ... and correct the visibility
1422  bool validRange = verticalScrollBar()->maximum() != verticalScrollBar()->minimum();
1423  if (verticalP == Qt::ScrollBarAsNeeded && (verticalScrollBar()->isVisibleTo(this) != validRange))
1424  verticalScrollBar()->setVisible(validRange);
1425  validRange = horizontalScrollBar()->maximum() > horizontalScrollBar()->minimum();
1426  if (horizontalP == Qt::ScrollBarAsNeeded && (horizontalScrollBar()->isVisibleTo(this) != validRange))
1427  horizontalScrollBar()->setVisible(validRange);
1428  //END bugs 213068, 287847 --------------------------------------------------------------
1429 }
1430 
1431 void KCategorizedView::currentChanged(const QModelIndex &current,
1432  const QModelIndex &previous)
1433 {
1434  QListView::currentChanged(current, previous);
1435 }
1436 
1437 void KCategorizedView::dataChanged(const QModelIndex &topLeft,
1438  const QModelIndex &bottomRight)
1439 {
1440  QListView::dataChanged(topLeft, bottomRight);
1441  if (!d->isCategorized()) {
1442  return;
1443  }
1444 
1445  *d->hoveredBlock = Private::Block();
1446  d->hoveredCategory = QString();
1447 
1448  //BEGIN: since the model changed data, we need to reconsider item sizes
1449  int i = topLeft.row();
1450  int indexToCheck = i;
1451  QModelIndex categoryIndex;
1452  QString category;
1453  Private::Block *block;
1454  while (i <= bottomRight.row()) {
1455  const QModelIndex currIndex = d->proxyModel->index(i, modelColumn(), rootIndex());
1456  if (i == indexToCheck) {
1457  categoryIndex = d->proxyModel->index(i, d->proxyModel->sortColumn(), rootIndex());
1458  category = categoryIndex.data(KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
1459  block = &d->blocks[category];
1460  block->quarantineStart = currIndex;
1461  indexToCheck = block->firstIndex.row() + block->items.count();
1462  }
1463  visualRect(currIndex);
1464  ++i;
1465  }
1466  //END: since the model changed data, we need to reconsider item sizes
1467 }
1468 
1469 void KCategorizedView::rowsInserted(const QModelIndex &parent,
1470  int start,
1471  int end)
1472 {
1473  QListView::rowsInserted(parent, start, end);
1474  if (!d->isCategorized()) {
1475  return;
1476  }
1477 
1478  *d->hoveredBlock = Private::Block();
1479  d->hoveredCategory = QString();
1480  d->rowsInserted(parent, start, end);
1481 }
1482 
1483 #ifndef KDE_NO_DEPRECATED
1484 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent,
1485  int start,
1486  int end)
1487 {
1488  Q_UNUSED(parent);
1489  Q_UNUSED(start);
1490  Q_UNUSED(end);
1491 }
1492 #endif
1493 
1494 #ifndef KDE_NO_DEPRECATED
1495 void KCategorizedView::rowsRemoved(const QModelIndex &parent,
1496  int start,
1497  int end)
1498 {
1499  Q_UNUSED(parent);
1500  Q_UNUSED(start);
1501  Q_UNUSED(end);
1502 }
1503 #endif
1504 
1505 void KCategorizedView::slotLayoutChanged()
1506 {
1507  if (!d->isCategorized()) {
1508  return;
1509  }
1510 
1511  d->blocks.clear();
1512  *d->hoveredBlock = Private::Block();
1513  d->hoveredCategory = QString();
1514  if (d->proxyModel->rowCount()) {
1515  d->rowsInserted(rootIndex(), 0, d->proxyModel->rowCount() - 1);
1516  }
1517 }
1518 
1519 //END: Public part
1520 
1521 #include "kcategorizedview.moc"
kcategorizedview.h
KCategorizedView::leaveEvent
virtual void leaveEvent(QEvent *event)
Reimplemented from QWidget.
Definition: kcategorizedview.cpp:1056
kcategorizedsortfilterproxymodel.h
KCategorizedView::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *event)
Reimplemented from QWidget.
Definition: kcategorizedview.cpp:932
KCategorizedView::currentChanged
virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1431
KCategorizedView::indexAt
virtual QModelIndex indexAt(const QPoint &point) const
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:716
KCategorizedView::categorySpacing
int categorySpacing() const
KCategorizedView::rowsRemoved
virtual void rowsRemoved(const QModelIndex &parent, int start, int end)
Definition: kcategorizedview.cpp:1495
KCategorizedView::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
Reimplemented from QWidget.
Definition: kcategorizedview.cpp:882
KCategorizedView::alternatingBlockColors
bool alternatingBlockColors() const
KCategorizedView::rowsInsertedArtifficial
virtual void rowsInsertedArtifficial(const QModelIndex &parent, int start, int end)
Definition: kcategorizedview.cpp:1484
KCategorizedView::updateGeometries
virtual void updateGeometries()
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1340
KCategorizedView::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *event)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1084
KCategorizedView::collapsibleBlocks
bool collapsibleBlocks() const
KCategorizedView::setCollapsibleBlocks
void setCollapsibleBlocks(bool enable)
Sets whether blocks can be collapsed or not.
Definition: kcategorizedview.cpp:688
QWidget
KCategorizedSortFilterProxyModel
This class lets you categorize a view.
Definition: kcategorizedsortfilterproxymodel.h:45
KCategorizedView::dragLeaveEvent
virtual void dragLeaveEvent(QDragLeaveEvent *event)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1089
operator!=
bool operator!=(const KEntry &k1, const KEntry &k2)
QString
QHash
KCategorizedView::slotLayoutChanged
virtual void slotLayoutChanged()
Definition: kcategorizedview.cpp:1505
KCategorizedView::setGridSizeOwn
void setGridSizeOwn(const QSize &size)
Definition: kcategorizedview.cpp:545
KCategorizedView::setCategoryDrawer
void setCategoryDrawer(KCategoryDrawer *categoryDrawer)
The category drawer that will be used for drawing categories.
Definition: kcategorizedview.cpp:637
KCategoryDrawer
Definition: kcategorydrawer.h:43
KCategorizedView
Item view for listing items in a categorized fashion optionally.
Definition: kcategorizedview.h:79
KCategorizedView::categoryDrawer
KCategoryDrawer * categoryDrawer() const
Returns the current category drawer.
Definition: kcategorizedview.cpp:632
KCategorizedView::setGridSize
void setGridSize(const QSize &size)
Calls to setGridSizeOwn().
Definition: kcategorizedview.cpp:540
KCategorizedView::dataChanged
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1437
QStringList
KCategorizedView::rowsAboutToBeRemoved
virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1218
KCategorizedView::~KCategorizedView
~KCategorizedView()
Definition: kcategorizedview.cpp:509
KCategorizedView::visualRect
virtual QRect visualRect(const QModelIndex &index) const
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:551
KCategoryDrawerV3
Definition: kcategorydrawer.h:150
KCategorizedView::reset
virtual void reset()
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:765
KCategorizedView::setSelection
virtual void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:888
KCategorizedView::setModel
virtual void setModel(QAbstractItemModel *model)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:514
KStandardAction::next
KAction * next(const QObject *recvr, const char *slot, QObject *parent)
Scroll down one page.
Definition: kstandardaction.cpp:414
KCategorizedView::KCategorizedView
KCategorizedView(QWidget *parent=0)
Definition: kcategorizedview.cpp:503
QAbstractItemModel
lessThan
bool lessThan(const QString &left, const QString &right)
Definition: kcompletion.cpp:812
KCategorizedView::rowsInserted
virtual void rowsInserted(const QModelIndex &parent, int start, int end)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1469
QListView
kcategorydrawer.h
KCategorizedView::block
QModelIndexList block(const QString &category)
Definition: kcategorizedview.cpp:693
KCategoryDrawerV2
Definition: kcategorydrawer.h:113
KCategorizedView::dropEvent
virtual void dropEvent(QDropEvent *event)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1094
QPoint
KCategorizedView::setAlternatingBlockColors
void setAlternatingBlockColors(bool enable)
Sets whether blocks should be drawn with alternating colors.
Definition: kcategorizedview.cpp:678
QRect
QPair
QSize
Item
KCategorizedView::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *event)
Reimplemented from QWidget.
Definition: kcategorizedview.cpp:1025
KStandardGuiItem::reset
KGuiItem reset()
Returns the 'Reset' gui item.
Definition: kstandardguiitem.cpp:258
KCategorizedView::setCategorySpacing
void setCategorySpacing(int categorySpacing)
Stablishes the category spacing.
Definition: kcategorizedview.cpp:659
KCategorizedSortFilterProxyModel::CategoryDisplayRole
This role is used for asking the category to a given index.
Definition: kcategorizedsortfilterproxymodel.h:52
KCategorizedView::moveCursor
virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1101
KCategorizedView::mousePressEvent
virtual void mousePressEvent(QMouseEvent *event)
Reimplemented from QWidget.
Definition: kcategorizedview.cpp:991
KCategorizedView::paintEvent
virtual void paintEvent(QPaintEvent *event)
Reimplemented from QWidget.
Definition: kcategorizedview.cpp:771
KStandardShortcut::end
const KShortcut & end()
Goto end of the document.
Definition: kstandardshortcut.cpp:348
KCategorizedView::startDrag
virtual void startDrag(Qt::DropActions supportedActions)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1073
KCategorizedView::dragMoveEvent
virtual void dragMoveEvent(QDragMoveEvent *event)
Reimplemented from QAbstractItemView.
Definition: kcategorizedview.cpp:1078
QList
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal