]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kcategorizedview.cpp
Use a QActionGroup and save 6 slots (and the duplication of the action names in the...
[dolphin.git] / src / kcategorizedview.cpp
index 1898799534c9817dd76bf10bfbeecb88426328f1..648a712a5373cc1c168cc24e2757c961b24ff16d 100644 (file)
@@ -129,12 +129,12 @@ QRect KCategorizedView::Private::visualRectInViewport(const QModelIndex &index)
     if (listView->layoutDirection() == Qt::LeftToRight)
     {
         retRect = QRect(listView->spacing(), listView->spacing() * 2 +
-                        categoryDrawer->categoryHeight(listView->viewOptions()), 0, 0);
+                        categoryDrawer->categoryHeight(index, listView->viewOptions()), 0, 0);
     }
     else
     {
         retRect = QRect(listView->viewport()->width() - listView->spacing(), listView->spacing() * 2 +
-                        categoryDrawer->categoryHeight(listView->viewOptions()), 0, 0);
+                        categoryDrawer->categoryHeight(index, listView->viewOptions()), 0, 0);
     }
 
     int viewportWidth = listView->viewport()->width() - listView->spacing();
@@ -189,7 +189,7 @@ QRect KCategorizedView::Private::visualRectInViewport(const QModelIndex &index)
 
         retRect.setTop(retRect.top() +
                        (rowsInt * itemHeight) +
-                       categoryDrawer->categoryHeight(listView->viewOptions()) +
+                       categoryDrawer->categoryHeight(index, listView->viewOptions()) +
                        listView->spacing() * 2);
 
         if (listView->gridSize().isEmpty())
@@ -273,7 +273,7 @@ QRect KCategorizedView::Private::visualCategoryRectInViewport(const QString &cat
 
         retRect.setTop(retRect.top() +
                        (rowsInt * itemHeight) +
-                       categoryDrawer->categoryHeight(listView->viewOptions()) +
+                       categoryDrawer->categoryHeight(index, listView->viewOptions()) +
                        listView->spacing() * 2);
 
         if (listView->gridSize().isEmpty())
@@ -283,7 +283,7 @@ QRect KCategorizedView::Private::visualCategoryRectInViewport(const QString &cat
         }
     }
 
-    retRect.setHeight(categoryDrawer->categoryHeight(listView->viewOptions()));
+    retRect.setHeight(categoryDrawer->categoryHeight(index, listView->viewOptions()));
 
     return retRect;
 }
@@ -365,19 +365,21 @@ void KCategorizedView::Private::drawNewCategory(const QModelIndex &index,
 
     optionCopy.state &= ~QStyle::State_Selected;
 
-    if ((category == hoveredCategory) && !mouseButtonPressed)
-    {
-        optionCopy.state |= QStyle::State_MouseOver;
-    }
-    else if ((category == hoveredCategory) && mouseButtonPressed)
-    {
-        QPoint initialPressPosition = listView->viewport()->mapFromGlobal(QCursor::pos());
-        initialPressPosition.setY(initialPressPosition.y() + listView->verticalOffset());
-        initialPressPosition.setX(initialPressPosition.x() + listView->horizontalOffset());
-
-        if (initialPressPosition == this->initialPressPosition)
+    if ((listView->selectionMode() != SingleSelection) && (listView->selectionMode() != NoSelection)) {
+        if ((category == hoveredCategory) && !mouseButtonPressed)
         {
-            optionCopy.state |= QStyle::State_Selected;
+            optionCopy.state |= QStyle::State_MouseOver;
+        }
+        else if ((category == hoveredCategory) && mouseButtonPressed)
+        {
+            QPoint initialPressPosition = listView->viewport()->mapFromGlobal(QCursor::pos());
+            initialPressPosition.setY(initialPressPosition.y() + listView->verticalOffset());
+            initialPressPosition.setX(initialPressPosition.x() + listView->horizontalOffset());
+
+            if (initialPressPosition == this->initialPressPosition)
+            {
+                optionCopy.state |= QStyle::State_Selected;
+            }
         }
     }
 
@@ -394,7 +396,7 @@ void KCategorizedView::Private::updateScrollbars()
     QModelIndex lastIndex = categoriesIndexes.isEmpty() ? QModelIndex() : categoriesIndexes[categories.last()].last();
 
     int lastItemBottom = cachedRectIndex(lastIndex).top() +
-                         listView->spacing() + (listView->gridSize().isEmpty() ? 0 : listView->gridSize().height()) - listView->viewport()->height();
+                         listView->spacing() + (listView->gridSize().isEmpty() ? biggestItemSize.height() : listView->gridSize().height()) - listView->viewport()->height();
 
     listView->horizontalScrollBar()->setRange(0, 0);
 
@@ -662,8 +664,6 @@ QModelIndex KCategorizedView::indexAt(const QPoint &point) const
         index = item[0];
     }
 
-    d->hovered = index;
-
     return index;
 }
 
@@ -744,7 +744,10 @@ void KCategorizedView::paintEvent(QPaintEvent *event)
                 option.state |= QStyle::State_Editing;
         }
 
-        if ((index == d->hovered) && !d->mouseButtonPressed)
+        // we are only interested to give the mouse over feedback when no
+        // dragging is happening (ereslibre)
+        if ((index == d->hovered) && !d->mouseButtonPressed &&
+            (this->state() == QAbstractItemView::NoState))
             option.state |= QStyle::State_MouseOver;
         else
             option.state &= ~QStyle::State_MouseOver;
@@ -773,39 +776,42 @@ void KCategorizedView::paintEvent(QPaintEvent *event)
         else if (intersectedInThePast)
         {
             break; // the visible area has been finished, we don't need to keep asking, the rest won't intersect
-                   // this is doable because we know that categories are correctly ordered on the list
+                // this is doable because we know that categories are correctly ordered on the list
         }
     }
 
-    if (d->mouseButtonPressed && !d->isDragging)
+    if ((selectionMode() != SingleSelection) && (selectionMode() != NoSelection))
     {
-        QPoint start, end, initialPressPosition;
+        if (d->mouseButtonPressed && !d->isDragging)
+        {
+            QPoint start, end, initialPressPosition;
 
-        initialPressPosition = d->initialPressPosition;
+            initialPressPosition = d->initialPressPosition;
 
-        initialPressPosition.setY(initialPressPosition.y() - verticalOffset());
-        initialPressPosition.setX(initialPressPosition.x() - horizontalOffset());
+            initialPressPosition.setY(initialPressPosition.y() - verticalOffset());
+            initialPressPosition.setX(initialPressPosition.x() - horizontalOffset());
 
-        if (d->initialPressPosition.x() > d->mousePosition.x() ||
-            d->initialPressPosition.y() > d->mousePosition.y())
-        {
-            start = d->mousePosition;
-            end = initialPressPosition;
-        }
-        else
-        {
-            start = initialPressPosition;
-            end = d->mousePosition;
-        }
+            if (d->initialPressPosition.x() > d->mousePosition.x() ||
+                d->initialPressPosition.y() > d->mousePosition.y())
+            {
+                start = d->mousePosition;
+                end = initialPressPosition;
+            }
+            else
+            {
+                start = initialPressPosition;
+                end = d->mousePosition;
+            }
 
-        QStyleOptionRubberBand yetAnotherOption;
-        yetAnotherOption.initFrom(this);
-        yetAnotherOption.shape = QRubberBand::Rectangle;
-        yetAnotherOption.opaque = false;
-        yetAnotherOption.rect = QRect(start, end).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
-        painter.save();
-        style()->drawControl(QStyle::CE_RubberBand, &yetAnotherOption, &painter);
-        painter.restore();
+            QStyleOptionRubberBand yetAnotherOption;
+            yetAnotherOption.initFrom(this);
+            yetAnotherOption.shape = QRubberBand::Rectangle;
+            yetAnotherOption.opaque = false;
+            yetAnotherOption.rect = QRect(start, end).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
+            painter.save();
+            style()->drawControl(QStyle::CE_RubberBand, &yetAnotherOption, &painter);
+            painter.restore();
+        }
     }
 
     if (d->isDragging && !d->dragLeftViewport)
@@ -986,6 +992,17 @@ void KCategorizedView::mouseMoveEvent(QMouseEvent *event)
         return;
     }
 
+    QModelIndexList item = d->intersectionSet(QRect(event->pos(), event->pos()));
+
+    if (item.count() == 1)
+    {
+        d->hovered = item[0];
+    }
+    else
+    {
+        d->hovered = QModelIndex();
+    }
+
     const QString previousHoveredCategory = d->hoveredCategory;
 
     d->mousePosition = event->pos();
@@ -1028,7 +1045,10 @@ void KCategorizedView::mouseMoveEvent(QMouseEvent *event)
             end = d->mousePosition;
         }
 
-        rect = QRect(start, end).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
+        rect = QRect(start, end).adjusted(-16, -16, 16, 16);
+        rect = rect.united(QRect(start, end).adjusted(16, 16, -16, -16)).intersected(viewport()->rect());
+
+        viewport()->update(rect);
     }
 }
 
@@ -1075,13 +1095,14 @@ void KCategorizedView::mouseReleaseEvent(QMouseEvent *event)
     initialPressPosition.setY(initialPressPosition.y() + verticalOffset());
     initialPressPosition.setX(initialPressPosition.x() + horizontalOffset());
 
-    if (initialPressPosition == d->initialPressPosition)
+    if ((selectionMode() != SingleSelection) && (selectionMode() != NoSelection) &&
+        (initialPressPosition == d->initialPressPosition))
     {
         foreach(const QString &category, d->categories)
         {
             if (d->categoryVisualRect(category).contains(event->pos()))
             {
-                QItemSelection selection;
+                QItemSelection selection = selectionModel()->selection();
                 QModelIndexList indexList = d->categoriesIndexes[category];
 
                 foreach (const QModelIndex &index, indexList)
@@ -1091,13 +1112,41 @@ void KCategorizedView::mouseReleaseEvent(QMouseEvent *event)
                     selection << QItemSelectionRange(selectIndex);
                 }
 
-                selectionModel()->select(selection, QItemSelectionModel::Select);
+                selectionModel()->select(selection, QItemSelectionModel::SelectCurrent);
 
                 break;
             }
         }
     }
 
+    QRect rect;
+    if (!d->isDragging)
+    {
+        QPoint start, end, initialPressPosition;
+
+        initialPressPosition = d->initialPressPosition;
+
+        initialPressPosition.setY(initialPressPosition.y() - verticalOffset());
+        initialPressPosition.setX(initialPressPosition.x() - horizontalOffset());
+
+        if (d->initialPressPosition.x() > d->mousePosition.x() ||
+            d->initialPressPosition.y() > d->mousePosition.y())
+        {
+            start = d->mousePosition;
+            end = initialPressPosition;
+        }
+        else
+        {
+            start = initialPressPosition;
+            end = d->mousePosition;
+        }
+
+        rect = QRect(start, end).adjusted(-16, -16, 16, 16);
+        rect = rect.united(QRect(start, end).adjusted(16, 16, -16, -16)).intersected(viewport()->rect());
+
+        viewport()->update(rect);
+    }
+
     if (d->hovered.isValid())
         viewport()->update(visualRect(d->hovered));
     else if (!d->hoveredCategory.isEmpty())
@@ -1145,14 +1194,15 @@ void KCategorizedView::dragMoveEvent(QDragMoveEvent *event)
 
     d->dragLeftViewport = false;
 
-    if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
-        !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
-    {
 #if defined(DOLPHIN_DRAGANDDROP)
-        QAbstractItemView::dragMoveEvent(event);
+    QAbstractItemView::dragMoveEvent(event);
 #else
-        QListView::dragMoveEvent(event);
+    QListView::dragMoveEvent(event);
 #endif
+
+    if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
+        !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
+    {
         return;
     }
 
@@ -1192,17 +1242,6 @@ QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction,
         return QListView::moveCursor(cursorAction, modifiers);
     }
 
-    QModelIndex current = selectionModel()->currentIndex();
-
-    if (!current.isValid())
-    {
-        current = model()->index(0, 0, QModelIndex());
-        selectionModel()->select(current, QItemSelectionModel::NoUpdate);
-        d->forcedSelectionPosition = 0;
-
-        return current;
-    }
-
     int viewportWidth = viewport()->width() - spacing();
     int itemWidth;
 
@@ -1220,6 +1259,28 @@ QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction,
     if (!elementsPerRow)
         elementsPerRow++;
 
+    QModelIndex current = selectionModel()->currentIndex();
+
+    if (!current.isValid())
+    {
+        if (cursorAction == MoveEnd)
+        {
+            current = model()->index(model()->rowCount() - 1, 0, QModelIndex());
+            d->forcedSelectionPosition = d->elementsInfo[current.row()].relativeOffsetToCategory % elementsPerRow;
+        }
+        else
+        {
+            current = model()->index(0, 0, QModelIndex());
+            d->forcedSelectionPosition = 0;
+        }
+
+        return current;
+    }
+    else if (!current.isValid())
+    {
+        return QModelIndex();
+    }
+
     QString lastCategory = d->categories.first();
     QString theCategory = d->categories.first();
     QString afterCategory = d->categories.first();
@@ -1526,7 +1587,8 @@ void KCategorizedView::currentChanged(const QModelIndex &current,
     if (!elementsPerRow)
         elementsPerRow++;
 
-    d->forcedSelectionPosition = d->elementsInfo[current.row()].relativeOffsetToCategory % elementsPerRow;
+    if (d->mouseButtonPressed || d->rightMouseButtonPressed)
+        d->forcedSelectionPosition = d->elementsInfo[current.row()].relativeOffsetToCategory % elementsPerRow;
 
     QListView::currentChanged(current, previous);
 }