File indexing completed on 2024-05-19 15:27:52
0001 /* This file is part of KGraphViewer. 0002 Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr> 0003 0004 KGraphViewer is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU General Public 0006 License as published by the Free Software Foundation, version 2. 0007 0008 This program is distributed in the hope that it will be useful, 0009 but WITHOUT ANY WARRANTY; without even the implied warranty of 0010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0011 General Public License for more details. 0012 0013 You should have received a copy of the GNU General Public License 0014 along with this program; if not, write to the Free Software 0015 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0016 02110-1301, USA 0017 */ 0018 0019 /* This file was part of the KDE project 0020 Copyright (C) 2005 Jarosław Staniek <staniek@kde.org> 0021 0022 This program is free software; you can redistribute it and/or 0023 modify it under the terms of the GNU Library General Public 0024 License as published by the Free Software Foundation; either 0025 version 2 of the License, or (at your option) any later version. 0026 */ 0027 0028 #ifndef KGVUNITWIDGETS_H 0029 #define KGVUNITWIDGETS_H 0030 0031 #include <KgvUnit.h> 0032 #include <QComboBox> 0033 #include <QDoubleSpinBox> 0034 #include <QEvent> 0035 #include <QIntValidator> 0036 #include <QLineEdit> 0037 #include <QSpinBox> 0038 0039 // ---------------------------------------------------------------- 0040 // Support classes 0041 0042 class KgvUnitDoubleBase; 0043 0044 // ### TODO: put it out of the public header file (if possible) 0045 /** 0046 * Validator for the unit widget classes 0047 * \internal 0048 * \since 1.4 (change of behavior) 0049 */ 0050 class KgvUnitDoubleValidator : public QDoubleValidator 0051 { 0052 public: 0053 KgvUnitDoubleValidator(KgvUnitDoubleBase *base, QObject *parent); 0054 0055 QValidator::State validate(QString &, int &) const override; 0056 0057 private: 0058 KgvUnitDoubleBase *m_base; 0059 }; 0060 0061 /** 0062 * Base for the unit widgets 0063 * \since 1.4 (change of behavior) 0064 */ 0065 class KgvUnitDoubleBase 0066 { 0067 public: 0068 KgvUnitDoubleBase(KgvUnit::Unit unit, unsigned int precision) 0069 : m_unit(unit) 0070 , m_precision(precision) 0071 { 0072 } 0073 virtual ~KgvUnitDoubleBase() 0074 { 0075 } 0076 0077 virtual void changeValue(double) = 0; 0078 virtual void setUnit(KgvUnit::Unit = KgvUnit::U_PT) = 0; 0079 0080 void setValueInUnit(double value, KgvUnit::Unit unit) 0081 { 0082 changeValue(KgvUnit::ptToUnit(KgvUnit::fromUserValue(value, unit), m_unit)); 0083 } 0084 0085 void setPrecision(unsigned int precision) 0086 { 0087 m_precision = precision; 0088 }; 0089 0090 protected: 0091 friend class KgvUnitDoubleValidator; 0092 /** 0093 * Transform the double in a nice text, using locale symbols 0094 * @param value the number as double 0095 * @return the resulting string 0096 */ 0097 QString getVisibleText(double value) const; 0098 /** 0099 * Transform a string into a double, while taking care of locale specific symbols. 0100 * @param str the string to transform into a number 0101 * @param ok true, if the conversion was successful 0102 * @return the value as double 0103 */ 0104 double toDouble(const QString &str, bool *ok) const; 0105 0106 protected: 0107 KgvUnitDoubleValidator *m_validator; 0108 KgvUnit::Unit m_unit; 0109 unsigned int m_precision; 0110 }; 0111 0112 // ---------------------------------------------------------------- 0113 // Widget classes 0114 0115 /** 0116 * Spin box for double precision numbers with unit display 0117 * \since 1.4 (change of behavior) 0118 */ 0119 class KgvUnitDoubleSpinBox : public QDoubleSpinBox, public KgvUnitDoubleBase 0120 { 0121 Q_OBJECT 0122 public: 0123 explicit KgvUnitDoubleSpinBox(QWidget *parent = nullptr); 0124 // lower, upper, step and value are in pt 0125 KgvUnitDoubleSpinBox(QWidget *parent, double lower, double upper, double step, double value = 0.0, KgvUnit::Unit unit = KgvUnit::U_PT, unsigned int precision = 2); 0126 // added so the class can be used in .ui files(by Tymoteusz Majewski, maju7@o2.pl) 0127 void changeValue(double) override; 0128 void setUnit(KgvUnit::Unit = KgvUnit::U_PT) override; 0129 0130 /// @return the current value, converted in points 0131 double value(void) const; 0132 0133 /// Set minimum value in points. 0134 void setMinValue(double min); 0135 0136 /// Set maximum value in points. 0137 void setMaxValue(double max); 0138 0139 /// Set step size in the current unit. 0140 void setLineStep(double step); 0141 0142 /// Set step size in points. 0143 void setLineStepPt(double step); 0144 0145 /// Set minimum, maximum value and the step size (all in points) (by Tymoteusz Majewski, maju7@o2.pl) 0146 void setMinMaxStep(double min, double max, double step); 0147 0148 Q_SIGNALS: 0149 /// emitted like valueChanged in the parent, but this one emits the point value 0150 void valueChangedPt(double); 0151 0152 private: 0153 double m_lowerInPoints; ///< lowest value in points 0154 double m_upperInPoints; ///< highest value in points 0155 double m_stepInPoints; ///< step in points 0156 0157 private Q_SLOTS: 0158 // exists to do emits for valueChangedPt 0159 void privateValueChanged(); 0160 }; 0161 0162 /** 0163 * Line edit for double precision numbers with unit display 0164 * \since 1.4 (change of behavior) 0165 */ 0166 class KgvUnitDoubleLineEdit : public QLineEdit, public KgvUnitDoubleBase 0167 { 0168 Q_OBJECT 0169 public: 0170 explicit KgvUnitDoubleLineEdit(QWidget *parent = nullptr); 0171 KgvUnitDoubleLineEdit(QWidget *parent, double lower, double upper, double value = 0.0, KgvUnit::Unit unit = KgvUnit::U_PT, unsigned int precision = 2); 0172 0173 void changeValue(double) override; 0174 void setUnit(KgvUnit::Unit = KgvUnit::U_PT) override; 0175 0176 /// @return the current value, converted in points 0177 double value(void) const; 0178 0179 protected: 0180 bool eventFilter(QObject *obj, QEvent *ev) override; 0181 0182 private: 0183 double m_value; 0184 double m_lower; 0185 double m_upper; 0186 double m_lowerInPoints; ///< lowest value in points 0187 double m_upperInPoints; ///< highest value in points 0188 }; 0189 0190 /** 0191 * Combo box for double precision numbers with unit display 0192 * \since 1.4 (change of behavior) 0193 */ 0194 class KgvUnitDoubleComboBox : public QComboBox, public KgvUnitDoubleBase 0195 { 0196 Q_OBJECT 0197 public: 0198 explicit KgvUnitDoubleComboBox(QWidget *parent = nullptr); 0199 KgvUnitDoubleComboBox(QWidget *parent, double lower, double upper, double value = 0.0, KgvUnit::Unit unit = KgvUnit::U_PT, unsigned int precision = 2); 0200 0201 void changeValue(double) override; 0202 void updateValue(double); 0203 void setUnit(KgvUnit::Unit = KgvUnit::U_PT) override; 0204 0205 /// @return the current value, converted in points 0206 double value(void) const; 0207 void insertItem(double, int index = -1); 0208 0209 protected: 0210 bool eventFilter(QObject *obj, QEvent *ev) override; 0211 0212 Q_SIGNALS: 0213 void valueChanged(double); 0214 0215 private Q_SLOTS: 0216 void slotActivated(int); 0217 0218 protected: 0219 double m_value; 0220 double m_lower; 0221 double m_upper; 0222 double m_lowerInPoints; ///< lowest value in points 0223 double m_upperInPoints; ///< highest value in points 0224 }; 0225 0226 /** 0227 * Combo box (with spin control) for double precision numbers with unit display 0228 * \since 1.4 (change of behavior) 0229 */ 0230 class KgvUnitDoubleSpinComboBox : public QWidget 0231 { 0232 Q_OBJECT 0233 public: 0234 explicit KgvUnitDoubleSpinComboBox(QWidget *parent = nullptr); 0235 KgvUnitDoubleSpinComboBox(QWidget *parent, double lower, double upper, double step, double value = 0.0, KgvUnit::Unit unit = KgvUnit::U_PT, unsigned int precision = 2); 0236 0237 void insertItem(double, int index = -1); 0238 void updateValue(double); 0239 /// @return the current value, converted in points 0240 double value(void) const; 0241 0242 Q_SIGNALS: 0243 void valueChanged(double); 0244 0245 private Q_SLOTS: 0246 void slotUpClicked(); 0247 void slotDownClicked(); 0248 0249 private: 0250 KgvUnitDoubleComboBox *m_combo; 0251 double m_step; 0252 }; 0253 0254 #endif // KGVUNITWIDGETS_H