File indexing completed on 2024-06-02 05:19:17

0001 /*
0002  *  spinbox2.h  -  spin box with extra pair of spin buttons
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2001-2022 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include "spinbox2_p.h"
0012 
0013 /**
0014  *  @short Spin box with a pair of spin buttons on either side.
0015  *
0016  *  The SpinBox2 class provides a spin box with two pairs of spin buttons, one on either side,
0017  *  provided that the current style places the two spin buttons on the same side of a QSpinBox.
0018  *
0019  *  It is designed as a base class for implementing such facilities as time spin boxes, where
0020  *  the hours and minutes values are separately displayed in the edit field. When the
0021  *  appropriate step increments are configured, the left spin arrows can then be used to
0022  *  change the hours value, while the right spin arrows can be used to change the minutes
0023  *  value.
0024  *
0025  *  Rather than using SpinBox2 directly for time entry, use in preference TimeSpinBox or
0026  *  TimeEdit classes which are tailored from SpinBox2 for this purpose.
0027  *
0028  *  Separate step increments may optionally be specified for use when the shift key is
0029  *  held down. Typically these would be larger than the normal steps. Then, when the user
0030  *  clicks the spin buttons, he/she can increment or decrement the value faster by holding
0031  *  the shift key down.
0032  *
0033  *  The widget may be set as read-only. This has the same effect as disabling it, except
0034  *  that its appearance is unchanged.
0035  *
0036  *  @author David Jarvie <djarvie@kde.org>
0037  */
0038 class SpinBox2 : public QWidget
0039 {
0040     Q_OBJECT
0041 public:
0042     /** Constructor.
0043      *  @param parent The parent object of this widget.
0044      */
0045     explicit SpinBox2(QWidget* parent = nullptr);
0046 
0047     /** Constructor.
0048      *  @param minValue The minimum value which the spin box can have.
0049      *  @param maxValue The maximum value which the spin box can have.
0050      *  @param pageStep The (unshifted) step interval for the left-hand spin buttons.
0051      *  @param parent The parent object of this widget.
0052      */
0053     SpinBox2(int minValue, int maxValue, int pageStep = 1, QWidget* parent = nullptr);
0054 
0055     /** Sets whether the spin box can be changed by the user.
0056      *  @param readOnly True to set the widget read-only, false to set it read-write.
0057      */
0058     virtual void     setReadOnly(bool readOnly)  { return mSpinbox2->setReadOnly(readOnly); }
0059 
0060     /** Returns true if the widget is read only. */
0061     bool             isReadOnly() const          { return mSpinbox2->isReadOnly(); }
0062 
0063     /** Sets whether the spin box value text should be selected when its value is stepped. */
0064     void             setSelectOnStep(bool sel)   { mSpinbox2->setSelectOnStep(sel); }
0065 
0066     /** Sets whether the spin button pairs should be reversed for a right-to-left language.
0067      *  The default is for them to be reversed.
0068      */
0069     void             setReverseWithLayout(bool reverse)  { mSpinbox2->setReverseWithLayout(reverse); }
0070 
0071     /** Returns whether the spin button pairs will be reversed for a right-to-left language. */
0072     bool             reverseButtons() const      { return mSpinbox2->reverseButtons(); }
0073 
0074     /** Returns the spin box's text, including any prefix() and suffix(). */
0075     QString          text() const                { return mSpinbox2->text(); }
0076 
0077     /** Returns the prefix for the spin box's text. */
0078     QString          prefix() const              { return mSpinbox2->prefix(); }
0079 
0080     /** Returns the suffix for the spin box's text. */
0081     QString          suffix() const              { return mSpinbox2->suffix(); }
0082 
0083     /** Sets the prefix which is prepended to the start of the displayed text. */
0084     void             setPrefix(const QString& text)  { mSpinbox2->setPrefix(text); }
0085 
0086     /** Sets the suffix which is prepended to the start of the displayed text. */
0087     void             setSuffix(const QString& text)  { mSpinbox2->setSuffix(text); }
0088 
0089     /** Returns the spin box's text with no prefix(), suffix() or leading or trailing whitespace. */
0090     QString          cleanText() const           { return mSpinbox2->cleanText(); }
0091 
0092     /** Sets the special-value text which, if non-null, is displayed instead of a numeric
0093      *  value when the current value is equal to minimum().
0094      */
0095     void             setSpecialValueText(const QString& text)  { mSpinbox2->setSpecialValueText(text); }
0096 
0097     /** Returns the special-value text which, if non-null, is displayed instead of a numeric
0098      *  value when the current value is equal to minimum().
0099      */
0100     QString          specialValueText() const    { return mSpinbox2->specialValueText(); }
0101 
0102     /** Sets whether it is possible to step the value from the highest value to the
0103      *  lowest value and vice versa.
0104      */
0105     void             setWrapping(bool on)        { mSpinbox2->setWrapping(on); }
0106 
0107     /** Returns whether it is possible to step the value from the highest value to the
0108      *  lowest value and vice versa.
0109      */
0110     bool             wrapping() const            { return mSpinbox2->wrapping(); }
0111 
0112     /** Set the text alignment of the widget */
0113     void             setAlignment(Qt::Alignment a) { mSpinbox2->setAlignment(a); }
0114 
0115     /** Sets the button symbols to use (arrows or plus/minus). */
0116     void             setButtonSymbols(QSpinBox::ButtonSymbols s)  { mSpinbox2->setButtonSymbols(s); }
0117 
0118     /** Returns the button symbols currently in use (arrows or plus/minus). */
0119     QSpinBox::ButtonSymbols buttonSymbols() const   { return mSpinbox2->buttonSymbols(); }
0120 
0121     /** Determine whether the current input is valid. */
0122     virtual QValidator::State validate(QString& s, int& pos) const  { return mSpinbox2->validate(s, pos); }
0123 
0124     QSize            sizeHint() const override         { return mSpinbox2->sizeHint(); }
0125     QSize            minimumSizeHint() const override  { return mSpinbox2->minimumSizeHint(); }
0126 
0127     /** Returns the minimum value of the spin box. */
0128     int              minimum() const             { return mSpinbox2->minimum(); }
0129 
0130     /** Returns the maximum value of the spin box. */
0131     int              maximum() const             { return mSpinbox2->maximum(); }
0132 
0133     /** Sets the minimum value of the spin box. */
0134     virtual void     setMinimum(int val)         { mSpinbox2->setMinimum(val); }
0135 
0136     /** Sets the maximum value of the spin box. */
0137     virtual void     setMaximum(int val)         { mSpinbox2->setMaximum(val); }
0138 
0139     /** Sets the minimum and maximum values of the spin box. */
0140     void             setRange(int minValue, int maxValue)   { mSpinbox2->setRange(minValue, maxValue); }
0141 
0142     /** Returns the current value of the spin box. */
0143     int              value() const               { return mSpinbox2->value(); }
0144 
0145     /** Returns the specified value clamped to the range of the spin box. */
0146     int              bound(int val) const        { return mSpinbox2->bound(val); }
0147 
0148     /** Returns the geometry of the right-hand "up" button. */
0149     QRect            upRect() const              { return mSpinbox2->upRect(); }
0150 
0151     /** Returns the geometry of the right-hand "down" button. */
0152     QRect            downRect() const            { return mSpinbox2->downRect(); }
0153 
0154     /** Returns the geometry of the left-hand "up" button.
0155      *  @return Button geometry, or invalid if left-hand buttons are not visible.
0156      */
0157     QRect            up2Rect() const             { return mSpinbox2->up2Rect(); }
0158 
0159     /** Returns the geometry of the left-hand "down" button.
0160      *  @return Button geometry, or invalid if left-hand buttons are not visible.
0161      */
0162     QRect            down2Rect() const           { return mSpinbox2->down2Rect(); }
0163 
0164     /** Returns the unshifted step increment for the right-hand spin buttons,
0165      *  i.e. the amount by which the spin box value changes when a right-hand
0166      *  spin button is clicked without the shift key being pressed.
0167      */
0168     int              singleStep() const          { return mSpinbox2->singleStep(); }
0169 
0170     /** Returns the shifted step increment for the right-hand spin buttons,
0171      *  i.e. the amount by which the spin box value changes when a right-hand
0172      *  spin button is clicked while the shift key is pressed.
0173      */
0174     int              singleShiftStep() const     { return mSpinbox2->singleShiftStep(); }
0175 
0176     /** Returns the unshifted step increment for the left-hand spin buttons,
0177      *  i.e. the amount by which the spin box value changes when a left-hand
0178      *  spin button is clicked without the shift key being pressed.
0179      */
0180     int              pageStep() const            { return mSpinbox2->pageStep(); }
0181 
0182     /** Returns the shifted step increment for the left-hand spin buttons,
0183      *  i.e. the amount by which the spin box value changes when a left-hand
0184      *  spin button is clicked while the shift key is pressed.
0185      */
0186     int              pageShiftStep() const       { return mSpinbox2->pageShiftStep(); }
0187 
0188     /** Sets the unshifted step increment for the right-hand spin buttons,
0189      *  i.e. the amount by which the spin box value changes when a right-hand
0190      *  spin button is clicked without the shift key being pressed.
0191      */
0192     void             setSingleStep(int step)     { mSpinbox2->setSingleStep(step); }
0193 
0194     /** Sets the unshifted step increments for the two pairs of spin buttons,
0195      *  i.e. the amount by which the spin box value changes when a spin button
0196      *  is clicked without the shift key being pressed.
0197      *  @param line The step increment for the right-hand spin buttons.
0198      *  @param page The step increment for the left-hand spin buttons.
0199      */
0200     void             setSteps(int line, int page)  { mSpinbox2->setSteps(line, page); }
0201 
0202     /** Sets the shifted step increments for the two pairs of spin buttons,
0203      *  i.e. the amount by which the spin box value changes when a spin button
0204      *  is clicked while the shift key is pressed. It also sets the increment
0205      *  when the left spin button is clicked when the control key is pressed.
0206      *  @param line     The shift step increment for the right-hand spin buttons.
0207      *  @param page     The shift step increment for the left-hand spin buttons.
0208      *  @param control  The increment for the left-hand spin buttons when Control,
0209      *                  is pressed, or 0 to use default Qt handling which
0210      *                  multiplies the single step by 10.
0211      *  @param modControl  Control steps should always set value to multiple of @p control.
0212      */
0213     void             setShiftSteps(int line, int page, int control, bool modControl = true)  { mSpinbox2->setShiftSteps(line, page, control, modControl); }
0214 
0215     /** Increments the current value by adding the unshifted step increment for
0216      *  the left-hand spin buttons.
0217      */
0218     void             addPage()                   { mSpinbox2->addPage(); }
0219 
0220     /** Decrements the current value by subtracting the unshifted step increment for
0221      *  the left-hand spin buttons.
0222      */
0223     void             subtractPage()              { mSpinbox2->subtractPage(); }
0224 
0225     /** Increments the current value by adding the unshifted step increment for
0226      *  the right-hand spin buttons.
0227      */
0228     void             addSingle()                 { mSpinbox2->addSingle(); }
0229 
0230     /** Decrements the current value by subtracting the unshifted step increment for
0231      *  the right-hand spin buttons.
0232      */
0233     void             subtractSingle()            { mSpinbox2->subtractSingle(); }
0234 
0235     /** Adjusts the current value by adding @p change. */
0236     void             addValue(int change)        { mSpinbox2->addValue(change); }
0237 
0238     /** Increments the current value by adding the unshifted increment for
0239      *  the right-hand spin buttons.
0240      */
0241     virtual void     stepBy(int increment)       { mSpinbox2->stepBy(increment); }
0242 
0243 public Q_SLOTS:
0244     /** Sets the current value to @p val. */
0245     void             setValue(int val)           { mSpinbox2->setValue(val); }
0246     /** Increments the current value by adding the unshifted step increment for
0247      *  the left-hand spin buttons.
0248      */
0249     virtual void     pageUp()                    { mSpinbox2->pageUp(); }
0250     /** Decrements the current value by subtracting the unshifted step increment for
0251      *  the left-hand spin buttons.
0252      */
0253     virtual void     pageDown()                  { mSpinbox2->pageDown(); }
0254     /** Selects all the text in the spin box's editor. */
0255     void             selectAll()                 { mSpinbox2->selectAll(); }
0256     /** Sets whether the widget is enabled. */
0257     virtual void     setEnabled(bool enabled)    { mSpinbox2->setEnabled(enabled); }
0258 
0259 Q_SIGNALS:
0260     /** Signal which is emitted whenever the value of the spin box changes. */
0261     void             valueChanged(int value);
0262 
0263 protected:
0264     virtual QString  textFromValue(int v) const    { return mSpinbox2->textFromValue(v); }
0265     virtual int      valueFromText(const QString& t) const  { return mSpinbox2->valueFromText(t); }
0266 
0267 private:
0268     void init();
0269 
0270     SpinBox2p* mSpinbox2;
0271 
0272 friend class SpinBox2p;
0273 };
0274 
0275 // vim: et sw=4: