Skip to content

Commit

Permalink
[api] Allow the text and icon for the 'empty layer' option in QgsMapL…
Browse files Browse the repository at this point in the history
…ayerComboBox to

be customised
  • Loading branch information
nyalldawson committed Mar 30, 2021
1 parent f488a6c commit f0aafd2
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 10 deletions.
4 changes: 3 additions & 1 deletion python/core/auto_generated/qgsmaplayermodel.sip.in
Expand Up @@ -79,10 +79,12 @@ Returns ``True`` if items in the model can be reordered via drag and drop.
checkAll changes the checkstate for all the layers
%End

void setAllowEmptyLayer( bool allowEmpty );
void setAllowEmptyLayer( bool allowEmpty, const QString &text = QString(), const QIcon &icon = QIcon() );
%Docstring
Sets whether an optional empty layer ("not set") option is present in the model.

Since QGIS 3.20, the optional ``text`` and ``icon`` arguments allows the text and icon for the empty layer item to be set.

.. seealso:: :py:func:`allowEmptyLayer`

.. versionadded:: 3.0
Expand Down
4 changes: 3 additions & 1 deletion python/gui/auto_generated/qgsmaplayercombobox.sip.in
Expand Up @@ -68,10 +68,12 @@ Returns the list of data providers which are excluded from the combobox.
.. versionadded:: 3.0
%End

void setAllowEmptyLayer( bool allowEmpty );
void setAllowEmptyLayer( bool allowEmpty, const QString &text = QString(), const QIcon &icon = QIcon() );
%Docstring
Sets whether an optional empty layer ("not set") option is shown in the combo box.

Since QGIS 3.20, the optional ``text`` and ``icon`` arguments allows the text and icon for the empty layer item to be set.

.. seealso:: :py:func:`allowEmptyLayer`

.. versionadded:: 3.0
Expand Down
11 changes: 8 additions & 3 deletions src/core/qgsmaplayermodel.cpp
Expand Up @@ -63,8 +63,10 @@ void QgsMapLayerModel::checkAll( Qt::CheckState checkState )
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
}

void QgsMapLayerModel::setAllowEmptyLayer( bool allowEmpty )
void QgsMapLayerModel::setAllowEmptyLayer( bool allowEmpty, const QString &text, const QIcon &icon )
{
mEmptyText = text;
mEmptyIcon = icon;
if ( allowEmpty == mAllowEmpty )
return;

Expand Down Expand Up @@ -252,7 +254,7 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
case Qt::EditRole:
{
if ( index.row() == 0 && mAllowEmpty )
return QVariant();
return mEmptyText;

if ( additionalIndex >= 0 )
return mAdditionalItems.at( additionalIndex );
Expand Down Expand Up @@ -337,7 +339,10 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const

case Qt::DecorationRole:
{
if ( isEmpty || additionalIndex >= 0 )
if ( isEmpty )
return mEmptyIcon.isNull() ? QVariant() : mEmptyIcon;

if ( additionalIndex >= 0 )
return QVariant();

QgsMapLayer *layer = mLayers.value( index.row() - ( mAllowEmpty ? 1 : 0 ) );
Expand Down
8 changes: 7 additions & 1 deletion src/core/qgsmaplayermodel.h
Expand Up @@ -19,6 +19,7 @@
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include <QStringList>
#include <QIcon>

#include "qgis_core.h"
#include "qgis_sip.h"
Expand Down Expand Up @@ -98,10 +99,13 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel

/**
* Sets whether an optional empty layer ("not set") option is present in the model.
*
* Since QGIS 3.20, the optional \a text and \a icon arguments allows the text and icon for the empty layer item to be set.
*
* \see allowEmptyLayer()
* \since QGIS 3.0
*/
void setAllowEmptyLayer( bool allowEmpty );
void setAllowEmptyLayer( bool allowEmpty, const QString &text = QString(), const QIcon &icon = QIcon() );

/**
* Returns TRUE if the model allows the empty layer ("not set") choice.
Expand Down Expand Up @@ -210,6 +214,8 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
private:

bool mAllowEmpty = false;
QString mEmptyText;
QIcon mEmptyIcon;
bool mShowCrs = false;
QStringList mAdditionalItems;
};
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsmaplayercombobox.cpp
Expand Up @@ -44,9 +44,9 @@ QStringList QgsMapLayerComboBox::excludedProviders() const
return mProxyModel->excludedProviders();
}

void QgsMapLayerComboBox::setAllowEmptyLayer( bool allowEmpty )
void QgsMapLayerComboBox::setAllowEmptyLayer( bool allowEmpty, const QString &text, const QIcon &icon )
{
mProxyModel->sourceLayerModel()->setAllowEmptyLayer( allowEmpty );
mProxyModel->sourceLayerModel()->setAllowEmptyLayer( allowEmpty, text, icon );
}

bool QgsMapLayerComboBox::allowEmptyLayer() const
Expand Down
5 changes: 4 additions & 1 deletion src/gui/qgsmaplayercombobox.h
Expand Up @@ -75,10 +75,13 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox

/**
* Sets whether an optional empty layer ("not set") option is shown in the combo box.
*
* Since QGIS 3.20, the optional \a text and \a icon arguments allows the text and icon for the empty layer item to be set.
*
* \see allowEmptyLayer()
* \since QGIS 3.0
*/
void setAllowEmptyLayer( bool allowEmpty );
void setAllowEmptyLayer( bool allowEmpty, const QString &text = QString(), const QIcon &icon = QIcon() );

/**
* Returns TRUE if the combo box allows the empty layer ("not set") choice.
Expand Down
8 changes: 7 additions & 1 deletion tests/src/python/test_qgsmaplayermodel.py
Expand Up @@ -12,7 +12,7 @@

import qgis # NOQA

from qgis.core import QgsVectorLayer, QgsProject, QgsMapLayerModel
from qgis.core import QgsVectorLayer, QgsProject, QgsMapLayerModel, QgsApplication
from qgis.PyQt.QtCore import Qt, QModelIndex

from qgis.testing import start_app, unittest
Expand Down Expand Up @@ -125,6 +125,12 @@ def testAllowEmpty(self):
self.assertEqual(m.data(m.index(2, 0), Qt.DisplayRole), 'l2')
self.assertEqual(m.data(m.index(3, 0), Qt.DisplayRole), 'l3')

self.assertIsNone(m.data(m.index(0, 0), Qt.DecorationRole))
# set icon and text for empty item
m.setAllowEmptyLayer(True, 'empty', QgsApplication.getThemeIcon('/mItemBookmark.svg'))
self.assertEqual(m.data(m.index(0, 0), Qt.DisplayRole), 'empty')
self.assertFalse(m.data(m.index(0, 0), Qt.DecorationRole).isNull())

QgsProject.instance().removeMapLayers([l1.id(), l2.id(), l3.id()])

def testAdditionalItems(self):
Expand Down

0 comments on commit f0aafd2

Please sign in to comment.