1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2012 Rene Kuettner <rene@bitkanal.net>
4 //
5 
6 #ifndef MARBLE_ECLIPSESMODEL_H
7 #define MARBLE_ECLIPSESMODEL_H
8 
9 #include <QAbstractItemModel>
10 
11 #include "GeoDataCoordinates.h"
12 #include "MarbleModel.h"
13 
14 class EclSolar;
15 
16 namespace Marble
17 {
18 
19 class EclipsesItem;
20 
21 /**
22  * @brief The model for eclipses
23  *
24  * EclipsesModel provides an interface to the eclsolar backend. Instances
25  * of this class hold EclipseItem objects for every eclipse event of a given
26  * year. Furthermore, it implements QTs AbstractItemModel interface and can
27  * be used with QTs view classes.
28  */
29 class EclipsesModel : public QAbstractItemModel
30 {
31     Q_OBJECT
32 
33 public:
34     explicit EclipsesModel( const MarbleModel *model, QObject *parent = nullptr );
35 
36     ~EclipsesModel() override;
37 
38     /**
39      * @brief Return the current observation point
40      *
41      * Returns the current observation point on which location specific
42      * eclipse calculations are based.
43      *
44      * @return GeoDataCoordinates of the current observation point
45      * @see setObservationPoint
46      */
47     const GeoDataCoordinates& observationPoint() const;
48 
49     /**
50      * @brief Set the current observation point
51      *
52      * @param coords
53      *
54      * Set the current observation point to @p coords. This will mark all
55      * items for recalculation of eclipses details according to the new
56      * observation point.
57      *
58      * @see observationPoint
59      */
60     void setObservationPoint( const GeoDataCoordinates &coords );
61 
62     /**
63      * @brief Set the year
64      *
65      * @param year The year
66      *
67      * Sets the year to @p year. This clears all items in the model and
68      * fills it with all eclipse items for the given year.
69      *
70      * @see year
71      */
72     void setYear( int year );
73 
74     /**
75      * @brief Return the year
76      *
77      * Returns the year of all eclipse items in this model.
78      *
79      * @return the year of eclipse items in this model
80      * @see setYear
81      */
82     int year() const;
83 
84     /**
85      * @brief Set if lunar eclipses are enabled
86      * @param enable Indicates whether or not to allow lunar eclipses
87      *
88      * Allows to enable or disable inclusion of lunar eclipses.
89      *
90      * @see withLunarEclipses
91      */
92     void setWithLunarEclipses( const bool enable );
93 
94     /**
95      * @brief Return whether or not lunar eclipses are enabled
96      *
97      * Returns whether or not lunar eclipses are included in the eclipse
98      * calculation.
99      *
100      * @return True if lunar eclipses are enabled or false otherwise
101      * @see setWithLunarEclipses
102      */
103     bool withLunarEclipses() const;
104 
105     /**
106      * @brief Get eclipse item of a given year
107      *
108      * @param index
109      *
110      * This returns the eclipse item with @p index for the year set. If
111      * there is no eclipse with @p index in the set year, NULL will be
112      * returned.
113      *
114      * @return the requested eclipse item or NULL if there is no eclipse
115      * @see setYear
116      */
117     EclipsesItem* eclipseWithIndex( int index );
118 
119     /**
120      * @brief Return the items in this model
121      *
122      * Returns a list of items currently in the model.
123      *
124      * @return list of items in the model
125      */
126     QList<EclipsesItem*> items() const;
127 
128     // QT abstract item model interface
129     QModelIndex index( int row, int column,
130                        const QModelIndex &parent = QModelIndex() ) const override;
131     QModelIndex parent( const QModelIndex &index ) const override;
132     int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
133     int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
134     QVariant data( const QModelIndex &index,
135                    int role = Qt::DisplayRole ) const override;
136     QVariant headerData( int section, Qt::Orientation orientation,
137                          int role = Qt::DisplayRole ) const override;
138 
139 public Q_SLOTS:
140     /**
141      * @brief Update the list of eclipse items
142      *
143      * This forces an update of the current list of eclipse items by
144      * calculating all eclipse events for the currently set year and
145      * adding them to the model. All previously added items are
146      * cleared before.
147      *
148      * @see clear
149      */
150     void update();
151 
152 private:
153     /**
154      * @brief Add an item to the model
155      * @param item the item to add
156      *
157      * Adds @p item to the model.
158      *
159      * @see clear
160      */
161     void addItem( EclipsesItem *item );
162 
163     /**
164      * @brief Clears all items
165      *
166      * Clear the model by removing all items.
167      *
168      * @see addItem
169      */
170     void clear();
171 
172     const MarbleModel *m_marbleModel;
173     EclSolar *m_ecl;
174     QList<EclipsesItem*> m_items;
175     int m_currentYear;
176     bool m_withLunarEclipses;
177     GeoDataCoordinates m_observationPoint;
178 };
179 
180 }
181 
182 #endif // MARBLE_ECLIPSESMODEL_H
183