Skip to content

Commit ec49341

Browse files
committedNov 16, 2016
Allow showing CRS in QgsMapLayerComboBox
1 parent 959f97f commit ec49341

File tree

7 files changed

+126
-11
lines changed

7 files changed

+126
-11
lines changed
 

‎python/core/qgsmaplayermodel.sip

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ class QgsMapLayerModel : QAbstractItemModel
5454
*/
5555
bool allowEmptyLayer() const;
5656

57+
/**
58+
* Sets whether the CRS of layers is also included in the model's display role.
59+
* @see showCrs()
60+
* @note added in QGIS 3.0
61+
*/
62+
void setShowCrs( bool showCrs );
63+
64+
/**
65+
* Returns true if the model includes layer's CRS in the display role.
66+
* @see setShowCrs()
67+
* @note added in QGIS 3.0
68+
*/
69+
bool showCrs() const;
70+
5771
/**
5872
* @brief layersChecked returns the list of layers which are checked (or unchecked)
5973
*/
@@ -75,15 +89,15 @@ class QgsMapLayerModel : QAbstractItemModel
7589
public:
7690
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
7791
QModelIndex parent( const QModelIndex &child ) const;
78-
int rowCount( const QModelIndex &parent ) const;
79-
int columnCount( const QModelIndex &parent ) const;
80-
QVariant data( const QModelIndex &index, int role ) const;
92+
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
93+
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
94+
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
8195

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

87-
bool setData( const QModelIndex &index, const QVariant &value, int role );
101+
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole );
88102
Qt::ItemFlags flags( const QModelIndex &index ) const;
89103
};

‎python/gui/qgsmaplayercombobox.sip

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ class QgsMapLayerComboBox : QComboBox
4242
*/
4343
bool allowEmptyLayer() const;
4444

45+
/**
46+
* Sets whether the CRS of layers is also included in the combo box text.
47+
* @see showCrs()
48+
* @note added in QGIS 3.0
49+
*/
50+
void setShowCrs( bool showCrs );
51+
52+
/**
53+
* Returns true if the combo box shows the layer's CRS.
54+
* @see setShowCrs()
55+
* @note added in QGIS 3.0
56+
*/
57+
bool showCrs() const;
58+
4559
/** Returns the current layer selected in the combo box.
4660
* @see layer
4761
*/

‎src/core/qgsmaplayermodel.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ QgsMapLayerModel::QgsMapLayerModel( const QList<QgsMapLayer *>& layers, QObject
2727
, mLayersChecked( QMap<QString, Qt::CheckState>() )
2828
, mItemCheckable( false )
2929
, mAllowEmpty( false )
30+
, mShowCrs( false )
3031
{
3132
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
3233
addLayers( layers );
@@ -37,6 +38,7 @@ QgsMapLayerModel::QgsMapLayerModel( QObject *parent )
3738
, mLayersChecked( QMap<QString, Qt::CheckState>() )
3839
, mItemCheckable( false )
3940
, mAllowEmpty( false )
41+
, mShowCrs( false )
4042
{
4143
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( addLayers( QList<QgsMapLayer*> ) ) );
4244
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
@@ -54,7 +56,7 @@ void QgsMapLayerModel::checkAll( Qt::CheckState checkState )
5456
{
5557
mLayersChecked[key] = checkState;
5658
}
57-
emit dataChanged( index( 0, 0 ), index( mLayers.length() - 1, 0 ) );
59+
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
5860
}
5961

6062
void QgsMapLayerModel::setAllowEmptyLayer( bool allowEmpty )
@@ -76,6 +78,15 @@ void QgsMapLayerModel::setAllowEmptyLayer( bool allowEmpty )
7678
}
7779
}
7880

81+
void QgsMapLayerModel::setShowCrs( bool showCrs )
82+
{
83+
if ( mShowCrs == showCrs )
84+
return;
85+
86+
mShowCrs = showCrs;
87+
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ), QVector<int>() << Qt::DisplayRole );
88+
}
89+
7990
QList<QgsMapLayer *> QgsMapLayerModel::layersChecked( Qt::CheckState checkState )
8091
{
8192
QList<QgsMapLayer *> layers;
@@ -183,7 +194,17 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
183194
return QVariant();
184195

185196
QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
186-
return layer ? layer->name() : QVariant();
197+
if ( !layer )
198+
return QVariant();
199+
200+
if ( !mShowCrs )
201+
{
202+
return layer->name();
203+
}
204+
else
205+
{
206+
return tr( "%1 [%2]" ).arg( layer->name(), layer->crs().authid() );
207+
}
187208
}
188209

189210
if ( role == LayerIdRole )

‎src/core/qgsmaplayermodel.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class QgsMapLayer;
3232
class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
3333
{
3434
Q_OBJECT
35+
36+
Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
37+
Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
38+
Q_PROPERTY( bool itemsCheckable READ itemsCheckable WRITE setItemsCheckable )
39+
3540
public:
3641

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

84+
/**
85+
* Sets whether the CRS of layers is also included in the model's display role.
86+
* @see showCrs()
87+
* @note added in QGIS 3.0
88+
*/
89+
void setShowCrs( bool showCrs );
90+
91+
/**
92+
* Returns true if the model includes layer's CRS in the display role.
93+
* @see setShowCrs()
94+
* @note added in QGIS 3.0
95+
*/
96+
bool showCrs() const { return mShowCrs; }
97+
7998
/**
8099
* @brief layersChecked returns the list of layers which are checked (or unchecked)
81100
*/
@@ -101,9 +120,9 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
101120
public:
102121
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
103122
QModelIndex parent( const QModelIndex &child ) const override;
104-
int rowCount( const QModelIndex &parent ) const override;
105-
int columnCount( const QModelIndex &parent ) const override;
106-
QVariant data( const QModelIndex &index, int role ) const override;
123+
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
124+
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
125+
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
107126

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

115-
bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
134+
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
116135
Qt::ItemFlags flags( const QModelIndex &index ) const override;
117136

118137
private:
119138

120139
bool mAllowEmpty;
140+
bool mShowCrs;
121141
};
122142

123143
#endif // QGSMAPLAYERMODEL_H

‎src/gui/qgsmaplayercombobox.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ bool QgsMapLayerComboBox::allowEmptyLayer() const
3838
return mProxyModel->sourceLayerModel()->allowEmptyLayer();
3939
}
4040

41+
void QgsMapLayerComboBox::setShowCrs( bool showCrs )
42+
{
43+
mProxyModel->sourceLayerModel()->setShowCrs( showCrs );
44+
}
45+
46+
bool QgsMapLayerComboBox::showCrs() const
47+
{
48+
return mProxyModel->sourceLayerModel()->showCrs();
49+
}
50+
4151
void QgsMapLayerComboBox::setLayer( QgsMapLayer *layer )
4252
{
4353
if ( !layer )

‎src/gui/qgsmaplayercombobox.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
3232
Q_OBJECT
3333
Q_FLAGS( QgsMapLayerProxyModel::Filters )
3434
Q_PROPERTY( QgsMapLayerProxyModel::Filters filters READ filters WRITE setFilters )
35+
Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
36+
Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
3537

3638
public:
3739

@@ -67,6 +69,20 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
6769
*/
6870
bool allowEmptyLayer() const;
6971

72+
/**
73+
* Sets whether the CRS of layers is also included in the combo box text.
74+
* @see showCrs()
75+
* @note added in QGIS 3.0
76+
*/
77+
void setShowCrs( bool showCrs );
78+
79+
/**
80+
* Returns true if the combo box shows the layer's CRS.
81+
* @see setShowCrs()
82+
* @note added in QGIS 3.0
83+
*/
84+
bool showCrs() const;
85+
7086
/** Returns the current layer selected in the combo box.
7187
* @see layer
7288
*/

‎tests/src/python/test_qgsmaplayermodel.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
def create_layer(name):
26-
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
26+
layer = QgsVectorLayer("Point?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
2727
name, "memory")
2828
return layer
2929

@@ -44,6 +44,11 @@ def testGettersSetters(self):
4444
m.setAllowEmptyLayer(False)
4545
self.assertFalse(m.allowEmptyLayer())
4646

47+
m.setShowCrs(True)
48+
self.assertTrue(m.showCrs())
49+
m.setShowCrs(False)
50+
self.assertFalse(m.showCrs())
51+
4752
def testAddingRemovingLayers(self):
4853
# test model handles layer addition and removal
4954
m = QgsMapLayerModel()
@@ -146,6 +151,21 @@ def testDisplayRole(self):
146151

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

154+
def testDisplayRoleShowCrs(self):
155+
l1 = create_layer('l1')
156+
l2 = create_layer('l2')
157+
QgsMapLayerRegistry.instance().addMapLayers([l1, l2])
158+
m = QgsMapLayerModel()
159+
m.setShowCrs(True)
160+
self.assertEqual(m.data(m.index(0, 0), Qt.DisplayRole), 'l1 [EPSG:3111]')
161+
self.assertEqual(m.data(m.index(1, 0), Qt.DisplayRole), 'l2 [EPSG:3111]')
162+
m.setAllowEmptyLayer(True)
163+
self.assertFalse(m.data(m.index(0, 0), Qt.DisplayRole))
164+
self.assertEqual(m.data(m.index(1, 0), Qt.DisplayRole), 'l1 [EPSG:3111]')
165+
self.assertEqual(m.data(m.index(2, 0), Qt.DisplayRole), 'l2 [EPSG:3111]')
166+
167+
QgsMapLayerRegistry.instance().removeMapLayers([l1.id(), l2.id()])
168+
149169
def testLayerIdRole(self):
150170
l1 = create_layer('l1')
151171
l2 = create_layer('l2')

0 commit comments

Comments
 (0)
Please sign in to comment.