File indexing completed on 2024-05-19 16:49:21

0001 /*
0002  * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef PIXMAPVIEWER_H
0008 #define PIXMAPVIEWER_H
0009 
0010 #include <QPixmap>
0011 #include <QQueue>
0012 #include <QTimeLine>
0013 #include <QWidget>
0014 
0015 class QPaintEvent;
0016 class QMovie;
0017 
0018 /**
0019  * @brief Widget which shows a pixmap centered inside the boundaries.
0020  *
0021  * When the pixmap is changed, a smooth transition is done from the old pixmap
0022  * to the new pixmap.
0023  */
0024 class PixmapViewer : public QWidget
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     enum Transition {
0030         /** No transition is done when the pixmap is changed. */
0031         NoTransition,
0032 
0033         /**
0034          * The old pixmap is replaced by the new pixmap and the size is
0035          * adjusted smoothly to the size of the new pixmap.
0036          */
0037         DefaultTransition,
0038 
0039         /**
0040          * If the old pixmap and the new pixmap have the same content, but
0041          * a different size it is recommended to use Transition::SizeTransition
0042          * instead of Transition::DefaultTransition. In this case it is assured
0043          * that the larger pixmap is used for downscaling, which leads
0044          * to an improved scaling output.
0045          */
0046         SizeTransition
0047     };
0048 
0049     explicit PixmapViewer(QWidget *parent, Transition transition = DefaultTransition);
0050 
0051     ~PixmapViewer() override;
0052     void setPixmap(const QPixmap &pixmap);
0053     QPixmap pixmap() const;
0054 
0055     /**
0056      * Sets the size hint to \a size and triggers a relayout
0057      * of the parent widget. Per default no size hint is given.
0058      */
0059     void setSizeHint(const QSize &size);
0060     QSize sizeHint() const override;
0061 
0062     void setAnimatedImageFileName(const QString &fileName);
0063     QString animatedImageFileName() const;
0064 
0065     void stopAnimatedImage();
0066 
0067     /**
0068      * Checks if \a mimeType has a format supported by QMovie.
0069      */
0070     static bool isAnimatedMimeType(const QString &mimeType);
0071 
0072 protected:
0073     void paintEvent(QPaintEvent *event) override;
0074 
0075 private Q_SLOTS:
0076     void checkPendingPixmaps();
0077     void updateAnimatedImageFrame();
0078 
0079 private:
0080     QPixmap m_pixmap;
0081     QPixmap m_oldPixmap;
0082     QMovie *m_animatedImage;
0083     QQueue<QPixmap> m_pendingPixmaps;
0084     QTimeLine m_animation;
0085     Transition m_transition;
0086     int m_animationStep;
0087     QSize m_sizeHint;
0088     bool m_hasAnimatedImage;
0089 };
0090 
0091 inline QPixmap PixmapViewer::pixmap() const
0092 {
0093     return m_pixmap;
0094 }
0095 
0096 #endif