From: Peter Penz Date: Mon, 21 May 2012 19:43:26 +0000 (+0200) Subject: Places Panel: Show drop indicator X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/894232ebda5b2cf155a4f4e5bf1287eb700faa18 Places Panel: Show drop indicator The dropping itself has not been implemented yet. --- diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp index b8d719d3b..645b2d34a 100644 --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -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); diff --git a/src/kitemviews/kitemlistview.cpp b/src/kitemviews/kitemlistview.cpp index ebcf48680..1586c9d96 100644 --- a/src/kitemviews/kitemlistview.cpp +++ b/src/kitemviews/kitemlistview.cpp @@ -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 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(); diff --git a/src/kitemviews/kitemlistview.h b/src/kitemviews/kitemlistview.h index de2d2a6bb..de40791da 100644 --- a/src/kitemviews/kitemlistview.h +++ b/src/kitemviews/kitemlistview.h @@ -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; diff --git a/src/kitemviews/kstandarditemmodel.cpp b/src/kitemviews/kstandarditemmodel.cpp index c5117fd65..dbf608c92 100644 --- a/src/kitemviews/kstandarditemmodel.cpp +++ b/src/kitemviews/kstandarditemmodel.cpp @@ -195,7 +195,7 @@ QList > KStandardItemModel::groups() const { QList > groups; - const QByteArray role = sortRole(); + const QByteArray role = sortRole().isEmpty() ? "group" : sortRole(); bool isFirstGroupValue = true; QString groupValue; const int maxIndex = count() - 1; diff --git a/src/panels/places/placesitemmodel.cpp b/src/panels/places/placesitemmodel.cpp index 68311bbab..2a3dfa441 100644 --- a/src/panels/places/placesitemmodel.cpp +++ b/src/panels/places/placesitemmodel.cpp @@ -339,12 +339,6 @@ QMimeData* PlacesItemModel::createMimeData(const QSet& indexes) const return mimeData; } -bool PlacesItemModel::supportsDropping(int index) const -{ - Q_UNUSED(index); - return true; -} - KUrl PlacesItemModel::convertedUrl(const KUrl& url) { KUrl newUrl = url; diff --git a/src/panels/places/placesitemmodel.h b/src/panels/places/placesitemmodel.h index 7225c04f4..18264f06a 100644 --- a/src/panels/places/placesitemmodel.h +++ b/src/panels/places/placesitemmodel.h @@ -108,9 +108,6 @@ public: /** @reimp */ virtual QMimeData* createMimeData(const QSet& 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 diff --git a/src/panels/places/placespanel.cpp b/src/panels/places/placespanel.cpp index 0229d3cac..ccf9d8cff 100644 --- a/src/panels/places/placespanel.cpp +++ b/src/panels/places/placespanel.cpp @@ -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)));