File indexing completed on 2024-12-01 07:35:11

0001 /*
0002     SPDX-FileCopyrightText: 2011 Till Theato <root@ttill.de>
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #pragma once
0007 
0008 #include <QDoubleSpinBox>
0009 #include <QProgressBar>
0010 #include <QSpinBox>
0011 #include <QWidget>
0012 #include <kselectaction.h>
0013 
0014 class QAction;
0015 class QMenu;
0016 class KSelectAction;
0017 
0018 class CustomLabel : public QProgressBar
0019 {
0020     Q_OBJECT
0021 public:
0022     explicit CustomLabel(const QString &label, bool showSlider = true, int range = 1000, QWidget *parent = nullptr);
0023     void setProgressValue(double value);
0024     void setStep(double step);
0025     void slotValueInc(double factor = 1);
0026     void slotValueDec(double factor = 1);
0027 
0028 protected:
0029     // virtual void mouseDoubleClickEvent(QMouseEvent * event);
0030     void mousePressEvent(QMouseEvent *event) override;
0031     void mouseReleaseEvent(QMouseEvent *event) override;
0032     void mouseMoveEvent(QMouseEvent *event) override;
0033     // virtual void paintEvent(QPaintEvent *event);
0034     void wheelEvent(QWheelEvent *event) override;
0035 
0036 private:
0037     QPoint m_dragStartPosition;
0038     QPoint m_dragLastPosition;
0039     bool m_dragMode;
0040     bool m_showSlider;
0041     double m_step;
0042     double m_value;
0043     void setNewValue(double, bool);
0044 
0045 Q_SIGNALS:
0046     void valueChanged(double, bool);
0047     void setInTimeline();
0048     void resetValue();
0049 };
0050 
0051 /** @class DragValue
0052     @brief A widget for modifying numbers by dragging, using the mouse wheel or entering them with the keyboard.
0053  */
0054 class DragValue : public QWidget
0055 {
0056     Q_OBJECT
0057 
0058 public:
0059     /**
0060      * @brief Default constructor.
0061      * @param label The label that will be displayed in the progress bar
0062      * @param defaultValue The default value
0063      * @param decimals The number of decimals for the parameter. 0 means it is an integer
0064      * @param min The minimum value
0065      * @param max The maximum value
0066      * @param id Used to identify this widget. If this parameter is set, "Show in Timeline" will be available in context menu.
0067      * @param suffix The suffix that will be displayed in the spinbox (for example '%')
0068      * @param showSlider If disabled, user can still drag on the label but no progress bar is shown
0069      */
0070     explicit DragValue(const QString &label, double defaultValue, int decimals, double min = 0, double max = 100, int id = -1,
0071                        const QString &suffix = QString(), bool showSlider = true, bool oddOnly = false, QWidget *parent = nullptr);
0072     ~DragValue() override;
0073 
0074     /** @brief Returns the precision = number of decimals */
0075     int precision() const;
0076     /** @brief Returns the maximum value */
0077     qreal minimum() const;
0078     /** @brief Returns the minimum value */
0079     qreal maximum() const;
0080 
0081     /** @brief Sets the minimum value. */
0082     void setMinimum(qreal min);
0083     /** @brief Sets the maximum value. */
0084     void setMaximum(qreal max);
0085     /** @brief Sets minimum and maximum value. */
0086     void setRange(qreal min, qreal max);
0087     /** @brief Sets the size of a step (when dragging or using the mouse wheel). */
0088     void setStep(qreal step);
0089 
0090     /** @brief Returns the current value */
0091     qreal value() const;
0092     /** @brief Change the "inTimeline" property to paint the intimeline widget differently. */
0093     void setInTimelineProperty(bool intimeline);
0094     /** @brief Returns minimum size for QSpinBox, used to set all spinboxes to the same width. */
0095     int spinSize();
0096     /** @brief Sets the minimum size for QSpinBox, used to set all spinboxes to the same width. */
0097     void setSpinSize(int width);
0098     /** @brief Returns true if widget is currently being edited */
0099     bool hasEditFocus() const;
0100 
0101 public Q_SLOTS:
0102     /** @brief Sets the value (forced to be in the valid range) and emits valueChanged. */
0103     void setValue(double value, bool final = true);
0104     void setValueFromProgress(double value, bool final);
0105     /** @brief Resets to default value */
0106     void slotReset();
0107 
0108 Q_SIGNALS:
0109     void valueChanged(double value, bool final = true);
0110     void inTimeline(int);
0111 
0112     /*
0113      * Private
0114      */
0115 
0116 protected:
0117     /*virtual void mousePressEvent(QMouseEvent *e);
0118     virtual void mouseMoveEvent(QMouseEvent *e);
0119     virtual void mouseReleaseEvent(QMouseEvent *e);*/
0120     /** @brief Forwards tab focus to lineedit since it is disabled. */
0121     bool eventFilter(QObject *watched, QEvent *event) override;
0122 
0123     // virtual void keyPressEvent(QKeyEvent *e);
0124     // virtual void wheelEvent(QWheelEvent *e);
0125     // virtual void paintEvent( QPaintEvent * event );
0126 
0127 private Q_SLOTS:
0128 
0129     void slotEditingFinished();
0130 
0131     void slotSetScaleMode(int mode);
0132     void slotSetDirectUpdate(bool directUpdate);
0133     void slotShowContextMenu(const QPoint &pos);
0134     void slotSetValue(int value);
0135     void slotSetValue(double value);
0136     void slotSetInTimeline();
0137 
0138 private:
0139     double m_maximum;
0140     double m_minimum;
0141     int m_decimals;
0142     double m_default;
0143     int m_id;
0144     QSpinBox *m_intEdit;
0145     QDoubleSpinBox *m_doubleEdit;
0146 
0147     QMenu *m_menu;
0148     KSelectAction *m_scale;
0149     QAction *m_directUpdate;
0150     CustomLabel *m_label;
0151 };