File indexing completed on 2024-04-21 03:42:16

0001 /*
0002     SPDX-FileCopyrightText: 2001 Thomas Kabelmann <tk78@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "auxiliary/filedownloader.h"
0010 
0011 #include <QDialog>
0012 #include <QFile>
0013 #include <QFrame>
0014 #include <QImage>
0015 #include <QPixmap>
0016 #include <QUrl>
0017 
0018 class QLabel;
0019 
0020 class ImageLabel : public QFrame
0021 {
0022         Q_OBJECT
0023     public:
0024         explicit ImageLabel(QWidget *parent);
0025         ~ImageLabel() override = default;
0026         void setImage(const QImage &img);
0027         void invertPixels();
0028 
0029         // ImageViewer needs access to the image in order to modify it
0030         QImage m_Image;
0031     protected:
0032         void paintEvent(QPaintEvent *e) override;
0033         void resizeEvent(QResizeEvent *) override;
0034 
0035     private:
0036         QPixmap pix;
0037 };
0038 
0039 /**
0040  * @class ImageViewer
0041  * @short Image viewer window for KStars
0042  * @author Thomas Kabelmann
0043  * @author Jasem Mutlaq
0044  * @version 1.1
0045  *
0046  * This image-viewer automatically resizes the picture. The output
0047  * works with kio-slaves and not directly with the QImage save-routines
0048  * because normally the image-files are in GIF-format and QT does not
0049  * save these files. For other formats, like PNG, this is not so important
0050  * because they can directly saved by QImage.
0051  *
0052  * The download-slave works asynchron so the parent-widget can be used at
0053  * this time. The save-slave works synchronously, but this is not important
0054  * because the files are at this time local saved and this works not so long.
0055  */
0056 class ImageViewer : public QDialog
0057 {
0058         Q_OBJECT
0059 
0060     public:
0061         /** Creates empty viewer. */
0062         explicit ImageViewer(const QString &caption, QWidget *parent = nullptr);
0063 
0064         /** Create image viewer from URL with caption */
0065         explicit ImageViewer(const QUrl &imageURL, const QString &capText = QString(), QWidget *parent = nullptr);
0066 
0067         /** Destructor. If there is a partially downloaded image file, delete it.*/
0068         ~ImageViewer() override;
0069 
0070         /**
0071          * @brief loadImage Load image from local file and display it
0072          * @param filename path to local image
0073          * @return True if opened and displayed, false otherwise
0074          */
0075         bool loadImage(const QString &filename);
0076 
0077         QJsonObject metadata();
0078 
0079     private:
0080         /**
0081          * Display the downloaded image.  Resize the window to fit the image,  If the image is
0082          * larger than the screen, make the image as large as possible while preserving the
0083          * original aspect ratio
0084          */
0085         bool showImage();
0086 
0087         /** Download the image file pointed to by the URL string. */
0088         void loadImageFromURL();
0089 
0090         /** Save the downloaded image to a local file. */
0091         void saveFile(QUrl &url);
0092 
0093         QFile file;
0094 
0095         const QUrl m_ImageUrl;
0096         bool fileIsImage { false };
0097         QString filename;
0098 
0099         FileDownloader downloadJob;
0100 
0101         ImageLabel *m_View { nullptr };
0102         QLabel *m_Caption { nullptr };
0103 
0104         // Share among image viewers
0105         static QUrl lastURL;
0106 
0107     private slots:
0108         /** Initialize (common part of onstructors) */
0109         void init(QString caption, QString capText);
0110         /** Make sure download has finished, then make sure file exists, then display the image */
0111         //void downloadReady (KJob *);
0112 
0113         void downloadReady();
0114         void downloadError(const QString &errorString);
0115 
0116         /** Saves file to disc. */
0117         void saveFileToDisc();
0118 
0119         /** Inverts colors **/
0120         void invertColors();
0121 };