Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add filter chaining to the pre-filters of the relation reference wdg
  • Loading branch information
m-kuhn committed Mar 26, 2015
1 parent 868eeb2 commit 733b7c9
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 84 deletions.
35 changes: 26 additions & 9 deletions src/gui/attributetable/qgsattributetablefiltermodel.h
Expand Up @@ -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 );

/**
Expand All @@ -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();

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

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

/**
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
47 changes: 43 additions & 4 deletions src/gui/attributetable/qgsfeaturelistmodel.cpp
Expand Up @@ -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( "" );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 );
}

Expand All @@ -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 )
Expand Down
15 changes: 15 additions & 0 deletions src/gui/attributetable/qgsfeaturelistmodel.h
Expand Up @@ -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();

/**
Expand Down Expand Up @@ -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 )
Expand Down
4 changes: 4 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferenceconfigdlg.cpp
Expand Up @@ -80,6 +80,8 @@ void QgsRelationReferenceConfigDlg::setConfig( const QMap<QString, QVariant>& co
{
addFilterField( fld );
}

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

Expand Down Expand Up @@ -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 )
Expand Down
4 changes: 4 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencefactory.cpp
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.