]> cloud.milkyroute.net Git - dolphin.git/blob - src/pixmapviewer.cpp
do a case insensitive filtering of filenames
[dolphin.git] / src / pixmapviewer.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 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 "pixmapviewer.h"
21
22 #include <kiconloader.h>
23
24 #include <QPainter>
25 #include <QPixmap>
26 #include <QKeyEvent>
27
28 PixmapViewer::PixmapViewer(QWidget* parent, Transition transition) :
29 QWidget(parent),
30 m_transition(transition),
31 m_animationStep(0)
32 {
33 setMinimumWidth(KIconLoader::SizeEnormous);
34 setMinimumHeight(KIconLoader::SizeEnormous);
35
36 m_animation.setDuration(150);
37 m_animation.setCurveShape(QTimeLine::LinearCurve);
38
39 if (m_transition != NoTransition) {
40 connect(&m_animation, SIGNAL(valueChanged(qreal)), this, SLOT(update()));
41 connect(&m_animation, SIGNAL(finished()), this, SLOT(checkPendingPixmaps()));
42 }
43 }
44
45 PixmapViewer::~PixmapViewer()
46 {
47 }
48
49 void PixmapViewer::setPixmap(const QPixmap& pixmap)
50 {
51 if (pixmap.isNull()) {
52 return;
53 }
54
55 if ((m_transition != NoTransition) && (m_animation.state() == QTimeLine::Running)) {
56 m_pendingPixmaps.enqueue(pixmap);
57 if (m_pendingPixmaps.count() > 5) {
58 // don't queue more than 5 pixmaps
59 m_pendingPixmaps.takeFirst();
60 }
61 return;
62 }
63
64 m_oldPixmap = m_pixmap.isNull() ? pixmap : m_pixmap;
65 m_pixmap = pixmap;
66 update();
67
68 const bool animate = (m_transition != NoTransition) &&
69 (m_pixmap.size() != m_oldPixmap.size());
70 if (animate) {
71 m_animation.start();
72 }
73 }
74
75 void PixmapViewer::paintEvent(QPaintEvent* event)
76 {
77 QWidget::paintEvent(event);
78
79 QPainter painter(this);
80
81 if (m_transition != NoTransition) {
82 const float value = m_animation.currentValue();
83 const int scaledWidth = static_cast<int>((m_oldPixmap.width() * (1.0 - value)) + (m_pixmap.width() * value));
84 const int scaledHeight = static_cast<int>((m_oldPixmap.height() * (1.0 - value)) + (m_pixmap.height() * value));
85
86 const int x = (width() - scaledWidth ) / 2;
87 const int y = (height() - scaledHeight) / 2;
88
89 const bool useOldPixmap = (m_transition == SizeTransition) &&
90 (m_oldPixmap.width() > m_pixmap.width());
91 const QPixmap& largePixmap = useOldPixmap ? m_oldPixmap : m_pixmap;
92 const QPixmap scaledPixmap = largePixmap.scaled(scaledWidth,
93 scaledHeight,
94 Qt::IgnoreAspectRatio,
95 Qt::FastTransformation);
96 painter.drawPixmap(x, y, scaledPixmap);
97 } else {
98 const int x = (width() - m_pixmap.width() ) / 2;
99 const int y = (height() - m_pixmap.height()) / 2;
100 painter.drawPixmap(x, y, m_pixmap);
101 }
102 }
103
104 void PixmapViewer::checkPendingPixmaps()
105 {
106 if (m_pendingPixmaps.count() > 0) {
107 QPixmap pixmap = m_pendingPixmaps.dequeue();
108 m_oldPixmap = m_pixmap.isNull() ? pixmap : m_pixmap;
109 m_pixmap = pixmap;
110 update();
111 m_animation.start();
112 } else {
113 m_oldPixmap = m_pixmap;
114 }
115 }
116
117 #include "pixmapviewer.moc"