32#include "moc_qgsstyleitemslistwidget.cpp"
34using namespace Qt::StringLiterals;
41QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyleModel *sourceModel, QObject *parent )
45QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyle *style, QObject *parent )
53Qt::ItemFlags QgsReadOnlyStyleModel::flags(
const QModelIndex &index )
const
55 return QgsStyleProxyModel::flags( index ) & ~Qt::ItemIsEditable;
58QVariant QgsReadOnlyStyleModel::data(
const QModelIndex &index,
int role )
const
60 if ( role == Qt::FontRole )
63 QFont f = QgsStyleProxyModel::data( index, role ).value<QFont>();
68 return QgsStyleProxyModel::data( index, role );
76QgsStyleModelDelegate::QgsStyleModelDelegate( QObject *parent )
77 : QStyledItemDelegate( parent )
80QSize QgsStyleModelDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
82 if (
const QListView *view = qobject_cast<const QListView *>( option.widget ) )
87 QFont f = option.font;
88 f.setPointSizeF( f.pointSizeF() * 1.4 );
89 const QFontMetrics fm( f );
90 return QSize( option.widget->width() - view->verticalScrollBar()->width() * 2, fm.height() );
96 const QSize
iconSize = option.decorationSize;
97 return QSize(
static_cast<int>(
iconSize.width() * 1.4 ),
static_cast<int>(
iconSize.height() * 1.7 ) );
100 else if ( qobject_cast<const QTreeView *>( option.widget ) )
104 QSize defaultSize = QStyledItemDelegate::sizeHint( option, index );
106 return QSize( defaultSize.width(),
static_cast<int>( defaultSize.height() * 1.2 ) );
110 return QStyledItemDelegate::sizeHint( option, index );
113void QgsStyleModelDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
117 QStyleOptionViewItem titleOption( option );
118 initStyleOption( &titleOption, index );
119 if ( qobject_cast<const QListView *>( option.widget ) )
121 titleOption.font.setBold(
true );
122 titleOption.font.setPointSizeF( titleOption.font.pointSizeF() * 1.4 );
125 painter->setBrush( titleOption.palette.windowText() );
126 painter->setFont( titleOption.font );
127 const QRect rect = QRect( titleOption.rect.left(), titleOption.rect.top(), titleOption.rect.width(), titleOption.rect.height() );
129 painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, index.data( Qt::DisplayRole ).toString() );
130 painter->setBrush( Qt::NoBrush );
131 QColor lineColor = titleOption.palette.windowText().color();
132 lineColor.setAlpha( 100 );
133 painter->setPen( QPen( lineColor, 1 ) );
134 painter->drawLine( titleOption.rect.left(), titleOption.rect.bottom(), titleOption.rect.right(), titleOption.rect.bottom() );
138 else if ( qobject_cast<const QTreeView *>( option.widget ) )
141 QColor lineColor = option.palette.windowText().color();
142 lineColor.setAlpha( 100 );
143 painter->setPen( QPen( lineColor, 1 ) );
145 QFont f = option.font;
148 titleOption.font = f;
149 titleOption.fontMetrics = QFontMetrics( titleOption.font );
151 painter->drawLine( index.column() == 0 ? 0 : option.rect.left(), option.rect.bottom(), index.column() == 0 ? option.rect.right() : option.widget->width(), option.rect.bottom() );
154 titleOption.state |= QStyle::State_Enabled;
155 QStyledItemDelegate::paint( painter, titleOption, index );
160 QStyledItemDelegate::paint( painter, option, index );
176 mDelegate =
new QgsStyleModelDelegate(
this );
179 btnAdvanced->setMenu(
new QMenu(
this ) );
182 viewSymbols->setIconSize( QSize(
static_cast<int>( iconSize ),
static_cast<int>( iconSize * 0.9 ) ) );
185 mSymbolTreeView->setIconSize( QSize(
static_cast<int>( treeIconSize ),
static_cast<int>( treeIconSize ) ) );
186 mSymbolTreeView->setMinimumHeight( mSymbolTreeView->fontMetrics().height() * 6 );
188 viewSymbols->setItemDelegate( mDelegate );
189 mSymbolTreeView->setItemDelegate( mDelegate );
191 viewSymbols->setSelectionBehavior( QAbstractItemView::SelectRows );
192 mSymbolTreeView->setSelectionMode( viewSymbols->selectionMode() );
194 connect( openStyleManagerButton, &QToolButton::clicked,
this, &QgsStyleItemsListWidget::openStyleManager );
196 lblSymbolName->clear();
198 connect( mButtonIconView, &QToolButton::toggled,
this, [
this](
bool active ) {
201 mSymbolViewStackedWidget->setCurrentIndex( 0 );
206 connect( mButtonListView, &QToolButton::toggled,
this, [
this](
bool active ) {
210 mSymbolViewStackedWidget->setCurrentIndex( 1 );
216 const int currentView = settings.
value( u
"UI/symbolsList/lastIconView"_s, 0,
QgsSettings::Gui ).toInt();
217 if ( currentView == 0 )
218 mButtonIconView->setChecked(
true );
220 mButtonListView->setChecked(
true );
222 mSymbolTreeView->header()->restoreState( settings.value( u
"UI/symbolsList/treeState"_s, QByteArray(),
QgsSettings::Gui ).toByteArray() );
223 connect( mSymbolTreeView->header(), &QHeaderView::sectionResized,
this, [
this] {
225 QgsSettings().setValue( u
"UI/symbolsList/treeState"_s, mSymbolTreeView->header()->saveState(), QgsSettings::Gui );
231 groupEdit->setPlaceholderText( tr(
"Filter symbols…" ) );
232 groupsCombo->setLineEdit( groupEdit );
243 mModel->addDesiredIconSize( viewSymbols->iconSize() );
244 mModel->addDesiredIconSize( mSymbolTreeView->iconSize() );
248 viewSymbols->setTextElideMode( Qt::TextElideMode::ElideRight );
250 viewSymbols->setModel( mModel );
251 mSymbolTreeView->setModel( mModel );
255 mSymbolTreeView->setSelectionModel( viewSymbols->selectionModel() );
256 connect( viewSymbols->selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsStyleItemsListWidget::onSelectionChanged );
257 connect( viewSymbols, &QListView::activated,
this, [
this](
const QModelIndex &index ) { onSelectionChanged( index, QModelIndex() ); } );
258 connect( mSymbolTreeView, &QTreeView::activated,
this, [
this](
const QModelIndex &index ) { onSelectionChanged( index, QModelIndex() ); } );
261 connect( groupsCombo,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsStyleItemsListWidget::groupsCombo_currentIndexChanged );
262 connect( groupsCombo, &QComboBox::currentTextChanged,
this, &QgsStyleItemsListWidget::updateModelFilters );
265 mSymbolTreeView->header()->restoreState( settings.
value( u
"UI/symbolsList/treeState"_s, QByteArray(),
QgsSettings::Gui ).toByteArray() );
270 mModel->setEntityFilterEnabled(
true );
271 mModel->setEntityFilter( type );
272 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
276 btnSaveSymbol->setText( tr(
"Save Symbol…" ) );
277 btnSaveSymbol->setToolTip( tr(
"Save symbol to styles" ) );
279 groupsCombo->setItemText( allGroup, tr(
"All Symbols" ) );
283 btnSaveSymbol->setText( tr(
"Save Color Ramp…" ) );
284 btnSaveSymbol->setToolTip( tr(
"Save color ramp to styles" ) );
286 groupsCombo->setItemText( allGroup, tr(
"All Color Ramps" ) );
290 btnSaveSymbol->setText( tr(
"Save Format…" ) );
291 btnSaveSymbol->setToolTip( tr(
"Save text format to styles" ) );
293 groupsCombo->setItemText( allGroup, tr(
"All Text Formats" ) );
297 btnSaveSymbol->setText( tr(
"Save Label Settings…" ) );
298 btnSaveSymbol->setToolTip( tr(
"Save label settings to styles" ) );
300 groupsCombo->setItemText( allGroup, tr(
"All Label Settings" ) );
304 btnSaveSymbol->setText( tr(
"Save Legend Patch Shape…" ) );
305 btnSaveSymbol->setToolTip( tr(
"Save legend patch shape to styles" ) );
307 groupsCombo->setItemText( allGroup, tr(
"All Legend Patch Shapes" ) );
311 btnSaveSymbol->setText( tr(
"Save 3D Symbol…" ) );
312 btnSaveSymbol->setToolTip( tr(
"Save 3D symbol to styles" ) );
314 groupsCombo->setItemText( allGroup, tr(
"All 3D Symbols" ) );
318 btnSaveSymbol->setText( tr(
"Save Material…" ) );
319 btnSaveSymbol->setToolTip( tr(
"Save material to styles" ) );
321 groupsCombo->setItemText( allGroup, tr(
"All Materials" ) );
332 mModel->setEntityFilterEnabled(
true );
333 mModel->setEntityFilters( filters );
336 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
339 btnSaveSymbol->setText( tr(
"Save Settings…" ) );
340 btnSaveSymbol->setToolTip( tr(
"Save label settings or text format to styles" ) );
342 groupsCombo->setItemText( allGroup, tr(
"All Settings" ) );
346 btnSaveSymbol->setText( tr(
"Save Symbol" ) );
347 btnSaveSymbol->setToolTip( tr(
"Save 3D symbol or material to styles" ) );
349 groupsCombo->setItemText( allGroup, tr(
"All Symbols" ) );
355 mModel->setSymbolTypeFilterEnabled(
true );
356 mModel->setSymbolType( type );
361 mModel->setLayerType( type );
366 return groupsCombo->currentData().toString() ==
"tag"_L1 ? groupsCombo->currentText() : QString();
371 return btnAdvanced->menu();
379 btnAdvanced->setMenu( menu );
385 btnAdvanced->setVisible( enabled );
390 const QItemSelection selection = viewSymbols->selectionModel()->selection();
391 if ( selection.isEmpty() )
394 const QModelIndex index = selection.at( 0 ).topLeft();
401 const QItemSelection selection = viewSymbols->selectionModel()->selection();
402 if ( selection.isEmpty() )
405 const QModelIndex index = selection.at( 0 ).topLeft();
421 QWidget::showEvent( event );
423 mSymbolTreeView->header()->restoreState( settings.
value( u
"UI/symbolsList/treeState"_s, QByteArray(),
QgsSettings::Gui ).toByteArray() );
426void QgsStyleItemsListWidget::populateGroups()
431 mUpdatingGroups =
true;
432 groupsCombo->blockSignals(
true );
433 groupsCombo->clear();
435 groupsCombo->addItem( tr(
"Favorites" ), QVariant(
"favorite" ) );
437 QString allText = tr(
"All Symbols" );
443 allText = tr(
"All Symbols" );
447 allText = tr(
"All Color Ramps" );
451 allText = tr(
"All Text Formats" );
455 allText = tr(
"All Label Settings" );
459 allText = tr(
"All Legend Patch Shapes" );
463 allText = tr(
"All 3D Symbols" );
467 allText = tr(
"All Materials" );
476 groupsCombo->addItem( allText, QVariant(
"all" ) );
479 QStringList tags = mStyle->tags();
480 if ( tags.count() > 0 )
483 groupsCombo->insertSeparator( index );
484 const auto constTags = tags;
485 for (
const QString &tag : constTags )
487 groupsCombo->addItem( tag, QVariant(
"tag" ) );
492 QStringList groups = mStyle->smartgroupNames();
493 if ( groups.count() > 0 )
496 groupsCombo->insertSeparator( index + 1 );
497 const auto constGroups = groups;
498 for (
const QString &group : constGroups )
500 groupsCombo->addItem( group, QVariant(
"smartgroup" ) );
503 groupsCombo->blockSignals(
false );
506 groupsCombo->setCurrentIndex( index );
508 mUpdatingGroups =
false;
510 updateModelFilters();
513void QgsStyleItemsListWidget::updateModelFilters()
515 if ( mUpdatingGroups || !mModel )
518 const QString text = groupsCombo->currentText();
519 const bool isFreeText = text != groupsCombo->itemText( groupsCombo->currentIndex() );
523 mModel->setFavoritesOnly(
false );
524 mModel->setTagString( QString() );
525 mModel->setSmartGroupId( -1 );
526 mModel->setFilterString( groupsCombo->currentText() );
528 else if ( groupsCombo->currentData().toString() ==
"favorite"_L1 )
530 mModel->setFavoritesOnly(
true );
531 mModel->setTagString( QString() );
532 mModel->setSmartGroupId( -1 );
533 mModel->setFilterString( QString() );
535 else if ( groupsCombo->currentData().toString() ==
"all"_L1 )
537 mModel->setFavoritesOnly(
false );
538 mModel->setTagString( QString() );
539 mModel->setSmartGroupId( -1 );
540 mModel->setFilterString( QString() );
542 else if ( groupsCombo->currentData().toString() ==
"smartgroup"_L1 )
544 mModel->setFavoritesOnly(
false );
545 mModel->setTagString( QString() );
546 mModel->setSmartGroupId( mStyle->smartgroupId( text ) );
547 mModel->setFilterString( QString() );
551 mModel->setFavoritesOnly(
false );
552 mModel->setTagString( text );
553 mModel->setSmartGroupId( -1 );
554 mModel->setFilterString( QString() );
558void QgsStyleItemsListWidget::openStyleManager()
569 std::unique_ptr< QgsStyleManagerDialog > dlg;
572 dlg = std::make_unique< QgsStyleManagerDialog >( mStyle,
this );
576 dlg = std::make_unique< QgsStyleManagerDialog >(
this );
580 updateModelFilters();
584void QgsStyleItemsListWidget::onSelectionChanged(
const QModelIndex &index,
const QModelIndex &previous )
589 if ( index.row() == previous.row() )
592 const QString symbolName = mModel->data( mModel->index( index.row(),
QgsStyleModel::Name ) ).toString();
593 lblSymbolName->setText( symbolName );
601void QgsStyleItemsListWidget::groupsCombo_currentIndexChanged(
int index )
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
static const double UI_SCALE_FACTOR
UI scaling factor.
A model which contains entities from multiple QgsStyle databases.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void setShowSearchIcon(bool visible)
Define if a search icon shall be shown on the left of the image when no text is entered.
void setShowClearButton(bool visible)
Sets whether the widget's clear button is visible.
static QgsWindowManagerInterface * windowManager()
Returns the global window manager, if set.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Stores properties relating to a screen.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
static const QgsSettingsEntryInteger * settingsSymbolsListGroupsIndex
Settings entry for symbols list groups index.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
@ IsTitle
True if the index corresponds to a title item.
@ StyleFileName
File name of associated QgsStyle (QgsStyle::fileName()).
@ Type
Style entity type, see QgsStyle::StyleEntity.
A QSortFilterProxyModel subclass for showing filtered symbol and color ramps entries from a QgsStyle ...
QgsStyle::StyleEntity entityFilter() const
Returns the style entity type filter.
bool entityFilterEnabled() const
Returns true if filtering by entity type is enabled.
A database of saved style entities, including symbols, color ramps, text formats and others.
StyleEntity
Enum for Entities involved in a style.
@ LabelSettingsEntity
Label settings.
@ TextFormatEntity
Text formats.
@ SmartgroupEntity
Smart groups.
@ Symbol3DEntity
3D symbol entity
@ ColorrampEntity
Color ramps.
@ LegendPatchShapeEntity
Legend patch shape.
@ MaterialSettingsEntity
Material settings.
void groupsModified()
Emitted every time a tag or smartgroup has been added, removed, or renamed.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.