1 /*
2     SPDX-FileCopyrightText: 2001 Thomas Kabelmann <tk78@gmx.de>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include "auxiliary/filedownloader.h"
10 
11 #include <QDialog>
12 #include <QFile>
13 #include <QFrame>
14 #include <QImage>
15 #include <QPixmap>
16 #include <QUrl>
17 
18 class QLabel;
19 
20 class ImageLabel : public QFrame
21 {
22         Q_OBJECT
23     public:
24         explicit ImageLabel(QWidget *parent);
25         ~ImageLabel() override = default;
26         void setImage(const QImage &img);
27         void invertPixels();
28 
29         // ImageViewer needs access to the image in order to modify it
30         QImage m_Image;
31     protected:
32         void paintEvent(QPaintEvent *e) override;
33         void resizeEvent(QResizeEvent *) override;
34 
35     private:
36         QPixmap pix;
37 };
38 
39 /**
40  * @class ImageViewer
41  * @short Image viewer window for KStars
42  * @author Thomas Kabelmann
43  * @author Jasem Mutlaq
44  * @version 1.1
45  *
46  * This image-viewer automatically resizes the picture. The output
47  * works with kio-slaves and not directly with the QImage save-routines
48  * because normally the image-files are in GIF-format and QT does not
49  * save these files. For other formats, like PNG, this is not so important
50  * because they can directly saved by QImage.
51  *
52  * The download-slave works asynchron so the parent-widget can be used at
53  * this time. The save-slave works synchronously, but this is not important
54  * because the files are at this time local saved and this works not so long.
55  */
56 class ImageViewer : public QDialog
57 {
58         Q_OBJECT
59 
60     public:
61         /** Creates empty viewer. */
62         explicit ImageViewer(const QString &caption, QWidget *parent = nullptr);
63 
64         /** Create image viewer from URL with caption */
65         explicit ImageViewer(const QUrl &imageURL, const QString &capText = QString(), QWidget *parent = nullptr);
66 
67         /** Destructor. If there is a partially downloaded image file, delete it.*/
68         ~ImageViewer() override;
69 
70         /**
71          * @brief loadImage Load image from local file and display it
72          * @param filename path to local image
73          * @return True if opened and displayed, false otherwise
74          */
75         bool loadImage(const QString &filename);
76 
77         QJsonObject metadata();
78 
79     private:
80         /**
81          * Display the downloaded image.  Resize the window to fit the image,  If the image is
82          * larger than the screen, make the image as large as possible while preserving the
83          * original aspect ratio
84          */
85         bool showImage();
86 
87         /** Download the image file pointed to by the URL string. */
88         void loadImageFromURL();
89 
90         /** Save the downloaded image to a local file. */
91         void saveFile(QUrl &url);
92 
93         QFile file;
94 
95         const QUrl m_ImageUrl;
96         bool fileIsImage { false };
97         QString filename;
98 
99         FileDownloader downloadJob;
100 
101         ImageLabel *m_View { nullptr };
102         QLabel *m_Caption { nullptr };
103 
104         // Share among image viewers
105         static QUrl lastURL;
106 
107     private slots:
108         /** Initialize (common part of onstructors) */
109         void init(QString caption, QString capText);
110         /** Make sure download has finished, then make sure file exists, then display the image */
111         //void downloadReady (KJob *);
112 
113         void downloadReady();
114         void downloadError(const QString &errorString);
115 
116         /** Saves file to disc. */
117         void saveFileToDisc();
118 
119         /** Inverts colors **/
120         void invertColors();
121 };
122