]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/pixmapviewer.cpp
2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "pixmapviewer.h"
11 #include <QImageReader>
16 PixmapViewer::PixmapViewer(QWidget
*parent
, Transition transition
)
18 , m_animatedImage(nullptr)
19 , m_transition(transition
)
22 , m_hasAnimatedImage(false)
24 setMinimumWidth(KIconLoader::SizeEnormous
);
25 setMinimumHeight(KIconLoader::SizeEnormous
);
27 m_animation
.setDuration(150);
28 m_animation
.setEasingCurve(QEasingCurve::Linear
);
30 if (m_transition
!= NoTransition
) {
31 connect(&m_animation
, &QTimeLine::valueChanged
, this, QOverload
<>::of(&PixmapViewer::update
));
32 connect(&m_animation
, &QTimeLine::finished
, this, &PixmapViewer::checkPendingPixmaps
);
36 PixmapViewer::~PixmapViewer()
40 void PixmapViewer::setPixmap(const QPixmap
&pixmap
)
42 if (pixmap
.isNull()) {
46 // Avoid flicker with static pixmap if an animated image is running
47 if (m_animatedImage
&& m_animatedImage
->state() == QMovie::Running
) {
51 if ((m_transition
!= NoTransition
) && (m_animation
.state() == QTimeLine::Running
)) {
52 m_pendingPixmaps
.enqueue(pixmap
);
53 if (m_pendingPixmaps
.count() > 5) {
54 // don't queue more than 5 pixmaps
55 m_pendingPixmaps
.takeFirst();
60 m_oldPixmap
= m_pixmap
.isNull() ? pixmap
: m_pixmap
;
64 const bool animateTransition
= (m_transition
!= NoTransition
) && (m_pixmap
.size() != m_oldPixmap
.size());
65 if (animateTransition
) {
67 } else if (m_hasAnimatedImage
) {
68 // If there is no transition animation but an animatedImage
69 // and it is not already running, start animating now
70 if (m_animatedImage
->state() != QMovie::Running
) {
71 m_animatedImage
->setScaledSize(m_pixmap
.size());
72 m_animatedImage
->start();
77 void PixmapViewer::setSizeHint(const QSize
&size
)
79 if (m_animatedImage
&& size
!= m_sizeHint
) {
80 m_animatedImage
->stop();
87 QSize
PixmapViewer::sizeHint() const
92 void PixmapViewer::setAnimatedImageFileName(const QString
&fileName
)
94 if (!m_animatedImage
) {
95 m_animatedImage
= new QMovie(this);
96 connect(m_animatedImage
, &QMovie::frameChanged
, this, &PixmapViewer::updateAnimatedImageFrame
);
99 if (m_animatedImage
->fileName() != fileName
) {
100 m_animatedImage
->stop();
101 m_animatedImage
->setFileName(fileName
);
104 m_hasAnimatedImage
= m_animatedImage
->isValid() && (m_animatedImage
->frameCount() > 1);
107 QString
PixmapViewer::animatedImageFileName() const
109 if (!m_hasAnimatedImage
) {
112 return m_animatedImage
->fileName();
115 void PixmapViewer::paintEvent(QPaintEvent
*event
)
117 QWidget::paintEvent(event
);
119 QPainter
painter(this);
121 if (m_transition
!= NoTransition
|| (m_hasAnimatedImage
&& m_animatedImage
->state() != QMovie::Running
)) {
122 const float value
= m_animation
.currentValue();
123 const int scaledWidth
= static_cast<int>((m_oldPixmap
.width() * (1.0 - value
)) + (m_pixmap
.width() * value
));
124 const int scaledHeight
= static_cast<int>((m_oldPixmap
.height() * (1.0 - value
)) + (m_pixmap
.height() * value
));
126 const bool useOldPixmap
= (m_transition
== SizeTransition
) && (m_oldPixmap
.width() > m_pixmap
.width());
127 const QPixmap
&largePixmap
= useOldPixmap
? m_oldPixmap
: m_pixmap
;
128 if (!largePixmap
.isNull()) {
129 const QPixmap scaledPixmap
= largePixmap
.scaled(scaledWidth
, scaledHeight
, Qt::IgnoreAspectRatio
, Qt::FastTransformation
);
131 style()->drawItemPixmap(&painter
, rect(), Qt::AlignCenter
, scaledPixmap
);
133 } else if (!m_pixmap
.isNull()) {
134 style()->drawItemPixmap(&painter
, rect(), Qt::AlignCenter
, m_pixmap
);
138 void PixmapViewer::checkPendingPixmaps()
140 if (!m_pendingPixmaps
.isEmpty()) {
141 QPixmap pixmap
= m_pendingPixmaps
.dequeue();
142 m_oldPixmap
= m_pixmap
.isNull() ? pixmap
: m_pixmap
;
146 } else if (m_hasAnimatedImage
) {
147 m_animatedImage
->setScaledSize(m_pixmap
.size());
148 m_animatedImage
->start();
150 m_oldPixmap
= m_pixmap
;
154 void PixmapViewer::updateAnimatedImageFrame()
156 Q_ASSERT(m_animatedImage
);
158 m_pixmap
= m_animatedImage
->currentPixmap();
162 void PixmapViewer::stopAnimatedImage()
164 if (m_hasAnimatedImage
) {
165 m_animatedImage
->stop();
166 m_hasAnimatedImage
= false;
170 bool PixmapViewer::isAnimatedMimeType(const QString
&mimeType
)
172 const QList
<QByteArray
> imageFormats
= QImageReader::imageFormatsForMimeType(mimeType
.toUtf8());
173 return std::any_of(imageFormats
.begin(), imageFormats
.end(), [](const QByteArray
&format
) {
174 return QMovie::supportedFormats().contains(format
);
178 #include "moc_pixmapviewer.cpp"