1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Leo Franchi <lfranchi@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 "DynamicControlWrapper.h"
21
#include "playlist/dynamic/DynamicControl.h"
22
#include "utils/TomahawkUtilsGui.h"
23
#include "utils/Logger.h"
25
#include <QHBoxLayout>
28
#include <QToolButton>
29
#include <QPaintEvent>
31
#include <QStackedLayout>
33
using namespace Tomahawk;
36
DynamicControlWrapper::DynamicControlWrapper( const Tomahawk::dyncontrol_ptr& control, QGridLayout* layout, int row, QWidget* parent )
41
, m_control( control )
43
, m_layout( QWeakPointer< QGridLayout >( layout ) )
45
m_typeSelector = new QComboBox( m_parent );
47
m_matchSelector = QWeakPointer<QWidget>( control->matchSelector() );
48
m_entryWidget = QWeakPointer<QWidget>( control->inputField() );
50
m_minusButton = initButton( m_parent );
51
m_minusButton->setIcon( TomahawkUtils::defaultPixmap( TomahawkUtils::ListRemove ) );
52
connect( m_minusButton, SIGNAL( clicked( bool ) ), this, SIGNAL( removeControl() ) );
54
m_plusL = new QStackedLayout();
55
m_plusL->setContentsMargins( 0, 0, 0, 0 );
56
m_plusL->setMargin( 0 );
57
m_plusL->addWidget( m_minusButton );
58
m_plusL->addWidget( createDummy( m_minusButton, m_parent ) ); // :-(
60
connect( m_typeSelector, SIGNAL( activated( QString) ), SLOT( typeSelectorChanged( QString ) ) );
61
connect( m_control.data(), SIGNAL( changed() ), this, SIGNAL( changed() ) );
63
m_layout.data()->addWidget( m_typeSelector, row, 0, Qt::AlignLeft );
65
if( !control.isNull() ) {
66
foreach( const QString& type, control->typeSelectors() )
67
m_typeSelector->addItem( type );
70
typeSelectorChanged( m_control.isNull() ? "" : m_control->selectedType(), true );
72
m_layout.data()->addLayout( m_plusL, m_row, 3, Qt::AlignCenter );
73
m_plusL->setCurrentIndex( 0 );
77
DynamicControlWrapper::~DynamicControlWrapper()
79
// remove the controls widgets from our layout so they are not parented
80
// we don't want to auto-delete them since the control should own them
81
// if we delete them, then the control will be holding on to null ptrs
84
if( !m_entryWidget.isNull() )
85
m_control->inputField()->setParent( 0 );
86
if( !m_matchSelector.isNull() )
87
m_control->matchSelector()->setParent( 0 );
89
delete m_typeSelector;
95
DynamicControlWrapper::control() const
102
DynamicControlWrapper::removeFromLayout()
104
if( m_layout.isNull() )
107
if( !m_matchSelector.isNull() )
108
m_layout.data()->removeWidget( m_matchSelector.data() );
109
if( !m_entryWidget.isNull() )
110
m_layout.data()->removeWidget( m_entryWidget.data() );
111
m_layout.data()->removeWidget( m_typeSelector );
112
m_layout.data()->removeItem( m_plusL );
117
DynamicControlWrapper::initButton( QWidget* parent )
119
QToolButton* btn = new QToolButton( parent );
120
btn->setAttribute( Qt::WA_LayoutUsesWidgetRect );
121
btn->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
122
btn->setIconSize( QSize( 16, 16 ) );
123
btn->setToolButtonStyle( Qt::ToolButtonIconOnly );
124
btn->setAutoRaise( true );
125
btn->setContentsMargins( 0, 0, 0, 0 );
131
DynamicControlWrapper::createDummy( QWidget* fromW, QWidget* parent )
133
QWidget* dummy = new QWidget( parent );
134
dummy->setAttribute( Qt::WA_LayoutUsesWidgetRect );
135
dummy->setContentsMargins( 0, 0, 0, 0 );
136
dummy->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
137
dummy->setMinimumSize( fromW->sizeHint() );
138
dummy->setMaximumSize( fromW->sizeHint() );
144
DynamicControlWrapper::typeSelectorChanged( const QString& type, bool firstLoad )
146
Q_ASSERT( !m_layout.isNull() );
147
m_layout.data()->removeWidget( m_matchSelector.data() );
148
m_layout.data()->removeWidget( m_entryWidget.data() );
150
if( m_control->selectedType() != type && !firstLoad )
151
m_control->setSelectedType( type );
154
int idx = m_typeSelector->findText( type );
156
m_typeSelector->setCurrentIndex( idx );
159
if( m_control->matchSelector() ) {
160
m_matchSelector = QWeakPointer<QWidget>( m_control->matchSelector() );
161
m_layout.data()->addWidget( m_matchSelector.data(), m_row, 1, Qt::AlignCenter );
162
m_matchSelector.data()->show();
164
if( m_control->inputField() ) {
165
m_entryWidget = QWeakPointer<QWidget>( m_control->inputField() );
166
m_layout.data()->addWidget( m_entryWidget.data(), m_row, 2 );
167
m_entryWidget.data()->show();
176
DynamicControlWrapper::enterEvent(QEvent* ev)
180
m_plusL->setCurrentIndex( 0 );
183
QObject::enterEvent( ev );
187
DynamicControlWrapper::leaveEvent(QEvent* ev)
191
m_plusL->setCurrentIndex( 1 );
194
QWidget::leaveEvent( ev );