1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2011, Casey Link <unnamedrambler@gmail.com>
4
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
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.
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.
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/>.
20
#include "BreadcrumbButton.h"
22
#include "Breadcrumb.h"
24
#include "utils/StyleHelper.h"
25
#include "utils/TomahawkUtilsGui.h"
26
#include "utils/Logger.h"
28
#include <QPaintEvent>
30
#include <QHBoxLayout>
32
using namespace Tomahawk;
34
class BreadcrumbArrow : public QWidget
37
BreadcrumbArrow( QWidget* parent )
40
setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
44
virtual void paintEvent( QPaintEvent* ) {
50
const bool reverse = opt.direction == Qt::RightToLeft;
51
const int menuButtonWidth = 12;
52
const int rightSpacing = 10;
53
const int right = !reverse ? r.right() - rightSpacing : r.left() + menuButtonWidth;
54
const int height = r.height();
56
QLine l1( 1, 0, right, height / 2 );
57
QLine l2( 1, height, right, height / 2 );
59
p.setRenderHint( QPainter::Antialiasing, true );
62
QColor shadow( 0, 0, 0, 100 );
68
// Draw the main arrow
69
QColor foreGround( "#747474" );
71
p.setPen( foreGround );
76
virtual QSize sizeHint() const
78
return QSize( 20, TomahawkUtils::defaultFontHeight() + 8 );
83
BreadcrumbButton::BreadcrumbButton( Breadcrumb* parent, QAbstractItemModel* model )
85
, m_breadcrumb( parent )
87
, m_combo( new ComboBox( this ) )
88
, m_arrow( new BreadcrumbArrow( this ) )
90
setLayout( new QHBoxLayout );
92
TomahawkUtils::unmarginLayout( layout() );
93
layout()->addWidget( m_combo );
94
layout()->addWidget( m_arrow );
96
setFixedHeight( TomahawkUtils::defaultFontHeight() + 8 );
97
m_combo->setSizeAdjustPolicy( QComboBox::AdjustToContents );
99
setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Expanding );
101
connect( m_combo, SIGNAL( activated( int ) ), SLOT( comboboxActivated( int ) ) );
106
BreadcrumbButton::paintEvent( QPaintEvent* )
110
StyleHelper::horizontalHeader( &p, rect() ); // draw the background
111
m_arrow->setVisible( hasChildren() );
116
BreadcrumbButton::sizeHint() const
118
// our width = width of combo + 20px for right-arrow and spacing
119
const int padding = hasChildren() ? 20 : 8;
120
return m_combo->sizeHint() + QSize( padding, 0 );
125
BreadcrumbButton::setParentIndex( const QModelIndex& idx )
128
// Populate listview with list of children.
129
// Then try to find a default
131
int count = m_model->rowCount( m_parentIndex );
132
int defaultIndex = -1, userSelected = -1;
133
for ( int i = 0; i < count; ++i )
135
QModelIndex idx = m_model->index( i, 0, m_parentIndex );
138
list << idx.data().toString();
139
if ( idx.data( Breadcrumb::DefaultRole ).toBool() )
141
if ( idx.data( Breadcrumb::UserSelectedRole ).toBool() )
146
if ( m_combo->count() && list.count() )
148
// Check if it's the same, Don't change if it is, as it'll cause flickering
150
for ( int i = 0; i < m_combo->count(); i++ )
151
old << m_combo->itemText( i );
159
m_combo->addItems( list );
161
if ( userSelected > -1 )
162
m_combo->setCurrentIndex( userSelected );
163
else if ( defaultIndex > -1 )
164
m_combo->setCurrentIndex( defaultIndex );
166
m_curIndex = m_model->index( m_combo->currentIndex(), 0, m_parentIndex );
169
m_combo->adjustSize();
174
BreadcrumbButton::comboboxActivated( int idx )
176
m_model->setData( m_curIndex, false, Breadcrumb::UserSelectedRole );
178
QModelIndex selected = m_model->index( idx, 0, m_parentIndex );
179
m_curIndex = selected;
180
m_model->setData( selected, true, Breadcrumb::UserSelectedRole );
182
emit currentIndexChanged( selected );
187
BreadcrumbButton::hasChildren() const
189
return m_model->rowCount( m_model->index( m_combo->currentIndex(), 0, m_parentIndex ) ) > 0;
194
BreadcrumbButton::currentIndex() const
196
return m_model->index( m_combo->currentIndex(), 0, m_parentIndex );