mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-01 03:40:16 +02:00
Allow playlist updaters to put custom questions in playlist delete dialogs
This commit is contained in:
@@ -37,7 +37,9 @@
|
||||
#include "PlaylistPlaylistInterface.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
#include "utils/Closure.h"
|
||||
#include "PlaylistUpdaterInterface.h"
|
||||
#include "widgets/SourceTreePopupDialog.h"
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
@@ -311,6 +313,84 @@ Playlist::removeUpdater( PlaylistUpdaterInterface* updater )
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
Playlist::hasCustomDeleter() const
|
||||
{
|
||||
foreach ( PlaylistUpdaterInterface* updater, m_updaters )
|
||||
{
|
||||
if ( !updater->deleteQuestions().isEmpty() )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Playlist::customDelete( const QPoint& leftCenter )
|
||||
{
|
||||
if ( !hasCustomDeleter() )
|
||||
return;
|
||||
|
||||
Tomahawk::PlaylistDeleteQuestions questions;
|
||||
foreach ( PlaylistUpdaterInterface* updater, m_updaters )
|
||||
{
|
||||
if ( updater->deleteQuestions().isEmpty() )
|
||||
continue;
|
||||
|
||||
questions.append( updater->deleteQuestions() );
|
||||
}
|
||||
|
||||
Q_ASSERT( !questions.isEmpty() );
|
||||
SourceTreePopupDialog* dialog = new SourceTreePopupDialog;
|
||||
NewClosure( dialog, SIGNAL( result( bool ) ), this, SLOT( onDeleteResult( SourceTreePopupDialog* ) ), dialog );
|
||||
|
||||
dialog->setMainText( tr( "Would you like to delete the playlist <b>\"%2\"</b>?", "e.g. Would you like to delete the playlist named Foobar?" )
|
||||
.arg( title() ) );
|
||||
dialog->setOkButtonText( tr( "Delete" ) );
|
||||
dialog->setExtraQuestions( questions );
|
||||
|
||||
dialog->move( leftCenter.x() - dialog->offset(), leftCenter.y() - dialog->sizeHint().height() / 2. );
|
||||
dialog->show();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Playlist::onDeleteResult( SourceTreePopupDialog* dialog )
|
||||
{
|
||||
dialog->deleteLater();
|
||||
|
||||
const bool ret = dialog->resultValue();
|
||||
|
||||
if ( !ret )
|
||||
return;
|
||||
|
||||
playlist_ptr p = m_weakSelf.toStrongRef();
|
||||
if ( p.isNull() )
|
||||
{
|
||||
qWarning() << "Got null m_weakSelf weak ref in Playlsit::onDeleteResult!!";
|
||||
Q_ASSERT( false );
|
||||
return;
|
||||
}
|
||||
|
||||
const QMap< int, bool > questionResults = dialog->questionResults();
|
||||
foreach ( PlaylistUpdaterInterface* updater, m_updaters )
|
||||
{
|
||||
updater->setQuestionResults( questionResults );
|
||||
}
|
||||
|
||||
dynplaylist_ptr dynpl = p.dynamicCast< DynamicPlaylist >();
|
||||
if ( !dynpl.isNull() )
|
||||
{
|
||||
DynamicPlaylist::remove( dynpl );
|
||||
}
|
||||
else
|
||||
{
|
||||
remove( p );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Playlist::loadRevision( const QString& rev )
|
||||
{
|
||||
|
@@ -195,6 +195,18 @@ public:
|
||||
void removeUpdater( PlaylistUpdaterInterface* updater );
|
||||
QList<PlaylistUpdaterInterface*> updaters() const { return m_updaters; }
|
||||
|
||||
/**
|
||||
* Some updaters might have custom deleters in order to perform more actions that require
|
||||
* user prompting on delete.
|
||||
*/
|
||||
bool hasCustomDeleter() const;
|
||||
/**
|
||||
* If this playlist has a custom deleter, let it do the deleting itself.
|
||||
*
|
||||
* If it needs user prompting, use the \param customDeleter as the right-most center point.
|
||||
*/
|
||||
void customDelete( const QPoint& rightCenter );
|
||||
|
||||
Tomahawk::playlistinterface_ptr playlistInterface();
|
||||
|
||||
signals:
|
||||
@@ -284,6 +296,7 @@ private slots:
|
||||
void onResultsFound( const QList<Tomahawk::result_ptr>& results );
|
||||
void onResolvingFinished();
|
||||
|
||||
void onDeleteResult( SourceTreePopupDialog* );
|
||||
private:
|
||||
Playlist();
|
||||
void init();
|
||||
|
@@ -98,6 +98,9 @@ namespace Tomahawk
|
||||
typedef QMultiHash< QString, SerializedUpdater > SerializedUpdaters;
|
||||
typedef QList< SerializedUpdater > SerializedUpdaterList;
|
||||
|
||||
// Yes/no questions with an associated enum value
|
||||
typedef QPair< QString, int > PlaylistDeleteQuestion;
|
||||
typedef QList< PlaylistDeleteQuestion > PlaylistDeleteQuestions;
|
||||
|
||||
namespace InfoSystem
|
||||
{
|
||||
|
@@ -131,44 +131,28 @@ SpotifyPlaylistUpdater::remove( bool askToDeletePlaylist )
|
||||
|
||||
|
||||
void
|
||||
SpotifyPlaylistUpdater::aboutToDelete()
|
||||
SpotifyPlaylistUpdater::unsyncOrDelete( bool toDelete )
|
||||
{
|
||||
if ( QThread::currentThread() != QApplication::instance()->thread() )
|
||||
QMetaObject::invokeMethod( const_cast<SpotifyPlaylistUpdater*>(this), "aboutToDelete", Qt::BlockingQueuedConnection );
|
||||
QMetaObject::invokeMethod( const_cast<SpotifyPlaylistUpdater*>(this), "unsyncOrDelete", Qt::BlockingQueuedConnection, Q_ARG( bool, toDelete ) );
|
||||
else
|
||||
{
|
||||
if ( m_subscribed )
|
||||
{
|
||||
m_spotify.data()->setSubscribedForPlaylist( playlist(), false );
|
||||
}
|
||||
else if ( m_sync )
|
||||
else if ( m_sync && toDelete )
|
||||
{
|
||||
checkDeleteDialog();
|
||||
// User wants to delete it!
|
||||
QVariantMap msg;
|
||||
msg[ "_msgtype" ] = "deletePlaylist";
|
||||
msg[ "playlistid" ] = m_spotifyId;
|
||||
m_spotify.data()->sendMessage( msg );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SpotifyPlaylistUpdater::checkDeleteDialog() const
|
||||
{
|
||||
// Ask if we should delete the playlist on the spotify side as well
|
||||
QMessageBox askDelete( QMessageBox::Question, tr( "Delete in Spotify?" ), tr( "Would you like to delete the corresponding Spotify playlist as well?" ), QMessageBox::Yes | QMessageBox::No, 0 );
|
||||
int ret = askDelete.exec();
|
||||
if ( ret == QMessageBox::Yes )
|
||||
{
|
||||
if ( m_spotify.isNull() )
|
||||
return;
|
||||
|
||||
// User wants to delete it!
|
||||
QVariantMap msg;
|
||||
msg[ "_msgtype" ] = "deletePlaylist";
|
||||
msg[ "playlistid" ] = m_spotifyId;
|
||||
m_spotify.data()->sendMessage( msg );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SpotifyPlaylistUpdater::playlistRevisionLoaded()
|
||||
{
|
||||
@@ -293,6 +277,25 @@ SpotifyPlaylistUpdater::canSubscribe() const
|
||||
}
|
||||
|
||||
|
||||
PlaylistDeleteQuestions
|
||||
SpotifyPlaylistUpdater::deleteQuestions() const
|
||||
{
|
||||
// 1234 is our magic key
|
||||
if ( m_sync && !m_subscribed )
|
||||
return Tomahawk::PlaylistDeleteQuestions() << qMakePair<QString, int>( tr( "Delete associated Spotify playlist?" ), 1234 );
|
||||
else
|
||||
return Tomahawk::PlaylistDeleteQuestions();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SpotifyPlaylistUpdater::setQuestionResults( const QMap< int, bool > results )
|
||||
{
|
||||
const bool toDelete = results.value( 1234, false );
|
||||
unsyncOrDelete( toDelete );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SpotifyPlaylistUpdater::spotifyTracksAdded( const QVariantList& tracks, const QString& startPosId, const QString& newRev, const QString& oldRev )
|
||||
{
|
||||
|
@@ -23,6 +23,7 @@
|
||||
#include "playlist/PlaylistUpdaterInterface.h"
|
||||
#include "utils/Closure.h"
|
||||
#include "DllMacro.h"
|
||||
#include "Typedefs.h"
|
||||
|
||||
#include <QQueue>
|
||||
#include <QVariant>
|
||||
@@ -63,6 +64,9 @@ public:
|
||||
void setCanSubscribe( bool canSub );
|
||||
QString spotifyId() const { return m_spotifyId; }
|
||||
|
||||
virtual Tomahawk::PlaylistDeleteQuestions deleteQuestions() const;
|
||||
virtual void setQuestionResults( const QMap< int, bool > results );
|
||||
|
||||
void remove( bool askToDeletePlaylist = true );
|
||||
public slots:
|
||||
/// Spotify callbacks when we are directly instructed from the resolver
|
||||
@@ -76,15 +80,13 @@ public slots:
|
||||
void tomahawkTracksMoved( const QList<Tomahawk::plentry_ptr>& ,int );
|
||||
void tomahawkPlaylistRenamed( const QString&, const QString& );
|
||||
|
||||
void aboutToDelete();
|
||||
|
||||
private slots:
|
||||
// SpotifyResolver message handlers, all take msgtype, msg as argument
|
||||
void onTracksInsertedReturn( const QString& msgType, const QVariantMap& msg, const QVariant& extraData );
|
||||
void onTracksRemovedReturn( const QString& msgType, const QVariantMap& msg, const QVariant& extraData );
|
||||
void onTracksMovedReturn( const QString& msgType, const QVariantMap& msg, const QVariant& extraData );
|
||||
|
||||
void checkDeleteDialog() const;
|
||||
void unsyncOrDelete( bool toDelete );
|
||||
|
||||
void playlistRevisionLoaded();
|
||||
private:
|
||||
|
@@ -25,6 +25,7 @@
|
||||
|
||||
#include <QTimer>
|
||||
#include <QMutex>
|
||||
#include <QPair>
|
||||
|
||||
#ifndef ENABLE_HEADLESS
|
||||
#include <QPixmap>
|
||||
@@ -84,6 +85,11 @@ public:
|
||||
virtual bool subscribed() const { return false; }
|
||||
virtual void setSubscribed( bool ) {}
|
||||
|
||||
// The int data value associated with each question must be unique across *all* playlist updaters,
|
||||
// as setQuestionResults is called with all questions from all updaters.
|
||||
virtual PlaylistDeleteQuestions deleteQuestions() const { return PlaylistDeleteQuestions(); }
|
||||
virtual void setQuestionResults( const QMap< int, bool > results ) {}
|
||||
|
||||
signals:
|
||||
void changed();
|
||||
|
||||
|
@@ -18,22 +18,25 @@
|
||||
|
||||
#include "SourceTreePopupDialog.h"
|
||||
|
||||
#include "sourcetree/SourceTreeView.h"
|
||||
|
||||
#include <QPaintEvent>
|
||||
#include <QPainter>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QCheckBox>
|
||||
#include <QTimer>
|
||||
|
||||
#ifdef QT_MAC_USE_COCOA
|
||||
#include "SourceTreePopupDialog_mac.h"
|
||||
#endif
|
||||
|
||||
SourceTreePopupDialog::SourceTreePopupDialog( SourceTreeView* parent )
|
||||
using namespace Tomahawk;
|
||||
|
||||
SourceTreePopupDialog::SourceTreePopupDialog()
|
||||
: QWidget( 0 )
|
||||
, m_layout( 0 )
|
||||
, m_result( false )
|
||||
, m_label( 0 )
|
||||
, m_buttons( 0 )
|
||||
@@ -54,10 +57,12 @@ SourceTreePopupDialog::SourceTreePopupDialog( SourceTreeView* parent )
|
||||
connect( m_buttons, SIGNAL( accepted() ), this, SLOT( onAccepted() ) );
|
||||
connect( m_buttons, SIGNAL( rejected() ), this, SLOT( onRejected() ) );
|
||||
|
||||
setLayout( new QVBoxLayout );
|
||||
m_layout = new QVBoxLayout;
|
||||
setLayout( m_layout );
|
||||
|
||||
layout()->addWidget( m_label );
|
||||
layout()->addWidget( m_buttons );
|
||||
setContentsMargins( contentsMargins().left() + 2, contentsMargins().top(), contentsMargins().right(), contentsMargins().bottom() );
|
||||
|
||||
/*
|
||||
m_buttons->button( QDialogButtonBox::Ok )->setStyleSheet(
|
||||
@@ -95,6 +100,30 @@ SourceTreePopupDialog::setOkButtonText( const QString& text )
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SourceTreePopupDialog::setExtraQuestions( const Tomahawk::PlaylistDeleteQuestions& questions )
|
||||
{
|
||||
m_questions = questions;
|
||||
|
||||
int idx = m_layout->indexOf( m_label ) + 1;
|
||||
foreach ( const Tomahawk::PlaylistDeleteQuestion& question, m_questions )
|
||||
{
|
||||
QCheckBox* cb = new QCheckBox( question.first, this );
|
||||
cb->setLayoutDirection( Qt::RightToLeft );
|
||||
cb->setProperty( "data", question.second );
|
||||
|
||||
QHBoxLayout* h = new QHBoxLayout;
|
||||
h->addStretch( 1 );
|
||||
h->addWidget( cb );
|
||||
// m_layout->insertLayout( h, cb, 0 );
|
||||
m_layout->insertLayout( idx, h, 0 );
|
||||
|
||||
m_questionCheckboxes << cb;
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SourceTreePopupDialog::paintEvent( QPaintEvent* event )
|
||||
{
|
||||
@@ -163,6 +192,7 @@ SourceTreePopupDialog::onAccepted()
|
||||
{
|
||||
hide();
|
||||
m_result = true;
|
||||
calculateResults();
|
||||
emit result( m_result );
|
||||
}
|
||||
|
||||
@@ -172,5 +202,19 @@ SourceTreePopupDialog::onRejected()
|
||||
{
|
||||
hide();
|
||||
m_result = false;
|
||||
calculateResults();
|
||||
emit result( m_result );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SourceTreePopupDialog::calculateResults()
|
||||
{
|
||||
foreach ( const QCheckBox* b, m_questionCheckboxes )
|
||||
{
|
||||
if ( b->property( "data" ).toInt() != 0 )
|
||||
{
|
||||
m_questionResults[ b->property( "data" ).toInt() ] = ( b->checkState() == Qt::Checked );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -20,15 +20,18 @@
|
||||
#define SOURCETREE_POPUP_DIALOG
|
||||
|
||||
#include "DllMacro.h"
|
||||
#include "Typedefs.h"
|
||||
|
||||
#include <QWidget>
|
||||
#include <QMetaType>
|
||||
|
||||
class QVBoxLayout;
|
||||
class QShowEvent;
|
||||
class QLabel;
|
||||
class QDialogButtonBox;
|
||||
class QPushButton;
|
||||
class QFocusEvent;
|
||||
class SourceTreeView;
|
||||
class QCheckBox;
|
||||
|
||||
/**
|
||||
* Place me at offset() to the left of the right edge of the sourcetree.
|
||||
@@ -37,14 +40,16 @@ class DLLEXPORT SourceTreePopupDialog : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit SourceTreePopupDialog( SourceTreeView* parent );
|
||||
explicit SourceTreePopupDialog();
|
||||
|
||||
int offset() const { return 16; }
|
||||
|
||||
void setMainText( const QString& text );
|
||||
void setOkButtonText( const QString& text );
|
||||
void setExtraQuestions( const Tomahawk::PlaylistDeleteQuestions& questions );
|
||||
|
||||
bool resultValue() const { return m_result; }
|
||||
QMap< int, bool > questionResults() const { return m_questionResults; }
|
||||
|
||||
signals:
|
||||
void result( bool accepted );
|
||||
@@ -59,11 +64,22 @@ private slots:
|
||||
void onRejected();
|
||||
|
||||
private:
|
||||
void calculateResults();
|
||||
|
||||
QVBoxLayout* m_layout;
|
||||
QList< QCheckBox* > m_questionCheckboxes;
|
||||
|
||||
QString m_text;
|
||||
bool m_result;
|
||||
Tomahawk::PlaylistDeleteQuestions m_questions;
|
||||
QMap< int, bool > m_questionResults;
|
||||
|
||||
QLabel* m_label;
|
||||
QDialogButtonBox* m_buttons;
|
||||
};
|
||||
|
||||
|
||||
Q_DECLARE_METATYPE( SourceTreePopupDialog* )
|
||||
#endif
|
||||
|
||||
class QCheckBox;
|
||||
|
@@ -364,21 +364,31 @@ SourceTreeView::deletePlaylist( const QModelIndex& idxIn )
|
||||
Q_ASSERT( false );
|
||||
}
|
||||
|
||||
if ( m_popupDialog.isNull() )
|
||||
PlaylistItem* item = itemFromIndex< PlaylistItem >( idx );
|
||||
playlist_ptr playlist = item->playlist();
|
||||
|
||||
const QPoint rightCenter = viewport()->mapToGlobal( visualRect( idx ).topRight() + QPoint( 0, visualRect( idx ).height() / 2 ) );
|
||||
if ( playlist->hasCustomDeleter() )
|
||||
{
|
||||
m_popupDialog = QWeakPointer< SourceTreePopupDialog >( new SourceTreePopupDialog( this ) );
|
||||
connect( m_popupDialog.data(), SIGNAL( result( bool ) ), this, SLOT( onDeletePlaylistResult( bool ) ) );
|
||||
playlist->customDelete( rightCenter );
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( m_popupDialog.isNull() )
|
||||
{
|
||||
m_popupDialog = QWeakPointer< SourceTreePopupDialog >( new SourceTreePopupDialog() );
|
||||
connect( m_popupDialog.data(), SIGNAL( result( bool ) ), this, SLOT( onDeletePlaylistResult( bool ) ) );
|
||||
}
|
||||
|
||||
m_popupDialog.data()->setMainText( tr( "Would you like to delete the %1 <b>\"%2\"</b>?", "e.g. Would you like to delete the playlist named Foobar?" )
|
||||
.arg( typeDesc ).arg( idx.data().toString() ) );
|
||||
m_popupDialog.data()->setOkButtonText( tr( "Delete" ) );
|
||||
m_popupDialog.data()->setProperty( "idx", QVariant::fromValue< QModelIndex >( idx ) );
|
||||
|
||||
m_popupDialog.data()->move( rightCenter.x() - m_popupDialog.data()->offset(), rightCenter.y() - m_popupDialog.data()->sizeHint().height() / 2. );
|
||||
m_popupDialog.data()->show();
|
||||
}
|
||||
|
||||
m_popupDialog.data()->setMainText( tr( "Would you like to delete the %1 <b>\"%2\"</b>?", "e.g. Would you like to delete the playlist named Foobar?" )
|
||||
.arg( typeDesc ).arg( idx.data().toString() ) );
|
||||
m_popupDialog.data()->setOkButtonText( tr( "Delete" ) );
|
||||
m_popupDialog.data()->setProperty( "idx", QVariant::fromValue< QModelIndex >( idx ) );
|
||||
|
||||
qDebug() << "POPUP HAS HEIGHT:" << m_popupDialog.data()->sizeHint().height();
|
||||
const QPoint rightCenter = viewport()->mapToGlobal( visualRect( idx ).topRight() + QPoint( 0, visualRect( idx ).height() / 2 ) );
|
||||
m_popupDialog.data()->move( rightCenter.x() - m_popupDialog.data()->offset(), rightCenter.y() - m_popupDialog.data()->sizeHint().height() / 2. );
|
||||
m_popupDialog.data()->show();
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user