File indexing completed on 2024-04-14 03:41:15

0001 /*
0002     SPDX-FileCopyrightText: Thomas Kabelmann
0003     SPDX-FileCopyrightText: 2018 Robert Lancaster <rlancaste@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include "auxiliary/filedownloader.h"
0011 
0012 #include <QDialog>
0013 #include <QLineEdit>
0014 #include <QSpinBox>
0015 #include "nonlineardoublespinbox.h"
0016 #include <QComboBox>
0017 #include <QFile>
0018 #include <QFrame>
0019 #include <QImage>
0020 #include <QPixmap>
0021 #include "kstarsdata.h"
0022 #include <QEvent>
0023 #include <QGestureEvent>
0024 #include <QPinchGesture>
0025 
0026 #ifndef Q_OS_WIN
0027 #include<QFutureWatcher>
0028 #endif
0029 
0030 
0031 class QLabel;
0032 /**
0033  * @class XPlanetImageLabel
0034  * @short XPlanet Image viewer QFrame for the KPlanetImageViewer for KStars
0035  * @author Thomas Kabelmann
0036  * @author Jasem Mutlaq
0037  * @author Robert Lancaster
0038  * @version 1.1
0039  *
0040  * This image-viewer QFrame automatically resizes the picture.  It also receives input from the mouse to
0041  * zoom and pan the XPlanet Image.
0042  */
0043 class XPlanetImageLabel : public QFrame
0044 {
0045     Q_OBJECT
0046   public:
0047     explicit XPlanetImageLabel(QWidget *parent);
0048     ~XPlanetImageLabel() override = default;
0049     void setImage(const QImage &img);
0050     void invertPixels();
0051     void refreshImage();
0052 
0053   public slots:
0054     void wheelEvent(QWheelEvent *e) override;
0055     void mousePressEvent(QMouseEvent *e) override;
0056     void mouseReleaseEvent(QMouseEvent *e) override;
0057     void mouseMoveEvent(QMouseEvent *e) override;
0058 
0059   signals:
0060     void zoomIn();
0061     void zoomOut();
0062     void changePosition(QPoint);
0063     void changeLocation(QPoint);
0064 
0065   protected:
0066     void paintEvent(QPaintEvent *e) override;
0067     void resizeEvent(QResizeEvent *) override;
0068 
0069   private:
0070     //Image related
0071     QPixmap m_Pix;
0072     QImage m_Image;
0073 
0074     //Mouse related
0075     bool m_MouseButtonDown = false;
0076     QPoint m_LastMousePoint;
0077     bool event(QEvent *event) override;
0078     bool gestureEvent(QGestureEvent *event);
0079     void pinchTriggered(QPinchGesture *gesture);
0080 };
0081 
0082 /**
0083  * @class XPlanetImageViewer
0084  * @short XPlanet Image viewer window for KStars
0085  * @author Thomas Kabelmann
0086  * @author Jasem Mutlaq
0087  * @author Robert Lancaster
0088  * @version 1.1
0089  *
0090  * This class is meant to interact with XPlanet and display the results.  It has interfaces to control many of the XPlanet Options.
0091  * It can change a number of properties using controls.  It works with the XPlanetImageLabel to display the results.  It also can work
0092  * with the XPlanetImageLabel to respond to mouse inputs on the ImageLabel and act on them in the Image Viewer.  It has a hover mode where
0093  * it hovers over the same planetary body or a remote mode where it views one body from another.  It has interfaces for changing the field of
0094  * view, the time, the rotation, and other settings.  In hover mode, it can change latitude, longitude, and radius with the mouse inputs.
0095  * It also can animate events so that you can watch xplanet frames like a movie to see solar system events in action.
0096  */
0097 class XPlanetImageViewer : public QDialog
0098 {
0099     Q_OBJECT
0100 
0101   public:
0102     /** Create xplanet image viewer from Object */
0103     explicit XPlanetImageViewer(const QString &obj, QWidget *parent = nullptr);
0104 
0105     /** Destructor. If there is a partially downloaded image file, delete it.*/
0106     ~XPlanetImageViewer() override;
0107 
0108     /**
0109      * @brief loadImage Load image and display it
0110      * @return True if opened and displayed, false otherwise
0111      */
0112     bool loadImage();
0113 
0114     void startXplanet();
0115 
0116   private:
0117 
0118 
0119     /** prepares the output file**/
0120     bool setupOutputFile();
0121 
0122     QImage m_Image;
0123 
0124     /** Save the downloaded image to a local file. */
0125     void saveFile(const QString & fileName);
0126 
0127     QFile m_File;
0128 
0129     XPlanetImageLabel *m_View { nullptr };
0130     QLabel *m_Caption { nullptr };
0131 
0132     QString m_LastFile;
0133 
0134     QStringList m_ObjectNames;
0135     QList<double> m_objectDefaultFOVs;
0136 
0137     typedef enum { YEARS, MONTHS, DAYS, HOURS, MINS, SECS } timeUnits;
0138 
0139     void setXPlanetDate(KStarsDateTime time);
0140 
0141     //XPlanet strings
0142     QString m_ObjectName;
0143     QString m_OriginName;
0144     QString m_Date;
0145     QString m_DateText;
0146 
0147     //XPlanet numbers
0148     int m_CurrentObjectIndex { 0 };
0149     int m_CurrentOriginIndex { 0 };
0150     int m_Rotation { 0 };
0151     int m_CurrentTimeUnitIndex { 0 };
0152     uint32_t m_Radius { 0 };
0153     double m_FOV { 0 };
0154     double m_lat { 0 };
0155     double m_lon { 0 };
0156     QPoint center;
0157 
0158 #ifndef Q_OS_WIN
0159     QFutureWatcher<bool> fifoImageLoadWatcher;
0160     QTimer watcherTimeout;
0161 #endif
0162 
0163     // Time
0164     KStarsDateTime m_XPlanetTime {};
0165     bool m_XPlanetRunning = false;
0166 
0167     QComboBox *m_OriginSelector {nullptr};
0168 
0169     // Field of view controls
0170     QPushButton *m_KStarsFOV  {nullptr};
0171     QPushButton *m_setFOV  {nullptr};
0172     QPushButton *m_NoFOV  {nullptr};
0173     NonLinearDoubleSpinBox *m_FOVEdit {nullptr};
0174 
0175     // Rotation controls
0176     QSpinBox *m_RotateEdit {nullptr};
0177 
0178     // Free rotation controls
0179     QLabel *m_PositionDisplay {nullptr};
0180     QPushButton *m_FreeRotate {nullptr};
0181     bool m_ImageLoadSucceeded = false;
0182 
0183     // Time controls
0184     QLabel *m_XPlanetTimeDisplay {nullptr};
0185     QSlider *m_TimeSlider {nullptr};
0186     QSpinBox *m_TimeEdit {nullptr};
0187     QComboBox *m_TimeUnitsSelect {nullptr};
0188 
0189     //Animation controls
0190     QPushButton *m_RunTime {nullptr};
0191     QTimer *m_XPlanetTimer {nullptr};
0192 
0193 
0194   private slots:
0195 
0196     /**
0197      * Display the downloaded image.  Resize the window to fit the image,  If the image is
0198      * larger than the screen, make the image as large as possible while preserving the
0199      * original aspect ratio
0200      */
0201     bool showImage();
0202 
0203     // Saves file to disk.
0204     void saveFileToDisk();
0205 
0206     // Inverts colors
0207     void invertColors();
0208 
0209     // Rotation slots
0210     void updateXPlanetRotationEdit();
0211     void resetXPlanetRotation();
0212     void invertXPlanetRotation();
0213 
0214     void reCenterXPlanet();
0215 
0216     // Free Rotation slots
0217     void changeXPlanetPosition(QPoint delta);
0218     void changeXPlanetLocation(QPoint delta);
0219     void slotFreeRotate();
0220     void updateStates();
0221     void updatePositionDisplay();
0222     void resetLocation();
0223 
0224     // Field of View slots
0225     void zoomInXPlanetFOV();
0226     void zoomOutXPlanetFOV();
0227     void updateXPlanetFOVEdit();
0228     void resetXPlanetFOV();
0229     void setKStarsXPlanetFOV();
0230     void setFOVfromList();
0231 
0232     // Time slots
0233     void updateXPlanetTime(int timeShift);
0234     void updateXPlanetObject(int objectIndex);
0235     void updateXPlanetOrigin(int originIndex);
0236     void updateXPlanetTimeUnits(int units);
0237     void updateXPlanetTimeEdit();
0238     void setXPlanetTime();
0239     void setXPlanetTimetoKStarsTime();
0240     void resetXPlanetTime();
0241 
0242     // Animation slots
0243     void incrementXPlanetTime();
0244     void toggleXPlanetRun();
0245     void timeSliderDisplay(int timeShift);
0246 
0247 };