]>
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>
24 #include <QImageReader>
29 PixmapViewer::PixmapViewer(QWidget
* parent
, Transition transition
) :
31 m_animatedImage(nullptr),
32 m_transition(transition
),
35 m_hasAnimatedImage(false)
37 setMinimumWidth(KIconLoader::SizeEnormous
);
38 setMinimumHeight(KIconLoader::SizeEnormous
);
40 m_animation
.setDuration(150);
41 m_animation
.setCurveShape(QTimeLine::LinearCurve
);
43 if (m_transition
!= NoTransition
) {
44 connect(&m_animation
, &QTimeLine::valueChanged
, this, QOverload
<>::of(&PixmapViewer::update
));
45 connect(&m_animation
, &QTimeLine::finished
, this, &PixmapViewer::checkPendingPixmaps
);
49 PixmapViewer::~PixmapViewer()
53 void PixmapViewer::setPixmap(const QPixmap
& pixmap
)
55 if (pixmap
.isNull()) {
59 // Avoid flicker with static pixmap if an animated image is running
60 if (m_animatedImage
&& m_animatedImage
->state() == QMovie::Running
) {
64 if ((m_transition
!= NoTransition
) && (m_animation
.state() == QTimeLine::Running
)) {
65 m_pendingPixmaps
.enqueue(pixmap
);
66 if (m_pendingPixmaps
.count() > 5) {
67 // don't queue more than 5 pixmaps
68 m_pendingPixmaps
.takeFirst();
73 m_oldPixmap
= m_pixmap
.isNull() ? pixmap
: m_pixmap
;
77 const bool animateTransition
= (m_transition
!= NoTransition
) &&
78 (m_pixmap
.size() != m_oldPixmap
.size());
79 if (animateTransition
) {
81 } else if (m_hasAnimatedImage
) {
82 // If there is no transition animation but an animatedImage
83 // and it is not already running, start animating now
84 if (m_animatedImage
->state() != QMovie::Running
) {
85 m_animatedImage
->setScaledSize(m_pixmap
.size());
86 m_animatedImage
->start();
91 void PixmapViewer::setSizeHint(const QSize
& size
)
93 if (m_animatedImage
&& size
!= m_sizeHint
) {
94 m_animatedImage
->stop();
101 QSize
PixmapViewer::sizeHint() const
106 void PixmapViewer::setAnimatedImageFileName(const QString
&fileName
)
108 if (!m_animatedImage
) {
109 m_animatedImage
= new QMovie(this);
110 connect(m_animatedImage
, &QMovie::frameChanged
, this, &PixmapViewer::updateAnimatedImageFrame
);
113 if (m_animatedImage
->fileName() != fileName
) {
114 m_animatedImage
->stop();
115 m_animatedImage
->setFileName(fileName
);
118 m_hasAnimatedImage
= m_animatedImage
->isValid() && (m_animatedImage
->frameCount() > 1);
122 QString
PixmapViewer::animatedImageFileName() const
124 if (!m_hasAnimatedImage
) {
127 return m_animatedImage
->fileName();
130 void PixmapViewer::paintEvent(QPaintEvent
* event
)
132 QWidget::paintEvent(event
);
134 QPainter
painter(this);
136 if (m_transition
!= NoTransition
|| (m_hasAnimatedImage
&& m_animatedImage
->state() != QMovie::Running
)) {
137 const float value
= m_animation
.currentValue();
138 const int scaledWidth
= static_cast<int>((m_oldPixmap
.width() * (1.0 - value
)) + (m_pixmap
.width() * value
));
139 const int scaledHeight
= static_cast<int>((m_oldPixmap
.height() * (1.0 - value
)) + (m_pixmap
.height() * value
));
141 const bool useOldPixmap
= (m_transition
== SizeTransition
) &&
142 (m_oldPixmap
.width() > m_pixmap
.width());
143 const QPixmap
& largePixmap
= useOldPixmap
? m_oldPixmap
: m_pixmap
;
144 if (!largePixmap
.isNull()) {
145 const QPixmap scaledPixmap
= largePixmap
.scaled(scaledWidth
,
147 Qt::IgnoreAspectRatio
,
148 Qt::FastTransformation
);
150 style()->drawItemPixmap(&painter
, rect(), Qt::AlignCenter
, scaledPixmap
);
153 style()->drawItemPixmap(&painter
, rect(), Qt::AlignCenter
, m_pixmap
);
157 void PixmapViewer::checkPendingPixmaps()
159 if (!m_pendingPixmaps
.isEmpty()) {
160 QPixmap pixmap
= m_pendingPixmaps
.dequeue();
161 m_oldPixmap
= m_pixmap
.isNull() ? pixmap
: m_pixmap
;
165 } else if (m_hasAnimatedImage
) {
166 m_animatedImage
->setScaledSize(m_pixmap
.size());
167 m_animatedImage
->start();
169 m_oldPixmap
= m_pixmap
;
173 void PixmapViewer::updateAnimatedImageFrame()
175 Q_ASSERT (m_animatedImage
);
177 m_pixmap
= m_animatedImage
->currentPixmap();
181 void PixmapViewer::stopAnimatedImage()
183 if (m_hasAnimatedImage
) {
184 m_animatedImage
->stop();
185 m_hasAnimatedImage
= false;
189 bool PixmapViewer::isAnimatedMimeType(const QString
&mimeType
)
191 const QList
<QByteArray
> imageFormats
= QImageReader::imageFormatsForMimeType(mimeType
.toUtf8());
192 return std::any_of(imageFormats
.begin(), imageFormats
.end(),
193 [](const QByteArray
&format
){ return QMovie::supportedFormats().contains(format
); });