1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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.h>
29 PixmapViewer::PixmapViewer(QWidget
* parent
, Transition transition
) :
31 m_transition(transition
),
35 setMinimumWidth(KIconLoader::SizeEnormous
);
36 setMinimumHeight(KIconLoader::SizeEnormous
);
38 m_animation
.setDuration(150);
39 m_animation
.setCurveShape(QTimeLine::LinearCurve
);
41 if (m_transition
!= NoTransition
) {
42 connect(&m_animation
, SIGNAL(valueChanged(qreal
)), this, SLOT(update()));
43 connect(&m_animation
, SIGNAL(finished()), this, SLOT(checkPendingPixmaps()));
47 PixmapViewer::~PixmapViewer()
51 void PixmapViewer::setPixmap(const QPixmap
& pixmap
)
53 if (pixmap
.isNull()) {
57 if ((m_transition
!= NoTransition
) && (m_animation
.state() == QTimeLine::Running
)) {
58 m_pendingPixmaps
.enqueue(pixmap
);
59 if (m_pendingPixmaps
.count() > 5) {
60 // don't queue more than 5 pixmaps
61 m_pendingPixmaps
.takeFirst();
66 m_oldPixmap
= m_pixmap
.isNull() ? pixmap
: m_pixmap
;
70 const bool animate
= (m_transition
!= NoTransition
) &&
71 (m_pixmap
.size() != m_oldPixmap
.size());
77 void PixmapViewer::setSizeHint(const QSize
& size
)
83 QSize
PixmapViewer::sizeHint() const
88 void PixmapViewer::paintEvent(QPaintEvent
* event
)
90 QWidget::paintEvent(event
);
92 QPainter
painter(this);
94 if (m_transition
!= NoTransition
) {
95 const float value
= m_animation
.currentValue();
96 const int scaledWidth
= static_cast<int>((m_oldPixmap
.width() * (1.0 - value
)) + (m_pixmap
.width() * value
));
97 const int scaledHeight
= static_cast<int>((m_oldPixmap
.height() * (1.0 - value
)) + (m_pixmap
.height() * value
));
99 const int x
= (width() - scaledWidth
) / 2;
100 const int y
= (height() - scaledHeight
) / 2;
102 const bool useOldPixmap
= (m_transition
== SizeTransition
) &&
103 (m_oldPixmap
.width() > m_pixmap
.width());
104 const QPixmap
& largePixmap
= useOldPixmap
? m_oldPixmap
: m_pixmap
;
105 const QPixmap scaledPixmap
= largePixmap
.scaled(scaledWidth
,
107 Qt::IgnoreAspectRatio
,
108 Qt::FastTransformation
);
109 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
;
130 #include "pixmapviewer.moc"