File indexing completed on 2024-05-12 05:09:48
0001 /* 0002 * Copyright (C) 2010 Stefan Burnicki (stefan.burnicki@gmx.de) 0003 * <https://bangarangkde.wordpress.com> BANGARANG MEDIA PLAYER 0004 * Copyright 2011 Jörg Ehrichs <joerg.ehrichs@gmx.de> 0005 * 0006 * This program is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "ratingdelegate.h" 0021 #include "starrating.h" 0022 0023 #include <QApplication> 0024 #include <QPainter> 0025 0026 RatingDelegate::RatingDelegate(QObject* parent /* = 0 */) : QStyledItemDelegate(parent), m_maxRating(StarRating::MaxRating) { 0027 } 0028 0029 void RatingDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { 0030 QStyle* style = option.widget ? option.widget->style() : QApplication::style(); 0031 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget); 0032 0033 const int left = option.rect.left(); 0034 const int top = option.rect.top(); 0035 const int width = option.rect.width(); 0036 const int height = option.rect.height(); 0037 0038 //Create base pixmap 0039 QPixmap pixmap(width, height); 0040 pixmap.fill(Qt::transparent); 0041 QPainter p(&pixmap); 0042 p.translate(-option.rect.topLeft()); 0043 0044 //Paint rating 0045 const int rating = qRound(index.data(Qt::DisplayRole).toFloat()); 0046 StarRating starRating(rating, StarRating::Medium); 0047 starRating.setMaxRating(m_maxRating); 0048 QSize ratingSize = starRating.sizeHint(); 0049 int ratingLeft = left + 2; 0050 int ratingTop = top + (height - ratingSize.height())/2; 0051 QRect ratingRect = QRect(QPoint(ratingLeft, ratingTop), ratingSize); 0052 starRating.setPoint(ratingRect.topLeft()); 0053 starRating.paint(&p); 0054 0055 p.end(); 0056 0057 //Draw finished pixmap 0058 painter->drawPixmap(option.rect.topLeft(), pixmap); 0059 } 0060 0061 QSize RatingDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { 0062 Q_UNUSED(option); 0063 Q_UNUSED(index); 0064 0065 return StarRating::sizeHint(StarRating::Medium); 0066 }