QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgsfields.h"
19 #include "qgsdatetimeeditfactory.h"
20 #include "qgsvectorlayer.h"
21 #include "qgsdatetimeedit.h"
22 #include "qcalendarwidget.h"
23 #include "qgsdatetimeeditconfig.h"
25 
26 #include <QSettings>
27 
29  : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
30 
31 {
32 }
33 
35 {
36  return true;
37 }
38 
40 {
41  return mExpression;
42 }
43 
45 {
46  if ( ! mDateTimeEdit )
47  return QDateTime();
48 
49  const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
50  const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
51  if ( fieldIsoFormat )
52  {
53  return mDateTimeEdit->dateTime().toString( Qt::ISODate );
54  }
55  else
56  {
57  return mDateTimeEdit->dateTime().toString( fieldFormat );
58  }
59 }
60 
61 QgsSearchWidgetWrapper::FilterFlags QgsDateTimeSearchWidgetWrapper::supportedFlags() const
62 {
64 }
65 
66 QgsSearchWidgetWrapper::FilterFlags QgsDateTimeSearchWidgetWrapper::defaultFlags() const
67 {
68  return EqualTo;
69 }
70 
71 QString QgsDateTimeSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
72 {
73  const QString fieldName = createFieldIdentifier();
74 
75  //clear any unsupported flags
76  flags &= supportedFlags();
77  if ( flags & IsNull )
78  return fieldName + " IS NULL";
79  if ( flags & IsNotNull )
80  return fieldName + " IS NOT NULL";
81 
82  const QVariant v = value();
83  if ( !v.isValid() )
84  return QString();
85 
86  if ( flags & EqualTo )
87  return fieldName + "='" + v.toString() + '\'';
88  else if ( flags & NotEqualTo )
89  return fieldName + "<>'" + v.toString() + '\'';
90  else if ( flags & GreaterThan )
91  return fieldName + ">'" + v.toString() + '\'';
92  else if ( flags & LessThan )
93  return fieldName + "<'" + v.toString() + '\'';
94  else if ( flags & GreaterThanOrEqualTo )
95  return fieldName + ">='" + v.toString() + '\'';
96  else if ( flags & LessThanOrEqualTo )
97  return fieldName + "<='" + v.toString() + '\'';
98 
99  return QString();
100 }
101 
103 {
104  if ( mDateTimeEdit )
105  {
106  mDateTimeEdit->setEmpty();
107  }
108 }
109 
111 {
112  if ( mDateTimeEdit )
113  {
114  mDateTimeEdit->setEnabled( enabled );
115  }
116 }
117 
119 {
120  return true;
121 }
122 
123 void QgsDateTimeSearchWidgetWrapper::setExpression( const QString &expression )
124 {
125  QString exp = expression;
126  const QString fieldName = layer()->fields().at( mFieldIdx ).name();
127 
128  const QString str = QStringLiteral( "%1 = '%3'" )
129  .arg( QgsExpression::quotedColumnRef( fieldName ),
130  exp.replace( '\'', QLatin1String( "''" ) )
131  );
132  mExpression = str;
133 }
134 
135 void QgsDateTimeSearchWidgetWrapper::dateTimeChanged( const QDateTime &dt )
136 {
137  if ( mDateTimeEdit )
138  {
139  const QString exp = value().toString();
140  setExpression( exp );
141  if ( dt.isValid() && !dt.isNull() )
142  emit valueChanged();
143  else
144  emit valueCleared();
146  }
147 }
148 
150 {
151  QgsDateTimeEdit *widget = new QgsDateTimeEdit( parent );
152  widget->setEmpty();
153  return widget;
154 }
155 
157 {
158  mDateTimeEdit = qobject_cast<QgsDateTimeEdit *>( editor );
159 
160  if ( mDateTimeEdit )
161  {
162  mDateTimeEdit->setAllowNull( false );
163 
164  const QString displayFormat = config( QStringLiteral( "display_format" ), QgsDateTimeFieldFormatter::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
165  mDateTimeEdit->setDisplayFormat( displayFormat );
166 
167  const bool calendar = config( QStringLiteral( "calendar_popup" ), false ).toBool();
168  mDateTimeEdit->setCalendarPopup( calendar );
169  if ( calendar && mDateTimeEdit->calendarWidget() )
170  {
171  // highlight today's date
172  QTextCharFormat todayFormat;
173  todayFormat.setBackground( QColor( 160, 180, 200 ) );
174  mDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
175  }
176 
177  mDateTimeEdit->setEmpty();
178 
179  connect( mDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &QgsDateTimeSearchWidgetWrapper::dateTimeChanged );
180  }
181 }
182 
183 
QgsDateTimeEdit
The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/reading null date/times.
Definition: qgsdatetimeedit.h:36
qgsfields.h
QgsDateTimeSearchWidgetWrapper::applyDirectly
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes.
Definition: qgsdatetimesearchwidgetwrapper.cpp:34
QgsSearchWidgetWrapper::expressionChanged
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
QgsSearchWidgetWrapper::valueChanged
void valueChanged()
Emitted when a user changes the value of the search widget.
QgsSearchWidgetWrapper::Between
@ Between
Supports searches between two values.
Definition: qgssearchwidgetwrapper.h:102
QgsDateTimeEdit::setEmpty
void setEmpty()
Resets the widget to show no value (ie, an "unknown" state).
Definition: qgsdatetimeedit.cpp:100
QgsSearchWidgetWrapper::NotEqualTo
@ NotEqualTo
Supports not equal to.
Definition: qgssearchwidgetwrapper.h:97
QgsDateTimeSearchWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgsdatetimesearchwidgetwrapper.cpp:118
QgsSearchWidgetWrapper
Shows a search widget on a filter form.
Definition: qgssearchwidgetwrapper.h:85
qgsdatetimesearchwidgetwrapper.h
QgsDateTimeSearchWidgetWrapper::setExpression
void setExpression(const QString &exp) override
Definition: qgsdatetimesearchwidgetwrapper.cpp:123
QgsDateTimeSearchWidgetWrapper::defaultFlags
QgsSearchWidgetWrapper::FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
Definition: qgsdatetimesearchwidgetwrapper.cpp:66
QgsDateTimeSearchWidgetWrapper::value
QVariant value() const
Returns a variant representing the current state of the widget, respecting the editor widget's config...
Definition: qgsdatetimesearchwidgetwrapper.cpp:44
QgsSearchWidgetWrapper::GreaterThanOrEqualTo
@ GreaterThanOrEqualTo
Supports >=.
Definition: qgssearchwidgetwrapper.h:100
QgsField::name
QString name
Definition: qgsfield.h:60
QgsDateTimeSearchWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgsdatetimesearchwidgetwrapper.cpp:156
QgsWidgetWrapper::widget
QWidget * widget()
Access the widget managed by this wrapper.
Definition: qgswidgetwrapper.cpp:47
QgsSearchWidgetWrapper::valueCleared
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state.
QgsDateTimeSearchWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgsdatetimesearchwidgetwrapper.cpp:149
QgsDateTimeSearchWidgetWrapper::supportedFlags
QgsSearchWidgetWrapper::FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
Definition: qgsdatetimesearchwidgetwrapper.cpp:61
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsDateTimeSearchWidgetWrapper::expression
QString expression() const override
Will be used to access the widget's value.
Definition: qgsdatetimesearchwidgetwrapper.cpp:39
QgsWidgetWrapper::layer
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
Definition: qgswidgetwrapper.cpp:92
QgsSearchWidgetWrapper::mFieldIdx
int mFieldIdx
Definition: qgssearchwidgetwrapper.h:266
qgsdatetimeedit.h
QgsDateTimeSearchWidgetWrapper::createExpression
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
Definition: qgsdatetimesearchwidgetwrapper.cpp:71
QgsDateTimeEdit::dateTime
QDateTime dateTime() const
Returns the date time which can be a null date/time.
Definition: qgsdatetimeedit.cpp:339
QgsSearchWidgetWrapper::IsNotBetween
@ IsNotBetween
Supports searching for values outside of a set range.
Definition: qgssearchwidgetwrapper.h:107
QgsDateTimeSearchWidgetWrapper::QgsDateTimeSearchWidgetWrapper
QgsDateTimeSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsDateTimeSearchWidgetWrapper.
Definition: qgsdatetimesearchwidgetwrapper.cpp:28
qgsdatetimeeditfactory.h
QgsDateTimeSearchWidgetWrapper::setEnabled
void setEnabled(bool enabled) override
Definition: qgsdatetimesearchwidgetwrapper.cpp:110
QgsSearchWidgetWrapper::mExpression
QString mExpression
Definition: qgssearchwidgetwrapper.h:265
qgsdatetimefieldformatter.h
QgsDateTimeEdit::setAllowNull
void setAllowNull(bool allowNull)
Determines if the widget allows setting null date/time.
Definition: qgsdatetimeedit.cpp:70
QgsSearchWidgetWrapper::GreaterThan
@ GreaterThan
Supports greater than.
Definition: qgssearchwidgetwrapper.h:98
QgsSearchWidgetWrapper::IsNull
@ IsNull
Supports searching for null values.
Definition: qgssearchwidgetwrapper.h:106
QgsDateTimeSearchWidgetWrapper::clearWidget
void clearWidget() override
Definition: qgsdatetimesearchwidgetwrapper.cpp:102
qgsvectorlayer.h
QgsDateTimeFieldFormatter::defaultFormat
static QString defaultFormat(QVariant::Type type)
Gets the default format in function of the type.
Definition: qgsdatetimefieldformatter.cpp:106
QgsWidgetWrapper::config
QVariantMap config() const
Returns the whole config.
Definition: qgswidgetwrapper.cpp:82
str
#define str(x)
Definition: qgis.cpp:37
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsSearchWidgetWrapper::LessThanOrEqualTo
@ LessThanOrEqualTo
Supports <=.
Definition: qgssearchwidgetwrapper.h:101
QgsExpression::quotedColumnRef
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
Definition: qgsexpression.cpp:68
QgsSearchWidgetWrapper::LessThan
@ LessThan
Supports less than.
Definition: qgssearchwidgetwrapper.h:99
QgsSearchWidgetWrapper::createFieldIdentifier
QString createFieldIdentifier() const
Gets a field name or expression to use as field comparison.
Definition: qgssearchwidgetwrapper.cpp:104
QgsSearchWidgetWrapper::EqualTo
@ EqualTo
Supports equal to.
Definition: qgssearchwidgetwrapper.h:96
QgsFields::at
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163
QgsSearchWidgetWrapper::IsNotNull
@ IsNotNull
Supports searching for non-null values.
Definition: qgssearchwidgetwrapper.h:108
qgsdatetimeeditconfig.h