17 #include <QMouseEvent> 20 #include <QToolButton> 28 : QDateTimeEdit( parent )
32 mClearButton =
new QToolButton(
this );
34 mClearButton->setCursor( Qt::ArrowCursor );
35 mClearButton->setStyleSheet(
"position: absolute; border: none; padding: 0px;" );
37 connect( mClearButton, SIGNAL( clicked() ),
this, SLOT(
clear() ) );
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;" );
44 setStyleSheet( QString(
"padding-right: %1px;" ).arg( mClearButton->sizeHint().width() + spinButtonWidth() + frameWidth() + 1 ) );
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 ) );
50 connect(
this, SIGNAL( dateTimeChanged( QDateTime ) ),
this, SLOT( changed( QDateTime ) ) );
53 QDateTimeEdit::setDateTime( QDateTime::currentDateTime() );
60 mNullLabel->setVisible( mAllowNull && mIsNull );
61 mClearButton->setVisible( mAllowNull && !mIsNull );
62 lineEdit()->setVisible( !mAllowNull || !mIsNull );
68 changed( QDateTime() );
69 emit dateTimeChanged( QDateTime() );
74 QRect lerect = rect().adjusted( 0, 0, -spinButtonWidth(), 0 );
75 if ( mAllowNull && mIsNull && lerect.contains( event->pos() ) )
78 QDateTimeEdit::mousePressEvent( event );
81 void QgsDateTimeEdit::changed(
const QDateTime &
dateTime )
83 mIsNull = dateTime.isNull();
84 mNullLabel->setVisible( mAllowNull && mIsNull );
85 mClearButton->setVisible( mAllowNull && !mIsNull );
86 lineEdit()->setVisible( !mAllowNull || !mIsNull );
89 int QgsDateTimeEdit::spinButtonWidth()
const 91 return calendarPopup() ? 25 : 18;
94 int QgsDateTimeEdit::frameWidth()
const 96 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
102 if ( !dateTime.isValid() || dateTime.isNull() )
108 QDateTimeEdit::setDateTime( dateTime );
115 if ( mAllowNull && mIsNull )
121 return QDateTimeEdit::dateTime();
127 QDateTimeEdit::resizeEvent( event );
129 QSize sz = mClearButton->sizeHint();
132 mClearButton->move( rect().right() - frameWidth() - spinButtonWidth() - sz.width(),
133 ( rect().bottom() + 1 - sz.height() ) / 2 );
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() );
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)
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