File indexing completed on 2025-01-05 04:25:43

0001 /****************************************************************************************
0002  * Copyright (c) 2009-2010 Joffrey Clavel <jclavel@clabert.info>                        *
0003  * Copyright (c) 2010 Alexandre Mendes <alex.mendes1988@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 ARTIST_WIDGET_H
0019 #define ARTIST_WIDGET_H
0020 
0021 #include "core/meta/forward_declarations.h"
0022 #include "network/NetworkAccessManagerProxy.h"
0023 #include "SimilarArtist.h"
0024 
0025 #include <QUrl>
0026 #include <KDateTime>
0027 #include <Plasma/ScrollWidget>
0028 
0029 #include <QTextLayout>
0030 
0031 class QGraphicsGridLayout;
0032 class QGraphicsLinearLayout;
0033 class QSignalMapper;
0034 class QLabel;
0035 
0036 namespace Plasma {
0037     class PushButton;
0038 }
0039 
0040 /**
0041  * A widget for display an artist with some details
0042  * @author Joffrey Clavel
0043  * @version 0.2
0044  */
0045 class ArtistWidget : public QGraphicsWidget
0046 {
0047     Q_OBJECT
0048     Q_PROPERTY( KDateTime bioPublished READ bioPublished )
0049     Q_PROPERTY( QString fullBio READ fullBio )
0050 
0051 public:
0052     /**
0053      * ArtistWidget constructor
0054      * @param artist The pointer to the artist item
0055      * @param parent The widget parent
0056      * @param wFlags The Qt::WindowFlags
0057      */
0058     explicit ArtistWidget( const SimilarArtistPtr &artist,
0059                   QGraphicsWidget *parent = nullptr, Qt::WindowFlags wFlags = 0 );
0060 
0061     /**
0062      * ArtistWidget destructor
0063      */
0064     ~ArtistWidget();
0065 
0066     virtual void paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget = 0 );
0067 
0068     /**
0069      * Pointer to the similar artist this widget is associated with
0070      */
0071     SimilarArtistPtr artist() const;
0072 
0073     /**
0074      * Clean the widget => the content of the QLabel is empty
0075      */
0076     void clear();
0077 
0078     /**
0079      * The date/time the bio was published
0080      * @return date/time
0081      */
0082     KDateTime bioPublished() const;
0083 
0084     /**
0085      * Complete bio of artist on last.fm
0086      * @return bio of artist
0087      */
0088     QString fullBio() const;
0089 
0090     /**
0091      * Change the most known track of this artist
0092      * @param topTrack the top track of this artist
0093      */
0094     void setTopTrack( const QString &topTrack );
0095 
0096     bool eventFilter( QObject *obj, QEvent *event );
0097 
0098 Q_SIGNALS:
0099     /**
0100      * Show similar artists to the artist associated with this widget
0101      */
0102     void showSimilarArtists();
0103 
0104     /**
0105      * Show full bio of artist
0106      */
0107     void showBio();
0108 
0109 protected:
0110     void resizeEvent( QGraphicsSceneResizeEvent *event );
0111 
0112 private:
0113     void fetchPhoto();       //!< Fetch the photo of the artist
0114     void fetchInfo();        //!< Fetch the artist info
0115     void fetchTopTrack();    //!< Fetch the artist'stop track
0116 
0117     /**
0118      * Set the artist bio summary
0119      * @param bio The bio of this artist
0120      */
0121     void setBioSummary( const QString &bio );
0122 
0123     /**
0124      * Set artist tags
0125      */
0126     void setTags();
0127 
0128     /**
0129      * Layout the text for artist's bio summary
0130      */
0131     void layoutBio();
0132 
0133     /**
0134      * Layout for the formatting of the widget contents
0135      */
0136     QGraphicsGridLayout *m_layout;
0137 
0138     /**
0139      * Image of the artist
0140      */
0141     QLabel *m_image;
0142 
0143     /**
0144      * Label showing the name of the artist
0145      */
0146     QLabel *m_nameLabel;
0147 
0148     /**
0149      * Similarity match percentage
0150      */
0151     QLabel *m_match;
0152 
0153     /**
0154      * Title of the top track
0155      */
0156     QString m_topTrackTitle;
0157 
0158     /**
0159      * Label showing the title of the top track of the artist
0160      */
0161     QLabel *m_topTrackLabel;
0162 
0163     /**
0164      * Label showing artist tags
0165      */
0166     QLabel *m_tagsLabel;
0167 
0168     /**
0169      * Meta::LabelPtr to the top track, if it's in a collection
0170      */
0171     Meta::TrackPtr m_topTrack;
0172 
0173     /**
0174      * Button to add the top track to the playlist
0175      */
0176     Plasma::PushButton *m_topTrackButton;
0177 
0178     /**
0179      * Button to add the last.fm similar artist station for this artist to the playlist
0180      */
0181     Plasma::PushButton *m_lastfmStationButton;
0182 
0183     /**
0184      * Button to navigate to the artist in the local collection
0185      */
0186     Plasma::PushButton *m_navigateButton;
0187 
0188     /**
0189      * Button to open Last.fm's artist webpage using external browser
0190      */
0191     Plasma::PushButton *m_urlButton;
0192 
0193     /**
0194      * Button to show similar artists to the artist associated with this widget
0195      */
0196     Plasma::PushButton *m_similarArtistButton;
0197 
0198     /**
0199      * Bio summary of the artist
0200      */
0201     QGraphicsWidget *m_bio;
0202 
0203     /**
0204      * Text layout for the artist bio
0205      */
0206     QTextLayout m_bioLayout;
0207 
0208     /**
0209      * Whether all of artist bio is shown
0210      */
0211     bool m_bioCropped;
0212 
0213     /**
0214      * The complete bio with its published date
0215      */
0216     QPair<KDateTime, QString> m_fullBio;
0217 
0218     /**
0219      * List of artist tags
0220      */
0221     QStringList m_tags;
0222 
0223     const SimilarArtistPtr m_artist;
0224 
0225 private Q_SLOTS:
0226     /**
0227      * Handle artist photo retrieved from Last.fm
0228      */
0229     void photoFetched( const QUrl &url, QByteArray data, NetworkAccessManagerProxy::Error e );
0230 
0231     /**
0232      * Parse the xml fetched on the lastFM API for the artist info.
0233      * Launched when the download of the data are finished and for each similarArtists.
0234      */
0235     void parseInfo( const QUrl &url, QByteArray data, NetworkAccessManagerProxy::Error e );
0236 
0237     /**
0238      * Parse the xml fetched on the lastFM API for the similarArtist most known track.
0239      * Launched when the download of the data are finished and for each similarArtists.
0240      */
0241     void parseTopTrack( const QUrl &url, QByteArray data, NetworkAccessManagerProxy::Error e );
0242 
0243     /**
0244      * Open an URL
0245      * @param url The URL of the artist
0246      */
0247     void openArtistUrl();
0248 
0249     /**
0250      * Add top track to the playlist
0251      */
0252     void addTopTrackToPlaylist();
0253 
0254     /**
0255      * Navigate to this artist in the local collection
0256      */
0257     void navigateToArtist();
0258 
0259     /**
0260      * Add this artists last.fm similar artist stream
0261      */
0262     void addLastfmArtistStation();
0263 
0264     /**
0265      * Get results from the query maker
0266      */
0267     void resultReady( const Meta::TrackList &tracks );
0268 
0269     void updateInfo();
0270 };
0271 
0272 class ArtistsListWidget : public Plasma::ScrollWidget
0273 {
0274     Q_OBJECT
0275     Q_PROPERTY( QString name READ name WRITE setName )
0276 
0277 public:
0278     explicit ArtistsListWidget( QGraphicsWidget *parent = nullptr );
0279     ~ArtistsListWidget();
0280 
0281     int count() const;
0282     bool isEmpty() const;
0283 
0284     void addArtists( const SimilarArtist::List &artists );
0285 
0286     QString name() const;
0287     void setName( const QString &name );
0288 
0289     void clear();
0290 
0291     ArtistWidget *widget( const QString &artistName );
0292 
0293     QSizeF sizeHint( Qt::SizeHint which, const QSizeF &constraint = QSizeF() ) const;
0294 
0295 Q_SIGNALS:
0296     void showSimilarArtists( const QString &artist );
0297     void showBio( const QString &artist );
0298 
0299 private:
0300     void addArtist( const SimilarArtistPtr &artist );
0301     void addSeparator();
0302     int m_separatorCount;
0303     QString m_name;
0304     QGraphicsLinearLayout *m_layout;
0305     QSignalMapper *m_showArtistsSigMapper;
0306     QSignalMapper *m_showBioSigMapper;
0307     QList<ArtistWidget*> m_widgets;
0308     Q_DISABLE_COPY( ArtistsListWidget )
0309 };
0310 
0311 #endif // ARTIST_WIDGET_H