]> cloud.milkyroute.net Git - dolphin.git/blob - src/pixmapviewer.cpp
* allow to toggle the content of split views by the context menu
[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 #include <QtGui/QPainter>
24 #include <QtGui/QPixmap>
25 #include <QtGui/QKeyEvent>
26
27 PixmapViewer::PixmapViewer(QWidget* parent, Transition transition) :
28 QWidget(parent),
29 m_transition(transition),
30 m_animationStep(0)
31 {
32 setMinimumWidth(K3Icon::SizeEnormous);
33 setMinimumWidth(K3Icon::SizeEnormous);
34
35 m_animation.setDuration(300);
36 connect(&m_animation, SIGNAL(valueChanged(qreal)), this, SLOT(update()));
37 }
38
39 PixmapViewer::~PixmapViewer()
40 {
41 }
42
43 void PixmapViewer::setPixmap(const QPixmap& pixmap)
44 {
45 if (pixmap.isNull()) {
46 return;
47 }
48
49 m_oldPixmap = m_pixmap.isNull() ? pixmap : m_pixmap;
50 m_pixmap = pixmap;
51 update();
52
53 const bool animate = (m_transition != NoTransition) &&
54 (m_pixmap.size() != m_oldPixmap.size());
55 if (animate) {
56 m_animation.start();
57 }
58 }
59
60 void PixmapViewer::paintEvent(QPaintEvent* event)
61 {
62 QWidget::paintEvent(event);
63
64 QPainter painter(this);
65
66 const float value = m_animation.currentValue();
67
68 const int scaledWidth = static_cast<int>((m_oldPixmap.width() * (1.0 - value)) + (m_pixmap.width() * value));
69 const int scaledHeight = static_cast<int>((m_oldPixmap.height() * (1.0 - value)) + (m_pixmap.height() * value));
70 const int x = (width() - scaledWidth ) / 2;
71 const int y = (height() - scaledHeight) / 2;
72
73 const bool useOldPixmap = (m_transition == SizeTransition) &&
74 (m_oldPixmap.width() > m_pixmap.width());
75 const QPixmap& largePixmap = useOldPixmap ? m_oldPixmap : m_pixmap;
76 const QPixmap scaledPixmap = largePixmap.scaled(scaledWidth,
77 scaledHeight,
78 Qt::IgnoreAspectRatio,
79 Qt::SmoothTransformation);
80 painter.drawPixmap(x, y, scaledPixmap);
81 }
82
83 #include "pixmapviewer.moc"