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

« back to all changes in this revision

Viewing changes to src/libtomahawk/playlist/dynamic/widgets/DynamicControlWrapper.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 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
 
 
33
using namespace Tomahawk;
 
34
 
 
35
 
 
36
DynamicControlWrapper::DynamicControlWrapper( const Tomahawk::dyncontrol_ptr& control, QGridLayout* layout, int row, QWidget* parent )
 
37
     : QObject( parent )
 
38
     , m_parent( parent )
 
39
     , m_row( row )
 
40
     , m_minusButton( 0 )
 
41
     , m_control( control )
 
42
     , m_typeSelector( 0 )
 
43
     , m_layout( QWeakPointer< QGridLayout >( layout ) )
 
44
{
 
45
    m_typeSelector = new QComboBox( m_parent );
 
46
 
 
47
    m_matchSelector = QWeakPointer<QWidget>( control->matchSelector() );
 
48
    m_entryWidget = QWeakPointer<QWidget>( control->inputField() );
 
49
 
 
50
    m_minusButton = initButton( m_parent );
 
51
    m_minusButton->setIcon( TomahawkUtils::defaultPixmap( TomahawkUtils::ListRemove ) );
 
52
    connect( m_minusButton, SIGNAL( clicked( bool ) ), this, SIGNAL( removeControl() ) );
 
53
 
 
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 ) ); // :-(
 
59
 
 
60
    connect( m_typeSelector, SIGNAL( activated( QString) ), SLOT( typeSelectorChanged( QString ) ) );
 
61
    connect( m_control.data(), SIGNAL( changed() ), this, SIGNAL( changed() ) );
 
62
 
 
63
    m_layout.data()->addWidget( m_typeSelector, row, 0, Qt::AlignLeft );
 
64
 
 
65
    if( !control.isNull() ) {
 
66
        foreach( const QString& type, control->typeSelectors() )
 
67
            m_typeSelector->addItem( type );
 
68
    }
 
69
 
 
70
    typeSelectorChanged( m_control.isNull() ? "" : m_control->selectedType(), true );
 
71
 
 
72
    m_layout.data()->addLayout( m_plusL, m_row, 3, Qt::AlignCenter );
 
73
    m_plusL->setCurrentIndex( 0 );
 
74
}
 
75
 
 
76
 
 
77
DynamicControlWrapper::~DynamicControlWrapper()
 
78
{
 
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
 
82
    removeFromLayout();
 
83
 
 
84
    if( !m_entryWidget.isNull() )
 
85
        m_control->inputField()->setParent( 0 );
 
86
    if( !m_matchSelector.isNull() )
 
87
        m_control->matchSelector()->setParent( 0 );
 
88
 
 
89
    delete m_typeSelector;
 
90
    delete m_minusButton;
 
91
}
 
92
 
 
93
 
 
94
dyncontrol_ptr
 
95
DynamicControlWrapper::control() const
 
96
{
 
97
    return m_control;
 
98
}
 
99
 
 
100
 
 
101
void
 
102
DynamicControlWrapper::removeFromLayout()
 
103
{
 
104
    if( m_layout.isNull() )
 
105
        return;
 
106
 
 
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 );
 
113
}
 
114
 
 
115
 
 
116
QToolButton*
 
117
DynamicControlWrapper::initButton( QWidget* parent )
 
118
{
 
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 );
 
126
    return btn;
 
127
}
 
128
 
 
129
 
 
130
QWidget*
 
131
DynamicControlWrapper::createDummy( QWidget* fromW, QWidget* parent )
 
132
{
 
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() );
 
139
    return dummy;
 
140
}
 
141
 
 
142
 
 
143
void
 
144
DynamicControlWrapper::typeSelectorChanged( const QString& type, bool firstLoad )
 
145
{
 
146
    Q_ASSERT( !m_layout.isNull() );
 
147
    m_layout.data()->removeWidget( m_matchSelector.data() );
 
148
    m_layout.data()->removeWidget( m_entryWidget.data() );
 
149
 
 
150
    if( m_control->selectedType() != type && !firstLoad )
 
151
        m_control->setSelectedType( type );
 
152
 
 
153
 
 
154
    int idx = m_typeSelector->findText( type );
 
155
    if( idx > -1 )
 
156
        m_typeSelector->setCurrentIndex( idx );
 
157
 
 
158
 
 
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();
 
163
    }
 
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();
 
168
    }
 
169
 
 
170
    emit changed();
 
171
}
 
172
 
 
173
 
 
174
/*
 
175
void
 
176
DynamicControlWrapper::enterEvent(QEvent* ev)
 
177
{
 
178
    m_mouseOver = true;
 
179
    if( m_isLocal )
 
180
        m_plusL->setCurrentIndex( 0 );
 
181
 
 
182
    if( ev )
 
183
        QObject::enterEvent( ev );
 
184
}
 
185
 
 
186
void
 
187
DynamicControlWrapper::leaveEvent(QEvent* ev)
 
188
{
 
189
    m_mouseOver = true;
 
190
    if( m_isLocal )
 
191
        m_plusL->setCurrentIndex( 1 );
 
192
 
 
193
    if( ev )
 
194
        QWidget::leaveEvent( ev );
 
195
}
 
196
*/
 
197