Skip to content
Permalink
Browse files

new edit widget for date/time capable of setting null dates

  • Loading branch information
3nids committed Aug 28, 2014
1 parent a4377ae commit 01b290c4d4556f0c99ec36db5b327dd4f1ea2e88
@@ -64,6 +64,7 @@ editorwidgets/qgsclassificationwidgetwrapper.cpp
editorwidgets/qgsclassificationwidgetwrapperfactory.cpp
editorwidgets/qgscolorwidgetwrapper.cpp
editorwidgets/qgscolorwidgetfactory.cpp
editorwidgets/qgsdatetimeedit.cpp
editorwidgets/qgsdatetimeeditfactory.cpp
editorwidgets/qgsdatetimeeditconfig.cpp
editorwidgets/qgsdatetimeeditwrapper.cpp
@@ -288,6 +289,7 @@ editorwidgets/qgscheckboxconfigdlg.h
editorwidgets/qgscheckboxwidgetwrapper.h
editorwidgets/qgsclassificationwidgetwrapper.h
editorwidgets/qgscolorwidgetwrapper.h
editorwidgets/qgsdatetimeedit.h
editorwidgets/qgsdatetimeeditconfig.h
editorwidgets/qgsdatetimeeditwrapper.h
editorwidgets/qgsdummyconfigdlg.h
@@ -510,6 +512,7 @@ editorwidgets/qgsclassificationwidgetwrapper.h
editorwidgets/qgsclassificationwidgetwrapperfactory.h
editorwidgets/qgscolorwidgetwrapper.h
editorwidgets/qgscolorwidgetfactory.h
editorwidgets/qgsdatetimeedit.h
editorwidgets/qgsdatetimeeditfactory.h
editorwidgets/qgsdatetimeeditconfig.h
editorwidgets/qgsdatetimeeditwrapper.h
@@ -0,0 +1,140 @@
/***************************************************************************
qgsdatetimeedit.cpp
--------------------------------------
Date : 08.2014
Copyright : (C) 2014 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QLineEdit>
#include <QMouseEvent>
#include <QSettings>
#include <QStyle>
#include <QToolButton>

#include "qgsdatetimeedit.h"

#include "qgsapplication.h"
#include "qgslogger.h"

QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
: QDateTimeEdit( parent )
, mAllowNull( true )
{
mClearButton = new QToolButton( this );
mClearButton->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
mClearButton->setCursor( Qt::ArrowCursor );
mClearButton->setStyleSheet( "position: absolute; border: none; padding: 0px;" );
mClearButton->hide();
connect( mClearButton, SIGNAL( clicked() ), this, SLOT( clear() ) );

mNullLabel = new QLineEdit( QSettings().value( "qgis/nullValue", "NULL" ).toString(), this );
mNullLabel->setReadOnly( true );
mNullLabel->setStyleSheet( "position: absolute; border: none; font-style: italic; color: grey;" );
mNullLabel->hide();

setStyleSheet( QString( "padding-right: %1px;" ).arg( mClearButton->sizeHint().width() + spinButtonWidth() + frameWidth() + 1 ) );

QSize msz = minimumSizeHint();
setMinimumSize( qMax( msz.width(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ),
qMax( msz.height(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ) );

connect( this, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( changed( QDateTime ) ) );

// init with current time so mIsNull is properly initialized
QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
}

void QgsDateTimeEdit::setAllowNull( bool allowNull )
{
mAllowNull = allowNull;

mNullLabel->setVisible( mAllowNull && mIsNull );
mClearButton->setVisible( mAllowNull && !mIsNull );
lineEdit()->setVisible( !mAllowNull || !mIsNull );
}


void QgsDateTimeEdit::clear()
{
changed( QDateTime() );
emit dateTimeChanged( QDateTime() );
}

void QgsDateTimeEdit::mousePressEvent( QMouseEvent* event )
{
QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
if ( mAllowNull && mIsNull && lerect.contains( event->pos() ) )
return;

QDateTimeEdit::mousePressEvent( event );
}

void QgsDateTimeEdit::changed( const QDateTime & dateTime )
{
mIsNull = dateTime.isNull();
mNullLabel->setVisible( mAllowNull && mIsNull );
mClearButton->setVisible( mAllowNull && !mIsNull );
lineEdit()->setVisible( !mAllowNull || !mIsNull );
}

int QgsDateTimeEdit::spinButtonWidth() const
{
return calendarPopup() ? 25 : 18;
}

int QgsDateTimeEdit::frameWidth() const
{
return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
}

void QgsDateTimeEdit::setDateTime( const QDateTime& dateTime )
{
// set an undefined date
if ( !dateTime.isValid() || dateTime.isNull() )
{
clear();
}
else
{
QDateTimeEdit::setDateTime( dateTime );
}
}

QDateTime QgsDateTimeEdit::dateTime() const
{
if ( mAllowNull && mIsNull )
{
return QDateTime();
}
else
{
return QDateTimeEdit::dateTime();
}
}

void QgsDateTimeEdit::resizeEvent( QResizeEvent * event )
{
QDateTimeEdit::resizeEvent( event );

QSize sz = mClearButton->sizeHint();


mClearButton->move( rect().right() - frameWidth() - spinButtonWidth() - sz.width() ,
( rect().bottom() + 1 - sz.height() ) / 2 );

mNullLabel->move( 0, 0 );
mNullLabel->setMinimumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
mNullLabel->setMaximumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
}




@@ -0,0 +1,78 @@
/***************************************************************************
qgsdatetimeedit.h
--------------------------------------
Date : 08.2014
Copyright : (C) 2014 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSDATETIMEEDIT_H
#define QGSDATETIMEEDIT_H

#include <QDateTimeEdit>

class QToolButton;
class QLineEdit;

/**
* @brief The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/reading null date/times.
*/
class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
{
Q_OBJECT
Q_PROPERTY( bool allowNull READ allowNull WRITE setAllowNull )

public:
explicit QgsDateTimeEdit( QWidget *parent = 0 );

//! determines if the widget allows setting null date/time.
void setAllowNull( bool allowNull );
bool allowNull() {return mAllowNull;}

/**
* @brief setDateTime set the date time in the widget and handles null date times.
* @note since QDateTimeEdit::setDateTime() is not virtual, setDateTime must be called for QgsDateTimeEdit.
*/
void setDateTime( const QDateTime &dateTime );

/**
* @brief dateTime returns the date time which can eventually be a null date/time
* @note since QDateTimeEdit::dateTime() is not virtual, dateTime must be called for QgsDateTimeEdit.
*/
QDateTime dateTime() const;

//! Set the current date as NULL
//! @note if the widget is not configured to accept NULL dates, this will have no effect
virtual void clear();


protected:
virtual void resizeEvent( QResizeEvent* event );

void mousePressEvent( QMouseEvent*event );


private slots:
void changed( const QDateTime & dateTime );


private:
int spinButtonWidth() const;
int frameWidth() const;

bool mAllowNull;
bool mIsNull;

QLineEdit* mNullLabel;
QToolButton* mClearButton;

};

#endif // QGSDATETIMEEDIT_H
@@ -108,6 +108,7 @@ QgsEditorWidgetConfig QgsDateTimeEditConfig::config()
myConfig.insert( "field_format", mFieldFormatEdit->text() );
myConfig.insert( "display_format", mDisplayFormatEdit->text() );
myConfig.insert( "calendar_popup", mCalendarPopupCheckBox->isChecked() );
myConfig.insert( "allow_null", mAllowNullCheckBox->isChecked() );

return myConfig;
}
@@ -149,4 +150,9 @@ void QgsDateTimeEditConfig::setConfig( const QgsEditorWidgetConfig &config )
mCalendarPopupCheckBox->setChecked( config[ "calendar_popup" ].toBool() );
}

if ( config.contains( "allow_null" ) )
{
mAllowNullCheckBox->setChecked( config[ "allow_null" ].toBool() );
}

}
@@ -43,6 +43,7 @@ QgsEditorWidgetConfig QgsDateTimeEditFactory::readConfig( const QDomElement& con
cfg.insert( "field_format", configElement.attribute( "field_format" ) );
cfg.insert( "display_format", configElement.attribute( "display_format" ) );
cfg.insert( "calendar_popup", configElement.attribute( "calendar_popup" ) == "1" );
cfg.insert( "allow_null", configElement.attribute( "allow_null" ) == "1" );

return cfg;
}
@@ -56,6 +57,7 @@ void QgsDateTimeEditFactory::writeConfig( const QgsEditorWidgetConfig& config, Q
configElement.setAttribute( "field_format", config["field_format"].toString() );
configElement.setAttribute( "display_format", config["display_format"].toString() );
configElement.setAttribute( "calendar_popup", config["calendar_popup"].toBool() );
configElement.setAttribute( "allow_null", config["allow_null"].toBool() );
}

QString QgsDateTimeEditFactory::representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const

0 comments on commit 01b290c

Please sign in to comment.
You can’t perform that action at this time.