]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/draganddrophelper.cpp
Sourcecode hierarchy cleanup: Move further files from src to src/views
[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 return mimeData->hasUrls() || mimeData->hasFormat("application/x-kde-dndextract");
53 }
54
55 void DragAndDropHelper::startDrag(QAbstractItemView* itemView,
56 Qt::DropActions supportedActions,
57 DolphinViewController* dolphinViewController)
58 {
59 // Do not start a new drag until the previous one has been finished.
60 // This is a (possibly temporary) fix for bug #187884.
61 static bool isDragging = false;
62 if (isDragging) {
63 return;
64 }
65 isDragging = true;
66
67 const QModelIndexList indexes = itemView->selectionModel()->selectedIndexes();
68 if (!indexes.isEmpty()) {
69 QMimeData *data = itemView->model()->mimeData(indexes);
70 if (data == 0) {
71 return;
72 }
73
74 if (dolphinViewController != 0) {
75 dolphinViewController->requestToolTipHiding();
76 }
77
78 QDrag* drag = new QDrag(itemView);
79 drag->setPixmap(createDragPixmap(itemView));
80 drag->setMimeData(data);
81
82 m_dragSource = itemView;
83 drag->exec(supportedActions, Qt::IgnoreAction);
84 m_dragSource = 0;
85 }
86 isDragging = false;
87 }
88
89 bool DragAndDropHelper::isDragSource(QAbstractItemView* itemView) const
90 {
91 return (m_dragSource != 0) && (m_dragSource == itemView);
92 }
93
94 void DragAndDropHelper::dropUrls(const KFileItem& destItem,
95 const KUrl& destPath,
96 QDropEvent* event,
97 QWidget* widget)
98 {
99 const bool dropToItem = !destItem.isNull() && (destItem.isDir() || destItem.isDesktopFile());
100 const KUrl destination = dropToItem ? destItem.url() : destPath;
101
102 const QMimeData* mimeData = event->mimeData();
103 if (mimeData->hasFormat("application/x-kde-dndextract")) {
104 QString remoteDBusClient = mimeData->data("application/x-kde-dndextract");
105 QDBusMessage message = QDBusMessage::createMethodCall(remoteDBusClient, "/DndExtract",
106 "org.kde.DndExtract", "extractSelectedFilesTo");
107 message.setArguments(QVariantList() << destination.path());
108 QDBusConnection::sessionBus().call(message);
109 } else {
110 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
111 const int urlsCount = urls.count();
112 if (urlsCount == 0) {
113 // TODO: handle dropping of other data
114 } else if ((urlsCount == 1) && (urls.first() == destination)) {
115 emit errorMessage(i18nc("@info:status", "A folder cannot be dropped into itself"));
116 } else if (dropToItem) {
117 KonqOperations::doDrop(destItem, destination, event, widget);
118 } else {
119 KonqOperations::doDrop(KFileItem(), destination, event, widget);
120 }
121 }
122 }
123
124 DragAndDropHelper::DragAndDropHelper()
125 : m_dragSource(0)
126 {
127 }
128
129 QPixmap DragAndDropHelper::createDragPixmap(QAbstractItemView* itemView) const
130 {
131 const QModelIndexList selectedIndexes = itemView->selectionModel()->selectedIndexes();
132 Q_ASSERT(!selectedIndexes.isEmpty());
133
134 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(itemView->model());
135 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
136
137 const int itemCount = selectedIndexes.count();
138
139 // If more than one item is dragged, align the items inside a
140 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
141 int xCount = 3;
142 int size = KIconLoader::SizeMedium;
143 if (itemCount > 16) {
144 xCount = 5;
145 size = KIconLoader::SizeSmall;
146 } else if (itemCount > 9) {
147 xCount = 4;
148 size = KIconLoader::SizeSmallMedium;
149 }
150
151 if (itemCount < xCount) {
152 xCount = itemCount;
153 }
154
155 int yCount = itemCount / xCount;
156 if (itemCount % xCount != 0) {
157 ++yCount;
158 }
159 if (yCount > xCount) {
160 yCount = xCount;
161 }
162
163 // Draw the selected items into the grid cells
164 QPixmap dragPixmap(xCount * size + xCount - 1, yCount * size + yCount - 1);
165 dragPixmap.fill(Qt::transparent);
166
167 QPainter painter(&dragPixmap);
168 int x = 0;
169 int y = 0;
170 foreach (const QModelIndex& selectedIndex, selectedIndexes) {
171 const QModelIndex index = proxyModel->mapToSource(selectedIndex);
172 const KFileItem item = dirModel->itemForIndex(index);
173 const QPixmap pixmap = item.pixmap(size, size);
174 painter.drawPixmap(x, y, pixmap);
175
176 x += size + 1;
177 if (x >= dragPixmap.width()) {
178 x = 0;
179 y += size + 1;
180 }
181 if (y >= dragPixmap.height()) {
182 break;
183 }
184 }
185
186 return dragPixmap;
187 }
188
189 #include "draganddrophelper.moc"