Skip to content

Commit 01b290c

Browse files
committedSep 1, 2014
new edit widget for date/time capable of setting null dates
1 parent a4377ae commit 01b290c

File tree

8 files changed

+325
-25
lines changed

8 files changed

+325
-25
lines changed
 

‎src/gui/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ editorwidgets/qgsclassificationwidgetwrapper.cpp
6464
editorwidgets/qgsclassificationwidgetwrapperfactory.cpp
6565
editorwidgets/qgscolorwidgetwrapper.cpp
6666
editorwidgets/qgscolorwidgetfactory.cpp
67+
editorwidgets/qgsdatetimeedit.cpp
6768
editorwidgets/qgsdatetimeeditfactory.cpp
6869
editorwidgets/qgsdatetimeeditconfig.cpp
6970
editorwidgets/qgsdatetimeeditwrapper.cpp
@@ -288,6 +289,7 @@ editorwidgets/qgscheckboxconfigdlg.h
288289
editorwidgets/qgscheckboxwidgetwrapper.h
289290
editorwidgets/qgsclassificationwidgetwrapper.h
290291
editorwidgets/qgscolorwidgetwrapper.h
292+
editorwidgets/qgsdatetimeedit.h
291293
editorwidgets/qgsdatetimeeditconfig.h
292294
editorwidgets/qgsdatetimeeditwrapper.h
293295
editorwidgets/qgsdummyconfigdlg.h
@@ -510,6 +512,7 @@ editorwidgets/qgsclassificationwidgetwrapper.h
510512
editorwidgets/qgsclassificationwidgetwrapperfactory.h
511513
editorwidgets/qgscolorwidgetwrapper.h
512514
editorwidgets/qgscolorwidgetfactory.h
515+
editorwidgets/qgsdatetimeedit.h
513516
editorwidgets/qgsdatetimeeditfactory.h
514517
editorwidgets/qgsdatetimeeditconfig.h
515518
editorwidgets/qgsdatetimeeditwrapper.h
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/***************************************************************************
2+
qgsdatetimeedit.cpp
3+
--------------------------------------
4+
Date : 08.2014
5+
Copyright : (C) 2014 Denis Rouzaud
6+
Email : denis.rouzaud@gmail.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include <QLineEdit>
17+
#include <QMouseEvent>
18+
#include <QSettings>
19+
#include <QStyle>
20+
#include <QToolButton>
21+
22+
#include "qgsdatetimeedit.h"
23+
24+
#include "qgsapplication.h"
25+
#include "qgslogger.h"
26+
27+
QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
28+
: QDateTimeEdit( parent )
29+
, mAllowNull( true )
30+
{
31+
mClearButton = new QToolButton( this );
32+
mClearButton->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
33+
mClearButton->setCursor( Qt::ArrowCursor );
34+
mClearButton->setStyleSheet( "position: absolute; border: none; padding: 0px;" );
35+
mClearButton->hide();
36+
connect( mClearButton, SIGNAL( clicked() ), this, SLOT( clear() ) );
37+
38+
mNullLabel = new QLineEdit( QSettings().value( "qgis/nullValue", "NULL" ).toString(), this );
39+
mNullLabel->setReadOnly( true );
40+
mNullLabel->setStyleSheet( "position: absolute; border: none; font-style: italic; color: grey;" );
41+
mNullLabel->hide();
42+
43+
setStyleSheet( QString( "padding-right: %1px;" ).arg( mClearButton->sizeHint().width() + spinButtonWidth() + frameWidth() + 1 ) );
44+
45+
QSize msz = minimumSizeHint();
46+
setMinimumSize( qMax( msz.width(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ),
47+
qMax( msz.height(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ) );
48+
49+
connect( this, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( changed( QDateTime ) ) );
50+
51+
// init with current time so mIsNull is properly initialized
52+
QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
53+
}
54+
55+
void QgsDateTimeEdit::setAllowNull( bool allowNull )
56+
{
57+
mAllowNull = allowNull;
58+
59+
mNullLabel->setVisible( mAllowNull && mIsNull );
60+
mClearButton->setVisible( mAllowNull && !mIsNull );
61+
lineEdit()->setVisible( !mAllowNull || !mIsNull );
62+
}
63+
64+
65+
void QgsDateTimeEdit::clear()
66+
{
67+
changed( QDateTime() );
68+
emit dateTimeChanged( QDateTime() );
69+
}
70+
71+
void QgsDateTimeEdit::mousePressEvent( QMouseEvent* event )
72+
{
73+
QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
74+
if ( mAllowNull && mIsNull && lerect.contains( event->pos() ) )
75+
return;
76+
77+
QDateTimeEdit::mousePressEvent( event );
78+
}
79+
80+
void QgsDateTimeEdit::changed( const QDateTime & dateTime )
81+
{
82+
mIsNull = dateTime.isNull();
83+
mNullLabel->setVisible( mAllowNull && mIsNull );
84+
mClearButton->setVisible( mAllowNull && !mIsNull );
85+
lineEdit()->setVisible( !mAllowNull || !mIsNull );
86+
}
87+
88+
int QgsDateTimeEdit::spinButtonWidth() const
89+
{
90+
return calendarPopup() ? 25 : 18;
91+
}
92+
93+
int QgsDateTimeEdit::frameWidth() const
94+
{
95+
return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
96+
}
97+
98+
void QgsDateTimeEdit::setDateTime( const QDateTime& dateTime )
99+
{
100+
// set an undefined date
101+
if ( !dateTime.isValid() || dateTime.isNull() )
102+
{
103+
clear();
104+
}
105+
else
106+
{
107+
QDateTimeEdit::setDateTime( dateTime );
108+
}
109+
}
110+
111+
QDateTime QgsDateTimeEdit::dateTime() const
112+
{
113+
if ( mAllowNull && mIsNull )
114+
{
115+
return QDateTime();
116+
}
117+
else
118+
{
119+
return QDateTimeEdit::dateTime();
120+
}
121+
}
122+
123+
void QgsDateTimeEdit::resizeEvent( QResizeEvent * event )
124+
{
125+
QDateTimeEdit::resizeEvent( event );
126+
127+
QSize sz = mClearButton->sizeHint();
128+
129+
130+
mClearButton->move( rect().right() - frameWidth() - spinButtonWidth() - sz.width() ,
131+
( rect().bottom() + 1 - sz.height() ) / 2 );
132+
133+
mNullLabel->move( 0, 0 );
134+
mNullLabel->setMinimumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
135+
mNullLabel->setMaximumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
136+
}
137+
138+
139+
140+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/***************************************************************************
2+
qgsdatetimeedit.h
3+
--------------------------------------
4+
Date : 08.2014
5+
Copyright : (C) 2014 Denis Rouzaud
6+
Email : denis.rouzaud@gmail.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSDATETIMEEDIT_H
17+
#define QGSDATETIMEEDIT_H
18+
19+
#include <QDateTimeEdit>
20+
21+
class QToolButton;
22+
class QLineEdit;
23+
24+
/**
25+
* @brief The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/reading null date/times.
26+
*/
27+
class GUI_EXPORT QgsDateTimeEdit : public QDateTimeEdit
28+
{
29+
Q_OBJECT
30+
Q_PROPERTY( bool allowNull READ allowNull WRITE setAllowNull )
31+
32+
public:
33+
explicit QgsDateTimeEdit( QWidget *parent = 0 );
34+
35+
//! determines if the widget allows setting null date/time.
36+
void setAllowNull( bool allowNull );
37+
bool allowNull() {return mAllowNull;}
38+
39+
/**
40+
* @brief setDateTime set the date time in the widget and handles null date times.
41+
* @note since QDateTimeEdit::setDateTime() is not virtual, setDateTime must be called for QgsDateTimeEdit.
42+
*/
43+
void setDateTime( const QDateTime &dateTime );
44+
45+
/**
46+
* @brief dateTime returns the date time which can eventually be a null date/time
47+
* @note since QDateTimeEdit::dateTime() is not virtual, dateTime must be called for QgsDateTimeEdit.
48+
*/
49+
QDateTime dateTime() const;
50+
51+
//! Set the current date as NULL
52+
//! @note if the widget is not configured to accept NULL dates, this will have no effect
53+
virtual void clear();
54+
55+
56+
protected:
57+
virtual void resizeEvent( QResizeEvent* event );
58+
59+
void mousePressEvent( QMouseEvent*event );
60+
61+
62+
private slots:
63+
void changed( const QDateTime & dateTime );
64+
65+
66+
private:
67+
int spinButtonWidth() const;
68+
int frameWidth() const;
69+
70+
bool mAllowNull;
71+
bool mIsNull;
72+
73+
QLineEdit* mNullLabel;
74+
QToolButton* mClearButton;
75+
76+
};
77+
78+
#endif // QGSDATETIMEEDIT_H

‎src/gui/editorwidgets/qgsdatetimeeditconfig.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ QgsEditorWidgetConfig QgsDateTimeEditConfig::config()
108108
myConfig.insert( "field_format", mFieldFormatEdit->text() );
109109
myConfig.insert( "display_format", mDisplayFormatEdit->text() );
110110
myConfig.insert( "calendar_popup", mCalendarPopupCheckBox->isChecked() );
111+
myConfig.insert( "allow_null", mAllowNullCheckBox->isChecked() );
111112

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

153+
if ( config.contains( "allow_null" ) )
154+
{
155+
mAllowNullCheckBox->setChecked( config[ "allow_null" ].toBool() );
156+
}
157+
152158
}

‎src/gui/editorwidgets/qgsdatetimeeditfactory.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ QgsEditorWidgetConfig QgsDateTimeEditFactory::readConfig( const QDomElement& con
4343
cfg.insert( "field_format", configElement.attribute( "field_format" ) );
4444
cfg.insert( "display_format", configElement.attribute( "display_format" ) );
4545
cfg.insert( "calendar_popup", configElement.attribute( "calendar_popup" ) == "1" );
46+
cfg.insert( "allow_null", configElement.attribute( "allow_null" ) == "1" );
4647

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

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

0 commit comments

Comments
 (0)