QGIS API Documentation 3.99.0-Master (d270888f95f)
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}
38
40{
41 return true;
42}
43
48
50{
51 if ( !mDateTimeEdit )
52 return QDateTime();
53
54 const bool fieldIsoFormat = config( u"field_iso_format"_s, false ).toBool();
55 const QString fieldFormat = config( u"field_format"_s, QgsDateTimeFieldFormatter::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
56 if ( fieldIsoFormat )
57 {
58 return mDateTimeEdit->dateTime().toString( Qt::ISODate );
59 }
60 else
61 {
62 return mDateTimeEdit->dateTime().toString( fieldFormat );
63 }
64}
65
70
75
77{
78 const QString fieldName = createFieldIdentifier();
79
80 //clear any unsupported flags
81 flags &= supportedFlags();
82 if ( flags & IsNull )
83 return fieldName + " IS NULL";
84 if ( flags & IsNotNull )
85 return fieldName + " IS NOT NULL";
86
87 const QVariant v = value();
88 if ( !v.isValid() )
89 return QString();
90
91 if ( flags & EqualTo )
92 return fieldName + "='" + v.toString() + '\'';
93 else if ( flags & NotEqualTo )
94 return fieldName + "<>'" + v.toString() + '\'';
95 else if ( flags & GreaterThan )
96 return fieldName + ">'" + v.toString() + '\'';
97 else if ( flags & LessThan )
98 return fieldName + "<'" + v.toString() + '\'';
99 else if ( flags & GreaterThanOrEqualTo )
100 return fieldName + ">='" + v.toString() + '\'';
101 else if ( flags & LessThanOrEqualTo )
102 return fieldName + "<='" + v.toString() + '\'';
103
104 return QString();
105}
106
108{
109 if ( mDateTimeEdit )
110 {
111 mDateTimeEdit->setEmpty();
112 }
113}
114
116{
117 if ( mDateTimeEdit )
118 {
119 mDateTimeEdit->setEnabled( enabled );
120 }
121}
122
124{
125 return true;
126}
127
129{
130 QString exp = expression;
131 const QString fieldName = layer()->fields().at( mFieldIdx ).name();
132
133 const QString str = u"%1 = '%3'"_s
134 .arg( QgsExpression::quotedColumnRef( fieldName ), exp.replace( '\'', "''"_L1 ) );
135 mExpression = str;
136}
137
138void QgsDateTimeSearchWidgetWrapper::dateTimeChanged( const QDateTime &dt )
139{
140 if ( mDateTimeEdit )
141 {
142 const QString exp = value().toString();
143 setExpression( exp );
144 if ( dt.isValid() && !dt.isNull() )
145 emit valueChanged();
146 else
147 emit valueCleared();
149 }
150}
151
153{
154 QgsDateTimeEdit *widget = new QgsDateTimeEdit( parent );
155 widget->setEmpty();
156 return widget;
157}
158
160{
161 mDateTimeEdit = qobject_cast<QgsDateTimeEdit *>( editor );
162
163 if ( mDateTimeEdit )
164 {
165 mDateTimeEdit->setAllowNull( false );
166
167 const QString displayFormat = config( u"display_format"_s, QgsDateTimeFieldFormatter::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
168 mDateTimeEdit->setDisplayFormat( displayFormat );
169
170 const bool calendar = config( u"calendar_popup"_s, false ).toBool();
171 mDateTimeEdit->setCalendarPopup( calendar );
172 if ( calendar && mDateTimeEdit->calendarWidget() )
173 {
174 // highlight today's date
175 QTextCharFormat todayFormat;
176 todayFormat.setBackground( QColor( 160, 180, 200 ) );
177 mDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
178 }
179
180 mDateTimeEdit->setEmpty();
181
182 connect( mDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &QgsDateTimeSearchWidgetWrapper::dateTimeChanged );
183 }
184}
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 quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes).
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.