Skip to content
Permalink
Browse files

Add filter chaining to the pre-filters of the relation reference wdg

  • Loading branch information
m-kuhn committed Mar 20, 2015
1 parent 868eeb2 commit 733b7c9c8d83062c053a13743a7a2b0afc5fce26
@@ -52,6 +52,13 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
*/
QgsAttributeTableFilterModel( QgsMapCanvas* canvas, QgsAttributeTableModel* sourceModel, QObject* parent = 0 );

/**
* Set the attribute table model that backs this model
*
* @param sourceModel The model
*
* @note added in 2.0
*/
void setSourceModel( QgsAttributeTableModel* sourceModel );

/**
@@ -77,6 +84,11 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
*/
virtual void setFilteredFeatures( QgsFeatureIds ids );

/**
* Get a list of currently filtered feature ids
*
* @return A list of feature ids
*/
QgsFeatureIds filteredFeatures();

/**
@@ -86,6 +98,11 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
*/
void setFilterMode( FilterMode filterMode );

/**
* The current filterModel
*
* @return Mode
*/
FilterMode filterMode() { return mFilterMode; }

/**
@@ -125,6 +142,15 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub

virtual QModelIndex mapFromMaster( const QModelIndex &sourceIndex ) const;

/**
* Sort by the given column using the given order.
* Prefetches all the data from the layer to speed up sorting.
*
* @param column The column which should be sorted
* @param order The order ( Qt::AscendingOrder or Qt::DescendingOrder )
*/
virtual void sort( int column, Qt::SortOrder order = Qt::AscendingOrder ) override;

protected:
/**
* Returns true if the source row will be accepted
@@ -146,15 +172,6 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
*/
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;

/**
* Sort by the given column using the given order.
* Prefetches all the data from the layer to speed up sorting.
*
* @param column The column which should be sorted
* @param order The order ( Qt::AscendingOrder or Qt::DescendingOrder )
*/
virtual void sort( int column, Qt::SortOrder order = Qt::AscendingOrder ) override;

public slots:
/**
* Is called upon every change of the visible extents on the map canvas.
@@ -6,9 +6,11 @@
#include "qgsattributetablefiltermodel.h"

#include <QItemSelection>
#include <QSettings>

QgsFeatureListModel::QgsFeatureListModel( QgsAttributeTableFilterModel *sourceModel, QObject *parent )
: QAbstractProxyModel( parent )
, mInjectNull( false )
{
setSourceModel( sourceModel );
mExpression = new QgsExpression( "" );
@@ -53,6 +55,18 @@ QModelIndex QgsFeatureListModel::fidToIdx( const QgsFeatureId fid ) const

QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const
{
if ( mInjectNull && index.row() == 0 )
{
if ( role == Qt::DisplayRole )
{
return QSettings().value( "qgis/nullValue", "NULL" ).toString();
}
else
{
return QVariant( QVariant::Int );
}
}

if ( role == Qt::DisplayRole || role == Qt::EditRole )
{
QgsFeature feat;
@@ -108,6 +122,21 @@ Qt::ItemFlags QgsFeatureListModel::flags( const QModelIndex &index ) const
return sourceModel()->flags( mapToSource( index ) ) & ~Qt::ItemIsEditable;
}

void QgsFeatureListModel::setInjectNull( bool injectNull )
{
if ( mInjectNull != injectNull )
{
emit beginResetModel();
mInjectNull = injectNull;
emit endResetModel();
}
}

bool QgsFeatureListModel::injectNull()
{
return mInjectNull;
}

QgsAttributeTableModel* QgsFeatureListModel::masterModel()
{
return mFilterModel->masterModel();
@@ -181,15 +210,19 @@ QModelIndex QgsFeatureListModel::mapToMaster( const QModelIndex &proxyIndex ) co
if ( !proxyIndex.isValid() )
return QModelIndex();

return mFilterModel->mapToMaster( mFilterModel->index( proxyIndex.row(), proxyIndex.column() ) );
int offset = mInjectNull ? 1 : 0;

return mFilterModel->mapToMaster( mFilterModel->index( proxyIndex.row() - offset, proxyIndex.column() ) );
}

QModelIndex QgsFeatureListModel::mapFromMaster( const QModelIndex &sourceIndex ) const
{
if ( !sourceIndex.isValid() )
return QModelIndex();

return createIndex( mFilterModel->mapFromMaster( sourceIndex ).row(), 0 );
int offset = mInjectNull ? 1 : 0;

return createIndex( mFilterModel->mapFromMaster( sourceIndex ).row() + offset, 0 );
}

QItemSelection QgsFeatureListModel::mapSelectionFromMaster( const QItemSelection& selection ) const
@@ -209,7 +242,9 @@ QModelIndex QgsFeatureListModel::mapToSource( const QModelIndex &proxyIndex ) co
if ( !proxyIndex.isValid() )
return QModelIndex();

return sourceModel()->index( proxyIndex.row(), proxyIndex.column() );
int offset = mInjectNull ? 1 : 0;

return sourceModel()->index( proxyIndex.row() - offset, proxyIndex.column() );
}

QModelIndex QgsFeatureListModel::mapFromSource( const QModelIndex &sourceIndex ) const
@@ -223,6 +258,7 @@ QModelIndex QgsFeatureListModel::mapFromSource( const QModelIndex &sourceIndex )
QModelIndex QgsFeatureListModel::index( int row, int column, const QModelIndex& parent ) const
{
Q_UNUSED( parent )

return createIndex( row, column );
}

@@ -241,7 +277,10 @@ int QgsFeatureListModel::columnCount( const QModelIndex&parent ) const
int QgsFeatureListModel::rowCount( const QModelIndex& parent ) const
{
Q_UNUSED( parent )
return sourceModel()->rowCount();

int offset = mInjectNull ? 1 : 0;

return sourceModel()->rowCount() + offset;
}

QModelIndex QgsFeatureListModel::fidToIndex( QgsFeatureId fid )
@@ -46,6 +46,20 @@ class GUI_EXPORT QgsFeatureListModel : public QAbstractProxyModel, public QgsFea
virtual QVariant data( const QModelIndex& index, int role ) const override;
virtual Qt::ItemFlags flags( const QModelIndex& index ) const override;

/**
* @brief If true is specified, a NULL value will be injected
* @param injectNull state of null value injection
* @note added in 2.9
*/
void setInjectNull( bool injectNull );

/**
* @brief Returns the current state of null value injection
* @return If a NULL value is added
* @note added in 2.9
*/
bool injectNull();

QgsAttributeTableModel* masterModel();

/**
@@ -94,6 +108,7 @@ class GUI_EXPORT QgsFeatureListModel : public QAbstractProxyModel, public QgsFea
QgsExpression* mExpression;
QgsAttributeTableFilterModel* mFilterModel;
QString mParserErrorString;
bool mInjectNull;
};

Q_DECLARE_METATYPE( QgsFeatureListModel::FeatureInfo )
@@ -80,6 +80,8 @@ void QgsRelationReferenceConfigDlg::setConfig( const QMap<QString, QVariant>& co
{
addFilterField( fld );
}

mCbxChainFilters->setChecked( config["ChainFilters"].toBool() );
}
}

@@ -134,6 +136,8 @@ QgsEditorWidgetConfig QgsRelationReferenceConfigDlg::config()
filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
}
myConfig.insert( "FilterFields", filterFields );

myConfig.insert( "ChainFilters", mCbxChainFilters->isChecked() );
}

if ( mReferencedLayer )
@@ -59,6 +59,8 @@ QgsEditorWidgetConfig QgsRelationReferenceFactory::readConfig( const QDomElement
filterFields << fieldElement.attribute( "name" );
}
cfg.insert( "FilterFields", filterFields );

cfg.insert( "ChainFilters", filterNode.toElement().attribute( "ChainFilters" ) == "1" );
}
return cfg;
}
@@ -87,5 +89,7 @@ void QgsRelationReferenceFactory::writeConfig( const QgsEditorWidgetConfig& conf
filterFields.appendChild( fieldElem );
}
configElement.appendChild( filterFields );

filterFields.setAttribute( "ChainFilters", config["ChainFilters"].toBool() );
}
}

0 comments on commit 733b7c9

Please sign in to comment.
You can’t perform that action at this time.