]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Places Panel: Show drop indicator
authorPeter Penz <peter.penz19@gmail.com>
Mon, 21 May 2012 19:43:26 +0000 (21:43 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Mon, 21 May 2012 19:48:45 +0000 (21:48 +0200)
The dropping itself has not been implemented yet.

src/kitemviews/kitemlistcontroller.cpp
src/kitemviews/kitemlistview.cpp
src/kitemviews/kitemlistview.h
src/kitemviews/kstandarditemmodel.cpp
src/panels/places/placesitemmodel.cpp
src/panels/places/placesitemmodel.h
src/panels/places/placespanel.cpp

index b8d719d3bb82c81debb60e01a6b6de3798cb4510..645b2d34a0ecc6a30275baf20d1e28663c75d103 100644 (file)
@@ -758,6 +758,7 @@ bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent* event, con
     Q_UNUSED(transform);
 
     m_view->setAutoScroll(false);
+    m_view->hideDropIndicator();
 
     KItemListWidget* widget = hoveredWidget();
     if (widget) {
@@ -793,6 +794,13 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, cons
             const int index = newHoveredWidget->index();
             if (m_model->supportsDropping(index)) {
                 newHoveredWidget->setHovered(true);
+            } else if (m_model->sortRole().isEmpty()) {
+                // The model supports the inserting of items on
+                // the given index as no sort-role has been
+                // specified. Assure that a drag-indicator
+                // is shown by the view.
+                const int dropIndex = m_view->showDropIndicator(pos);
+                Q_UNUSED(dropIndex); // TODO
             }
             emit itemHovered(index);
 
index ebcf48680388b28512b304b31d2be84afc0d82a7..1586c9d96bd705cc304e5541b5b6df89a3987f87 100644 (file)
@@ -84,7 +84,8 @@ KItemListView::KItemListView(QGraphicsWidget* parent) :
     m_autoScrollIncrement(0),
     m_autoScrollTimer(0),
     m_header(0),
-    m_headerWidget(0)
+    m_headerWidget(0),
+    m_dropIndicator()
 {
     setAcceptHoverEvents(true);
 
@@ -622,6 +623,22 @@ void KItemListView::paint(QPainter* painter, const QStyleOptionGraphicsItem* opt
         opt.rect = rubberBandRect.toRect();
         style()->drawControl(QStyle::CE_RubberBand, &opt, painter);
     }
+
+    if (!m_dropIndicator.isEmpty()) {
+        const QRectF r = m_dropIndicator.toRect();
+
+        QColor color = palette().brush(QPalette::Normal, QPalette::Highlight).color();
+        painter->setPen(color);
+
+        // TODO: The following implementation works only for a vertical scroll-orientation
+        // and assumes a height of the m_draggingInsertIndicator of 1.
+        Q_ASSERT(r.height() == 1);
+        painter->drawLine(r.left() + 1, r.top(), r.right() - 1, r.top());
+
+        color.setAlpha(128);
+        painter->setPen(color);
+        painter->drawRect(r.left(), r.top() - 1, r.width() - 1, 2);
+    }
 }
 
 void KItemListView::setItemSize(const QSizeF& size)
@@ -2286,6 +2303,39 @@ bool KItemListView::scrollBarRequired(const QSizeF& size) const
                                                            : maxOffset > size.width();
 }
 
+int KItemListView::showDropIndicator(const QPointF& pos)
+{
+    QHashIterator<int, KItemListWidget*> it(m_visibleItems);
+    while (it.hasNext()) {
+        it.next();
+        const KItemListWidget* widget = it.value();
+
+        const QPointF mappedPos = widget->mapFromItem(this, pos);
+        const QRectF rect = itemRect(widget->index());
+        if (mappedPos.y() >= 0 && mappedPos.y() <= rect.height()) {
+            const qreal y = (mappedPos.y () < rect.height() / 2) ?
+                            rect.top() : rect.bottom();
+
+            const QRectF draggingInsertIndicator(rect.left(), y, rect.width(), 1);
+            if (m_dropIndicator != draggingInsertIndicator) {
+                m_dropIndicator = draggingInsertIndicator;
+                update();
+            }
+            return widget->index();
+        }
+    }
+
+    return -1;
+}
+
+void KItemListView::hideDropIndicator()
+{
+    if (!m_dropIndicator.isNull()) {
+        m_dropIndicator = QRectF();
+        update();
+    }
+}
+
 void KItemListView::updateGroupHeaderHeight()
 {
     qreal groupHeaderHeight = m_styleOption.fontMetrics.height();
index de2d2a6bb243c8d431ef1504dd257f6e69d7ab13..de40791da0bf5fded2e9662a133dc13a8accfb03 100644 (file)
@@ -151,6 +151,10 @@ public:
     void setGroupHeaderCreator(KItemListGroupHeaderCreatorBase* groupHeaderCreator);
     KItemListGroupHeaderCreatorBase* groupHeaderCreator() const;
 
+    /**
+     * @return The basic size of all items. The size of an item may be larger than
+     *         the basic size (see KItemListView::itemSizeHint() and KItemListView::itemRect()).
+     */
     QSizeF itemSize() const;
 
     const KItemListStyleOption& styleOption() const;
@@ -626,6 +630,15 @@ private:
      */
     bool scrollBarRequired(const QSizeF& size) const;
 
+    /**
+     * 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.
+     */
+    int showDropIndicator(const QPointF& pos);
+    void hideDropIndicator();
+
     /**
      * Applies the height of the group header to the layouter. The height
      * depends on the used scroll orientation.
@@ -716,6 +729,14 @@ private:
     KItemListHeader* m_header;
     KItemListHeaderWidget* m_headerWidget;
 
+    // When dragging items into the view where the sort-role of the model
+    // is empty, a visual indicator should be shown during dragging where
+    // the dropping will happen. This indicator is specified by an index
+    // of the item. -1 means that no indicator will be shown at all.
+    // The m_dropIndicator is set by the KItemListController
+    // by KItemListView::showDropIndicator() and KItemListView::hideDropIndicator().
+    QRectF m_dropIndicator;
+
     friend class KItemListContainer; // Accesses scrollBarRequired()
     friend class KItemListHeader;    // Accesses m_headerWidget
     friend class KItemListController;
index c5117fd65638fc807bea481a15ac62ceb0034565..dbf608c92734fc8fa9808849030499675425443c 100644 (file)
@@ -195,7 +195,7 @@ QList<QPair<int, QVariant> > KStandardItemModel::groups() const
 {
     QList<QPair<int, QVariant> > groups;
 
-    const QByteArray role = sortRole();
+    const QByteArray role = sortRole().isEmpty() ? "group" : sortRole();
     bool isFirstGroupValue = true;
     QString groupValue;
     const int maxIndex = count() - 1;
index 68311bbabcda465e6a836eb1bcee4b1c53082dbb..2a3dfa441826c83e83c9f065d229c0accc49c891 100644 (file)
@@ -339,12 +339,6 @@ QMimeData* PlacesItemModel::createMimeData(const QSet<int>& indexes) const
     return mimeData;
 }
 
-bool PlacesItemModel::supportsDropping(int index) const
-{
-    Q_UNUSED(index);
-    return true;
-}
-
 KUrl PlacesItemModel::convertedUrl(const KUrl& url)
 {
     KUrl newUrl = url;
index 7225c04f47b3e9d5e2a18e19f2c1eaa95e6c9acc..18264f06a8163837f0fb5f224b0684014aca5cc9 100644 (file)
@@ -108,9 +108,6 @@ public:
     /** @reimp */
     virtual QMimeData* createMimeData(const QSet<int>& indexes) const;
 
-    /** @reimp */
-    virtual bool supportsDropping(int index) const;
-
     /**
      * @return Converts the URL, which contains "virtual" URLs for system-items like
      *         "search:/documents" into a Nepomuk-Query-URL that will be handled by
index 0229d3cac23115781d5573177120a1e404d144a6..ccf9d8cffc11783cbaf18f5e027976004a65d5f4 100644 (file)
@@ -76,7 +76,6 @@ void PlacesPanel::showEvent(QShowEvent* event)
         // used at all and stays invisible.
         m_model = new PlacesItemModel(this);
         m_model->setGroupedSorting(true);
-        m_model->setSortRole("group");
         connect(m_model, SIGNAL(errorMessage(QString)),
                 this, SIGNAL(errorMessage(QString)));