00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "bookdelegate.h"
00025
00026 #include <QtGui>
00027
00028 BookDelegate::BookDelegate(QObject *parent)
00029 : QSqlRelationalDelegate(parent), star(QPixmap(":images/star.png"))
00030 {
00031 }
00032
00033 void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
00034 const QModelIndex &index) const
00035 {
00036 if (index.column() != 5) {
00037 QStyleOptionViewItem opt = option;
00038 opt.rect.adjust(0, 0, -1, -1);
00039 QSqlRelationalDelegate::paint(painter, opt, index);
00040 } else {
00041 const QAbstractItemModel *model = index.model();
00042 QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
00043 ? QPalette::Normal : QPalette::Disabled;
00044 if (option.state & QStyle::State_Selected)
00045 painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
00046
00047 int rating = model->data(index, Qt::DisplayRole).toInt();
00048 int width = star.width();
00049 int height = star.height();
00050 int x = option.rect.x();
00051 int y = option.rect.y() + (option.rect.height() / 2) - (height / 2);
00052 for (int i = 0; i < rating; ++i) {
00053 painter->drawPixmap(x, y, star);
00054 x += width;
00055 }
00056 drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1));
00057 }
00058
00059 QPen pen = painter->pen();
00060 painter->setPen(option.palette.color(QPalette::Mid));
00061 painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
00062 painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
00063 painter->setPen(pen);
00064 }
00065
00066 QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option,
00067 const QModelIndex &index) const
00068 {
00069 if (index.column() == 5)
00070 return QSize(5 * star.width(), star.height()) + QSize(1, 1);
00071
00072 return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1);
00073 }
00074
00075 bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
00076 const QStyleOptionViewItem &option,
00077 const QModelIndex &index)
00078 {
00079 if (index.column() != 5)
00080 return QSqlRelationalDelegate::editorEvent(event, model, option, index);
00081
00082 QMouseEvent *mouseEvent;
00083 int stars;
00084
00085 switch (event->type()) {
00086 case QEvent::MouseButtonPress:
00087 mouseEvent = static_cast<QMouseEvent*>(event);
00088 stars = qBound(0, int(0.7 + qreal(mouseEvent->pos().x()
00089 - option.rect.x()) / star.width()), 5);
00090 model->setData(index, QVariant(stars));
00091 break;
00092 default:
00093 break;
00094 }
00095
00096 return true;
00097 }
00098
00099 QWidget *BookDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
00100 const QModelIndex &index) const
00101 {
00102 if (index.column() != 4)
00103 return QSqlRelationalDelegate::createEditor(parent, option, index);
00104
00105
00106 QSpinBox *sb = new QSpinBox(parent);
00107 sb->setFrame(false);
00108 sb->setMaximum(2100);
00109 sb->setMinimum(-1000);
00110
00111 return sb;
00112 }
00113