source: trunk/demos/books/bookdelegate.cpp@ 158

Last change on this file since 158 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
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.
22**
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
26** package.
27**
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.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "bookdelegate.h"
43
44#include <QtGui>
45
46BookDelegate::BookDelegate(QObject *parent)
47 : QSqlRelationalDelegate(parent), star(QPixmap(":images/star.png"))
48{
49}
50
51void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
52 const QModelIndex &index) const
53{
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);
58 } else {
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;
62
63 if (option.state & QStyle::State_Selected)
64 painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
65
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);
73 x += width;
74 }
75 drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
76 }
77
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());
82 painter->setPen(pen);
83}
84
85QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option,
86 const QModelIndex &index) const
87{
88 if (index.column() == 5)
89 return QSize(5 * star.width(), star.height()) + QSize(1, 1);
90
91 return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1); // since we draw the grid ourselves
92}
93
94bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
95 const QStyleOptionViewItem &option,
96 const QModelIndex &index)
97{
98 if (index.column() != 5)
99 return QSqlRelationalDelegate::editorEvent(event, model, option, index);
100
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
107 }
108
109 return true;
110}
111
112QWidget *BookDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
113 const QModelIndex &index) const
114{
115 if (index.column() != 4)
116 return QSqlRelationalDelegate::createEditor(parent, option, index);
117
118 // for editing the year, return a spinbox with a range from -1000 to 2100.
119 QSpinBox *sb = new QSpinBox(parent);
120 sb->setFrame(false);
121 sb->setMaximum(2100);
122 sb->setMinimum(-1000);
123
124 return sb;
125}
126
Note: See TracBrowser for help on using the repository browser.