QGIS API Documentation 4.3.0-Master (35968c35f7e)
Loading...
Searching...
No Matches
qgsdatetimesearchwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatetimesearchwidgetwrapper.cpp
3 ---------------------------------
4 Date : 2016-05-23
5 Copyright : (C) 2016 Nyall Dawson
6 Email : nyall dot dawson at gmail dot 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
17
18#include "qgsdatetimeedit.h"
22#include "qgsfields.h"
23#include "qgsvectorlayer.h"
24
25#include <QSettings>
26#include <QString>
27#include <qcalendarwidget.h>
28
29#include "moc_qgsdatetimesearchwidgetwrapper.cpp"
30
31using namespace Qt::StringLiterals;
32
34 : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
35
36{}
37
39{
40 return true;
41}
42
47
49{
50 if ( !mDateTimeEdit )
51 return QDateTime();
52
53 QMetaType::Type fieldType = QMetaType::QString;
54 if ( QgsVectorLayer *vl = layer() )
55 {
56 fieldType = vl->fields().at( mFieldIdx ).type();
57 }
58
59 switch ( fieldType )
60 {
61 case QMetaType::Type::QDateTime:
62 return mDateTimeEdit->dateTime();
63 case QMetaType::Type::QDate:
64 return mDateTimeEdit->date();
65 case QMetaType::Type::QTime:
66 return mDateTimeEdit->time();
67 default:
68 break;
69 }
70
71 // if not an explicit date/time field, treat as string values
72 const bool fieldIsoFormat = config( u"field_iso_format"_s, false ).toBool();
73 const QString fieldFormat = config( u"field_format"_s, QgsDateTimeFieldFormatter::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
74 if ( fieldIsoFormat )
75 {
76 return mDateTimeEdit->dateTime().toString( Qt::ISODate );
77 }
78 else
79 {
80 return mDateTimeEdit->dateTime().toString( fieldFormat );
81 }
82}
83
88
93
95{
96 const QString fieldName = createFieldIdentifier();
97 const QMetaType::Type fieldType = layer()->fields().at( mFieldIdx ).type();
98
99 //clear any unsupported flags
100 flags &= supportedFlags();
101 if ( flags & IsNull )
102 return fieldName + " IS NULL";
103 if ( flags & IsNotNull )
104 return fieldName + " IS NOT NULL";
105
106 const QVariant v = value();
107 if ( !v.isValid() )
108 return QString();
109
110 QString valuePart;
111 switch ( fieldType )
112 {
113 case QMetaType::Type::QDateTime:
114 valuePart = u"to_datetime('%1')"_s.arg( v.toDateTime().toString( Qt::ISODate ) );
115 break;
116 case QMetaType::Type::QDate:
117 valuePart = u"to_date('%1')"_s.arg( v.toDate().toString( Qt::ISODate ) );
118 break;
119 case QMetaType::Type::QTime:
120 valuePart = u"to_time('%1')"_s.arg( v.toTime().toString( Qt::ISODate ) );
121 break;
122 default:
123 valuePart = QgsExpression::quotedString( v.toString() );
124 break;
125 }
126
127 if ( flags & EqualTo )
128 return fieldName + "=" + valuePart;
129 else if ( flags & NotEqualTo )
130 return fieldName + "<>" + valuePart;
131 else if ( flags & GreaterThan )
132 return fieldName + ">" + valuePart;
133 else if ( flags & LessThan )
134 return fieldName + "<" + valuePart;
135 else if ( flags & GreaterThanOrEqualTo )
136 return fieldName + ">=" + valuePart;
137 else if ( flags & LessThanOrEqualTo )
138 return fieldName + "<=" + valuePart;
139
140 return QString();
141}
142
144{
145 if ( mDateTimeEdit )
146 {
147 mDateTimeEdit->setEmpty();
148 }
149}
150
152{
153 if ( mDateTimeEdit )
154 {
155 mDateTimeEdit->setEnabled( enabled );
156 }
157}
158
160{
161 return true;
162}
163
165{
166 QString exp = expression;
167
168 const QgsField field = layer()->fields().at( mFieldIdx );
169 const QMetaType::Type fieldType = field.type();
170 const QString fieldName = field.name();
171 switch ( fieldType )
172 {
173 case QMetaType::Type::QDateTime:
174 mExpression = u"%1 = to_datetime('%2')"_s.arg( QgsExpression::quotedColumnRef( fieldName ), expression );
175 break;
176 case QMetaType::Type::QDate:
177 mExpression = u"%1 = to_date('%2')"_s.arg( QgsExpression::quotedColumnRef( fieldName ), expression );
178 break;
179 case QMetaType::Type::QTime:
180 mExpression = u"%1 = to_time('%2')"_s.arg( QgsExpression::quotedColumnRef( fieldName ), expression );
181 break;
182 default:
184 break;
185 }
186}
187
188void QgsDateTimeSearchWidgetWrapper::dateTimeChanged( const QDateTime &dt )
189{
190 if ( mDateTimeEdit )
191 {
192 const QVariant v = value();
193 QString valueAsString;
194 switch ( v.userType() )
195 {
196 case QMetaType::Type::QDateTime:
197 valueAsString = v.toDateTime().toString( Qt::ISODate );
198 break;
199 case QMetaType::Type::QDate:
200 valueAsString = v.toDate().toString( Qt::ISODate );
201 break;
202 case QMetaType::Type::QTime:
203 valueAsString = v.toTime().toString( Qt::ISODate );
204 break;
205 default:
206 valueAsString = v.toString();
207 break;
208 }
209
210 setExpression( valueAsString );
211 if ( dt.isValid() && !dt.isNull() )
212 emit valueChanged();
213 else
214 emit valueCleared();
216 }
217}
218
220{
221 QgsDateTimeEdit *widget = new QgsDateTimeEdit( parent );
222 widget->setEmpty();
223 return widget;
224}
225
227{
228 mDateTimeEdit = qobject_cast<QgsDateTimeEdit *>( editor );
229
230 if ( mDateTimeEdit )
231 {
232 mDateTimeEdit->setAllowNull( false );
233
234 const QString displayFormat = config( u"display_format"_s, QgsDateTimeFieldFormatter::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
235 mDateTimeEdit->setDisplayFormat( displayFormat );
236
237 const bool calendar = config( u"calendar_popup"_s, false ).toBool();
238 mDateTimeEdit->setCalendarPopup( calendar );
239 if ( calendar && mDateTimeEdit->calendarWidget() )
240 {
241 // highlight today's date
242 QTextCharFormat todayFormat;
243 todayFormat.setBackground( QColor( 160, 180, 200 ) );
244 mDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
245 }
246
247 mDateTimeEdit->setEmpty();
248
249 connect( mDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &QgsDateTimeSearchWidgetWrapper::dateTimeChanged );
250 }
251}
A QDateTimeEdit with the capability of setting/reading null date/times.
static QString defaultFormat(QMetaType::Type type)
Gets the default format in function of the type.
void setExpression(const QString &exp) override
QString expression() const override
Will be used to access the widget's value.
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QgsSearchWidgetWrapper::FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
QgsSearchWidgetWrapper::FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
QVariant value() const
Returns a variant representing the current state of the widget, respecting the editor widget's config...
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
Creates a filter expression based on the current state of the search widget and the specified filter ...
QgsDateTimeSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsDateTimeSearchWidgetWrapper.
bool valid() const override
Returns true if the widget has been properly initialized.
static QString quotedString(QString text)
Returns a quoted version of a string (in single quotes).
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value, QMetaType::Type fieldType=QMetaType::Type::UnknownType)
Create an expression allowing to evaluate if a field is equal to a value.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes).
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
QMetaType::Type type
Definition qgsfield.h:63
QString name
Definition qgsfield.h:65
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
@ IsNotBetween
Supports searching for values outside of a set range.
@ LessThan
Supports less than.
@ IsNull
Supports searching for null values.
@ GreaterThan
Supports greater than.
@ IsNotNull
Supports searching for non-null values.
@ Between
Supports searches between two values.
@ NotEqualTo
Supports not equal to.
void valueChanged()
Emitted when a user changes the value of the search widget.
QgsSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Create a new widget wrapper.
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state.
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
QString createFieldIdentifier() const
Gets a field name or expression to use as field comparison.
QFlags< FilterFlag > FilterFlags
Represents a vector layer which manages a vector based dataset.
QWidget * widget()
Access the widget managed by this wrapper.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QVariantMap config() const
Returns the whole config.