kalarm Library API Documentation

spinbox2.cpp

00001 /*
00002  *  spinbox2.cpp  -  spin box with extra pair of spin buttons (for QT3)
00003  *  Program:  kalarm
00004  *  (C) 2001, 2002 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
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *
00020  *  As a special exception, permission is given to link this program
00021  *  with any edition of Qt, and distribute the resulting executable,
00022  *  without including the source code for Qt in the source distribution.
00023  */
00024 
00025 #include <qglobal.h>
00026 #if QT_VERSION >= 300
00027 
00028 #include <qstyle.h>
00029 
00030 #include "spinbox2.moc"
00031 
00032 
00033 SpinBox2::SpinBox2(QWidget* parent, const char* name)
00034         : QFrame(parent, name)
00035 {
00036         updown2Frame = new QFrame(this);
00037         spinboxFrame = new QFrame(this);
00038         updown2 = new SB2_SpinWidget(updown2Frame, "updown2");
00039         spinbox = new SB2_SpinBox(this, spinboxFrame);
00040         initSpinBox2();
00041 }
00042 
00043 
00044 SpinBox2::SpinBox2(int minValue, int maxValue, int step, int step2, QWidget* parent, const char* name)
00045         : QFrame(parent, name)
00046 {
00047         updown2Frame = new QFrame(this);
00048         spinboxFrame = new QFrame(this);
00049         updown2 = new SB2_SpinWidget(minValue, maxValue, step2, updown2Frame, "updown2");
00050         spinbox = new SB2_SpinBox(minValue, maxValue, step, this, spinboxFrame);
00051         spinbox->setSteps(step, step2);
00052         initSpinBox2();
00053 }
00054 
00055 void SpinBox2::initSpinBox2()
00056 {
00057         selectOnStep = false;
00058         setFocusProxy(spinbox);
00059         connect(spinbox, SIGNAL(valueChanged(int)), SLOT(valueChange()));
00060         connect(spinbox, SIGNAL(valueChanged(int)), SIGNAL(valueChanged(int)));
00061         connect(spinbox, SIGNAL(valueChanged(const QString&)), SIGNAL(valueChanged(const QString&)));
00062         connect(updown2, SIGNAL(stepped(int)), SLOT(stepped2(int)));
00063 }
00064 
00065 void SpinBox2::setButtonSymbols(QSpinBox::ButtonSymbols newSymbols)
00066 {
00067         if (spinbox->buttonSymbols() == newSymbols)
00068                 return;
00069         spinbox->setButtonSymbols(newSymbols);
00070         updown2->setButtonSymbols(newSymbols);
00071 }
00072 
00073 void SpinBox2::addVal(int change)
00074 {
00075         int newval = spinbox->value() + change;
00076         if (newval > spinbox->maxValue())
00077         {
00078                 if (spinbox->wrapping())
00079                 {
00080                         int range = spinbox->maxValue() - spinbox->minValue() + 1;
00081                         newval = spinbox->minValue() + (newval - spinbox->maxValue() - 1) % range;
00082                 }
00083                 else
00084                         newval = spinbox->maxValue();
00085         }
00086         else if (newval < spinbox->minValue())
00087         {
00088                 if (spinbox->wrapping())
00089                 {
00090                         int range = spinbox->maxValue() - spinbox->minValue() + 1;
00091                         newval = spinbox->maxValue() - (spinbox->minValue() - 1 - newval) % range;
00092                 }
00093                 else
00094                         newval = spinbox->minValue();
00095         }
00096         spinbox->setValue(newval);
00097 }
00098 
00099 void SpinBox2::valueChange()
00100 {
00101         bool blocked = updown2->signalsBlocked();
00102         updown2->blockSignals(true);
00103         updown2->setValue(spinbox->value());
00104         updown2->blockSignals(blocked);
00105 }
00106 
00107 void SpinBox2::stepped2(int direction)
00108 {
00109         bool focus = selectOnStep && updown2->hasFocus();
00110         if (focus)
00111                 spinbox->setFocus();    // make displayed text be selected, as for stepping with the spinbox buttons
00112         int step = spinbox->pageStep();
00113         addVal(direction >= 0 ? step : -step);
00114         if (focus)
00115                 updown2->setFocus();
00116 }
00117 
00118 // Called when the widget is about to be displayed.
00119 // (At construction time, the spin button widths cannot be determined correctly,
00120 //  so we need to wait until now to definitively rearrange the widget.)
00121 void SpinBox2::showEvent(QShowEvent*)
00122 {
00123         arrange();
00124 }
00125 
00126 QSize SpinBox2::sizeHint() const
00127 {
00128         getMetrics();
00129         QSize size = spinbox->sizeHint();
00130         size.setWidth(size.width() - xSpinbox + wUpdown2 + wGap);
00131         return size;
00132 }
00133 
00134 QSize SpinBox2::minimumSizeHint() const
00135 {
00136         getMetrics();
00137         QSize size = spinbox->minimumSizeHint();
00138         size.setWidth(size.width() - xSpinbox + wUpdown2 + wGap);
00139         return size;
00140 }
00141 
00142 void SpinBox2::arrange()
00143 {
00144         getMetrics();
00145         updown2Frame->setGeometry(QStyle::visualRect(QRect(0, 0, wUpdown2, height()), this));
00146         updown2->setGeometry(-xUpdown2, 0, updown2->width(), height());
00147         spinboxFrame->setGeometry(QStyle::visualRect(QRect(wUpdown2 + wGap, 0, width() - wUpdown2 - wGap, height()), this));
00148         spinbox->setGeometry(-xSpinbox, 0, spinboxFrame->width() + xSpinbox, height());
00149 }
00150 
00151 void SpinBox2::getMetrics() const
00152 {
00153         QRect rect = updown2->style().querySubControlMetrics(QStyle::CC_SpinWidget, updown2, QStyle::SC_SpinWidgetButtonField);
00154         xUpdown2 = rect.left();
00155         wUpdown2 = updown2->width() - xUpdown2;
00156         xSpinbox = spinbox->style().querySubControlMetrics(QStyle::CC_SpinWidget, spinbox, QStyle::SC_SpinWidgetEditField).left();
00157         wGap = 0;
00158 
00159         // Make style-specific adjustments for a better appearance
00160         if (style().isA("QMotifPlusStyle"))
00161         {
00162                 xSpinbox = 0;      // show the edit control left border
00163                 wGap = 2;          // leave a space to the right of the left-hand pair of spin buttons
00164         }
00165 }
00166 
00167 
00168 void SpinBox2::SB2_SpinBox::valueChange()
00169 {
00170         bool focus = !spinBox2->selectOnStep && hasFocus();
00171         if (focus)
00172                 clearFocus();     // prevent selection of the spin box text
00173         QSpinBox::valueChange();
00174         if (focus)
00175                 setFocus();
00176 }
00177 
00178 void SB2_SpinWidget::valueChange()
00179 {
00180         bool focus = hasFocus();
00181         if (focus)
00182                 clearFocus();     // prevent selection of the invisible spin box text
00183         QSpinBox::valueChange();
00184         if (focus)
00185                 setFocus();
00186 }
00187 
00188 #endif // QT_VERSION >= 300
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:27 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001