File indexing completed on 2025-02-16 04:48:45

0001 /*
0002  *  slider.h  -  slider control with read-only option
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2004-2021 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include <QSlider>
0012 class QLabel;
0013 class QMouseEvent;
0014 class QKeyEvent;
0015 
0016 
0017 /**
0018  *  @short A QSlider with read-only option and connection to a value label.
0019  *
0020  *  The Slider class is a QSlider with a read-only option.
0021  *
0022  *  The widget may be set as read-only. This has the same effect as disabling it, except
0023  *  that its appearance is unchanged.
0024  *
0025  *  @author David Jarvie <djarvie@kde.org>
0026  */
0027 class Slider : public QSlider
0028 {
0029     Q_OBJECT
0030     Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
0031 public:
0032     /** Constructor.
0033      *  @param parent The parent object of this widget.
0034      */
0035     explicit Slider(QWidget* parent = nullptr);
0036 
0037     /** Constructor.
0038      *  @param orient The orientation of the slider, either Qt::Horizontal or Qt::Vertical.
0039      *  @param parent The parent object of this widget.
0040      */
0041     explicit Slider(Qt::Orientation orient, QWidget* parent = nullptr);
0042 
0043     /** Constructor.
0044      *  @param minValue The minimum value which the slider can have.
0045      *  @param maxValue The maximum value which the slider can have.
0046      *  @param pageStep The page step increment.
0047      *  @param orient The orientation of the slider, either Qt::Horizontal or Qt::Vertical.
0048      *  @param parent The parent object of this widget.
0049      */
0050     Slider(int minValue, int maxValue, int pageStep, Qt::Orientation orient,
0051            QWidget* parent = nullptr);
0052 
0053     /** Returns true if the slider is read only. */
0054     bool         isReadOnly() const  { return mReadOnly; }
0055 
0056     /** Sets whether the slider is read-only for the user.
0057      *  @param readOnly True to set the widget read-only, false to set it read-write.
0058      */
0059     virtual void setReadOnly(bool readOnly);
0060 
0061     /** Set a label to display the slider's value.
0062      *  The label will be updated whenever the slider value changes.
0063      *  The value will be displayed in localised form.
0064      *  @param label           The label to display the slider's value.
0065      *  @param format          Format string for the value to display, containing "%1"
0066      *                         where the value is to be substituted. Any other '%'
0067      *                         will be substituted with the locale's percent symbol.
0068      *  @param hideIfDisabled  Hide the label if the slider is disabled.
0069      */
0070     void setValueLabel(QLabel* label, const QString& format = QStringLiteral("%1"), bool hideIfDisabled = false);
0071 
0072     void setVisible(bool) override;
0073 
0074 protected:
0075     void mousePressEvent(QMouseEvent*) override;
0076     void mouseReleaseEvent(QMouseEvent*) override;
0077     void mouseMoveEvent(QMouseEvent*) override;
0078     void keyPressEvent(QKeyEvent*) override;
0079     void keyReleaseEvent(QKeyEvent*) override;
0080     void changeEvent(QEvent*) override;
0081 
0082 private Q_SLOTS:
0083     void valueHasChanged(int value);
0084     void valueLabelDestroyed(QObject*);
0085 
0086 private:
0087     QLabel* mValueLabel {nullptr};  // label to display the slider's value
0088     QString mValueFormat;           // format used for mValueLabel
0089     bool    mValueLabelHide;        // hide mValueLabel if slider is disabled
0090     bool    mReadOnly {false};      // value cannot be changed by the user
0091 };
0092 
0093 // vim: et sw=4: