1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: Qt Software Information (qt-info@nokia.com)
6
** This file is part of the demonstration applications of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
10
** This file contains pre-release code and may not be distributed.
11
** You may use this file in accordance with the terms and conditions
12
** contained in the either Technology Preview License Agreement or the
13
** Beta Release License Agreement.
15
** GNU Lesser General Public License Usage
16
** Alternatively, this file may be used under the terms of the GNU Lesser
17
** General Public License version 2.1 as published by the Free Software
18
** Foundation and appearing in the file LICENSE.LGPL included in the
19
** packaging of this file. Please review the following information to
20
** ensure the GNU Lesser General Public License version 2.1 requirements
21
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
** In addition, as a special exception, Nokia gives you certain
24
** additional rights. These rights are described in the Nokia Qt LGPL
25
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
28
** GNU General Public License Usage
29
** Alternatively, this file may be used under the terms of the GNU
30
** General Public License version 3.0 as published by the Free Software
31
** Foundation and appearing in the file LICENSE.GPL included in the
32
** packaging of this file. Please review the following information to
33
** ensure the GNU General Public License version 3.0 requirements will be
34
** met: http://www.gnu.org/copyleft/gpl.html.
36
** If you are unsure which license is appropriate for your use, please
37
** contact the sales department at qt-sales@nokia.com.
40
****************************************************************************/
42
#include "bookdelegate.h"
46
BookDelegate::BookDelegate(QObject *parent)
47
: QSqlRelationalDelegate(parent), star(QPixmap(":images/star.png"))
51
void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
52
const QModelIndex &index) const
54
if (index.column() != 5) {
55
QStyleOptionViewItemV3 opt = option;
56
opt.rect.adjust(0, 0, -1, -1); // since we draw the grid ourselves
57
QSqlRelationalDelegate::paint(painter, opt, index);
59
const QAbstractItemModel *model = index.model();
60
QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
61
(option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive : QPalette::Disabled;
63
if (option.state & QStyle::State_Selected)
64
painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
66
int rating = model->data(index, Qt::DisplayRole).toInt();
67
int width = star.width();
68
int height = star.height();
69
int x = option.rect.x();
70
int y = option.rect.y() + (option.rect.height() / 2) - (height / 2);
71
for (int i = 0; i < rating; ++i) {
72
painter->drawPixmap(x, y, star);
75
drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
78
QPen pen = painter->pen();
79
painter->setPen(option.palette.color(QPalette::Mid));
80
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
81
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
85
QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option,
86
const QModelIndex &index) const
88
if (index.column() == 5)
89
return QSize(5 * star.width(), star.height()) + QSize(1, 1);
91
return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1); // since we draw the grid ourselves
94
bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
95
const QStyleOptionViewItem &option,
96
const QModelIndex &index)
98
if (index.column() != 5)
99
return QSqlRelationalDelegate::editorEvent(event, model, option, index);
101
if (event->type() == QEvent::MouseButtonPress) {
102
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
103
int stars = qBound(0, int(0.7 + qreal(mouseEvent->pos().x()
104
- option.rect.x()) / star.width()), 5);
105
model->setData(index, QVariant(stars));
106
return false; //so that the selection can change
112
QWidget *BookDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
113
const QModelIndex &index) const
115
if (index.column() != 4)
116
return QSqlRelationalDelegate::createEditor(parent, option, index);
118
// for editing the year, return a spinbox with a range from -1000 to 2100.
119
QSpinBox *sb = new QSpinBox(parent);
121
sb->setMaximum(2100);
122
sb->setMinimum(-1000);