~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/input/xcursor/itemdelegate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2006-2007 Fredrik Höglund <fredrik@kde.org>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public
 
6
 * License version 2 or at your option version 3 as published 
 
7
 * by the Free Software Foundation.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; see the file COPYING.  If not, write to
 
16
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 * Boston, MA 02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "itemdelegate.h"
 
21
#include "cursortheme.h"
 
22
 
 
23
#include <QApplication>
 
24
#include <QModelIndex>
 
25
#include <QPainter>
 
26
 
 
27
namespace
 
28
{
 
29
    const int decorationMargin = 8;
 
30
}
 
31
 
 
32
 
 
33
ItemDelegate::ItemDelegate(QObject *parent)
 
34
    : QAbstractItemDelegate(parent)
 
35
{
 
36
}
 
37
 
 
38
 
 
39
ItemDelegate::~ItemDelegate()
 
40
{
 
41
}
 
42
 
 
43
 
 
44
QString ItemDelegate::firstLine(const QModelIndex &index) const
 
45
{
 
46
    if (index.isValid())
 
47
        return index.model()->data(index, Qt::DisplayRole).toString();
 
48
 
 
49
    return QString();
 
50
}
 
51
 
 
52
 
 
53
QString ItemDelegate::secondLine(const QModelIndex &index) const
 
54
{
 
55
    if (index.isValid())
 
56
        return index.model()->data(index, CursorTheme::DisplayDetailRole).toString();
 
57
 
 
58
        return QString();
 
59
}
 
60
 
 
61
 
 
62
QPixmap ItemDelegate::decoration(const QModelIndex &index) const
 
63
{
 
64
    if (index.isValid())
 
65
        return qvariant_cast<QPixmap>(index.model()->data(index, Qt::DecorationRole));
 
66
 
 
67
    return QPixmap();
 
68
}
 
69
 
 
70
 
 
71
QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
 
72
{
 
73
    if (!index.isValid())
 
74
        return QSize();
 
75
 
 
76
    QFont normalfont = option.font;
 
77
    QFont boldfont = normalfont;
 
78
    boldfont.setBold(true);
 
79
 
 
80
    // Extract the items we want to measure
 
81
    QString firstRow   = firstLine(index);
 
82
    QString secondRow  = secondLine(index);
 
83
 
 
84
    // Compute the height
 
85
    QFontMetrics fm1(boldfont);
 
86
    QFontMetrics fm2(normalfont);
 
87
    int height = fm1.lineSpacing() + fm2.lineSpacing();
 
88
    height = qMax(height, option.decorationSize.height());
 
89
 
 
90
    // Compute the text width
 
91
    int width = fm1.width(firstRow);
 
92
    width = qMax(width, fm2.width(secondRow));
 
93
 
 
94
    // Add decoration width + margin
 
95
    width += option.decorationSize.width() + decorationMargin;
 
96
 
 
97
    return QSize(width, height + 16);
 
98
}
 
99
 
 
100
 
 
101
QPalette::ColorRole ItemDelegate::foregroundRole(const QStyleOptionViewItem &option, const QModelIndex &index) const
 
102
{
 
103
    Q_UNUSED(index)
 
104
 
 
105
    if (option.state & QStyle::State_Selected)
 
106
        return QPalette::HighlightedText;
 
107
 
 
108
    return QPalette::Text;
 
109
}
 
110
 
 
111
 
 
112
void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 
113
{
 
114
    if (!index.isValid())
 
115
        return;
 
116
 
 
117
    painter->save();
 
118
 
 
119
    QFont normalfont = option.font;
 
120
    QFont boldfont = normalfont;
 
121
    boldfont.setBold(true);
 
122
 
 
123
    QString firstRow  = firstLine(index);
 
124
    QString secondRow = secondLine(index);
 
125
    QPixmap pixmap    = decoration(index);
 
126
 
 
127
    QColor textcol = option.palette.color(foregroundRole(option, index));
 
128
 
 
129
    // Draw the background
 
130
    QStyleOptionViewItemV4 opt = option;
 
131
    QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
 
132
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
 
133
 
 
134
    // Draw the icon
 
135
    int x = option.rect.left() + (option.decorationSize.width() - pixmap.width() + decorationMargin) / 2;
 
136
    int y = option.rect.top() + (option.rect.height() - pixmap.height()) / 2;
 
137
 
 
138
    QRect pixmapRect = QStyle::visualRect(option.direction, option.rect,
 
139
                                          QRect(x, y, pixmap.width(), pixmap.height()));
 
140
 
 
141
    painter->drawPixmap(pixmapRect.x(), pixmapRect.y(), pixmap);
 
142
 
 
143
    // Draw the text
 
144
    QFontMetrics fm1(boldfont);
 
145
    QFontMetrics fm2(normalfont);
 
146
 
 
147
    int textAreaHeight = fm1.lineSpacing() + fm2.lineSpacing();
 
148
 
 
149
    x = option.rect.left() + option.decorationSize.width() + decorationMargin;
 
150
    int y1 = option.rect.top() + (option.rect.height() - textAreaHeight) / 2;
 
151
    int y2 = y1 + fm1.lineSpacing();
 
152
 
 
153
    QRect firstRowRect = QStyle::visualRect(option.direction, option.rect,
 
154
                                            QRect(x, y1, fm1.width(firstRow), fm1.lineSpacing()));
 
155
 
 
156
    QRect secondRowRect = QStyle::visualRect(option.direction, option.rect,
 
157
                                             QRect(x, y2, fm2.width(secondRow), fm2.lineSpacing()));
 
158
 
 
159
    painter->setPen(textcol);
 
160
 
 
161
    // First line
 
162
    painter->setFont(boldfont);
 
163
    painter->drawText(firstRowRect, Qt::AlignCenter, firstRow);
 
164
 
 
165
    // Second line
 
166
    painter->setFont(normalfont);
 
167
    painter->drawText(secondRowRect, Qt::AlignCenter, secondRow);
 
168
 
 
169
    painter->restore();
 
170
}
 
171