File indexing completed on 2025-04-27 03:58:37

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2014-11-30
0007  * Description : Save space slider widget
0008  *
0009  * SPDX-FileCopyrightText: 2014-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText:      2010 by Justin Noel <justin at ics dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef DIGIKAM_DSLIDER_SPINBOX_H
0017 #define DIGIKAM_DSLIDER_SPINBOX_H
0018 
0019 // Qt includes
0020 
0021 #include <QAbstractSpinBox>
0022 #include <QStyleOptionSpinBox>
0023 #include <QStyleOptionProgressBar>
0024 
0025 namespace Digikam
0026 {
0027 
0028 class DAbstractSliderSpinBoxPrivate;
0029 class DSliderSpinBoxPrivate;
0030 class DDoubleSliderSpinBoxPrivate;
0031 
0032 class DAbstractSliderSpinBox : public QWidget
0033 {
0034     Q_OBJECT
0035     Q_DISABLE_COPY(DAbstractSliderSpinBox)
0036     Q_DECLARE_PRIVATE(DAbstractSliderSpinBox)
0037 
0038 protected:
0039 
0040     explicit DAbstractSliderSpinBox(QWidget* const parent, DAbstractSliderSpinBoxPrivate* const q);
0041 
0042 public:
0043 
0044     ~DAbstractSliderSpinBox() override;
0045 
0046     void showEdit();
0047     void hideEdit();
0048 
0049     void setPrefix(const QString& prefix);
0050     void setSuffix(const QString& suffix);
0051 
0052     void setExponentRatio(double dbl);
0053 
0054     /**
0055      * If set to block, it informs inheriting classes that they shouldn't emit signals
0056      * if the update comes from a mouse dragging the slider.
0057      * Set this to true when dragging the slider and updates during the drag are not needed.
0058      */
0059     void setBlockUpdateSignalOnDrag(bool block);
0060 
0061     QSize sizeHint() const override;
0062     QSize minimumSizeHint() const override;
0063     virtual QSize minimumSize() const;
0064 
0065     bool isDragging() const;
0066 
0067 protected:
0068 
0069     void paintEvent(QPaintEvent* e) override;
0070     void mousePressEvent(QMouseEvent* e) override;
0071     void mouseReleaseEvent(QMouseEvent* e) override;
0072     void mouseMoveEvent(QMouseEvent* e) override;
0073     void keyPressEvent(QKeyEvent* e) override;
0074     void wheelEvent(QWheelEvent* e) override;
0075     void focusInEvent(QFocusEvent* e) override;
0076     bool eventFilter(QObject* recv, QEvent* e) override;
0077 
0078     QStyleOptionSpinBox spinBoxOptions() const;
0079     QStyleOptionProgressBar progressBarOptions() const;
0080 
0081     QRect progressRect(const QStyleOptionSpinBox& spinBoxOptions) const;
0082     QRect upButtonRect(const QStyleOptionSpinBox& spinBoxOptions) const;
0083     QRect downButtonRect(const QStyleOptionSpinBox& spinBoxOptions) const;
0084 
0085     int valueForX(int x, Qt::KeyboardModifiers modifiers = Qt::NoModifier) const;
0086 
0087     virtual QString valueString() const = 0;
0088 
0089     /**
0090      * Sets the slider internal value. Inheriting classes should respect blockUpdateSignal
0091      * so that, in specific cases, we have a performance improvement. See setIgnoreMouseMoveEvents.
0092      */
0093     virtual void setInternalValue(int value, bool blockUpdateSignal) = 0;
0094 
0095 protected Q_SLOTS:
0096 
0097     void contextMenuEvent(QContextMenuEvent* event) override;
0098     void editLostFocus();
0099 
0100 protected:
0101 
0102     DAbstractSliderSpinBoxPrivate* const d_ptr;
0103 
0104     // -- QWidget interface --------------------------------------
0105 
0106 protected:
0107 
0108     void changeEvent(QEvent* e) override;
0109     void paint(QPainter& painter);
0110     void paintFusion(QPainter& painter);
0111     void paintPlastique(QPainter& painter);
0112     void paintBreeze(QPainter& painter);
0113 
0114 private:
0115 
0116     void setInternalValue(int value);
0117 };
0118 
0119 // ---------------------------------------------------------------------------------
0120 
0121 class DSliderSpinBox : public DAbstractSliderSpinBox
0122 {
0123     Q_OBJECT
0124     Q_DECLARE_PRIVATE(DSliderSpinBox)
0125     Q_PROPERTY(int minimum READ minimum WRITE setMinimum)
0126     Q_PROPERTY(int maximum READ maximum WRITE setMaximum)
0127 
0128 public:
0129 
0130     explicit DSliderSpinBox(QWidget* const parent = nullptr);
0131     ~DSliderSpinBox() override;
0132 
0133     void setRange(int minimum, int maximum);
0134 
0135     int  minimum() const;
0136     void setMinimum(int minimum);
0137     int  maximum() const;
0138     void setMaximum(int maximum);
0139     int  fastSliderStep() const;
0140     void setFastSliderStep(int step);
0141 
0142     /// Get the value, don't use value()
0143     int  value();
0144 
0145     void setSingleStep(int value);
0146     void setPageStep(int value);
0147 
0148 public Q_SLOTS:
0149 
0150     /// Set the value, don't use setValue()
0151     void setValue(int value);
0152 
0153 protected:
0154 
0155     QString valueString() const override;
0156     void setInternalValue(int value, bool blockUpdateSignal) override;
0157 
0158 Q_SIGNALS:
0159 
0160     void valueChanged(int value);
0161 };
0162 
0163 // ---------------------------------------------------------------------------------
0164 
0165 class DDoubleSliderSpinBox : public DAbstractSliderSpinBox
0166 {
0167     Q_OBJECT
0168     Q_DECLARE_PRIVATE(DDoubleSliderSpinBox)
0169 
0170 public:
0171 
0172     explicit DDoubleSliderSpinBox(QWidget* const parent = nullptr);
0173     ~DDoubleSliderSpinBox() override;
0174 
0175     void   setRange(double minimum, double maximum, int decimals = 0);
0176 
0177     double minimum() const;
0178     void   setMinimum(double minimum);
0179     double maximum() const;
0180     void   setMaximum(double maximum);
0181     double fastSliderStep() const;
0182     void   setFastSliderStep(double step);
0183 
0184     double value();
0185     void   setSingleStep(double value);
0186 
0187 public Q_SLOTS:
0188 
0189     void setValue(double value);
0190 
0191 protected:
0192 
0193     QString valueString() const override;
0194     void setInternalValue(int value, bool blockUpdateSignal) override;
0195 
0196 Q_SIGNALS:
0197 
0198     void valueChanged(double value);
0199 };
0200 
0201 } // namespace Digikam
0202 
0203 #endif // DIGIKAM_DSLIDER_SPINBOX_H