File indexing completed on 2024-05-12 16:02:27

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Laurent Valentin Jospin <laurent.valentin@famillejospin.ch>
0003  *  SPDX-FileCopyrightText: 2021 Deif Lou <ginoba@gmail.com>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #ifndef KIS_DOUBLEPARSEUNITSPINBOX_H
0009 #define KIS_DOUBLEPARSEUNITSPINBOX_H
0010 
0011 #include <KoUnit.h>
0012 
0013 #include "kis_double_parse_spin_box.h"
0014 #include "kritawidgetutils_export.h"
0015 
0016 class KisSpinBoxUnitManager;
0017 
0018 /*!
0019  * \brief The KisDoubleParseUnitSpinBox class is an evolution of the \see KoUnitDoubleSpinBox, but inherit from \see KisDoubleParseSpinBox to be able to parse math expressions.
0020  *
0021  * This class store the
0022  */
0023 class KRITAWIDGETUTILS_EXPORT  KisDoubleParseUnitSpinBox : public KisDoubleParseSpinBox
0024 {
0025 
0026     Q_OBJECT
0027 
0028 public:
0029     KisDoubleParseUnitSpinBox(QWidget* parent = 0);
0030     ~KisDoubleParseUnitSpinBox() override;
0031 
0032     void setUnitManager(KisSpinBoxUnitManager* unitManager);
0033 
0034     /**
0035      * Set the new value in points (or other reference unit) which will then be converted to the current unit for display
0036      * @param newValue the new value
0037      * @see value()
0038      */
0039     virtual void changeValue( double newValue );
0040 
0041     /**
0042      * This spinbox shows the internal value after a conversion to the unit set here.
0043      */
0044     virtual void setUnit(const KoUnit &unit);
0045     virtual void setUnit(const QString & symbol);
0046     /*!
0047      * \brief setReturnUnit set a unit, such that the spinbox now return values in this unit instead of the reference unit for the current dimension.
0048      * \param symbol the symbol of the new unit.
0049      */
0050     void setReturnUnit(const QString & symbol);
0051 
0052     /**
0053      * @brief setDimensionType set the dimension (for example length or angle) of the units the spinbox manage
0054      * @param dim the dimension id. (if not an id in KisSpinBoxUnitManager::UnitDimension, then the function does nothing).
0055      */
0056     virtual void setDimensionType(int dim);
0057 
0058     /// @return the current value, converted in points
0059     double value( ) const;
0060 
0061     /// Set minimum value in points.
0062     void setMinimum(double min);
0063 
0064     /// Set maximum value in points.
0065     void setMaximum(double max);
0066 
0067     /// Set step size in the current unit.
0068     void setLineStep(double step);
0069 
0070     /// Set step size in points.
0071     void setLineStepPt(double step);
0072 
0073     /// Set minimum, maximum value and the step size (all in points)
0074     void setMinMaxStep( double min, double max, double step );
0075 
0076     /**
0077      * Transform the double in a nice text, using locale symbols
0078      * @param value the number as double
0079      * @return the resulting string
0080      */
0081     QString textFromValue( double value ) const override;
0082 
0083     //! \brief get the text in the spinbox without prefix or suffix, and remove unit symbol if present.
0084     QString veryCleanText() const override;
0085 
0086     /**
0087      * Transform a string into a double, while taking care of locale specific symbols.
0088      * @param str the string to transform into a number
0089      * @return the value as double
0090      */
0091     double valueFromText( const QString& str ) const override;
0092 
0093     void setUnitChangeFromOutsideBehavior(bool toggle); //if set to false, setting the unit using KoUnit won't have any effect.
0094 
0095     //! \brief display the unit symbol in the spinbox or not. For example if the unit is displayed in a combobox connected to the unit manager.
0096     void setDisplayUnit(bool toggle);
0097 
0098     void preventDecimalsChangeFromUnitManager(bool prevent);
0099 
0100 Q_SIGNALS:
0101     /// emitted like valueChanged in the parent, but this one emits the point value, or converted to another reference unit.
0102     void valueChangedPt( qreal );
0103 
0104 
0105 private:
0106     class Private;
0107     Private * const d;
0108 
0109     QString detectUnit();
0110     QString makeTextClean(QString const& txt) const;
0111 
0112     //those functions are useful to sync the spinbox with its unitmanager.
0113     //! \brief change the unit, reset the spin box every time. From the outside it's always set unit that should be called.
0114     void internalUnitChange(QString const& symbol);
0115     void prepareUnitChange();
0116 
0117 private Q_SLOTS:
0118     // exists to do emits for valueChangedPt
0119     void privateValueChanged();
0120     void detectUnitChanges();
0121     void disconnectExternalUnitManager();
0122 
0123 };
0124 
0125 #endif // KIS_DOUBLEPARSEUNITSPINBOX_H