Initial checkin
[opensuse:qtobs.git] / statusdelegate.cpp
1 /*
2  *  Qt OBS - A Qt based OBS client
3  *
4  *  Copyright (C) 2010 Novell Inc, Klaas Freitag <freitag@suse.de>
5  *
6  *  This program 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 2 of the License, or
9  *  (at your option) version 3.
10  *
11  *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  --
20  *  Written by Klaas Freitag <freitag@suse.de>
21  *  Contributors:
22  */
23
24 #include "statusdelegate.h"
25
26 #include <QtGui>
27
28 #include "statusdelegate.h"
29
30 StatusDelegate::StatusDelegate(QObject *parent)
31     : QStyledItemDelegate(parent)
32 {
33
34 }
35
36
37 void StatusDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
38                            const QModelIndex &index) const
39 {
40   painter->save();
41   if (option.state & QStyle::State_Selected)
42     painter->fillRect(option.rect, option.palette.highlight());
43 #if 0
44   int size = qMin(option.rect.width(), option.rect.height());
45   int brightness = index.model()->data( index, Qt::DisplayRole ).toInt();
46   double radius = (size/2.0) - (brightness/255.0 * size/2.0);
47   if (radius == 0.0)
48     return;
49
50   painter->save();
51   painter->setRenderHint(QPainter::Antialiasing, true);
52   painter->setPen(Qt::NoPen);
53   if (option.state & QStyle::State_Selected)
54     painter->setBrush(option.palette.highlightedText());
55   else
56     painter->setBrush(QBrush(Qt::black));
57
58   painter->drawEllipse(QRectF(option.rect.x() + option.rect.width()/2 - radius,
59                               option.rect.y() + option.rect.height()/2 - radius,
60                               2*radius, 2*radius));
61 #endif
62   QFontMetrics fmetrics = painter->fontMetrics();
63   painter->drawText(option.rect.x(), option.rect.y()+fmetrics.height(),
64                     index.model()->data( index, Qt::DisplayRole ).toString() );
65   painter->restore();
66 }
67
68 QSize StatusDelegate::sizeHint(const QStyleOptionViewItem& option,
69                                const QModelIndex& index ) const
70 {
71     QString val = index.model()->data( index, Qt::DisplayRole ).toString();
72     QFontMetrics fm = option.fontMetrics;
73
74     return QSize( 1.2*fm.width( val ), 1.2*fm.height() );
75 }