Skip to content

Commit

Permalink
Allow showing CRS in QgsMapLayerComboBox
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 16, 2016
1 parent 959f97f commit ec49341
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 11 deletions.
22 changes: 18 additions & 4 deletions python/core/qgsmaplayermodel.sip
Expand Up @@ -54,6 +54,20 @@ class QgsMapLayerModel : QAbstractItemModel
*/
bool allowEmptyLayer() const;

/**
* Sets whether the CRS of layers is also included in the model's display role.
* @see showCrs()
* @note added in QGIS 3.0
*/
void setShowCrs( bool showCrs );

/**
* Returns true if the model includes layer's CRS in the display role.
* @see setShowCrs()
* @note added in QGIS 3.0
*/
bool showCrs() const;

/**
* @brief layersChecked returns the list of layers which are checked (or unchecked)
*/
Expand All @@ -75,15 +89,15 @@ class QgsMapLayerModel : QAbstractItemModel
public:
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
QModelIndex parent( const QModelIndex &child ) const;
int rowCount( const QModelIndex &parent ) const;
int columnCount( const QModelIndex &parent ) const;
QVariant data( const QModelIndex &index, int role ) const;
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;

/**
* Returns strings for all roles supported by this model.
*/
QHash<int, QByteArray> roleNames() const;

bool setData( const QModelIndex &index, const QVariant &value, int role );
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole );
Qt::ItemFlags flags( const QModelIndex &index ) const;
};
14 changes: 14 additions & 0 deletions python/gui/qgsmaplayercombobox.sip
Expand Up @@ -42,6 +42,20 @@ class QgsMapLayerComboBox : QComboBox
*/
bool allowEmptyLayer() const;

/**
* Sets whether the CRS of layers is also included in the combo box text.
* @see showCrs()
* @note added in QGIS 3.0
*/
void setShowCrs( bool showCrs );

/**
* Returns true if the combo box shows the layer's CRS.
* @see setShowCrs()
* @note added in QGIS 3.0
*/
bool showCrs() const;

/** Returns the current layer selected in the combo box.
* @see layer
*/
Expand Down
25 changes: 23 additions & 2 deletions src/core/qgsmaplayermodel.cpp
Expand Up @@ -27,6 +27,7 @@ QgsMapLayerModel::QgsMapLayerModel( const QList<QgsMapLayer *>& layers, QObject
, mLayersChecked( QMap<QString, Qt::CheckState>() )
, mItemCheckable( false )
, mAllowEmpty( false )
, mShowCrs( false )
{
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
addLayers( layers );
Expand All @@ -37,6 +38,7 @@ QgsMapLayerModel::QgsMapLayerModel( QObject *parent )
, mLayersChecked( QMap<QString, Qt::CheckState>() )
, mItemCheckable( false )
, mAllowEmpty( false )
, mShowCrs( false )
{
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( addLayers( QList<QgsMapLayer*> ) ) );
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
Expand All @@ -54,7 +56,7 @@ void QgsMapLayerModel::checkAll( Qt::CheckState checkState )
{
mLayersChecked[key] = checkState;
}
emit dataChanged( index( 0, 0 ), index( mLayers.length() - 1, 0 ) );
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
}

void QgsMapLayerModel::setAllowEmptyLayer( bool allowEmpty )
Expand All @@ -76,6 +78,15 @@ void QgsMapLayerModel::setAllowEmptyLayer( bool allowEmpty )
}
}

void QgsMapLayerModel::setShowCrs( bool showCrs )
{
if ( mShowCrs == showCrs )
return;

mShowCrs = showCrs;
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ), QVector<int>() << Qt::DisplayRole );
}

QList<QgsMapLayer *> QgsMapLayerModel::layersChecked( Qt::CheckState checkState )
{
QList<QgsMapLayer *> layers;
Expand Down Expand Up @@ -183,7 +194,17 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
return QVariant();

QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
return layer ? layer->name() : QVariant();
if ( !layer )
return QVariant();

if ( !mShowCrs )
{
return layer->name();
}
else
{
return tr( "%1 [%2]" ).arg( layer->name(), layer->crs().authid() );
}
}

if ( role == LayerIdRole )
Expand Down
28 changes: 24 additions & 4 deletions src/core/qgsmaplayermodel.h
Expand Up @@ -32,6 +32,11 @@ class QgsMapLayer;
class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
{
Q_OBJECT

Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
Q_PROPERTY( bool itemsCheckable READ itemsCheckable WRITE setItemsCheckable )

public:

//! Item data roles
Expand Down Expand Up @@ -76,6 +81,20 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
*/
bool allowEmptyLayer() const { return mAllowEmpty; }

/**
* Sets whether the CRS of layers is also included in the model's display role.
* @see showCrs()
* @note added in QGIS 3.0
*/
void setShowCrs( bool showCrs );

/**
* Returns true if the model includes layer's CRS in the display role.
* @see setShowCrs()
* @note added in QGIS 3.0
*/
bool showCrs() const { return mShowCrs; }

/**
* @brief layersChecked returns the list of layers which are checked (or unchecked)
*/
Expand All @@ -101,9 +120,9 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
public:
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
QModelIndex parent( const QModelIndex &child ) const override;
int rowCount( const QModelIndex &parent ) const override;
int columnCount( const QModelIndex &parent ) const override;
QVariant data( const QModelIndex &index, int role ) const override;
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;

/**
* Returns strings for all roles supported by this model.
Expand All @@ -112,12 +131,13 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
*/
QHash<int, QByteArray> roleNames() const override;

bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
Qt::ItemFlags flags( const QModelIndex &index ) const override;

private:

bool mAllowEmpty;
bool mShowCrs;
};

#endif // QGSMAPLAYERMODEL_H
10 changes: 10 additions & 0 deletions src/gui/qgsmaplayercombobox.cpp
Expand Up @@ -38,6 +38,16 @@ bool QgsMapLayerComboBox::allowEmptyLayer() const
return mProxyModel->sourceLayerModel()->allowEmptyLayer();
}

void QgsMapLayerComboBox::setShowCrs( bool showCrs )
{
mProxyModel->sourceLayerModel()->setShowCrs( showCrs );
}

bool QgsMapLayerComboBox::showCrs() const
{
return mProxyModel->sourceLayerModel()->showCrs();
}

void QgsMapLayerComboBox::setLayer( QgsMapLayer *layer )
{
if ( !layer )
Expand Down
16 changes: 16 additions & 0 deletions src/gui/qgsmaplayercombobox.h
Expand Up @@ -32,6 +32,8 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
Q_OBJECT
Q_FLAGS( QgsMapLayerProxyModel::Filters )
Q_PROPERTY( QgsMapLayerProxyModel::Filters filters READ filters WRITE setFilters )
Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )

public:

Expand Down Expand Up @@ -67,6 +69,20 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
*/
bool allowEmptyLayer() const;

/**
* Sets whether the CRS of layers is also included in the combo box text.
* @see showCrs()
* @note added in QGIS 3.0
*/
void setShowCrs( bool showCrs );

/**
* Returns true if the combo box shows the layer's CRS.
* @see setShowCrs()
* @note added in QGIS 3.0
*/
bool showCrs() const;

/** Returns the current layer selected in the combo box.
* @see layer
*/
Expand Down
22 changes: 21 additions & 1 deletion tests/src/python/test_qgsmaplayermodel.py
Expand Up @@ -23,7 +23,7 @@


def create_layer(name):
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
layer = QgsVectorLayer("Point?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
name, "memory")
return layer

Expand All @@ -44,6 +44,11 @@ def testGettersSetters(self):
m.setAllowEmptyLayer(False)
self.assertFalse(m.allowEmptyLayer())

m.setShowCrs(True)
self.assertTrue(m.showCrs())
m.setShowCrs(False)
self.assertFalse(m.showCrs())

def testAddingRemovingLayers(self):
# test model handles layer addition and removal
m = QgsMapLayerModel()
Expand Down Expand Up @@ -146,6 +151,21 @@ def testDisplayRole(self):

QgsMapLayerRegistry.instance().removeMapLayers([l1.id(), l2.id()])

def testDisplayRoleShowCrs(self):
l1 = create_layer('l1')
l2 = create_layer('l2')
QgsMapLayerRegistry.instance().addMapLayers([l1, l2])
m = QgsMapLayerModel()
m.setShowCrs(True)
self.assertEqual(m.data(m.index(0, 0), Qt.DisplayRole), 'l1 [EPSG:3111]')
self.assertEqual(m.data(m.index(1, 0), Qt.DisplayRole), 'l2 [EPSG:3111]')
m.setAllowEmptyLayer(True)
self.assertFalse(m.data(m.index(0, 0), Qt.DisplayRole))
self.assertEqual(m.data(m.index(1, 0), Qt.DisplayRole), 'l1 [EPSG:3111]')
self.assertEqual(m.data(m.index(2, 0), Qt.DisplayRole), 'l2 [EPSG:3111]')

QgsMapLayerRegistry.instance().removeMapLayers([l1.id(), l2.id()])

def testLayerIdRole(self):
l1 = create_layer('l1')
l2 = create_layer('l2')
Expand Down

0 comments on commit ec49341

Please sign in to comment.