Skip to content

Commit

Permalink
[FEATURE] Add a search widget wrapper for datetime widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 23, 2016
1 parent 6d86859 commit 026e352
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 1 deletion.
32 changes: 32 additions & 0 deletions python/gui/editorwidgets/qgsdatetimesearchwidgetwrapper.sip
@@ -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 );
};
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -254,6 +254,7 @@
%Include editorwidgets/core/qgswidgetwrapper.sip
%Include editorwidgets/qgscheckboxsearchwidgetwrapper.sip
%Include editorwidgets/qgsdatetimeedit.sip
%Include editorwidgets/qgsdatetimesearchwidgetwrapper.sip
%Include editorwidgets/qgsdefaultsearchwidgetwrapper.sip
%Include editorwidgets/qgsdoublespinbox.sip
%Include editorwidgets/qgsmultiedittoolbutton.sip
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -105,6 +105,7 @@ SET(QGIS_GUI_SRCS
editorwidgets/qgsdatetimeeditfactory.cpp
editorwidgets/qgsdatetimeeditconfig.cpp
editorwidgets/qgsdatetimeeditwrapper.cpp
editorwidgets/qgsdatetimesearchwidgetwrapper.cpp
editorwidgets/qgsdefaultsearchwidgetwrapper.cpp
editorwidgets/qgsdoublespinbox.cpp
editorwidgets/qgsdummyconfigdlg.cpp
Expand Down Expand Up @@ -530,6 +531,7 @@ SET(QGIS_GUI_MOC_HDRS
editorwidgets/qgscolorwidgetwrapper.h
editorwidgets/qgsdatetimeedit.h
editorwidgets/qgsdatetimeeditconfig.h
editorwidgets/qgsdatetimesearchwidgetwrapper.h
editorwidgets/qgsdatetimeeditwrapper.h
editorwidgets/qgsdefaultsearchwidgetwrapper.h
editorwidgets/qgsdoublespinbox.h
Expand Down
6 changes: 6 additions & 0 deletions src/gui/editorwidgets/qgsdatetimeeditfactory.cpp
Expand Up @@ -16,6 +16,7 @@
#include "qgsdatetimeeditfactory.h"
#include "qgsdatetimeeditconfig.h"
#include "qgsdatetimeeditwrapper.h"
#include "qgsdatetimesearchwidgetwrapper.h"

#include <QSettings>

Expand All @@ -29,6 +30,11 @@ QgsEditorWidgetWrapper *QgsDateTimeEditFactory::create( QgsVectorLayer *vl, int
return new QgsDateTimeEditWrapper( vl, fieldIdx, editor, parent );
}

QgsSearchWidgetWrapper* QgsDateTimeEditFactory::createSearchWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) const
{
return new QgsDateTimeSearchWidgetWrapper( vl, fieldIdx, parent );
}

QgsEditorConfigWidget *QgsDateTimeEditFactory::configWidget( QgsVectorLayer *vl, int fieldIdx, QWidget *parent ) const
{
return new QgsDateTimeEditConfig( vl, fieldIdx, parent );
Expand Down
1 change: 1 addition & 0 deletions src/gui/editorwidgets/qgsdatetimeeditfactory.h
Expand Up @@ -34,6 +34,7 @@ class GUI_EXPORT QgsDateTimeEditFactory : public QgsEditorWidgetFactory
// QgsEditorWidgetFactory interface
public:
QgsEditorWidgetWrapper* create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const override;
QgsSearchWidgetWrapper* createSearchWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) const override;
QgsEditorConfigWidget* configWidget( QgsVectorLayer *vl, int fieldIdx, QWidget *parent ) const override;
QgsEditorWidgetConfig readConfig( const QDomElement &configElement, QgsVectorLayer *layer, int fieldIdx ) override;
void writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx ) override;
Expand Down
6 changes: 6 additions & 0 deletions src/gui/editorwidgets/qgsdatetimeeditwrapper.cpp
Expand Up @@ -101,6 +101,12 @@ bool QgsDateTimeEditWrapper::valid() const
return mQgsDateTimeEdit || mQDateTimeEdit;
}

void QgsDateTimeEditWrapper::showIndeterminateState()
{
if ( mQgsDateTimeEdit )
mQgsDateTimeEdit->setEmpty();
}

void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
{
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
Expand Down
1 change: 1 addition & 0 deletions src/gui/editorwidgets/qgsdatetimeeditwrapper.h
Expand Up @@ -55,6 +55,7 @@ class GUI_EXPORT QgsDateTimeEditWrapper : public QgsEditorWidgetWrapper
QWidget *createWidget( QWidget *parent ) override;
void initWidget( QWidget *editor ) override;
bool valid() const override;
virtual void showIndeterminateState() override;

public slots:
void setValue( const QVariant &value ) override;
Expand Down
171 changes: 171 additions & 0 deletions src/gui/editorwidgets/qgsdatetimesearchwidgetwrapper.cpp
@@ -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 ) ) );
}
}


67 changes: 67 additions & 0 deletions src/gui/editorwidgets/qgsdatetimesearchwidgetwrapper.h
@@ -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

0 comments on commit 026e352

Please sign in to comment.