2 This file is part of the Nepomuk KDE project.
3 Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #ifndef _NEPOMUK_RATING_PAINTER_H_
21 #define _NEPOMUK_RATING_PAINTER_H_
25 #include <QtCore/QRect>
26 #include <QtCore/QPoint>
33 * Utility class that draws a row of stars for a rating value.
39 * Create a new RatingPainter.
40 * Normally the static methods should be sufficient.
45 int maxRating() const;
46 bool halfStepsEnabled() const;
47 Qt::Alignment
alignment() const;
48 Qt::LayoutDirection
direction() const;
50 QPixmap
customPixmap() const;
53 * The maximum rating. Defaults to 10.
55 void setMaxRating( int max
);
58 * If half steps are enabled (the default) then
59 * one rating step corresponds to half a star.
61 void setHalfStepsEnabled( bool enabled
);
64 * The alignment of the stars in the drawing rect.
65 * All alignment flags are supported.
67 void setAlignment( Qt::Alignment align
);
72 void setLayoutDirection( Qt::LayoutDirection direction
);
75 * Set a custom icon. Defaults to "rating".
77 void setIcon( const KIcon
& icon
);
80 * Set a custom pixmap.
82 void setCustomPixmap( const QPixmap
& pixmap
);
85 * Draw the rating into the configured rect.
87 void draw( QPainter
* painter
, const QRect
& rect
, int rating
, int hoverRating
= -1 );
90 * Calculate the rating value from mouse position pos.
92 * \return The rating corresponding to pos or -1 if pos is
93 * outside of the configured rect.
95 int fromPosition( const QRect
& rect
, const QPoint
& pos
);
98 * Convenience method that draws a rating into the given rect.
100 * LayoutDirection is read from QPainter.
102 * \param align can be aligned vertically and horizontally. Using Qt::AlignJustify will insert spacing
105 static void drawRating( QPainter
* p
, const QRect
& rect
, Qt::Alignment align
, int rating
, int hoverRating
= -1 );
108 * Get the rating that would be selected if the user clicked position pos
109 * within rect if the rating has been drawn with drawRating() using the same
110 * rect and align values.
112 * \return The new rating or -1 if pos is outside of the rating area.
114 static int getRatingFromPosition( const QRect
& rect
, Qt::Alignment align
, Qt::LayoutDirection direction
, const QPoint
& pos
);