]>
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 if ((m_transition
!= NoTransition
) && (m_animation
.state() == QTimeLine::Running
)) {
47 m_pendingPixmaps
.enqueue(pixmap
);
48 if (m_pendingPixmaps
.count() > 5) {
49 // don't queue more than 5 pixmaps
50 m_pendingPixmaps
.takeFirst();
55 m_oldPixmap
= m_pixmap
.isNull() ? pixmap
: m_pixmap
;
58 // Avoid flicker with static pixmap if an animated image is running
59 if (m_animatedImage
&& m_animatedImage
->state() == QMovie::Running
) {
65 const bool animateTransition
= (m_transition
!= NoTransition
) && (m_pixmap
.size() != m_oldPixmap
.size());
66 if (animateTransition
) {
68 } else if (m_hasAnimatedImage
) {
69 // If there is no transition animation but an animatedImage
70 // and it is not already running, start animating now
71 if (m_animatedImage
->state() != QMovie::Running
) {
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
->setFileName(fileName
);
103 m_hasAnimatedImage
= m_animatedImage
->isValid() && (m_animatedImage
->frameCount() > 1);
106 QString
PixmapViewer::animatedImageFileName() const
108 if (!m_hasAnimatedImage
) {
111 return m_animatedImage
->fileName();
114 void PixmapViewer::paintEvent(QPaintEvent
*event
)
116 QWidget::paintEvent(event
);
118 QPainter
painter(this);
120 if (m_transition
!= NoTransition
|| (m_hasAnimatedImage
&& m_animatedImage
->state() != QMovie::Running
)) {
121 const float value
= m_animation
.currentValue();
122 const int scaledWidth
= static_cast<int>((m_oldPixmap
.width() * (1.0 - value
)) + (m_pixmap
.width() * value
));
123 const int scaledHeight
= static_cast<int>((m_oldPixmap
.height() * (1.0 - value
)) + (m_pixmap
.height() * value
));
125 const bool useOldPixmap
= (m_transition
== SizeTransition
) && (m_oldPixmap
.width() > m_pixmap
.width());
126 const QPixmap
&largePixmap
= useOldPixmap
? m_oldPixmap
: m_pixmap
;
127 if (!largePixmap
.isNull()) {
128 QPixmap scaledPixmap
= largePixmap
.scaled(scaledWidth
, scaledHeight
, Qt::IgnoreAspectRatio
, Qt::FastTransformation
);
129 scaledPixmap
.setDevicePixelRatio(devicePixelRatioF());
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
->start();
149 m_oldPixmap
= m_pixmap
;
153 void PixmapViewer::updateAnimatedImageFrame()
155 Q_ASSERT(m_animatedImage
);
157 m_pixmap
= m_animatedImage
->currentPixmap();
161 void PixmapViewer::stopAnimatedImage()
163 if (m_hasAnimatedImage
) {
164 m_animatedImage
->stop();
165 m_hasAnimatedImage
= false;
166 delete m_animatedImage
;
167 m_animatedImage
= nullptr;
171 bool PixmapViewer::isAnimatedMimeType(const QString
&mimeType
)
173 const QList
<QByteArray
> imageFormats
= QImageReader::imageFormatsForMimeType(mimeType
.toUtf8());
174 return std::any_of(imageFormats
.begin(), imageFormats
.end(), [](const QByteArray
&format
) {
175 return QMovie::supportedFormats().contains(format
);
179 #include "moc_pixmapviewer.cpp"