Skip to content

Commit

Permalink
Add time field filter option for QgsFieldComboWidget (also add docs)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 11, 2016
1 parent b4029dc commit 5177d93
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
27 changes: 17 additions & 10 deletions python/gui/qgsfieldproxymodel.sip
Expand Up @@ -10,15 +10,18 @@ class QgsFieldProxyModel : QSortFilterProxyModel
%End

public:

//! Field type filters
enum Filter
{
String,
Int,
LongLong,
Double,
Numeric,
Date,
All
String, /*!< String fields */
Int, /*!< Integer fields */
LongLong, /*!< Longlong fields */
Double, /*!< Double fields */
Numeric, /*!< All numeric fields */
Date, /*!< Date or datetime fields */
Time, /*!< Time fields */
All, /*!< All fields */
};
typedef QFlags<QgsFieldProxyModel::Filter> Filters;

Expand All @@ -28,15 +31,19 @@ class QgsFieldProxyModel : QSortFilterProxyModel
*/
explicit QgsFieldProxyModel( QObject *parent /TransferThis/ = 0 );

//! sourceFieldModel returns the QgsFieldModel used in this QSortFilterProxyModel
//! Returns the QgsFieldModel used in this QSortFilterProxyModel
QgsFieldModel* sourceFieldModel();

/**
* @brief setFilters set flags that affect how fields are filtered
* Set flags that affect how fields are filtered in the model.
* @param filters are Filter flags
* @note added in 2.3
* @see filters()
*/
QgsFieldProxyModel* setFilters( const Filters& filters );

/** Returns the filters controlling displayed fields.
* @see setFilters()
*/
const Filters& filters() const;

// QSortFilterProxyModel interface
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsfieldproxymodel.cpp
Expand Up @@ -54,7 +54,8 @@ bool QgsFieldProxyModel::filterAcceptsRow( int source_row, const QModelIndex &so
( mFilters.testFlag( Int ) && type == QVariant::Int ) ||
( mFilters.testFlag( Double ) && type == QVariant::Double ) ||
( mFilters.testFlag( Date ) && type == QVariant::Date ) ||
( mFilters.testFlag( Date ) && type == QVariant::DateTime ) )
( mFilters.testFlag( Date ) && type == QVariant::DateTime ) ||
( mFilters.testFlag( Time ) && type == QVariant::Time ) )
return true;

return false;
Expand Down
27 changes: 17 additions & 10 deletions src/gui/qgsfieldproxymodel.h
Expand Up @@ -30,15 +30,18 @@ class GUI_EXPORT QgsFieldProxyModel : public QSortFilterProxyModel
Q_FLAGS( Filters )

public:

//! Field type filters
enum Filter
{
String = 1,
Int = 2,
LongLong = 4,
Double = 8,
Numeric = Int | LongLong | Double,
Date = 16,
All = Numeric | Date | String
String = 1, /*!< String fields */
Int = 2, /*!< Integer fields */
LongLong = 4, /*!< Longlong fields */
Double = 8, /*!< Double fields */
Numeric = Int | LongLong | Double, /*!< All numeric fields */
Date = 16, /*!< Date or datetime fields */
Time = 32, /*!< Time fields */
All = Numeric | Date | String | Time, /*!< All fields */
};
Q_DECLARE_FLAGS( Filters, Filter )

Expand All @@ -48,15 +51,19 @@ class GUI_EXPORT QgsFieldProxyModel : public QSortFilterProxyModel
*/
explicit QgsFieldProxyModel( QObject *parent = nullptr );

//! sourceFieldModel returns the QgsFieldModel used in this QSortFilterProxyModel
//! Returns the QgsFieldModel used in this QSortFilterProxyModel
QgsFieldModel* sourceFieldModel() { return mModel; }

/**
* @brief setFilters set flags that affect how fields are filtered
* Set flags that affect how fields are filtered in the model.
* @param filters are Filter flags
* @note added in 2.3
* @see filters()
*/
QgsFieldProxyModel* setFilters( const QgsFieldProxyModel::Filters& filters );

/** Returns the filters controlling displayed fields.
* @see setFilters()
*/
const Filters& filters() const { return mFilters; }

private:
Expand Down
4 changes: 4 additions & 0 deletions tests/src/gui/testqgsfieldexpressionwidget.cpp
Expand Up @@ -266,6 +266,10 @@ void TestQgsFieldExpressionWidget::testFilters()
QCOMPARE( widget->mCombo->itemText( 0 ), QString( "datefld" ) );
QCOMPARE( widget->mCombo->itemText( 1 ), QString( "datetimefld" ) );

widget->setFilters( QgsFieldProxyModel::Time );
QCOMPARE( widget->mCombo->count(), 1 );
QCOMPARE( widget->mCombo->itemText( 0 ), QString( "timefld" ) );

QgsMapLayerRegistry::instance()->removeMapLayer( layer );
}

Expand Down

0 comments on commit 5177d93

Please sign in to comment.