PageRenderTime 352ms CodeModel.GetById 81ms app.highlight 81ms RepoModel.GetById 129ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
C++ | 189 lines | 138 code | 34 blank | 17 comment | 10 complexity | ead54d4fe055fdf5c261c024c6481a77 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 *
  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 "DynamicControlList.h"
 20
 21#include "DynamicControlWrapper.h"
 22#include "playlist/dynamic/GeneratorInterface.h"
 23#include "utils/ImageRegistry.h"
 24#include "utils/TomahawkUtils.h"
 25#include "utils/Logger.h"
 26#include "Source.h"
 27
 28#include <QLayout>
 29#include <QLabel>
 30#include <QPaintEvent>
 31#include <QPushButton>
 32#include <QToolButton>
 33#include <QPainter>
 34#include <QGridLayout>
 35#include <QHBoxLayout>
 36
 37
 38using namespace Tomahawk;
 39
 40
 41DynamicControlList::DynamicControlList( QWidget* parent )
 42    : QWidget( parent )
 43    , m_layout( new QGridLayout )
 44{
 45    init();
 46}
 47
 48
 49DynamicControlList::DynamicControlList( const geninterface_ptr& generator, const QList< dyncontrol_ptr >& controls, QWidget* parent )
 50    : QWidget( parent )
 51    , m_generator( generator )
 52    , m_layout( new QGridLayout )
 53{
 54    init();
 55    setControls(  generator, controls );
 56}
 57
 58
 59DynamicControlList::~DynamicControlList()
 60{
 61}
 62
 63
 64void
 65DynamicControlList::init()
 66{
 67    qDebug() << "GRIDLAYOUT: " << m_layout->rowCount();
 68    setContentsMargins( 0, 0, 0, 0 );
 69    setLayout( m_layout );
 70    m_layout->setColumnStretch( 2, 1 );
 71    m_layout->setMargin( 0 );
 72    m_layout->setVerticalSpacing( 0 );
 73#ifdef Q_OS_MAC // on OS X we don't want the right edge of the toolbuttons against the window
 74    m_layout->setContentsMargins( 0, 0, 3, 0 );
 75#else
 76    m_layout->setContentsMargins( 0, 0, 0, 0 );
 77#endif
 78    m_layout->setSizeConstraint( QLayout::SetMinimumSize );
 79
 80    m_collapseLayout = new QHBoxLayout();
 81    m_collapseLayout->setContentsMargins( 0, 0, 0, 0 );
 82    m_collapseLayout->setMargin( 0 );
 83    m_collapseLayout->setSpacing( 0 );
 84    m_collapse = new QPushButton( tr( "Save Settings" ), this );
 85    m_collapse->setAttribute( Qt::WA_LayoutUsesWidgetRect );
 86    m_collapseLayout->addWidget( m_collapse );
 87    m_addControl = new QToolButton( this );
 88    m_addControl->setAttribute( Qt::WA_LayoutUsesWidgetRect );
 89    m_addControl->setIcon( ImageRegistry::instance()->icon( RESPATH "images/list-add.svg" ) );
 90    m_addControl->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
 91    m_addControl->setIconSize( QSize( 16, 16 ) );
 92    m_addControl->setToolButtonStyle( Qt::ToolButtonIconOnly );
 93    m_addControl->setAutoRaise( true );
 94    m_addControl->setContentsMargins( 0, 0, 0, 0 );
 95    m_collapseLayout->addWidget( m_addControl );
 96    m_collapse->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
 97
 98    connect( m_collapse, SIGNAL( clicked() ), this, SIGNAL( toggleCollapse() ) );
 99    connect( m_addControl, SIGNAL( clicked() ), this, SLOT( addNewControl() ) );
100
101    setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
102}
103
104
105void
106DynamicControlList::setControls( const geninterface_ptr& generator, const QList< dyncontrol_ptr >& controls )
107{
108    if( m_controls.size() == controls.size() && controls.size() > 0 ) { // check if we're setting the same controls we already have, and exit if we are
109        bool different = false;
110        for( int i = 0; i < m_controls.size(); i++ ) {
111            if( m_controls.value( i )->control().data() != controls.value( i ).data() ) {
112                different = true;
113                break;
114            }
115        }
116        if( !different ) { // no work to do
117            return;
118        }
119    }
120
121    if( !m_controls.isEmpty() ) {
122        qDeleteAll( m_controls );
123        m_controls.clear();
124    }
125
126    m_layout->removeItem( m_collapseLayout );
127
128    m_generator = generator;
129    if( controls.isEmpty() ) {
130        qDebug() << "CREATING DEFAULT CONTROL";
131        DynamicControlWrapper* ctrlW = new DynamicControlWrapper( generator->createControl(), m_layout, m_controls.size(), this );
132        connect( ctrlW, SIGNAL( removeControl() ), this, SLOT( removeControl() ) );
133        connect( ctrlW, SIGNAL( changed() ), this, SLOT( controlChanged() ) );
134        m_controls << ctrlW;
135    } else
136    {
137        foreach( const dyncontrol_ptr& control, controls ) {
138            DynamicControlWrapper* ctrlW = new DynamicControlWrapper( control, m_layout, m_controls.size(), this );
139            connect( ctrlW, SIGNAL( removeControl() ), this, SLOT( removeControl() ) );
140            connect( ctrlW, SIGNAL( changed() ), this, SLOT( controlChanged() ) );
141
142            m_controls << ctrlW;
143        }
144    }
145    m_layout->addItem( m_collapseLayout, m_layout->rowCount(), 0, 1, 4, Qt::AlignCenter );
146
147}
148
149
150void
151DynamicControlList::addNewControl()
152{
153    m_layout->removeItem( m_collapseLayout );
154
155    dyncontrol_ptr control = m_generator->createControl();
156    m_controls.append( new DynamicControlWrapper( control, m_layout, m_layout->rowCount(), this ) );
157    connect( m_controls.last(), SIGNAL( removeControl() ), this, SLOT( removeControl() ) );
158    connect( m_controls.last(), SIGNAL( changed() ), this, SLOT( controlChanged() ) );
159
160    m_layout->addItem( m_collapseLayout, m_layout->rowCount(), 0, 1, 4, Qt::AlignCenter );
161    emit controlsChanged( true );
162}
163
164
165void
166DynamicControlList::removeControl()
167{
168    DynamicControlWrapper* w = qobject_cast<DynamicControlWrapper*>( sender() );
169    w->removeFromLayout();
170    m_controls.removeAll( w );
171
172    m_generator->removeControl( w->control() );
173    w->deleteLater();
174
175    emit controlsChanged( false );
176}
177
178
179void
180DynamicControlList::controlChanged()
181{
182    Q_ASSERT( sender() && qobject_cast<DynamicControlWrapper*>(sender()) );
183    DynamicControlWrapper* widget = qobject_cast<DynamicControlWrapper*>(sender());
184
185    qDebug() << "control changed!";
186    foreach( DynamicControlWrapper* c, m_controls )
187        qDebug() << c->control()->id() << c->control()->selectedType() << c->control()->match() << c->control()->input();
188    emit controlChanged( widget->control() );
189}