]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/pixmapviewer.cpp
Merge branch 'Applications/18.12'
[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 <QStyle>
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 bool useOldPixmap = (m_transition == SizeTransition) &&
98 (m_oldPixmap.width() > m_pixmap.width());
99 const QPixmap& largePixmap = useOldPixmap ? m_oldPixmap : m_pixmap;
100 if (!largePixmap.isNull()) {
101 const QPixmap scaledPixmap = largePixmap.scaled(scaledWidth,
102 scaledHeight,
103 Qt::IgnoreAspectRatio,
104 Qt::FastTransformation);
105
106 style()->drawItemPixmap(&painter, rect(), Qt::AlignCenter, scaledPixmap);
107 }
108 } else {
109 style()->drawItemPixmap(&painter, rect(), Qt::AlignCenter, m_pixmap);
110 }
111 }
112
113 void PixmapViewer::checkPendingPixmaps()
114 {
115 if (!m_pendingPixmaps.isEmpty()) {
116 QPixmap pixmap = m_pendingPixmaps.dequeue();
117 m_oldPixmap = m_pixmap.isNull() ? pixmap : m_pixmap;
118 m_pixmap = pixmap;
119 update();
120 m_animation.start();
121 } else {
122 m_oldPixmap = m_pixmap;
123 }
124 }
125