mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-13 17:43:59 +02:00
TWK-744: Add individual account toggles in factory wrapper delegate
This commit is contained in:
@@ -45,7 +45,7 @@ AccountFactoryWrapper::AccountFactoryWrapper( AccountFactory* factory, QWidget*
|
||||
|
||||
connect( del, SIGNAL( openConfig( Tomahawk::Accounts::Account* ) ), this, SLOT( openAccountConfig( Tomahawk::Accounts::Account* ) ) );
|
||||
connect( del, SIGNAL( removeAccount( Tomahawk::Accounts::Account* ) ), this, SLOT( removeAccount( Tomahawk::Accounts::Account* ) ) );
|
||||
|
||||
connect( del, SIGNAL( checkOrUncheck( QModelIndex, Tomahawk::Accounts::Account* , Qt::CheckState ) ), this, SLOT( accountCheckedOrUnchecked( QModelIndex ,Tomahawk::Accounts::Account* ,Qt::CheckState ) ) );
|
||||
load();
|
||||
|
||||
connect( m_ui->buttonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );
|
||||
@@ -72,6 +72,7 @@ AccountFactoryWrapper::load()
|
||||
{
|
||||
QTreeWidgetItem* item = new QTreeWidgetItem( m_ui->accountsList );
|
||||
item->setData( 0, AccountRole, QVariant::fromValue< QObject *>( acc ) );
|
||||
item->setCheckState( 0, acc->enabled() ? Qt::Checked : Qt::Unchecked );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +105,25 @@ AccountFactoryWrapper::removeAccount( Tomahawk::Accounts::Account* acct )
|
||||
load();
|
||||
}
|
||||
|
||||
void
|
||||
AccountFactoryWrapper::accountCheckedOrUnchecked( const QModelIndex& index, Account* acct, Qt::CheckState newstate )
|
||||
{
|
||||
QTreeWidgetItem* item = m_ui->accountsList->topLevelItem( index.row() );
|
||||
Q_ASSERT( item );
|
||||
|
||||
if ( newstate == Qt::Checked )
|
||||
{
|
||||
item->setCheckState( 0, Qt::Checked );
|
||||
AccountManager::instance()->enableAccount( acct );
|
||||
}
|
||||
else if ( newstate == Qt::Unchecked )
|
||||
{
|
||||
item->setCheckState( 0, Qt::Unchecked );
|
||||
AccountManager::instance()->disableAccount( acct );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
AccountFactoryWrapper::buttonClicked( QAbstractButton* button )
|
||||
{
|
||||
|
@@ -20,6 +20,7 @@
|
||||
#define ACCOUNTFACTORYWRAPPER_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QModelIndex>
|
||||
|
||||
class QAbstractButton;
|
||||
namespace Tomahawk {
|
||||
@@ -45,6 +46,7 @@ public:
|
||||
public slots:
|
||||
void openAccountConfig( Tomahawk::Accounts::Account* );
|
||||
void removeAccount( Tomahawk::Accounts::Account* );
|
||||
void accountCheckedOrUnchecked( const QModelIndex& , Tomahawk::Accounts::Account* , Qt::CheckState );
|
||||
|
||||
private slots:
|
||||
void buttonClicked( QAbstractButton* );
|
||||
|
@@ -62,8 +62,16 @@ AccountFactoryWrapperDelegate::paint(QPainter* painter, const QStyleOptionViewIt
|
||||
Account* acc = qobject_cast< Account* >( index.data( AccountFactoryWrapper::AccountRole ).value< QObject* >() );
|
||||
Q_ASSERT( acc );
|
||||
|
||||
// Checkbox on left edge, then text
|
||||
const QRect checkRect( PADDING/4, PADDING/4 + opt.rect.top(), opt.rect.height() - PADDING/4, opt.rect.height() - PADDING/4 );
|
||||
m_cachedCheckRects[ index ] = checkRect;
|
||||
QStyleOptionViewItemV4 opt2 = opt;
|
||||
opt2.rect = checkRect;
|
||||
opt.checkState == Qt::Checked ? opt2.state |= QStyle::State_On : opt2.state |= QStyle::State_Off;
|
||||
style->drawPrimitive( QStyle::PE_IndicatorViewItemCheck, &opt2, painter, w );
|
||||
|
||||
// name on left
|
||||
painter->drawText( opt.rect.adjusted( PADDING, PADDING, -PADDING, -PADDING ), Qt::AlignLeft | Qt::AlignVCenter, acc->accountFriendlyName() );
|
||||
painter->drawText( opt.rect.adjusted( checkRect.right() + PADDING, PADDING, -PADDING, -PADDING ), Qt::AlignLeft | Qt::AlignVCenter, acc->accountFriendlyName() );
|
||||
|
||||
// remove, config, status on right
|
||||
const QRect pmRect( opt.rect.right() - PADDING - m_removePixmap.width(), topIcon, ICON_SIZE, ICON_SIZE );
|
||||
@@ -152,10 +160,22 @@ AccountFactoryWrapperDelegate::editorEvent( QEvent* event, QAbstractItemModel*,
|
||||
emit update( m_configPressed );
|
||||
|
||||
m_configPressed = QModelIndex();
|
||||
Account* acct = qobject_cast< Account* >( index.data( AccountFactoryWrapper::AccountRole ).value< QObject* >() );
|
||||
|
||||
if ( m_cachedCheckRects.contains( index ) && m_cachedCheckRects[ index ].contains( me->pos() ) )
|
||||
{
|
||||
// Check box for this row
|
||||
// eat the double click events inside the check rect
|
||||
if( event->type() == QEvent::MouseButtonDblClick ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Qt::CheckState curState = static_cast< Qt::CheckState >( index.data( Qt::CheckStateRole ).toInt() );
|
||||
Qt::CheckState newState = curState == Qt::Checked ? Qt::Unchecked : Qt::Checked;
|
||||
emit checkOrUncheck( index, acct, newState );
|
||||
}
|
||||
if ( m_cachedButtonRects.contains( index ) && m_cachedButtonRects[ index ].contains( me->pos() ) )
|
||||
{
|
||||
Account* acct = qobject_cast< Account* >( index.data( AccountFactoryWrapper::AccountRole ).value< QObject* >() );
|
||||
emit removeAccount( acct );
|
||||
|
||||
return true;
|
||||
|
@@ -46,11 +46,14 @@ signals:
|
||||
void openConfig( Tomahawk::Accounts::Account* );
|
||||
void removeAccount( Tomahawk::Accounts::Account* );
|
||||
|
||||
void checkOrUncheck( const QModelIndex& row, Tomahawk::Accounts::Account* account, Qt::CheckState newState );
|
||||
|
||||
private:
|
||||
QPixmap m_removePixmap, m_offlineIcon, m_onlineIcon;
|
||||
QIcon m_configIcon;
|
||||
QModelIndex m_configPressed;
|
||||
|
||||
mutable QHash< QPersistentModelIndex, QRect > m_cachedCheckRects;
|
||||
mutable QHash< QPersistentModelIndex, QRect > m_cachedButtonRects;
|
||||
mutable QHash< QPersistentModelIndex, QRect > m_cachedConfigRects;
|
||||
};
|
||||
|
Reference in New Issue
Block a user