~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to kicker/kicker/ui/appletitemdelegate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
Import upstream version 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
  * This file is part of the KDE project
 
3
  * Copyright (C) 2007, 2006 Rafael Fernández López <ereslibre@gmail.com>
 
4
  *
 
5
  * This library is free software; you can redistribute it and/or
 
6
  * modify it under the terms of the GNU Library General Public
 
7
  * License version 2 as published by the Free Software Foundation.
 
8
  *
 
9
  * This library 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
  * Library General Public License for more details.
 
13
  *
 
14
  * You should have received a copy of the GNU Library General Public License
 
15
  * along with this library; see the file COPYING.LIB.  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 "appletitemdelegate.h"
 
21
 
 
22
class AppletItemDelegate::Private
 
23
{
 
24
public:
 
25
    QString title(const QModelIndex &index) const;
 
26
    QString description(const QModelIndex &index) const;
 
27
    QPixmap icon(const QModelIndex &index, int width, int height) const; 
 
28
    int calculateCenter(const QRect &rect, int pixmapHeight) const;
 
29
 
 
30
    int iconWidth;
 
31
    int iconHeight;
 
32
    int minimumItemWidth;
 
33
    int leftMargin;
 
34
    int rightMargin;
 
35
    int separatorPixels;
 
36
};
 
37
 
 
38
QString AppletItemDelegate::Private::title(const QModelIndex &index) const
 
39
{
 
40
    return index.model()->data(index, Qt::DisplayRole).toString();
 
41
}
 
42
 
 
43
QString AppletItemDelegate::Private::description(const QModelIndex &index) const
 
44
{
 
45
    return index.model()->data(index, SecondaryDisplayRole).toString();
 
46
}
 
47
 
 
48
QPixmap AppletItemDelegate::Private::icon(const QModelIndex &index, int width, int height) const
 
49
{
 
50
    QVariant icon = index.model()->data(index, Qt::DecorationRole);
 
51
 
 
52
    return icon.value<QIcon>().pixmap(width, height);
 
53
}
 
54
 
 
55
int AppletItemDelegate::Private::calculateCenter(const QRect &rect, int pixmapHeight) const
 
56
{
 
57
    return (rect.height() / 2) - (pixmapHeight / 2);
 
58
}
 
59
 
 
60
AppletItemDelegate::AppletItemDelegate(QObject *parent)
 
61
    : QItemDelegate(parent)
 
62
    , d(new Private)
 
63
{
 
64
}
 
65
 
 
66
AppletItemDelegate::~AppletItemDelegate()
 
67
{
 
68
    delete d;
 
69
}
 
70
 
 
71
void AppletItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 
72
{
 
73
    QFontMetrics fontMetrics = painter->fontMetrics();
 
74
 
 
75
    QColor unselectedTextColor = option.palette.text().color();
 
76
    QColor selectedTextColor = option.palette.highlightedText().color();
 
77
    QPen currentPen = painter->pen();
 
78
    QPen unselectedPen = QPen(currentPen);
 
79
    QPen selectedPen = QPen(currentPen);
 
80
 
 
81
    unselectedPen.setColor(unselectedTextColor);
 
82
    selectedPen.setColor(selectedTextColor);
 
83
 
 
84
    if (option.state & QStyle::State_Selected)
 
85
    {
 
86
        painter->fillRect(option.rect, option.palette.highlight());
 
87
        painter->setPen(selectedPen);
 
88
    }
 
89
    else
 
90
    {
 
91
        painter->setPen(unselectedPen);
 
92
    }
 
93
 
 
94
    QPixmap iconPixmap = d->icon(index, d->iconWidth, d->iconHeight);
 
95
 
 
96
    QFont title(painter->font());
 
97
    QFont previousFont(painter->font());
 
98
 
 
99
    title.setPointSize(title.pointSize() + 2);
 
100
    title.setWeight(QFont::Bold);
 
101
 
 
102
    painter->save();
 
103
 
 
104
    painter->setRenderHint(QPainter::Antialiasing, true);
 
105
 
 
106
    painter->setFont(title);
 
107
 
 
108
    QString display = painter->fontMetrics().elidedText(d->title(index), Qt::ElideRight, option.rect.width() - d->leftMargin - d->rightMargin - iconPixmap.width() - d->separatorPixels);
 
109
 
 
110
    QString secondaryDisplay = fontMetrics.elidedText(d->description(index), Qt::ElideRight, option.rect.width() - d->leftMargin - d->rightMargin - iconPixmap.width() - d->separatorPixels);
 
111
 
 
112
    painter->drawText(d->leftMargin + d->separatorPixels + iconPixmap.width(), d->separatorPixels + option.rect.top(), painter->fontMetrics().width(display), painter->fontMetrics().height(), Qt::AlignLeft, display);
 
113
 
 
114
    painter->setFont(previousFont);
 
115
 
 
116
    painter->drawText(d->leftMargin + d->separatorPixels + iconPixmap.width(), option.rect.height() - d->separatorPixels - fontMetrics.height() + option.rect.top(), painter->fontMetrics().width(secondaryDisplay), painter->fontMetrics().height(), Qt::AlignLeft, secondaryDisplay);
 
117
 
 
118
    painter->drawPixmap(d->leftMargin, d->calculateCenter(option.rect, iconPixmap.height()) + option.rect.top(), iconPixmap);
 
119
 
 
120
    painter->restore();
 
121
 
 
122
}
 
123
 
 
124
QSize AppletItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
 
125
{
 
126
    int itemWidth = qMax(option.rect.width(), d->minimumItemWidth);
 
127
    int itemHeight = (d->separatorPixels * 2) + d->iconHeight;
 
128
 
 
129
    return QSize(itemWidth, itemHeight);
 
130
}
 
131
 
 
132
void AppletItemDelegate::setIconSize(int width, int height)
 
133
{
 
134
    d->iconWidth = width;
 
135
    d->iconHeight = height;
 
136
}
 
137
 
 
138
void AppletItemDelegate::setMinimumItemWidth(int minimumItemWidth)
 
139
{
 
140
    d->minimumItemWidth = minimumItemWidth;
 
141
}
 
142
 
 
143
void AppletItemDelegate::setLeftMargin(int leftMargin)
 
144
{
 
145
    d->leftMargin = leftMargin;
 
146
}
 
147
 
 
148
void AppletItemDelegate::setRightMargin(int rightMargin)
 
149
{
 
150
    d->rightMargin = rightMargin;
 
151
}
 
152
 
 
153
void AppletItemDelegate::setSeparatorPixels(int separatorPixels)
 
154
{
 
155
    d->separatorPixels = separatorPixels;
 
156
}
 
157
 
 
158
#include "appletitemdelegate.moc"