File indexing completed on 2024-05-12 16:27:16

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QGraphicsView>
0010 #include <QWidget>
0011 
0012 #include "libruqolawidgets_private_export.h"
0013 
0014 class QLabel;
0015 class QSlider;
0016 class QDoubleSpinBox;
0017 class QMovie;
0018 class ImageGraphicsView;
0019 class RocketChatAccount;
0020 class LIBRUQOLAWIDGETS_TESTS_EXPORT ShowImageWidget : public QWidget
0021 {
0022     Q_OBJECT
0023 public:
0024     struct ImageInfo {
0025         QString bigImagePath;
0026         QString previewImagePath;
0027         QPixmap pixmap;
0028         bool isAnimatedImage = false;
0029         bool needToDownloadBigImage = false;
0030     };
0031 
0032     explicit ShowImageWidget(RocketChatAccount *account, QWidget *parent = nullptr);
0033     ~ShowImageWidget() override;
0034 
0035     void setImageInfo(const ImageInfo &info);
0036     [[nodiscard]] const ShowImageWidget::ImageInfo &imageInfo() const;
0037 
0038     void saveAs();
0039 
0040     void copyImage();
0041     void copyLocation();
0042 
0043 protected:
0044     void keyPressEvent(QKeyEvent *event) override;
0045 
0046 private:
0047     LIBRUQOLAWIDGETS_NO_EXPORT void updateRanges();
0048     LIBRUQOLAWIDGETS_NO_EXPORT void slotFileDownloaded(const QString &filePath, const QUrl &cacheImageUrl);
0049     ImageGraphicsView *const mImageGraphicsView;
0050     QWidget *const mZoomControls;
0051     QDoubleSpinBox *const mZoomSpin;
0052     QSlider *const mSlider;
0053     RocketChatAccount *const mRocketChatAccount;
0054 };
0055 Q_DECLARE_TYPEINFO(ShowImageWidget::ImageInfo, Q_MOVABLE_TYPE);
0056 
0057 class LIBRUQOLAWIDGETS_TESTS_EXPORT ImageGraphicsView : public QGraphicsView
0058 {
0059     Q_OBJECT
0060 public:
0061     explicit ImageGraphicsView(RocketChatAccount *account, QWidget *parent = nullptr);
0062     ~ImageGraphicsView() override;
0063 
0064     void clearContents();
0065 
0066     [[nodiscard]] qreal minimumZoom() const;
0067     [[nodiscard]] qreal maximumZoom() const;
0068 
0069     [[nodiscard]] qreal zoom() const;
0070     void setZoom(qreal zoom, QPointF centerPos = {});
0071 
0072     void setImageInfo(const ShowImageWidget::ImageInfo &info);
0073     [[nodiscard]] const ShowImageWidget::ImageInfo &imageInfo() const;
0074 
0075     void updatePixmap(const QPixmap &pix, const QString &path);
0076     [[nodiscard]] QPixmap pixmap() const;
0077 
0078     void fitToView();
0079 
0080 protected:
0081     void wheelEvent(QWheelEvent *e) override;
0082 
0083 Q_SIGNALS:
0084     void zoomChanged(qreal zoom);
0085     void minimumZoomChanged(qreal zoom);
0086     void maximumZoomChanged(qreal zoom);
0087 
0088 private:
0089     LIBRUQOLAWIDGETS_NO_EXPORT void zoomIn(QPointF centerPos = {});
0090     LIBRUQOLAWIDGETS_NO_EXPORT void zoomOut(QPointF centerPos = {});
0091     LIBRUQOLAWIDGETS_NO_EXPORT void updateRanges();
0092     [[nodiscard]] LIBRUQOLAWIDGETS_NO_EXPORT QSize originalImageSize() const;
0093 
0094     QString mImagePath;
0095 
0096     QSize mOriginalMovieSize;
0097     QScopedPointer<QMovie> mMovie;
0098     ShowImageWidget::ImageInfo mImageInfo;
0099 
0100     QGraphicsProxyWidget *mGraphicsProxyWidget = nullptr;
0101     QGraphicsPixmapItem *mGraphicsPixmapItem = nullptr;
0102     QLabel *const mAnimatedLabel;
0103     RocketChatAccount *const mRocketChatAccount;
0104 
0105     qreal mMinimumZoom;
0106     qreal mMaximumZoom;
0107     bool mIsUpdatingZoom = false;
0108 };
0109 LIBRUQOLAWIDGETS_EXPORT QDebug operator<<(QDebug d, const ShowImageWidget::ImageInfo &t);