File indexing completed on 2024-05-12 05:09:49

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 "starrating.h"
0021 
0022 #include <QPainter>
0023 #include <QSize>
0024 #include <QRect>
0025 #include <QIcon>
0026 
0027 StarRating::StarRating(int rating, int size, QPoint point) : m_rating(rating), m_maxRating(MaxRating), m_point(point) {
0028   setSize(size);
0029 }
0030 
0031 bool StarRating::valid(int rating) const {
0032   return (rating >= MinRating && rating <= MaxRating);
0033 }
0034 
0035 void StarRating::paint(QPainter* painter) {
0036   painter->save();
0037 
0038   painter->translate(m_point + QPoint(StarRating::Margin, StarRating::Margin));
0039   for(int i = 1; i <= m_rating; ++i) {
0040     painter->drawPixmap(((i - 1) * (m_starSize + 2*Margin)), 0, m_starNormal);
0041   }
0042 
0043   painter->restore();
0044 }
0045 
0046 void StarRating::setRating(int rating_) {
0047   m_rating = qMin(rating_, m_maxRating);
0048 }
0049 
0050 void StarRating::setMaxRating(int maxRating_) {
0051   m_maxRating = qMin(int(MaxRating), maxRating_);
0052 }
0053 
0054 void StarRating::setSize(int size) {
0055   int px = (size < Small) ? Small : size;
0056   QSize sz = QSize(px, px);
0057   QIcon icon = QIcon(QLatin1String(":/icons/star_on"));
0058   m_starNormal = icon.pixmap(sz);
0059   m_starSize = m_starNormal.size().width(); //maybe we didn't get the full size
0060   sz = QSize(m_starSize, m_starSize);
0061   m_starInactive = icon.pixmap(sz, QIcon::Disabled);
0062 }
0063 
0064 //static
0065 QSize StarRating::sizeHint(int size) {
0066   // *__*__*__*__*   +  StarRating::Margin around it
0067   const int bothMargin = (StarRating::Margin * 2);
0068   return QSize( 5 * (size + 2) - 2, size) + QSize(bothMargin, bothMargin);
0069 }