lib

spinbox.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2004  Alexander Dymo <cloudtemple@mskat.net>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "spinbox.h"
00022 
00023 #include "property.h"
00024 
00025 #include <qlayout.h>
00026 #include <qobjectlist.h>
00027 #include <qvariant.h>
00028 #include <qpainter.h>
00029 
00030 #include <kglobal.h>
00031 #include <klocale.h>
00032 
00033 #ifdef QT_ONLY
00035 #else
00036 #include <qlineedit.h>
00037 #endif
00038 
00039 using namespace KoProperty;
00040 
00041 IntSpinBox::IntSpinBox(int lower, int upper, int step, int value, int base, IntEdit *parent, const char *name)
00042 : KIntSpinBox(lower, upper, step, value, base, parent, name)
00043 {
00044     editor()->setAlignment(Qt::AlignLeft);
00045     installEventFilter(editor());
00046     installEventFilter(this);
00047     QObjectList *spinwidgets = queryList( "QSpinWidget", 0, false, true );
00048     QSpinWidget* spin = static_cast<QSpinWidget*>(spinwidgets->first());
00049     if (spin)
00050         spin->installEventFilter(this);
00051     delete spinwidgets;
00052 }
00053 
00054 bool
00055 IntSpinBox::eventFilter(QObject *o, QEvent *e)
00056 {
00057     if(o == editor())
00058     {
00059         if(e->type() == QEvent::KeyPress)
00060         {
00061             QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00062             if((ev->key()==Key_Up || ev->key()==Key_Down) && ev->state() !=ControlButton)
00063             {
00064                 parentWidget()->eventFilter(o, e);
00065                 return true;
00066             }
00067         }
00068     }
00069     if ((o == editor() || o == this || o->parent() == this) 
00070         && e->type() == QEvent::Wheel && static_cast<IntEdit*>(parentWidget())->isReadOnly())
00071     {
00072         return true; //avoid value changes for read-only widget
00073     }
00074 
00075     return KIntSpinBox::eventFilter(o, e);
00076 }
00077 
00078 
00080 
00081 IntEdit::IntEdit(Property *property, QWidget *parent, const char *name)
00082  : Widget(property, parent, name)
00083 {
00084     QVariant minVal( property ? property->option("min") : 0 );
00085     QVariant maxVal( property ? property->option("max") : QVariant() );
00086     QVariant minValueText( property ? property->option("minValueText") : QVariant() );
00087     if (minVal.isNull())
00088         minVal = 0;
00089     if (maxVal.isNull())
00090         maxVal = INT_MAX;
00091 
00092     m_edit = new IntSpinBox(minVal.toInt(), maxVal.toInt(), 1, 0, 10, this);
00093     if (!minValueText.isNull())
00094         m_edit->setSpecialValueText(minValueText.toString());
00095     m_edit->setMinimumHeight(5);
00096     setEditor(m_edit);
00097 
00098     setLeavesTheSpaceForRevertButton(true);
00099     setFocusWidget(m_edit);
00100     connect(m_edit, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
00101 }
00102 
00103 IntEdit::~IntEdit()
00104 {}
00105 
00106 QVariant
00107 IntEdit::value() const
00108 {
00109     //return m_edit->cleanText().toInt();  adymo: why cleanText()
00110     return m_edit->value();
00111 }
00112 
00113 void
00114 IntEdit::setValue(const QVariant &value, bool emitChange)
00115 {
00116     m_edit->blockSignals(true);
00117     m_edit->setValue(value.toInt());
00118     updateSpinWidgets();
00119     m_edit->blockSignals(false);
00120     if (emitChange)
00121         emit valueChanged(this);
00122 }
00123 
00124 void
00125 IntEdit::drawViewer(QPainter *p, const QColorGroup &cg, const QRect &r, const QVariant &value)
00126 {
00127     QString valueText = value.toString();
00128     if (property() && property()->hasOptions()) {
00129         //replace min value with minValueText if defined
00130         QVariant minValue( property()->option("min") );
00131         QVariant minValueText( property()->option("minValueText") );
00132         if (!minValue.isNull() && !minValueText.isNull() && minValue.toInt() == value.toInt()) {
00133             valueText = minValueText.toString();
00134         }
00135     }
00136 
00137     Widget::drawViewer(p, cg, r, valueText);
00138 //  p->eraseRect(r);
00139 //  p->drawText(r, Qt::AlignLeft | Qt::AlignVCenter | Qt::SingleLine, valueText);
00140 }
00141 
00142 void
00143 IntEdit::slotValueChanged(int)
00144 {
00145     emit valueChanged(this);
00146 }
00147 
00148 void
00149 IntEdit::updateSpinWidgets()
00150 {
00151     QObjectList *spinwidgets = queryList( "QSpinWidget", 0, false, true );
00152     QSpinWidget* spin = static_cast<QSpinWidget*>(spinwidgets->first());
00153     if (spin) {
00154         spin->setUpEnabled(!isReadOnly());
00155         spin->setDownEnabled(!isReadOnly());
00156     }
00157     delete spinwidgets;
00158 }
00159 
00160 void
00161 IntEdit::setReadOnlyInternal(bool readOnly)
00162 {
00163     //disable editor and spin widget
00164     m_edit->editor()->setReadOnly(readOnly);
00165     updateSpinWidgets();
00166     if (readOnly)
00167         setLeavesTheSpaceForRevertButton(false);
00168 }
00169 
00172 
00173 DoubleSpinBox::DoubleSpinBox (double lower, double upper, double step, double value, int precision, DoubleEdit *parent)
00174 : KDoubleSpinBox(lower, upper, step, value, precision, parent)
00175 {
00176     editor()->setAlignment(Qt::AlignLeft);
00177     installEventFilter(editor());
00178     installEventFilter(this);
00179     QObjectList *spinwidgets = queryList( "QSpinWidget", 0, false, true );
00180     QSpinWidget* spin = static_cast<QSpinWidget*>(spinwidgets->first());
00181     if (spin)
00182         spin->installEventFilter(this);
00183     delete spinwidgets;
00184 }
00185 
00186 bool
00187 DoubleSpinBox::eventFilter(QObject *o, QEvent *e)
00188 {
00189     if(o == editor())
00190     {
00191         if(e->type() == QEvent::KeyPress)
00192         {
00193             QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00194             if((ev->key()==Key_Up || ev->key()==Key_Down) && ev->state()!=ControlButton)
00195             {
00196                 parentWidget()->eventFilter(o, e);
00197                 return true;
00198             }
00199         }
00200     }
00201     if ((o == editor() || o == this || o->parent() == this) 
00202         && e->type() == QEvent::Wheel && static_cast<IntEdit*>(parentWidget())->isReadOnly())
00203     {
00204         return true; //avoid value changes for read-only widget
00205     }
00206 
00207     return KDoubleSpinBox::eventFilter(o, e);
00208 }
00209 
00210 
00211 void DoubleSpinBox::setValue ( double value )
00212 {
00213     if (static_cast<IntEdit*>(parentWidget())->isReadOnly())
00214         return;
00215     KDoubleSpinBox::setValue(value);
00216 }
00217 
00219 
00220 DoubleEdit::DoubleEdit(Property *property, QWidget *parent, const char *name)
00221  : Widget(property, parent, name)
00222 {
00223     QVariant minVal( property ? property->option("min") : 0 );
00224     QVariant maxVal( property ? property->option("max") : QVariant() );
00225     QVariant step( property ? property->option("step") : QVariant());
00226     QVariant precision( property ? property->option("precision") : QVariant());
00227     QVariant minValueText( property ? property->option("minValueText") : QVariant() );
00228     if (minVal.isNull())
00229         minVal = 0;
00230     if (maxVal.isNull())
00231         maxVal = (double)(INT_MAX/100);
00232     if(step.isNull())
00233         step = 0.1;
00234     if(precision.isNull())
00235         precision = 2;
00236 
00237     m_edit = new DoubleSpinBox(minVal.toDouble(), maxVal.toDouble(), step.toDouble(),
00238          0, precision.toInt(), this);
00239     if (!minValueText.isNull())
00240         m_edit->setSpecialValueText(minValueText.toString());
00241     m_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00242     m_edit->setMinimumHeight(5);
00243     setEditor(m_edit);
00244 
00245     setLeavesTheSpaceForRevertButton(true);
00246     setFocusWidget(m_edit);
00247     connect(m_edit, SIGNAL(valueChanged(double)), this, SLOT(slotValueChanged(double)));
00248 }
00249 
00250 DoubleEdit::~DoubleEdit()
00251 {}
00252 
00253 QVariant
00254 DoubleEdit::value() const
00255 {
00256     //return m_edit->cleanText().toInt();  adymo: why cleanText()
00257     return m_edit->value();
00258 }
00259 
00260 void
00261 DoubleEdit::setValue(const QVariant &value, bool emitChange)
00262 {
00263     m_edit->blockSignals(true);
00264     m_edit->setValue(value.toDouble());
00265     updateSpinWidgets();
00266     m_edit->blockSignals(false);
00267     if (emitChange)
00268         emit valueChanged(this);
00269 }
00270 
00271 void
00272 DoubleEdit::drawViewer(QPainter *p, const QColorGroup &cg, const QRect &r, const QVariant &value)
00273 {
00274     QString valueText;
00275     if (property() && property()->hasOptions()) {
00276         //replace min value with minValueText if defined
00277         QVariant minValue( property()->option("min") );
00278         QVariant minValueText( property()->option("minValueText") );
00279         if (!minValue.isNull() && !minValueText.isNull() && minValue.toString().toDouble() == value.toString().toDouble()) {
00280             valueText = minValueText.toString();
00281         }
00282     }
00283     if (valueText.isEmpty())
00284         valueText = QString(value.toString()).replace('.', KGlobal::locale()->decimalSymbol());
00285 
00286     Widget::drawViewer(p, cg, r, valueText);
00287 //  p->eraseRect(r);
00288 //  p->drawText(r, Qt::AlignLeft | Qt::AlignVCenter | Qt::SingleLine, valueText);
00289 }
00290 
00291 void
00292 DoubleEdit::slotValueChanged(double)
00293 {
00294     emit valueChanged(this);
00295 }
00296 
00297 void
00298 DoubleEdit::updateSpinWidgets()
00299 {
00300     QObjectList *spinwidgets = queryList( "QSpinWidget", 0, false, true );
00301     QSpinWidget* spin = static_cast<QSpinWidget*>(spinwidgets->first());
00302     if (spin) {
00303         spin->setUpEnabled(!isReadOnly());
00304         spin->setDownEnabled(!isReadOnly());
00305     }
00306     delete spinwidgets;
00307 }
00308 
00309 void
00310 DoubleEdit::setReadOnlyInternal(bool readOnly)
00311 {
00312     //disable editor and spin widget
00313     m_edit->editor()->setReadOnly(readOnly);
00314     updateSpinWidgets();
00315     if (readOnly)
00316         setLeavesTheSpaceForRevertButton(false);
00317 }
00318 
00319 #include "spinbox.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys