PageRenderTime 52ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 11ms app.codeStats 1ms

/src/settingslistdelegate.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 74 lines | 43 code | 12 blank | 19 comment | 4 complexity | 916a4511369205b6f0f91367f46c3225 MD5 | raw file
 1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 2 *
 3 *   Copyright 2011, Michael Zanetti <mzanetti@kde.org>
 4 *   Copyright 2011, Leo Franchi <lfranchi@kde.org>
 5 *
 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.
10 *
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.
15 *
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/>.
18 */
19
20#include "settingslistdelegate.h"
21#include "utils/logger.h"
22
23#include <QPainter>
24#include <QIcon>
25#include <QApplication>
26
27SettingsListDelegate::SettingsListDelegate(QObject *parent) :
28    QStyledItemDelegate(parent)
29{
30}
31
32
33void SettingsListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
34{
35    painter->save();
36
37    QStyleOptionViewItemV4 opt = option;
38    initStyleOption( &opt, QModelIndex() );
39    qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter );
40
41#if defined(Q_WS_MAC) || defined(Q_WS_WIN)
42    // On mac draw our own selection rect as we don't get one from osx (around the whole icon or around just text)
43    if ( opt.state & QStyle::State_Selected )
44    {
45        painter->setRenderHint( QPainter::Antialiasing );
46
47        QPainterPath p;
48        p.addRoundedRect( opt.rect.adjusted( 2, 1, -1, -1 ), 5, 5 );
49
50        QColor fill( 214, 214, 214 );
51        QColor border( 107, 107, 107 );
52        painter->setPen( border );
53        painter->drawPath( p );
54        painter->fillPath( p, fill );
55    }
56#else
57    if ( ( option.state & QStyle::State_Selected ) == QStyle::State_Selected )
58    {
59        painter->setPen( option.palette.color( QPalette::HighlightedText ) );
60    }
61#endif
62
63    int horizontalOffset = ( option.rect.width() - option.decorationSize.width() ) /2;
64    QRect iconRect = option.rect.adjusted( horizontalOffset, 6, -horizontalOffset, -option.rect.height() + 6 + option.decorationSize.height() );
65    QPixmap avatar = index.data( Qt::DecorationRole ).value<QIcon>().pixmap( iconRect.size() );
66    painter->drawPixmap( iconRect, avatar );
67
68    QRect textRect = option.rect.adjusted( 6, iconRect.height() + 8, -6, 0 );
69    QString text = painter->fontMetrics().elidedText( index.data( Qt::DisplayRole ).toString(), Qt::ElideRight, textRect.width() );
70    QTextOption to( Qt::AlignHCenter );
71    painter->drawText( textRect, text, to );
72
73    painter->restore();
74}