delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / dolphin / src / pixmapviewer.cpp
blob416e53f1faa9d291ece142bd6cabc011f9a1f4b6
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 ***************************************************************************/
20 #include "pixmapviewer.h"
22 #include <kiconloader.h>
24 #include <QLayout>
25 #include <QPainter>
26 #include <QPixmap>
27 #include <QKeyEvent>
29 PixmapViewer::PixmapViewer(QWidget* parent, Transition transition) :
30 QWidget(parent),
31 m_transition(transition),
32 m_animationStep(0),
33 m_sizeHint()
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()) {
54 return;
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();
63 return;
66 m_oldPixmap = m_pixmap.isNull() ? pixmap : m_pixmap;
67 m_pixmap = pixmap;
68 update();
70 const bool animate = (m_transition != NoTransition) &&
71 (m_pixmap.size() != m_oldPixmap.size());
72 if (animate) {
73 m_animation.start();
77 void PixmapViewer::setSizeHint(const QSize& size)
79 m_sizeHint = size;
80 updateGeometry();
83 QSize PixmapViewer::sizeHint() const
85 return m_sizeHint;
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,
106 scaledHeight,
107 Qt::IgnoreAspectRatio,
108 Qt::FastTransformation);
109 painter.drawPixmap(x, y, scaledPixmap);
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);
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;
122 m_pixmap = pixmap;
123 update();
124 m_animation.start();
125 } else {
126 m_oldPixmap = m_pixmap;
130 #include "pixmapviewer.moc"