Skip to content

Commit 9406247

Browse files
committed
Add explicit API call for style model icon sizes
1 parent e6084a6 commit 9406247

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

python/core/auto_generated/symbology/qgsstylemodel.sip.in

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ The ``style`` object must exist for the lifetime of this model.
6363
virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const;
6464

6565

66+
void addDesiredIconSize( QSize size );
67+
%Docstring
68+
Adds an additional icon ``size`` to generate for Qt.DecorationRole data.
69+
70+
This allows style icons to be generated at an icon size which
71+
corresponds exactly to the view's icon size in which this model is used.
72+
%End
73+
6674
};
6775

6876
class QgsStyleProxyModel: QSortFilterProxyModel
@@ -236,6 +244,14 @@ Returns true if the model is showing only favorited entities.
236244
Sets whether the model should show only favorited entities.
237245

238246
.. seealso:: :py:func:`setFavoritesOnly`
247+
%End
248+
249+
void addDesiredIconSize( QSize size );
250+
%Docstring
251+
Adds an additional icon ``size`` to generate for Qt.DecorationRole data.
252+
253+
This allows style icons to be generated at an icon size which
254+
corresponds exactly to the view's icon size in which this model is used.
239255
%End
240256

241257
public slots:

src/core/symbology/qgsstylemodel.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,8 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
7171

7272
case Qt::DecorationRole:
7373
{
74-
// check the model custom property for icon sizes to generate. This is used
75-
// by instances of the model to indicate the required sizes for decorations in all
76-
// views connected to the model, and allows the model to have size responsive icons.
77-
// By using a QObject property we avoid having public GUI/view related API within
78-
// the model, and mostly avoid view related properties contaminating the pure model...
79-
const QVariantList iconSizes = property( "icon_sizes" ).toList();
80-
74+
// Generate icons at all additional sizes specified for the model.
75+
// This allows the model to have size responsive icons.
8176
switch ( index.column() )
8277
{
8378
case Name:
@@ -87,7 +82,7 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
8782
QIcon icon;
8883
icon.addPixmap( QgsSymbolLayerUtils::symbolPreviewPixmap( symbol.get(), QSize( 24, 24 ), 1 ) );
8984

90-
for ( const QVariant &size : iconSizes )
85+
for ( const QVariant &size : mAdditionalSizes )
9186
{
9287
QSize s = size.toSize();
9388
icon.addPixmap( QgsSymbolLayerUtils::symbolPreviewPixmap( symbol.get(), s, static_cast< int >( s.width() * ICON_PADDING_FACTOR ) ) );
@@ -100,7 +95,7 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
10095
std::unique_ptr< QgsColorRamp > ramp( mStyle->colorRamp( name ) );
10196
QIcon icon;
10297
icon.addPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( ramp.get(), QSize( 24, 24 ), 1 ) );
103-
for ( const QVariant &size : iconSizes )
98+
for ( const QVariant &size : mAdditionalSizes )
10499
{
105100
QSize s = size.toSize();
106101
icon.addPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( ramp.get(), s, static_cast< int >( s.width() * ICON_PADDING_FACTOR ) ) );
@@ -237,6 +232,11 @@ int QgsStyleModel::columnCount( const QModelIndex & ) const
237232
return 2;
238233
}
239234

235+
void QgsStyleModel::addDesiredIconSize( QSize size )
236+
{
237+
mAdditionalSizes << size;
238+
}
239+
240240
void QgsStyleModel::onSymbolAdded( const QString &name, QgsSymbol * )
241241
{
242242
const QStringList oldSymbolNames = mSymbolNames;
@@ -466,6 +466,11 @@ void QgsStyleProxyModel::setFavoritesOnly( bool favoritesOnly )
466466
invalidateFilter();
467467
}
468468

469+
void QgsStyleProxyModel::addDesiredIconSize( QSize size )
470+
{
471+
mModel->addDesiredIconSize( size );
472+
}
473+
469474
bool QgsStyleProxyModel::symbolTypeFilterEnabled() const
470475
{
471476
return mSymbolTypeFilterEnabled;

src/core/symbology/qgsstylemodel.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ class CORE_EXPORT QgsStyleModel: public QAbstractItemModel
7575
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
7676
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
7777

78+
/**
79+
* Adds an additional icon \a size to generate for Qt::DecorationRole data.
80+
*
81+
* This allows style icons to be generated at an icon size which
82+
* corresponds exactly to the view's icon size in which this model is used.
83+
*/
84+
void addDesiredIconSize( QSize size );
85+
7886
private slots:
7987

8088
void onSymbolAdded( const QString &name, QgsSymbol *symbol );
@@ -90,6 +98,7 @@ class CORE_EXPORT QgsStyleModel: public QAbstractItemModel
9098
QgsStyle *mStyle = nullptr;
9199
QStringList mSymbolNames;
92100
QStringList mRampNames;
101+
QList< QSize > mAdditionalSizes;
93102

94103
};
95104

@@ -252,6 +261,14 @@ class CORE_EXPORT QgsStyleProxyModel: public QSortFilterProxyModel
252261
*/
253262
void setFavoritesOnly( bool favoritesOnly );
254263

264+
/**
265+
* Adds an additional icon \a size to generate for Qt::DecorationRole data.
266+
*
267+
* This allows style icons to be generated at an icon size which
268+
* corresponds exactly to the view's icon size in which this model is used.
269+
*/
270+
void addDesiredIconSize( QSize size );
271+
255272
public slots:
256273

257274
/**

src/gui/symbology/qgssymbolslistwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ QgsSymbolsListWidget::QgsSymbolsListWidget( QgsSymbol *symbol, QgsStyle *style,
121121
double iconSize = Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 10;
122122
viewSymbols->setIconSize( QSize( static_cast< int >( iconSize ), static_cast< int >( iconSize * 0.9 ) ) ); // ~100, 90 on low dpi
123123

124-
mModel->sourceModel()->setProperty( "icon_sizes", QVariantList() << viewSymbols->iconSize() );
124+
mModel->addDesiredIconSize( viewSymbols->iconSize() );
125125
viewSymbols->setModel( mModel );
126126

127127
connect( viewSymbols->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsSymbolsListWidget::setSymbolFromStyle );

tests/src/python/test_qgsstylemodel.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -728,17 +728,18 @@ def testIconSize(self):
728728
ramp_a = QgsLimitedRandomColorRamp(5)
729729
self.assertTrue(style.addColorRamp('ramp a', ramp_a, True))
730730

731-
model = QgsStyleModel(style)
732-
self.assertEqual(model.rowCount(), 2)
733731
for i in range(2):
732+
model = QgsStyleModel(style)
733+
self.assertEqual(model.rowCount(), 2)
734734
icon = model.data(model.index(i, 0), Qt.DecorationRole)
735735
# by default, only 24x24 icon
736736
self.assertEqual(icon.availableSizes(), [QSize(24, 24)])
737737
self.assertEqual(icon.actualSize(QSize(10, 10)), QSize(10, 10))
738738
self.assertEqual(icon.actualSize(QSize(24, 24)), QSize(24, 24))
739739
self.assertEqual(icon.actualSize(QSize(90, 90)), QSize(24, 24))
740740

741-
model.setProperty('icon_sizes', [QSize(24, 24), QSize(100, 90)])
741+
model.addDesiredIconSize(QSize(24, 24))
742+
model.addDesiredIconSize(QSize(100, 90))
742743
icon = model.data(model.index(i, 0), Qt.DecorationRole)
743744
self.assertEqual(icon.availableSizes(), [QSize(24, 24), QSize(100, 90)])
744745
self.assertEqual(icon.actualSize(QSize(10, 10)), QSize(10, 10))
@@ -747,7 +748,9 @@ def testIconSize(self):
747748
self.assertEqual(icon.actualSize(QSize(90, 90)), QSize(90, 81))
748749
self.assertEqual(icon.actualSize(QSize(125, 125)), QSize(100, 90))
749750

750-
model.setProperty('icon_sizes', [QSize(100, 90), QSize(200, 180)])
751+
model = QgsStyleModel(style)
752+
model.addDesiredIconSize(QSize(100, 90))
753+
model.addDesiredIconSize(QSize(200, 180))
751754
icon = model.data(model.index(i, 0), Qt.DecorationRole)
752755
self.assertEqual(icon.availableSizes(), [QSize(24, 24), QSize(100, 90), QSize(200, 180)])
753756
self.assertEqual(icon.actualSize(QSize(10, 10)), QSize(10, 10))
@@ -756,7 +759,6 @@ def testIconSize(self):
756759
self.assertEqual(icon.actualSize(QSize(90, 90)), QSize(90, 81))
757760
self.assertEqual(icon.actualSize(QSize(125, 125)), QSize(125, 112))
758761
self.assertEqual(icon.actualSize(QSize(225, 225)), QSize(200, 180))
759-
model.setProperty('icon_sizes', None)
760762

761763
def testSetData(self):
762764
"""

0 commit comments

Comments
 (0)