]> cloud.milkyroute.net Git - dolphin.git/blob - src/ratingpainter.h
Optimize PNG files again.
[dolphin.git] / src / ratingpainter.h
1 /*
2 This file is part of the Nepomuk KDE project.
3 Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
4
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.
8
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.
13
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.
18 */
19
20 #ifndef _NEPOMUK_RATING_PAINTER_H_
21 #define _NEPOMUK_RATING_PAINTER_H_
22
23 class QPainter;
24
25 #include <QtCore/QRect>
26 #include <QtCore/QPoint>
27
28 #include <kicon.h>
29
30
31 namespace Nepomuk {
32 /**
33 * Utility class that draws a row of stars for a rating value.
34 */
35 class RatingPainter
36 {
37 public:
38 /**
39 * Create a new RatingPainter.
40 * Normally the static methods should be sufficient.
41 */
42 RatingPainter();
43 ~RatingPainter();
44
45 int maxRating() const;
46 bool halfStepsEnabled() const;
47 Qt::Alignment alignment() const;
48 Qt::LayoutDirection direction() const;
49 KIcon icon() const;
50 QPixmap customPixmap() const;
51
52 /**
53 * The maximum rating. Defaults to 10.
54 */
55 void setMaxRating( int max );
56
57 /**
58 * If half steps are enabled (the default) then
59 * one rating step corresponds to half a star.
60 */
61 void setHalfStepsEnabled( bool enabled );
62
63 /**
64 * The alignment of the stars in the drawing rect.
65 * All alignment flags are supported.
66 */
67 void setAlignment( Qt::Alignment align );
68
69 /**
70 * LTR or RTL
71 */
72 void setLayoutDirection( Qt::LayoutDirection direction );
73
74 /**
75 * Set a custom icon. Defaults to "rating".
76 */
77 void setIcon( const KIcon& icon );
78
79 /**
80 * Set a custom pixmap.
81 */
82 void setCustomPixmap( const QPixmap& pixmap );
83
84 /**
85 * Draw the rating into the configured rect.
86 */
87 void draw( QPainter* painter, const QRect& rect, int rating, int hoverRating = -1 );
88
89 /**
90 * Calculate the rating value from mouse position pos.
91 *
92 * \return The rating corresponding to pos or -1 if pos is
93 * outside of the configured rect.
94 */
95 int fromPosition( const QRect& rect, const QPoint& pos );
96
97 /**
98 * Convenience method that draws a rating into the given rect.
99 *
100 * LayoutDirection is read from QPainter.
101 *
102 * \param align can be aligned vertically and horizontally. Using Qt::AlignJustify will insert spacing
103 * between the stars.
104 */
105 static void drawRating( QPainter* p, const QRect& rect, Qt::Alignment align, int rating, int hoverRating = -1 );
106
107 /**
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.
111 *
112 * \return The new rating or -1 if pos is outside of the rating area.
113 */
114 static int getRatingFromPosition( const QRect& rect, Qt::Alignment align, Qt::LayoutDirection direction, const QPoint& pos );
115
116 private:
117 class Private;
118 Private* const d;
119 };
120 }
121
122 #endif