]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Implement dropping of items into the Places Panel
authorPeter Penz <peter.penz19@gmail.com>
Mon, 28 May 2012 21:10:20 +0000 (23:10 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Mon, 28 May 2012 21:11:52 +0000 (23:11 +0200)
Some polishing regarding the icons and group-alignment must still
be done, but at least the basic functionality is back again.

src/kitemviews/kitemlistview.cpp
src/kitemviews/kitemlistview.h
src/panels/places/placesitemmodel.cpp
src/panels/places/placesitemmodel.h

index ee69c89904850209beea50d87274861756c53c29..5f4c5460687f3efd9f423d236701f68e7df1eace 100644 (file)
@@ -1616,6 +1616,11 @@ void KItemListView::doLayout(LayoutAnimationHint hint, int changedIndex, int cha
         }
 
         if (animate) {
+            if (m_animation->isStarted(widget, KItemListViewAnimation::MovingAnimation)) {
+                m_animation->start(widget,  KItemListViewAnimation::MovingAnimation, newPos);
+                applyNewPos = false;
+            }
+
             const bool itemsRemoved = (changedCount < 0);
             const bool itemsInserted = (changedCount > 0);
             if (itemsRemoved && (i >= changedIndex + changedCount + 1)) {
@@ -2330,7 +2335,8 @@ int KItemListView::showDropIndicator(const QPointF& pos)
         }
     }
 
-    return -1;
+    const QRectF firstItemRect = itemRect(firstVisibleIndex());
+    return (pos.y() <= firstItemRect.top()) ? 0 : -1;
 }
 
 void KItemListView::hideDropIndicator()
index de40791da0bf5fded2e9662a133dc13a8accfb03..5723b9aaab26019ecad698f94c9a855ace35766d 100644 (file)
@@ -634,7 +634,8 @@ private:
      * Shows a drop-indicator between items dependent on the given
      * cursor position. The cursor position is relative the the upper left
      * edge of the view.
-     * @return Index of the item where the dropping is done.
+     * @return Index of the item where the dropping is done. An index of -1
+     *         indicates that the item has been dropped after the last item.
      */
     int showDropIndicator(const QPointF& pos);
     void hideDropIndicator();
index bc9975c47689a704389858c7e45dcc023801dee9..aea26227783bfaadb3f04ed8a746480c55316130 100644 (file)
@@ -338,11 +338,46 @@ QMimeData* PlacesItemModel::createMimeData(const QSet<int>& indexes) const
 
 void PlacesItemModel::dropMimeData(int index, const QMimeData* mimeData)
 {
-    Q_UNUSED(index); // TODO
     if (mimeData->hasFormat(internalMimeType())) {
-        // TODO
+        // The item has been moved inside the view
+        QByteArray itemData = mimeData->data(internalMimeType());
+        QDataStream stream(&itemData, QIODevice::ReadOnly);
+        int oldIndex;
+        stream >> oldIndex;
+
+        PlacesItem* oldItem = placesItem(oldIndex);
+        if (!oldItem) {
+            return;
+        }
+
+        PlacesItem* newItem = new PlacesItem(oldItem->bookmark());
+        removeItem(oldIndex);
+
+        if (oldIndex <= index) {
+            --index;
+        }
+
+        const int dropIndex = groupedDropIndex(index, newItem);
+        insertItem(dropIndex, newItem);
     } else if (mimeData->hasFormat("text/uri-list")) {
-        // TODO
+        // One or more items must be added to the model
+        const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
+        for (int i = urls.count() - 1; i >= 0; --i) {
+            const KUrl& url = urls[i];
+
+            QString text = url.fileName();
+            if (text.isEmpty()) {
+                text = url.host();
+            }
+
+            KBookmark bookmark = PlacesItem::createBookmark(m_bookmarkManager,
+                                                            text,
+                                                            url,
+                                                            "folder");
+            PlacesItem* newItem = new PlacesItem(bookmark);
+            const int dropIndex = groupedDropIndex(index, newItem);
+            insertItem(dropIndex, newItem);
+        }
     }
 }
 
@@ -892,6 +927,50 @@ QString PlacesItemModel::internalMimeType() const
             QString::number((qptrdiff)this);
 }
 
+int PlacesItemModel::groupedDropIndex(int index, const PlacesItem* item) const
+{
+    Q_ASSERT(item);
+
+    int dropIndex = index;
+    const PlacesItem::GroupType type = item->groupType();
+
+    const int itemCount = count();
+    if (index < 0) {
+        dropIndex = itemCount;
+    }
+
+    // Search nearest previous item with the same group
+    int previousIndex = -1;
+    for (int i = dropIndex - 1; i >= 0; --i) {
+        if (placesItem(i)->groupType() == type) {
+            previousIndex = i;
+            break;
+        }
+    }
+
+    // Search nearest next item with the same group
+    int nextIndex = -1;
+    for (int i = dropIndex; i < count(); ++i) {
+        if (placesItem(i)->groupType() == type) {
+            nextIndex = i;
+            break;
+        }
+    }
+
+    // Adjust the drop-index to be inserted to the
+    // nearest item with the same group.
+    if (previousIndex >= 0 && nextIndex >= 0) {
+        dropIndex = (dropIndex - previousIndex < nextIndex - dropIndex) ?
+                    previousIndex + 1 : nextIndex;
+    } else if (previousIndex >= 0) {
+        dropIndex = previousIndex + 1;
+    } else if (nextIndex >= 0) {
+        dropIndex = nextIndex;
+    }
+
+    return dropIndex;
+}
+
 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark& b1, const KBookmark& b2)
 {
     const QString udi1 = b1.metaDataItem("UDI");
index 95a994e769dd950484a3ef93ce14a3886d2348f6..b850f356fb74030c9202cddbaf05bba9251fc21d 100644 (file)
@@ -199,6 +199,12 @@ private:
 
     QString internalMimeType() const;
 
+    /**
+     * @return Adjusted drop index which assures that the item is aligned
+     *         into the same group as specified by PlacesItem::groupType().
+     */
+    int groupedDropIndex(int index, const PlacesItem* item) const;
+
     /**
      * @return True if the bookmarks have the same identifiers. The identifier
      *         is the unique "ID"-property in case if no UDI is set, otherwise