kexi

kexidblineedit.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
00004 
00005    This program 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 program 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 program; see the file COPYING.  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 "kexidblineedit.h"
00022 
00023 #include <knumvalidator.h>
00024 #include <kdatetbl.h>
00025 #include <kexiutils/utils.h>
00026 #include <kexidb/queryschema.h>
00027 #include <kexiutils/utils.h>
00028 
00029 KexiDBLineEdit::KexiDBLineEdit(QWidget *parent, const char *name)
00030  : KLineEdit(parent, name)
00031  , KexiDBTextWidgetInterface()
00032  , KexiFormDataItemInterface()
00033 // , m_autonumberDisplayParameters(0)
00034 {
00035     connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
00036 }
00037 
00038 KexiDBLineEdit::~KexiDBLineEdit()
00039 {
00040 //  delete m_autonumberDisplayParameters;
00041 }
00042 
00043 void KexiDBLineEdit::setInvalidState( const QString& displayText )
00044 {
00045     setReadOnly(true);
00047     if (focusPolicy() & TabFocus)
00048         setFocusPolicy(QWidget::ClickFocus);
00049     setText(displayText);
00050 }
00051 
00052 void KexiDBLineEdit::setValueInternal(const QVariant& add, bool removeOld)
00053 {
00054     if (m_columnInfo && m_columnInfo->field->type()==KexiDB::Field::Boolean) {
00056         setText( add.toBool() ? "1" : "0" );
00057     }
00058     else {
00059         if (removeOld)
00060             setText( add.toString() );
00061         else
00062             setText( m_origValue.toString() + add.toString() );
00063     }
00064 }
00065 
00066 QVariant KexiDBLineEdit::value()
00067 {
00068     return text();
00069 }
00070 
00071 void KexiDBLineEdit::slotTextChanged(const QString&)
00072 {
00073     signalValueChanged();
00074 }
00075 
00076 bool KexiDBLineEdit::valueIsNull()
00077 {
00078     return text().isNull();
00079 }
00080 
00081 bool KexiDBLineEdit::valueIsEmpty()
00082 {
00083     return text().isEmpty();
00084 }
00085 
00086 bool KexiDBLineEdit::isReadOnly() const
00087 {
00088     return KLineEdit::isReadOnly();
00089 }
00090 
00091 QWidget* KexiDBLineEdit::widget()
00092 {
00093     return this;
00094 }
00095 
00096 bool KexiDBLineEdit::cursorAtStart()
00097 {
00098     return cursorPosition()==0;
00099 }
00100 
00101 bool KexiDBLineEdit::cursorAtEnd()
00102 {
00103     return cursorPosition()==(int)text().length();
00104 }
00105 
00106 void KexiDBLineEdit::clear()
00107 {
00108     setText(QString::null);
00109 }
00110 
00111 void KexiDBLineEdit::setColumnInfo(KexiDB::QueryColumnInfo* cinfo)
00112 {
00113     KexiFormDataItemInterface::setColumnInfo(cinfo);
00114     if (!cinfo)
00115         return;
00119     const KexiDB::Field::Type t = cinfo->field->type();
00120     if (cinfo->field->isIntegerType()) {
00121         QValidator *validator = 0;
00122         const bool u = cinfo->field->isUnsigned();
00123         int bottom, top;
00124         if (t==KexiDB::Field::Byte) {
00125             bottom = u ? 0 : -0x80;
00126             top = u ? 0xff : 0x7f;
00127         }
00128         else if (t==KexiDB::Field::ShortInteger) {
00129             bottom = u ? 0 : -0x8000;
00130             top = u ? 0xffff : 0x7fff;
00131         }
00132         else if (t==KexiDB::Field::Integer) {
00133             bottom = u ? 0 : -0x7fffffff-1;
00134             top = u ? 0xffffffff : 0x7fffffff;
00135         }
00136         else if (t==KexiDB::Field::BigInteger) {
00140             validator = new KIntValidator(this);
00141         }
00142 
00143         if (!validator)
00144             validator = new KIntValidator(bottom, top, this);
00145         setValidator( validator );
00146     }
00147     else if (cinfo->field->isFPNumericType()) {
00148         QValidator *validator;
00149         if (t==KexiDB::Field::Float) {
00150             if (cinfo->field->isUnsigned()) //ok?
00151                 validator = new KDoubleValidator(0, 3.4e+38, cinfo->field->scale(), this);
00152             else
00153                 validator = new KDoubleValidator(this);
00154         }
00155         else {//double
00156             if (cinfo->field->isUnsigned()) //ok?
00157                 validator = new KDoubleValidator(0, 1.7e+308, cinfo->field->scale(), this);
00158             else
00159                 validator = new KDoubleValidator(this);
00160         }
00161         setValidator( validator );
00162     }
00163     else if (t==KexiDB::Field::Date) {
00165         QValidator *validator = new KDateValidator(this);
00166         setValidator( validator );
00167     }
00168     else if (t==KexiDB::Field::Time) {
00170         setInputMask("00:00:00");
00171     }
00172     else if (t==KexiDB::Field::Boolean) {
00174         QValidator *validator = new KIntValidator(0, 1, this);
00175         setValidator( validator );
00176     }
00177 
00178     KexiDBTextWidgetInterface::setColumnInfo(cinfo, this);
00179 }
00180 
00181 void KexiDBLineEdit::paintEvent ( QPaintEvent *pe )
00182 {
00183     KLineEdit::paintEvent( pe );
00184     KexiDBTextWidgetInterface::paintEvent( this, text().isEmpty(), alignment(), hasFocus() );
00185 }
00186 
00187 bool KexiDBLineEdit::event( QEvent * e )
00188 {
00189     const bool ret = KLineEdit::event( e );
00190     KexiDBTextWidgetInterface::event(e, this, text().isEmpty());
00191     return ret;
00192 }
00193 
00194 #include "kexidblineedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys