demos/books/bookdelegate.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the demonstration applications of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
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); // since we draw the grid ourselves
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)); // since we draw the grid ourselves
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); // since we draw the grid ourselves
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     // for editing the year, return a spinbox with a range from -1000 to 2100.
00106     QSpinBox *sb = new QSpinBox(parent);
00107     sb->setFrame(false);
00108     sb->setMaximum(2100);
00109     sb->setMinimum(-1000);
00110 
00111     return sb;
00112 }
00113 

Generated on Thu Mar 15 11:52:28 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1