QGIS API Documentation  2.8.2-Wien
 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
53  QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
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 
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 
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  }
110 }
111 
112 QDateTime QgsDateTimeEdit::dateTime() const
113 {
114  if ( mAllowNull && mIsNull )
115  {
116  return QDateTime();
117  }
118  else
119  {
120  return QDateTimeEdit::dateTime();
121  }
122 }
123 
124 void QgsDateTimeEdit::resizeEvent( QResizeEvent * event )
125 {
127 
128  QSize sz = mClearButton->sizeHint();
129 
130 
131  mClearButton->move( rect().right() - frameWidth() - spinButtonWidth() - sz.width(),
132  ( rect().bottom() + 1 - sz.height() ) / 2 );
133 
134  mNullLabel->move( 0, 0 );
135  mNullLabel->setMinimumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
136  mNullLabel->setMaximumSize( rect().adjusted( 0, 0, -spinButtonWidth(), 0 ).size() );
137 }
138 
139 
140 
141