22#include "kexidbautofield.h"
27#include <tqmetaobject.h>
28#include <tqapplication.h>
33#include "kexidbcheckbox.h"
34#include "kexidbimagebox.h"
35#include "kexidblabel.h"
36#include "kexidblineedit.h"
37#include "kexidbtextedit.h"
38#include "kexidbcombobox.h"
39#include "kexipushbutton.h"
40#include "kexidbform.h"
42#include <kexidb/queryschema.h>
43#include <formeditor/utils.h>
44#include <kexiutils/utils.h>
46#define KexiDBAutoField_SPACING 10
49class KexiDBAutoField::Private
56 WidgetType widgetType;
58 WidgetType widgetType_property;
59 LabelPosition lblPosition;
64 TQString fieldCaptionInternal;
68 bool focusPolicyChanged : 1;
74KexiDBAutoField::KexiDBAutoField(
const TQString &text, WidgetType type, LabelPosition pos,
75 TQWidget *parent,
const char *name,
bool designMode)
76 : TQWidget(parent, name)
81 d->designMode = designMode;
82 init(text, type, pos);
85KexiDBAutoField::KexiDBAutoField(TQWidget *parent,
const char *name,
bool designMode, LabelPosition pos)
86 : TQWidget(parent, name)
91 d->designMode = designMode;
92 init(TQString(), Auto, pos);
95KexiDBAutoField::~KexiDBAutoField()
97 setUpdatesEnabled(
false);
99 m_subwidget->setUpdatesEnabled(
false);
104KexiDBAutoField::init(
const TQString &text, WidgetType type, LabelPosition pos)
109 d->label =
new TQLabel(text,
this);
110 d->label->installEventFilter(
this );
113 d->autoCaption =
true;
114 d->focusPolicyChanged =
false;
115 d->widgetType = Auto;
116 d->widgetType_property = (type==Auto ? Text : type);
119 d->baseColor = palette().active().base();
120 d->textColor = palette().active().text();
124KexiDBAutoField::setWidgetType(WidgetType type)
126 const bool differ = (type != d->widgetType_property);
127 d->widgetType_property = type;
133 d->widgetType = Auto;
136 d->widgetType = d->widgetType_property;
145 delete (TQWidget *)m_subwidget;
148 TQWidget *newSubwidget;
149 switch( d->widgetType ) {
156 newSubwidget =
new KexiDBLineEdit(
this, TQCString(
"KexiDBAutoField_KexiDBLineEdit:")+name() );
159 newSubwidget =
new KexiDBTextEdit(
this, TQCString(
"KexiDBAutoField_KexiDBTextEdit:")+name() );
165 newSubwidget =
new KexiDBImageBox(d->designMode,
this, TQCString(
"KexiDBAutoField_KexiDBImageBox:")+name());
168 newSubwidget =
new KexiDBComboBox(
this, TQCString(
"KexiDBAutoField_KexiDBComboBox:")+name(), d->designMode);
179 newSubwidget->setName( TQCString(
"KexiDBAutoField_") + newSubwidget->className() );
181 dynamic_cast<KexiFormDataItemInterface*
>(newSubwidget)
183 dynamic_cast<KexiFormDataItemInterface*
>(newSubwidget)
185 newSubwidget->setProperty(
"dataSource",
dataSource());
186 KFormDesigner::DesignTimeDynamicChildWidgetHandler::childWidgetAdded(
this);
187 newSubwidget->show();
188 d->label->setBuddy(newSubwidget);
189 if (d->focusPolicyChanged) {
190 newSubwidget->setFocusPolicy(focusPolicy());
193 TQWidget::setFocusPolicy(newSubwidget->focusPolicy());
195 setFocusProxy(newSubwidget);
197 newSubwidget->setPalette( tqApp->palette() );
210 TQPalette p( m_subwidget->palette() );
211 p.setColor( TQPalette::Active, TQColorGroup::Base, d->baseColor );
212 if(d->widgetType == Boolean)
213 p.setColor( TQPalette::Active, TQColorGroup::Foreground, d->textColor );
215 p.setColor( TQPalette::Active, TQColorGroup::Text, d->textColor );
216 m_subwidget->setPalette(p);
224 d->lblPosition = position;
226 TQBoxLayout *lyr = d->layout;
234 if (position==Top || position==Left) {
235 int align = d->label->alignment();
236 if(position == Top) {
237 d->layout = (TQBoxLayout*)
new TQVBoxLayout(
this);
238 align |= AlignVertical_Mask;
239 align ^= AlignVertical_Mask;
243 d->layout = (TQBoxLayout*)
new TQHBoxLayout(
this);
244 align |= AlignVertical_Mask;
245 align ^= AlignVertical_Mask;
246 align |= AlignVCenter;
248 d->label->setAlignment(align);
249 if(d->widgetType == Boolean
257 d->layout->addWidget(d->label, 0, position == Top ?
int(TQt::AlignLeft) : 0);
258 if(position == Left && d->widgetType != Boolean)
259 d->layout->addSpacing(KexiDBAutoField_SPACING);
260 d->layout->addWidget(m_subwidget, 1);
262 if (subwidgetInterface) {
263 if (subwidgetInterface->appendStretchRequired(
this))
264 d->layout->addStretch(0);
265 if (subwidgetInterface->subwidgetStretchRequired(
this)) {
266 TQSizePolicy sizePolicy( m_subwidget->sizePolicy() );
267 if(position == Left) {
268 sizePolicy.setHorData( TQSizePolicy::Minimum );
269 d->label->setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Preferred);
272 sizePolicy.setVerData( TQSizePolicy::Minimum );
273 d->label->setSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Fixed);
275 m_subwidget->setSizePolicy(sizePolicy);
282 d->layout = (TQBoxLayout*)
new TQHBoxLayout(
this);
284 d->layout->addWidget(m_subwidget);
287 resize(size()+TQSize(1,0));
288 resize(size()-TQSize(1,0));
289 if (
dynamic_cast<KexiDBAutoField*
>((TQWidget*)m_subwidget)) {
291 dynamic_cast<KexiDBAutoField*
>((TQWidget*)m_subwidget)->setLabelPosition(position);
301 d->widgetType = Auto;
303 setFocusPolicy(TQWidget::NoFocus);
305 m_subwidget->setFocusPolicy(TQWidget::NoFocus);
307 d->label->setText( text );
313 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
323 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
331 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
333 iface->
setValue(m_origValue, add, removeOld);
340 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
342 return iface->
value();
349 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
358 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
367 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
376 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
377 kexipluginsdbg << m_origValue << endl;
387 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
392KexiDBAutoField::WidgetType KexiDBAutoField::widgetType()
const
394 return d->widgetType_property;
397KexiDBAutoField::LabelPosition KexiDBAutoField::labelPosition()
const
399 return d->lblPosition;
402TQString KexiDBAutoField::caption()
const
407bool KexiDBAutoField::hasAutoCaption()
const
409 return d->autoCaption;
412TQWidget* KexiDBAutoField::editor()
const
417TQLabel* KexiDBAutoField::label()
const
422int KexiDBAutoField::fieldTypeInternal()
const
424 return d->fieldTypeInternal;
427TQString KexiDBAutoField::fieldCaptionInternal()
const
429 return d->fieldCaptionInternal;
435 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
444 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
453 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
471 fieldType = d->fieldTypeInternal;
473 const WidgetType newWidgetType = KexiDBAutoField::widgetTypeForFieldType( fieldType );
475 if(d->widgetType != newWidgetType) {
476 d->widgetType = newWidgetType;
485 d->fieldCaptionInternal = text;
487 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
488 if((!iface || !iface->
columnInfo()) && d->autoCaption) {
504 if(d->widgetType_property == Auto) {
505 WidgetType newWidgetType = Auto;
515 newWidgetType = KexiDBAutoField::widgetTypeForFieldType( fieldType );
517 if(d->widgetType != newWidgetType || newWidgetType==Auto) {
518 d->widgetType = newWidgetType;
525 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
531KexiDBAutoField::WidgetType
544 case KexiDB::Field::Date:
546 case KexiDB::Field::DateTime:
548 case KexiDB::Field::Time:
553 return MultiLineText;
570 bool unbound =
false;
571 if (d->autoCaption && (d->widgetType==Auto || dataSource().isEmpty())) {
573 realText = TQString::fromLatin1(name())+
" "+i18n(
"Unbound Auto Field",
"(unbound)");
575 realText = TQString();
583 if (!text.isEmpty()) {
584 realText = text[0].upper() + text.mid(1);
585 if (d->widgetType!=Boolean) {
597 d->label->setAlignment( TQt::AlignCenter | TQt::WordBreak );
599 d->label->setAlignment( TQt::AlignCenter );
601 if(d->widgetType == Boolean) {
602 static_cast<TQCheckBox*
>((TQWidget*)m_subwidget)->setText(realText);
606 d->label->setText(realText);
620KexiDBAutoField::setCaption(
const TQString &caption)
622 d->caption = caption;
623 if(!d->autoCaption && !caption.isEmpty())
628KexiDBAutoField::setAutoCaption(
bool autoCaption)
630 d->autoCaption = autoCaption;
645KexiDBAutoField::setDataSource(
const TQString &ds ) {
653KexiDBAutoField::sizeHint()
const
655 if (d->lblPosition == NoLabel)
656 return m_subwidget ? m_subwidget->sizeHint() : TQWidget::sizeHint();
660 s1 = m_subwidget->sizeHint();
661 TQSize s2(d->label->sizeHint());
662 if (d->lblPosition == Top)
663 return TQSize(TQMAX(s1.width(), s2.width()), s1.height()+KexiDBAutoField_SPACING+s2.height());
666 return TQSize(s1.width()+KexiDBAutoField_SPACING+s2.width(), TQMAX(s1.height(), s2.height()));
670KexiDBAutoField::setFocusPolicy( TQWidget::FocusPolicy policy )
672 d->focusPolicyChanged =
true;
673 TQWidget::setFocusPolicy(policy);
674 d->label->setFocusPolicy(policy);
676 m_subwidget->setFocusPolicy(policy);
680KexiDBAutoField::updateInformationAboutUnboundField()
683 || (!d->autoCaption && d->caption.isEmpty()) )
685 d->label->setText( TQString::fromLatin1(name())+
" "+i18n(
"Unbound Auto Field",
" (unbound)") );
709KexiDBAutoField::paletteChange(
const TQPalette& oldPal )
712 d->label->setPalette( palette() );
715void KexiDBAutoField::unsetPalette()
717 TQWidget::unsetPalette();
730 d->textColor = color;
741 d->baseColor = color;
747 if(d->widgetType == Boolean)
750 return d->label->paletteForegroundColor();
755 if(d->widgetType == Boolean)
758 d->label->setPaletteForegroundColor(color);
759 TQWidget::setPaletteForegroundColor(color);
765 if(d->widgetType == Boolean)
768 return d->label->paletteBackgroundColor();
773 if(d->widgetType == Boolean)
776 d->label->setPaletteBackgroundColor(color);
777 TQWidget::setPaletteBackgroundColor(color);
790 return TQWidget::property(name);
798 return TQWidget::setProperty(name,
value);
801bool KexiDBAutoField::eventFilter( TQObject *o, TQEvent *e )
803 if (o==d->label && d->label->buddy() && e->type()==TQEvent::MouseButtonRelease) {
805 d->label->buddy()->setFocus();
807 return TQWidget::eventFilter(o, e);
813 if (
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget))
814 dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget)->setDisplayDefaultValue(m_subwidget, displayDefaultValue);
819 KexiDataItemInterface *iface =
dynamic_cast<KexiDataItemInterface*
>((TQWidget*)m_subwidget);
826 KexiDataItemInterface *iface =
dynamic_cast<KexiDataItemInterface*
>((TQWidget*)m_subwidget);
833 KexiDataItemInterface *iface =
dynamic_cast<KexiDataItemInterface*
>((TQWidget*)m_subwidget);
840 KexiFormDataItemInterface *iface =
dynamic_cast<KexiFormDataItemInterface*
>((TQWidget*)m_subwidget);
841 if (iface && iface->keyPressed(ke))
846#include "kexidbautofield.moc"
virtual void setForegroundLabelColor(const TQColor &color)
Sets label's foreground color.
virtual void clear()
clears item's data, so the data will contain NULL data
virtual void moveCursorToEnd()
Implemented for KexiDataItemInterface.
virtual TQVariant value()
virtual void setLabelPosition(LabelPosition position)
virtual void installListener(KexiDataItemChangesListener *listener)
Reimpelmented to also install listenter for internal editor.
const TQColor & paletteForegroundColor() const
Reimplemented to return internal editor's color.
void setFieldCaptionInternal(const TQString &text)
const TQColor & foregroundLabelColor() const
virtual void setColumnInfo(KexiDB::QueryColumnInfo *cinfo)
virtual void setReadOnly(bool readOnly)
virtual bool cursorAtStart()
void changeText(const TQString &text, bool beautify=true)
const TQColor & backgroundLabelColor() const
void setFieldTypeInternal(int kexiDBFieldType)
virtual void setInvalidState(const TQString &text)
virtual void setDisplayDefaultValue(TQWidget *widget, bool displayDefaultValue)
const TQColor & paletteBackgroundColor() const
Reimplemented to return internal editor's color.
virtual bool valueChanged()
virtual bool keyPressed(TQKeyEvent *ke)
virtual void moveCursorToStart()
Implemented for KexiDataItemInterface.
virtual void setPaletteBackgroundColor(const TQColor &color)
Reimplemented to set internal editor's color.
virtual bool setProperty(const char *name, const TQVariant &value)
Reimplemented to accept subproperties.
void setPaletteForegroundColor(const TQColor &color)
Reimplemented to set internal editor's color.
virtual bool valueIsEmpty()
virtual TQVariant property(const char *name) const
Reimplemented to accept subproperties.
virtual bool isReadOnly() const
virtual bool valueIsNull()
virtual void createEditor()
void copyPropertiesToEditor()
internal editor can be created too late, so certain properties should be copied
virtual void selectAll()
Implemented for KexiDataItemInterface.
virtual void setValueInternal(const TQVariant &add, bool removeOld)
virtual bool valueIsValid()
virtual bool cursorAtEnd()
void setColumnInfoInternal(KexiDB::QueryColumnInfo *cinfo, KexiDB::QueryColumnInfo *visibleColumnInfo)
Used by KexiDBAutoField::setColumnInfo() and KexiDBComboBox::setColumnInfo().
virtual void setBackgroundLabelColor(const TQColor &color)
Sets label's background color.
Combo box widget for Kexi forms.
A data-aware, editable image box.
Line edit widget for Kexi forms.
Multiline edit widget for Kexi forms.
Helper class that assigns additional information for the column in a query.
TQString captionOrAliasOrName() const
An helper class used to react on KexiDataItemInterface objects' changes.
virtual bool cursorAtEnd()=0
void setParentDataItemInterface(KexiDataItemInterface *parentDataItemInterface)
virtual bool cursorAtStart()=0
virtual bool isReadOnly() const
virtual void moveCursorToStart()
virtual TQVariant value()=0
virtual bool valueIsNull()=0
virtual bool valueChanged()
virtual bool valueIsEmpty()=0
void setValue(const TQVariant &value, const TQVariant &add=TQVariant(), bool removeOld=false, const TQVariant *visibleValue=0)
virtual bool valueIsValid()
virtual KexiDB::Field * field() const =0
virtual void installListener(KexiDataItemChangesListener *listener)
Sets listener. No need to reimplement this.
virtual void clear()=0
clears item's data, so the data will contain NULL data
virtual void moveCursorToEnd()