1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2012 Teo Mrnjavac <teo@kde.org>
5
* Tomahawk is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* Tomahawk is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
19
#include "AccountListWidget.h"
21
#include "accounts/AccountModel.h"
22
#include "accounts/AccountManager.h"
23
#include "AccountWidget.h"
24
#include "utils/ImageRegistry.h"
25
#include "utils/TomahawkUtilsGui.h"
26
#include "utils/Logger.h"
28
#include <QPushButton>
31
AccountListWidget::AccountListWidget( AccountModelFactoryProxy* model, QWidget* parent )
35
QVBoxLayout* mainLayout = new QVBoxLayout( this );
36
TomahawkUtils::unmarginLayout( mainLayout );
37
m_layout = new QVBoxLayout;
38
TomahawkUtils::unmarginLayout( m_layout );
39
mainLayout->addLayout( m_layout );
40
mainLayout->setSpacing( 8 );
42
connect( m_model, SIGNAL( dataChanged( QModelIndex, QModelIndex ) ),
43
this, SLOT( updateEntries( QModelIndex, QModelIndex ) ) );
44
connect( m_model, SIGNAL( rowsInserted ( QModelIndex, int, int ) ),
45
this, SLOT( insertEntries( QModelIndex, int, int ) ) );
46
connect( m_model, SIGNAL( rowsRemoved( QModelIndex, int, int ) ),
47
this, SLOT( removeEntries( QModelIndex, int, int ) ) );
48
connect( m_model, SIGNAL( modelReset() ),
49
this, SLOT( loadAllEntries() ) );
51
connect( m_model, SIGNAL( dataChanged( QModelIndex, QModelIndex ) ),
52
this, SLOT( updateToggleOnlineStateButton() ) );
54
QWidget* separatorLine = new QWidget( this );
55
separatorLine->setFixedHeight( 1 );
56
separatorLine->setContentsMargins( 0, 0, 0, 0 );
57
separatorLine->setStyleSheet( "QWidget { border-top: 1px solid " +
58
TomahawkUtils::Colors::BORDER_LINE.name() + "; }" ); //from ProxyStyle
59
mainLayout->insertWidget( 0, separatorLine );
60
mainLayout->addSpacing( 6 );
62
QLabel *connectionsLabel = new QLabel( tr( "Connections" ).toUpper(), this );
63
QFont clFont = connectionsLabel->font();
64
clFont.setBold( true );
65
connectionsLabel->setStyleSheet( "color: " + TomahawkUtils::Colors::GROUP_HEADER.name() );
66
clFont.setPointSize( TomahawkUtils::defaultFontSize() + 1 );
67
connectionsLabel->setFont( clFont );
68
connectionsLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
70
m_toggleOnlineButton = new QPushButton( tr( "Connect &All" ), this );
71
m_toggleOnlineButtonState = false;
72
connect( m_toggleOnlineButton, SIGNAL( clicked() ),
73
this, SLOT( toggleOnlineStateForAll() ) );
75
QHBoxLayout *headerLayout = new QHBoxLayout( this );
76
headerLayout->addWidget( connectionsLabel );
77
headerLayout->addSpacing( 30 );
78
headerLayout->addWidget( m_toggleOnlineButton );
79
mainLayout->insertLayout( 0, headerLayout );
81
updateToggleOnlineStateButton();
85
AccountListWidget::updateEntries( const QModelIndex& topLeft, const QModelIndex& bottomRight )
87
for( int row = topLeft.row(); row <= bottomRight.row(); ++row )
89
QPersistentModelIndex idx( m_model->index( row, 0 ) );
91
int newCount = idx.data( Tomahawk::Accounts::AccountModel::ChildrenOfFactoryRole )
92
.value< QList< Tomahawk::Accounts::Account* > >().count();
94
if( m_entries.value( idx ).count() == newCount )
100
removeEntries( idx.parent(), idx.row(), idx.row() );
101
insertEntries( idx.parent(), idx.row(), idx.row() );
107
AccountListWidget::updateEntry( const QPersistentModelIndex& idx )
109
for ( int i = 0; i < m_entries.value( idx ).count(); ++i )
111
m_entries[ idx ][ i ]->update( idx, i ); //update the i-th account of the idx-th factory
116
AccountListWidget::loadAllEntries()
118
foreach ( QList< AccountWidget* > entry, m_entries )
120
foreach ( AccountWidget* w, entry )
122
m_layout->removeWidget( w );
129
int rc = m_model->rowCount();
130
insertEntries( QModelIndex(), 0, rc - 1 );
134
AccountListWidget::insertEntries( const QModelIndex& parent, int start, int end )
136
for ( int i = start; i <= end; ++i )
138
QPersistentModelIndex idx( m_model->index( i, 0, parent ) );
139
int count = idx.data( Tomahawk::Accounts::AccountModel::ChildrenOfFactoryRole )
140
.value< QList< Tomahawk::Accounts::Account* > >().count();
141
QList< AccountWidget* > entryAccounts;
142
for ( int j = 0; j < count; ++j )
144
AccountWidget *entry = new AccountWidget( this );
145
entryAccounts.append( entry );
147
m_entries.insert( idx, entryAccounts );
148
for ( int j = 0; j < entryAccounts.length(); ++j )
149
m_layout->insertWidget( i+j, entryAccounts.at( j ) );
153
for ( int j = 0; j < entryAccounts.length(); ++j )
155
entryAccounts[ j ]->setupConnections( idx, j );
161
AccountListWidget::removeEntries( const QModelIndex& parent, int start, int end )
163
for ( int i = start; i <= end; ++i )
165
QPersistentModelIndex idx( m_model->index( i, 0, parent ) );
166
if ( !idx.isValid() ) //means we just removed the last account for a factory
168
for ( QHash< QPersistentModelIndex, QList< AccountWidget* > >::iterator it = m_entries.begin();
169
it != m_entries.end(); ++it )
171
if ( !it.key().isValid() )
178
QList< AccountWidget* > &entryAccounts = m_entries[ idx ];
179
for ( int j = 0; j < entryAccounts.count(); ++j )
181
AccountWidget *a = entryAccounts.at( j );
182
m_layout->removeWidget( a );
185
m_entries.remove( idx );
188
qobject_cast< QWidget* >( QWidget::parent() )->adjustSize();
192
AccountListWidget::toggleOnlineStateForAll()
194
bool newState = !m_toggleOnlineButtonState;
195
foreach ( QList< AccountWidget* > awgts, m_entries )
197
foreach ( AccountWidget* awgt, awgts )
199
awgt->setConnectionState( newState );
205
AccountListWidget::updateToggleOnlineStateButton()
207
bool newState = false;
208
foreach ( QList< AccountWidget* > awgts, m_entries )
210
foreach ( AccountWidget* awgt, awgts )
212
if ( awgt->connectionState() )
215
goto end; //break 2 levels
221
if ( newState != m_toggleOnlineButtonState )
223
m_toggleOnlineButtonState = newState;
225
Tomahawk::Accounts::AccountManager::instance()->connectAll();
227
Tomahawk::Accounts::AccountManager::instance()->disconnectAll();
230
m_toggleOnlineButton->setText( m_toggleOnlineButtonState ? tr( "Disconnect &All" )
231
: tr( "Connect &All" ) );
232
m_toggleOnlineButton->setIcon( m_toggleOnlineButtonState ?
233
ImageRegistry::instance()->icon( RESPATH "images/account-online.svg" ) :
234
ImageRegistry::instance()->icon( RESPATH "images/account-offline.svg" ) );