|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the demonstration applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "bookdelegate.h" |
|
43 |
|
44 #include <QtGui> |
|
45 |
|
46 BookDelegate::BookDelegate(QObject *parent) |
|
47 : QSqlRelationalDelegate(parent), star(QPixmap(":images/star.png")) |
|
48 { |
|
49 } |
|
50 |
|
51 void 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 |
|
85 QSize 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 |
|
94 bool 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 |
|
112 QWidget *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 |