]>
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
) {
48 m_animatedImage
->setScaledSize(pixmap
.size());
49 if (m_animatedImage
->state() == QMovie::Running
) {
54 if ((m_transition
!= NoTransition
) && (m_animation
.state() == QTimeLine::Running
)) {
55 m_pendingPixmaps
.enqueue(pixmap
);
56 if (m_pendingPixmaps
.count() > 5) {
57 // don't queue more than 5 pixmaps
58 m_pendingPixmaps
.takeFirst();
63 m_oldPixmap
= m_pixmap
.isNull() ? pixmap
: m_pixmap
;
67 const bool animateTransition
= (m_transition
!= NoTransition
) && (m_pixmap
.size() != m_oldPixmap
.size());
68 if (animateTransition
) {
70 } else if (m_hasAnimatedImage
) {
71 // If there is no transition animation but an animatedImage
72 // and it is not already running, start animating now
73 if (m_animatedImage
->state() != QMovie::Running
) {
74 m_animatedImage
->start();
79 void PixmapViewer::setSizeHint(const QSize
&size
)
81 if (m_animatedImage
&& size
!= m_sizeHint
) {
82 m_animatedImage
->stop();
89 QSize
PixmapViewer::sizeHint() const
94 void PixmapViewer::setAnimatedImageFileName(const QString
&fileName
)
96 if (!m_animatedImage
) {
97 m_animatedImage
= new QMovie(this);
98 connect(m_animatedImage
, &QMovie::frameChanged
, this, &PixmapViewer::updateAnimatedImageFrame
);
101 if (m_animatedImage
->fileName() != fileName
) {
102 m_animatedImage
->stop();
103 m_animatedImage
->setFileName(fileName
);
106 m_hasAnimatedImage
= m_animatedImage
->isValid() && (m_animatedImage
->frameCount() > 1);
109 QString
PixmapViewer::animatedImageFileName() const
111 if (!m_hasAnimatedImage
) {
114 return m_animatedImage
->fileName();
117 void PixmapViewer::paintEvent(QPaintEvent
*event
)
119 QWidget::paintEvent(event
);
121 QPainter
painter(this);
123 if (m_transition
!= NoTransition
|| (m_hasAnimatedImage
&& m_animatedImage
->state() != QMovie::Running
)) {
124 const float value
= m_animation
.currentValue();
125 const int scaledWidth
= static_cast<int>((m_oldPixmap
.width() * (1.0 - value
)) + (m_pixmap
.width() * value
));
126 const int scaledHeight
= static_cast<int>((m_oldPixmap
.height() * (1.0 - value
)) + (m_pixmap
.height() * value
));
128 const bool useOldPixmap
= (m_transition
== SizeTransition
) && (m_oldPixmap
.width() > m_pixmap
.width());
129 const QPixmap
&largePixmap
= useOldPixmap
? m_oldPixmap
: m_pixmap
;
130 if (!largePixmap
.isNull()) {
131 QPixmap scaledPixmap
= largePixmap
.scaled(scaledWidth
, scaledHeight
, Qt::IgnoreAspectRatio
, Qt::FastTransformation
);
132 scaledPixmap
.setDevicePixelRatio(devicePixelRatioF());
134 style()->drawItemPixmap(&painter
, rect(), Qt::AlignCenter
, scaledPixmap
);
136 } else if (!m_pixmap
.isNull()) {
137 style()->drawItemPixmap(&painter
, rect(), Qt::AlignCenter
, m_pixmap
);
141 void PixmapViewer::checkPendingPixmaps()
143 if (!m_pendingPixmaps
.isEmpty()) {
144 QPixmap pixmap
= m_pendingPixmaps
.dequeue();
145 m_oldPixmap
= m_pixmap
.isNull() ? pixmap
: m_pixmap
;
149 } else if (m_hasAnimatedImage
) {
150 m_animatedImage
->start();
152 m_oldPixmap
= m_pixmap
;
156 void PixmapViewer::updateAnimatedImageFrame()
158 Q_ASSERT(m_animatedImage
);
160 m_pixmap
= m_animatedImage
->currentPixmap();
164 void PixmapViewer::stopAnimatedImage()
166 if (m_hasAnimatedImage
) {
167 m_animatedImage
->stop();
168 m_hasAnimatedImage
= false;
172 bool PixmapViewer::isAnimatedMimeType(const QString
&mimeType
)
174 const QList
<QByteArray
> imageFormats
= QImageReader::imageFormatsForMimeType(mimeType
.toUtf8());
175 return std::any_of(imageFormats
.begin(), imageFormats
.end(), [](const QByteArray
&format
) {
176 return QMovie::supportedFormats().contains(format
);
180 #include "moc_pixmapviewer.cpp"