[Amarok] d790a58 Filter providers with QSortFilterProxyModel.
Bart Cerneels
bart.cerneels at kde.org
Thu Mar 11 21:42:06 CET 2010
commit d790a588ecbf30a8399852c3eaef6d2db22783e3
Author: Bart Cerneels <bart.cerneels at kde.org>
Date: Wed Mar 10 09:13:57 2010 +0100
Filter providers with QSortFilterProxyModel.
Usability RFC:
It's currently possible to uncheck all providers.
Because of QSFPM this will end up showing all providers anyway.
We can prevent this by disabling the last checkbutton.
Or let the user know what happend in an inline infobox.
Or.... better ideas you cna come up with.
CCMAIL:amarok-devel at kde.org
diff --git a/src/browsers/playlistbrowser/PlaylistCategory.cpp b/src/browsers/playlistbrowser/PlaylistCategory.cpp
index e43c9ca..12b943a 100644
--- a/src/browsers/playlistbrowser/PlaylistCategory.cpp
+++ b/src/browsers/playlistbrowser/PlaylistCategory.cpp
@@ -18,6 +18,7 @@
#include "PlaylistCategory.h"
#include "CollectionManager.h"
+#include "Debug.h"
#include "PaletteHandler.h"
#include "playlist/PlaylistModel.h"
#include "PlaylistsInGroupsProxy.h"
@@ -28,9 +29,13 @@
#include "UserPlaylistModel.h"
#include <KAction>
+#include <KActionMenu>
+#include <KButtonGroup>
#include <KIcon>
#include <KLineEdit>
+#include <QButtonGroup>
+#include <QCheckBox>
#include <QGridLayout>
#include <QHeaderView>
#include <QLabel>
@@ -72,6 +77,12 @@ PlaylistCategory::PlaylistCategory( QWidget * parent )
m_byFolderProxy = new PlaylistsInGroupsProxy( The::userPlaylistModel() );
m_defaultItemView = m_playlistView->itemDelegate();
+ m_filterProxy = new QSortFilterProxyModel( this );
+ m_filterProxy->setDynamicSortFilter( true );
+ m_filterProxy->setFilterKeyColumn( PlaylistBrowserNS::UserModel::ProviderColumn );
+
+ m_playlistView->setModel( m_filterProxy );
+
m_addGroupAction = new KAction( KIcon( "folder-new" ), i18n( "Add Folder" ), this );
toolBar->addAction( m_addGroupAction );
connect( m_addGroupAction, SIGNAL( triggered( bool ) ),
@@ -85,6 +96,10 @@ PlaylistCategory::PlaylistCategory( QWidget * parent )
QSizePolicy::MinimumExpanding );
toolBar->addWidget( spacerWidget );
+ m_providerMenu = new KActionMenu( KIcon( "view-list-tree" ), i18n( "Visible Sources"), this );
+ m_providerMenu->setDelayed( false );
+ toolBar->addAction( m_providerMenu );
+
KAction *toggleAction = new KAction( KIcon( "view-list-tree" ), QString(), toolBar );
toggleAction->setToolTip( i18n( "Merged View" ) );
toggleAction->setCheckable( true );
@@ -111,7 +126,11 @@ PlaylistCategory::PlaylistCategory( QWidget * parent )
m_playlistView->setAlternatingRowColors( true );
- new PlaylistTreeItemDelegate( m_playlistView );
+ foreach( const PlaylistProvider *provider,
+ The::playlistManager()->providersForCategory( PlaylistManager::UserPlaylist ) )
+ {
+ createProviderButton( provider );
+ }
}
PlaylistCategory::~PlaylistCategory()
@@ -130,13 +149,13 @@ PlaylistCategory::toggleView( bool merged )
{
if( merged )
{
- m_playlistView->setModel( m_byFolderProxy );
+ m_filterProxy->setSourceModel( m_byFolderProxy );
m_playlistView->setItemDelegate( m_defaultItemView );
m_playlistView->setRootIsDecorated( true );
}
else
{
- m_playlistView->setModel( m_byProviderProxy );
+ m_filterProxy->setSourceModel( m_byProviderProxy );
m_playlistView->setItemDelegate( m_byProviderDelegate );
m_playlistView->setRootIsDecorated( false );
}
@@ -148,4 +167,55 @@ PlaylistCategory::toggleView( bool merged )
Amarok::config( s_configGroup ).writeEntry( s_mergeViewKey, merged );
}
+void
+PlaylistCategory::slotProviderAdded( PlaylistProvider *provider, int category )
+{
+ if( !m_providerActions.keys().contains( provider ) )
+ createProviderButton( provider );
+}
+
+void
+PlaylistCategory::slotProviderRemoved( PlaylistProvider *provider, int category )
+{
+ if( m_providerActions.keys().contains( provider ) )
+ {
+ QAction *providerToggle = m_providerActions.take( provider );
+ m_providerMenu->removeAction( providerToggle );
+ }
+}
+
+void
+PlaylistCategory::createProviderButton( const PlaylistProvider *provider )
+{
+ QAction *providerToggle = new QAction( provider->icon(), provider->prettyName(), this );
+ providerToggle->setCheckable( true );
+ providerToggle->setChecked( true );
+ providerToggle->setData( QVariant::fromValue( provider ) );
+ connect( providerToggle, SIGNAL(toggled(bool)), SLOT(slotToggleProviderButton(bool)) );
+ m_providerMenu->addAction( providerToggle );
+ m_providerActions.insert( provider, providerToggle );
+}
+
+void
+PlaylistCategory::slotToggleProviderButton( bool enabled )
+{
+ DEBUG_BLOCK
+ QAction *action = qobject_cast<QAction *>( QObject::sender() );
+ const PlaylistProvider *provider = action->data().value<const PlaylistProvider *>();
+ if( !m_providerActions.keys().contains( provider ) )
+ return;
+
+ QString filter;
+ foreach( const PlaylistProvider *p, m_providerActions.keys() )
+ {
+ QAction *action = m_providerActions.value( p );
+ if( action->isChecked() )
+ {
+ QString escapedName = QRegExp::escape( p->prettyName() ).replace( " ", "\\ " );
+ filter += QString( filter.isEmpty() ? "^%1" : "|^%1" ).arg( escapedName );
+ }
+ }
+ m_filterProxy->setFilterRegExp( filter );
+}
+
#include "PlaylistCategory.moc"
diff --git a/src/browsers/playlistbrowser/PlaylistCategory.h b/src/browsers/playlistbrowser/PlaylistCategory.h
index dd59826..aa20282 100644
--- a/src/browsers/playlistbrowser/PlaylistCategory.h
+++ b/src/browsers/playlistbrowser/PlaylistCategory.h
@@ -25,12 +25,16 @@
#include <QModelIndex>
#include <QPoint>
+#include <QSortFilterProxyModel>
class QTreeView;
class KAction;
+class KActionMenu;
class KLineEdit;
+class PlaylistProvider;
+
class PlaylistsInGroupsProxy;
class PlaylistsByProviderProxy;
class PlaylistTreeItemDelegate;
@@ -54,21 +58,32 @@ public:
~PlaylistCategory();
private slots:
- void newPalette( const QPalette & palette );
+ void newPalette( const QPalette &palette );
void toggleView( bool );
+ void slotProviderAdded( PlaylistProvider *provider, int category );
+ void slotProviderRemoved( PlaylistProvider *provider, int category );
+ void slotToggleProviderButton( bool enabled );
private:
- UserPlaylistTreeView * m_playlistView;
+ void createProviderButton( const PlaylistProvider *provider );
+
+ UserPlaylistTreeView *m_playlistView;
- KAction * m_addGroupAction;
+ KAction *m_addGroupAction;
+ KActionMenu *m_providerMenu;
+ QMap<const PlaylistProvider *, QAction *> m_providerActions;
PlaylistTreeItemDelegate *m_byProviderDelegate;
QAbstractItemDelegate *m_defaultItemView;
PlaylistsInGroupsProxy *m_byFolderProxy;
PlaylistsByProviderProxy *m_byProviderProxy;
+ QSortFilterProxyModel *m_filterProxy;
};
}
+//for saving it in a QVariant
+Q_DECLARE_METATYPE( const PlaylistProvider * );
+
#endif
More information about the Amarok-devel
mailing list