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