, categoryDrawer(0)
, biggestItemSize(QSize(0, 0))
, mouseButtonPressed(false)
+ , rightMouseButtonPressed(false)
, isDragging(false)
, dragLeftViewport(false)
, proxyModel(0)
void KCategorizedView::setModel(QAbstractItemModel *model)
{
d->lastSelection = QItemSelection();
- d->currentViewIndex = QModelIndex();
d->forcedSelectionPosition = 0;
d->elementsInfo.clear();
d->elementsPosition.clear();
d->modelIndexList.clear();
d->hovered = QModelIndex();
d->mouseButtonPressed = false;
+ d->rightMouseButtonPressed = false;
if (d->proxyModel)
{
void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer)
{
d->lastSelection = QItemSelection();
- d->currentViewIndex = QModelIndex();
d->forcedSelectionPosition = 0;
d->elementsInfo.clear();
d->elementsPosition.clear();
d->modelIndexList.clear();
d->hovered = QModelIndex();
d->mouseButtonPressed = false;
+ d->rightMouseButtonPressed = false;
if (!categoryDrawer && d->proxyModel)
{
QListView::reset();
d->lastSelection = QItemSelection();
- d->currentViewIndex = QModelIndex();
d->forcedSelectionPosition = 0;
d->elementsInfo.clear();
d->elementsPosition.clear();
d->hovered = QModelIndex();
d->biggestItemSize = QSize(0, 0);
d->mouseButtonPressed = false;
+ d->rightMouseButtonPressed = false;
}
void KCategorizedView::paintEvent(QPaintEvent *event)
if (flags & QItemSelectionModel::Clear)
{
selectionModel()->clear();
+ d->lastSelection.clear();
}
QModelIndexList dirtyIndexes = d->intersectionSet(rect);
// no items affected, just leave
if (!dirtyIndexes.count())
{
- selectionModel()->select(d->lastSelection, flags);
-
- d->lastSelection.clear();
-
- return;
- }
-
- d->lastSelection.clear();
-
- if (!(flags & QItemSelectionModel::Current))
- {
- Q_ASSERT(dirtyIndexes.count() == 1);
-
- selectionModel()->select(dirtyIndexes[0], flags);
+ selectionModel()->select(d->lastSelection, QItemSelectionModel::SelectCurrent);
return;
}
QModelIndex topLeft;
QModelIndex bottomRight;
- if (d->mouseButtonPressed) // selection with click + drag
+ if (d->mouseButtonPressed || d->rightMouseButtonPressed) // selection with click + drag
{
+ QItemSelection selection;
+
QModelIndex prev = dirtyIndexes[0];
QModelIndex first = prev;
foreach (const QModelIndex &index, dirtyIndexes)
{
+ // we have a different interval. non-contiguous items
if ((index.row() - prev.row()) > 1) {
- d->lastSelection << QItemSelectionRange(first, prev);
+ selection << QItemSelectionRange(first, prev);
first = index;
}
prev = index;
}
- d->lastSelection << QItemSelectionRange(first, prev);
+ selection << QItemSelectionRange(first, prev);
+
+ if (flags & QItemSelectionModel::Current)
+ {
+ if (rect.topLeft() == rect.bottomRight())
+ {
+ selectionModel()->setCurrentIndex(indexAt(rect.topLeft()), QItemSelectionModel::NoUpdate);
+ }
+
+ selection.merge(d->lastSelection, flags);
+ }
+ else
+ {
+ selection.merge(selectionModel()->selection(), flags);
+
+ selectionModel()->select(selection, QItemSelectionModel::SelectCurrent);
- selectionModel()->select(d->lastSelection, flags);
+ return;
+ }
+
+ selectionModel()->select(selection, flags);
}
else // selection with click + keyboard keys
{
dirtyIndexes << model()->index(i, theoricDirty[0].column(), theoricDirty[0].parent());
}
- // our current selection will result modified
- d->lastSelection = QItemSelection(topLeft, bottomRight);
+ QItemSelection selection(topLeft, bottomRight);
- selectionModel()->select(d->lastSelection, flags);
+ selectionModel()->select(selection, flags);
}
}
d->initialPressPosition.setX(d->initialPressPosition.x() +
horizontalOffset());
}
+ else if (event->button() == Qt::RightButton)
+ {
+ d->rightMouseButtonPressed = true;
+ }
QListView::mousePressEvent(event);
+ d->lastSelection = selectionModel()->selection();
+
viewport()->update(d->categoryVisualRect(d->hoveredCategory));
}
void KCategorizedView::mouseReleaseEvent(QMouseEvent *event)
{
d->mouseButtonPressed = false;
+ d->rightMouseButtonPressed = false;
QListView::mouseReleaseEvent(event);
selection << QItemSelectionRange(selectIndex);
}
- selectionModel()->select(selection, QItemSelectionModel::SelectCurrent);
+ selectionModel()->select(selection, QItemSelectionModel::Select);
break;
}
d->isDragging = false;
d->mouseButtonPressed = false;
+ d->rightMouseButtonPressed = false;
viewport()->update(d->lastDraggedItemsRect);
}
if (!current.isValid())
{
current = model()->index(0, 0, QModelIndex());
- setCurrentIndex(current);
+ selectionModel()->select(current, QItemSelectionModel::NoUpdate);
d->forcedSelectionPosition = 0;
return current;
if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
!d->categoryDrawer || !d->proxyModel->isCategorizedModel())
{
- d->lastSelection = QItemSelection();
- d->currentViewIndex = QModelIndex();
d->forcedSelectionPosition = 0;
d->elementsInfo.clear();
d->elementsPosition.clear();
d->hovered = QModelIndex();
d->biggestItemSize = QSize(0, 0);
d->mouseButtonPressed = false;
+ d->rightMouseButtonPressed = false;
return;
}
{
Q_UNUSED(parent);
- d->lastSelection = QItemSelection();
- d->currentViewIndex = QModelIndex();
d->forcedSelectionPosition = 0;
d->elementsInfo.clear();
d->elementsPosition.clear();
d->hovered = QModelIndex();
d->biggestItemSize = QSize(0, 0);
d->mouseButtonPressed = false;
+ d->rightMouseButtonPressed = false;
if (start > end || end < 0 || start < 0 || !d->proxyModel->rowCount())
{
d->categories << prevCategory;
d->updateScrollbars();
+
+ // FIXME: We need to safely save the last selection. This is on my TODO
+ // list (ereslibre).
+ selectionModel()->clear();
}
void KCategorizedView::rowsRemoved(const QModelIndex &parent,
d->layoutChanged();
}
+void KCategorizedView::currentChanged(const QModelIndex ¤t,
+ const QModelIndex &previous)
+{
+ // We need to update the forcedSelectionPosition property in order to correctly
+ // navigate after with keyboard using up & down keys
+
+ int viewportWidth = viewport()->width() - spacing();
+
+ int itemHeight;
+ int itemWidth;
+
+ if (gridSize().isEmpty())
+ {
+ itemHeight = d->biggestItemSize.height();
+ itemWidth = d->biggestItemSize.width();
+ }
+ else
+ {
+ itemHeight = gridSize().height();
+ itemWidth = gridSize().width();
+ }
+
+ int itemWidthPlusSeparation = spacing() + itemWidth;
+ int elementsPerRow = viewportWidth / itemWidthPlusSeparation;
+ if (!elementsPerRow)
+ elementsPerRow++;
+
+ d->forcedSelectionPosition = d->elementsInfo[current.row()].relativeOffsetToCategory % elementsPerRow;
+
+ QListView::currentChanged(current, previous);
+}
+
#include "kcategorizedview.moc"