mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-21 13:21:52 +02:00
Allow to use DownloadButton as a real standalone widget
This commit is contained in:
@@ -37,22 +37,25 @@ using namespace Tomahawk;
|
|||||||
|
|
||||||
DownloadButton::DownloadButton( const Tomahawk::query_ptr& query, QWidget* parent, QAbstractItemView* view, const QModelIndex& index )
|
DownloadButton::DownloadButton( const Tomahawk::query_ptr& query, QWidget* parent, QAbstractItemView* view, const QModelIndex& index )
|
||||||
: DropDownButton( parent )
|
: DropDownButton( parent )
|
||||||
, m_query( query )
|
|
||||||
, m_view( view )
|
, m_view( view )
|
||||||
, m_index( index )
|
, m_index( index )
|
||||||
{
|
{
|
||||||
Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr();
|
init();
|
||||||
if ( result.isNull() )
|
|
||||||
return;
|
|
||||||
|
|
||||||
QStringList formats;
|
setQuery( query );
|
||||||
foreach ( const DownloadFormat& format, result->downloadFormats() )
|
}
|
||||||
{
|
|
||||||
formats << QObject::tr( "Download %1" ).arg( format.extension.toUpper() );
|
|
||||||
}
|
|
||||||
|
|
||||||
addItems( formats );
|
|
||||||
|
|
||||||
|
DownloadButton::DownloadButton( QWidget* parent )
|
||||||
|
: DropDownButton( parent )
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
DownloadButton::init()
|
||||||
|
{
|
||||||
connect( this, SIGNAL( clicked() ), this, SLOT( addDownloadJob() ) );
|
connect( this, SIGNAL( clicked() ), this, SLOT( addDownloadJob() ) );
|
||||||
connect( this, SIGNAL( activated( int ) ), 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
|
void
|
||||||
DownloadButton::addDownloadJob()
|
DownloadButton::addDownloadJob()
|
||||||
{
|
{
|
||||||
|
if ( m_query.isNull() )
|
||||||
|
return;
|
||||||
|
|
||||||
Tomahawk::result_ptr result = m_query->numResults( true ) ? m_query->results().first() : Tomahawk::result_ptr();
|
Tomahawk::result_ptr result = m_query->numResults( true ) ? m_query->results().first() : Tomahawk::result_ptr();
|
||||||
if ( result.isNull() )
|
if ( result.isNull() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ( m_view && m_index.isValid() )
|
|
||||||
{
|
|
||||||
m_view->closePersistentEditor( m_index );
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( !result->downloadFormats().isEmpty() )
|
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() ) ) );
|
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 );
|
QPainter p( this );
|
||||||
setupPainter( &p );
|
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
|
bool
|
||||||
DownloadButton::drawPrimitive( QPainter* painter, const QRect& rect, const Tomahawk::query_ptr& query, bool hovering )
|
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();
|
Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr();
|
||||||
if ( result.isNull() )
|
if ( result.isNull() )
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
if ( result->downloadJob() && result->downloadJob()->state() != DownloadJob::Finished )
|
if ( result->downloadJob() && result->downloadJob()->state() != DownloadJob::Finished )
|
||||||
{
|
{
|
||||||
|
@@ -32,9 +32,11 @@ Q_OBJECT
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
explicit DownloadButton( const Tomahawk::query_ptr& query, QWidget* parent = nullptr, QAbstractItemView* view = nullptr, const QModelIndex& index = QModelIndex() );
|
explicit DownloadButton( const Tomahawk::query_ptr& query, QWidget* parent = nullptr, QAbstractItemView* view = nullptr, const QModelIndex& index = QModelIndex() );
|
||||||
|
explicit DownloadButton( QWidget* parent = nullptr );
|
||||||
virtual ~DownloadButton();
|
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 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 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 );
|
static QWidget* handleCreateEditor( QWidget* parent, const Tomahawk::query_ptr& query , QAbstractItemView* view, const QModelIndex& index );
|
||||||
@@ -46,7 +48,10 @@ private slots:
|
|||||||
void addDownloadJob();
|
void addDownloadJob();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void init();
|
||||||
|
|
||||||
Tomahawk::query_ptr m_query;
|
Tomahawk::query_ptr m_query;
|
||||||
|
Tomahawk::result_ptr m_result;
|
||||||
QAbstractItemView* m_view;
|
QAbstractItemView* m_view;
|
||||||
QModelIndex m_index;
|
QModelIndex m_index;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user