File indexing completed on 2024-04-28 03:59:12

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2006-2007 Sebastian Trueg <trueg@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KRATINGWIDGET_H
0009 #define KRATINGWIDGET_H
0010 
0011 #include <QFrame>
0012 #include <memory>
0013 
0014 #include <kwidgetsaddons_export.h>
0015 
0016 /**
0017  * \class KRatingWidget kratingwidget.h KRatingWidget
0018  *
0019  * \brief Displays a rating value as a row of pixmaps.
0020  *
0021  * The KRatingWidget displays a range of stars or other arbitrary
0022  * pixmaps and allows the user to select a certain number by mouse.
0023  *
0024  * \sa KRatingPainter
0025  *
0026  * \author Sebastian Trueg <trueg@kde.org>
0027  */
0028 class KWIDGETSADDONS_EXPORT KRatingWidget : public QFrame
0029 {
0030     Q_OBJECT
0031     Q_PROPERTY(int rating READ rating WRITE setRating)
0032     Q_PROPERTY(int maxRating READ maxRating WRITE setMaxRating)
0033     Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
0034     Q_PROPERTY(bool halfStepsEnabled READ halfStepsEnabled WRITE setHalfStepsEnabled)
0035     Q_PROPERTY(int spacing READ spacing WRITE setSpacing)
0036     Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
0037 
0038 public:
0039     /**
0040      * Creates a new rating widget.
0041      */
0042     explicit KRatingWidget(QWidget *parent = nullptr);
0043 
0044     /**
0045      * Destructor
0046      */
0047     ~KRatingWidget() override;
0048 
0049     /**
0050      * \return The current rating.
0051      */
0052     int rating() const;
0053 
0054     /**
0055      * \return the maximum possible rating.
0056      */
0057     int maxRating() const;
0058 
0059     /**
0060      * The alignment of the stars.
0061      *
0062      * \sa setAlignment
0063      */
0064     Qt::Alignment alignment() const;
0065 
0066     /**
0067      * The layout direction. If RTL the stars
0068      * representing the rating value will be drawn from the
0069      * right.
0070      *
0071      * \sa setLayoutDirection
0072      */
0073     Qt::LayoutDirection layoutDirection() const;
0074 
0075     /**
0076      * The spacing between the rating stars.
0077      *
0078      * \sa setSpacing
0079      */
0080     int spacing() const;
0081 
0082     QSize sizeHint() const override;
0083 
0084     /**
0085      * If half steps are enabled one star equals to 2 rating
0086      * points and uneven rating values result in half-stars being
0087      * drawn.
0088      *
0089      * \sa setHalfStepsEnabled
0090      */
0091     bool halfStepsEnabled() const;
0092 
0093     /**
0094      * The icon used to draw a star. In case a custom pixmap has been set
0095      * this value is ignored.
0096      *
0097      * \sa setIcon, setCustomPixmap
0098      */
0099     QIcon icon() const;
0100 
0101 Q_SIGNALS:
0102     /**
0103      * This signal is emitted when the rating is changed.
0104      */
0105     void ratingChanged(int rating);
0106 
0107 public Q_SLOTS:
0108     /**
0109      * Set the current rating. Calling this method will trigger the
0110      * ratingChanged signal if @p rating is different from the previous rating.
0111      */
0112     void setRating(int rating);
0113 
0114     /**
0115      * Set the maximum allowed rating value. The default is 10 which means
0116      * that a rating from 1 to 10 is selectable. If \a max is uneven steps
0117      * are automatically only allowed full.
0118      */
0119     void setMaxRating(int max);
0120 
0121     /**
0122      * If half steps are enabled (the default) then
0123      * one rating step corresponds to half a star.
0124      */
0125     void setHalfStepsEnabled(bool enabled);
0126 
0127     /**
0128      * Set the spacing between the pixmaps. The default is 0.
0129      */
0130     void setSpacing(int);
0131 
0132     /**
0133      * The alignment of the stars in the drawing rect.
0134      * All alignment flags are supported.
0135      */
0136     void setAlignment(Qt::Alignment align);
0137 
0138     /**
0139      * LTR or RTL
0140      */
0141     void setLayoutDirection(Qt::LayoutDirection direction);
0142 
0143     /**
0144      * Set a custom icon. Defaults to "rating".
0145      */
0146     void setIcon(const QIcon &icon);
0147 
0148     /**
0149      * Set a custom pixmap.
0150      */
0151     void setCustomPixmap(const QPixmap &pixmap);
0152 
0153     /**
0154      * Set the recommended size of the pixmaps. This is
0155      * only used for the sizeHint. The actual size is always
0156      * dependent on the size of the widget itself.
0157      */
0158     void setPixmapSize(int size);
0159 
0160 protected:
0161     void mousePressEvent(QMouseEvent *e) override;
0162     void mouseMoveEvent(QMouseEvent *e) override;
0163     void leaveEvent(QEvent *e) override;
0164     void paintEvent(QPaintEvent *e) override;
0165     void resizeEvent(QResizeEvent *e) override;
0166 
0167 private:
0168     std::unique_ptr<class KRatingWidgetPrivate> const d;
0169 };
0170 
0171 #endif