File indexing completed on 2024-05-12 04:21:32
0001 /* This file is part of the KDE libraries 0002 * Copyright (c) 1997 Patrick Dowler <dowler@morgul.fsh.uvic.ca> 0003 * Copyright (c) 2000 Dirk Mueller <mueller@kde.org> 0004 * Copyright (c) 2002 Marc Mutz <mutz@kde.org> 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Library General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2 of the License, or (at your option) any later version. 0010 * 0011 * This library is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Library General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Library General Public License 0017 * along with this library; see the file COPYING.LIB. If not, write to 0018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 * Boston, MA 02110-1301, USA. 0020 */ 0021 0022 #ifndef kpNumInput_H 0023 #define kpNumInput_H 0024 0025 #include <QWidget> 0026 #include <QSpinBox> 0027 0028 class QSlider; 0029 0030 class kpNumInputPrivate; 0031 0032 /** 0033 * You need to inherit from this class if you want to implement K*NumInput 0034 * for a different variable type 0035 * 0036 */ 0037 class kpNumInput : public QWidget 0038 { 0039 Q_OBJECT 0040 Q_PROPERTY(QString label READ label WRITE setLabel) 0041 public: 0042 /** 0043 * Default constructor 0044 * @param parent If parent is 0, the new widget becomes a top-level 0045 * window. If parent is another widget, this widget becomes a child 0046 * window inside parent. The new widget is deleted when its parent is deleted. 0047 */ 0048 explicit kpNumInput(QWidget *parent = nullptr); 0049 0050 /** 0051 * Destructor 0052 */ 0053 ~kpNumInput() override; 0054 0055 /** 0056 * Sets the text and alignment of the main description label. 0057 * 0058 * @param label The text of the label. 0059 * Use QString() to remove an existing one. 0060 * 0061 * @param a The alignment of the label (Qt::Alignment). 0062 * Default is @p Qt:AlignLeft | @p Qt:AlignTop. 0063 * 0064 * The vertical alignment flags have special meaning with this 0065 * widget: 0066 * 0067 * @li @p Qt:AlignTop The label is placed above the edit/slider 0068 * @li @p Qt:AlignVCenter The label is placed left beside the edit 0069 * @li @p Qt:AlignBottom The label is placed below the edit/slider 0070 * 0071 */ 0072 virtual void setLabel(const QString &label, Qt::Alignment a = Qt::AlignLeft | Qt::AlignTop); 0073 0074 /** 0075 * @return the text of the label. 0076 */ 0077 QString label() const; 0078 0079 /** 0080 * @return if the num input has a slider. 0081 */ 0082 bool showSlider() const; 0083 0084 /** 0085 * Sets the spacing of tickmarks for the slider. 0086 * 0087 * @param minor Minor tickmark separation. 0088 * @param major Major tickmark separation. 0089 */ 0090 void setSteps(int minor, int major); 0091 0092 /** 0093 * Returns a size which fits the contents of the control. 0094 * 0095 * @return the preferred size necessary to show the control 0096 */ 0097 QSize sizeHint() const override; 0098 0099 protected: 0100 /** 0101 * @return the slider widget. 0102 * @internal 0103 */ 0104 QSlider *slider() const; 0105 0106 /** 0107 * Call this function whenever you change something in the geometry 0108 * of your kpNumInput child. 0109 * 0110 */ 0111 void layout(); 0112 0113 /** 0114 * You need to overwrite this method and implement your layout 0115 * calculations there. 0116 * 0117 * See kpIntNumInput::doLayout and kpDoubleNumInput::doLayout implementation 0118 * for details. 0119 * 0120 */ 0121 virtual void doLayout() = 0; 0122 0123 private: 0124 friend class kpNumInputPrivate; 0125 kpNumInputPrivate *const d; 0126 0127 Q_DISABLE_COPY(kpNumInput) 0128 }; 0129 0130 /* ------------------------------------------------------------------------ */ 0131 0132 /** 0133 * @short An input widget for integer numbers, consisting of a spinbox and a slider. 0134 * 0135 * kpIntNumInput combines a QSpinBox and optionally a QSlider 0136 * with a label to make an easy to use control for setting some integer 0137 * parameter. This is especially nice for configuration dialogs, 0138 * which can have many such combinated controls. 0139 * 0140 * A special feature of kpIntNumInput, designed specifically for 0141 * the situation when there are several kpIntNumInputs in a column, 0142 * is that you can specify what portion of the control is taken by the 0143 * QSpinBox (the remaining portion is used by the slider). This makes 0144 * it very simple to have all the sliders in a column be the same size. 0145 * 0146 * kpIntNumInput enforces the value to be in the given range. 0147 */ 0148 0149 class kpIntNumInput : public kpNumInput 0150 { 0151 Q_OBJECT 0152 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged USER true) 0153 Q_PROPERTY(int minimum READ minimum WRITE setMinimum) 0154 Q_PROPERTY(int maximum READ maximum WRITE setMaximum) 0155 Q_PROPERTY(int singleStep READ singleStep WRITE setSingleStep) 0156 Q_PROPERTY(QString suffix READ suffix WRITE setSuffix) 0157 Q_PROPERTY(QString specialValueText READ specialValueText WRITE setSpecialValueText) 0158 0159 public: 0160 /** 0161 * Constructs an input control for integer values 0162 * with initial value 0. 0163 */ 0164 explicit kpIntNumInput(QWidget *parent = nullptr); 0165 /** 0166 * Constructor 0167 * It constructs a QSpinBox that allows the input of integer numbers 0168 * in the range of -INT_MAX to +INT_MAX. To set a descriptive label, 0169 * use setLabel(). To enforce the value being in a range and optionally to 0170 * attach a slider to it, use setRange(). 0171 * 0172 * @param value initial value for the control 0173 * @param parent parent QWidget 0174 */ 0175 explicit kpIntNumInput(int value, QWidget *parent = nullptr); 0176 0177 /** 0178 * Destructor 0179 * 0180 * 0181 */ 0182 ~kpIntNumInput() override; 0183 0184 /** 0185 * @return the current value. 0186 */ 0187 int value() const; 0188 0189 /** 0190 * @return the suffix displayed behind the value. 0191 * @see setSuffix() 0192 */ 0193 QString suffix() const; 0194 /** 0195 * @return the string displayed for a special value. 0196 * @see setSpecialValueText() 0197 */ 0198 QString specialValueText() const; 0199 0200 /** 0201 * Sets the allowed input range and the step size for the slider and the 0202 * spin box. 0203 * 0204 * @param min minimum value 0205 * @param max maximum value 0206 * @param step step size 0207 */ 0208 void setRange(int min, int max, int singleStep = 1); 0209 0210 /** 0211 * Sets the minimum value. 0212 */ 0213 void setMinimum(int min); 0214 /** 0215 * @return the minimum value. 0216 */ 0217 int minimum() const; 0218 /** 0219 * Sets the maximum value. 0220 */ 0221 void setMaximum(int max); 0222 /** 0223 * @return the maximum value. 0224 */ 0225 int maximum() const; 0226 0227 /** 0228 * @return the step of the spin box 0229 */ 0230 int singleStep() const; 0231 0232 /** 0233 * @return the step of the spin box 0234 */ 0235 void setSingleStep(int step); 0236 0237 /** 0238 * Sets the special value text. If set, the SpinBox will display 0239 * this text instead of the numeric value whenever the current 0240 * value is equal to minVal(). Typically this is used for indicating 0241 * that the choice has a special (default) meaning. 0242 */ 0243 void setSpecialValueText(const QString &text); 0244 0245 void setLabel(const QString &label, Qt::Alignment a = Qt::AlignLeft | Qt::AlignTop) override; 0246 0247 /** 0248 * This method returns the minimum size necessary to display the 0249 * control. The minimum size is enough to show all the labels 0250 * in the current font (font change may invalidate the return value). 0251 * 0252 * @return the minimum size necessary to show the control 0253 */ 0254 QSize minimumSizeHint() const override; 0255 0256 public Q_SLOTS: 0257 /** 0258 * Sets the value of the control. 0259 */ 0260 void setValue(int); 0261 0262 /** 0263 * Sets the suffix to @p suffix. 0264 * Use QString() to disable this feature. 0265 * Formatting has to be provided (e.g. a space separator between the 0266 * prepended @p value and the suffix's text has to be provided 0267 * as the first character in the suffix). 0268 * 0269 * @see QSpinBox::setSuffix() 0270 */ 0271 void setSuffix(const QString &suffix); 0272 0273 /** 0274 * sets focus to the edit widget and marks all text in if mark == true 0275 * 0276 */ 0277 void setEditFocus(bool mark = true); 0278 0279 Q_SIGNALS: 0280 /** 0281 * Emitted every time the value changes (by calling setValue() or 0282 * by user interaction). 0283 */ 0284 void valueChanged(int); 0285 0286 0287 private Q_SLOTS: 0288 void spinValueChanged(int); 0289 0290 protected: 0291 /** 0292 * @return the spin box widget. 0293 * @internal 0294 */ 0295 QSpinBox *spinBox() const; 0296 0297 void doLayout() override; 0298 void resizeEvent(QResizeEvent *) override; 0299 0300 private: 0301 void initWidget(int value); 0302 0303 private: 0304 class kpIntNumInputPrivate; 0305 friend class kpIntNumInputPrivate; 0306 kpIntNumInputPrivate *const d; 0307 0308 Q_DISABLE_COPY(kpIntNumInput) 0309 }; 0310 0311 /* ------------------------------------------------------------------------ */ 0312 0313 class kpDoubleLine; 0314 0315 /** 0316 * @short An input control for real numbers, consisting of a spinbox and a slider. 0317 * 0318 * kpDoubleNumInput combines a QSpinBox and optionally a QSlider 0319 * with a label to make an easy to use control for setting some float 0320 * parameter. This is especially nice for configuration dialogs, 0321 * which can have many such combinated controls. 0322 * 0323 * The slider is created only when the user specifies a range 0324 * for the control using the setRange function with the slider 0325 * parameter set to "true". 0326 * 0327 * A special feature of kpDoubleNumInput, designed specifically for 0328 * the situation when there are several instances in a column, 0329 * is that you can specify what portion of the control is taken by the 0330 * QSpinBox (the remaining portion is used by the slider). This makes 0331 * it very simple to have all the sliders in a column be the same size. 0332 * 0333 * @see kpIntNumInput 0334 */ 0335 0336 class kpDoubleNumInput : public kpNumInput 0337 { 0338 Q_OBJECT 0339 Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged USER true) 0340 Q_PROPERTY(double minimum READ minimum WRITE setMinimum) 0341 Q_PROPERTY(double maximum READ maximum WRITE setMaximum) 0342 Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep) 0343 Q_PROPERTY(QString suffix READ suffix WRITE setSuffix) 0344 Q_PROPERTY(QString specialValueText READ specialValueText WRITE setSpecialValueText) 0345 Q_PROPERTY(int decimals READ decimals WRITE setDecimals) 0346 0347 public: 0348 /** 0349 * Constructs an input control for double values 0350 * with initial value 0.00. 0351 */ 0352 explicit kpDoubleNumInput(QWidget *parent = nullptr); 0353 0354 /** 0355 * Constructor 0356 * 0357 * @param lower lower boundary value 0358 * @param upper upper boundary value 0359 * @param value initial value for the control 0360 * @param singleStep step size to use for up/down arrow clicks 0361 * @param precision number of digits after the decimal point 0362 * @param parent parent QWidget 0363 */ 0364 kpDoubleNumInput(double lower, double upper, double value, QWidget *parent = nullptr, double singleStep = 0.01, 0365 int precision = 2); 0366 0367 /** 0368 * destructor 0369 */ 0370 ~kpDoubleNumInput() override; 0371 0372 /** 0373 * @return the current value. 0374 */ 0375 double value() const; 0376 0377 /** 0378 * @return the suffix. 0379 * @see setSuffix() 0380 */ 0381 QString suffix() const; 0382 0383 /** 0384 * @return number of decimals. 0385 * @see setDecimals() 0386 */ 0387 int decimals() const; 0388 0389 /** 0390 * @return the string displayed for a special value. 0391 * @see setSpecialValueText() 0392 */ 0393 QString specialValueText() const; 0394 0395 /** 0396 * @param min minimum value 0397 * @param max maximum value 0398 * @param singleStep step size for the QSlider 0399 * @param slider whether the slider is created or not 0400 */ 0401 void setRange(double min, double max, double singleStep = 1); 0402 0403 /** 0404 * Sets the minimum value. 0405 */ 0406 void setMinimum(double min); 0407 /** 0408 * @return the minimum value. 0409 */ 0410 double minimum() const; 0411 /** 0412 * Sets the maximum value. 0413 */ 0414 void setMaximum(double max); 0415 /** 0416 * @return the maximum value. 0417 */ 0418 double maximum() const; 0419 0420 /** 0421 * @return the step of the spin box 0422 */ 0423 double singleStep() const; 0424 0425 /** 0426 * @return the step of the spin box 0427 */ 0428 void setSingleStep(double singleStep); 0429 0430 /** 0431 * Specifies the number of digits to use. 0432 */ 0433 void setDecimals(int decimals); 0434 0435 /** 0436 * Sets the special value text. If set, the spin box will display 0437 * this text instead of the numeric value whenever the current 0438 * value is equal to minVal(). Typically this is used for indicating 0439 * that the choice has a special (default) meaning. 0440 */ 0441 void setSpecialValueText(const QString &text); 0442 0443 void setLabel(const QString &label, Qt::Alignment a = Qt::AlignLeft | Qt::AlignTop) override; 0444 QSize minimumSizeHint() const override; 0445 0446 public Q_SLOTS: 0447 /** 0448 * Sets the value of the control. 0449 */ 0450 void setValue(double); 0451 0452 /** 0453 * Sets the suffix to be displayed to @p suffix. Use QString() to disable 0454 * this feature. Note that the suffix is attached to the value without any 0455 * spacing. So if you prefer to display a space separator, set suffix 0456 * to something like " cm". 0457 * @see setSuffix() 0458 */ 0459 void setSuffix(const QString &suffix); 0460 0461 Q_SIGNALS: 0462 /** 0463 * Emitted every time the value changes (by calling setValue() or 0464 * by user interaction). 0465 */ 0466 void valueChanged(double); 0467 0468 private Q_SLOTS: 0469 void sliderMoved(int); 0470 void spinBoxChanged(double); 0471 0472 protected: 0473 void doLayout() override; 0474 void resizeEvent(QResizeEvent *) override; 0475 0476 friend class kpDoubleLine; 0477 private: 0478 void initWidget(double value, double lower, double upper, 0479 double singleStep, int precision); 0480 double mapSliderToSpin(int) const; 0481 0482 private: 0483 class kpDoubleNumInputPrivate; 0484 friend class kpDoubleNumInputPrivate; 0485 kpDoubleNumInputPrivate *const d; 0486 0487 Q_DISABLE_COPY(kpDoubleNumInput) 0488 }; 0489 0490 0491 #endif // kpNumInput_H