]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/pixmapviewer.cpp
Merge branch 'Applications/14.12' into frameworks
[dolphin.git] / src / panels / information / pixmapviewer.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
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>
23
24 #include <QPainter>
25 #include <QPixmap>
26
27 PixmapViewer::PixmapViewer(QWidget* parent, Transition transition) :
28 QWidget(parent),
29 m_transition(transition),
30 m_animationStep(0),
31 m_sizeHint()
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, &QTimeLine::valueChanged, this, static_cast<void(PixmapViewer::*)()>(&PixmapViewer::update));
41 connect(&m_animation, &QTimeLine::finished, this, &PixmapViewer::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::setSizeHint(const QSize& size)
76 {
77 m_sizeHint = size;
78 updateGeometry();
79 }
80
81 QSize PixmapViewer::sizeHint() const
82 {
83 return m_sizeHint;
84 }
85
86 void PixmapViewer::paintEvent(QPaintEvent* event)
87 {
88 QWidget::paintEvent(event);
89
90 QPainter painter(this);
91
92 if (m_transition != NoTransition) {
93 const float value = m_animation.currentValue();
94 const int scaledWidth = static_cast<int>((m_oldPixmap.width() * (1.0 - value)) + (m_pixmap.width() * value));
95 const int scaledHeight = static_cast<int>((m_oldPixmap.height() * (1.0 - value)) + (m_pixmap.height() * value));
96
97 const int x = (width() - scaledWidth ) / 2;
98 const int y = (height() - scaledHeight) / 2;
99
100 const bool useOldPixmap = (m_transition == SizeTransition) &&
101 (m_oldPixmap.width() > m_pixmap.width());
102 const QPixmap& largePixmap = useOldPixmap ? m_oldPixmap : m_pixmap;
103 if (!largePixmap.isNull()) {
104 const QPixmap scaledPixmap = largePixmap.scaled(scaledWidth,
105 scaledHeight,
106 Qt::IgnoreAspectRatio,
107 Qt::FastTransformation);
108 painter.drawPixmap(x, y, scaledPixmap);
109 }
110 } else {
111 const int x = (width() - m_pixmap.width() ) / 2;
112 const int y = (height() - m_pixmap.height()) / 2;
113 painter.drawPixmap(x, y, m_pixmap);
114 }
115 }
116
117 void PixmapViewer::checkPendingPixmaps()
118 {
119 if (m_pendingPixmaps.count() > 0) {
120 QPixmap pixmap = m_pendingPixmaps.dequeue();
121 m_oldPixmap = m_pixmap.isNull() ? pixmap : m_pixmap;
122 m_pixmap = pixmap;
123 update();
124 m_animation.start();
125 } else {
126 m_oldPixmap = m_pixmap;
127 }
128 }
129