Skip to content

Commit

Permalink
Fix auto conf of QgsDateTimeEditWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Valsecchi committed Sep 7, 2016
1 parent 8d9cf9d commit 5eb70d3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
22 changes: 20 additions & 2 deletions src/gui/editorwidgets/qgsdatetimeeditconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 )
Expand All @@ -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() )
{
Expand Down
7 changes: 7 additions & 0 deletions src/gui/editorwidgets/qgsdatetimeeditconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 4 additions & 3 deletions src/gui/editorwidgets/qgsdatetimeeditfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 5 additions & 4 deletions src/gui/editorwidgets/qgsdatetimeeditwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgsmessagelog.h"
#include "qgslogger.h"
#include "qgsdatetimeedit.h"
#include "qgsdatetimeeditconfig.h"

#include <QDateTimeEdit>
#include <QDateEdit>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 ) );
}

Expand All @@ -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 )
{
Expand All @@ -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 )
Expand Down
5 changes: 3 additions & 2 deletions src/gui/editorwidgets/qgsdatetimesearchwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "qgsvectorlayer.h"
#include "qgsdatetimeedit.h"
#include "qcalendarwidget.h"
#include "qgsdatetimeeditconfig.h"

#include <QSettings>

Expand All @@ -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 );
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 5eb70d3

Please sign in to comment.