]> cloud.milkyroute.net Git - dolphin.git/blob - src/draganddrophelper.cpp
don't draw anything if the bounding rectangle is empty
[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 if (bounds.isEmpty()) {
65 return;
66 }
67
68 QPainter painter(widget);
69 painter.save();
70 QBrush blendedBrush(brush);
71 QColor color = blendedBrush.color();
72 color.setAlpha(64);
73 blendedBrush.setColor(color);
74
75 const int radius = 10;
76 QPainterPath path(QPointF(bounds.left(), bounds.top() + radius));
77 path.quadTo(bounds.left(), bounds.top(), bounds.left() + radius, bounds.top());
78 path.lineTo(bounds.right() - radius, bounds.top());
79 path.quadTo(bounds.right(), bounds.top(), bounds.right(), bounds.top() + radius);
80 path.lineTo(bounds.right(), bounds.bottom() - radius);
81 path.quadTo(bounds.right(), bounds.bottom(), bounds.right() - radius, bounds.bottom());
82 path.lineTo(bounds.left() + radius, bounds.bottom());
83 path.quadTo(bounds.left(), bounds.bottom(), bounds.left(), bounds.bottom() - radius);
84 path.closeSubpath();
85
86 painter.setRenderHint(QPainter::Antialiasing);
87 painter.fillPath(path, blendedBrush);
88 painter.restore();
89 }