1 | /* |
2 | * Copyright (C) 1997-2002 Richard J. Moore <rich@kde.org> |
3 | * Copyright (C) 2002-2010 Aaron J. Seigo <aseigo@kde.org> |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU Lesser 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 Lesser General Public License |
16 | * along with this program; if not, write to the Free Software |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | * Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "ksnapshotpreview.h" |
22 | |
23 | #include <QPainter> |
24 | #include <QMouseEvent> |
25 | #include <QPixmap> |
26 | |
27 | #include <KDebug> |
28 | #include <KGlobalSettings> |
29 | |
30 | #include "expblur.cpp" |
31 | |
32 | KSnapshotPreview::KSnapshotPreview(QWidget *parent) |
33 | : QLabel(parent) |
34 | { |
35 | setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); |
36 | setCursor(Qt::OpenHandCursor); |
37 | } |
38 | |
39 | KSnapshotPreview::~KSnapshotPreview() |
40 | { |
41 | } |
42 | |
43 | void KSnapshotPreview::setPreview(const QPixmap &pm) |
44 | { |
45 | static const int BLUR_PAD = 6; |
46 | static const int BLUR_RADIUS = 2; |
47 | QPixmap pixmap = pm.scaled(width() - BLUR_PAD, height() - BLUR_PAD, Qt::KeepAspectRatio, Qt::SmoothTransformation ); |
48 | |
49 | QImage blur(pixmap.size() + QSize(BLUR_PAD, BLUR_PAD), QImage::Format_ARGB32); |
50 | QRect blurRect = QRect(QPoint(BLUR_PAD / 2, BLUR_PAD / 2), pixmap.size()); |
51 | blur.fill(Qt::transparent); |
52 | //kDebug() << blur.size() << blurRect << pixmap.size(); |
53 | |
54 | const QColor color = qGray(palette().color(QPalette::Base).rgb()) < 192 ? Qt::white : Qt::black; |
55 | |
56 | { |
57 | QPainter p(&blur); |
58 | p.fillRect(blurRect, color); |
59 | p.end(); |
60 | } |
61 | |
62 | // apply blur for the thumbnail shadow |
63 | expblur<16, 7>(blur, BLUR_RADIUS); |
64 | |
65 | { |
66 | QPainter p(&blur); |
67 | p.setCompositionMode(QPainter::CompositionMode_SourceIn); |
68 | p.fillRect(blur.rect(), color); |
69 | p.fillRect(QRect(blurRect.topLeft(), pixmap.size()), Qt::transparent); |
70 | p.setCompositionMode(QPainter::CompositionMode_SourceOver); |
71 | p.drawPixmap(QRect(blurRect.topLeft(), pixmap.size()), pixmap); |
72 | p.end(); |
73 | } |
74 | |
75 | setPixmap(QPixmap::fromImage(blur)); |
76 | } |
77 | |
78 | void KSnapshotPreview::mousePressEvent(QMouseEvent * e) |
79 | { |
80 | if ( e->button() == Qt::LeftButton ) |
81 | mClickPt = e->pos(); |
82 | } |
83 | |
84 | void KSnapshotPreview::mouseMoveEvent(QMouseEvent * e) |
85 | { |
86 | if (mClickPt != QPoint(0, 0) && |
87 | (e->pos() - mClickPt).manhattanLength() > KGlobalSettings::dndEventDelay()) |
88 | { |
89 | mClickPt = QPoint(0, 0); |
90 | emit startDrag(); |
91 | } |
92 | } |
93 | |
94 | void KSnapshotPreview::mouseReleaseEvent(QMouseEvent * /*e*/) |
95 | { |
96 | mClickPt = QPoint(0, 0); |
97 | } |
98 | |
99 | #include "ksnapshotpreview.moc" |
100 | |
101 | |