File indexing completed on 2024-04-14 03:47:39

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
0004 //
0005 
0006 #ifndef MARBLE_ABSTRACTDATAPLUGINMODEL_H
0007 #define MARBLE_ABSTRACTDATAPLUGINMODEL_H
0008 
0009 #include <QObject>
0010 #include <QList>
0011 #include <QHash>
0012 
0013 #include "marble_export.h"
0014 
0015 class QPoint;
0016 class QUrl;
0017 class QString;
0018 class QStringList;
0019 
0020 namespace Marble
0021 {
0022     
0023 class AbstractDataPluginModelPrivate;
0024 class AbstractDataPluginItem;
0025 class GeoDataLatLonAltBox;
0026 class MarbleModel;
0027 class ViewportParams;
0028 
0029 /**
0030  * @short An abstract data model (not based on QAbstractModel) for a AbstractDataPlugin.
0031  *
0032  * This class is an abstract model for a AbstractDataPlugin. 
0033  * It provides the storage and selection of added <b>items</b> and it is also responsible for
0034  * downloading item data.
0035  *
0036  * The functions <b>getAdditionalItems()</b> and <b>parseFile()</b> have to be reimplemented in
0037  * a subclass.
0038  **/
0039 class MARBLE_EXPORT AbstractDataPluginModel : public QObject
0040 {
0041     Q_OBJECT
0042 
0043     /** @todo FIXME Qt Quick segfaults if using the real class here instead of QObject */
0044     Q_PROPERTY( QObject* favoritesModel READ favoritesModel CONSTANT )
0045  
0046  public:
0047     explicit AbstractDataPluginModel( const QString& name, const MarbleModel *marbleModel, QObject *parent = nullptr );
0048     ~AbstractDataPluginModel() override;
0049         
0050     const MarbleModel *marbleModel() const;
0051 
0052     /**
0053      * @brief Get the items on the viewport
0054      * Returns the currently downloaded images in the @p viewport.
0055      * The maximum number of images can be specified with @p number,
0056      * 0 means no limit.
0057      * @return The list of item with most important item first.
0058      */
0059     QList<AbstractDataPluginItem*> items( const ViewportParams *viewport,
0060                                           qint32 number = 10 );
0061     
0062     /**
0063      * @brief Get all items that contain the given point
0064      * Returns a list of all items that contain the point @p curpos
0065      */
0066     QList<AbstractDataPluginItem *> whichItemAt( const QPoint& curpos );
0067 
0068     /**
0069      * @brief Sets the settings for all items.
0070      * Sets the settings for all items before painting. This ensures that all items react on
0071      * changed settings.
0072      */
0073     void setItemSettings(const QHash<QString, QVariant> &itemSettings);
0074 
0075     virtual void setFavoriteItems( const QStringList& list );
0076     QStringList favoriteItems() const;
0077 
0078     void setFavoriteItemsOnly( bool favoriteOnly );
0079     bool isFavoriteItemsOnly() const;
0080 
0081     QObject* favoritesModel();
0082 
0083     /**
0084      * Finds the item with @p id in the list.
0085      * @return The pointer to the item or (if no item has been found) 0
0086      */
0087     AbstractDataPluginItem *findItem( const QString& id ) const;
0088 
0089     /**
0090      * Testing the existence of the item @p id in the list
0091      */
0092     bool itemExists( const QString& id ) const;
0093 
0094 public Q_SLOTS:
0095     /**
0096      * Adds the @p items to the list of initialized items. It checks if items with the same id are
0097      * already in the list and ignores and deletes them in this case.
0098      */
0099     void addItemsToList( const QList<AbstractDataPluginItem*> &items );
0100 
0101     /**
0102      * Convenience method to add one item to the list. See addItemsToList
0103      */
0104     void addItemToList( AbstractDataPluginItem *item );
0105 
0106     /**
0107      * Removes all items
0108      */
0109     void clear();
0110 
0111  protected:
0112     /**
0113      * Managing to get @p number additional items in @p box. This includes generating a url and
0114      * downloading the corresponding file.
0115      * This method has to be implemented in a subclass.
0116      **/
0117     virtual void getAdditionalItems( const GeoDataLatLonAltBox& box,
0118                                      qint32 number = 10 ) = 0;
0119 
0120     /**
0121      * @brief Retrieve data for a specific item
0122      * @param id Item id of the item to retrieve
0123      */
0124     virtual void getItem( const QString &id );
0125        
0126     /**
0127      * Parse the @p file and generate items. The items will be added to the list or the method
0128      * starts additionally needed downloads.
0129      * This method has to be implemented in a subclass.
0130      **/
0131     virtual void parseFile( const QByteArray& file );
0132         
0133     /**
0134      * Downloads the file from @p url. @p item -> addDownloadedFile() will be called when the
0135      * download is finished.
0136      * @param url the file URL
0137      * @param type The type of the download (to be specified by the subclasser)
0138      * @param item the data plugin item
0139      **/
0140     void downloadItem( const QUrl& url, const QString& type, AbstractDataPluginItem *item );
0141 
0142     /**
0143      * Download the description file from the @p url.
0144      */
0145     void downloadDescriptionFile( const QUrl& url );
0146 
0147     void registerItemProperties( const QMetaObject& item );
0148     
0149  private Q_SLOTS:
0150     /**
0151      * @brief Get new items with getAdditionalItems if it is reasonable.
0152      */
0153     void handleChangedViewport();
0154     
0155     /**
0156      * @brief This method will assign downloaded files to the corresponding items
0157      * @param relativeUrlString The string containing the relative (to the downloader path)
0158      *                          url of the downloaded file.
0159      * @param id The id of the downloaded file
0160      */
0161     void processFinishedJob( const QString& relativeUrlString, const QString& id );
0162     
0163     /**
0164      * @brief Removes the item from the list.
0165      */
0166     void removeItem( QObject *item );
0167 
0168     void favoriteItemChanged( const QString& id, bool isFavorite );
0169 
0170     void scheduleItemSort();
0171 
0172     void themeChanged();
0173 
0174  Q_SIGNALS:
0175     void itemsUpdated();
0176     void favoriteItemsChanged( const QStringList& favoriteItems );
0177     void favoriteItemsOnlyChanged();
0178 
0179  private:
0180     AbstractDataPluginModelPrivate * const d;
0181     friend class AbstractDataPluginModelPrivate;
0182 };
0183 
0184 }
0185 
0186 #endif