QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
54 }
55 
56 void QgsDateTimeEdit::setAllowNull( bool allowNull )
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 
73 {
74  QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
75  if ( mAllowNull && mIsNull && lerect.contains( event->pos() ) )
76  return;
77 
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 
114 {
115  if ( mAllowNull && mIsNull )
116  {
117  return QDateTime();
118  }
119  else
120  {
121  return QDateTimeEdit::dateTime();
122  }
123 }
124 
126 {
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 
void setStyleSheet(const QString &styleSheet)
QDateTime dateTime() const
dateTime returns the date time which can eventually be a null date/time
void dateTimeChanged(const QDateTime &datetime)
int width() const
void setCursor(const QCursor &)
virtual QSize sizeHint() const
virtual QSize minimumSizeHint() const
QStyle * style() const
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const =0
virtual void setVisible(bool visible)
QgsDateTimeEdit(QWidget *parent=0)
void setIcon(const QIcon &icon)
void setReadOnly(bool)
QSize size() const
void setMinimumSize(const QSize &)
QLineEdit * lineEdit() const
void move(int x, int y)
bool contains(const QPoint &point, bool proper) const
void hide()
virtual void mousePressEvent(QMouseEvent *event)
QRect rect() const
QRect adjusted(int dx1, int dy1, int dx2, int dy2) const
void setMaximumSize(const QSize &)
bool isValid() const
bool calendarPopup() const
void setDateTime(const QDateTime &dateTime)
QDateTime currentDateTime()
bool allowNull() const
bool isNull() const
virtual void resizeEvent(QResizeEvent *event)
int height() const
int bottom() const
void setAllowNull(bool allowNull)
determines if the widget allows setting null date/time.
const QPoint & pos() const
void setDateTime(const QDateTime &dateTime)
setDateTime set the date time in the widget and handles null date times.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual void resizeEvent(QResizeEvent *event) override
virtual void clear() override
Set the current date as NULL.
void mousePressEvent(QMouseEvent *event) override