kalarm/lib

spinbox2.h

00001 /*
00002  *  spinbox2.h  -  spin box with extra pair of spin buttons (for Qt 3)
00003  *  Program:  kalarm
00004  *  Copyright © 2001-2007 by David Jarvie <software@astrojar.org.uk>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #ifndef SPINBOX2_H
00022 #define SPINBOX2_H
00023 
00024 #include <tqglobal.h>
00025 #include <tqlineedit.h>
00026 
00027 class SpinMirror;
00028 class ExtraSpinBox;
00029 #include "spinbox.h"
00030 
00031 
00056 class SpinBox2 : public QFrame
00057 {
00058         Q_OBJECT
00059     public:
00064         explicit SpinBox2(TQWidget* parent = 0, const char* name = 0);
00073         SpinBox2(int minValue, int maxValue, int step = 1, int step2 = 1,
00074                  TQWidget* parent = 0, const char* name = 0);
00078         virtual void        setReadOnly(bool readOnly);
00080         bool                isReadOnly() const          { return mSpinbox->isReadOnly(); }
00082         void                setSelectOnStep(bool sel)   { mSpinbox->setSelectOnStep(sel); }
00086         void                setReverseWithLayout(bool reverse);
00088         bool                reverseButtons() const      { return mReverseLayout  &&  !mReverseWithLayout; }
00089 
00091         TQString             text() const                { return mSpinbox->text(); }
00093         virtual TQString     prefix() const              { return mSpinbox->prefix(); }
00095         virtual TQString     suffix() const              { return mSpinbox->suffix(); }
00097         virtual TQString     cleanText() const           { return mSpinbox->cleanText(); }
00098 
00102         virtual void        setSpecialValueText(const TQString& text)  { mSpinbox->setSpecialValueText(text); }
00106         TQString             specialValueText() const    { return mSpinbox->specialValueText(); }
00107 
00111         virtual void        setWrapping(bool on);
00115         bool                wrapping() const            { return mSpinbox->wrapping(); }
00116 
00118         void                setAlignment(int a)         { mSpinbox->setAlignment(a); }
00120         virtual void        setButtonSymbols(TQSpinBox::ButtonSymbols);
00122         TQSpinBox::ButtonSymbols buttonSymbols() const   { return mSpinbox->buttonSymbols(); }
00123 
00127         virtual void        setValidator(const TQValidator* v)  { mSpinbox->setValidator(v); }
00131         const TQValidator*   validator() const           { return mSpinbox->validator(); }
00132 
00133         virtual TQSize       sizeHint() const;
00134         virtual TQSize       minimumSizeHint() const;
00135 
00137         int                 minValue() const            { return mMinValue; }
00139         int                 maxValue() const            { return mMaxValue; }
00141         virtual void        setMinValue(int val);
00143         virtual void        setMaxValue(int val);
00145         void                setRange(int minValue, int maxValue)   { setMinValue(minValue);  setMaxValue(maxValue); }
00147         int                 value() const               { return mSpinbox->value(); }
00149         int                 bound(int val) const;
00150 
00152         TQRect               upRect() const              { return mSpinbox->upRect(); }
00154         TQRect               downRect() const            { return mSpinbox->downRect(); }
00156         TQRect               up2Rect() const;
00158         TQRect               down2Rect() const;
00159 
00164         int                 lineStep() const            { return mLineStep; }
00169         int                 lineShiftStep() const       { return mLineShiftStep; }
00174         int                 pageStep() const            { return mPageStep; }
00179         int                 pageShiftStep() const       { return mPageShiftStep; }
00184         void                setLineStep(int step);
00191         void                setSteps(int line, int page);
00198         void                setShiftSteps(int line, int page);
00199 
00203         void                addPage()                   { addValue(mPageStep); }
00207         void                subtractPage()              { addValue(-mPageStep); }
00211         void                addLine()                   { addValue(mLineStep); }
00215         void                subtractLine()              { addValue(-mLineStep); }
00217         void                addValue(int change)        { mSpinbox->addValue(change); }
00218 
00219     public slots:
00221         virtual void        setValue(int val)           { mSpinbox->setValue(val); }
00223         virtual void        setPrefix(const TQString& text)  { mSpinbox->setPrefix(text); }
00225         virtual void        setSuffix(const TQString& text)  { mSpinbox->setSuffix(text); }
00229         virtual void        stepUp()                    { addValue(mLineStep); }
00233         virtual void        stepDown()                  { addValue(-mLineStep); }
00237         virtual void        pageUp()                    { addValue(mPageStep); }
00241         virtual void        pageDown()                  { addValue(-mPageStep); }
00243         virtual void        selectAll()                 { mSpinbox->selectAll(); }
00245         virtual void        setEnabled(bool enabled);
00246 
00247     signals:
00249         void                valueChanged(int value);
00251         void                valueChanged(const TQString& valueText);
00252 
00253     protected:
00254         virtual TQString     mapValueToText(int v)         { return mSpinbox->mapValToText(v); }
00255         virtual int         mapTextToValue(bool* ok)      { return mSpinbox->mapTextToVal(ok); }
00256         virtual void        resizeEvent(TQResizeEvent*)    { arrange(); }
00257         virtual void        showEvent(TQShowEvent*);
00258         virtual void        styleChange(TQStyle&);
00259         virtual void        getMetrics() const;
00260 
00261         mutable int      wUpdown2;        // width of second spin widget
00262         mutable int      xUpdown2;        // x offset of visible area in 'mUpdown2'
00263         mutable int      xSpinbox;        // x offset of visible area in 'mSpinbox'
00264         mutable int      wGap;            // gap between mUpdown2Frame and mSpinboxFrame
00265 
00266     protected slots:
00267         virtual void        valueChange();
00268         virtual void        stepPage(int);
00269 
00270     private slots:
00271         void                updateMirror();
00272 
00273     private:
00274         void                init();
00275         void                arrange();
00276         int                 whichButton(TQObject* spinWidget, const TQPoint&);
00277         void                setShiftStepping(bool on);
00278 
00279         // Visible spin box class.
00280         // Declared here to allow use of mSpinBox in inline methods.
00281         class MainSpinBox : public SpinBox
00282         {
00283             public:
00284                 MainSpinBox(SpinBox2* sb2, TQWidget* parent, const char* name = 0)
00285                                 : SpinBox(parent, name), owner(sb2) { }
00286                 MainSpinBox(int minValue, int maxValue, int step, SpinBox2* sb2, TQWidget* parent, const char* name = 0)
00287                                 : SpinBox(minValue, maxValue, step, parent, name), owner(sb2) { }
00288                 void            setAlignment(int a)       { editor()->setAlignment(a); }
00289                 virtual TQString mapValueToText(int v)     { return owner->mapValueToText(v); }
00290                 virtual int     mapTextToValue(bool* ok)  { return owner->mapTextToValue(ok); }
00291                 TQString         mapValToText(int v)       { return SpinBox::mapValueToText(v); }
00292                 int             mapTextToVal(bool* ok)    { return SpinBox::mapTextToValue(ok); }
00293                 virtual int     shiftStepAdjustment(int oldValue, int shiftStep);
00294             private:
00295                 SpinBox2* owner;   // owner SpinBox2
00296         };
00297 
00298         enum { NO_BUTTON = -1, UP, DOWN, UP2, DOWN2 };
00299 
00300         static int       mReverseLayout;  // widgets are mirrored right to left
00301         TQFrame*          mUpdown2Frame;   // contains visible part of the extra pair of spin buttons
00302         TQFrame*          mSpinboxFrame;   // contains the main spin box
00303         ExtraSpinBox*    mUpdown2;        // the extra pair of spin buttons
00304         MainSpinBox*     mSpinbox;        // the visible spin box
00305         SpinMirror*      mSpinMirror;     // image of the extra pair of spin buttons
00306         int              mMinValue;
00307         int              mMaxValue;
00308         int              mLineStep;           // right button increment
00309         int              mLineShiftStep;      // right button increment with shift pressed
00310         int              mPageStep;           // left button increment
00311         int              mPageShiftStep;      // left button increment with shift pressed
00312         bool             mReverseWithLayout;  // reverse button positions if reverse layout (default = true)
00313 
00314     friend class MainSpinBox;
00315 };
00316 
00317 #endif // SPINBOX2_H