-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add a search widget wrapper for datetime widgets
- Loading branch information
1 parent
6d86859
commit 026e352
Showing
10 changed files
with
346 additions
and
1 deletion.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
python/gui/editorwidgets/qgsdatetimesearchwidgetwrapper.sip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
src/gui/editorwidgets/qgsdatetimesearchwidgetwrapper.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) ) ); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.