82 changes: 67 additions & 15 deletions src/gui/qgsattributeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <qgsmaplayerregistry.h>
#include <qgslogger.h>
#include <qgsexpression.h>
#include <qgsfilterlineedit.h>

#include <QScrollArea>
#include <QPushButton>
Expand Down Expand Up @@ -87,8 +88,12 @@ void QgsAttributeEditor::selectDate()
dlg->setWindowTitle( tr( "Select a date" ) );
QVBoxLayout *vl = new QVBoxLayout( dlg );

const QgsFieldValidator *v = dynamic_cast<const QgsFieldValidator *>( le->validator() );
QString dateFormat = v ? v->dateFormat() : "yyyy-MM-dd";

QCalendarWidget *cw = new QCalendarWidget( dlg );
cw->setSelectedDate( QDate::fromString( le->text(), Qt::ISODate ) );
QString prevValue = le->text();
cw->setSelectedDate( QDate::fromString( prevValue, dateFormat ) );
vl->addWidget( cw );

QDialogButtonBox *buttonBox = new QDialogButtonBox( dlg );
Expand All @@ -101,7 +106,9 @@ void QgsAttributeEditor::selectDate()

if ( dlg->exec() == QDialog::Accepted )
{
le->setText( cw->selectedDate().toString( Qt::ISODate ) );
QString newValue = cw->selectedDate().toString( dateFormat );
le->setText( newValue );
le->setModified( newValue != prevValue );
}
}

Expand Down Expand Up @@ -483,7 +490,7 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
}
else
{
le = new QLineEdit( parent );
le = new QgsFilterLineEdit( parent );
}

if ( le )
Expand All @@ -507,7 +514,7 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
le->setReadOnly( true );
}

le->setValidator( new QgsFieldValidator( le, field ) );
le->setValidator( new QgsFieldValidator( le, field, vl->dateFormat( idx ) ) );

myWidget = le;
}
Expand All @@ -526,13 +533,13 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
if ( cb )
{
if ( cb->isEditable() )
cb->setValidator( new QgsFieldValidator( cb, field ) );
cb->setValidator( new QgsFieldValidator( cb, field, vl->dateFormat( idx ) ) );
myWidget = cb;
}

if ( myWidget )
{
if (editType == QgsVectorLayer::Immutable)
if ( editType == QgsVectorLayer::Immutable )
myWidget->setDisabled( true );

QgsStringRelay* relay = NULL;
Expand Down Expand Up @@ -611,7 +618,7 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
}
else
{
le = new QLineEdit();
le = new QgsFilterLineEdit();

pb = new QPushButton( tr( "..." ) );

Expand All @@ -625,6 +632,9 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
myWidget->setLayout( hbl );
}

if ( le )
le->setValidator( new QgsFieldValidator( le, field, vl->dateFormat( idx ) ) );

if ( pb )
{
if ( editType == QgsVectorLayer::FileName )
Expand Down Expand Up @@ -783,7 +793,7 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
QCalendarWidget *cw = qobject_cast<QCalendarWidget *>( widget );
if ( cw )
{
text = cw->selectedDate().toString( Qt::ISODate );
text = cw->selectedDate().toString( vl->dateFormat( idx ) );
}

le = widget->findChild<QLineEdit *>();
Expand All @@ -792,6 +802,11 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
if ( !cw && !gb && le )
{
text = le->text();
modified = le->isModified();
if ( text == nullValue )
{
text = QString::null;
}
}

switch ( theField.type() )
Expand Down Expand Up @@ -842,7 +857,7 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
break;
case QVariant::Date:
{
QDate myDateValue = QDate::fromString( text, Qt::ISODate );
QDate myDateValue = QDate::fromString( text, vl->dateFormat( idx ) );
if ( myDateValue.isValid() && !text.isEmpty() )
{
value = myDateValue;
Expand All @@ -856,7 +871,10 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
break;
default: //string
modified = true;
value = QVariant( text );
if ( text.isNull() )
value = QVariant();
else
value = QVariant( text );
break;
}

Expand Down Expand Up @@ -957,19 +975,25 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx,
case QgsVectorLayer::UniqueValuesEditable:
case QgsVectorLayer::Immutable:
case QgsVectorLayer::UuidGenerator:
default:
case QgsVectorLayer::TextEdit:
{
QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( editor );
QLineEdit *le = qobject_cast<QLineEdit *>( editor );
QComboBox *cb = qobject_cast<QComboBox *>( editor );
QTextEdit *te = qobject_cast<QTextEdit *>( editor );
QPlainTextEdit *pte = qobject_cast<QPlainTextEdit *>( editor );
if ( !le && ! cb && !te && !pte )
return false;

if ( fle && !( myFieldType == QVariant::Int || myFieldType == QVariant::Double || myFieldType == QVariant::LongLong || myFieldType == QVariant::Date ) )
{
fle->setNullValue( nullValue );
}

QString text;
if ( value.isNull() )
{
if ( myFieldType == QVariant::Int || myFieldType == QVariant::Double || myFieldType == QVariant::LongLong )
if ( myFieldType == QVariant::Int || myFieldType == QVariant::Double || myFieldType == QVariant::LongLong || myFieldType == QVariant::Date )
text = "";
else if ( editType == QgsVectorLayer::UuidGenerator )
text = QUuid::createUuid().toString();
Expand Down Expand Up @@ -1002,18 +1026,47 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx,
break;
}

QLineEdit* le = qobject_cast<QLineEdit*>( editor );
QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( editor );
QLineEdit *le = qobject_cast<QLineEdit*>( editor );
if ( !le )
{
le = editor->findChild<QLineEdit *>();
fle = qobject_cast<QgsFilterLineEdit *>( le );
}
if ( !le )
{
return false;
}
le->setText( value.toString() );

if ( fle && !( myFieldType == QVariant::Int || myFieldType == QVariant::Double || myFieldType == QVariant::LongLong || myFieldType == QVariant::Date ) )
{
fle->setNullValue( nullValue );
}

QString text;
if ( value.isNull() )
{
if ( myFieldType == QVariant::Int || myFieldType == QVariant::Double || myFieldType == QVariant::LongLong || myFieldType == QVariant::Date )
text = "";
else
text = nullValue;
}
else if ( editType == QgsVectorLayer::Calendar && value.canConvert( QVariant::Date ) )
{
text = value.toDate().toString( vl->dateFormat( idx ) );
}
else
{
text = value.toString();
}


le->setText( text );
}
break;

case QgsVectorLayer::Hidden:
break;
}

return true;
Expand All @@ -1030,7 +1083,6 @@ QWidget* QgsAttributeEditor::createWidgetFromDef( const QgsAttributeEditorElemen
const QgsAttributeEditorField* fieldDef = dynamic_cast<const QgsAttributeEditorField*>( widgetDef );
newWidget = createAttributeEditor( parent, 0, vl, fieldDef->idx(), attrs.value( fieldDef->idx(), QVariant() ), proxyWidgets );


if ( vl->editType( fieldDef->idx() ) != QgsVectorLayer::Immutable )
{
newWidget->setEnabled( newWidget->isEnabled() && vl->isEditable() );
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qgsfieldvalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
#include "qgslonglongvalidator.h"
#include "qgsfield.h"

QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field )
QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field, QString dateFormat )
: QValidator( parent )
, mField( field )
, mDateFormat( dateFormat )
{
switch ( mField.type() )
{
Expand Down Expand Up @@ -128,7 +129,7 @@ QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
}
else if ( mField.type() == QVariant::Date )
{
return QDate::fromString( s ).isValid() ? Acceptable : Intermediate;
return QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
}
else
{
Expand Down
5 changes: 4 additions & 1 deletion src/gui/qgsfieldvalidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ class GUI_EXPORT QgsFieldValidator : public QValidator
Q_OBJECT

public:
QgsFieldValidator( QObject *parent, const QgsField &field );
QgsFieldValidator( QObject *parent, const QgsField &field, QString dateFormat );
~QgsFieldValidator();

virtual State validate( QString &, int & ) const;
virtual void fixup( QString & ) const;

QString dateFormat() const { return mDateFormat; }

private:
// Disables copy constructing
Q_DISABLE_COPY( QgsFieldValidator )

QValidator *mValidator;
QgsField mField;
QString mNullValue;
QString mDateFormat;
};

#endif // QGSFIELDVALIDATOR_H
12 changes: 10 additions & 2 deletions src/gui/qgsfilterlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include <QToolButton>
#include <QStyle>

QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent ) : QLineEdit( parent )
QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, QString nullValue )
: QLineEdit( parent )
, mNullValue( nullValue )
{
btnClear = new QToolButton( this );
btnClear->setIcon( QgsApplication::getThemeIcon( "/mIconClear.png" ) );
Expand Down Expand Up @@ -51,7 +53,13 @@ void QgsFilterLineEdit::resizeEvent( QResizeEvent * )
( rect().bottom() + 1 - sz.height() ) / 2 );
}

void QgsFilterLineEdit::clear()
{
setText( mNullValue );
setModified( true );
}

void QgsFilterLineEdit::toggleClearButton( const QString &text )
{
btnClear->setVisible( !text.isEmpty() );
btnClear->setVisible( !isReadOnly() && text != mNullValue );
}
6 changes: 5 additions & 1 deletion src/gui/qgsfilterlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
{
Q_OBJECT
public:
QgsFilterLineEdit( QWidget* parent = 0 );
QgsFilterLineEdit( QWidget* parent = 0, QString nullValue = QString::null );

void setNullValue( QString nullValue ) { mNullValue = nullValue; }

signals:
void cleared();
Expand All @@ -38,9 +40,11 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
void resizeEvent( QResizeEvent * );

private slots:
void clear();
void toggleClearButton( const QString &text );

private:
QString mNullValue;
QToolButton *btnClear;
};

Expand Down
13 changes: 13 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
<< QgsVectorDataProvider::NativeType( tr( "Text, fixed length (char)" ), "char", QVariant::String, 1, 255 )
<< QgsVectorDataProvider::NativeType( tr( "Text, limited variable length (varchar)" ), "varchar", QVariant::String, 1, 255 )
<< QgsVectorDataProvider::NativeType( tr( "Text, unlimited length (text)" ), "text", QVariant::String )

// date type
<< QgsVectorDataProvider::NativeType( tr( "Date" ), "date", QVariant::Date )
;

QString key;
Expand Down Expand Up @@ -708,6 +711,11 @@ bool QgsPostgresProvider::loadFields()
fieldSize = -1;
}
}
else if ( fieldTypeName == "date" )
{
fieldType = QVariant::Date;
fieldSize = -1;
}
else if ( fieldTypeName == "text" ||
fieldTypeName == "bpchar" ||
fieldTypeName == "bool" ||
Expand Down Expand Up @@ -2777,6 +2785,11 @@ bool QgsPostgresProvider::convertField( QgsField &field )
fieldPrec = 0;
break;

case QVariant::Date:
fieldType = "numeric";
fieldSize = -1;
break;

case QVariant::Double:
if ( fieldSize > 18 )
{
Expand Down
28 changes: 19 additions & 9 deletions src/ui/qgsattributetypeedit.ui
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,8 @@
</layout>
</widget>
<widget class="QWidget" name="calendarPage">
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>A calendar widget to enter a date.</string>
</property>
</widget>
</item>
<item>
<layout class="QGridLayout" name="gridLayout_5">
<item row="3" column="1">
<spacer name="verticalSpacer_9">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -525,6 +518,23 @@
</property>
</spacer>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_10">
<property name="text">
<string>Date format</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label_4">
<property name="text">
<string>A calendar widget to enter a date.</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="leDateFormat"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="valueRelationPage">
Expand Down