Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
with
346 additions
and 1 deletion.
- +32 −0 python/gui/editorwidgets/qgsdatetimesearchwidgetwrapper.sip
- +1 −0 python/gui/gui.sip
- +2 −0 src/gui/CMakeLists.txt
- +6 −0 src/gui/editorwidgets/qgsdatetimeeditfactory.cpp
- +1 −0 src/gui/editorwidgets/qgsdatetimeeditfactory.h
- +6 −0 src/gui/editorwidgets/qgsdatetimeeditwrapper.cpp
- +1 −0 src/gui/editorwidgets/qgsdatetimeeditwrapper.h
- +171 −0 src/gui/editorwidgets/qgsdatetimesearchwidgetwrapper.cpp
- +67 −0 src/gui/editorwidgets/qgsdatetimesearchwidgetwrapper.h
- +59 −1 tests/src/python/test_qgssearchwidgetwrapper.py
@@ -0,0 +1,32 @@ | ||
/** | ||
* Wraps a date time search widget. | ||
*/ | ||
class QgsDateTimeSearchWidgetWrapper : QgsSearchWidgetWrapper | ||
{ | ||
%TypeHeaderCode | ||
#include <qgsdatetimesearchwidgetwrapper.h> | ||
%End | ||
public: | ||
|
||
public: | ||
explicit QgsDateTimeSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent /TransferThis/ = nullptr ); | ||
bool applyDirectly(); | ||
QString expression(); | ||
bool valid() const; | ||
QVariant value() const; | ||
FilterFlags supportedFlags() const; | ||
FilterFlags defaultFlags() const; | ||
virtual QString createExpression( FilterFlags flags ) const; | ||
|
||
public slots: | ||
|
||
virtual void clearWidget(); | ||
virtual void setEnabled( bool enabled ); | ||
|
||
protected: | ||
QWidget* createWidget( QWidget* parent ); | ||
void initWidget( QWidget* editor ); | ||
|
||
protected slots: | ||
void setExpression( QString exp ); | ||
}; |
@@ -0,0 +1,171 @@ | ||
/*************************************************************************** | ||
qgsdatetimesearchwidgetwrapper.cpp | ||
--------------------------------- | ||
Date : 2016-05-23 | ||
Copyright : (C) 2016 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot 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 "qgsdatetimesearchwidgetwrapper.h" | ||
|
||
#include "qgsfield.h" | ||
#include "qgsdatetimeeditfactory.h" | ||
#include "qgsvectorlayer.h" | ||
#include "qgsdatetimeedit.h" | ||
#include "qcalendarwidget.h" | ||
|
||
#include <QSettings> | ||
|
||
QgsDateTimeSearchWidgetWrapper::QgsDateTimeSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) | ||
: QgsSearchWidgetWrapper( vl, fieldIdx, parent ) | ||
, mDateTimeEdit( nullptr ) | ||
, mLayer( nullptr ) | ||
{ | ||
} | ||
|
||
bool QgsDateTimeSearchWidgetWrapper::applyDirectly() | ||
{ | ||
return true; | ||
} | ||
|
||
QString QgsDateTimeSearchWidgetWrapper::expression() | ||
{ | ||
return mExpression; | ||
} | ||
|
||
QVariant QgsDateTimeSearchWidgetWrapper::value() const | ||
{ | ||
if ( ! mDateTimeEdit ) | ||
return QDateTime(); | ||
|
||
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString(); | ||
return mDateTimeEdit->dateTime().toString( fieldFormat ); | ||
} | ||
|
||
QgsSearchWidgetWrapper::FilterFlags QgsDateTimeSearchWidgetWrapper::supportedFlags() const | ||
{ | ||
return EqualTo | NotEqualTo | GreaterThan | LessThan | GreaterThanOrEqualTo | LessThanOrEqualTo | IsNull; | ||
} | ||
|
||
QgsSearchWidgetWrapper::FilterFlags QgsDateTimeSearchWidgetWrapper::defaultFlags() const | ||
{ | ||
return EqualTo; | ||
} | ||
|
||
QString QgsDateTimeSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const | ||
{ | ||
QString fieldName = QgsExpression::quotedColumnRef( layer()->fields().at( mFieldIdx ).name() ); | ||
|
||
//clear any unsupported flags | ||
flags &= supportedFlags(); | ||
if ( flags & IsNull ) | ||
return fieldName + " IS NULL"; | ||
|
||
QVariant v = value(); | ||
if ( !v.isValid() ) | ||
return QString(); | ||
|
||
if ( flags & EqualTo ) | ||
return fieldName + "='" + v.toString() + '\''; | ||
else if ( flags & NotEqualTo ) | ||
return fieldName + "<>'" + v.toString() + '\''; | ||
else if ( flags & GreaterThan ) | ||
return fieldName + ">'" + v.toString() + '\''; | ||
else if ( flags & LessThan ) | ||
return fieldName + "<'" + v.toString() + '\''; | ||
else if ( flags & GreaterThanOrEqualTo ) | ||
return fieldName + ">='" + v.toString() + '\''; | ||
else if ( flags & LessThanOrEqualTo ) | ||
return fieldName + "<='" + v.toString() + '\''; | ||
|
||
return QString(); | ||
} | ||
|
||
void QgsDateTimeSearchWidgetWrapper::clearWidget() | ||
{ | ||
if ( mDateTimeEdit ) | ||
{ | ||
mDateTimeEdit->setEmpty(); | ||
} | ||
} | ||
|
||
void QgsDateTimeSearchWidgetWrapper::setEnabled( bool enabled ) | ||
{ | ||
if ( mDateTimeEdit ) | ||
{ | ||
mDateTimeEdit->setEnabled( enabled ); | ||
} | ||
} | ||
|
||
bool QgsDateTimeSearchWidgetWrapper::valid() const | ||
{ | ||
return true; | ||
} | ||
|
||
void QgsDateTimeSearchWidgetWrapper::setExpression( QString exp ) | ||
{ | ||
QString fieldName = layer()->fields().at( mFieldIdx ).name(); | ||
|
||
QString str = QString( "%1 = '%3'" ) | ||
.arg( QgsExpression::quotedColumnRef( fieldName ), | ||
exp.replace( '\'', "''" ) | ||
); | ||
mExpression = str; | ||
} | ||
|
||
void QgsDateTimeSearchWidgetWrapper::dateTimeChanged( const QDateTime& dt ) | ||
{ | ||
if ( mDateTimeEdit ) | ||
{ | ||
QString exp = value().toString(); | ||
setExpression( exp ); | ||
if ( dt.isValid() && !dt.isNull() ) | ||
emit valueChanged(); | ||
else | ||
emit valueCleared(); | ||
emit expressionChanged( mExpression ); | ||
} | ||
} | ||
|
||
QWidget* QgsDateTimeSearchWidgetWrapper::createWidget( QWidget* parent ) | ||
{ | ||
QgsDateTimeEdit* widget = new QgsDateTimeEdit( parent ); | ||
widget->setEmpty(); | ||
return widget; | ||
} | ||
|
||
void QgsDateTimeSearchWidgetWrapper::initWidget( QWidget* editor ) | ||
{ | ||
mDateTimeEdit = qobject_cast<QgsDateTimeEdit*>( editor ); | ||
|
||
if ( mDateTimeEdit ) | ||
{ | ||
mDateTimeEdit->setAllowNull( false ); | ||
|
||
const QString displayFormat = config( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString(); | ||
mDateTimeEdit->setDisplayFormat( displayFormat ); | ||
|
||
const bool calendar = config( "calendar_popup", false ).toBool(); | ||
mDateTimeEdit->setCalendarPopup( calendar ); | ||
if ( calendar ) | ||
{ | ||
// highlight today's date | ||
QTextCharFormat todayFormat; | ||
todayFormat.setBackground( QColor( 160, 180, 200 ) ); | ||
mDateTimeEdit->calendarWidget()->setDateTextFormat( QDate::currentDate(), todayFormat ); | ||
} | ||
|
||
mDateTimeEdit->setEmpty(); | ||
|
||
connect( mDateTimeEdit, SIGNAL( dateTimeChanged( QDateTime ) ), this, SLOT( dateTimeChanged( QDateTime ) ) ); | ||
} | ||
} | ||
|
||
|
@@ -0,0 +1,67 @@ | ||
/*************************************************************************** | ||
qgsdatetimesearchwidgetwrapper.h | ||
------------------------------- | ||
Date : 2016-05-23 | ||
Copyright : (C) 2016 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot 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. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSDATETIMESEARCHWIDGETWRAPPER_H | ||
#define QGSDATETIMESEARCHWIDGETWRAPPER_H | ||
|
||
#include "qgssearchwidgetwrapper.h" | ||
#include "qgsdatetimeeditwrapper.h" | ||
|
||
#include <QComboBox> | ||
#include <QListWidget> | ||
#include <QLineEdit> | ||
|
||
class QgsDateTimeEditFactory; | ||
|
||
/** | ||
* Wraps a date time search widget. | ||
*/ | ||
class GUI_EXPORT QgsDateTimeSearchWidgetWrapper : public QgsSearchWidgetWrapper | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit QgsDateTimeSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent = nullptr ); | ||
bool applyDirectly() override; | ||
QString expression() override; | ||
bool valid() const override; | ||
QVariant value() const; | ||
FilterFlags supportedFlags() const override; | ||
FilterFlags defaultFlags() const override; | ||
virtual QString createExpression( FilterFlags flags ) const override; | ||
|
||
public slots: | ||
|
||
virtual void clearWidget() override; | ||
virtual void setEnabled( bool enabled ) override; | ||
|
||
protected: | ||
QWidget* createWidget( QWidget* parent ) override; | ||
void initWidget( QWidget* editor ) override; | ||
|
||
protected slots: | ||
void setExpression( QString exp ) override; | ||
|
||
private slots: | ||
void dateTimeChanged( const QDateTime &date ); | ||
|
||
private: | ||
QgsDateTimeEdit* mDateTimeEdit; | ||
QgsVectorLayer* mLayer; | ||
|
||
friend class QgsDateTimeEditFactory; | ||
}; | ||
|
||
#endif // QGSDATETIMESEARCHWIDGETWRAPPER_H |
Oops, something went wrong.