kexi

kexidbautofield.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2005 Christian Nitschkowski <segfault_ii@web.de>
00004    Copyright (C) 2005-2007 Jaroslaw Staniek <js@iidea.pl>
00005 
00006    This program is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this program; see the file COPYING.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kexidbautofield.h"
00023 
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 #include <qpainter.h>
00027 #include <qmetaobject.h>
00028 #include <qapplication.h>
00029 
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032 
00033 #include "kexidbcheckbox.h"
00034 #include "kexidbimagebox.h"
00035 #include "kexidblabel.h"
00036 #include "kexidblineedit.h"
00037 #include "kexidbtextedit.h"
00038 #include "kexidbcombobox.h"
00039 #include "kexipushbutton.h"
00040 #include "kexidbform.h"
00041 
00042 #include <kexidb/queryschema.h>
00043 #include <formeditor/utils.h>
00044 #include <kexiutils/utils.h>
00045 
00046 #define KexiDBAutoField_SPACING 10 //10 pixel for spacing between a label and an editor widget
00047 
00049 class KexiDBAutoField::Private
00050 {
00051     public:
00052         Private()
00053         {
00054         }
00055 
00056         WidgetType widgetType; 
00057 
00058         WidgetType  widgetType_property; 
00059         LabelPosition  lblPosition;
00060         QBoxLayout  *layout;
00061         QLabel  *label;
00062         QString  caption;
00063         KexiDB::Field::Type fieldTypeInternal;
00064         QString fieldCaptionInternal;
00065         QColor baseColor; 
00066         QColor textColor; 
00067         bool autoCaption : 1;
00068         bool focusPolicyChanged : 1;
00069         bool designMode : 1;
00070 };
00071 
00072 //-------------------------------------
00073 
00074 KexiDBAutoField::KexiDBAutoField(const QString &text, WidgetType type, LabelPosition pos, 
00075     QWidget *parent, const char *name, bool designMode)
00076  : QWidget(parent, name)
00077  , KexiFormDataItemInterface()
00078  , KFormDesigner::DesignTimeDynamicChildWidgetHandler()
00079  , d( new Private() )
00080 {
00081     d->designMode = designMode;
00082     init(text, type, pos);
00083 }
00084 
00085 KexiDBAutoField::KexiDBAutoField(QWidget *parent, const char *name, bool designMode, LabelPosition pos)
00086  : QWidget(parent, name)
00087  , KexiFormDataItemInterface()
00088  , KFormDesigner::DesignTimeDynamicChildWidgetHandler()
00089  , d( new Private() )
00090 {
00091     d->designMode = designMode;
00092     init(QString::null/*i18n("Auto Field")*/, Auto, pos);
00093 }
00094 
00095 KexiDBAutoField::~KexiDBAutoField()
00096 {
00097     setUpdatesEnabled(false);
00098     if (m_subwidget)
00099         m_subwidget->setUpdatesEnabled(false);
00100     delete d;
00101 }
00102 
00103 void
00104 KexiDBAutoField::init(const QString &text, WidgetType type, LabelPosition pos)
00105 {
00106     d->fieldTypeInternal = KexiDB::Field::InvalidType;
00107     d->layout = 0;
00108     m_subwidget = 0;
00109     d->label = new QLabel(text, this);
00110     d->label->installEventFilter( this );
00111     //QFontMetrics fm( font() );
00112     //d->label->setFixedWidth( fm.width("This is a test string length") );
00113     d->autoCaption = true;
00114     d->focusPolicyChanged = false;
00115     d->widgetType = Auto;
00116     d->widgetType_property = (type==Auto ? Text : type); //to force "differ" to be true in setWidgetType()
00117     setLabelPosition(pos);
00118     setWidgetType(type);
00119     d->baseColor = palette().active().base();
00120     d->textColor = palette().active().text();
00121 }
00122 
00123 void
00124 KexiDBAutoField::setWidgetType(WidgetType type)
00125 {
00126     const bool differ = (type != d->widgetType_property);
00127     d->widgetType_property = type;
00128     if(differ) {
00129         if(type == Auto) {// try to guess type from data source type
00130             if (visibleColumnInfo())
00131                 d->widgetType = KexiDBAutoField::widgetTypeForFieldType(visibleColumnInfo()->field->type());
00132             else
00133                 d->widgetType = Auto;
00134         }
00135         else
00136             d->widgetType = d->widgetType_property;
00137         createEditor();
00138     }
00139 }
00140 
00141 void
00142 KexiDBAutoField::createEditor()
00143 {
00144     if(m_subwidget) {
00145         delete (QWidget *)m_subwidget;
00146     }
00147 
00148     QWidget *newSubwidget;
00149     switch( d->widgetType ) {
00150         case Text:
00151         case Double: 
00152         case Integer: 
00153         case Date:
00154         case Time:
00155         case DateTime:
00156             newSubwidget = new KexiDBLineEdit( this, QCString("KexiDBAutoField_KexiDBLineEdit:")+name() );
00157             break;
00158         case MultiLineText:
00159             newSubwidget = new KexiDBTextEdit( this, QCString("KexiDBAutoField_KexiDBTextEdit:")+name() );
00160             break;
00161         case Boolean:
00162             newSubwidget = new KexiDBCheckBox(dataSource(), this, QCString("KexiDBAutoField_KexiDBCheckBox:")+name());
00163             break;
00164         case Image:
00165             newSubwidget = new KexiDBImageBox(d->designMode, this, QCString("KexiDBAutoField_KexiDBImageBox:")+name());
00166             break;
00167         case ComboBox:
00168             newSubwidget = new KexiDBComboBox(this, QCString("KexiDBAutoField_KexiDBComboBox:")+name(), d->designMode);
00169             break;
00170         default:
00171             newSubwidget = 0;
00172             changeText(d->caption);
00173             //d->label->setText( d->dataSource.isEmpty() ? "<datasource>" : d->dataSource );
00174             break;
00175     }
00176 
00177     setSubwidget( newSubwidget ); //this will also allow to declare subproperties, see KFormDesigner::WidgetWithSubpropertiesInterface
00178     if(newSubwidget) {
00179         newSubwidget->setName( QCString("KexiDBAutoField_") + newSubwidget->className() );
00180         dynamic_cast<KexiDataItemInterface*>(newSubwidget)->setParentDataItemInterface(this);
00181         dynamic_cast<KexiFormDataItemInterface*>(newSubwidget)
00182             ->setColumnInfo(columnInfo()); //needed at least by KexiDBImageBox
00183         dynamic_cast<KexiFormDataItemInterface*>(newSubwidget)
00184             ->setVisibleColumnInfo(visibleColumnInfo()); //needed at least by KexiDBComboBox
00185         newSubwidget->setProperty("dataSource", dataSource()); //needed at least by KexiDBImageBox
00186         KFormDesigner::DesignTimeDynamicChildWidgetHandler::childWidgetAdded(this);
00187         newSubwidget->show();
00188         d->label->setBuddy(newSubwidget);
00189         if (d->focusPolicyChanged) {//if focusPolicy is changed at top level, editor inherits it
00190             newSubwidget->setFocusPolicy(focusPolicy());
00191         }
00192         else {//if focusPolicy is not changed at top level, inherit it from editor
00193             QWidget::setFocusPolicy(newSubwidget->focusPolicy());
00194         }
00195         setFocusProxy(newSubwidget); //ok?
00196         if (parentWidget())
00197             newSubwidget->setPalette( qApp->palette() );
00198         copyPropertiesToEditor();
00199 //      KFormDesigner::installRecursiveEventFilter(newSubwidget, this);
00200     }
00201 
00202     setLabelPosition(labelPosition());
00203 }
00204 
00205 void KexiDBAutoField::copyPropertiesToEditor()
00206 {
00207     if (m_subwidget) {
00208 //      kdDebug() << "KexiDBAutoField::copyPropertiesToEditor(): base col: " <<  d->baseColor.name() << 
00209 //          "; text col: " << d->textColor.name() << endl;
00210         QPalette p( m_subwidget->palette() );
00211         p.setColor( QPalette::Active, QColorGroup::Base, d->baseColor );
00212         if(d->widgetType == Boolean)
00213             p.setColor( QPalette::Active, QColorGroup::Foreground, d->textColor );
00214         else
00215             p.setColor( QPalette::Active, QColorGroup::Text, d->textColor );
00216         m_subwidget->setPalette(p);
00217         //m_subwidget->setPaletteBackgroundColor( d->baseColor );
00218     }
00219 }
00220 
00221 void
00222 KexiDBAutoField::setLabelPosition(LabelPosition position)
00223 {
00224     d->lblPosition = position;
00225     if(d->layout) {
00226         QBoxLayout *lyr = d->layout;
00227         d->layout = 0;
00228         delete lyr;
00229     }
00230 
00231     if(m_subwidget)
00232         m_subwidget->show();
00234     if (position==Top || position==Left) {
00235         int align = d->label->alignment();
00236         if(position == Top) {
00237             d->layout = (QBoxLayout*) new QVBoxLayout(this);
00238             align |= AlignVertical_Mask;
00239             align ^= AlignVertical_Mask;
00240             align |= AlignTop;
00241         }
00242         else {
00243             d->layout = (QBoxLayout*) new QHBoxLayout(this);
00244             align |= AlignVertical_Mask;
00245             align ^= AlignVertical_Mask;
00246             align |= AlignVCenter;
00247         }
00248         d->label->setAlignment(align);
00249         if(d->widgetType == Boolean 
00250             || (d->widgetType == Auto && fieldTypeInternal() == KexiDB::Field::InvalidType && !d->designMode))
00251         {
00252             d->label->hide();
00253         }
00254         else {
00255             d->label->show();
00256         }
00257         d->layout->addWidget(d->label, 0, position == Top ? int(Qt::AlignLeft) : 0);
00258         if(position == Left && d->widgetType != Boolean)
00259             d->layout->addSpacing(KexiDBAutoField_SPACING);
00260         d->layout->addWidget(m_subwidget, 1);
00261         KexiSubwidgetInterface *subwidgetInterface = dynamic_cast<KexiSubwidgetInterface*>((QWidget*)m_subwidget);
00262         if (subwidgetInterface) {
00263             if (subwidgetInterface->appendStretchRequired(this))
00264                 d->layout->addStretch(0);
00265             if (subwidgetInterface->subwidgetStretchRequired(this)) {
00266                 QSizePolicy sizePolicy( m_subwidget->sizePolicy() );
00267                 if(position == Left) {
00268                     sizePolicy.setHorData( QSizePolicy::Minimum );
00269                     d->label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
00270                 }
00271                 else {
00272                     sizePolicy.setVerData( QSizePolicy::Minimum );
00273                     d->label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
00274                 }
00275                 m_subwidget->setSizePolicy(sizePolicy);
00276             }
00277         }
00278 //      if(m_subwidget)
00279     //      m_subwidget->setSizePolicy(...);
00280     }
00281     else {
00282         d->layout = (QBoxLayout*) new QHBoxLayout(this);
00283         d->label->hide();
00284         d->layout->addWidget(m_subwidget);
00285     }
00286     //a hack to force layout to be refreshed (any better idea for this?)
00287     resize(size()+QSize(1,0));
00288     resize(size()-QSize(1,0));
00289     if (dynamic_cast<KexiDBAutoField*>((QWidget*)m_subwidget)) {
00290         //needed for KexiDBComboBox
00291         dynamic_cast<KexiDBAutoField*>((QWidget*)m_subwidget)->setLabelPosition(position);
00292     }
00293 }
00294 
00295 void
00296 KexiDBAutoField::setInvalidState( const QString &text )
00297 {
00298     // Widget with an invalid dataSource is just a QLabel
00299     if (d->designMode)
00300         return;
00301     d->widgetType = Auto;
00302     createEditor();
00303     setFocusPolicy(QWidget::NoFocus);
00304     if (m_subwidget)
00305         m_subwidget->setFocusPolicy(QWidget::NoFocus);
00307     d->label->setText( text );
00308 }
00309 
00310 bool
00311 KexiDBAutoField::isReadOnly() const
00312 {
00313     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00314     if(iface)
00315         return iface->isReadOnly();
00316     else
00317         return false;
00318 }
00319 
00320 void
00321 KexiDBAutoField::setReadOnly( bool readOnly )
00322 {
00323     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00324     if(iface)
00325         iface->setReadOnly(readOnly);
00326 }
00327 
00328 void
00329 KexiDBAutoField::setValueInternal(const QVariant& add, bool removeOld)
00330 {
00331     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00332     if(iface)
00333         iface->setValue(m_origValue, add, removeOld);
00334 //      iface->setValueInternal(add, removeOld);
00335 }
00336 
00337 QVariant
00338 KexiDBAutoField::value()
00339 {
00340     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00341     if(iface)
00342         return iface->value();
00343     return QVariant();
00344 }
00345 
00346 bool
00347 KexiDBAutoField::valueIsNull()
00348 {
00349     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00350     if(iface)
00351         return iface->valueIsNull();
00352     return true;
00353 }
00354 
00355 bool
00356 KexiDBAutoField::valueIsEmpty()
00357 {
00358     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00359     if(iface)
00360         return iface->valueIsEmpty();
00361     return true;
00362 }
00363 
00364 bool
00365 KexiDBAutoField::valueIsValid()
00366 {
00367     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00368     if(iface)
00369         return iface->valueIsValid();
00370     return true;
00371 }
00372 
00373 bool
00374 KexiDBAutoField::valueChanged()
00375 {
00376     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00377     kexipluginsdbg << m_origValue  << endl;
00378     if(iface)
00379         return iface->valueChanged();
00380     return false;
00381 }
00382 
00383 void
00384 KexiDBAutoField::installListener(KexiDataItemChangesListener* listener)
00385 {
00386     KexiFormDataItemInterface::installListener(listener);
00387     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00388     if(iface)
00389         iface->installListener(listener);
00390 }
00391 
00392 KexiDBAutoField::WidgetType KexiDBAutoField::widgetType() const
00393 {
00394     return d->widgetType_property;
00395 }
00396 
00397 KexiDBAutoField::LabelPosition KexiDBAutoField::labelPosition() const
00398 {
00399     return d->lblPosition;
00400 }
00401 
00402 QString KexiDBAutoField::caption() const
00403 {
00404     return d->caption;
00405 }
00406 
00407 bool KexiDBAutoField::hasAutoCaption() const
00408 {
00409     return d->autoCaption;
00410 }
00411 
00412 QWidget* KexiDBAutoField::editor() const
00413 {
00414     return m_subwidget;
00415 }
00416 
00417 QLabel* KexiDBAutoField::label() const
00418 {
00419     return d->label;
00420 }
00421 
00422 int KexiDBAutoField::fieldTypeInternal() const
00423 {
00424     return d->fieldTypeInternal;
00425 }
00426 
00427 QString KexiDBAutoField::fieldCaptionInternal() const
00428 {
00429     return d->fieldCaptionInternal;
00430 }
00431 
00432 bool
00433 KexiDBAutoField::cursorAtStart()
00434 {
00435     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00436     if(iface)
00437         return iface->cursorAtStart();
00438     return false;
00439 }
00440 
00441 bool
00442 KexiDBAutoField::cursorAtEnd()
00443 {
00444     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00445     if(iface)
00446         return iface->cursorAtEnd();
00447     return false;
00448 }
00449 
00450 void
00451 KexiDBAutoField::clear()
00452 {
00453     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00454     if(iface)
00455         iface->clear();
00456 }
00457 
00458 void
00459 KexiDBAutoField::setFieldTypeInternal(int kexiDBFieldType)
00460 {
00461     d->fieldTypeInternal = (KexiDB::Field::Type)kexiDBFieldType;
00462     KexiDB::Field::Type fieldType;
00463     //find real fied type to use
00464     if (d->fieldTypeInternal==KexiDB::Field::InvalidType) {
00465         if (visibleColumnInfo())
00466             fieldType = KexiDB::Field::Text;
00467         else
00468             fieldType = KexiDB::Field::InvalidType;
00469     }
00470     else
00471         fieldType = d->fieldTypeInternal;
00472 
00473     const WidgetType newWidgetType = KexiDBAutoField::widgetTypeForFieldType( fieldType );
00474 
00475     if(d->widgetType != newWidgetType) {
00476         d->widgetType = newWidgetType;
00477         createEditor();
00478     }
00479     setFieldCaptionInternal(d->fieldCaptionInternal);
00480 }
00481 
00482 void
00483 KexiDBAutoField::setFieldCaptionInternal(const QString& text)
00484 {
00485     d->fieldCaptionInternal = text;
00486     //change text only if autocaption is set and no columnInfo is available
00487     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00488     if((!iface || !iface->columnInfo()) && d->autoCaption) {
00489         changeText(d->fieldCaptionInternal);
00490     }
00491 }
00492 
00493 void
00494 KexiDBAutoField::setColumnInfo(KexiDB::QueryColumnInfo* cinfo)
00495 {
00496     KexiFormDataItemInterface::setColumnInfo(cinfo);
00497     setColumnInfoInternal(cinfo, cinfo);
00498 }
00499 
00500 void
00501 KexiDBAutoField::setColumnInfoInternal(KexiDB::QueryColumnInfo* cinfo, KexiDB::QueryColumnInfo* visibleColumnInfo)
00502 {
00503     // change widget type depending on field type
00504     if(d->widgetType_property == Auto) {
00505         WidgetType newWidgetType = Auto;
00506         KexiDB::Field::Type fieldType;
00507         if (cinfo)
00508             fieldType = visibleColumnInfo->field->type();
00509         else if (dataSource().isEmpty())
00510             fieldType = KexiDB::Field::InvalidType;
00511         else
00512             fieldType = KexiDB::Field::Text;
00513 
00514         if (fieldType != KexiDB::Field::InvalidType) {
00515             newWidgetType = KexiDBAutoField::widgetTypeForFieldType( fieldType );
00516         }
00517         if(d->widgetType != newWidgetType || newWidgetType==Auto) {
00518             d->widgetType = newWidgetType;
00519             createEditor();
00520         }
00521     }
00522     // update label's text
00523     changeText((cinfo && d->autoCaption) ? cinfo->captionOrAliasOrName() : d->caption);
00524 
00525     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00526     if(iface)
00527         iface->setColumnInfo(visibleColumnInfo);
00528 }
00529 
00530 //static
00531 KexiDBAutoField::WidgetType
00532 KexiDBAutoField::widgetTypeForFieldType(KexiDB::Field::Type type)
00533 {
00534     switch(type) {
00535         case KexiDB::Field::Integer:
00536         case KexiDB::Field::ShortInteger:
00537         case KexiDB::Field::BigInteger:
00538             return Integer;
00539         case  KexiDB::Field::Boolean:
00540             return Boolean;
00541         case KexiDB::Field::Float:
00542         case KexiDB::Field::Double:
00543             return Double;
00544         case KexiDB::Field::Date:
00545             return Date;
00546         case KexiDB::Field::DateTime:
00547             return DateTime;
00548         case KexiDB::Field::Time:
00549             return Time;
00550         case KexiDB::Field::Text:
00551             return Text;
00552         case KexiDB::Field::LongText:
00553             return MultiLineText;
00554         case KexiDB::Field::Enum:
00555             return ComboBox;
00556         case KexiDB::Field::InvalidType:
00557             return Auto;
00558         case KexiDB::Field::BLOB:
00559             return Image;
00560         default:
00561             break;
00562     }
00563     return Text;
00564 }
00565 
00566 void
00567 KexiDBAutoField::changeText(const QString &text, bool beautify)
00568 {
00569     QString realText;
00570     bool unbound = false;
00571     if (d->autoCaption && (d->widgetType==Auto || dataSource().isEmpty())) {
00572         if (d->designMode)
00573             realText = QString::fromLatin1(name())+" "+i18n("Unbound Auto Field", "(unbound)");
00574         else
00575             realText = QString::null;
00576         unbound = true;
00577     }
00578     else {
00579         if (beautify) {
00583             if (!text.isEmpty()) {
00584                 realText = text[0].upper() + text.mid(1);
00585                 if (d->widgetType!=Boolean) {
00588                     realText += ": ";
00589                 }
00590             }
00591         }
00592         else
00593             realText = text;
00594     }
00595 
00596     if (unbound)
00597         d->label->setAlignment( Qt::AlignCenter | Qt::WordBreak );
00598     else
00599         d->label->setAlignment( Qt::AlignCenter );
00600 //  QWidget* widgetToAlterForegroundColor;
00601     if(d->widgetType == Boolean) {
00602         static_cast<QCheckBox*>((QWidget*)m_subwidget)->setText(realText);
00603 //      widgetToAlterForegroundColor = m_subwidget;
00604     }
00605     else {
00606         d->label->setText(realText);
00607 //      widgetToAlterForegroundColor = d->label;
00608     }
00609 /*
00610     if (unbound)
00611         widgetToAlterForegroundColor->setPaletteForegroundColor( 
00612             KexiUtils::blendedColors(
00613                 widgetToAlterForegroundColor->paletteForegroundColor(), 
00614                 widgetToAlterForegroundColor->paletteBackgroundColor(), 2, 1));
00615     else
00616         widgetToAlterForegroundColor->setPaletteForegroundColor( paletteForegroundColor() );*/
00617 }
00618 
00619 void
00620 KexiDBAutoField::setCaption(const QString &caption)
00621 {
00622     d->caption = caption;
00623     if(!d->autoCaption && !caption.isEmpty())
00624         changeText(d->caption);
00625 }
00626 
00627 void
00628 KexiDBAutoField::setAutoCaption(bool autoCaption)
00629 {
00630     d->autoCaption = autoCaption;
00631     if(d->autoCaption) {
00632         //d->caption = QString::null;
00633         if(columnInfo()) {
00634             changeText(columnInfo()->captionOrAliasOrName());
00635         }
00636         else {
00637             changeText(d->fieldCaptionInternal);
00638         }
00639     }
00640     else
00641         changeText(d->caption);
00642 }
00643 
00644 void
00645 KexiDBAutoField::setDataSource( const QString &ds ) {
00646     KexiFormDataItemInterface::setDataSource(ds);
00647     if (ds.isEmpty()) {
00648         setColumnInfo(0);
00649     }
00650 }
00651 
00652 QSize
00653 KexiDBAutoField::sizeHint() const
00654 {
00655     if (d->lblPosition == NoLabel)
00656         return m_subwidget ? m_subwidget->sizeHint() : QWidget::sizeHint();
00657 
00658     QSize s1(0,0);
00659     if (m_subwidget)
00660         s1 = m_subwidget->sizeHint();
00661     QSize s2(d->label->sizeHint());
00662     if (d->lblPosition == Top)
00663         return QSize(QMAX(s1.width(), s2.width()), s1.height()+KexiDBAutoField_SPACING+s2.height());
00664 
00665     //left
00666     return QSize(s1.width()+KexiDBAutoField_SPACING+s2.width(), QMAX(s1.height(), s2.height()));
00667 }
00668 
00669 void
00670 KexiDBAutoField::setFocusPolicy( FocusPolicy policy )
00671 {
00672     d->focusPolicyChanged = true;
00673     QWidget::setFocusPolicy(policy);
00674     d->label->setFocusPolicy(policy);
00675     if (m_subwidget)
00676         m_subwidget->setFocusPolicy(policy);
00677 }
00678 
00679 void
00680 KexiDBAutoField::updateInformationAboutUnboundField()
00681 {
00682     if ( (d->autoCaption && (dataSource().isEmpty() || dataSourceMimeType().isEmpty()))
00683         || (!d->autoCaption && d->caption.isEmpty()) )
00684     {
00685         d->label->setText( QString::fromLatin1(name())+" "+i18n("Unbound Auto Field", " (unbound)") );
00686     }
00687 //  else
00688 //      d->label->setText( QString::fromLatin1(name())+" "+i18n(" (unbound)") );
00689 }
00690 
00691 /*void
00692 KexiDBAutoField::paintEvent( QPaintEvent* pe )
00693 {
00694     QWidget::paintEvent( pe );
00695 
00696     if (   (d->autoCaption && (dataSource().isEmpty() || dataSourceMimeType().isEmpty()))
00697         || (!d->autoCaption && d->caption.isEmpty()) )
00698     {
00699         QPainter p(this);
00700         p.setPen( d->label->paletteForegroundColor() );
00701         p.setClipRect(pe->rect());
00702         p.setFont(d->label->font());
00703         p.drawText(rect(), Qt::AlignLeft | Qt::WordBreak, 
00704             QString::fromLatin1(name())+" "+i18n(" (unbound)"));
00705     }
00706 }*/
00707 
00708 void
00709 KexiDBAutoField::paletteChange( const QPalette& oldPal )
00710 {
00711     Q_UNUSED(oldPal);
00712     d->label->setPalette( palette() );
00713 }
00714 
00715 void KexiDBAutoField::unsetPalette()
00716 {
00717     QWidget::unsetPalette();
00718 
00719 }
00720 
00721 // ===== methods below are just proxies for the internal editor or label =====
00722 
00723 const QColor & KexiDBAutoField::paletteForegroundColor() const
00724 {
00725     return d->textColor;
00726 }
00727 
00728 void KexiDBAutoField::setPaletteForegroundColor( const QColor & color )
00729 {
00730     d->textColor = color;
00731     copyPropertiesToEditor();
00732 }
00733 
00734 const QColor & KexiDBAutoField::paletteBackgroundColor() const
00735 {
00736     return d->baseColor;
00737 }
00738 
00739 void KexiDBAutoField::setPaletteBackgroundColor( const QColor & color )
00740 {
00741     d->baseColor = color;
00742     copyPropertiesToEditor();
00743 }
00744 
00745 const QColor & KexiDBAutoField::foregroundLabelColor() const
00746 {
00747     if(d->widgetType == Boolean)
00748         return paletteForegroundColor();
00749 
00750     return d->label->paletteForegroundColor();
00751 }
00752 
00753 void KexiDBAutoField::setForegroundLabelColor( const QColor & color )
00754 {
00755     if(d->widgetType == Boolean)
00756         setPaletteForegroundColor(color);
00757     else {
00758         d->label->setPaletteForegroundColor(color);
00759         QWidget::setPaletteForegroundColor(color);
00760     }
00761 }
00762 
00763 const QColor & KexiDBAutoField::backgroundLabelColor() const
00764 {
00765     if(d->widgetType == Boolean)
00766         return paletteBackgroundColor();
00767 
00768     return d->label->paletteBackgroundColor();
00769 }
00770 
00771 void KexiDBAutoField::setBackgroundLabelColor( const QColor & color )
00772 {
00773     if(d->widgetType == Boolean)
00774         setPaletteBackgroundColor(color);
00775     else {
00776         d->label->setPaletteBackgroundColor(color);
00777         QWidget::setPaletteBackgroundColor(color);
00778     }
00779 
00780 //  if (m_subwidget)
00781 //      m_subwidget->setPalette( qApp->palette() );
00782 }
00783 
00784 QVariant KexiDBAutoField::property( const char * name ) const
00785 {
00786     bool ok;
00787     QVariant val = KFormDesigner::WidgetWithSubpropertiesInterface::subproperty(name, ok);
00788     if (ok)
00789         return val;
00790     return QWidget::property(name);
00791 }
00792 
00793 bool KexiDBAutoField::setProperty( const char * name, const QVariant & value )
00794 {
00795     bool ok = KFormDesigner::WidgetWithSubpropertiesInterface::setSubproperty(name, value);
00796     if (ok)
00797         return true;
00798     return QWidget::setProperty(name, value);
00799 }
00800 
00801 bool KexiDBAutoField::eventFilter( QObject *o, QEvent *e )
00802 {
00803     if (o==d->label && d->label->buddy() && e->type()==QEvent::MouseButtonRelease) {
00804         //focus label's buddy when user clicked the label
00805         d->label->buddy()->setFocus();
00806     }
00807     return QWidget::eventFilter(o, e);
00808 }
00809 
00810 void KexiDBAutoField::setDisplayDefaultValue(QWidget* widget, bool displayDefaultValue)
00811 {
00812     KexiFormDataItemInterface::setDisplayDefaultValue(widget, displayDefaultValue);
00813     if (dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget))
00814         dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget)->setDisplayDefaultValue(m_subwidget, displayDefaultValue);
00815 }
00816 
00817 void KexiDBAutoField::moveCursorToEnd()
00818 {
00819     KexiDataItemInterface *iface = dynamic_cast<KexiDataItemInterface*>((QWidget*)m_subwidget);
00820     if (iface)
00821         iface->moveCursorToEnd();
00822 }
00823 
00824 void KexiDBAutoField::moveCursorToStart()
00825 {
00826     KexiDataItemInterface *iface = dynamic_cast<KexiDataItemInterface*>((QWidget*)m_subwidget);
00827     if (iface)
00828         iface->moveCursorToStart();
00829 }
00830 
00831 void KexiDBAutoField::selectAll()
00832 {
00833     KexiDataItemInterface *iface = dynamic_cast<KexiDataItemInterface*>((QWidget*)m_subwidget);
00834     if (iface)
00835         iface->selectAll();
00836 }
00837 
00838 bool KexiDBAutoField::keyPressed(QKeyEvent *ke)
00839 {
00840     KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>((QWidget*)m_subwidget);
00841     if (iface && iface->keyPressed(ke))
00842         return true;
00843     return false;
00844 }
00845 
00846 #include "kexidbautofield.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys