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