2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "bookdelegate.h"
|
|
|
|
|
2011-05-08 07:13:40 +00:00
|
|
|
#include <QtWidgets>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
BookDelegate::BookDelegate(QObject *parent)
|
|
|
|
: QSqlRelationalDelegate(parent), star(QPixmap(":images/star.png"))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.column() != 5) {
|
2020-09-03 08:51:40 +00:00
|
|
|
QSqlRelationalDelegate::paint(painter, option, index);
|
2011-04-27 10:05:43 +00:00
|
|
|
} else {
|
|
|
|
const QAbstractItemModel *model = index.model();
|
|
|
|
QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
|
2018-06-05 11:13:38 +00:00
|
|
|
(option.state & QStyle::State_Active) ?
|
|
|
|
QPalette::Normal :
|
|
|
|
QPalette::Inactive :
|
|
|
|
QPalette::Disabled;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if (option.state & QStyle::State_Selected)
|
2018-06-05 11:13:38 +00:00
|
|
|
painter->fillRect(
|
|
|
|
option.rect,
|
|
|
|
option.palette.color(cg, QPalette::Highlight));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
int rating = model->data(index, Qt::DisplayRole).toInt();
|
|
|
|
int width = star.width();
|
|
|
|
int height = star.height();
|
|
|
|
int x = option.rect.x();
|
|
|
|
int y = option.rect.y() + (option.rect.height() / 2) - (height / 2);
|
|
|
|
for (int i = 0; i < rating; ++i) {
|
|
|
|
painter->drawPixmap(x, y, star);
|
|
|
|
x += width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPen pen = painter->pen();
|
|
|
|
painter->setPen(option.palette.color(QPalette::Mid));
|
|
|
|
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
|
|
|
|
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
|
|
|
painter->setPen(pen);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.column() == 5)
|
|
|
|
return QSize(5 * star.width(), star.height()) + QSize(1, 1);
|
2018-06-05 11:13:38 +00:00
|
|
|
// Since we draw the grid ourselves:
|
|
|
|
return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
|
|
|
|
const QStyleOptionViewItem &option,
|
|
|
|
const QModelIndex &index)
|
|
|
|
{
|
|
|
|
if (index.column() != 5)
|
|
|
|
return QSqlRelationalDelegate::editorEvent(event, model, option, index);
|
|
|
|
|
|
|
|
if (event->type() == QEvent::MouseButtonPress) {
|
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
|
2020-06-04 16:07:06 +00:00
|
|
|
int stars = qBound(0, int(0.7 + qreal(mouseEvent->position().toPoint().x()
|
2011-04-27 10:05:43 +00:00
|
|
|
- option.rect.x()) / star.width()), 5);
|
|
|
|
model->setData(index, QVariant(stars));
|
2018-06-05 11:13:38 +00:00
|
|
|
// So that the selection can change:
|
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-05 11:13:38 +00:00
|
|
|
QWidget *BookDelegate::createEditor(QWidget *parent,
|
|
|
|
const QStyleOptionViewItem &option,
|
2011-04-27 10:05:43 +00:00
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.column() != 4)
|
|
|
|
return QSqlRelationalDelegate::createEditor(parent, option, index);
|
|
|
|
|
2018-06-05 11:13:38 +00:00
|
|
|
// For editing the year, return a spinbox with a range from -1000 to 2100.
|
2011-04-27 10:05:43 +00:00
|
|
|
QSpinBox *sb = new QSpinBox(parent);
|
|
|
|
sb->setFrame(false);
|
|
|
|
sb->setMaximum(2100);
|
|
|
|
sb->setMinimum(-1000);
|
|
|
|
|
|
|
|
return sb;
|
|
|
|
}
|