diff options
| author | Peter Penz <[email protected]> | 2011-09-04 17:40:44 +0200 |
|---|---|---|
| committer | Peter Penz <[email protected]> | 2011-09-04 17:41:15 +0200 |
| commit | 7a91492cff931c0c4e0d38dd0aee77d9dcb29373 (patch) | |
| tree | 97c0e00397f95c673ff5dc3a4cf0dcb79a0134df /src/views | |
| parent | 8266e456a10670fe5ef855680d61e0b6ab0d6292 (diff) | |
Improved drag and drop support
Diffstat (limited to 'src/views')
| -rw-r--r-- | src/views/dolphinview.cpp | 19 | ||||
| -rw-r--r-- | src/views/dolphinview.h | 3 | ||||
| -rw-r--r-- | src/views/draganddrophelper.cpp | 146 | ||||
| -rw-r--r-- | src/views/draganddrophelper.h | 62 |
4 files changed, 42 insertions, 188 deletions
diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index 53171966c..200de7904 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -22,10 +22,12 @@ #include <QAbstractItemView> #include <QApplication> +#include <QBoxLayout> #include <QClipboard> +#include <QDropEvent> +#include <QGraphicsSceneDragDropEvent> #include <QKeyEvent> #include <QItemSelection> -#include <QBoxLayout> #include <QTimer> #include <QScrollBar> @@ -60,6 +62,7 @@ #include "dolphin_detailsmodesettings.h" #include "dolphin_generalsettings.h" #include "dolphinitemlistcontainer.h" +#include "draganddrophelper.h" #include "renamedialog.h" #include "settings/dolphinsettings.h" #include "viewmodecontroller.h" @@ -172,6 +175,7 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) : connect(controller, SIGNAL(itemExpansionToggleClicked(int)), this, SLOT(slotItemExpansionToggleClicked(int))); connect(controller, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int))); connect(controller, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int))); + connect(controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*))); KItemListSelectionManager* selectionManager = controller->selectionManager(); connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)), @@ -776,6 +780,19 @@ void DolphinView::slotItemUnhovered(int index) emit requestItemInfo(KFileItem()); } +void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event) +{ + const KFileItem destItem = fileItemModel()->fileItem(index); + + QDropEvent dropEvent(event->pos().toPoint(), + event->possibleActions(), + event->mimeData(), + event->buttons(), + event->modifiers()); + + DragAndDropHelper::dropUrls(destItem, url(), &dropEvent, this); +} + void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous) { const int currentCount = current.count(); diff --git a/src/views/dolphinview.h b/src/views/dolphinview.h index 085de332d..49da948d6 100644 --- a/src/views/dolphinview.h +++ b/src/views/dolphinview.h @@ -18,7 +18,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ - #ifndef DOLPHINVIEW_H #define DOLPHINVIEW_H @@ -48,6 +47,7 @@ class KFileItemModel; class KUrl; class ToolTipManager; class ViewProperties; +class QGraphicsSceneDragDropEvent; class QRegExp; /** @@ -559,6 +559,7 @@ private slots: void slotItemExpansionToggleClicked(int index); void slotItemHovered(int index); void slotItemUnhovered(int index); + void slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event); /** * Emits the signal \a selectionChanged() with a small delay. This is diff --git a/src/views/draganddrophelper.cpp b/src/views/draganddrophelper.cpp index bc1361b9a..6cd17b6ba 100644 --- a/src/views/draganddrophelper.cpp +++ b/src/views/draganddrophelper.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2007 by Peter Penz <[email protected]> * + * Copyright (C) 2007-2011 by Peter Penz <[email protected]> * * Copyright (C) 2007 by David Faure <[email protected]> * * * * This program is free software; you can redistribute it and/or modify * @@ -20,83 +20,25 @@ #include "draganddrophelper.h" -#include <KDirModel> #include <KFileItem> -#include <KIcon> #include <KLocale> #include <konq_operations.h> - -#include "views/dolphiniconsview.h" -#include "views/dolphinviewcontroller.h" - -#include <QAbstractItemView> -#include <QAbstractProxyModel> +#include <KUrl> #include <QtDBus> -#include <QDrag> -#include <QPainter> - -class DragAndDropHelperSingleton -{ -public: - DragAndDropHelper instance; -}; -K_GLOBAL_STATIC(DragAndDropHelperSingleton, s_dragAndDropHelper) - -DragAndDropHelper& DragAndDropHelper::instance() -{ - return s_dragAndDropHelper->instance; -} - -void DragAndDropHelper::startDrag(QAbstractItemView* itemView, - Qt::DropActions supportedActions, - DolphinViewController* dolphinViewController) -{ - // Do not start a new drag until the previous one has been finished. - // This is a (possibly temporary) fix for bug #187884. - static bool isDragging = false; - if (isDragging) { - return; - } - isDragging = true; - - const QModelIndexList indexes = itemView->selectionModel()->selectedIndexes(); - if (!indexes.isEmpty()) { - QMimeData *data = itemView->model()->mimeData(indexes); - if (!data) { - return; - } - - if (dolphinViewController) { - dolphinViewController->requestToolTipHiding(); - } - - QDrag* drag = new QDrag(itemView); - drag->setPixmap(createDragPixmap(itemView)); - drag->setMimeData(data); - - m_dragSource = itemView; - drag->exec(supportedActions, Qt::IgnoreAction); - m_dragSource = 0; - } - isDragging = false; -} - -bool DragAndDropHelper::isDragSource(QAbstractItemView* itemView) const -{ - return m_dragSource && (m_dragSource == itemView); -} +#include <QDropEvent> +#include <QWidget> -void DragAndDropHelper::dropUrls(const KFileItem& destItem, - const KUrl& destPath, - QDropEvent* event, - QWidget* widget) +QString DragAndDropHelper::dropUrls(const KFileItem& destItem, + const KUrl& destPath, + QDropEvent* event, + QWidget* widget) { const bool dropToItem = !destItem.isNull() && (destItem.isDir() || destItem.isDesktopFile()); const KUrl destination = dropToItem ? destItem.url() : destPath; const QMimeData* mimeData = event->mimeData(); if (mimeData->hasFormat("application/x-kde-dndextract")) { - QString remoteDBusClient = mimeData->data("application/x-kde-dndextract"); + const QString remoteDBusClient = mimeData->data("application/x-kde-dndextract"); QDBusMessage message = QDBusMessage::createMethodCall(remoteDBusClient, "/DndExtract", "org.kde.DndExtract", "extractSelectedFilesTo"); message.setArguments(QVariantList() << destination.pathOrUrl()); @@ -104,79 +46,15 @@ void DragAndDropHelper::dropUrls(const KFileItem& destItem, } else { const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData()); const int urlsCount = urls.count(); - if ((urlsCount == 1) && (urls.first() == destination)) { - emit errorMessage(i18nc("@info:status", "A folder cannot be dropped into itself")); + if (urlsCount == 1 && urls.first() == destination) { + return i18nc("@info:status", "A folder cannot be dropped into itself"); } else if (dropToItem) { KonqOperations::doDrop(destItem, destination, event, widget); } else { KonqOperations::doDrop(KFileItem(), destination, event, widget); } } -} - -DragAndDropHelper::DragAndDropHelper() - : m_dragSource(0) -{ -} - -QPixmap DragAndDropHelper::createDragPixmap(QAbstractItemView* itemView) const -{ - const QModelIndexList selectedIndexes = itemView->selectionModel()->selectedIndexes(); - Q_ASSERT(!selectedIndexes.isEmpty()); - - QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(itemView->model()); - KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel()); - - const int itemCount = selectedIndexes.count(); - - // If more than one item is dragged, align the items inside a - // rectangular grid. The maximum grid size is limited to 5 x 5 items. - int xCount = 3; - int size = KIconLoader::SizeMedium; - if (itemCount > 16) { - xCount = 5; - size = KIconLoader::SizeSmall; - } else if (itemCount > 9) { - xCount = 4; - size = KIconLoader::SizeSmallMedium; - } - - if (itemCount < xCount) { - xCount = itemCount; - } - - int yCount = itemCount / xCount; - if (itemCount % xCount != 0) { - ++yCount; - } - if (yCount > xCount) { - yCount = xCount; - } - - // Draw the selected items into the grid cells - QPixmap dragPixmap(xCount * size + xCount - 1, yCount * size + yCount - 1); - dragPixmap.fill(Qt::transparent); - - QPainter painter(&dragPixmap); - int x = 0; - int y = 0; - foreach (const QModelIndex& selectedIndex, selectedIndexes) { - const QModelIndex index = proxyModel->mapToSource(selectedIndex); - const KFileItem item = dirModel->itemForIndex(index); - const QPixmap pixmap = item.pixmap(size, size); - painter.drawPixmap(x, y, pixmap); - - x += size + 1; - if (x >= dragPixmap.width()) { - x = 0; - y += size + 1; - } - if (y >= dragPixmap.height()) { - break; - } - } - return dragPixmap; + return QString(); } -#include "draganddrophelper.moc" diff --git a/src/views/draganddrophelper.h b/src/views/draganddrophelper.h index b107efc18..85e47077d 100644 --- a/src/views/draganddrophelper.h +++ b/src/views/draganddrophelper.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2007 by Peter Penz <[email protected]> * + * Copyright (C) 2007-2011 by Peter Penz <[email protected]> * * Copyright (C) 2007 by David Faure <[email protected]> * * * * This program is free software; you can redistribute it and/or modify * @@ -22,44 +22,17 @@ #define DRAGANDDROPHELPER_H #include "libdolphin_export.h" -#include <QObject> -#include <QPixmap> -class DolphinViewController; +#include <QString> + class KFileItem; class KUrl; class QDropEvent; -class QAbstractItemView; -class QMimeData; class QWidget; -/** - * @brief Helper class for having a common drag and drop behavior. - * - * The class is used by DolphinIconsView, DolphinDetailsView, - * DolphinColumnView and PanelTreeView to have a consistent - * drag and drop behavior between all views. - */ -class LIBDOLPHINPRIVATE_EXPORT DragAndDropHelper : public QObject +class LIBDOLPHINPRIVATE_EXPORT DragAndDropHelper { - Q_OBJECT - public: - static DragAndDropHelper& instance(); - - /** - * Creates a drag object for the view \a itemView for all selected items. - */ - void startDrag(QAbstractItemView* itemView, - Qt::DropActions supportedActions, - DolphinViewController* dolphinViewController = 0); - - /** - * Returns true if and only if the view \a itemView was the last view to - * be passed to startDrag(...), and that drag is still in progress. - */ - bool isDragSource(QAbstractItemView* itemView) const; - /** * Handles the dropping of URLs to the given * destination. A context menu with the options @@ -69,28 +42,13 @@ public: * @param destPath Path of the destination. * @param event Drop event. * @param widget Source widget where the dragging has been started. + * @return Error message if dropping is not possible. If an empty string + * is returned, the dropping has been successful. */ - void dropUrls(const KFileItem& destItem, - const KUrl& destPath, - QDropEvent* event, - QWidget* widget); -signals: - void errorMessage(const QString& msg); - -private: - DragAndDropHelper(); - - /** - * Creates a pixmap the contains the all icons of the items - * that are dragged. - */ - QPixmap createDragPixmap(QAbstractItemView* itemView) const; - - // The last view passed in startDrag(...), or 0 if - // no startDrag(...) initiated drag is in progress. - QAbstractItemView *m_dragSource; - - friend class DragAndDropHelperSingleton; + static QString dropUrls(const KFileItem& destItem, + const KUrl& destPath, + QDropEvent* event, + QWidget* widget); }; #endif |
