Skip to content
Permalink
Browse files
Fix auto conf of QgsDateTimeEditWrapper
  • Loading branch information
Patrick Valsecchi committed Sep 7, 2016
1 parent 8d9cf9d commit 5eb70d3382c75412598e538f0f7d810210356f1e
@@ -15,6 +15,7 @@

#include "qgsdatetimeeditconfig.h"
#include "qgsdatetimeeditfactory.h"
#include "qgsvectorlayer.h"

QgsDateTimeEditConfig::QgsDateTimeEditConfig( QgsVectorLayer* vl, int fieldIdx, QWidget* parent )
: QgsEditorConfigWidget( vl, fieldIdx, parent )
@@ -119,9 +120,26 @@ QgsEditorWidgetConfig QgsDateTimeEditConfig::config()
}


QString QgsDateTimeEditConfig::defaultFormat( const QVariant::Type type )
{
switch ( type )
{
case QVariant::DateTime:
return QGSDATETIMEEDIT_DATETIMEFORMAT;
break;
case QVariant::Time:
return QGSDATETIMEEDIT_TIMEFORMAT;
break;
default:
return QGSDATETIMEEDIT_DATEFORMAT;
}
}


void QgsDateTimeEditConfig::setConfig( const QgsEditorWidgetConfig &config )
{
const QString fieldFormat = config.value( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QgsField fieldDef = layer()->fields().at( field() );
const QString fieldFormat = config.value( "field_format", defaultFormat( fieldDef.type() ) ).toString();
mFieldFormatEdit->setText( fieldFormat );

if ( fieldFormat == QGSDATETIMEEDIT_DATEFORMAT )
@@ -133,7 +151,7 @@ void QgsDateTimeEditConfig::setConfig( const QgsEditorWidgetConfig &config )
else
mFieldFormatComboBox->setCurrentIndex( 3 );

QString displayFormat = config.value( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
QString displayFormat = config.value( "display_format", defaultFormat( fieldDef.type() ) ).toString();
mDisplayFormatEdit->setText( displayFormat );
if ( displayFormat == mFieldFormatEdit->text() )
{
@@ -41,6 +41,13 @@ class GUI_EXPORT QgsDateTimeEditConfig : public QgsEditorConfigWidget, private U
public:
QgsEditorWidgetConfig config() override;
void setConfig( const QgsEditorWidgetConfig &config ) override;

/**
* Get the default format in fonction of the type
* @param type the field type
* @return the date/time format
*/
static QString defaultFormat( const QVariant::Type type );
};

#endif // QGSDATETIMEEDITCONFIG_H
@@ -81,8 +81,9 @@ QString QgsDateTimeEditFactory::representValue( QgsVectorLayer* vl, int fieldIdx
return settings.value( "qgis/nullValue", "NULL" ).toString();
}

const QString displayFormat = config.value( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString fieldFormat = config.value( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QgsField field = vl->fields().at( fieldIdx );
const QString displayFormat = config.value( "display_format", QgsDateTimeEditConfig::defaultFormat( field.type() ) ).toString();
const QString fieldFormat = config.value( "field_format", QgsDateTimeEditConfig::defaultFormat( field.type() ) ).toString();

QDateTime date = QDateTime::fromString( value.toString(), fieldFormat );

@@ -120,7 +121,7 @@ unsigned int QgsDateTimeEditFactory::fieldScore( const QgsVectorLayer* vl, int f
const QgsField field = vl->fields().field( fieldIdx );
const QVariant::Type type = field.type();
const QgsEditorWidgetConfig config = vl->editFormConfig().widgetConfig( field.name() );
if ( type == QVariant::DateTime || config.contains( "field_format" ) )
if ( type == QVariant::DateTime || type == QVariant::Date || type == QVariant::Time || config.contains( "field_format" ) )
{
return 20;
}
@@ -18,6 +18,7 @@
#include "qgsmessagelog.h"
#include "qgslogger.h"
#include "qgsdatetimeedit.h"
#include "qgsdatetimeeditconfig.h"

#include <QDateTimeEdit>
#include <QDateEdit>
@@ -62,7 +63,7 @@ void QgsDateTimeEditWrapper::initWidget( QWidget *editor )
return;
}

const QString displayFormat = config( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString displayFormat = config( "display_format", QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();
mQDateTimeEdit->setDisplayFormat( displayFormat );

const bool calendar = config( "calendar_popup", false ).toBool();
@@ -110,7 +111,7 @@ void QgsDateTimeEditWrapper::showIndeterminateState()

void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
{
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString fieldFormat = config( "field_format", QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();
emit valueChanged( dateTime.toString( fieldFormat ) );
}

@@ -131,7 +132,7 @@ QVariant QgsDateTimeEditWrapper::value() const
}
}

const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString fieldFormat = config( "field_format", QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();

if ( mQgsDateTimeEdit )
{
@@ -148,7 +149,7 @@ void QgsDateTimeEditWrapper::setValue( const QVariant &value )
if ( !mQDateTimeEdit )
return;

const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString fieldFormat = config( "field_format", QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();
const QDateTime date = field().type() == QVariant::DateTime ? value.toDateTime() : QDateTime::fromString( value.toString(), fieldFormat );

if ( mQgsDateTimeEdit )
@@ -20,6 +20,7 @@
#include "qgsvectorlayer.h"
#include "qgsdatetimeedit.h"
#include "qcalendarwidget.h"
#include "qgsdatetimeeditconfig.h"

#include <QSettings>

@@ -45,7 +46,7 @@ QVariant QgsDateTimeSearchWidgetWrapper::value() const
if ( ! mDateTimeEdit )
return QDateTime();

const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString fieldFormat = config( "field_format", QgsDateTimeEditConfig::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
return mDateTimeEdit->dateTime().toString( fieldFormat );
}

@@ -151,7 +152,7 @@ void QgsDateTimeSearchWidgetWrapper::initWidget( QWidget* editor )
{
mDateTimeEdit->setAllowNull( false );

const QString displayFormat = config( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString displayFormat = config( "display_format", QgsDateTimeEditConfig::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
mDateTimeEdit->setDisplayFormat( displayFormat );

const bool calendar = config( "calendar_popup", false ).toBool();

0 comments on commit 5eb70d3

Please sign in to comment.