PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 32ms RepoModel.GetById 29ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
C++ | 205 lines | 121 code | 40 blank | 44 comment | 14 complexity | 30c51b9f2dc1cf4c474bfa067883c565 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 "DynamicControlWrapper.h"
 20
 21#include "playlist/dynamic/DynamicControl.h"
 22#include "utils/TomahawkUtilsGui.h"
 23#include "utils/Logger.h"
 24
 25#include <QHBoxLayout>
 26#include <QComboBox>
 27#include <QLayout>
 28#include <QToolButton>
 29#include <QPaintEvent>
 30#include <QPainter>
 31#include <QStackedLayout>
 32#include <QApplication>
 33
 34using namespace Tomahawk;
 35
 36
 37DynamicControlWrapper::DynamicControlWrapper( const Tomahawk::dyncontrol_ptr& control, QGridLayout* layout, int row, QWidget* parent )
 38     : QObject( parent )
 39     , m_parent( parent )
 40     , m_row( row )
 41     , m_minusButton( 0 )
 42     , m_control( control )
 43     , m_typeSelector( 0 )
 44     , m_layout( QPointer< QGridLayout >( layout ) )
 45{
 46    m_typeSelector = new QComboBox( m_parent );
 47
 48    m_matchSelector = QPointer<QWidget>( control->matchSelector() );
 49    m_entryWidget = QPointer<QWidget>( control->inputField() );
 50
 51    m_minusButton = initButton( m_parent );
 52    m_minusButton->setIcon( TomahawkUtils::defaultPixmap( TomahawkUtils::ListRemove ) );
 53    connect( m_minusButton, SIGNAL( clicked( bool ) ), this, SIGNAL( removeControl() ) );
 54
 55    m_plusL = new QStackedLayout();
 56    m_plusL->setContentsMargins( 0, 0, 0, 0 );
 57    m_plusL->setMargin( 0 );
 58    m_plusL->addWidget( m_minusButton );
 59    m_plusL->addWidget( createDummy( m_minusButton, m_parent ) ); // :-(
 60
 61    connect( m_typeSelector, SIGNAL( activated( int ) ), SLOT( typeSelectorChanged( int ) ) );
 62    connect( m_control.data(), SIGNAL( changed() ), this, SIGNAL( changed() ) );
 63
 64    m_layout.data()->addWidget( m_typeSelector, row, 0, Qt::AlignLeft );
 65
 66    if( !control.isNull() ) {
 67        foreach( const QString& type, control->typeSelectors() )
 68            m_typeSelector->addItem( qApp->translate( "Type selector", type.toUtf8() ), type );
 69    }
 70
 71
 72    int typeIndex = 0;
 73    if ( !m_control.isNull() ) {
 74        typeIndex = m_typeSelector->findData( m_control->selectedType() );
 75        if (typeIndex == -1) typeIndex = 0;
 76    }
 77    typeSelectorChanged( typeIndex, true );
 78
 79    m_layout.data()->addLayout( m_plusL, m_row, 3, Qt::AlignCenter );
 80    m_plusL->setCurrentIndex( 0 );
 81}
 82
 83
 84DynamicControlWrapper::~DynamicControlWrapper()
 85{
 86    // remove the controls widgets from our layout so they are not parented
 87    // we don't want to auto-delete them since the control should own them
 88    // if we delete them, then the control will be holding on to null ptrs
 89    removeFromLayout();
 90
 91    if( !m_entryWidget.isNull() )
 92        m_control->inputField()->setParent( 0 );
 93    if( !m_matchSelector.isNull() )
 94        m_control->matchSelector()->setParent( 0 );
 95
 96    delete m_typeSelector;
 97    delete m_minusButton;
 98}
 99
100
101dyncontrol_ptr
102DynamicControlWrapper::control() const
103{
104    return m_control;
105}
106
107
108void
109DynamicControlWrapper::removeFromLayout()
110{
111    if( m_layout.isNull() )
112        return;
113
114    if( !m_matchSelector.isNull() )
115        m_layout.data()->removeWidget( m_matchSelector.data() );
116    if( !m_entryWidget.isNull() )
117        m_layout.data()->removeWidget( m_entryWidget.data() );
118    m_layout.data()->removeWidget( m_typeSelector );
119    m_layout.data()->removeItem( m_plusL );
120}
121
122
123QToolButton*
124DynamicControlWrapper::initButton( QWidget* parent )
125{
126    QToolButton* btn = new QToolButton( parent );
127    btn->setAttribute( Qt::WA_LayoutUsesWidgetRect );
128    btn->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
129    btn->setIconSize( QSize( 16, 16 ) );
130    btn->setToolButtonStyle( Qt::ToolButtonIconOnly );
131    btn->setAutoRaise( true );
132    btn->setContentsMargins( 0, 0, 0, 0 );
133    return btn;
134}
135
136
137QWidget*
138DynamicControlWrapper::createDummy( QWidget* fromW, QWidget* parent )
139{
140    QWidget* dummy = new QWidget( parent );
141    dummy->setAttribute( Qt::WA_LayoutUsesWidgetRect );
142    dummy->setContentsMargins( 0, 0, 0, 0 );
143    dummy->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
144    dummy->setMinimumSize( fromW->sizeHint() );
145    dummy->setMaximumSize( fromW->sizeHint() );
146    return dummy;
147}
148
149
150void
151DynamicControlWrapper::typeSelectorChanged( int typeIndex, bool firstLoad )
152{
153    Q_ASSERT( !m_layout.isNull() );
154    m_layout.data()->removeWidget( m_matchSelector.data() );
155    m_layout.data()->removeWidget( m_entryWidget.data() );
156
157    // Get the untranslated type name
158    QString type = m_typeSelector->itemData(typeIndex).toString();
159
160    if( m_control->selectedType() != type && !firstLoad )
161        m_control->setSelectedType( type );
162
163
164    m_typeSelector->setCurrentIndex( typeIndex );
165
166
167    if( m_control->matchSelector() ) {
168        m_matchSelector = QPointer<QWidget>( m_control->matchSelector() );
169        m_layout.data()->addWidget( m_matchSelector.data(), m_row, 1, Qt::AlignCenter );
170        m_matchSelector.data()->show();
171    }
172    if( m_control->inputField() ) {
173        m_entryWidget = QPointer<QWidget>( m_control->inputField() );
174        m_layout.data()->addWidget( m_entryWidget.data(), m_row, 2 );
175        m_entryWidget.data()->show();
176    }
177
178    emit changed();
179}
180
181
182/*
183void
184DynamicControlWrapper::enterEvent(QEvent* ev)
185{
186    m_mouseOver = true;
187    if( m_isLocal )
188        m_plusL->setCurrentIndex( 0 );
189
190    if( ev )
191        QObject::enterEvent( ev );
192}
193
194void
195DynamicControlWrapper::leaveEvent(QEvent* ev)
196{
197    m_mouseOver = true;
198    if( m_isLocal )
199        m_plusL->setCurrentIndex( 1 );
200
201    if( ev )
202        QWidget::leaveEvent( ev );
203}
204*/
205