krita

kis_int_spinbox.cc

00001 /*
00002  *  Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org>
00003  *  Copyright (c) 2006 Casper Boemann <cbr@boemann.dk>
00004  *
00005  *  Requires the Qt widget libraries, available at no cost at
00006  *  http://www.troll.no/
00007  *
00008  *  This library is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Library General Public
00010  *  License as published by the Free Software Foundation; either
00011  *  version 2 of the License, or (at your option) any later version.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Library General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Library General Public License
00019  *  along with this library; see the file COPYING.LIB.  If not, write to
00020  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021  *  Boston, MA 02110-1301, USA.
00022  */
00023 
00024 #ifdef HAVE_LIMITS_H
00025 #include <limits.h>
00026 #endif
00027 #include <assert.h>
00028 #include <math.h>
00029 #include <algorithm>
00030 
00031 #include <qtimer.h>
00032 #include <qapplication.h>
00033 #include <qsize.h>
00034 #include <qslider.h>
00035 #include <qstyle.h>
00036 #include <qlabel.h>
00037 #include <qpopupmenu.h>
00038 #include <qlineedit.h>
00039 #include <qlayout.h>
00040 #include <qvalidator.h>
00041 
00042 #include <knuminput.h>
00043 #include <kglobal.h>
00044 #include <klocale.h>
00045 #include <kdebug.h>
00046 #include <karrowbutton.h>
00047 
00048 #include "kdialog.h"
00049 #include "knumvalidator.h"
00050 #include "kis_int_spinbox.h"
00051 
00052 class KisIntSpinbox::KisIntSpinboxPrivate {
00053 public:
00054 
00055     KIntSpinBox * m_numinput;
00056     KisPopupSlider *m_slider;
00057     KArrowButton *m_arrow;
00058     int m_prevValue;
00059     QValidator *m_validator;
00060     QTimer m_timer;
00061 };
00062 
00063 
00064 KisIntSpinbox::KisIntSpinbox(QWidget *parent, const char *name)
00065     : QWidget(parent, name)
00066 {
00067     init(0);
00068 }
00069 
00070 KisIntSpinbox::KisIntSpinbox(const QString & /*label*/, int val, QWidget *parent, const char *name)
00071     : QWidget(parent, name)
00072 {
00073     init(val);
00074 }
00075 
00076 void KisIntSpinbox::init(int val)
00077 {
00078     d = new KisIntSpinboxPrivate( );
00079     QBoxLayout * l = new QHBoxLayout( this );
00080 
00081     l->insertStretch(0, 1);
00082     d->m_numinput = new KIntSpinBox(0, 100, 1, val, 10, this, "KisIntSpinbox::KIntSpinBox");
00083 
00084     d->m_numinput->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
00085     d->m_numinput->setSuffix("%");
00086     l->addWidget( d->m_numinput );
00087 
00088     d->m_slider = new KisPopupSlider(0, 100, 10, val, QSlider::Horizontal, this);
00089     d->m_slider->setFrameStyle(QFrame::Panel|QFrame::Raised);
00090 
00091     d->m_arrow = new KArrowButton(this, Qt::DownArrow);
00092     d->m_arrow->setPopup(d->m_slider);
00093     d->m_arrow->setMaximumHeight( fontMetrics().height() + 4);
00094     d->m_arrow->setEnabled(true); // Why is the arrow still gray?
00095 
00096     l->addWidget( d->m_arrow );
00097 
00098     d->m_prevValue = val;
00099     setValue(val);
00100     setFocusProxy(d->m_numinput);
00101     layout();
00102 
00103     connect(d->m_numinput, SIGNAL(valueChanged(int)), SLOT(spinboxValueChanged(int)));
00104     connect(d->m_slider, SIGNAL(valueChanged(int)), SLOT(sliderValueChanged(int)));
00105     connect(d->m_slider, SIGNAL(aboutToShow()), SLOT(slotAboutToShow()));
00106     connect(d->m_slider, SIGNAL(aboutToHide()), SLOT(slotAboutToHide()));
00107 
00108     connect(&(d->m_timer), SIGNAL(timeout()), this, SLOT(slotTimeout()));
00109 }
00110 
00111 void KisIntSpinbox::spinboxValueChanged(int val)
00112 {
00113     setValue(val);
00114     d->m_timer.start(300, true);
00115 
00116 }
00117 
00118 void KisIntSpinbox::sliderValueChanged(int val)
00119 {
00120     setValue(val);
00121     emit valueChanged(val);
00122     emit valueChanged(val, true);
00123 }
00124 
00125 void KisIntSpinbox::setRange(int lower, int upper, int /*step*/)
00126 {
00127     upper = kMax(upper, lower);
00128     lower = kMin(upper, lower);
00129     d->m_slider->setRange(lower, upper);
00130 
00131     layout();
00132 }
00133 
00134 void KisIntSpinbox::setMinValue(int min)
00135 {
00136     setRange(min, maxValue(), d->m_slider->lineStep());
00137 }
00138 
00139 int KisIntSpinbox::minValue() const
00140 {
00141     return d->m_slider->minValue();
00142 }
00143 
00144 void KisIntSpinbox::setMaxValue(int max)
00145 {
00146     setRange(minValue(), max, d->m_slider->lineStep());
00147 }
00148 
00149 int KisIntSpinbox::maxValue() const
00150 {
00151     return d->m_slider->maxValue();
00152 }
00153 
00154 KisIntSpinbox::~KisIntSpinbox()
00155 {
00156     delete d;
00157 }
00158 
00159 void KisIntSpinbox::setValue(int val)
00160 {
00161    d->m_slider->blockSignals(true);
00162    d->m_slider->setValue(val);
00163    d->m_slider->blockSignals(false);
00164 
00165    d->m_numinput->blockSignals(true);
00166    d->m_numinput->setValue(val);
00167    d->m_numinput->blockSignals(false);
00168 }
00169 
00170 int  KisIntSpinbox::value() const
00171 {
00172     return d->m_numinput->value(); // From the numinput: that one isn't in steps of ten
00173 }
00174 
00175 void KisIntSpinbox::setLabel(const QString & /*label*/)
00176 {
00177 }
00178 
00179 void KisIntSpinbox::slotAboutToShow()
00180 {
00181     d->m_prevValue = value();
00182 }
00183 
00184 void KisIntSpinbox::slotAboutToHide()
00185 {
00186     if( d->m_prevValue != value() )
00187     {
00188         emit finishedChanging( d->m_prevValue, value() );
00189         d->m_prevValue = value();
00190     }
00191 }
00192 
00193 void KisIntSpinbox::slotTimeout()
00194 {
00195     emit valueChanged(value());
00196     emit valueChanged(value(), true);
00197 }
00198 #include "kis_int_spinbox.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys