xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/database/bookdelegate.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "bookdelegate.h"
52 
53 #include <QtWidgets>
54 
BookDelegate(QObject * parent)55 BookDelegate::BookDelegate(QObject *parent)
56     : QSqlRelationalDelegate(parent), star(QPixmap(":images/star.png"))
57 {
58 }
59 
paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const60 void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
61                            const QModelIndex &index) const
62 {
63     if (index.column() != 5) {
64         QStyleOptionViewItem opt = option;
65         // Since we draw the grid ourselves:
66         opt.rect.adjust(0, 0, -1, -1);
67         QSqlRelationalDelegate::paint(painter, opt, index);
68     } else {
69         const QAbstractItemModel *model = index.model();
70         QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
71             (option.state & QStyle::State_Active) ?
72                         QPalette::Normal :
73                         QPalette::Inactive :
74                         QPalette::Disabled;
75 
76         if (option.state & QStyle::State_Selected)
77             painter->fillRect(
78                         option.rect,
79                         option.palette.color(cg, QPalette::Highlight));
80 
81         int rating = model->data(index, Qt::DisplayRole).toInt();
82         int width = star.width();
83         int height = star.height();
84         int x = option.rect.x();
85         int y = option.rect.y() + (option.rect.height() / 2) - (height / 2);
86         for (int i = 0; i < rating; ++i) {
87             painter->drawPixmap(x, y, star);
88             x += width;
89         }
90         // Since we draw the grid ourselves:
91         drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1));
92     }
93 
94     QPen pen = painter->pen();
95     painter->setPen(option.palette.color(QPalette::Mid));
96     painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
97     painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
98     painter->setPen(pen);
99 }
100 
sizeHint(const QStyleOptionViewItem & option,const QModelIndex & index) const101 QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option,
102                                  const QModelIndex &index) const
103 {
104     if (index.column() == 5)
105         return QSize(5 * star.width(), star.height()) + QSize(1, 1);
106     // Since we draw the grid ourselves:
107     return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1);
108 }
109 
editorEvent(QEvent * event,QAbstractItemModel * model,const QStyleOptionViewItem & option,const QModelIndex & index)110 bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
111                                const QStyleOptionViewItem &option,
112                                const QModelIndex &index)
113 {
114     if (index.column() != 5)
115         return QSqlRelationalDelegate::editorEvent(event, model, option, index);
116 
117     if (event->type() == QEvent::MouseButtonPress) {
118         QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
119         int stars = qBound(0, int(0.7 + qreal(mouseEvent->pos().x()
120             - option.rect.x()) / star.width()), 5);
121         model->setData(index, QVariant(stars));
122         // So that the selection can change:
123         return false;
124     }
125 
126     return true;
127 }
128 
createEditor(QWidget * parent,const QStyleOptionViewItem & option,const QModelIndex & index) const129 QWidget *BookDelegate::createEditor(QWidget *parent,
130                                     const QStyleOptionViewItem &option,
131                                     const QModelIndex &index) const
132 {
133     if (index.column() != 4)
134         return QSqlRelationalDelegate::createEditor(parent, option, index);
135 
136     // For editing the year, return a spinbox with a range from -1000 to 2100.
137     QSpinBox *sb = new QSpinBox(parent);
138     sb->setFrame(false);
139     sb->setMaximum(2100);
140     sb->setMinimum(-1000);
141 
142     return sb;
143 }
144