PageRenderTime 164ms CodeModel.GetById 61ms app.highlight 42ms RepoModel.GetById 58ms app.codeStats 0ms

/src/libtomahawk/playlist/dynamic/widgets/CollapsibleControls.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 217 lines | 154 code | 42 blank | 21 comment | 11 complexity | 67e33b2e7c3fa1b1da8b003aea02c1f0 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU General Public License as published by
  8 *   the Free Software Foundation, either version 3 of the License, or
  9 *   (at your option) any later version.
 10 *
 11 *   Tomahawk is distributed in the hope that it will be useful,
 12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 *   GNU General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#include "CollapsibleControls.h"
 21
 22#include "DynamicControlList.h"
 23#include "DynamicControlWrapper.h"
 24#include "playlist/dynamic/GeneratorInterface.h"
 25#include "playlist/dynamic/DynamicControl.h"
 26#include "utils/ImageRegistry.h"
 27#include "utils/TomahawkUtils.h"
 28#include "widgets/ElidedLabel.h"
 29#include "Source.h"
 30
 31#include <QLabel>
 32#include <QStackedLayout>
 33#include <QToolButton>
 34#include <QEasingCurve>
 35#include <QTimeLine>
 36#include <QPaintEvent>
 37#include <QPainter>
 38
 39#include "utils/Logger.h"
 40
 41using namespace Tomahawk;
 42
 43
 44CollapsibleControls::CollapsibleControls( QWidget* parent )
 45    : QWidget( parent )
 46    , m_isLocal( true )
 47{
 48    init();
 49}
 50
 51
 52CollapsibleControls::CollapsibleControls( const dynplaylist_ptr& playlist, bool isLocal, QWidget* parent )
 53    : QWidget( parent )
 54    , m_dynplaylist( playlist )
 55    , m_isLocal( isLocal )
 56{
 57    init();
 58    setControls( m_dynplaylist, m_isLocal );
 59}
 60
 61
 62Tomahawk::CollapsibleControls::~CollapsibleControls()
 63{
 64
 65}
 66
 67
 68void
 69CollapsibleControls::init()
 70{
 71    m_timeline = new QTimeLine( 250, this );
 72    m_timeline->setUpdateInterval( 5 );
 73    m_animHeight = -1;
 74    m_collapseAnimation = false;
 75
 76    connect( m_timeline, SIGNAL( frameChanged( int ) ), this, SLOT( onAnimationStep( int ) ) );
 77    connect( m_timeline, SIGNAL( finished() ), this, SLOT( onAnimationFinished() ) );
 78
 79    m_layout = new QStackedLayout;
 80    setContentsMargins( 0, 0, 0, 0 );
 81    m_layout->setContentsMargins( 0, 0, 0, 0 );
 82    m_layout->setSpacing( 0 );
 83
 84    m_controls = new Tomahawk::DynamicControlList( this );
 85    m_layout->addWidget( m_controls );
 86    connect( m_controls, SIGNAL( toggleCollapse() ), this, SLOT( toggleCollapse() ) );
 87
 88    m_summaryWidget = new QWidget( this );
 89    m_summaryWidget->setMinimumHeight( 24 );
 90    m_summaryWidget->setMaximumHeight( 24 );
 91    m_summaryWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
 92    m_summaryLayout = new QHBoxLayout;
 93    m_summaryWidget->setLayout( m_summaryLayout );
 94    m_summaryLayout->setMargin( 0 );
 95    m_summaryWidget->setContentsMargins( 3, 0, 0, 0 );
 96
 97    m_summary = new ElidedLabel( m_summaryWidget );
 98    QFont f = m_summary->font();
 99    f.setPointSize( f.pointSize() + 1 );
100    f.setBold( true );
101    m_summary->setFont( f );
102    m_summaryLayout->addWidget( m_summary, 1 );
103    m_summaryExpand = DynamicControlWrapper::initButton( this );
104    m_summaryExpand->setIcon( ImageRegistry::instance()->icon( RESPATH "images/arrow-down-double.svg" ) );
105    m_expandL = new QStackedLayout;
106    m_expandL->setContentsMargins( 0, 0, 0, 0 );
107    m_expandL->setMargin( 0 );
108    m_expandL->addWidget( m_summaryExpand );
109    m_expandL->addWidget( DynamicControlWrapper::createDummy( m_summaryExpand, this ) );
110    m_summaryLayout->addLayout( m_expandL );
111    if( m_isLocal )
112        m_expandL->setCurrentIndex( 0 );
113    else
114        m_expandL->setCurrentIndex( 1 );
115
116    m_layout->addWidget( m_summaryWidget );
117    connect( m_summaryExpand, SIGNAL( clicked( bool ) ), this, SLOT( toggleCollapse() ) );
118
119    if( m_isLocal )
120        m_layout->setCurrentWidget( m_controls );
121    else
122        m_layout->setCurrentWidget( m_summary );
123
124    connect( m_controls, SIGNAL( controlChanged( Tomahawk::dyncontrol_ptr ) ), SIGNAL( controlChanged( Tomahawk::dyncontrol_ptr ) ) );
125    connect( m_controls, SIGNAL( controlsChanged( bool ) ), SIGNAL( controlsChanged( bool ) ) );
126
127    setLayout( m_layout );
128
129    setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
130}
131
132
133QList< DynamicControlWrapper* >
134Tomahawk::CollapsibleControls::controls() const
135{
136    return m_controls->controls();
137}
138
139
140void
141CollapsibleControls::setControls( const dynplaylist_ptr& playlist, bool isLocal )
142{
143    m_dynplaylist = playlist;
144    m_isLocal = isLocal;
145    m_controls->setControls( m_dynplaylist->generator(), m_dynplaylist->generator()->controls() );
146
147    if( !m_isLocal ) {
148        m_expandL->setCurrentIndex( 1 );
149        m_summary->setText( m_dynplaylist->generator()->sentenceSummary() );
150        m_layout->setCurrentWidget( m_summaryWidget );
151        setMaximumHeight( m_summaryWidget->sizeHint().height() );
152    } else {
153        m_expandL->setCurrentIndex( 0  );
154    }
155}
156
157
158void
159CollapsibleControls::toggleCollapse()
160{
161//     qDebug() << "TOGGLING SIZEHINTS:" << m_controls->height() << m_summaryWidget->sizeHint();
162    m_timeline->setEasingCurve( QEasingCurve::OutBack );
163    m_timeline->setFrameRange( m_summaryWidget->sizeHint().height(), m_controls->height() );
164    if( m_layout->currentWidget() == m_controls ) {
165        m_summary->setText( m_dynplaylist->generator()->sentenceSummary() );
166        m_controls->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
167
168        m_timeline->setDirection( QTimeLine::Backward );
169        m_timeline->start();
170
171        m_collapseAnimation = true;
172    } else {
173        m_summaryWidget->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
174        m_layout->setCurrentWidget( m_controls );
175
176        m_timeline->setDirection( QTimeLine::Forward );
177        m_timeline->start();
178
179        m_collapseAnimation = false;
180    }
181}
182
183
184void
185CollapsibleControls::onAnimationStep( int step )
186{
187//     qDebug() << "ANIMATION STEP:" << step;
188    resize( width(), step );
189    m_animHeight = step;
190    setMaximumHeight( m_animHeight );
191}
192
193
194void
195CollapsibleControls::onAnimationFinished()
196{
197//     qDebug() << "ANIMATION DONE:" << m_animHeight;
198    setMaximumHeight( m_animHeight );
199    m_animHeight = -1;
200
201    if( m_collapseAnimation ) {
202        m_layout->setCurrentWidget( m_summaryWidget );
203    } else {
204        setMaximumHeight( QWIDGETSIZE_MAX );
205    }
206}
207
208
209QSize
210CollapsibleControls::sizeHint() const
211{
212    if( m_animHeight >= 0 ) {
213        return QSize( QWidget::sizeHint().width(), m_animHeight );
214    } else {
215        return QWidget::sizeHint();
216    }
217}