From 108f8cf93bc32bf389c12446ee967ffd93c80a75 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Mon, 3 Jan 2011 13:08:41 +0100 Subject: [PATCH] * Added basic WelcomeWidget, which gets shown when Tomahawk starts up. Again, work in progress. --- src/CMakeLists.txt | 3 + src/libtomahawk/playlist.cpp | 17 +++ src/libtomahawk/playlist.h | 2 + src/libtomahawk/tomahawksettings.cpp | 29 +++++ src/libtomahawk/tomahawksettings.h | 8 +- src/playlist/playlistmanager.cpp | 10 +- src/widgets/welcomewidget.cpp | 164 +++++++++++++++++++++++++++ src/widgets/welcomewidget.h | 85 ++++++++++++++ src/widgets/welcomewidget.ui | 50 ++++++++ 9 files changed, 364 insertions(+), 4 deletions(-) create mode 100644 src/widgets/welcomewidget.cpp create mode 100644 src/widgets/welcomewidget.h create mode 100644 src/widgets/welcomewidget.ui diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ed5392389..1e5c1c6a1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -97,6 +97,7 @@ SET( tomahawkSourcesGui ${tomahawkSourcesGui} infowidgets/sourceinfowidget.cpp widgets/newplaylistwidget.cpp + widgets/welcomewidget.cpp transferview.cpp tomahawkwindow.cpp @@ -176,6 +177,7 @@ SET( tomahawkHeadersGui ${tomahawkHeadersGui} infowidgets/sourceinfowidget.h widgets/newplaylistwidget.h + widgets/welcomewidget.h transferview.h tomahawkwindow.h @@ -194,6 +196,7 @@ SET( tomahawkUI ${tomahawkUI} topbar/topbar.ui infowidgets/sourceinfowidget.ui widgets/newplaylistwidget.ui + widgets/welcomewidget.ui ) INCLUDE_DIRECTORIES( diff --git a/src/libtomahawk/playlist.cpp b/src/libtomahawk/playlist.cpp index 00fe3ba5b..cf8c32ff8 100644 --- a/src/libtomahawk/playlist.cpp +++ b/src/libtomahawk/playlist.cpp @@ -11,6 +11,7 @@ #include "database/databasecommand_renameplaylist.h" #include "pipeline.h" +#include "source.h" #include "sourcelist.h" using namespace Tomahawk; @@ -101,6 +102,22 @@ Playlist::create( const source_ptr& author, } +playlist_ptr +Playlist::load( const QString& guid ) +{ + playlist_ptr p; + + foreach( const Tomahawk::source_ptr& source, SourceList::instance()->sources() ) + { + p = source->collection()->playlist( guid ); + if ( !p.isNull() ) + return p; + } + + return p; +} + + bool Playlist::remove( const playlist_ptr& playlist ) { diff --git a/src/libtomahawk/playlist.h b/src/libtomahawk/playlist.h index a1afff28c..f198666e7 100644 --- a/src/libtomahawk/playlist.h +++ b/src/libtomahawk/playlist.h @@ -90,6 +90,8 @@ friend class ::DatabaseCommand_SetPlaylistRevision; friend class ::DatabaseCommand_CreatePlaylist; public: + static Tomahawk::playlist_ptr load( const QString& guid ); + // one CTOR is private, only called by DatabaseCommand_LoadAllPlaylists static Tomahawk::playlist_ptr create( const source_ptr& author, const QString& guid, diff --git a/src/libtomahawk/tomahawksettings.cpp b/src/libtomahawk/tomahawksettings.cpp index 862f2f2e4..743f134eb 100644 --- a/src/libtomahawk/tomahawksettings.cpp +++ b/src/libtomahawk/tomahawksettings.cpp @@ -198,6 +198,35 @@ TomahawkSettings::setPlaylistColumnSizes( const QList& cols ) } +QList +TomahawkSettings::recentlyPlayedPlaylists() const +{ + QStringList playlist_guids = value( "playlists/recentlyPlayed" ).toStringList(); + + QList playlists; + foreach( const QString& guid, playlist_guids ) + { + Tomahawk::playlist_ptr pl = Tomahawk::Playlist::load( guid ); + if ( !pl.isNull() ) + playlists << pl; + } + + return playlists; +} + + +void +TomahawkSettings::appendRecentlyPlayedPlaylist( const Tomahawk::playlist_ptr& playlist ) +{ + QStringList playlist_guids = value( "playlists/recentlyPlayed" ).toStringList(); + + playlist_guids.removeAll( playlist->guid() ); + playlist_guids.append( playlist->guid() ); + + setValue( "playlists/recentlyPlayed", playlist_guids ); +} + + bool TomahawkSettings::jabberAutoConnect() const { diff --git a/src/libtomahawk/tomahawksettings.h b/src/libtomahawk/tomahawksettings.h index 9bd8ea710..7b0d1124b 100644 --- a/src/libtomahawk/tomahawksettings.h +++ b/src/libtomahawk/tomahawksettings.h @@ -5,6 +5,8 @@ #include "dllmacro.h" +#include "playlist.h" + /** * Convenience wrapper around QSettings for tomahawk-specific config */ @@ -29,9 +31,13 @@ public: QByteArray mainWindowState() const; void setMainWindowState( const QByteArray& state ); + /// Playlist stuff QList playlistColumnSizes() const; void setPlaylistColumnSizes( const QList& cols ); - + + QList recentlyPlayedPlaylists() const; + void appendRecentlyPlayedPlaylist( const Tomahawk::playlist_ptr& playlist ); + /// Jabber settings bool jabberAutoConnect() const; /// true by default void setJabberAutoConnect( bool autoconnect = false ); diff --git a/src/playlist/playlistmanager.cpp b/src/playlist/playlistmanager.cpp index 7f771854c..5c53c7e5f 100644 --- a/src/playlist/playlistmanager.cpp +++ b/src/playlist/playlistmanager.cpp @@ -17,9 +17,12 @@ #include "albumproxymodel.h" #include "albummodel.h" #include "sourcelist.h" +#include "tomahawksettings.h" #include "infowidgets/sourceinfowidget.h" +#include "widgets/welcomewidget.h" + #define FILTER_TIMEOUT 280 @@ -63,11 +66,10 @@ PlaylistManager::PlaylistManager( QObject* parent ) m_stack->addWidget( m_superCollectionView ); m_stack->addWidget( m_superAlbumView ); - m_currentInterface = m_superCollectionView->proxyModel(); + + show( new WelcomeWidget() ); connect( &m_filterTimer, SIGNAL( timeout() ), SLOT( applyFilter() ) ); - - linkPlaylist(); } @@ -116,6 +118,8 @@ PlaylistManager::show( const Tomahawk::playlist_ptr& playlist ) m_modesAvailable = false; linkPlaylist(); + TomahawkSettings::instance()->appendRecentlyPlayedPlaylist( playlist ); + emit numSourcesChanged( SourceList::instance()->count() ); return true; } diff --git a/src/widgets/welcomewidget.cpp b/src/widgets/welcomewidget.cpp new file mode 100644 index 000000000..e2416600b --- /dev/null +++ b/src/widgets/welcomewidget.cpp @@ -0,0 +1,164 @@ +#include "welcomewidget.h" +#include "ui_welcomewidget.h" + +#include "tomahawk/tomahawkapp.h" +#include "utils/tomahawkutils.h" + +#include "playlist/playlistmanager.h" +#include "playlist/playlistmodel.h" + +#include "sourcelist.h" +#include "tomahawksettings.h" + +#include + +#define FILTER_TIMEOUT 280 + + +WelcomeWidget::WelcomeWidget( QWidget* parent ) + : QWidget( parent ) + , ui( new Ui::WelcomeWidget ) +{ + ui->setupUi( this ); + ui->playlistWidget->setItemDelegate( new PlaylistDelegate() ); + + connect( SourceList::instance(), SIGNAL( sourceAdded( Tomahawk::source_ptr ) ), SLOT( onSourceAdded( Tomahawk::source_ptr ) ) ); + + connect( ui->playlistWidget, SIGNAL( itemActivated( QListWidgetItem* ) ), SLOT( onPlaylistActivated( QListWidgetItem* ) ) ); +} + + +WelcomeWidget::~WelcomeWidget() +{ + delete ui; +} + + +void +WelcomeWidget::updatePlaylists() +{ + ui->playlistWidget->clear(); + + QList playlists = TomahawkSettings::instance()->recentlyPlayedPlaylists(); + + foreach( const Tomahawk::playlist_ptr& playlist, playlists ) + { + connect( playlist.data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ), SLOT( updatePlaylists() ) ); + + PlaylistWidgetItem* item = new PlaylistWidgetItem( playlist ); + ui->playlistWidget->addItem( item ); + item->setData( Qt::DisplayRole, playlist->title() ); + } +} + + +void +WelcomeWidget::onSourceAdded( const Tomahawk::source_ptr& source ) +{ + connect( source->collection().data(), SIGNAL( playlistsAdded( QList ) ), SLOT( updatePlaylists() ) ); + connect( source->collection().data(), SIGNAL( playlistsDeleted( QList ) ), SLOT( updatePlaylists() ) ); +} + + +void +WelcomeWidget::onPlaylistActivated( QListWidgetItem* item ) +{ + qDebug() << Q_FUNC_INFO; + + PlaylistWidgetItem* pwi = dynamic_cast(item); + APP->playlistManager()->show( pwi->playlist() ); +} + + +void +WelcomeWidget::changeEvent( QEvent* e ) +{ + QWidget::changeEvent( e ); + switch ( e->type() ) + { + case QEvent::LanguageChange: + ui->retranslateUi( this ); + break; + + default: + break; + } +} + + +QVariant +PlaylistWidgetItem::data( int role ) const +{ + if ( role == ArtistRole ) + { + if ( m_artists.isEmpty() ) + { + QStringList artists; + + foreach( const Tomahawk::plentry_ptr& entry, m_playlist->entries() ) + { + if ( !artists.contains( entry->query()->artist() ) ) + artists << entry->query()->artist(); + } + + m_artists = artists.join( ", " ); + } + + return m_artists; + } + + if ( role == TrackCountRole ) + { + return m_playlist->entries().count(); + } + + if ( role == Qt::DisplayRole ) + { + return m_playlist->title(); + } + + return QListWidgetItem::data( role ); +} + + +QSize +PlaylistDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const +{ + return QSize( 0, 64 ); +} + + +void +PlaylistDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const +{ + QStyleOptionViewItemV4 opt = option; + initStyleOption( &opt, QModelIndex() ); + APP->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter ); + + if ( option.state & QStyle::State_Selected ) + { + opt.palette.setColor( QPalette::Text, opt.palette.color( QPalette::HighlightedText ) ); + } + + painter->save(); + painter->setRenderHint( QPainter::Antialiasing ); + painter->setPen( opt.palette.color( QPalette::Text ) ); + + QTextOption to; + to.setAlignment( Qt::AlignCenter ); + QFont font = opt.font; + QFont boldFont = opt.font; + boldFont.setBold( true ); + + painter->drawPixmap( option.rect.adjusted( 10, 12, -option.rect.width() + 42, -12 ), m_playlistIcon ); + + painter->drawText( option.rect.adjusted( 56, 26, -100, -8 ), index.data( PlaylistWidgetItem::ArtistRole ).toString() ); + + QString trackCount = tr( "%1 tracks" ).arg( index.data( PlaylistWidgetItem::TrackCountRole ).toString() ); + painter->drawText( option.rect.adjusted( option.rect.width() - 96, 2, 0, -2 ), trackCount, to ); + + painter->setFont( boldFont ); + painter->drawText( option.rect.adjusted( 56, 6, -100, -option.rect.height() + 20 ), index.data().toString() ); + + painter->restore(); +} diff --git a/src/widgets/welcomewidget.h b/src/widgets/welcomewidget.h new file mode 100644 index 000000000..ed5bd48f8 --- /dev/null +++ b/src/widgets/welcomewidget.h @@ -0,0 +1,85 @@ +#ifndef WELCOMEWIDGET_H +#define WELCOMEWIDGET_H + +#include +#include +#include + +#include "tomahawk/tomahawkapp.h" +#include "playlistinterface.h" + +#include "playlist.h" +#include "result.h" + +class PlaylistModel; + +namespace Ui +{ + class WelcomeWidget; +} + +class PlaylistDelegate : public QStyledItemDelegate +{ +Q_OBJECT + +public: + PlaylistDelegate() + { + m_playlistIcon = QPixmap( RESPATH "images/playlist-icon.png" ); + } + +protected: + void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const; + QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const; + +private: + QPixmap m_playlistIcon; +}; + + +class PlaylistWidgetItem : public QListWidgetItem +{ +public: + enum ItemRoles + { ArtistRole = Qt::UserRole, TrackCountRole }; + + PlaylistWidgetItem( const Tomahawk::playlist_ptr& playlist ) : QListWidgetItem() { m_playlist = playlist; } + ~PlaylistWidgetItem() {} + + virtual QVariant data( int role ) const; + + Tomahawk::playlist_ptr playlist() const { return m_playlist; } + +private: + Tomahawk::playlist_ptr m_playlist; + + mutable QString m_artists; +}; + + +class WelcomeWidget : public QWidget +{ +Q_OBJECT + +public: + WelcomeWidget( QWidget* parent = 0 ); + ~WelcomeWidget(); + +protected: + void changeEvent( QEvent* e ); + +signals: + void destroyed( QWidget* widget ); + +public slots: + void updatePlaylists(); + +private slots: + void onSourceAdded( const Tomahawk::source_ptr& source ); + void onPlaylistActivated( QListWidgetItem* item ); + +private: + Ui::WelcomeWidget *ui; +}; + +#endif // WELCOMEWIDGET_H diff --git a/src/widgets/welcomewidget.ui b/src/widgets/welcomewidget.ui new file mode 100644 index 000000000..7fc27e6b6 --- /dev/null +++ b/src/widgets/welcomewidget.ui @@ -0,0 +1,50 @@ + + + WelcomeWidget + + + + 0 + 0 + 985 + 460 + + + + + + + + 20 + 75 + true + + + + Welcome to Tomahawk! + + + false + + + + + + + + 14 + + + + Recently played playlists: + + + + + + + + + + +