34 QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyleModel *sourceModel, QObject *parent )
40 QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyle *style, QObject *parent )
46 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
54 Qt::ItemFlags QgsReadOnlyStyleModel::flags(
const QModelIndex &index )
const
56 return QgsStyleProxyModel::flags( index ) & ~Qt::ItemIsEditable;
59 QVariant QgsReadOnlyStyleModel::data(
const QModelIndex &index,
int role )
const
61 if ( role == Qt::FontRole )
64 QFont f = QgsStyleProxyModel::data( index, role ).value< QFont >();
69 return QgsStyleProxyModel::data( index, role );
77 QgsStyleModelDelegate::QgsStyleModelDelegate( QObject *parent )
78 : QStyledItemDelegate( parent )
83 QSize QgsStyleModelDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
85 if (
const QListView *view = qobject_cast< const QListView * >( option.widget ) )
90 QFont f = option.font;
91 f.setPointSizeF( f.pointSizeF() * 1.4 );
92 const QFontMetrics fm( f );
93 return QSize( option.widget->width() - view->verticalScrollBar()->width() * 2, fm.height() );
99 const QSize
iconSize = option.decorationSize;
100 return QSize(
static_cast< int >(
iconSize.width() * 1.4 ),
static_cast< int >(
iconSize.height() * 1.7 ) );
103 else if ( qobject_cast< const QTreeView * >( option.widget ) )
107 QSize defaultSize = QStyledItemDelegate::sizeHint( option, index );
109 return QSize( defaultSize.width(),
static_cast< int >( defaultSize.height() * 1.2 ) );
113 return QStyledItemDelegate::sizeHint( option, index );
116 void QgsStyleModelDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
120 QStyleOptionViewItem titleOption( option );
121 initStyleOption( &titleOption, index );
122 if ( qobject_cast< const QListView * >( option.widget ) )
124 titleOption.font.setBold(
true );
125 titleOption.font.setPointSizeF( titleOption.font.pointSizeF() * 1.4 );
128 painter->setBrush( titleOption.palette.windowText() );
129 painter->setFont( titleOption.font );
130 const QRect rect = QRect( titleOption.rect.left(), titleOption.rect.top(),
131 titleOption.rect.width(), titleOption.rect.height() );
133 painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, index.data( Qt::DisplayRole ).toString() );
134 painter->setBrush( Qt::NoBrush );
135 QColor lineColor = titleOption.palette.windowText().color();
136 lineColor.setAlpha( 100 );
137 painter->setPen( QPen( lineColor, 1 ) );
138 painter->drawLine( titleOption.rect.left(), titleOption.rect.bottom(), titleOption.rect.right(), titleOption.rect.bottom() );
142 else if ( qobject_cast< const QTreeView * >( option.widget ) )
145 QColor lineColor = option.palette.windowText().color();
146 lineColor.setAlpha( 100 );
147 painter->setPen( QPen( lineColor, 1 ) );
149 QFont f = option.font;
152 titleOption.font = f;
153 titleOption.fontMetrics = QFontMetrics( titleOption.font );
155 painter->drawLine( index.column() == 0 ? 0 : option.rect.left(),
156 option.rect.bottom(),
157 index.column() == 0 ? option.rect.right() : option.widget->width(),
158 option.rect.bottom() );
161 titleOption.state |= QStyle::State_Enabled;
162 QStyledItemDelegate::paint( painter, titleOption, index );
167 QStyledItemDelegate::paint( painter, option, index );
184 mDelegate =
new QgsStyleModelDelegate(
this );
187 btnAdvanced->setMenu(
new QMenu(
this ) );
190 viewSymbols->setIconSize( QSize(
static_cast< int >(
iconSize ),
static_cast< int >(
iconSize * 0.9 ) ) );
193 mSymbolTreeView->setIconSize( QSize(
static_cast< int >( treeIconSize ),
static_cast< int >( treeIconSize ) ) );
194 mSymbolTreeView->setMinimumHeight( mSymbolTreeView->fontMetrics().height() * 6 );
196 viewSymbols->setItemDelegate( mDelegate );
197 mSymbolTreeView->setItemDelegate( mDelegate );
199 viewSymbols->setSelectionBehavior( QAbstractItemView::SelectRows );
200 mSymbolTreeView->setSelectionMode( viewSymbols->selectionMode() );
202 connect( openStyleManagerButton, &QToolButton::clicked,
this, &QgsStyleItemsListWidget::openStyleManager );
204 lblSymbolName->clear();
206 connect( mButtonIconView, &QToolButton::toggled,
this, [ = ](
bool active )
210 mSymbolViewStackedWidget->setCurrentIndex( 0 );
215 connect( mButtonListView, &QToolButton::toggled,
this, [ = ](
bool active )
220 mSymbolViewStackedWidget->setCurrentIndex( 1 );
226 const int currentView = settings.
value( QStringLiteral(
"UI/symbolsList/lastIconView" ), 0,
QgsSettings::Gui ).toInt();
227 if ( currentView == 0 )
228 mButtonIconView->setChecked(
true );
230 mButtonListView->setChecked(
true );
232 mSymbolTreeView->header()->restoreState( settings.value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
233 connect( mSymbolTreeView->header(), &QHeaderView::sectionResized,
this, [
this]
236 QgsSettings().setValue( QStringLiteral(
"UI/symbolsList/treeState" ), mSymbolTreeView->header()->saveState(), QgsSettings::Gui );
242 groupEdit->setPlaceholderText( tr(
"Filter symbols…" ) );
243 groupsCombo->setLineEdit( groupEdit );
252 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
254 :
new QgsReadOnlyStyleModel( mStyle,
this );
257 :
new QgsReadOnlyStyleModel( mStyle,
this );
263 viewSymbols->setTextElideMode( Qt::TextElideMode::ElideRight );
265 viewSymbols->setModel( mModel );
266 mSymbolTreeView->setModel( mModel );
270 mSymbolTreeView->setSelectionModel( viewSymbols->selectionModel() );
271 connect( viewSymbols->selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsStyleItemsListWidget::onSelectionChanged );
274 connect( groupsCombo,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsStyleItemsListWidget::groupsCombo_currentIndexChanged );
275 connect( groupsCombo, &QComboBox::currentTextChanged,
this, &QgsStyleItemsListWidget::updateModelFilters );
278 mSymbolTreeView->header()->restoreState( settings.
value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
285 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
289 btnSaveSymbol->setText( tr(
"Save Symbol…" ) );
290 btnSaveSymbol->setToolTip( tr(
"Save symbol to styles" ) );
292 groupsCombo->setItemText( allGroup, tr(
"All Symbols" ) );
296 btnSaveSymbol->setText( tr(
"Save Color Ramp…" ) );
297 btnSaveSymbol->setToolTip( tr(
"Save color ramp to styles" ) );
299 groupsCombo->setItemText( allGroup, tr(
"All Color Ramps" ) );
303 btnSaveSymbol->setText( tr(
"Save Format…" ) );
304 btnSaveSymbol->setToolTip( tr(
"Save text format to styles" ) );
306 groupsCombo->setItemText( allGroup, tr(
"All Text Formats" ) );
310 btnSaveSymbol->setText( tr(
"Save Label Settings…" ) );
311 btnSaveSymbol->setToolTip( tr(
"Save label settings to styles" ) );
313 groupsCombo->setItemText( allGroup, tr(
"All Label Settings" ) );
317 btnSaveSymbol->setText( tr(
"Save Legend Patch Shape…" ) );
318 btnSaveSymbol->setToolTip( tr(
"Save legend patch shape to styles" ) );
320 groupsCombo->setItemText( allGroup, tr(
"All Legend Patch Shapes" ) );
324 btnSaveSymbol->setText( tr(
"Save 3D Symbol…" ) );
325 btnSaveSymbol->setToolTip( tr(
"Save 3D symbol to styles" ) );
327 groupsCombo->setItemText( allGroup, tr(
"All 3D Symbols" ) );
342 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
345 btnSaveSymbol->setText( tr(
"Save Settings…" ) );
346 btnSaveSymbol->setToolTip( tr(
"Save label settings or text format to styles" ) );
348 groupsCombo->setItemText( allGroup, tr(
"All Settings" ) );
365 return groupsCombo->currentData().toString() == QLatin1String(
"tag" ) ? groupsCombo->currentText() : QString();
370 return btnAdvanced->menu();
378 btnAdvanced->setMenu( menu );
384 btnAdvanced->setVisible( enabled );
389 const QItemSelection selection = viewSymbols->selectionModel()->selection();
390 if ( selection.isEmpty() )
393 const QModelIndex index = selection.at( 0 ).topLeft();
400 const QItemSelection selection = viewSymbols->selectionModel()->selection();
401 if ( selection.isEmpty() )
404 const QModelIndex index = selection.at( 0 ).topLeft();
415 QWidget::showEvent( event );
417 mSymbolTreeView->header()->restoreState( settings.
value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
420 void QgsStyleItemsListWidget::populateGroups()
425 mUpdatingGroups =
true;
426 groupsCombo->blockSignals(
true );
427 groupsCombo->clear();
429 groupsCombo->addItem( tr(
"Favorites" ), QVariant(
"favorite" ) );
431 QString allText = tr(
"All Symbols" );
437 allText = tr(
"All Symbols" );
441 allText = tr(
"All Color Ramps" );
445 allText = tr(
"All Text Formats" );
449 allText = tr(
"All Label Settings" );
453 allText = tr(
"All Legend Patch Shapes" );
457 allText = tr(
"All 3D Symbols" );
466 groupsCombo->addItem( allText, QVariant(
"all" ) );
469 QStringList tags = mStyle->
tags();
470 if ( tags.count() > 0 )
473 groupsCombo->insertSeparator( index );
474 const auto constTags = tags;
475 for (
const QString &tag : constTags )
477 groupsCombo->addItem( tag, QVariant(
"tag" ) );
483 if ( groups.count() > 0 )
486 groupsCombo->insertSeparator( index + 1 );
487 const auto constGroups = groups;
488 for (
const QString &group : constGroups )
490 groupsCombo->addItem( group, QVariant(
"smartgroup" ) );
493 groupsCombo->blockSignals(
false );
496 index = settings.
value( QStringLiteral(
"qgis/symbolsListGroupsIndex" ), 0 ).toInt();
497 groupsCombo->setCurrentIndex( index );
499 mUpdatingGroups =
false;
501 updateModelFilters();
504 void QgsStyleItemsListWidget::updateModelFilters()
506 if ( mUpdatingGroups || !mModel )
509 const QString text = groupsCombo->currentText();
510 const bool isFreeText = text != groupsCombo->itemText( groupsCombo->currentIndex() );
519 else if ( groupsCombo->currentData().toString() == QLatin1String(
"favorite" ) )
526 else if ( groupsCombo->currentData().toString() == QLatin1String(
"all" ) )
533 else if ( groupsCombo->currentData().toString() == QLatin1String(
"smartgroup" ) )
549 void QgsStyleItemsListWidget::openStyleManager()
565 updateModelFilters();
569 void QgsStyleItemsListWidget::onSelectionChanged(
const QModelIndex &index )
574 const QString symbolName = mModel->data( mModel->index( index.row(),
QgsStyleModel::Name ) ).toString();
575 lblSymbolName->setText( symbolName );
583 void QgsStyleItemsListWidget::groupsCombo_currentIndexChanged(
int index )
586 settings.
setValue( QStringLiteral(
"qgis/symbolsListGroupsIndex" ), index );