]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/pixmapviewer.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "pixmapviewer.h"
22 #include <KIconLoader>
27 PixmapViewer::PixmapViewer(QWidget
* parent
, Transition transition
) :
29 m_transition(transition
),
33 setMinimumWidth(KIconLoader::SizeEnormous
);
34 setMinimumHeight(KIconLoader::SizeEnormous
);
36 m_animation
.setDuration(150);
37 m_animation
.setCurveShape(QTimeLine::LinearCurve
);
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
);
45 PixmapViewer::~PixmapViewer()
49 void PixmapViewer::setPixmap(const QPixmap
& pixmap
)
51 if (pixmap
.isNull()) {
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();
64 m_oldPixmap
= m_pixmap
.isNull() ? pixmap
: m_pixmap
;
68 const bool animate
= (m_transition
!= NoTransition
) &&
69 (m_pixmap
.size() != m_oldPixmap
.size());
75 void PixmapViewer::setSizeHint(const QSize
& size
)
81 QSize
PixmapViewer::sizeHint() const
86 void PixmapViewer::paintEvent(QPaintEvent
* event
)
88 QWidget::paintEvent(event
);
90 QPainter
painter(this);
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
));
97 const int x
= (width() - scaledWidth
) / 2;
98 const int y
= (height() - scaledHeight
) / 2;
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
,
106 Qt::IgnoreAspectRatio
,
107 Qt::FastTransformation
);
108 painter
.drawPixmap(x
, y
, scaledPixmap
);
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
);
117 void PixmapViewer::checkPendingPixmaps()
119 if (m_pendingPixmaps
.count() > 0) {
120 QPixmap pixmap
= m_pendingPixmaps
.dequeue();
121 m_oldPixmap
= m_pixmap
.isNull() ? pixmap
: m_pixmap
;
126 m_oldPixmap
= m_pixmap
;