[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    [PATCH] RatingPainter and RatingWidget take 2
From:       Sebastian =?iso-8859-1?q?Tr=FCg?= <strueg () mandriva ! com>
Date:       2008-02-27 12:00:52
Message-ID: 200802271300.52412.strueg () mandriva ! com
[Download RAW message or body]

Hi guys,

please find attached the source for the enhanced rating handling. As told 
before, the ratingpainter is already used in libnepomuk (through 
KRatingWidget), in Dolphin to draw categorization headers, and in 
playground/base/nepomuk to draw ratings for search results.

Open question for me is: should I keep it in the Nepomuk namespace or rename 
it to KRatingPainter seeing that it could be using without Nepomuk.

Awaiting your input. :)

Cheers,
Sebastian

["ratingpainter.cpp" (text/x-c++src)]

/*
   This file is part of the Nepomuk KDE project.
   Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
 */

#include "ratingpainter.h"

#include <QtGui/QPainter>
#include <QtGui/QPixmap>
#include <QtGui/QIcon>
#include <QtCore/QRect>
#include <QtCore/QPoint>

#include <kicon.h>
#include <kiconeffect.h>
#include <kdebug.h>


class Nepomuk::RatingPainter::Private
{
public:
    Private()
        : maxRating(10),
          bHalfSteps(true),
          alignment(Qt::AlignCenter),
          direction(Qt::LeftToRight),
          spacing(0) {
    }

    QPixmap getPixmap( int size );

    int maxRating;
    QIcon icon;
    bool bHalfSteps;
    Qt::Alignment alignment;
    Qt::LayoutDirection direction;
    QPixmap customPixmap;
    int spacing;
};


QPixmap Nepomuk::RatingPainter::Private::getPixmap( int size )
{
    if ( !customPixmap.isNull() ) {
        return customPixmap.scaled( QSize( size, size ) );
    }
    else {
        QIcon _icon( icon );
        if ( _icon.isNull() ) {
            _icon = KIcon( "rating" );
        }
        return _icon.pixmap( size );
    }
}


Nepomuk::RatingPainter::RatingPainter()
    : d(new Private())
{
}


Nepomuk::RatingPainter::~RatingPainter()
{
    delete d;
}


int Nepomuk::RatingPainter::maxRating() const
{
    return d->maxRating;
}


bool Nepomuk::RatingPainter::halfStepsEnabled() const
{
    return d->bHalfSteps;
}


Qt::Alignment Nepomuk::RatingPainter::alignment() const
{
    return d->alignment;
}


Qt::LayoutDirection Nepomuk::RatingPainter::layoutDirection() const
{
    return d->direction;
}


QIcon Nepomuk::RatingPainter::icon() const
{
    return d->icon;
}


QPixmap Nepomuk::RatingPainter::customPixmap() const
{
    return d->customPixmap;
}


int Nepomuk::RatingPainter::spacing() const
{
    return d->spacing;
}


void Nepomuk::RatingPainter::setMaxRating( int max )
{
    d->maxRating = max;
}


void Nepomuk::RatingPainter::setHalfStepsEnabled( bool enabled )
{
    d->bHalfSteps = enabled;
}


void Nepomuk::RatingPainter::setAlignment( Qt::Alignment align )
{
    d->alignment = align;
}


void Nepomuk::RatingPainter::setLayoutDirection( Qt::LayoutDirection direction )
{
    d->direction = direction;
}


void Nepomuk::RatingPainter::setIcon( const QIcon& icon )
{
    d->icon = icon;
}


void Nepomuk::RatingPainter::setCustomPixmap( const QPixmap& pixmap )
{
    d->customPixmap = pixmap;
}


void Nepomuk::RatingPainter::setSpacing( int s )
{
    d->spacing = qMax( 0, s );
}


void Nepomuk::RatingPainter::paint( QPainter* painter, const QRect& rect, int rating, \
int hoverRating ) const {
    rating = qMin( rating, d->maxRating );
    hoverRating = qMin( hoverRating, d->maxRating );

    int numUsedStars = d->bHalfSteps ? d->maxRating/2 : d->maxRating;

    if ( hoverRating > 0 && hoverRating < rating ) {
        int tmp = hoverRating;
        hoverRating = rating;
        rating = tmp;
    }

    int usedSpacing = d->spacing;

    // get the rating pixmaps
    int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / \
numUsedStars;  QPixmap ratingPix = d->getPixmap( qMin( rect.height(), maxHSizeOnePix \
) );

    QPixmap disabledRatingPix = KIconEffect().apply( ratingPix, KIconEffect::ToGray, \
1.0, QColor(), false );  QPixmap hoverPix;

    bool half = d->bHalfSteps && rating%2;
    int numRatingStars = d->bHalfSteps ? rating/2 : rating;

    int numHoverStars = 0;
    bool halfHover = false;
    if ( hoverRating > 0 && rating != hoverRating ) {
        numHoverStars = d->bHalfSteps ? hoverRating/2 : hoverRating;
        halfHover = d->bHalfSteps && hoverRating%2;
        hoverPix = KIconEffect().apply( ratingPix, KIconEffect::ToGray, 0.5, \
QColor(), false );  }

    if ( d->alignment & Qt::AlignJustify ) {
        int w = rect.width();
        w -= numUsedStars * ratingPix.width();
        usedSpacing = w / ( numUsedStars-1 );
    }

    int ratingAreaWidth = ratingPix.width()*numUsedStars + \
usedSpacing*(numUsedStars-1);

    int i = 0;
    int x = rect.x();
    if ( d->alignment & Qt::AlignRight ) {
        x += ( rect.width() - ratingAreaWidth );
    }
    else if ( d->alignment & Qt::AlignHCenter ) {
        x += ( rect.width() - ratingAreaWidth )/2;
    }

    int xInc = ratingPix.width() + usedSpacing;
    if ( d->direction == Qt::RightToLeft ) {
        x = rect.width() - ratingPix.width() - x;
        xInc = -xInc;
    }

    int y = rect.y();
    if( d->alignment & Qt::AlignVCenter ) {
        y += ( rect.height() / 2 - ratingPix.height() / 2 );
    }
    else if ( d->alignment & Qt::AlignBottom ) {
        y += ( rect.height() - ratingPix.height() );
    }
    for(; i < numRatingStars; ++i ) {
        painter->drawPixmap( x, y, ratingPix );
        x += xInc;
    }
    if( half ) {
        painter->drawPixmap( x, y, ratingPix.width()/2, ratingPix.height(),
                             d->direction == Qt::LeftToRight ? ratingPix : ( \
                numHoverStars > 0 ? hoverPix : disabledRatingPix ),
                             0, 0, ratingPix.width()/2, ratingPix.height() );
        painter->drawPixmap( x + ratingPix.width()/2, y, ratingPix.width()/2, \
                ratingPix.height(),
                             d->direction == Qt::LeftToRight ? ( numHoverStars > 0 ? \
                hoverPix : disabledRatingPix ) : ratingPix,
                             ratingPix.width()/2, 0, ratingPix.width()/2, \
ratingPix.height() );  x += xInc;
        ++i;
    }
    for(; i < numHoverStars; ++i ) {
        painter->drawPixmap( x, y, hoverPix );
        x += xInc;
    }
    if( halfHover ) {
        painter->drawPixmap( x, y, ratingPix.width()/2, ratingPix.height(),
                             d->direction == Qt::LeftToRight ? hoverPix : \
                disabledRatingPix,
                             0, 0, ratingPix.width()/2, ratingPix.height() );
        painter->drawPixmap( x + ratingPix.width()/2, y, ratingPix.width()/2, \
                ratingPix.height(),
                             d->direction == Qt::LeftToRight ? disabledRatingPix : \
                hoverPix,
                             ratingPix.width()/2, 0, ratingPix.width()/2, \
ratingPix.height() );  x += xInc;
        ++i;
    }
    for(; i < numUsedStars; ++i ) {
        painter->drawPixmap( x, y, disabledRatingPix );
        x += xInc;
    }
}


int Nepomuk::RatingPainter::ratingFromPosition( const QRect& rect, const QPoint& pos \
) const {
    int usedSpacing = d->spacing;
    int numUsedStars = d->bHalfSteps ? d->maxRating/2 : d->maxRating;
    int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / \
numUsedStars;  QPixmap ratingPix = d->getPixmap( qMin( rect.height(), maxHSizeOnePix \
) );

    int ratingAreaWidth = ratingPix.width()*numUsedStars + \
usedSpacing*(numUsedStars-1);

    QRect usedRect( rect );
    if ( d->alignment & Qt::AlignRight ) {
        usedRect.setLeft( rect.right() - ratingAreaWidth );
    }
    else if ( d->alignment & Qt::AlignHCenter ) {
        int x = ( rect.width() - ratingAreaWidth )/2;
        usedRect.setLeft( rect.left() + x );
        usedRect.setRight( rect.right() - x );
    }
    else { // d->alignment & Qt::AlignLeft
        usedRect.setRight( rect.left() + ratingAreaWidth - 1 );
    }

    if ( d->alignment & Qt::AlignBottom ) {
        usedRect.setTop( rect.bottom() - ratingPix.height() + 1 );
    }
    else if ( d->alignment & Qt::AlignVCenter ) {
        int x = ( rect.height() - ratingPix.height() )/2;
        usedRect.setTop( rect.top() + x );
        usedRect.setBottom( rect.bottom() - x );
    }
    else { // d->alignment & Qt::AlignTop
        usedRect.setBottom( rect.top() + ratingPix.height() - 1 );
    }

    if ( usedRect.contains( pos ) ) {
        int x = 0;
        if ( d->direction == Qt::RightToLeft ) {
            x = usedRect.right() - pos.x();
        }
        else {
            x = pos.x() - usedRect.left();
        }

        double one = ( double )usedRect.width() / ( double )d->maxRating;

//        kDebug() << "rating:" << ( int )( ( double )x/one + 0.5 );

        return ( int )( ( double )x/one + 0.5 );
    }
    else {
        return -1;
    }
}


void Nepomuk::RatingPainter::paintRating( QPainter* painter, const QRect& rect, \
Qt::Alignment align, int rating, int hoverRating ) {
    RatingPainter rp;
    rp.setAlignment( align );
    rp.setLayoutDirection( painter->layoutDirection() );
    rp.paint( painter, rect, rating, hoverRating );
}


int Nepomuk::RatingPainter::getRatingFromPosition( const QRect& rect, Qt::Alignment \
align, Qt::LayoutDirection direction, const QPoint& pos ) {
    RatingPainter rp;
    rp.setAlignment( align );
    rp.setLayoutDirection( direction );
    return rp.ratingFromPosition( rect, pos );
}


["ratingpainter.h" (text/x-c++hdr)]

/*
   This file is part of the Nepomuk KDE project.
   Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
 */

#ifndef _NEPOMUK_RATING_PAINTER_H_
#define _NEPOMUK_RATING_PAINTER_H_

#include <nepomuk/nepomuk_export.h>

#include <QtCore/QRect>

class QIcon;
class QPixmap;
class QPainter;
class QPoint;
class QRect;


namespace Nepomuk {
    /**
     * \brief Utility class that draws a row of stars for a rating value.
     *
     * The RatingPainter also allows to determine a rating value from
     * a position in the draw area. it supports all different alignments
     * and custom icons.
     *
     * \author Sebastian Trueg <trueg@kde.org>
     */
    class NEPOMUK_EXPORT RatingPainter
    {
    public:
        /**
         * Create a new RatingPainter.
         * For most cases the static methods paintRating and getRatingFromPosition
         * should be sufficient.
         */
        RatingPainter();
        
        /**
         * Destructor
         */
        ~RatingPainter();

        /**
         * The maximum rating, i.e. how many stars are drawn
         * in total.
         *
         * \sa setMaxRating
         */
        int maxRating() const;

        /**
         * If half steps are enabled one star equals to 2 rating
         * points and uneven rating values result in half-stars being
         * drawn.
         *
         * \sa setHalfStepsEnabled
         */
        bool halfStepsEnabled() const;

        /**
         * The alignment of the stars.
         *
         * \sa setAlignment
         */
        Qt::Alignment alignment() const;

        /**
         * The layout direction. If RTL the stars
         * representing the rating value will be drawn from the 
         * right.
         *
         * \sa setLayoutDirection
         */
        Qt::LayoutDirection layoutDirection() const;

        /**
         * The icon used to draw a star. In case a custom pixmap has been set
         * this value is ignored.
         *
         * \sa setIcon, setCustomPixmap
         */
        QIcon icon() const;

        /**
         * The custom pixmap set to draw a star. If no custom
         * pixmap has been set, an invalid pixmap is returned.
         *
         * \sa setCustomPixmap
         */
        QPixmap customPixmap() const;

        /**
         * The spacing between rating pixmaps.
         *
         * \sa setSpacing
         */
        int spacing() const;

        /**
         * The maximum rating. Defaults to 10.
         */
        void setMaxRating( int max );

        /**
         * If half steps are enabled (the default) then
         * one rating step corresponds to half a star.
         */
        void setHalfStepsEnabled( bool enabled );
        
        /**
         * The alignment of the stars in the drawing rect.
         * All alignment flags are supported.
         */
        void setAlignment( Qt::Alignment align );

        /**
         * LTR or RTL
         */
        void setLayoutDirection( Qt::LayoutDirection direction );

        /**
         * Set a custom icon. Defaults to "rating".
         */
        void setIcon( const QIcon& icon );

        /**
         * Set a custom pixmap.
         */
        void setCustomPixmap( const QPixmap& pixmap );

        /**
         * Set the spacing between rating pixmaps. Be aware that
         * for justified horizontal alignment this values may be
         * ignored.
         */
        void setSpacing( int spacing );

        /**
         * Draw the rating.
         *
         * \param painter The painter to draw the rating to.
         * \param rect The geometry of the rating. The alignment of the rating is \
                used relative
         *             to this value.
         * \param rating The actual rating value to draw.
         * \param hoverRating The hover rating indicates the position the user hovers \
                the mouse
         *                    pointer at. This will provide visual feedback about the \
                new rating
         *                    if the user would actually click as well as the \
                difference to the
         *                    current rating.
         */
        void paint( QPainter* painter, const QRect& rect, int rating, int hoverRating \
= -1 ) const;

        /**
         * Calculate the rating value from mouse position pos.
         *
         * \return The rating corresponding to pos or -1 if pos is
         * outside of the configured rect.
         */
        int ratingFromPosition( const QRect& rect, const QPoint& pos ) const;

        /**
         * Convenience method that paints a rating into the given rect.
         *
         * LayoutDirection is read from QPainter.
         *
         * \param align can be aligned vertically and horizontally. Using \
                Qt::AlignJustify will insert spacing
         * between the stars.
         */
        static void paintRating( QPainter* p, const QRect& rect, Qt::Alignment align, \
int rating, int hoverRating = -1 );

        /**
         * Get the rating that would be selected if the user clicked position pos
         * within rect if the rating has been drawn with paintRating() using the same
         * rect and align values.
         *
         * \return The new rating or -1 if pos is outside of the rating area.
         */
        static int getRatingFromPosition( const QRect& rect, Qt::Alignment align, \
Qt::LayoutDirection direction, const QPoint& pos );

    private:
        class Private;
        Private* const d;
    };
}

#endif



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic