QgsMapLayerModel: allow direct retrieval of map layer

This commit is contained in:
Matthias Kuhn 2016-02-23 15:43:18 +01:00
parent de06cd7612
commit ee46ece2da
5 changed files with 47 additions and 5 deletions

View File

@ -1371,7 +1371,7 @@ INCLUDE_FILE_PATTERNS =
# undefined via #undef or recursively expanded use the := operator
# instead of the = operator.
PREDEFINED =
PREDEFINED = "QT_VERSION=0x040800"
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then
# this tag can be used to specify a list of macro names that should be expanded.

View File

@ -13,7 +13,11 @@ class QgsMapLayerModel : QAbstractItemModel
%End
public:
static const int LayerIdRole;
enum
{
LayerIdRole,
LayerRole
};
/**
* @brief QgsMapLayerModel creates a model to display layers in widgets.
@ -56,6 +60,14 @@ class QgsMapLayerModel : QAbstractItemModel
int rowCount( const QModelIndex &parent ) const;
int columnCount( const QModelIndex &parent ) const;
QVariant data( const QModelIndex &index, int role ) const;
%If (QT5_SUPPORT)
/**
* Returns strings for all roles supported by this model.
*
* @note Available only with Qt5 (python and c++)
*/
QHash<int, QByteArray> roleNames() const;
%End
bool setData( const QModelIndex &index, const QVariant &value, int role );
Qt::ItemFlags flags( const QModelIndex &index ) const;
};

View File

@ -759,4 +759,6 @@ class CORE_EXPORT QgsMapLayer : public QObject
QgsMapLayerStyleManager* mStyleManager;
};
Q_DECLARE_METATYPE( QgsMapLayer* )
#endif

View File

@ -22,8 +22,6 @@
#include "qgsvectorlayer.h"
const int QgsMapLayerModel::LayerIdRole = Qt::UserRole + 1;
QgsMapLayerModel::QgsMapLayerModel( const QList<QgsMapLayer *>& layers, QObject *parent )
: QAbstractItemModel( parent )
, mLayersChecked( QMap<QString, Qt::CheckState>() )
@ -151,6 +149,11 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
return layer->id();
}
if ( role == LayerRole )
{
return QVariant::fromValue<QgsMapLayer*>( static_cast<QgsMapLayer*>( index.internalPointer() ) );
}
if ( role == Qt::CheckStateRole && mItemCheckable )
{
QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
@ -213,6 +216,16 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
return QVariant();
}
#if QT_VERSION >= 0x050000
QHash<int, QByteArray> QgsMapLayerModel::roleNames() const
{
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
roles[LayerIdRole] = "layerId";
roles[LayerRole] = "layer";
return roles;
}
#endif
Qt::ItemFlags QgsMapLayerModel::flags( const QModelIndex &index ) const
{

View File

@ -29,11 +29,16 @@ class QgsMapLayer;
* @see QgsFieldModel to combine in with a field selector.
* @note added in 2.3
*/
// TODO QGIS3: move to core
class GUI_EXPORT QgsMapLayerModel : public QAbstractItemModel
{
Q_OBJECT
public:
static const int LayerIdRole;
enum
{
LayerIdRole = Qt::UserRole + 1,
LayerRole
};
/**
* @brief QgsMapLayerModel creates a model to display layers in widgets.
@ -81,6 +86,16 @@ class GUI_EXPORT QgsMapLayerModel : public QAbstractItemModel
int rowCount( const QModelIndex &parent ) const override;
int columnCount( const QModelIndex &parent ) const override;
QVariant data( const QModelIndex &index, int role ) const override;
///@cond PRIVATE
#if QT_VERSION >= 0x050000
/**
* Returns strings for all roles supported by this model.
*
* @note Available only with Qt5 (python and c++)
*/
QHash<int, QByteArray> roleNames() const override;
#endif
///@endcond
bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
Qt::ItemFlags flags( const QModelIndex &index ) const override;
};