From 170e2e1c8645401555921229e13f8fc1b2b84739 Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Fri, 15 Apr 2016 20:22:51 +0200 Subject: [PATCH] Allow to use DownloadButton as a real standalone widget --- src/libtomahawk/widgets/DownloadButton.cpp | 98 +++++++++++++++------- src/libtomahawk/widgets/DownloadButton.h | 7 +- 2 files changed, 75 insertions(+), 30 deletions(-) diff --git a/src/libtomahawk/widgets/DownloadButton.cpp b/src/libtomahawk/widgets/DownloadButton.cpp index aab419923..b5e424c3b 100644 --- a/src/libtomahawk/widgets/DownloadButton.cpp +++ b/src/libtomahawk/widgets/DownloadButton.cpp @@ -37,22 +37,25 @@ using namespace Tomahawk; DownloadButton::DownloadButton( const Tomahawk::query_ptr& query, QWidget* parent, QAbstractItemView* view, const QModelIndex& index ) : DropDownButton( parent ) - , m_query( query ) , m_view( view ) , m_index( index ) { - Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr(); - if ( result.isNull() ) - return; + init(); - QStringList formats; - foreach ( const DownloadFormat& format, result->downloadFormats() ) - { - formats << QObject::tr( "Download %1" ).arg( format.extension.toUpper() ); - } + setQuery( query ); +} - addItems( formats ); +DownloadButton::DownloadButton( QWidget* parent ) + : DropDownButton( parent ) +{ + init(); +} + + +void +DownloadButton::init() +{ connect( this, SIGNAL( clicked() ), this, SLOT( addDownloadJob() ) ); connect( this, SIGNAL( activated( int ) ), this, SLOT( addDownloadJob() ) ); } @@ -63,31 +66,64 @@ DownloadButton::~DownloadButton() } +void +DownloadButton::setQuery( const query_ptr& query ) +{ + if ( !m_query.isNull() ) + { + m_query->disconnect( this ); + } + if ( !m_result.isNull() ) + { + m_result->disconnect( this ); + } + + clear(); + m_result.clear(); + + m_query = query; + + if ( query.isNull() ) + return; + + Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr(); + if ( result.isNull() ) + return; + + QStringList formats; + foreach ( const DownloadFormat& format, result->downloadFormats() ) + { + formats << QObject::tr( "Download %1" ).arg( format.extension.toUpper() ); + } + + addItems( formats ); +} + void DownloadButton::addDownloadJob() { + if ( m_query.isNull() ) + return; + Tomahawk::result_ptr result = m_query->numResults( true ) ? m_query->results().first() : Tomahawk::result_ptr(); if ( result.isNull() ) return; - if ( m_view && m_index.isValid() ) - { - m_view->closePersistentEditor( m_index ); - } - if ( !result->downloadFormats().isEmpty() ) + { + if ( m_view && m_index.isValid() ) + { + m_view->closePersistentEditor( m_index ); + } + else + { + m_result = result; + connect( result.data(), SIGNAL( updated() ), SLOT( update() ) ); + connect( m_query.data(), SIGNAL( resultsChanged() ), SLOT( update() ) ); + } + DownloadManager::instance()->addJob( result->toDownloadJob( result->downloadFormats().at( currentIndex() ) ) ); -} - - -bool -DownloadButton::shouldShowButton( const Tomahawk::query_ptr& query ) -{ - Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr(); - if ( result.isNull() ) - return false; - - return result && !result->downloadFormats().isEmpty() && !result->downloadJob(); + } } @@ -97,18 +133,22 @@ DownloadButton::paintEvent( QPaintEvent* event ) QPainter p( this ); setupPainter( &p ); - DownloadButton::drawPrimitive( &p, contentsRect(), m_query, m_hovering ); + if ( DownloadButton::drawPrimitive( &p, contentsRect(), m_query, m_hovering ) ) + setCursor( Qt::PointingHandCursor ); + else + setCursor( Qt::ArrowCursor ); } bool DownloadButton::drawPrimitive( QPainter* painter, const QRect& rect, const Tomahawk::query_ptr& query, bool hovering ) { + if ( query.isNull() ) + return false; + Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr(); if ( result.isNull() ) - { return false; - } if ( result->downloadJob() && result->downloadJob()->state() != DownloadJob::Finished ) { diff --git a/src/libtomahawk/widgets/DownloadButton.h b/src/libtomahawk/widgets/DownloadButton.h index c4eddcfbd..4e4e7fb18 100644 --- a/src/libtomahawk/widgets/DownloadButton.h +++ b/src/libtomahawk/widgets/DownloadButton.h @@ -32,9 +32,11 @@ Q_OBJECT public: explicit DownloadButton( const Tomahawk::query_ptr& query, QWidget* parent = nullptr, QAbstractItemView* view = nullptr, const QModelIndex& index = QModelIndex() ); + explicit DownloadButton( QWidget* parent = nullptr ); virtual ~DownloadButton(); - static bool shouldShowButton( const Tomahawk::query_ptr& query ); + void setQuery( const Tomahawk::query_ptr& query ); + static bool drawPrimitive( QPainter* p, const QRect& rect, const Tomahawk::query_ptr& query, bool hovering ); static bool handleEditorEvent( QEvent* event, QAbstractItemView* view, PlayableProxyModel* model, const QModelIndex& index ); static QWidget* handleCreateEditor( QWidget* parent, const Tomahawk::query_ptr& query , QAbstractItemView* view, const QModelIndex& index ); @@ -46,7 +48,10 @@ private slots: void addDownloadJob(); private: + void init(); + Tomahawk::query_ptr m_query; + Tomahawk::result_ptr m_result; QAbstractItemView* m_view; QModelIndex m_index; };