File indexing completed on 2024-05-05 04:48:22

0001 /****************************************************************************************
0002  * Copyright (c) 2004 Pierpaolo Di Panfilo <pippo_dp@libero.it>                         *
0003  * Copyright (c) 2007 Dan Meltzer <parallelgrapefruit@gmail.com>                        *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #ifndef COVERMANAGER_H
0019 #define COVERMANAGER_H
0020 
0021 #include "core/meta/Observer.h"
0022 #include "covermanager/CoverFetcher.h"
0023 
0024 #include <QDialog>
0025 
0026 #include <QDropEvent>
0027 #include <QLabel>
0028 #include <QListWidget>
0029 #include <QListWidgetItem>
0030 #include <QAction>
0031 
0032 namespace Amarok { class LineEdit; }
0033 
0034 class CompoundProgressBar;
0035 class CoverViewItem;
0036 class QTreeWidget;
0037 class QTreeWidgetItem;
0038 class KSqueezedTextLabel;
0039 class QPushButton;
0040 class QMenu;
0041 class QLabel;
0042 class CoverView;
0043 class QHBoxLayout;
0044 class QHBoxLayout;
0045 class QColorGroup;
0046 class QSplitter;
0047 
0048 class CoverManager : public QDialog, public Meta::Observer
0049 {
0050         Q_OBJECT
0051 
0052         static CoverManager *s_instance;
0053         static bool s_constructed;
0054 
0055     public:
0056         explicit CoverManager( QWidget *parent = nullptr );
0057        ~CoverManager() override;
0058 
0059         static bool isConstructed() { return s_constructed; }
0060         static CoverManager *instance() { return s_instance; }
0061 
0062         static void showOnce( const QString &artist = QString(), QWidget* parent = nullptr );
0063         static void viewCover( const Meta::AlbumPtr &album, QWidget* parent = nullptr );
0064 
0065         void setStatusText(const QString &text );
0066 
0067         // Reimplemented from Meta::Observer
0068         using Observer::metadataChanged;
0069         void metadataChanged( const Meta::AlbumPtr &album ) override;
0070 
0071     public Q_SLOTS:
0072         void updateStatusBar();
0073         void delayedDestruct();
0074 
0075     private:
0076         enum View { AllAlbums = 0, AlbumsWithCover, AlbumsWithoutCover };
0077 
0078     private Q_SLOTS:
0079         void slotArtistQueryResult( Meta::ArtistList artists );
0080         void slotContinueConstruction();
0081 
0082         void slotArtistSelected();
0083         void slotAlbumQueryResult( const Meta::AlbumList &albums );
0084         void slotAlbumFilterTriggered( QAction *action );
0085         void slotArtistQueryDone();
0086         void coverItemClicked( QListWidgetItem *item );
0087         void slotSetFilter();
0088         void slotSetFilterTimeout();
0089 
0090         void slotShowAllAlbums()          { changeView( AllAlbums );          }
0091         void slotShowAlbumsWithCover()    { changeView( AlbumsWithCover );    }
0092         void slotShowAlbumsWithoutCover() { changeView( AlbumsWithoutCover ); }
0093         void changeView( View id, bool force = false );
0094 
0095         void fetchMissingCovers();
0096         void updateFetchingProgress( int state );
0097         void stopFetching();
0098 
0099         void progressAllDone();
0100         void cancelCoverViewLoading();
0101 
0102     private:
0103         void loadCover( const QString &, const QString & );
0104 
0105         QSplitter        *m_splitter;
0106         QTreeWidget      *m_artistView;
0107         CoverView        *m_coverView;
0108 
0109         //hack to have something to show while the real list is hidden when loading thumbnails
0110         CoverView        *m_coverViewSpacer;
0111         Amarok::LineEdit *m_searchEdit;
0112         QPushButton      *m_fetchButton;
0113         QPushButton      *m_viewButton;
0114         QMenu            *m_viewMenu;
0115         View              m_currentView;
0116 
0117         Meta::ArtistList m_artistList;
0118         QList< QTreeWidgetItem* > m_items;
0119         Meta::AlbumList m_albumList;
0120 
0121         CoverFetcher   *m_fetcher;
0122 
0123         QAction        *m_selectAllAlbums;
0124         QAction        *m_selectAlbumsWithCover;
0125         QAction        *m_selectAlbumsWithoutCover;
0126 
0127         //status bar widgets
0128         CompoundProgressBar *m_progress;
0129         KSqueezedTextLabel *m_statusLabel;
0130         QString         m_oldStatusText;
0131 
0132         QTimer         *m_timer;              //search filter timer
0133         QList<CoverViewItem*> m_coverItems; //used for filtering
0134         QString         m_filter;
0135 
0136 
0137         // Used by fetchCoversLoop() for temporary storage
0138         Meta::AlbumList m_fetchCovers;
0139 
0140         //used to display information about cover fetching in the status bar
0141         bool m_fetchingCovers;
0142         int m_coversFetched;
0143         int m_coverErrors;
0144 
0145         bool m_isLoadingCancelled;
0146 };
0147 
0148 class CoverView : public QListWidget
0149 {
0150     Q_OBJECT
0151 
0152     public:
0153         explicit CoverView( QWidget *parent = nullptr, const char *name = nullptr, Qt::WindowFlags f = {} );
0154 
0155     protected:
0156         void contextMenuEvent( QContextMenuEvent *event ) override;
0157 
0158     private Q_SLOTS:
0159         void setStatusText( QListWidgetItem *item );
0160 };
0161 
0162 class CoverViewItem : public QListWidgetItem
0163 {
0164     public:
0165         CoverViewItem( QListWidget *parent, Meta::AlbumPtr album );
0166         ~CoverViewItem() override;
0167 
0168         void loadCover();
0169         bool hasCover() const;
0170         bool canRemoveCover() const { return !m_embedded && hasCover(); }
0171         QString artist() const { return m_artist; }
0172         QString album() const { return m_album; }
0173         Meta::AlbumPtr albumPtr() const { return m_albumPtr; }
0174 
0175     protected:
0176         void paintFocus(QPainter *, const QColorGroup &) { }
0177         void dragEntered();
0178         void dragLeft();
0179 
0180     private:
0181         Meta::AlbumPtr m_albumPtr;
0182         QString m_artist;
0183         QString m_album;
0184         QString m_coverImagePath;
0185         bool    m_embedded;
0186 };
0187 
0188 #endif