Navigation Menu

Skip to content

Commit

Permalink
QgsMapLayerModel: allow direct retrieval of map layer
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Mar 9, 2016
1 parent de06cd7 commit ee46ece
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmake_templates/Doxyfile.in
Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion python/gui/qgsmaplayermodel.sip
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
};
2 changes: 2 additions & 0 deletions src/core/qgsmaplayer.h
Expand Up @@ -759,4 +759,6 @@ class CORE_EXPORT QgsMapLayer : public QObject
QgsMapLayerStyleManager* mStyleManager;
};

Q_DECLARE_METATYPE( QgsMapLayer* )

#endif
17 changes: 15 additions & 2 deletions src/gui/qgsmaplayermodel.cpp
Expand Up @@ -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>() )
Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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
{
Expand Down
17 changes: 16 additions & 1 deletion src/gui/qgsmaplayermodel.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit ee46ece

Please sign in to comment.