File indexing completed on 2024-12-22 03:46:48
0001 /**************************************************************************** 0002 ** 0003 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 0004 ** Contact: Qt Software Information (qt-info@nokia.com) 0005 ** 0006 ** This file is part of the demonstration applications of the Qt Toolkit. 0007 ** 0008 ** $QT_BEGIN_LICENSE:LGPL$ 0009 ** No Commercial Usage 0010 ** This file contains pre-release code and may not be distributed. 0011 ** You may use this file in accordance with the terms and conditions 0012 ** contained in the either Technology Preview License Agreement or the 0013 ** Beta Release License Agreement. 0014 ** 0015 ** GNU Lesser General Public License Usage 0016 ** Alternatively, this file may be used under the terms of the GNU Lesser 0017 ** General Public License version 2.1 as published by the Free Software 0018 ** Foundation and appearing in the file LICENSE.LGPL included in the 0019 ** packaging of this file. Please review the following information to 0020 ** ensure the GNU Lesser General Public License version 2.1 requirements 0021 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 0022 ** 0023 ** In addition, as a special exception, Nokia gives you certain 0024 ** additional rights. These rights are described in the Nokia Qt LGPL 0025 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 0026 ** package. 0027 ** 0028 ** GNU General Public License Usage 0029 ** Alternatively, this file may be used under the terms of the GNU 0030 ** General Public License version 3.0 as published by the Free Software 0031 ** Foundation and appearing in the file LICENSE.GPL included in the 0032 ** packaging of this file. Please review the following information to 0033 ** ensure the GNU General Public License version 3.0 requirements will be 0034 ** met: http://www.gnu.org/copyleft/gpl.html. 0035 ** 0036 ** If you are unsure which license is appropriate for your use, please 0037 ** contact the sales department at qt-sales@nokia.com. 0038 ** $QT_END_LICENSE$ 0039 ** 0040 ****************************************************************************/ 0041 0042 #include "bookdelegate.h" 0043 0044 #include <QMouseEvent> 0045 #include <QPainter> 0046 #include <QSpinBox> 0047 0048 BookDelegate::BookDelegate(QObject *parent) 0049 : QSqlRelationalDelegate(parent) 0050 , star(QPixmap(":images/star.png")) 0051 { 0052 } 0053 0054 void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0055 { 0056 if (index.column() != 5) { 0057 QSqlRelationalDelegate::paint(painter, option, index); 0058 } else { 0059 const QAbstractItemModel *model = index.model(); 0060 QPalette::ColorGroup cg = 0061 (option.state & QStyle::State_Enabled) ? (option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive : QPalette::Disabled; 0062 0063 if (option.state & QStyle::State_Selected) 0064 painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight)); 0065 0066 int rating = model->data(index, Qt::DisplayRole).toInt(); 0067 int width = star.width(); 0068 int height = star.height(); 0069 int x = option.rect.x(); 0070 int y = option.rect.y() + (option.rect.height() / 2) - (height / 2); 0071 for (int i = 0; i < rating; ++i) { 0072 painter->drawPixmap(x, y, star); 0073 x += width; 0074 } 0075 } 0076 0077 QPen pen = painter->pen(); 0078 painter->setPen(option.palette.color(QPalette::Mid)); 0079 painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); 0080 painter->drawLine(option.rect.topRight(), option.rect.bottomRight()); 0081 painter->setPen(pen); 0082 } 0083 0084 QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 0085 { 0086 if (index.column() == 5) 0087 return QSize(5 * star.width(), star.height()) + QSize(1, 1); 0088 0089 return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1); // since we draw the grid ourselves 0090 } 0091 0092 bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) 0093 { 0094 if (index.column() != 5) 0095 return QSqlRelationalDelegate::editorEvent(event, model, option, index); 0096 0097 if (event->type() == QEvent::MouseButtonPress) { 0098 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); 0099 int stars = qBound(0, int(0.7 + qreal(mouseEvent->pos().x() - option.rect.x()) / star.width()), 5); 0100 model->setData(index, QVariant(stars)); 0101 return false; // so that the selection can change 0102 } 0103 0104 return true; 0105 } 0106 0107 QWidget *BookDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const 0108 { 0109 if (index.column() != 4) 0110 return QSqlRelationalDelegate::createEditor(parent, option, index); 0111 0112 // for editing the year, return a spinbox with a range from -1000 to 2100. 0113 QSpinBox *sb = new QSpinBox(parent); 0114 sb->setFrame(false); 0115 sb->setMaximum(2100); 0116 sb->setMinimum(-1000); 0117 0118 return sb; 0119 }