QGIS API Documentation  2.8.6-Wien
qgsdatetimeedit.cpp
Go to the documentation of this file.
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 
28  : QDateTimeEdit( parent )
29  , mAllowNull( true )
30  , mIsNull( true )
31 {
32  mClearButton = new QToolButton( this );
33  mClearButton->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
34  mClearButton->setCursor( Qt::ArrowCursor );
35  mClearButton->setStyleSheet( "position: absolute; border: none; padding: 0px;" );
36  mClearButton->hide();
37  connect( mClearButton, SIGNAL( clicked() ), this, SLOT( clear() ) );
38 
39  mNullLabel = new QLineEdit( QSettings().value( "qgis/nullValue", "NULL" ).toString(), this );
40  mNullLabel->setReadOnly( true );
41  mNullLabel->setStyleSheet( "position: absolute; border: none; font-style: italic; color: grey;" );
42  mNullLabel->hide();
43 
44  setStyleSheet( QString( "padding-right: %1px;" ).arg( mClearButton->sizeHint().width() + spinButtonWidth() + frameWidth() + 1 ) );
45 
46  QSize msz = minimumSizeHint();
47  setMinimumSize( qMax( msz.width(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ),
48  qMax( msz.height(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ) );
49 
50  connect( this, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( changed( QDateTime ) ) );
51 
52  // init with current time so mIsNull is properly initialized
53  QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
54 }
55 
57 {
58  mAllowNull = allowNull;
59 
60  mNullLabel->setVisible( mAllowNull && mIsNull );
61  mClearButton->setVisible( mAllowNull && !mIsNull );
62  lineEdit()->setVisible( !mAllowNull || !mIsNull );
63 }
64 
65 
67 {
68  changed( QDateTime() );
69  emit dateTimeChanged( QDateTime() );
70 }
71 
72 void QgsDateTimeEdit::mousePressEvent( QMouseEvent* event )
73 {
74  QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
75  if ( mAllowNull && mIsNull && lerect.contains( event->pos() ) )
76  return;
77 
78  QDateTimeEdit::mousePressEvent( event );
79 }
80 
81 void QgsDateTimeEdit::changed( const QDateTime & dateTime )
82 {
83  mIsNull = dateTime.isNull();
84  mNullLabel->setVisible( mAllowNull && mIsNull );
85  mClearButton->setVisible( mAllowNull && !mIsNull );
86  lineEdit()->setVisible( !mAllowNull || !mIsNull );
87 }
88 
89 int QgsDateTimeEdit::spinButtonWidth() const
90 {
91  return calendarPopup() ? 25 : 18;
92 }
93 
94 int QgsDateTimeEdit::frameWidth() const
95 {
96  return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
97 }
98 
99 void QgsDateTimeEdit::setDateTime( const QDateTime& dateTime )
100 {
101  // set an undefined date
102  if ( !dateTime.isValid() || dateTime.isNull() )
103  {
104  clear();
105  }
106  else
107  {
108  QDateTimeEdit::setDateTime( dateTime );
109  mIsNull = false;
110  }
111 }
112 
113 QDateTime QgsDateTimeEdit::dateTime() const
114 {
115  if ( mAllowNull && mIsNull )
116  {
117  return QDateTime();
118  }
119  else
120  {
121  return QDateTimeEdit::dateTime();
122  }
123 }
124 
125 void QgsDateTimeEdit::resizeEvent( QResizeEvent * event )
126 {
127  QDateTimeEdit::resizeEvent( event );
128 
129  QSize sz = mClearButton->sizeHint();
130 
131 
132  mClearButton->move( rect().right() - frameWidth() - spinButtonWidth() - sz.width(),
133  ( rect().bottom() + 1 - sz.height() ) / 2 );
134 
135  mNullLabel->move( 0, 0 );
136  mNullLabel->setMinimumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
137  mNullLabel->setMaximumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
138 }
139 
140 
141 
142 
QDateTime dateTime() const
dateTime returns the date time which can eventually be a null date/time
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
QgsDateTimeEdit(QWidget *parent=0)
bool allowNull() const
void setAllowNull(bool allowNull)
determines if the widget allows setting null date/time.
void setDateTime(const QDateTime &dateTime)
setDateTime set the date time in the widget and handles null date times.
virtual void resizeEvent(QResizeEvent *event) override
virtual void clear() override
Set the current date as NULL.
void mousePressEvent(QMouseEvent *event) override