00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kexidblineedit.h"
00022 #include "kexidbautofield.h"
00023
00024 #include <kdebug.h>
00025 #include <knumvalidator.h>
00026 #include <kdatetbl.h>
00027
00028 #include <qpopupmenu.h>
00029 #include <qpainter.h>
00030
00031 #include <kexiutils/utils.h>
00032 #include <kexidb/queryschema.h>
00033 #include <kexidb/fieldvalidator.h>
00034 #include <kexiutils/utils.h>
00035
00037
00038
00040 class KexiDBLineEdit_ReadOnlyValidator : public QValidator
00041 {
00042 public:
00043 KexiDBLineEdit_ReadOnlyValidator( QObject * parent )
00044 : QValidator(parent)
00045 {
00046 }
00047 ~KexiDBLineEdit_ReadOnlyValidator() {}
00048 virtual State validate( QString &, int & ) const { return Invalid; }
00049 };
00050
00051
00052
00053 KexiDBLineEdit::KexiDBLineEdit(QWidget *parent, const char *name)
00054 : KLineEdit(parent, name)
00055 , KexiDBTextWidgetInterface()
00056 , KexiFormDataItemInterface()
00057
00058
00059 , m_menuExtender(this, this)
00060 , m_internalReadOnly(false)
00061 , m_slotTextChanged_enabled(true)
00062 {
00063 #ifdef USE_KLineEdit_setReadOnly
00065 QPalette p(widget->palette());
00066 p.setColor( lighterGrayBackgroundColor(palette()) );
00067 widget->setPalette(p);
00068 #endif
00069
00070 connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
00071 }
00072
00073 KexiDBLineEdit::~KexiDBLineEdit()
00074 {
00075
00076
00077 }
00078
00079 void KexiDBLineEdit::setInvalidState( const QString& displayText )
00080 {
00081 KLineEdit::setReadOnly(true);
00083 if (focusPolicy() & TabFocus)
00084 setFocusPolicy(QWidget::ClickFocus);
00085 setText(displayText);
00086 }
00087
00088 void KexiDBLineEdit::setValueInternal(const QVariant& add, bool removeOld)
00089 {
00090 #if 0 //moved to KexiTextFormatter
00091 QVariant value;
00092 if (removeOld)
00093 value = add;
00094 else {
00095 if (add.toString().isEmpty())
00096 value = m_origValue;
00097 else
00098 value = m_origValue.toString() + add.toString();
00099 }
00100
00101 if (m_columnInfo) {
00102 const KexiDB::Field::Type t = m_columnInfo->field->type();
00103 if (t == KexiDB::Field::Boolean) {
00105 setText( value.toBool() ? "1" : "0" );
00106 return;
00107 }
00108 else if (t == KexiDB::Field::Date) {
00109 setText( dateFormatter()->dateToString( value.toString().isEmpty() ? QDate() : value.toDate() ) );
00110 setCursorPosition(0);
00111 return;
00112 }
00113 else if (t == KexiDB::Field::Time) {
00114 setText(
00115 timeFormatter()->timeToString(
00116
00117 value.toString().isEmpty() ? value.toTime() : QTime(99,0,0)
00118 )
00119 );
00120 setCursorPosition(0);
00121 return;
00122 }
00123 else if (t == KexiDB::Field::DateTime) {
00124 if (value.toString().isEmpty() ) {
00125 setText( QString::null );
00126 }
00127 else {
00128 setText(
00129 dateFormatter()->dateToString( value.toDateTime().date() ) + " " +
00130 timeFormatter()->timeToString( value.toDateTime().time() )
00131 );
00132 }
00133 setCursorPosition(0);
00134 return;
00135 }
00136 }
00137 #endif
00138 m_slotTextChanged_enabled = false;
00139 setText( m_textFormatter.valueToText(removeOld ? QVariant() : m_origValue, add.toString()) );
00140
00141 setCursorPosition(0);
00142 m_slotTextChanged_enabled = true;
00143 }
00144
00145 QVariant KexiDBLineEdit::value()
00146 {
00147 return m_textFormatter.textToValue( text() );
00148 #if 0 // moved to KexiTextFormatter
00149 if (! m_columnInfo)
00150 return QVariant();
00151 const KexiDB::Field::Type t = m_columnInfo->field->type();
00152 switch (t) {
00153 case KexiDB::Field::Text:
00154 case KexiDB::Field::LongText:
00155 return text();
00156 case KexiDB::Field::Byte:
00157 case KexiDB::Field::ShortInteger:
00158 return text().toShort();
00160 case KexiDB::Field::Integer:
00161 return text().toInt();
00162 case KexiDB::Field::BigInteger:
00163 return text().toLongLong();
00164 case KexiDB::Field::Boolean:
00166 return text() == "1" ? QVariant(true,1) : QVariant(false,0);
00167 case KexiDB::Field::Date:
00168 return dateFormatter()->stringToVariant( text() );
00169 case KexiDB::Field::Time:
00170 return timeFormatter()->stringToVariant( text() );
00171 case KexiDB::Field::DateTime:
00172 return stringToDateTime(*dateFormatter(), *timeFormatter(), text());
00173 case KexiDB::Field::Float:
00174 return text().toFloat();
00175 case KexiDB::Field::Double:
00176 return text().toDouble();
00177 default:
00178 return QVariant();
00179 }
00181 return text();
00182 #endif
00183 }
00184
00185 void KexiDBLineEdit::slotTextChanged(const QString&)
00186 {
00187 if (!m_slotTextChanged_enabled)
00188 return;
00189 signalValueChanged();
00190 }
00191
00192 bool KexiDBLineEdit::valueIsNull()
00193 {
00194 return valueIsEmpty();
00195 }
00196
00197 bool KexiDBLineEdit::valueIsEmpty()
00198 {
00199 return m_textFormatter.valueIsEmpty( text() );
00200 #if 0 // moved to KexiTextFormatter
00201 if (text().isEmpty())
00202 return true;
00203
00204 if (m_columnInfo) {
00205 const KexiDB::Field::Type t = m_columnInfo->field->type();
00206 if (t == KexiDB::Field::Date || )
00207 return dateFormatter()->isEmpty( text() );
00208 else if (t == KexiDB::Field::Time)
00209 return timeFormatter()->isEmpty( text() );
00210 else if (t == KexiDB::Field::Time)
00211 return dateTimeIsEmpty( *dateFormatter(), *timeFormatter(), text() );
00212 }
00213
00215 return text().isEmpty();
00216 #endif
00217 }
00218
00219 bool KexiDBLineEdit::valueIsValid()
00220 {
00221 return m_textFormatter.valueIsValid( text() );
00222 #if 0 // moved to KexiTextFormatter
00223 if (!m_columnInfo)
00224 return true;
00226 if (valueIsEmpty())
00227 return true;
00228
00229 const KexiDB::Field::Type t = m_columnInfo->field->type();
00230 if (t == KexiDB::Field::Date)
00231 return dateFormatter()->stringToVariant( text() ).isValid();
00232 else if (t == KexiDB::Field::Time)
00233 return timeFormatter()->stringToVariant( text() ).isValid();
00234 else if (t == KexiDB::Field::DateTime)
00235 return dateTimeIsValid( *dateFormatter(), *timeFormatter(), text() );
00236
00238 return true;
00239 #endif
00240 }
00241
00242 bool KexiDBLineEdit::isReadOnly() const
00243 {
00244 return m_internalReadOnly;
00245 }
00246
00247 void KexiDBLineEdit::setReadOnly( bool readOnly )
00248 {
00249 #ifdef USE_KLineEdit_setReadOnly
00251 return KLineEdit::setReadOnly( readOnly );
00252 #else
00253 m_internalReadOnly = readOnly;
00254 if (m_internalReadOnly) {
00255 m_readWriteValidator = validator();
00256 if (!m_readOnlyValidator)
00257 m_readOnlyValidator = new KexiDBLineEdit_ReadOnlyValidator(this);
00258 setValidator( m_readOnlyValidator );
00259 }
00260 else {
00261
00262 setValidator( m_readWriteValidator );
00263 }
00264 m_menuExtender.updatePopupMenuActions();
00265 #endif
00266 }
00267
00268 QPopupMenu * KexiDBLineEdit::createPopupMenu()
00269 {
00270 QPopupMenu *contextMenu = KLineEdit::createPopupMenu();
00271 m_menuExtender.createTitle(contextMenu);
00272 return contextMenu;
00273 }
00274
00275
00276 QWidget* KexiDBLineEdit::widget()
00277 {
00278 return this;
00279 }
00280
00281 bool KexiDBLineEdit::cursorAtStart()
00282 {
00283 return cursorPosition()==0;
00284 }
00285
00286 bool KexiDBLineEdit::cursorAtEnd()
00287 {
00288 return cursorPosition()==(int)text().length();
00289 }
00290
00291 void KexiDBLineEdit::clear()
00292 {
00293 if (!m_internalReadOnly)
00294 KLineEdit::clear();
00295 }
00296
00297
00298 void KexiDBLineEdit::setColumnInfo(KexiDB::QueryColumnInfo* cinfo)
00299 {
00300 KexiFormDataItemInterface::setColumnInfo(cinfo);
00301 m_textFormatter.setField( cinfo ? cinfo->field : 0 );
00302
00303 if (!cinfo)
00304 return;
00305
00307 setValidator( new KexiDB::FieldValidator(*cinfo->field, this) );
00308
00309 #if 0 // moved to KexiTextFormatter
00310 if (t==KexiDB::Field::Date) {
00312 setInputMask( dateFormatter()->inputMask() );
00313 }
00314 else if (t==KexiDB::Field::Time) {
00316
00317 setInputMask( timeFormatter()->inputMask() );
00318 }
00319 else if (t==KexiDB::Field::DateTime) {
00320 setInputMask(
00321 dateTimeInputMask( *dateFormatter(), *timeFormatter() ) );
00322 }
00323 #endif
00324 const QString inputMask( m_textFormatter.inputMask() );
00325 if (!inputMask.isEmpty())
00326 setInputMask( inputMask );
00327
00328 KexiDBTextWidgetInterface::setColumnInfo(cinfo, this);
00329 }
00330
00331
00332
00333
00334
00335
00336
00337 void KexiDBLineEdit::paintEvent ( QPaintEvent *pe )
00338 {
00339 KLineEdit::paintEvent( pe );
00340 QPainter p(this);
00341 KexiDBTextWidgetInterface::paint( this, &p, text().isEmpty(), alignment(), hasFocus() );
00342 }
00343
00344 bool KexiDBLineEdit::event( QEvent * e )
00345 {
00346 const bool ret = KLineEdit::event( e );
00347 KexiDBTextWidgetInterface::event(e, this, text().isEmpty());
00348 if (e->type()==QEvent::FocusOut) {
00349 QFocusEvent *fe = static_cast<QFocusEvent *>(e);
00350
00351 if (fe->reason()==QFocusEvent::Tab || fe->reason()==QFocusEvent::Backtab) {
00352
00354 setCursorPosition(0);
00355 }
00356 }
00357 return ret;
00358 }
00359
00360 bool KexiDBLineEdit::appendStretchRequired(KexiDBAutoField* autoField) const
00361 {
00362 return KexiDBAutoField::Top == autoField->labelPosition();
00363 }
00364
00365 void KexiDBLineEdit::handleAction(const QString& actionName)
00366 {
00367 if (actionName=="edit_copy") {
00368 copy();
00369 }
00370 else if (actionName=="edit_paste") {
00371 paste();
00372 }
00373 else if (actionName=="edit_cut") {
00374 cut();
00375 }
00377 }
00378
00379 void KexiDBLineEdit::setDisplayDefaultValue(QWidget *widget, bool displayDefaultValue)
00380 {
00381 KexiFormDataItemInterface::setDisplayDefaultValue(widget, displayDefaultValue);
00382
00383 KexiDisplayUtils::DisplayParameters * const params
00384 = displayDefaultValue ? m_displayParametersForDefaultValue : m_displayParametersForEnteredValue;
00385 setFont(params->font);
00386 QPalette pal(palette());
00387 pal.setColor(QPalette::Active, QColorGroup::Text, params->textColor);
00388 setPalette(pal);
00389 }
00390
00391 void KexiDBLineEdit::undo()
00392 {
00393 cancelEditor();
00394 }
00395
00396 void KexiDBLineEdit::moveCursorToEnd()
00397 {
00398 KLineEdit::end(false);
00399 }
00400
00401 void KexiDBLineEdit::moveCursorToStart()
00402 {
00403 KLineEdit::home(false);
00404 }
00405
00406 void KexiDBLineEdit::selectAll()
00407 {
00408 KLineEdit::selectAll();
00409 }
00410
00411 bool KexiDBLineEdit::keyPressed(QKeyEvent *ke)
00412 {
00413 Q_UNUSED(ke);
00414 return false;
00415 }
00416
00417 #include "kexidblineedit.moc"