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  , m_slotTextChanged_enabled(true)
00035 {
00036     connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
00037 }
00038 
00039 KexiDBLineEdit::~KexiDBLineEdit()
00040 {
00041 //  delete m_autonumberDisplayParameters;
00042 }
00043 
00044 void KexiDBLineEdit::setInvalidState( const QString& displayText )
00045 {
00046     KLineEdit::setReadOnly(true);
00048     if (focusPolicy() & TabFocus)
00049         setFocusPolicy(QWidget::ClickFocus);
00050     setText(displayText);
00051 }
00052 
00053 void KexiDBLineEdit::setValueInternal(const QVariant& add, bool removeOld)
00054 {
00055     if (m_columnInfo && m_columnInfo->field->type()==KexiDB::Field::Boolean) {
00057         setText( add.toBool() ? "1" : "0" );
00058     }
00059     else {
00060         if (removeOld)
00061             setText( add.toString() );
00062         else
00063             setText( m_origValue.toString() + add.toString() );
00064     }
00065 
00066 
00067 
00068     if (removeOld && m_columnInfo) {
00069         const KexiDB::Field::Type t = m_columnInfo->field->type();
00070         if (t == KexiDB::Field::Boolean) {
00072             setText( add.toBool() ? "1" : "0" );
00073             return;
00074         }
00075     }
00076     
00077     m_slotTextChanged_enabled = false;
00078 
00079     if (removeOld)
00080         setText( add.toString() );
00081     else
00082         setText( m_origValue.toString() + add.toString() );
00083     setCursorPosition(0); //ok?
00084 
00085     m_slotTextChanged_enabled = true;
00086 }
00087 
00088 QVariant KexiDBLineEdit::value()
00089 {
00090     const KexiDB::Field::Type t = m_columnInfo->field->type();
00091     switch (t) {
00092     case KexiDB::Field::Text:
00093     case KexiDB::Field::LongText:
00094         return text();
00095     case KexiDB::Field::Byte:
00096     case KexiDB::Field::ShortInteger:
00097         return text().toShort();
00099     case KexiDB::Field::Integer:
00100         return text().toInt();
00101     case KexiDB::Field::BigInteger:
00102         return text().toLongLong();
00103     case KexiDB::Field::Boolean:
00105         return text() == "1" ? QVariant(true,1) : QVariant(false,0);
00106 /*  case KexiDB::Field::Date:
00107         return dateFormatter()->stringToVariant( text() );
00108     case KexiDB::Field::Time:
00109         return timeFormatter()->stringToVariant( text() );
00110     case KexiDB::Field::DateTime:
00111         return stringToDateTime(*dateFormatter(), *timeFormatter(), text());*/
00112     case KexiDB::Field::Float:
00113         return text().toFloat();
00114     case KexiDB::Field::Double:
00115         return text().toDouble();
00116     }
00118 
00119     return text();
00120 }
00121 
00122 void KexiDBLineEdit::slotTextChanged(const QString&)
00123 {
00124     if (!m_slotTextChanged_enabled)
00125         return;
00126     signalValueChanged();
00127 }
00128 
00129 bool KexiDBLineEdit::valueIsNull()
00130 {
00131     return valueIsEmpty(); //ok??? text().isNull();
00132 }
00133 
00134 bool KexiDBLineEdit::valueIsEmpty()
00135 {
00136     if (text().isEmpty())
00137         return true;
00138 
00139     return text().isEmpty();
00140 }
00141 
00142 bool KexiDBLineEdit::isReadOnly() const
00143 {
00144     return KLineEdit::isReadOnly();
00145 }
00146 
00147 QWidget* KexiDBLineEdit::widget()
00148 {
00149     return this;
00150 }
00151 
00152 bool KexiDBLineEdit::cursorAtStart()
00153 {
00154     return cursorPosition()==0;
00155 }
00156 
00157 bool KexiDBLineEdit::cursorAtEnd()
00158 {
00159     return cursorPosition()==(int)text().length();
00160 }
00161 
00162 void KexiDBLineEdit::clear()
00163 {
00164     KLineEdit::clear();
00165 }
00166 
00167 void KexiDBLineEdit::setColumnInfo(KexiDB::QueryColumnInfo* cinfo)
00168 {
00169     KexiFormDataItemInterface::setColumnInfo(cinfo);
00170     if (!cinfo)
00171         return;
00175     const KexiDB::Field::Type t = cinfo->field->type();
00176     if (cinfo->field->isIntegerType()) {
00177         QValidator *validator = 0;
00178         const bool u = cinfo->field->isUnsigned();
00179         int bottom, top;
00180         if (t==KexiDB::Field::Byte) {
00181             bottom = u ? 0 : -0x80;
00182             top = u ? 0xff : 0x7f;
00183         }
00184         else if (t==KexiDB::Field::ShortInteger) {
00185             bottom = u ? 0 : -0x8000;
00186             top = u ? 0xffff : 0x7fff;
00187         }
00188         else if (t==KexiDB::Field::Integer) {
00189             bottom = u ? 0 : -0x7fffffff-1;
00190             top = u ? 0xffffffff : 0x7fffffff;
00191         }
00192         else if (t==KexiDB::Field::BigInteger) {
00196             validator = new KIntValidator(this);
00197         }
00198 
00199         if (!validator)
00200             validator = new KIntValidator(bottom, top, this);
00201         setValidator( validator );
00202     }
00203     else if (cinfo->field->isFPNumericType()) {
00204         QValidator *validator;
00205         if (t==KexiDB::Field::Float) {
00206             if (cinfo->field->isUnsigned()) //ok?
00207                 validator = new KDoubleValidator(0, 3.4e+38, cinfo->field->scale(), this);
00208             else
00209                 validator = new KDoubleValidator(this);
00210         }
00211         else {//double
00212             if (cinfo->field->isUnsigned()) //ok?
00213                 validator = new KDoubleValidator(0, 1.7e+308, cinfo->field->scale(), this);
00214             else
00215                 validator = new KDoubleValidator(this);
00216         }
00217         setValidator( validator );
00218     }
00219     else if (t==KexiDB::Field::Date) {
00221         QValidator *validator = new KDateValidator(this);
00222         setValidator( validator );
00223     }
00224     else if (t==KexiDB::Field::Time) {
00226         setInputMask("00:00:00");
00227     }
00228     else if (t==KexiDB::Field::Boolean) {
00230         QValidator *validator = new KIntValidator(0, 1, this);
00231         setValidator( validator );
00232     }
00233 
00234     KexiDBTextWidgetInterface::setColumnInfo(cinfo, this);
00235 }
00236 
00237 void KexiDBLineEdit::paintEvent ( QPaintEvent *pe )
00238 {
00239     KLineEdit::paintEvent( pe );
00240     KexiDBTextWidgetInterface::paintEvent( this, text().isEmpty(), alignment(), hasFocus() );
00241 }
00242 
00243 bool KexiDBLineEdit::event( QEvent * e )
00244 {
00245     const bool ret = KLineEdit::event( e );
00246     KexiDBTextWidgetInterface::event(e, this, text().isEmpty());
00247     return ret;
00248 }
00249 
00250 #include "kexidblineedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys