diff options
Diffstat (limited to 'src/datablocks/rating.cpp')
-rw-r--r-- | src/datablocks/rating.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/datablocks/rating.cpp b/src/datablocks/rating.cpp new file mode 100644 index 0000000..83f0d9b --- /dev/null +++ b/src/datablocks/rating.cpp @@ -0,0 +1,86 @@ +/*************************************************************************** +* Copyright (C) 2005 by Jason Kivlighn * +* [email protected] * +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +***************************************************************************/ + +#include "rating.h" + +#include <tqpainter.h> +#include <tqbitmap.h> + +#include <kiconloader.h> + +TQPixmap Rating::starsPixmap( double stars_d, bool include_empty ) +{ + int stars = tqRound(stars_d * 2); //multiply by two to make it easier to work with half-stars + + TQPixmap star = UserIcon(TQString::fromLatin1("star_on")); + TQPixmap star_off; + if ( include_empty ) + star_off = UserIcon(TQString::fromLatin1("star_off")); + + int pixmapWidth; + if ( include_empty ) + pixmapWidth = 18*5; + else + pixmapWidth = 18*(stars/2)+((stars%2==1)?9:0); + + TQPixmap generatedPixmap(pixmapWidth,18); + + if ( !generatedPixmap.isNull() ) { //there aren't zero stars + generatedPixmap.fill(); + TQPainter painter( &generatedPixmap ); + + int pixmapWidth = 18*(stars/2)+((stars%2==1)?9:0); + if ( include_empty ) + painter.drawTiledPixmap(0,0,18*5,18,star_off); //fill with empty stars + painter.drawTiledPixmap(0,0,pixmapWidth,18,star); //write over the empty stars to show the rating + } + + generatedPixmap.setMask( generatedPixmap.createHeuristicMask() ); + + return generatedPixmap; +} + +void Rating::append( const RatingCriteria &rc ) +{ + ratingCriteriaList.append( rc ); +} + +double Rating::average() const +{ + double sum = 0; + int count = 0; + for ( RatingCriteriaList::const_iterator rc_it = ratingCriteriaList.begin(); rc_it != ratingCriteriaList.end(); ++rc_it ) { + count++; + sum += (*rc_it).stars; + } + + if ( count > 0 ) + return sum/count; + else + return -1; +} + + +double RatingList::average() +{ + int rating_total = 0; + double rating_sum = 0; + for ( RatingList::const_iterator rating_it = begin(); rating_it != end(); ++rating_it ) { + for ( RatingCriteriaList::const_iterator rc_it = (*rating_it).ratingCriteriaList.begin(); rc_it != (*rating_it).ratingCriteriaList.end(); ++rc_it ) { + rating_total++; + rating_sum += (*rc_it).stars; + } + } + + if ( rating_total > 0 ) + return rating_sum/rating_total; + else + return -1; +} |