]> cloud.milkyroute.net Git - dolphin.git/blob - src/draganddrophelper.cpp
Unify the search interface for non-indexed and indexed folders
[dolphin.git] / src / 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 #include "dolphiniconsview.h"
23 #include "dolphinviewcontroller.h"
24
25 #include <kdirmodel.h>
26 #include <kfileitem.h>
27 #include <kicon.h>
28 #include <klocale.h>
29 #include <konq_operations.h>
30
31 #include <QAbstractItemView>
32 #include <QAbstractProxyModel>
33 #include <QtDBus>
34 #include <QDrag>
35 #include <QPainter>
36
37 class DragAndDropHelperSingleton
38 {
39 public:
40 DragAndDropHelper instance;
41 };
42 K_GLOBAL_STATIC(DragAndDropHelperSingleton, s_dragAndDropHelper)
43
44 DragAndDropHelper& DragAndDropHelper::instance()
45 {
46 return s_dragAndDropHelper->instance;
47 }
48
49 bool DragAndDropHelper::isMimeDataSupported(const QMimeData* mimeData) const
50 {
51 return mimeData->hasUrls() || mimeData->hasFormat("application/x-kde-dndextract");
52 }
53
54 void DragAndDropHelper::startDrag(QAbstractItemView* itemView,
55 Qt::DropActions supportedActions,
56 DolphinViewController* dolphinViewController)
57 {
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;
61 if (isDragging) {
62 return;
63 }
64 isDragging = true;
65
66 const QModelIndexList indexes = itemView->selectionModel()->selectedIndexes();
67 if (!indexes.isEmpty()) {
68 QMimeData *data = itemView->model()->mimeData(indexes);
69 if (data == 0) {
70 return;
71 }
72
73 if (dolphinViewController != 0) {
74 dolphinViewController->requestToolTipHiding();
75 }
76
77 QDrag* drag = new QDrag(itemView);
78 drag->setPixmap(createDragPixmap(itemView));
79 drag->setMimeData(data);
80
81 m_dragSource = itemView;
82 drag->exec(supportedActions, Qt::IgnoreAction);
83 m_dragSource = 0;
84 }
85 isDragging = false;
86 }
87
88 bool DragAndDropHelper::isDragSource(QAbstractItemView* itemView) const
89 {
90 return (m_dragSource != 0) && (m_dragSource == itemView);
91 }
92
93 void DragAndDropHelper::dropUrls(const KFileItem& destItem,
94 const KUrl& destPath,
95 QDropEvent* event,
96 QWidget* widget)
97 {
98 const bool dropToItem = !destItem.isNull() && (destItem.isDir() || destItem.isDesktopFile());
99 const KUrl destination = dropToItem ? destItem.url() : destPath;
100
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);
108 } else {
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);
117 } else {
118 KonqOperations::doDrop(KFileItem(), destination, event, widget);
119 }
120 }
121 }
122
123 DragAndDropHelper::DragAndDropHelper()
124 : m_dragSource(0)
125 {
126 }
127
128 QPixmap DragAndDropHelper::createDragPixmap(QAbstractItemView* itemView) const
129 {
130 const QModelIndexList selectedIndexes = itemView->selectionModel()->selectedIndexes();
131 Q_ASSERT(!selectedIndexes.isEmpty());
132
133 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(itemView->model());
134 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
135
136 const int itemCount = selectedIndexes.count();
137
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.
140 int xCount = 3;
141 int size = KIconLoader::SizeMedium;
142 if (itemCount > 16) {
143 xCount = 5;
144 size = KIconLoader::SizeSmall;
145 } else if (itemCount > 9) {
146 xCount = 4;
147 size = KIconLoader::SizeSmallMedium;
148 }
149
150 if (itemCount < xCount) {
151 xCount = itemCount;
152 }
153
154 int yCount = itemCount / xCount;
155 if (itemCount % xCount != 0) {
156 ++yCount;
157 }
158 if (yCount > xCount) {
159 yCount = xCount;
160 }
161
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);
165
166 QPainter painter(&dragPixmap);
167 int x = 0;
168 int y = 0;
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);
174
175 x += size + 1;
176 if (x >= dragPixmap.width()) {
177 x = 0;
178 y += size + 1;
179 }
180 if (y >= dragPixmap.height()) {
181 break;
182 }
183 }
184
185 return dragPixmap;
186 }
187
188 #include "draganddrophelper.moc"