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>
30 #include <nepomuk/nepomuk_export.h>
35 * Utility class that draws a row of stars for a rating value.
37 class NEPOMUK_EXPORT RatingPainter
41 * Create a new RatingPainter.
42 * Normally the static methods should be sufficient.
47 int maxRating() const;
48 bool halfStepsEnabled() const;
49 Qt::Alignment
alignment() const;
50 Qt::LayoutDirection
direction() const;
52 QPixmap
customPixmap() const;
55 * The maximum rating. Defaults to 10.
57 void setMaxRating( int max
);
60 * If half steps are enabled (the default) then
61 * one rating step corresponds to half a star.
63 void setHalfStepsEnabled( bool enabled
);
66 * The alignment of the stars in the drawing rect.
67 * All alignment flags are supported.
69 void setAlignment( Qt::Alignment align
);
74 void setLayoutDirection( Qt::LayoutDirection direction
);
77 * Set a custom icon. Defaults to "rating".
79 void setIcon( const KIcon
& icon
);
82 * Set a custom pixmap.
84 void setCustomPixmap( const QPixmap
& pixmap
);
87 * Draw the rating into the configured rect.
89 void draw( QPainter
* painter
, const QRect
& rect
, int rating
, int hoverRating
= -1 );
92 * Calculate the rating value from mouse position pos.
94 * \return The rating corresponding to pos or -1 if pos is
95 * outside of the configured rect.
97 int fromPosition( const QRect
& rect
, const QPoint
& pos
);
100 * Convenience method that draws a rating into the given rect.
102 * LayoutDirection is read from QPainter.
104 * \param align can be aligned vertically and horizontally. Using Qt::AlignJustify will insert spacing
107 static void drawRating( QPainter
* p
, const QRect
& rect
, Qt::Alignment align
, int rating
, int hoverRating
= -1 );
110 * Get the rating that would be selected if the user clicked position pos
111 * within rect if the rating has been drawn with drawRating() using the same
112 * rect and align values.
114 * \return The new rating or -1 if pos is outside of the rating area.
116 static int getRatingFromPosition( const QRect
& rect
, Qt::Alignment align
, Qt::LayoutDirection direction
, const QPoint
& pos
);