![]() |
Mixxx
|
00001 /*************************************************************************** 00002 starrating.cpp 00003 ------------------- 00004 copyright : (C) 2010 Tobias Rafreider 00005 copyright : (C) 2009 Nokia Corporation 00006 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 00019 #include <QtGui> 00020 #include <math.h> 00021 00022 #include "starrating.h" 00023 00024 const int PaintingScaleFactor = 15; 00025 00026 00027 00028 StarRating::StarRating(int starCount, int maxStarCount) 00029 { 00030 m_myStarCount = starCount; 00031 m_myMaxStarCount = maxStarCount; 00032 00033 m_starPolygon << QPointF(1.0, 0.5); 00034 for (int i = 1; i < 5; ++i) 00035 m_starPolygon << QPointF(0.5 + 0.5 * cos(0.8 * i * 3.14), 0.5 + 0.5 * sin(0.8 * i * 3.14)); 00036 m_diamondPolygon << QPointF(0.4, 0.5) << QPointF(0.5, 0.4) << QPointF(0.6, 0.5) << QPointF(0.5, 0.6) << QPointF(0.4, 0.5); 00037 } 00038 00039 QSize StarRating::sizeHint() const 00040 { 00041 return PaintingScaleFactor * QSize(m_myMaxStarCount, 1); 00042 } 00043 00044 /* 00045 * function paints the stars in this StarRating object on a paint device 00046 */ 00047 void StarRating::paint(QPainter *painter, const QRect &rect, const QPalette &palette, EditMode mode) const 00048 { 00049 painter->save(); 00050 00051 painter->setRenderHint(QPainter::Antialiasing, true); 00052 painter->setPen(Qt::NoPen); 00053 00054 // Workaround for painting issue. If we are editable, assume we are 00055 // selected, so use the highlight and hightlightedText colors. 00056 if (mode == Editable) { 00057 painter->fillRect(rect, palette.highlight()); 00058 painter->setBrush(palette.highlightedText()); 00059 } 00060 00061 int yOffset = (rect.height() - PaintingScaleFactor) / 2; 00062 painter->translate(rect.x(), rect.y() + yOffset); 00063 painter->scale(PaintingScaleFactor, PaintingScaleFactor); 00064 00065 for (int i = 0; i < m_myMaxStarCount; ++i) { 00066 if (i < m_myStarCount) { 00067 painter->drawPolygon(m_starPolygon, Qt::WindingFill); 00068 } else { 00069 painter->drawPolygon(m_diamondPolygon, Qt::WindingFill); 00070 } 00071 painter->translate(1.0, 0.0); 00072 } 00073 00074 painter->restore(); 00075 }