kalarm/lib

spinbox2.h
1 /*
2  * spinbox2.h - spin box with extra pair of spin buttons (for TQt 3)
3  * Program: kalarm
4  * Copyright © 2001-2007 by David Jarvie <software@astrojar.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #ifndef SPINBOX2_H
22 #define SPINBOX2_H
23 
24 #include <tqglobal.h>
25 #include <tqlineedit.h>
26 
27 class SpinMirror;
28 class ExtraSpinBox;
29 #include "spinbox.h"
30 
31 
56 class SpinBox2 : public TQFrame
57 {
58  Q_OBJECT
59 
60  public:
65  explicit SpinBox2(TQWidget* parent = 0, const char* name = 0);
74  SpinBox2(int minValue, int maxValue, int step = 1, int step2 = 1,
75  TQWidget* parent = 0, const char* name = 0);
79  virtual void setReadOnly(bool readOnly);
81  bool isReadOnly() const { return mSpinbox->isReadOnly(); }
83  void setSelectOnStep(bool sel) { mSpinbox->setSelectOnStep(sel); }
87  void setReverseWithLayout(bool reverse);
89  bool reverseButtons() const { return mReverseLayout && !mReverseWithLayout; }
90 
92  TQString text() const { return mSpinbox->text(); }
94  virtual TQString prefix() const { return mSpinbox->prefix(); }
96  virtual TQString suffix() const { return mSpinbox->suffix(); }
98  virtual TQString cleanText() const { return mSpinbox->cleanText(); }
99 
103  virtual void setSpecialValueText(const TQString& text) { mSpinbox->setSpecialValueText(text); }
107  TQString specialValueText() const { return mSpinbox->specialValueText(); }
108 
112  virtual void setWrapping(bool on);
116  bool wrapping() const { return mSpinbox->wrapping(); }
117 
119  void setAlignment(int a) { mSpinbox->setAlignment(a); }
121  virtual void setButtonSymbols(TQSpinBox::ButtonSymbols);
123  TQSpinBox::ButtonSymbols buttonSymbols() const { return mSpinbox->buttonSymbols(); }
124 
128  virtual void setValidator(const TQValidator* v) { mSpinbox->setValidator(v); }
132  const TQValidator* validator() const { return mSpinbox->validator(); }
133 
134  virtual TQSize sizeHint() const;
135  virtual TQSize minimumSizeHint() const;
136 
138  int minValue() const { return mMinValue; }
140  int maxValue() const { return mMaxValue; }
142  virtual void setMinValue(int val);
144  virtual void setMaxValue(int val);
146  void setRange(int minValue, int maxValue) { setMinValue(minValue); setMaxValue(maxValue); }
148  int value() const { return mSpinbox->value(); }
150  int bound(int val) const;
151 
153  TQRect upRect() const { return mSpinbox->upRect(); }
155  TQRect downRect() const { return mSpinbox->downRect(); }
157  TQRect up2Rect() const;
159  TQRect down2Rect() const;
160 
165  int lineStep() const { return mLineStep; }
170  int lineShiftStep() const { return mLineShiftStep; }
175  int pageStep() const { return mPageStep; }
180  int pageShiftStep() const { return mPageShiftStep; }
185  void setLineStep(int step);
192  void setSteps(int line, int page);
199  void setShiftSteps(int line, int page);
200 
204  void addPage() { addValue(mPageStep); }
208  void subtractPage() { addValue(-mPageStep); }
212  void addLine() { addValue(mLineStep); }
216  void subtractLine() { addValue(-mLineStep); }
218  void addValue(int change) { mSpinbox->addValue(change); }
219 
220  public slots:
222  virtual void setValue(int val) { mSpinbox->setValue(val); }
224  virtual void setPrefix(const TQString& text) { mSpinbox->setPrefix(text); }
226  virtual void setSuffix(const TQString& text) { mSpinbox->setSuffix(text); }
230  virtual void stepUp() { addValue(mLineStep); }
234  virtual void stepDown() { addValue(-mLineStep); }
238  virtual void pageUp() { addValue(mPageStep); }
242  virtual void pageDown() { addValue(-mPageStep); }
244  virtual void selectAll() { mSpinbox->selectAll(); }
246  virtual void setEnabled(bool enabled);
247 
248  signals:
250  void valueChanged(int value);
252  void valueChanged(const TQString& valueText);
253 
254  protected:
255  virtual TQString mapValueToText(int v) { return mSpinbox->mapValToText(v); }
256  virtual int mapTextToValue(bool* ok) { return mSpinbox->mapTextToVal(ok); }
257  virtual void resizeEvent(TQResizeEvent*) { arrange(); }
258  virtual void showEvent(TQShowEvent*);
259  virtual void styleChange(TQStyle&);
260  virtual void getMetrics() const;
261 
262  mutable int wUpdown2; // width of second spin widget
263  mutable int xUpdown2; // x offset of visible area in 'mUpdown2'
264  mutable int xSpinbox; // x offset of visible area in 'mSpinbox'
265  mutable int wGap; // gap between mUpdown2Frame and mSpinboxFrame
266 
267  protected slots:
268  virtual void valueChange();
269  virtual void stepPage(int);
270 
271  private slots:
272  void updateMirror();
273 
274  private:
275  void init();
276  void arrange();
277  int whichButton(TQObject* spinWidget, const TQPoint&);
278  void setShiftStepping(bool on);
279 
280  // Visible spin box class.
281  // Declared here to allow use of mSpinBox in inline methods.
282  class MainSpinBox : public SpinBox
283  {
284  public:
285  MainSpinBox(SpinBox2* sb2, TQWidget* parent, const char* name = 0)
286  : SpinBox(parent, name), owner(sb2) { }
287  MainSpinBox(int minValue, int maxValue, int step, SpinBox2* sb2, TQWidget* parent, const char* name = 0)
288  : SpinBox(minValue, maxValue, step, parent, name), owner(sb2) { }
289  void setAlignment(int a) { editor()->setAlignment(a); }
290  virtual TQString mapValueToText(int v) { return owner->mapValueToText(v); }
291  virtual int mapTextToValue(bool* ok) { return owner->mapTextToValue(ok); }
292  TQString mapValToText(int v) { return SpinBox::mapValueToText(v); }
293  int mapTextToVal(bool* ok) { return SpinBox::mapTextToValue(ok); }
294  virtual int shiftStepAdjustment(int oldValue, int shiftStep);
295  private:
296  SpinBox2* owner; // owner SpinBox2
297  };
298 
299  enum { NO_BUTTON = -1, UP, DOWN, UP2, DOWN2 };
300 
301  static int mReverseLayout; // widgets are mirrored right to left
302  TQFrame* mUpdown2Frame; // contains visible part of the extra pair of spin buttons
303  TQFrame* mSpinboxFrame; // contains the main spin box
304  ExtraSpinBox* mUpdown2; // the extra pair of spin buttons
305  MainSpinBox* mSpinbox; // the visible spin box
306  SpinMirror* mSpinMirror; // image of the extra pair of spin buttons
307  int mMinValue;
308  int mMaxValue;
309  int mLineStep; // right button increment
310  int mLineShiftStep; // right button increment with shift pressed
311  int mPageStep; // left button increment
312  int mPageShiftStep; // left button increment with shift pressed
313  bool mReverseWithLayout; // reverse button positions if reverse layout (default = true)
314 
315  friend class MainSpinBox;
316 };
317 
318 #endif // SPINBOX2_H