From 6529309f48c2c0b4a83173d5947e36cffd1f762d Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Fri, 13 May 2022 14:55:17 +1000 Subject: [PATCH] Fix header data for combined model --- .../auto_generated/symbology/qgscombinedstylemodel.sip.in | 3 +++ src/core/symbology/qgscombinedstylemodel.cpp | 5 +++++ src/core/symbology/qgscombinedstylemodel.h | 3 +++ src/core/symbology/qgsstylemodel.cpp | 5 +++++ src/core/symbology/qgsstylemodel.h | 3 +++ tests/src/python/test_qgscombinedstylemodel.py | 4 +++- 6 files changed, 22 insertions(+), 1 deletion(-) diff --git a/python/core/auto_generated/symbology/qgscombinedstylemodel.sip.in b/python/core/auto_generated/symbology/qgscombinedstylemodel.sip.in index 3cf4883e376..3e5e82d8ac6 100644 --- a/python/core/auto_generated/symbology/qgscombinedstylemodel.sip.in +++ b/python/core/auto_generated/symbology/qgscombinedstylemodel.sip.in @@ -40,6 +40,9 @@ A model which contains entities from multiple :py:class:`QgsStyle` databases. Constructor for QgsCombinedStyleModel with the specified ``parent`` object. %End + virtual QVariant headerData( int section, Qt::Orientation orientation, + int role = Qt::DisplayRole ) const; + void addStyle( QgsStyle *style ); %Docstring Adds a style to the model. diff --git a/src/core/symbology/qgscombinedstylemodel.cpp b/src/core/symbology/qgscombinedstylemodel.cpp index 13315af1e6e..f1d5c6634a0 100644 --- a/src/core/symbology/qgscombinedstylemodel.cpp +++ b/src/core/symbology/qgscombinedstylemodel.cpp @@ -28,6 +28,11 @@ QgsCombinedStyleModel::QgsCombinedStyleModel( QObject *parent ) } +QVariant QgsCombinedStyleModel::headerData( int section, Qt::Orientation orientation, int role ) const +{ + return QgsStyleModel::headerDataStatic( section, orientation, role ); +} + void QgsCombinedStyleModel::addStyle( QgsStyle *style ) { connect( style, &QgsStyle::destroyed, this, [this, style]() diff --git a/src/core/symbology/qgscombinedstylemodel.h b/src/core/symbology/qgscombinedstylemodel.h index 1c0fc906c8e..b26f2bcb9d1 100644 --- a/src/core/symbology/qgscombinedstylemodel.h +++ b/src/core/symbology/qgscombinedstylemodel.h @@ -61,6 +61,9 @@ class CORE_EXPORT QgsCombinedStyleModel: public QConcatenateTablesProxyModel */ explicit QgsCombinedStyleModel( QObject *parent SIP_TRANSFERTHIS = nullptr ); + QVariant headerData( int section, Qt::Orientation orientation, + int role = Qt::DisplayRole ) const override; + /** * Adds a style to the model. * diff --git a/src/core/symbology/qgsstylemodel.cpp b/src/core/symbology/qgsstylemodel.cpp index d7af48d9766..189be32524e 100644 --- a/src/core/symbology/qgsstylemodel.cpp +++ b/src/core/symbology/qgsstylemodel.cpp @@ -511,6 +511,11 @@ Qt::ItemFlags QgsStyleModel::flags( const QModelIndex &index ) const } QVariant QgsStyleModel::headerData( int section, Qt::Orientation orientation, int role ) const +{ + return headerDataStatic( section, orientation, role ); +} + +QVariant QgsStyleModel::headerDataStatic( int section, Qt::Orientation orientation, int role ) { if ( role == Qt::DisplayRole ) { diff --git a/src/core/symbology/qgsstylemodel.h b/src/core/symbology/qgsstylemodel.h index e2cc1737155..cd69ba3e98d 100644 --- a/src/core/symbology/qgsstylemodel.h +++ b/src/core/symbology/qgsstylemodel.h @@ -199,7 +199,10 @@ class CORE_EXPORT QgsStyleModel: public QAbstractItemModel QgsStyle::StyleEntity entityTypeFromRow( int row ) const; int offsetForEntity( QgsStyle::StyleEntity entity ) const; + static QVariant headerDataStatic( int section, Qt::Orientation orientation, + int role = Qt::DisplayRole ); + friend class QgsCombinedStyleModel; }; /** diff --git a/tests/src/python/test_qgscombinedstylemodel.py b/tests/src/python/test_qgscombinedstylemodel.py index fb23e88349d..cc03794a2a9 100644 --- a/tests/src/python/test_qgscombinedstylemodel.py +++ b/tests/src/python/test_qgscombinedstylemodel.py @@ -14,7 +14,7 @@ import os import qgis # NOQA -from qgis.PyQt.QtCore import QCoreApplication, QEvent +from qgis.PyQt.QtCore import QCoreApplication, QEvent, Qt from qgis.core import ( QgsStyle, @@ -47,6 +47,8 @@ class TestQgsCombinedStyleModel(unittest.TestCase): model.addStyle(style1) self.assertEqual(model.styles(), [style1]) + self.assertEqual(model.headerData(0, Qt.Horizontal), 'Name') + self.assertEqual(model.headerData(1, Qt.Horizontal), 'Tags') self.assertEqual(model.columnCount(), 2) self.assertEqual(model.rowCount(), 1)