]> cloud.milkyroute.net Git - dolphin.git/blob - src/draganddrophelper.cpp
Drag & drop fixes for all views: assure that a consistent pixmap for the drag object...
[dolphin.git] / src / draganddrophelper.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "draganddrophelper.h"
21
22 #include <kdirmodel.h>
23 #include <kicon.h>
24
25 #include <QAbstractItemView>
26 #include <QAbstractProxyModel>
27 #include <QBrush>
28 #include <QDrag>
29 #include <QPainter>
30 #include <QRect>
31 #include <QWidget>
32
33 void DragAndDropHelper::startDrag(QAbstractItemView* itemView, Qt::DropActions supportedActions)
34 {
35 QModelIndexList indexes = itemView->selectionModel()->selectedIndexes();
36 if (indexes.count() > 0) {
37 QMimeData *data = itemView->model()->mimeData(indexes);
38 if (data == 0) {
39 return;
40 }
41
42 QDrag* drag = new QDrag(itemView);
43 QPixmap pixmap;
44 if (indexes.count() == 1) {
45 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(itemView->model());
46 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
47 const QModelIndex index = proxyModel->mapToSource(indexes.first());
48
49 const KFileItem item = dirModel->itemForIndex(index);
50 pixmap = item.pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium);
51 } else {
52 pixmap = KIcon("item-drag-multiple").pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium);
53 }
54 drag->setPixmap(pixmap);
55 drag->setMimeData(data);
56 drag->exec(supportedActions);
57 }
58 }
59
60 void DragAndDropHelper::drawHoverIndication(QWidget* widget,
61 const QRect& bounds,
62 const QBrush& brush)
63 {
64 QPainter painter(widget);
65 painter.save();
66 QBrush blendedBrush(brush);
67 QColor color = blendedBrush.color();
68 color.setAlpha(64);
69 blendedBrush.setColor(color);
70
71 const int radius = 10;
72 QPainterPath path(QPointF(bounds.left(), bounds.top() + radius));
73 path.quadTo(bounds.left(), bounds.top(), bounds.left() + radius, bounds.top());
74 path.lineTo(bounds.right() - radius, bounds.top());
75 path.quadTo(bounds.right(), bounds.top(), bounds.right(), bounds.top() + radius);
76 path.lineTo(bounds.right(), bounds.bottom() - radius);
77 path.quadTo(bounds.right(), bounds.bottom(), bounds.right() - radius, bounds.bottom());
78 path.lineTo(bounds.left() + radius, bounds.bottom());
79 path.quadTo(bounds.left(), bounds.bottom(), bounds.left(), bounds.bottom() - radius);
80 path.closeSubpath();
81
82 painter.setRenderHint(QPainter::Antialiasing);
83 painter.fillPath(path, blendedBrush);
84 painter.restore();
85 }