-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsdatetimeeditwrapper.cpp
169 lines (147 loc) · 5.26 KB
/
qgsdatetimeeditwrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/***************************************************************************
qgsdatetimeeditwrapper.cpp
--------------------------------------
Date : 03.2014
Copyright : (C) 2014 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsdatetimeeditwrapper.h"
#include "qgsdatetimeeditfactory.h"
#include "qgsmessagelog.h"
#include "qgslogger.h"
#include <QDateTimeEdit>
#include <QDateEdit>
#include <QTimeEdit>
#include <QTextCharFormat>
#include <QCalendarWidget>
QgsDateTimeEditWrapper::QgsDateTimeEditWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
: QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
, mQDateTimeEdit( nullptr )
, mQgsDateTimeEdit( nullptr )
{
}
QWidget *QgsDateTimeEditWrapper::createWidget( QWidget *parent )
{
QgsDateTimeEdit* widget = new QgsDateTimeEdit( parent );
widget->setDateTime( QDateTime::currentDateTime() );
return widget;
}
void QgsDateTimeEditWrapper::initWidget( QWidget *editor )
{
QgsDateTimeEdit* qgsEditor = dynamic_cast<QgsDateTimeEdit*>( editor );
if ( qgsEditor )
{
mQgsDateTimeEdit = qgsEditor;
}
// assign the Qt editor also if the QGIS editor has been previously assigned
// this avoids testing each time which widget to use
// the QGIS editor must be used for non-virtual methods (dateTime, setDateTime)
QDateTimeEdit* qtEditor = dynamic_cast<QDateTimeEdit*>( editor );
if ( qtEditor )
{
mQDateTimeEdit = qtEditor;
}
if ( !mQDateTimeEdit )
{
QgsDebugMsg( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit." );
QgsMessageLog::logMessage( "Date/time edit widget could not be initialized because provided widget is not a QDateTimeEdit.", "UI forms", QgsMessageLog::WARNING );
return;
}
const QString displayFormat = config( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
mQDateTimeEdit->setDisplayFormat( displayFormat );
const bool calendar = config( "calendar_popup", false ).toBool();
mQDateTimeEdit->setCalendarPopup( calendar );
if ( calendar )
{
// highlight today's date
QTextCharFormat todayFormat;
todayFormat.setBackground( QColor( 160, 180, 200 ) );
mQDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
}
const bool allowNull = config( "allow_null", true ).toBool();
if ( mQgsDateTimeEdit )
{
mQgsDateTimeEdit->setAllowNull( allowNull );
}
else
{
QgsMessageLog::instance()->logMessage( tr( "The usual date/time widget QDateTimeEdit cannot be configured to allow NULL values. "
"For that the QGIS custom widget QgsDateTimeEdit needs to be used." ),
"field widgets" );
}
if ( mQgsDateTimeEdit )
{
connect( mQgsDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
}
else
{
connect( mQDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
}
}
bool QgsDateTimeEditWrapper::valid() const
{
return mQgsDateTimeEdit || mQDateTimeEdit;
}
void QgsDateTimeEditWrapper::showIndeterminateState()
{
if ( mQgsDateTimeEdit )
mQgsDateTimeEdit->setEmpty();
}
void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
{
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
emit valueChanged( dateTime.toString( fieldFormat ) );
}
QVariant QgsDateTimeEditWrapper::value() const
{
if ( !mQDateTimeEdit )
return QVariant( field().type() );
if ( field().type() == QVariant::DateTime )
{
if ( mQgsDateTimeEdit )
{
return mQgsDateTimeEdit->dateTime();
}
else
{
return mQDateTimeEdit->dateTime();
}
}
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
if ( mQgsDateTimeEdit )
{
return mQgsDateTimeEdit->dateTime().toString( fieldFormat );
}
else
{
return mQDateTimeEdit->dateTime().toString( fieldFormat );
}
}
void QgsDateTimeEditWrapper::setValue( const QVariant &value )
{
if ( !mQDateTimeEdit )
return;
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QDateTime date = field().type() == QVariant::DateTime ? value.toDateTime() : QDateTime::fromString( value.toString(), fieldFormat );
if ( mQgsDateTimeEdit )
{
mQgsDateTimeEdit->setDateTime( date );
}
else
{
mQDateTimeEdit->setDateTime( date );
}
}
void QgsDateTimeEditWrapper::setEnabled( bool enabled )
{
if ( !mQDateTimeEdit )
return;
mQDateTimeEdit->setEnabled( enabled );
}