From 3394deea4619f0185e2e821d899ca15d11227bca Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 19 Mar 2015 09:08:28 +0100 Subject: [PATCH] Show download formats in drop down and start download when picking one. --- .../playlist/PlaylistItemDelegate.cpp | 63 ++++++++++++++++++- .../playlist/PlaylistItemDelegate.h | 6 ++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/libtomahawk/playlist/PlaylistItemDelegate.cpp b/src/libtomahawk/playlist/PlaylistItemDelegate.cpp index a90c83e0b..62ef5b703 100644 --- a/src/libtomahawk/playlist/PlaylistItemDelegate.cpp +++ b/src/libtomahawk/playlist/PlaylistItemDelegate.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -128,6 +129,65 @@ PlaylistItemDelegate::prepareStyleOption( QStyleOptionViewItemV4* option, const } +QWidget* +PlaylistItemDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const +{ + PlayableItem* item = m_model->itemFromIndex( m_model->mapToSource( index ) ); + Q_ASSERT( item ); + + if ( index.column() == PlayableModel::Download && item->result() && !item->result()->downloadFormats().isEmpty() ) + { + QStringList formats; + foreach ( const DownloadFormat& format, item->result()->downloadFormats() ) + { + formats << tr( "Download %1" ).arg( format.extension ); + } + + QComboBox* editor = new QComboBox( parent ); + editor->addItems( formats ); + + _detail::Closure* closure = NewClosure( editor, SIGNAL( activated( int ) ), + const_cast(this), SLOT( closeEditor( const QModelIndex&, PlayableItem*, QComboBox* ) ), index, item, editor ); + return editor; + } + + return 0; +} + + +void +PlaylistItemDelegate::closeEditor( const QModelIndex& index, PlayableItem* item, QComboBox* editor ) +{ + m_view->closePersistentEditor( index ); + + QComboBox* cb = static_cast< QComboBox* >(editor); + DownloadManager::instance()->addJob( item->result()->toDownloadJob( item->result()->downloadFormats().at( cb->currentIndex() ) ) ); +} + + +void +PlaylistItemDelegate::updateEditorGeometry( QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index ) const +{ + QStyledItemDelegate::updateEditorGeometry( editor, option, index ); + + QComboBox* comboBox = static_cast(editor); + comboBox->resize( option.rect.size() - QSize( 8, 0 ) ); + comboBox->move( option.rect.x() + 4, option.rect.y() ); + + if ( !comboBox->property( "shownPopup" ).toBool() ) + { + comboBox->showPopup(); + comboBox->setProperty( "shownPopup", true ); + } +} + + +void +PlaylistItemDelegate::setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const +{ +} + + void PlaylistItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { @@ -898,7 +958,8 @@ PlaylistItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, con } else if ( index.column() == PlayableModel::Download ) { - DownloadManager::instance()->addJob( item->result()->toDownloadJob() ); + m_view->edit( index ); + return true; } event->accept(); diff --git a/src/libtomahawk/playlist/PlaylistItemDelegate.h b/src/libtomahawk/playlist/PlaylistItemDelegate.h index 8460c10df..1fbbdebbf 100644 --- a/src/libtomahawk/playlist/PlaylistItemDelegate.h +++ b/src/libtomahawk/playlist/PlaylistItemDelegate.h @@ -32,6 +32,8 @@ class PlayableItem; class PlayableProxyModel; class TrackView; +class QComboBox; + namespace Tomahawk { class PixmapDelegateFader; } @@ -55,12 +57,16 @@ signals: private slots: void doUpdateIndex( const QPersistentModelIndex& index ); + void closeEditor( const QModelIndex& index, PlayableItem* item, QComboBox* editor ); protected: void prepareStyleOption( QStyleOptionViewItemV4* option, const QModelIndex& index, PlayableItem* item ) const; void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const; + QWidget* createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const; bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index ); + void updateEditorGeometry( QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index ) const; + void setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const; QPersistentModelIndex hoveringOver() const { return m_hoveringOver; }