~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/widgets/AccountListWidget.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2012 Teo Mrnjavac <teo@kde.org>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
#include "AccountListWidget.h"
 
20
 
 
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"
 
27
 
 
28
#include <QPushButton>
 
29
 
 
30
 
 
31
AccountListWidget::AccountListWidget( AccountModelFactoryProxy* model, QWidget* parent )
 
32
    : QWidget( parent )
 
33
    , m_model( model )
 
34
{
 
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 );
 
41
 
 
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() ) );
 
50
 
 
51
    connect( m_model, SIGNAL( dataChanged( QModelIndex, QModelIndex ) ),
 
52
             this, SLOT( updateToggleOnlineStateButton() ) );
 
53
 
 
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 );
 
61
 
 
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 );
 
69
 
 
70
    m_toggleOnlineButton = new QPushButton( tr( "Connect &All" ), this );
 
71
    m_toggleOnlineButtonState = false;
 
72
    connect( m_toggleOnlineButton, SIGNAL( clicked() ),
 
73
             this, SLOT( toggleOnlineStateForAll() ) );
 
74
 
 
75
    QHBoxLayout *headerLayout = new QHBoxLayout( this );
 
76
    headerLayout->addWidget( connectionsLabel );
 
77
    headerLayout->addSpacing( 30 );
 
78
    headerLayout->addWidget( m_toggleOnlineButton );
 
79
    mainLayout->insertLayout( 0, headerLayout );
 
80
 
 
81
    updateToggleOnlineStateButton();
 
82
}
 
83
 
 
84
void
 
85
AccountListWidget::updateEntries( const QModelIndex& topLeft, const QModelIndex& bottomRight )
 
86
{
 
87
    for( int row = topLeft.row(); row <= bottomRight.row(); ++row )
 
88
    {
 
89
        QPersistentModelIndex idx( m_model->index( row, 0 ) );
 
90
 
 
91
        int newCount = idx.data( Tomahawk::Accounts::AccountModel::ChildrenOfFactoryRole )
 
92
                            .value< QList< Tomahawk::Accounts::Account* > >().count();
 
93
 
 
94
        if( m_entries.value( idx ).count() == newCount )
 
95
        {
 
96
            updateEntry( idx );
 
97
        }
 
98
        else
 
99
        {
 
100
            removeEntries( idx.parent(), idx.row(), idx.row() );
 
101
            insertEntries( idx.parent(), idx.row(), idx.row() );
 
102
        }
 
103
    }
 
104
}
 
105
 
 
106
void
 
107
AccountListWidget::updateEntry( const QPersistentModelIndex& idx )
 
108
{
 
109
    for ( int i = 0; i < m_entries.value( idx ).count(); ++i )
 
110
    {
 
111
        m_entries[ idx ][ i ]->update( idx, i ); //update the i-th account of the idx-th factory
 
112
    }
 
113
}
 
114
 
 
115
void
 
116
AccountListWidget::loadAllEntries()
 
117
{
 
118
    foreach ( QList< AccountWidget* > entry, m_entries )
 
119
    {
 
120
        foreach ( AccountWidget* w, entry )
 
121
        {
 
122
            m_layout->removeWidget( w );
 
123
            w->deleteLater();
 
124
        }
 
125
        entry.clear();
 
126
    }
 
127
    m_entries.clear();
 
128
 
 
129
    int rc =  m_model->rowCount();
 
130
    insertEntries( QModelIndex(), 0, rc - 1 );
 
131
}
 
132
 
 
133
void
 
134
AccountListWidget::insertEntries(  const QModelIndex& parent, int start, int end )
 
135
{
 
136
    for ( int i = start; i <= end; ++i )
 
137
    {
 
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 )
 
143
        {
 
144
            AccountWidget *entry = new AccountWidget( this );
 
145
            entryAccounts.append( entry );
 
146
        }
 
147
        m_entries.insert( idx, entryAccounts );
 
148
        for ( int j = 0; j < entryAccounts.length(); ++j )
 
149
            m_layout->insertWidget( i+j, entryAccounts.at( j ) );
 
150
 
 
151
        updateEntry( idx );
 
152
 
 
153
        for ( int j = 0; j < entryAccounts.length(); ++j )
 
154
        {
 
155
            entryAccounts[ j ]->setupConnections( idx, j );
 
156
        }
 
157
    }
 
158
}
 
159
 
 
160
void
 
161
AccountListWidget::removeEntries( const QModelIndex& parent, int start, int end )
 
162
{
 
163
    for ( int i = start; i <= end; ++i )
 
164
    {
 
165
        QPersistentModelIndex idx( m_model->index( i, 0, parent ) );
 
166
        if ( !idx.isValid() ) //means we just removed the last account for a factory
 
167
        {
 
168
            for ( QHash< QPersistentModelIndex, QList< AccountWidget* > >::iterator it = m_entries.begin();
 
169
                  it != m_entries.end(); ++it )
 
170
            {
 
171
                if ( !it.key().isValid() )
 
172
                {
 
173
                    idx = it.key();
 
174
                }
 
175
            }
 
176
        }
 
177
 
 
178
        QList< AccountWidget* > &entryAccounts = m_entries[ idx ];
 
179
        for ( int j = 0; j < entryAccounts.count(); ++j )
 
180
        {
 
181
            AccountWidget *a = entryAccounts.at( j );
 
182
            m_layout->removeWidget( a );
 
183
            a->deleteLater();
 
184
        }
 
185
        m_entries.remove( idx );
 
186
    }
 
187
    adjustSize();
 
188
    qobject_cast< QWidget* >( QWidget::parent() )->adjustSize();
 
189
}
 
190
 
 
191
void
 
192
AccountListWidget::toggleOnlineStateForAll()
 
193
{
 
194
    bool newState = !m_toggleOnlineButtonState;
 
195
    foreach ( QList< AccountWidget* > awgts, m_entries )
 
196
    {
 
197
        foreach ( AccountWidget* awgt, awgts )
 
198
        {
 
199
            awgt->setConnectionState( newState );
 
200
        }
 
201
    }
 
202
}
 
203
 
 
204
void
 
205
AccountListWidget::updateToggleOnlineStateButton()
 
206
{
 
207
    bool newState = false;
 
208
    foreach ( QList< AccountWidget* > awgts, m_entries )
 
209
    {
 
210
        foreach ( AccountWidget* awgt, awgts )
 
211
        {
 
212
            if ( awgt->connectionState() )
 
213
            {
 
214
                newState = true;
 
215
                goto end; //break 2 levels
 
216
            }
 
217
        }
 
218
    }
 
219
    end:;
 
220
 
 
221
    if ( newState != m_toggleOnlineButtonState )
 
222
    {
 
223
        m_toggleOnlineButtonState = newState;
 
224
        if ( newState )
 
225
            Tomahawk::Accounts::AccountManager::instance()->connectAll();
 
226
        else
 
227
            Tomahawk::Accounts::AccountManager::instance()->disconnectAll();
 
228
    }
 
229
 
 
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" ) );
 
235
}