File indexing completed on 2024-06-02 05:09:21

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2023 Thomas Fischer <fischer@unix-ag.uni-kl.de>
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
0018  ***************************************************************************/
0019 
0020 #ifndef KBIBTEX_GUI_STARRATING_H
0021 #define KBIBTEX_GUI_STARRATING_H
0022 
0023 #include <QWidget>
0024 
0025 #include <KRatingPainter>
0026 
0027 #include <Value>
0028 
0029 #include "kbibtexgui_export.h"
0030 
0031 class QLabel;
0032 class QPaintEvent;
0033 class QMouseEvent;
0034 class QPushButton;
0035 
0036 class KBIBTEXGUI_EXPORT StarRatingPainter : public KRatingPainter
0037 {
0038 public:
0039     static const int numberOfStars;
0040 
0041     explicit StarRatingPainter();
0042 
0043     void paint(QPainter *painter, const QRect &rect, double percent, double hoverPercent = -1.0);
0044 
0045     static double roundToNearestHalfStarPercent(double percent);
0046 };
0047 
0048 /**
0049  * A widget which shows a number of stars in a horizonal row.
0050  * A floating-point value between 0.0 and n (n=number of stars) can be
0051  * assigned to this widget; based on this value, the corresponding
0052  * number of stars on the left side will be colored golden, the stars
0053  * on the right side will be shown in grey.
0054  *
0055  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
0056  */
0057 class KBIBTEXGUI_EXPORT StarRating : public QWidget
0058 {
0059     Q_OBJECT
0060 
0061 public:
0062     /**
0063      * Create a star rating widget with a given number of stars.
0064      *
0065      * @param maxNumberOfStars number of stars (recommended value is 8)
0066      * @param parent parent widget
0067      */
0068     explicit StarRating(QWidget *parent = nullptr);
0069     ~StarRating();
0070 
0071     /**
0072      * Get the current rating in percent (i.e >=0.0 and <=100.0).
0073      * If no rating has been set (e.g. by a previous call of @see unsetValue),
0074      * the return value will be negative.
0075      * @return either percent between 0.0 and 100.0, or a negative value
0076      */
0077     double value() const;
0078 
0079     /**
0080      * Set the rating in percent (valid only >=0.0 and <=100.0).
0081      * @param percent value between 0.0 and 100.0
0082      */
0083     void setValue(double percent);
0084 
0085     /**
0086      * Remove any value assigned to this widget.
0087      * No stars will be highlighted and some "no value set" text
0088      * will be shown.
0089      * @see value will return a negative value.
0090      */
0091     void unsetValue();
0092 
0093     /**
0094      * Set this widget in read-only or read-writeable mode.
0095      * @param isReadOnly @c true if widget is to be read-only, @c false if modifyable
0096      */
0097     void setReadOnly(bool isReadOnly);
0098 
0099 Q_SIGNALS:
0100     void modified();
0101 
0102 protected:
0103     void paintEvent(QPaintEvent *) override;
0104     void mouseReleaseEvent(QMouseEvent *) override;
0105     void mouseMoveEvent(QMouseEvent *) override;
0106     void leaveEvent(QEvent *) override;
0107     bool eventFilter(QObject *obj, QEvent *event) override;
0108 
0109 private Q_SLOTS:
0110     void clear();
0111     void buttonHeight();
0112 
0113 private:
0114     class Private;
0115     Private *const d;
0116 };
0117 
0118 /**
0119  * A specialization of @see StarRating mimicing a FieldInput widget.
0120  * As part of this specialization, @see apply and @see reset functions
0121  * to write to or read from a Value object.
0122  *
0123  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
0124  */
0125 class KBIBTEXGUI_EXPORT StarRatingFieldInput : public StarRating
0126 {
0127     Q_OBJECT
0128 
0129 public:
0130     explicit StarRatingFieldInput(QWidget *parent = nullptr)
0131             : StarRating(parent) {
0132         /* nothing */
0133     }
0134 
0135     /**
0136      * Set this widget's state based on the provided Value object
0137      * @param value Value object to evaluate
0138      * @return @c true if the Value could be interpreted into a star rating, @c false otherwise
0139      */
0140     bool reset(const Value &value);
0141 
0142     /**
0143      * Write this widget's value into the provided Value object.
0144      * @param value Value object to modify
0145      * @return @c true if the apply operation succeeded, @c false otherwise (should not happen)
0146      */
0147     bool apply(Value &value) const;
0148 
0149     bool validate(QWidget **widgetWithIssue, QString &message) const;
0150 };
0151 
0152 #endif // KBIBTEX_GUI_STARRATING_H