]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistview.cpp
Enable basic drag and drop support for the Places Panel
[dolphin.git] / src / kitemviews / kitemlistview.cpp
index ae86b28422a74b6b61e01a50649f4b50871980c3..ebcf48680388b28512b304b31d2be84afc0d82a7 100644 (file)
@@ -36,6 +36,7 @@
 
 #include <QCursor>
 #include <QGraphicsSceneMouseEvent>
+#include <QGraphicsView>
 #include <QPainter>
 #include <QPropertyAnimation>
 #include <QStyle>
@@ -566,8 +567,21 @@ KItemListHeader* KItemListView::header() const
 
 QPixmap KItemListView::createDragPixmap(const QSet<int>& indexes) const
 {
-    Q_UNUSED(indexes);
-    return QPixmap();
+    QPixmap pixmap;
+
+    if (indexes.count() == 1) {
+        KItemListWidget* item = m_visibleItems.value(indexes.toList().first());
+        QGraphicsView* graphicsView = scene()->views()[0];
+        if (item && graphicsView) {
+            pixmap = item->createDragPixmap(0, graphicsView);
+        }
+    } else {
+        // TODO: Not implemented yet. Probably extend the interface
+        // from KItemListWidget::createDragPixmap() to return a pixmap
+        // that can be used for multiple indexes.
+    }
+
+    return pixmap;
 }
 
 void KItemListView::editRole(int index, const QByteArray& role)
@@ -693,6 +707,10 @@ void KItemListView::setStyleOption(const KItemListStyleOption& option)
     m_layouter->markAsDirty();
     doLayout(animate ? Animation : NoAnimation);
 
+    if (m_itemSize.isEmpty()) {
+        updatePreferredColumnWidths();
+    }
+
     onStyleOptionChanged(option, previousOption);
 }
 
@@ -757,13 +775,8 @@ void KItemListView::onControllerChanged(KItemListController* current, KItemListC
 
 void KItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous)
 {
+    Q_UNUSED(current);
     Q_UNUSED(previous);
-
-    m_sizeHintResolver->clearCache();
-    const int itemCount = current->count();
-    if (itemCount > 0) {
-        m_sizeHintResolver->itemsInserted(0, itemCount);
-    }
 }
 
 void KItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
@@ -961,9 +974,17 @@ void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges)
 #endif
         m_endTransactionAnimationHint = NoAnimation;
         endTransaction();
+
         updateSiblingsInformation();
     }
 
+    if (m_grouped && (hasMultipleRanges || itemRanges.first().count < m_model->count())) {
+        // In case if items of the same group have been inserted before an item that
+        // currently represents the first item of the group, the group header of
+        // this item must be removed.
+        updateVisibleGroupHeaders();
+    }
+
     if (useAlternateBackgrounds()) {
         updateAlternateBackgrounds();
     }
@@ -1079,6 +1100,12 @@ void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
         updateSiblingsInformation();
     }
 
+    if (m_grouped && (hasMultipleRanges || m_model->count() > 0)) {
+        // In case if the first item of a group has been removed, the group header
+        // must be applied to the next visible item.
+        updateVisibleGroupHeaders();
+    }
+
     if (useAlternateBackgrounds()) {
         updateAlternateBackgrounds();
     }
@@ -1442,6 +1469,8 @@ void KItemListView::setModel(KItemModelBase* model)
                    this,    SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
     }
 
+    m_sizeHintResolver->clearCache();
+
     m_model = model;
     m_layouter->setModel(model);
     m_grouped = model->groupedSorting();
@@ -1461,6 +1490,12 @@ void KItemListView::setModel(KItemModelBase* model)
                 this,    SLOT(slotSortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
         connect(m_model, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
                 this,    SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
+
+        const int itemCount = m_model->count();
+        if (itemCount > 0) {
+            m_sizeHintResolver->itemsInserted(0, itemCount);
+            slotItemsInserted(KItemRangeList() << KItemRange(0, itemCount));
+        }
     }
 
     onModelChanged(model, previous);
@@ -1694,17 +1729,22 @@ bool KItemListView::moveWidget(KItemListWidget* widget,const QPointF& newPos)
 
     bool startMovingAnim = false;
 
-    // When having a grid the moving-animation should only be started, if it is done within
-    // one row in the vertical scroll-orientation or one column in the horizontal scroll-orientation.
-    // Otherwise instead of a moving-animation a create-animation on the new position will be used
-    // instead. This is done to prevent overlapping (and confusing) moving-animations.
-    const int index = widget->index();
-    const Cell cell = m_visibleCells.value(index);
-    if (cell.column >= 0 && cell.row >= 0) {
-        if (scrollOrientation() == Qt::Vertical) {
-            startMovingAnim = (cell.row == m_layouter->itemRow(index));
-        } else {
-            startMovingAnim = (cell.column == m_layouter->itemColumn(index));
+    if (m_itemSize.isEmpty()) {
+        // The items are not aligned in a grid but either as columns or rows.
+        startMovingAnim = !supportsItemExpanding();
+    } else {
+        // When having a grid the moving-animation should only be started, if it is done within
+        // one row in the vertical scroll-orientation or one column in the horizontal scroll-orientation.
+        // Otherwise instead of a moving-animation a create-animation on the new position will be used
+        // instead. This is done to prevent overlapping (and confusing) moving-animations.
+        const int index = widget->index();
+        const Cell cell = m_visibleCells.value(index);
+        if (cell.column >= 0 && cell.row >= 0) {
+            if (scrollOrientation() == Qt::Vertical) {
+                startMovingAnim = (cell.row == m_layouter->itemRow(index));
+            } else {
+                startMovingAnim = (cell.column == m_layouter->itemColumn(index));
+            }
         }
     }
 
@@ -1841,7 +1881,7 @@ void KItemListView::updateGroupHeaderForWidget(KItemListWidget* widget)
     }
 
     const QList<QPair<int, QVariant> > groups = model()->groups();
-    if (groups.isEmpty()) {
+    if (groups.isEmpty() || !groupHeaderCreator()) {
         return;
     }
 
@@ -1880,8 +1920,10 @@ void KItemListView::updateGroupHeaderLayout(KItemListWidget* widget)
         // In the vertical scroll orientation the group header should always span
         // the whole width no matter which temporary position the parent widget
         // has. In this case the x-position and width will be adjusted manually.
-        groupHeader->setPos(-widget->x(), -groupHeaderRect.height());
-        groupHeader->resize(size().width(), groupHeaderRect.size().height());
+        const qreal x = -widget->x() - itemOffset();
+        const qreal width = maximumItemOffset();
+        groupHeader->setPos(x, -groupHeaderRect.height());
+        groupHeader->resize(width, groupHeaderRect.size().height());
     } else {
         groupHeader->setPos(groupHeaderRect.x() - itemRect.x(), -widget->y());
         groupHeader->resize(groupHeaderRect.size());
@@ -2130,7 +2172,7 @@ void KItemListView::applyAutomaticColumnWidths()
         // Stretch the first column to use the whole remaining width
         firstColumnWidth += availableWidth - requiredWidth;
         m_headerWidget->setColumnWidth(firstRole, firstColumnWidth);
-    } else if (requiredWidth > availableWidth) {
+    } else if (requiredWidth > availableWidth && m_visibleRoles.count() > 1) {
         // Shrink the first column to be able to show as much other
         // columns as possible
         qreal shrinkedFirstColumnWidth = firstColumnWidth - requiredWidth + availableWidth;
@@ -2214,6 +2256,12 @@ bool KItemListView::changesItemGridLayout(const QSizeF& newGridSize,
 
 bool KItemListView::animateChangedItemCount(int changedItemCount) const
 {
+    if (m_itemSize.isEmpty()) {
+        // We have only columns or only rows, but no grid: An animation is usually
+        // welcome when inserting or removing items.
+        return !supportsItemExpanding();
+    }
+
     if (m_layouter->size().isEmpty() || m_layouter->itemSize().isEmpty()) {
         return false;
     }