PageRenderTime 223ms CodeModel.GetById 120ms app.highlight 10ms RepoModel.GetById 91ms app.codeStats 0ms

/src/libtomahawk/widgets/ComboBox.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 93 lines | 60 code | 16 blank | 17 comment | 2 complexity | 65cf2f00f9a4d4d4dd148f75f968bbe2 MD5 | raw file
 1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 2 *
 3 *   Copyright 2011, Casey Link <unnamedrambler@gmail.com>
 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 "ComboBox.h"
20
21#include "utils/TomahawkStyle.h"
22#include "utils/TomahawkUtilsGui.h"
23#include "utils/Logger.h"
24
25#include <QStyle>
26#include <QTextOption>
27#include <QStylePainter>
28#include <QStyleOptionComboBox>
29
30
31ComboBox::ComboBox( QWidget* parent )
32    : QComboBox( parent )
33{
34}
35
36
37ComboBox::~ComboBox()
38{
39}
40
41
42QSize
43ComboBox::sizeHint() const
44{
45    return QComboBox::sizeHint() + QSize( 8, 0 );
46}
47
48
49void
50ComboBox::paintEvent( QPaintEvent* )
51{
52    QStylePainter p( this );
53    p.setPen( palette().color( QPalette::Text ) );
54    QStyleOptionComboBox cb;
55    initStyleOption( &cb );
56    QRect r = cb.rect;
57    r.setHeight( TomahawkUtils::defaultFontHeight() + 8 );
58
59    TomahawkStyle::horizontalHeader( &p, r );
60
61    if ( cb.state & QStyle::State_MouseOver )
62    {
63        p.save();
64        QRect highlightRect( r );
65        QSize shrink( 3, 4 );
66        QSize hS( highlightRect.size() );
67        hS -= shrink;
68        highlightRect.setSize( hS );
69        highlightRect.translate( 0, 2 );
70        p.setRenderHint( QPainter::Antialiasing );
71        p.setBrush( TomahawkStyle::HEADER_HIGHLIGHT );
72        p.drawRoundedRect( highlightRect, 10.0, 10.0 );
73        p.restore();
74    }
75
76    p.save();
77    QTextOption to( Qt::AlignVCenter );
78    r.adjust( 8, 0, -8, 0 );
79    p.setPen( Qt::white );
80    p.setBrush( TomahawkStyle::HEADER_TEXT );
81    p.drawText( r, cb.currentText, to );
82
83    bool reverse = cb.direction == Qt::RightToLeft;
84    int menuButtonWidth = 12;
85    int left = !reverse ? r.right() - menuButtonWidth : r.left();
86    int right = !reverse ? r.right() : r.left() + menuButtonWidth;
87    QRect arrowRect( ( left + right ) / 2 + ( reverse ? 6 : -6 ), r.center().y() - 3, 9, 9 ); //FIXME: no consts please
88
89    QStyleOption arrowOpt = cb;
90    arrowOpt.rect = arrowRect;
91    TomahawkStyle::drawArrow( QStyle::PE_IndicatorArrowDown, &p, &arrowOpt );
92    p.restore();
93}