QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsfield.h"
19 #include "qgsdatetimeeditfactory.h"
20 #include "qgsvectorlayer.h"
21 #include "qgsdatetimeedit.h"
22 #include "qcalendarwidget.h"
23 
24 #include <QSettings>
25 
27  : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
28  , mDateTimeEdit( nullptr )
29  , mLayer( nullptr )
30 {
31 }
32 
34 {
35  return true;
36 }
37 
39 {
40  return mExpression;
41 }
42 
44 {
45  if ( ! mDateTimeEdit )
46  return QDateTime();
47 
48  const bool fieldIsoFormat = config( "field_iso_format", false ).toBool();
49  const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
50 
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  QString fieldName = QgsExpression::quotedColumnRef( layer()->fields().at( mFieldIdx ).name() );
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  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 
124 {
125  QString fieldName = layer()->fields().at( mFieldIdx ).name();
126 
127  QString str = QString( "%1 = '%3'" )
128  .arg( QgsExpression::quotedColumnRef( fieldName ),
129  exp.replace( '\'', "''" )
130  );
131  mExpression = str;
132 }
133 
134 void QgsDateTimeSearchWidgetWrapper::dateTimeChanged( const QDateTime& dt )
135 {
136  if ( mDateTimeEdit )
137  {
138  QString exp = value().toString();
139  setExpression( exp );
140  if ( dt.isValid() && !dt.isNull() )
141  emit valueChanged();
142  else
143  emit valueCleared();
145  }
146 }
147 
149 {
150  QgsDateTimeEdit* widget = new QgsDateTimeEdit( parent );
151  widget->setEmpty();
152  return widget;
153 }
154 
156 {
157  mDateTimeEdit = qobject_cast<QgsDateTimeEdit*>( editor );
158 
159  if ( mDateTimeEdit )
160  {
161  mDateTimeEdit->setAllowNull( false );
162 
163  const QString displayFormat = config( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
164  mDateTimeEdit->setDisplayFormat( displayFormat );
165 
166  const bool calendar = config( "calendar_popup", false ).toBool();
167  mDateTimeEdit->setCalendarPopup( calendar );
168  if ( calendar && mDateTimeEdit->calendarWidget() )
169  {
170  // highlight today's date
171  QTextCharFormat todayFormat;
172  todayFormat.setBackground( QColor( 160, 180, 200 ) );
173  mDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat );
174  }
175 
176  mDateTimeEdit->setEmpty();
177 
178  connect( mDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) );
179  }
180 }
181 
182 
QString toString(Qt::DateFormat format) const
Manages an editor widget Widget and wrapper share the same parent.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
#define QGSDATETIMEEDIT_DATEFORMAT
QString name
Definition: qgsfield.h:52
void setDisplayFormat(const QString &format)
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QVariant value() const
Returns a variant representing the current state of the widget, respecting the editor widget&#39;s config...
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:422
QgsFields fields() const
Returns the list of fields of this layer.
const char * name() const
void setEnabled(bool)
bool valid() const override
Return true if the widget has been properly initialized.
void valueChanged()
Emitted when a user changes the value of the search widget.
void setBackground(const QBrush &brush)
void setDateTextFormat(const QDate &date, const QTextCharFormat &format)
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
bool isValid() const
void setCalendarPopup(bool enable)
QString & replace(int position, int n, QChar after)
QCalendarWidget * calendarWidget() const
bool isNull() const
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state...
QDate currentDate()
void setEmpty()
Resets the widget to show no value (ie, an "unknown" state).
QString expression() override
Will be used to access the widget&#39;s value.
void setAllowNull(bool allowNull)
determines if the widget allows setting null date/time.
virtual QString createExpression(FilterFlags flags) const override
Creates a filter expression based on the current state of the search widget and the specified filter ...
bool isValid() const
QWidget * widget()
Access the widget managed by this wrapper.
QgsEditorWidgetConfig config() const
Returns the whole config.
QgsDateTimeSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsDateTimeSearchWidgetWrapper.
virtual void setEnabled(bool enabled) override
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Represents a vector layer which manages a vector based data sets.
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes...
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
QDateTime dateTime() const
dateTime returns the date time which can eventually be a null date/time
The QgsDateTimeEdit class is a QDateTimeEdit with the capability of setting/reading null date/times...