30
30
#include " qgsnewauxiliarylayerdialog.h"
31
31
#include " qgsauxiliarystorage.h"
32
32
#include " qgssvgcache.h"
33
+ #include " qgsstylemodel.h"
33
34
34
35
#include < QAction>
35
36
#include < QString>
44
45
#include < QPushButton>
45
46
46
47
48
+ //
49
+ // QgsReadOnlyStyleModel
50
+ //
51
+ QgsReadOnlyStyleModel::QgsReadOnlyStyleModel ( QgsStyle *style, QObject *parent )
52
+ : QgsStyleProxyModel( style, parent )
53
+ {
54
+
55
+ }
56
+
57
+ Qt::ItemFlags QgsReadOnlyStyleModel::flags ( const QModelIndex &index ) const
58
+ {
59
+ return QgsStyleProxyModel::flags ( index ) & ~Qt::ItemIsEditable;
60
+ }
61
+
62
+ QVariant QgsReadOnlyStyleModel::data ( const QModelIndex &index, int role ) const
63
+ {
64
+ if ( role == Qt::FontRole )
65
+ {
66
+ // drop font size to get reasonable amount of item name shown
67
+ QFont f = QgsStyleProxyModel::data ( index, role ).value < QFont >();
68
+ f.setPointSize ( 10 );
69
+ return f;
70
+ }
71
+ return QgsStyleProxyModel::data ( index, role );
72
+ }
73
+
74
+
75
+ //
76
+ // QgsSymbolsListWidget
77
+ //
78
+
47
79
QgsSymbolsListWidget::QgsSymbolsListWidget ( QgsSymbol *symbol, QgsStyle *style, QMenu *menu, QWidget *parent, QgsVectorLayer *layer )
48
80
: QWidget( parent )
49
81
, mSymbol( symbol )
@@ -58,6 +90,15 @@ QgsSymbolsListWidget::QgsSymbolsListWidget( QgsSymbol *symbol, QgsStyle *style,
58
90
mSymbolUnitWidget ->setUnits ( QgsUnitTypes::RenderUnitList () << QgsUnitTypes::RenderMillimeters << QgsUnitTypes::RenderMetersInMapUnits << QgsUnitTypes::RenderMapUnits << QgsUnitTypes::RenderPixels
59
91
<< QgsUnitTypes::RenderPoints << QgsUnitTypes::RenderInches );
60
92
93
+ mModel = new QgsReadOnlyStyleModel ( mStyle , this );
94
+ mModel ->setEntityFilterEnabled ( true );
95
+ mModel ->setEntityFilter ( QgsStyle::SymbolEntity );
96
+ if ( mSymbol )
97
+ {
98
+ mModel ->setSymbolTypeFilterEnabled ( true );
99
+ mModel ->setSymbolType ( mSymbol ->type () );
100
+ }
101
+
61
102
btnAdvanced->hide (); // advanced button is hidden by default
62
103
if ( menu ) // show it if there is a menu pointer
63
104
{
@@ -73,11 +114,15 @@ QgsSymbolsListWidget::QgsSymbolsListWidget( QgsSymbol *symbol, QgsStyle *style,
73
114
mClipFeaturesAction ->setCheckable ( true );
74
115
connect ( mClipFeaturesAction , &QAction::toggled, this , &QgsSymbolsListWidget::clipFeaturesToggled );
75
116
76
- QStandardItemModel *model = new QStandardItemModel ( viewSymbols );
77
- viewSymbols->setModel ( model );
117
+ double iconSize = Qgis::UI_SCALE_FACTOR * fontMetrics ().width ( ' X' ) * 10 ;
118
+ viewSymbols->setIconSize ( QSize ( static_cast < int >( iconSize ), static_cast < int >( iconSize * 0.9 ) ) ); // ~100, 90 on low dpi
119
+ viewSymbols->setGridSize ( QSize ( static_cast < int >( iconSize * 1.2 ), static_cast < int >( iconSize * 1.1 ) ) ); // ~120,110 on low dpi
120
+
121
+ mModel ->sourceModel ()->setProperty ( " icon_sizes" , QVariantList () << viewSymbols->iconSize () );
122
+ viewSymbols->setModel ( mModel );
123
+
78
124
connect ( viewSymbols->selectionModel (), &QItemSelectionModel::currentChanged, this , &QgsSymbolsListWidget::setSymbolFromStyle );
79
125
80
- connect ( mStyle , &QgsStyle::symbolSaved, this , &QgsSymbolsListWidget::symbolAddedToStyle );
81
126
connect ( mStyle , &QgsStyle::groupsModified, this , &QgsSymbolsListWidget::populateGroups );
82
127
83
128
connect ( openStyleManagerButton, &QPushButton::pressed, this , &QgsSymbolsListWidget::openStyleManager );
@@ -121,7 +166,7 @@ QgsSymbolsListWidget::QgsSymbolsListWidget( QgsSymbol *symbol, QgsStyle *style,
121
166
// have been generated using the temporary "downloading" svg. In this case
122
167
// we require the preview to be regenerated to use the correct fetched
123
168
// svg
124
- connect ( QgsApplication::svgCache (), &QgsSvgCache::remoteSvgFetched, this , &QgsSymbolsListWidget::populateSymbolView );
169
+ connect ( QgsApplication::svgCache (), &QgsSvgCache::remoteSvgFetched, this , &QgsSymbolsListWidget::updateModelFilters );
125
170
}
126
171
127
172
QgsSymbolsListWidget::~QgsSymbolsListWidget ()
@@ -254,71 +299,36 @@ void QgsSymbolsListWidget::populateGroups()
254
299
index = settings.value ( QStringLiteral ( " qgis/symbolsListGroupsIndex" ), 0 ).toInt ();
255
300
groupsCombo->setCurrentIndex ( index );
256
301
257
- populateSymbolView ();
302
+ updateModelFilters ();
258
303
}
259
304
260
- void QgsSymbolsListWidget::populateSymbolView ()
305
+ void QgsSymbolsListWidget::updateModelFilters ()
261
306
{
262
- QStringList symbols;
263
- QString text = groupsCombo->currentText ();
264
- int id;
307
+ const QString text = groupsCombo->currentText ();
265
308
266
309
if ( groupsCombo->currentData ().toString () == QLatin1String ( " favorite" ) )
267
310
{
268
- symbols = mStyle ->symbolsOfFavorite ( QgsStyle::SymbolEntity );
311
+ mModel ->setFavoritesOnly ( true );
312
+ mModel ->setTagId ( -1 );
313
+ mModel ->setSmartGroupId ( -1 );
269
314
}
270
315
else if ( groupsCombo->currentData ().toString () == QLatin1String ( " all" ) )
271
316
{
272
- symbols = mStyle ->symbolNames ();
317
+ mModel ->setFavoritesOnly ( false );
318
+ mModel ->setTagId ( -1 );
319
+ mModel ->setSmartGroupId ( -1 );
273
320
}
274
321
else if ( groupsCombo->currentData ().toString () == QLatin1String ( " smartgroup" ) )
275
322
{
276
- id = mStyle ->smartgroupId ( text );
277
- symbols = mStyle ->symbolsOfSmartgroup ( QgsStyle::SymbolEntity, id );
323
+ mModel ->setFavoritesOnly ( false );
324
+ mModel ->setTagId ( -1 );
325
+ mModel ->setSmartGroupId ( mStyle ->smartgroupId ( text ) );
278
326
}
279
327
else
280
328
{
281
- id = mStyle ->tagId ( text );
282
- symbols = mStyle ->symbolsWithTag ( QgsStyle::SymbolEntity, id );
283
- }
284
-
285
- symbols.sort ();
286
- populateSymbols ( symbols );
287
- }
288
-
289
- void QgsSymbolsListWidget::populateSymbols ( const QStringList &names )
290
- {
291
- QSize previewSize = viewSymbols->iconSize ();
292
-
293
- QStandardItemModel *model = qobject_cast<QStandardItemModel *>( viewSymbols->model () );
294
- if ( !model )
295
- {
296
- return ;
297
- }
298
- model->clear ();
299
-
300
- for ( int i = 0 ; i < names.count (); i++ )
301
- {
302
- std::unique_ptr< QgsSymbol > s ( mStyle ->symbol ( names[i] ) );
303
- if ( !s || s->type () != mSymbol ->type () )
304
- {
305
- continue ;
306
- }
307
- QStringList tags = mStyle ->tagsOfSymbol ( QgsStyle::SymbolEntity, names[i] );
308
- QStandardItem *item = new QStandardItem ( names[i] );
309
- item->setData ( names[i], Qt::UserRole ); // so we can load symbol with that name
310
- item->setText ( names[i] );
311
- item->setToolTip ( QStringLiteral ( " <b>%1</b><br><i>%2</i>" ).arg ( names[i], tags.count () > 0 ? tags.join ( QStringLiteral ( " , " ) ) : tr ( " Not tagged" ) ) );
312
- item->setFlags ( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
313
- // Set font to 10points to show reasonable text
314
- QFont itemFont = item->font ();
315
- itemFont.setPointSize ( 10 );
316
- item->setFont ( itemFont );
317
- // create preview icon
318
- QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon ( s.get (), previewSize, 15 );
319
- item->setIcon ( icon );
320
- // add to model
321
- model->appendRow ( item );
329
+ mModel ->setFavoritesOnly ( false );
330
+ mModel ->setTagId ( mStyle ->tagId ( text ) );
331
+ mModel ->setSmartGroupId ( -1 );
322
332
}
323
333
}
324
334
@@ -327,7 +337,7 @@ void QgsSymbolsListWidget::openStyleManager()
327
337
QgsStyleManagerDialog dlg ( mStyle , this );
328
338
dlg.exec ();
329
339
330
- populateSymbolView ();
340
+ updateModelFilters (); // probably not needed -- the model should automatically update if any changes were made
331
341
}
332
342
333
343
void QgsSymbolsListWidget::clipFeaturesToggled ( bool checked )
@@ -439,13 +449,6 @@ void QgsSymbolsListWidget::updateAssistantSymbol()
439
449
mWidthDDBtn ->setSymbol ( mAssistantSymbol );
440
450
}
441
451
442
- void QgsSymbolsListWidget::symbolAddedToStyle ( const QString &name, QgsSymbol *symbol )
443
- {
444
- Q_UNUSED ( name );
445
- Q_UNUSED ( symbol );
446
- populateSymbolView ();
447
- }
448
-
449
452
void QgsSymbolsListWidget::addSymbolToStyle ()
450
453
{
451
454
bool ok;
@@ -472,7 +475,6 @@ void QgsSymbolsListWidget::addSymbolToStyle()
472
475
473
476
// make sure the symbol is stored
474
477
mStyle ->saveSymbol ( name, mSymbol ->clone (), false , QStringList () );
475
- populateSymbolView ();
476
478
}
477
479
478
480
void QgsSymbolsListWidget::saveSymbol ()
@@ -636,7 +638,7 @@ void QgsSymbolsListWidget::updateSymbolInfo()
636
638
637
639
void QgsSymbolsListWidget::setSymbolFromStyle ( const QModelIndex &index )
638
640
{
639
- QString symbolName = index. data ( Qt::UserRole ).toString ();
641
+ QString symbolName = mModel -> data ( mModel -> index ( index. row (), QgsStyleModel::Name ) ).toString ();
640
642
lblSymbolName->setText ( symbolName );
641
643
// get new instance of symbol from style
642
644
std::unique_ptr< QgsSymbol > s ( mStyle ->symbol ( symbolName ) );
@@ -663,5 +665,5 @@ void QgsSymbolsListWidget::groupsCombo_currentIndexChanged( int index )
663
665
QgsSettings settings;
664
666
settings.setValue ( QStringLiteral ( " qgis/symbolsListGroupsIndex" ), index );
665
667
666
- populateSymbolView ();
668
+ updateModelFilters ();
667
669
}
0 commit comments