]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/draganddrophelper.cpp
removed: include of unused (and deprecated) header
[dolphin.git] / src / views / draganddrophelper.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2007 by David Faure <faure@kde.org> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "draganddrophelper.h"
22
23 #include <kdirmodel.h>
24 #include <kfileitem.h>
25 #include <kicon.h>
26 #include <klocale.h>
27 #include <konq_operations.h>
28
29 #include "views/dolphiniconsview.h"
30 #include "views/dolphinviewcontroller.h"
31
32 #include <QAbstractItemView>
33 #include <QAbstractProxyModel>
34 #include <QtDBus>
35 #include <QDrag>
36 #include <QPainter>
37
38 class DragAndDropHelperSingleton
39 {
40 public:
41 DragAndDropHelper instance;
42 };
43 K_GLOBAL_STATIC(DragAndDropHelperSingleton, s_dragAndDropHelper)
44
45 DragAndDropHelper& DragAndDropHelper::instance()
46 {
47 return s_dragAndDropHelper->instance;
48 }
49
50 bool DragAndDropHelper::isMimeDataSupported(const QMimeData* mimeData) const
51 {
52 // We support everything, cf KonqOperations::doDrop which can save any data.
53 //return mimeData->hasUrls() || mimeData->hasFormat("application/x-kde-dndextract");
54 Q_UNUSED(mimeData);
55 return true;
56 }
57
58 void DragAndDropHelper::startDrag(QAbstractItemView* itemView,
59 Qt::DropActions supportedActions,
60 DolphinViewController* dolphinViewController)
61 {
62 // Do not start a new drag until the previous one has been finished.
63 // This is a (possibly temporary) fix for bug #187884.
64 static bool isDragging = false;
65 if (isDragging) {
66 return;
67 }
68 isDragging = true;
69
70 const QModelIndexList indexes = itemView->selectionModel()->selectedIndexes();
71 if (!indexes.isEmpty()) {
72 QMimeData *data = itemView->model()->mimeData(indexes);
73 if (data == 0) {
74 return;
75 }
76
77 if (dolphinViewController != 0) {
78 dolphinViewController->requestToolTipHiding();
79 }
80
81 QDrag* drag = new QDrag(itemView);
82 drag->setPixmap(createDragPixmap(itemView));
83 drag->setMimeData(data);
84
85 m_dragSource = itemView;
86 drag->exec(supportedActions, Qt::IgnoreAction);
87 m_dragSource = 0;
88 }
89 isDragging = false;
90 }
91
92 bool DragAndDropHelper::isDragSource(QAbstractItemView* itemView) const
93 {
94 return (m_dragSource != 0) && (m_dragSource == itemView);
95 }
96
97 void DragAndDropHelper::dropUrls(const KFileItem& destItem,
98 const KUrl& destPath,
99 QDropEvent* event,
100 QWidget* widget)
101 {
102 const bool dropToItem = !destItem.isNull() && (destItem.isDir() || destItem.isDesktopFile());
103 const KUrl destination = dropToItem ? destItem.url() : destPath;
104
105 const QMimeData* mimeData = event->mimeData();
106 if (mimeData->hasFormat("application/x-kde-dndextract")) {
107 QString remoteDBusClient = mimeData->data("application/x-kde-dndextract");
108 QDBusMessage message = QDBusMessage::createMethodCall(remoteDBusClient, "/DndExtract",
109 "org.kde.DndExtract", "extractSelectedFilesTo");
110 message.setArguments(QVariantList() << destination.path());
111 QDBusConnection::sessionBus().call(message);
112 } else {
113 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
114 const int urlsCount = urls.count();
115 if ((urlsCount == 1) && (urls.first() == destination)) {
116 emit errorMessage(i18nc("@info:status", "A folder cannot be dropped into itself"));
117 } else if (dropToItem) {
118 KonqOperations::doDrop(destItem, destination, event, widget);
119 } else {
120 KonqOperations::doDrop(KFileItem(), destination, event, widget);
121 }
122 }
123 }
124
125 DragAndDropHelper::DragAndDropHelper()
126 : m_dragSource(0)
127 {
128 }
129
130 QPixmap DragAndDropHelper::createDragPixmap(QAbstractItemView* itemView) const
131 {
132 const QModelIndexList selectedIndexes = itemView->selectionModel()->selectedIndexes();
133 Q_ASSERT(!selectedIndexes.isEmpty());
134
135 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(itemView->model());
136 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
137
138 const int itemCount = selectedIndexes.count();
139
140 // If more than one item is dragged, align the items inside a
141 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
142 int xCount = 3;
143 int size = KIconLoader::SizeMedium;
144 if (itemCount > 16) {
145 xCount = 5;
146 size = KIconLoader::SizeSmall;
147 } else if (itemCount > 9) {
148 xCount = 4;
149 size = KIconLoader::SizeSmallMedium;
150 }
151
152 if (itemCount < xCount) {
153 xCount = itemCount;
154 }
155
156 int yCount = itemCount / xCount;
157 if (itemCount % xCount != 0) {
158 ++yCount;
159 }
160 if (yCount > xCount) {
161 yCount = xCount;
162 }
163
164 // Draw the selected items into the grid cells
165 QPixmap dragPixmap(xCount * size + xCount - 1, yCount * size + yCount - 1);
166 dragPixmap.fill(Qt::transparent);
167
168 QPainter painter(&dragPixmap);
169 int x = 0;
170 int y = 0;
171 foreach (const QModelIndex& selectedIndex, selectedIndexes) {
172 const QModelIndex index = proxyModel->mapToSource(selectedIndex);
173 const KFileItem item = dirModel->itemForIndex(index);
174 const QPixmap pixmap = item.pixmap(size, size);
175 painter.drawPixmap(x, y, pixmap);
176
177 x += size + 1;
178 if (x >= dragPixmap.width()) {
179 x = 0;
180 y += size + 1;
181 }
182 if (y >= dragPixmap.height()) {
183 break;
184 }
185 }
186
187 return dragPixmap;
188 }
189
190 #include "draganddrophelper.moc"