1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-06 14:16:32 +02:00

Add an Install From File and Remove Account option

This commit is contained in:
Leo Franchi
2012-02-13 18:03:30 -05:00
parent 8ceb537860
commit 3687ebdc6b
7 changed files with 97 additions and 15 deletions

View File

@@ -172,6 +172,8 @@ AccountDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option,
// Draw individual accounts and add account button for factories // Draw individual accounts and add account button for factories
m_cachedButtonRects[ index ] = QRect(); m_cachedButtonRects[ index ] = QRect();
bool canDelete = index.data( AccountModel::CanDeleteRole ) .toBool();
if ( rowType == Tomahawk::Accounts::AccountModel::TopLevelFactory ) if ( rowType == Tomahawk::Accounts::AccountModel::TopLevelFactory )
{ {
const QList< Account* > accts = index.data( AccountModel::ChildrenOfFactoryRole ).value< QList< Tomahawk::Accounts::Account* > >(); const QList< Account* > accts = index.data( AccountModel::ChildrenOfFactoryRole ).value< QList< Tomahawk::Accounts::Account* > >();
@@ -224,6 +226,29 @@ AccountDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option,
} }
} }
else if ( canDelete )
{
const QString btnText = tr( "Remove Account" );
const int btnWidth = installMetrics.width( btnText ) + 2*PADDING;
QRect btnRect;
if ( hasConfigWrench )
btnRect = QRect( opt.rect.right() - PADDING - btnWidth, opt.rect.bottom() - installMetrics.height() - 3*PADDING, btnWidth, installMetrics.height() + 2*PADDING );
else
btnRect = QRect( opt.rect.right() - PADDING - btnWidth, center - ( installMetrics.height() + 4 ) / 2, btnWidth, installMetrics.height() + 2*PADDING );
leftEdge = btnRect.left();
m_cachedButtonRects[ index ] = btnRect;
painter->save();
painter->setPen( opt.palette.color( QPalette::Active, QPalette::AlternateBase ) );
drawRoundedButton( painter, btnRect, true );
painter->setFont( installFont );
painter->drawText( btnRect, Qt::AlignCenter, btnText );
painter->restore();
}
// Draw the title and description // Draw the title and description
// title // title
@@ -431,7 +456,7 @@ AccountDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const QS
else if ( m_cachedButtonRects.contains( index ) && m_cachedButtonRects[ index ].contains( me->pos() ) ) else if ( m_cachedButtonRects.contains( index ) && m_cachedButtonRects[ index ].contains( me->pos() ) )
{ {
// Install/create/etc button for this row // Install/create/etc button for this row
model->setData( index, true, AccountModel::AddAccountButtonRole ); model->setData( index, true, AccountModel::CustomButtonRole );
} }
} }
@@ -475,7 +500,7 @@ AccountDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const QS
void void
AccountDelegate::drawRoundedButton( QPainter* painter, const QRect& btnRect ) const AccountDelegate::drawRoundedButton( QPainter* painter, const QRect& btnRect, bool red ) const
{ {
QPainterPath btnPath; QPainterPath btnPath;
const int radius = 3; const int radius = 3;
@@ -490,8 +515,16 @@ AccountDelegate::drawRoundedButton( QPainter* painter, const QRect& btnRect ) co
btnPath.lineTo( btnRect.left(), btnCenter ); btnPath.lineTo( btnRect.left(), btnCenter );
QLinearGradient g; QLinearGradient g;
g.setColorAt( 0, QColor(54, 127, 211) ); if ( !red )
g.setColorAt( 0.5, QColor(43, 104, 182) ); {
g.setColorAt( 0, QColor(54, 127, 211) );
g.setColorAt( 0.5, QColor(43, 104, 182) );
}
else
{
g.setColorAt( 0, QColor(206, 63, 63) );
g.setColorAt( 0.5, QColor(170, 52, 52) );
}
//painter->setPen( bg.darker() ); //painter->setPen( bg.darker() );
painter->fillPath( btnPath, g ); painter->fillPath( btnPath, g );
//painter->drawPath( btnPath ); //painter->drawPath( btnPath );
@@ -505,8 +538,16 @@ AccountDelegate::drawRoundedButton( QPainter* painter, const QRect& btnRect ) co
btnPath.lineTo( btnRect.right(), btnCenter ); btnPath.lineTo( btnRect.right(), btnCenter );
btnPath.lineTo( btnRect.left(), btnCenter ); btnPath.lineTo( btnRect.left(), btnCenter );
g.setColorAt( 0, QColor(34, 85, 159) ); if ( !red )
g.setColorAt( 0.5, QColor(35, 79, 147) ); {
g.setColorAt( 0, QColor(34, 85, 159) );
g.setColorAt( 0.5, QColor(35, 79, 147) );
}
else
{
g.setColorAt( 0, QColor(150, 50, 50) );
g.setColorAt( 0.5, QColor(130, 40, 40) );
}
painter->fillPath( btnPath, g ); painter->fillPath( btnPath, g );
} }

View File

@@ -47,7 +47,7 @@ signals:
void openConfig( Tomahawk::Accounts::AccountFactory* ); void openConfig( Tomahawk::Accounts::AccountFactory* );
private: private:
void drawRoundedButton( QPainter* painter, const QRect& buttonRect ) const; void drawRoundedButton( QPainter* painter, const QRect& buttonRect, bool red = false ) const;
// Returns new left edge // Returns new left edge
int drawStatus( QPainter* painter, const QPointF& rightTopEdge, Account* acct, bool drawText = false ) const; int drawStatus( QPainter* painter, const QPointF& rightTopEdge, Account* acct, bool drawText = false ) const;
void drawCheckBox( QStyleOptionViewItemV4& opt, QPainter* p, const QWidget* w ) const; void drawCheckBox( QStyleOptionViewItemV4& opt, QPainter* p, const QWidget* w ) const;

View File

@@ -207,6 +207,10 @@ AccountModel::data( const QModelIndex& index, int role ) const
else else
return UniqueFactory; return UniqueFactory;
} }
else if ( role == CanDeleteRole )
{
return node->type == AccountModelNode::ManualResolverType;
}
Account* acct = 0; Account* acct = 0;
if ( node->type == AccountModelNode::ManualResolverType ) if ( node->type == AccountModelNode::ManualResolverType )
@@ -349,12 +353,23 @@ AccountModel::setData( const QModelIndex& index, const QVariant& value, int role
} }
// The install/create/remove/etc button was clicked. Handle it properly depending on this item // The install/create/remove/etc button was clicked. Handle it properly depending on this item
if ( role == AddAccountButtonRole ) if ( role == CustomButtonRole )
{ {
Q_ASSERT( node->type == AccountModelNode::FactoryType ); if ( node->type == AccountModelNode::FactoryType )
// Make a new account of this factory type {
emit createAccount( node->factory ); // Make a new account of this factory type
return true; emit createAccount( node->factory );
return true;
}
else if ( node->type == AccountModelNode::ManualResolverType )
{
Q_ASSERT( node->resolverAccount );
AccountManager::instance()->removeAccount( node->resolverAccount );
return true;
}
Q_ASSERT( false ); // Should not be here, only the above two types should have this button
return false;
} }

View File

@@ -58,9 +58,10 @@ public:
AccountData = Qt::UserRole + 28, // raw plugin AccountData = Qt::UserRole + 28, // raw plugin
CanRateRole = Qt::UserRole + 32, CanRateRole = Qt::UserRole + 32,
AccountTypeRole = Qt::UserRole + 33, AccountTypeRole = Qt::UserRole + 33,
CanDeleteRole = Qt::UserRole + 34,
CheckboxClickedRole = Qt::UserRole + 29, // the checkbox for this row was toggled CheckboxClickedRole = Qt::UserRole + 29, // the checkbox for this row was toggled
AddAccountButtonRole = Qt::UserRole + 30, // the add account button CustomButtonRole = Qt::UserRole + 30, // the add account or remove account button
// Used by factories // Used by factories
ChildrenOfFactoryRole = Qt::UserRole + 31 ChildrenOfFactoryRole = Qt::UserRole + 31

View File

@@ -20,6 +20,7 @@
#define RESOLVERACCOUNT_H #define RESOLVERACCOUNT_H
#include "accounts/Account.h" #include "accounts/Account.h"
#include "dllmacro.h"
namespace Tomahawk { namespace Tomahawk {
@@ -27,7 +28,7 @@ class ExternalResolverGui;
namespace Accounts { namespace Accounts {
class ResolverAccountFactory : public AccountFactory class DLLEXPORT ResolverAccountFactory : public AccountFactory
{ {
Q_OBJECT Q_OBJECT
public: public:
@@ -51,7 +52,7 @@ public:
* *
* Contains the resolver* that is it wrapping * Contains the resolver* that is it wrapping
*/ */
class ResolverAccount : public Account class DLLEXPORT ResolverAccount : public Account
{ {
Q_OBJECT Q_OBJECT
public: public:

View File

@@ -53,6 +53,7 @@
#include "accounts/Account.h" #include "accounts/Account.h"
#include "accounts/AccountManager.h" #include "accounts/AccountManager.h"
#include <accounts/AccountModelFilterProxy.h> #include <accounts/AccountModelFilterProxy.h>
#include <accounts/ResolverAccount.h>
#include "utils/logger.h" #include "utils/logger.h"
#include "AccountFactoryWrapper.h" #include "AccountFactoryWrapper.h"
@@ -119,7 +120,9 @@ SettingsDialog::SettingsDialog( QWidget *parent )
ui->accountsView->setModel( m_accountProxy ); ui->accountsView->setModel( m_accountProxy );
connect( ui->installFromFileBtn, SIGNAL( clicked( bool ) ), this, SLOT( installFromFile() ) );
connect( m_accountModel, SIGNAL( createAccount( Tomahawk::Accounts::AccountFactory* ) ), this, SLOT( createAccountFromFactory( Tomahawk::Accounts::AccountFactory* ) ) ); connect( m_accountModel, SIGNAL( createAccount( Tomahawk::Accounts::AccountFactory* ) ), this, SLOT( createAccountFromFactory( Tomahawk::Accounts::AccountFactory* ) ) );
connect( AccountManager::instance(), SIGNAL( added( Tomahawk::Accounts::Account* ) ), ui->accountsView, SLOT( scrollToBottom() ) );
ui->accountsFilterCombo->addItem( tr( "All" ), Accounts::NoType ); ui->accountsFilterCombo->addItem( tr( "All" ), Accounts::NoType );
ui->accountsFilterCombo->addItem( accountTypeToString( SipType ), SipType ); ui->accountsFilterCombo->addItem( accountTypeToString( SipType ), SipType );
@@ -606,6 +609,25 @@ SettingsDialog::handleAccountAdded( Account* account, bool added )
} }
void
SettingsDialog::installFromFile()
{
const QString resolver = QFileDialog::getOpenFileName( this, tr( "Install resolver from file" ), TomahawkSettings::instance()->scriptDefaultPath() );
if( !resolver.isEmpty() )
{
Account* acct = ResolverAccountFactory::createFromPath( resolver, false );
AccountManager::instance()->addAccount( acct );
TomahawkSettings::instance()->addAccount( acct->accountId() );
AccountManager::instance()->enableAccount( acct );
QFileInfo resolverAbsoluteFilePath( resolver );
TomahawkSettings::instance()->setScriptDefaultPath( resolverAbsoluteFilePath.absolutePath() );
}
}
void void
SettingsDialog::requiresRestart() SettingsDialog::requiresRestart()
{ {

View File

@@ -99,6 +99,8 @@ private slots:
void accountConfigDelete(); void accountConfigDelete();
void accountCreateConfigClosed( int value ); void accountCreateConfigClosed( int value );
void installFromFile();
void updateScanOptionsView(); void updateScanOptionsView();
void changePage( QListWidgetItem*, QListWidgetItem* ); void changePage( QListWidgetItem*, QListWidgetItem* );