1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2007 by David Faure <faure@kde.org> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "draganddrophelper.h"
22 #include "dolphiniconsview.h"
23 #include "dolphinviewcontroller.h"
25 #include <kdirmodel.h>
26 #include <kfileitem.h>
29 #include <konq_operations.h>
31 #include <QAbstractItemView>
32 #include <QAbstractProxyModel>
37 class DragAndDropHelperSingleton
40 DragAndDropHelper instance
;
42 K_GLOBAL_STATIC(DragAndDropHelperSingleton
, s_dragAndDropHelper
)
44 DragAndDropHelper
& DragAndDropHelper::instance()
46 return s_dragAndDropHelper
->instance
;
49 bool DragAndDropHelper::isMimeDataSupported(const QMimeData
* mimeData
) const
51 return mimeData
->hasUrls() || mimeData
->hasFormat("application/x-kde-dndextract");
54 void DragAndDropHelper::startDrag(QAbstractItemView
* itemView
,
55 Qt::DropActions supportedActions
,
56 DolphinViewController
* dolphinViewController
)
58 // Do not start a new drag until the previous one has been finished.
59 // This is a (possibly temporary) fix for bug #187884.
60 static bool isDragging
= false;
66 const QModelIndexList indexes
= itemView
->selectionModel()->selectedIndexes();
67 if (!indexes
.isEmpty()) {
68 QMimeData
*data
= itemView
->model()->mimeData(indexes
);
73 if (dolphinViewController
!= 0) {
74 dolphinViewController
->requestToolTipHiding();
77 QDrag
* drag
= new QDrag(itemView
);
78 drag
->setPixmap(createDragPixmap(itemView
));
79 drag
->setMimeData(data
);
81 m_dragSource
= itemView
;
82 drag
->exec(supportedActions
, Qt::IgnoreAction
);
88 bool DragAndDropHelper::isDragSource(QAbstractItemView
* itemView
) const
90 return (m_dragSource
!= 0) && (m_dragSource
== itemView
);
93 void DragAndDropHelper::dropUrls(const KFileItem
& destItem
,
98 const bool dropToItem
= !destItem
.isNull() && (destItem
.isDir() || destItem
.isDesktopFile());
99 const KUrl destination
= dropToItem
? destItem
.url() : destPath
;
101 const QMimeData
* mimeData
= event
->mimeData();
102 if (mimeData
->hasFormat("application/x-kde-dndextract")) {
103 QString remoteDBusClient
= mimeData
->data("application/x-kde-dndextract");
104 QDBusMessage message
= QDBusMessage::createMethodCall(remoteDBusClient
, "/DndExtract",
105 "org.kde.DndExtract", "extractSelectedFilesTo");
106 message
.setArguments(QVariantList() << destination
.path());
107 QDBusConnection::sessionBus().call(message
);
109 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
110 const int urlsCount
= urls
.count();
111 if (urlsCount
== 0) {
112 // TODO: handle dropping of other data
113 } else if ((urlsCount
== 1) && (urls
.first() == destination
)) {
114 emit
errorMessage(i18nc("@info:status", "A folder cannot be dropped into itself"));
115 } else if (dropToItem
) {
116 KonqOperations::doDrop(destItem
, destination
, event
, widget
);
118 KonqOperations::doDrop(KFileItem(), destination
, event
, widget
);
123 DragAndDropHelper::DragAndDropHelper()
128 QPixmap
DragAndDropHelper::createDragPixmap(QAbstractItemView
* itemView
) const
130 const QModelIndexList selectedIndexes
= itemView
->selectionModel()->selectedIndexes();
131 Q_ASSERT(!selectedIndexes
.isEmpty());
133 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(itemView
->model());
134 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
136 const int itemCount
= selectedIndexes
.count();
138 // If more than one item is dragged, align the items inside a
139 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
141 int size
= KIconLoader::SizeMedium
;
142 if (itemCount
> 16) {
144 size
= KIconLoader::SizeSmall
;
145 } else if (itemCount
> 9) {
147 size
= KIconLoader::SizeSmallMedium
;
150 if (itemCount
< xCount
) {
154 int yCount
= itemCount
/ xCount
;
155 if (itemCount
% xCount
!= 0) {
158 if (yCount
> xCount
) {
162 // Draw the selected items into the grid cells
163 QPixmap
dragPixmap(xCount
* size
+ xCount
- 1, yCount
* size
+ yCount
- 1);
164 dragPixmap
.fill(Qt::transparent
);
166 QPainter
painter(&dragPixmap
);
169 foreach (const QModelIndex
& selectedIndex
, selectedIndexes
) {
170 const QModelIndex index
= proxyModel
->mapToSource(selectedIndex
);
171 const KFileItem item
= dirModel
->itemForIndex(index
);
172 const QPixmap pixmap
= item
.pixmap(size
, size
);
173 painter
.drawPixmap(x
, y
, pixmap
);
176 if (x
>= dragPixmap
.width()) {
180 if (y
>= dragPixmap
.height()) {
188 #include "draganddrophelper.moc"