29#include "moc_qgsstyleitemslistwidget.cpp"
36QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyleModel *sourceModel, QObject *parent )
41QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyle *style, QObject *parent )
51Qt::ItemFlags QgsReadOnlyStyleModel::flags(
const QModelIndex &index )
const
53 return QgsStyleProxyModel::flags( index ) & ~Qt::ItemIsEditable;
56QVariant QgsReadOnlyStyleModel::data(
const QModelIndex &index,
int role )
const
58 if ( role == Qt::FontRole )
61 QFont f = QgsStyleProxyModel::data( index, role ).value<QFont>();
66 return QgsStyleProxyModel::data( index, role );
74QgsStyleModelDelegate::QgsStyleModelDelegate( QObject *parent )
75 : QStyledItemDelegate( parent )
79QSize QgsStyleModelDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
81 if (
const QListView *view = qobject_cast<const QListView *>( option.widget ) )
86 QFont f = option.font;
87 f.setPointSizeF( f.pointSizeF() * 1.4 );
88 const QFontMetrics fm( f );
89 return QSize( option.widget->width() - view->verticalScrollBar()->width() * 2, fm.height() );
95 const QSize
iconSize = option.decorationSize;
96 return QSize(
static_cast<int>(
iconSize.width() * 1.4 ),
static_cast<int>(
iconSize.height() * 1.7 ) );
99 else if ( qobject_cast<const QTreeView *>( option.widget ) )
103 QSize defaultSize = QStyledItemDelegate::sizeHint( option, index );
105 return QSize( defaultSize.width(),
static_cast<int>( defaultSize.height() * 1.2 ) );
109 return QStyledItemDelegate::sizeHint( option, index );
112void QgsStyleModelDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
116 QStyleOptionViewItem titleOption( option );
117 initStyleOption( &titleOption, index );
118 if ( qobject_cast<const QListView *>( option.widget ) )
120 titleOption.font.setBold(
true );
121 titleOption.font.setPointSizeF( titleOption.font.pointSizeF() * 1.4 );
124 painter->setBrush( titleOption.palette.windowText() );
125 painter->setFont( titleOption.font );
126 const QRect rect = QRect( titleOption.rect.left(), titleOption.rect.top(), titleOption.rect.width(), titleOption.rect.height() );
128 painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, index.data( Qt::DisplayRole ).toString() );
129 painter->setBrush( Qt::NoBrush );
130 QColor lineColor = titleOption.palette.windowText().color();
131 lineColor.setAlpha( 100 );
132 painter->setPen( QPen( lineColor, 1 ) );
133 painter->drawLine( titleOption.rect.left(), titleOption.rect.bottom(), titleOption.rect.right(), titleOption.rect.bottom() );
137 else if ( qobject_cast<const QTreeView *>( option.widget ) )
140 QColor lineColor = option.palette.windowText().color();
141 lineColor.setAlpha( 100 );
142 painter->setPen( QPen( lineColor, 1 ) );
144 QFont f = option.font;
147 titleOption.font = f;
148 titleOption.fontMetrics = QFontMetrics( titleOption.font );
150 painter->drawLine( index.column() == 0 ? 0 : option.rect.left(), option.rect.bottom(), index.column() == 0 ? option.rect.right() : option.widget->width(), option.rect.bottom() );
153 titleOption.state |= QStyle::State_Enabled;
154 QStyledItemDelegate::paint( painter, titleOption, index );
159 QStyledItemDelegate::paint( painter, option, index );
175 mDelegate =
new QgsStyleModelDelegate(
this );
178 btnAdvanced->setMenu(
new QMenu(
this ) );
181 viewSymbols->setIconSize( QSize(
static_cast<int>( iconSize ),
static_cast<int>( iconSize * 0.9 ) ) );
184 mSymbolTreeView->setIconSize( QSize(
static_cast<int>( treeIconSize ),
static_cast<int>( treeIconSize ) ) );
185 mSymbolTreeView->setMinimumHeight( mSymbolTreeView->fontMetrics().height() * 6 );
187 viewSymbols->setItemDelegate( mDelegate );
188 mSymbolTreeView->setItemDelegate( mDelegate );
190 viewSymbols->setSelectionBehavior( QAbstractItemView::SelectRows );
191 mSymbolTreeView->setSelectionMode( viewSymbols->selectionMode() );
193 connect( openStyleManagerButton, &QToolButton::clicked,
this, &QgsStyleItemsListWidget::openStyleManager );
195 lblSymbolName->clear();
197 connect( mButtonIconView, &QToolButton::toggled,
this, [
this](
bool active ) {
200 mSymbolViewStackedWidget->setCurrentIndex( 0 );
205 connect( mButtonListView, &QToolButton::toggled,
this, [
this](
bool active ) {
209 mSymbolViewStackedWidget->setCurrentIndex( 1 );
215 const int currentView = settings.
value( QStringLiteral(
"UI/symbolsList/lastIconView" ), 0,
QgsSettings::Gui ).toInt();
216 if ( currentView == 0 )
217 mButtonIconView->setChecked(
true );
219 mButtonListView->setChecked(
true );
221 mSymbolTreeView->header()->restoreState( settings.value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
222 connect( mSymbolTreeView->header(), &QHeaderView::sectionResized,
this, [
this] {
224 QgsSettings().setValue( QStringLiteral(
"UI/symbolsList/treeState" ), mSymbolTreeView->header()->saveState(), QgsSettings::Gui );
230 groupEdit->setPlaceholderText( tr(
"Filter symbols…" ) );
231 groupsCombo->setLineEdit( groupEdit );
241 :
new QgsReadOnlyStyleModel( mStyle,
this );
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 ) {
258 onSelectionChanged( index, QModelIndex() );
260 connect( mSymbolTreeView, &QTreeView::activated,
this, [
this](
const QModelIndex &index ) {
261 onSelectionChanged( index, QModelIndex() );
265 connect( groupsCombo,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsStyleItemsListWidget::groupsCombo_currentIndexChanged );
266 connect( groupsCombo, &QComboBox::currentTextChanged,
this, &QgsStyleItemsListWidget::updateModelFilters );
269 mSymbolTreeView->header()->restoreState( settings.
value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
274 mModel->setEntityFilterEnabled(
true );
275 mModel->setEntityFilter( type );
276 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
280 btnSaveSymbol->setText( tr(
"Save Symbol…" ) );
281 btnSaveSymbol->setToolTip( tr(
"Save symbol to styles" ) );
283 groupsCombo->setItemText( allGroup, tr(
"All Symbols" ) );
287 btnSaveSymbol->setText( tr(
"Save Color Ramp…" ) );
288 btnSaveSymbol->setToolTip( tr(
"Save color ramp to styles" ) );
290 groupsCombo->setItemText( allGroup, tr(
"All Color Ramps" ) );
294 btnSaveSymbol->setText( tr(
"Save Format…" ) );
295 btnSaveSymbol->setToolTip( tr(
"Save text format to styles" ) );
297 groupsCombo->setItemText( allGroup, tr(
"All Text Formats" ) );
301 btnSaveSymbol->setText( tr(
"Save Label Settings…" ) );
302 btnSaveSymbol->setToolTip( tr(
"Save label settings to styles" ) );
304 groupsCombo->setItemText( allGroup, tr(
"All Label Settings" ) );
308 btnSaveSymbol->setText( tr(
"Save Legend Patch Shape…" ) );
309 btnSaveSymbol->setToolTip( tr(
"Save legend patch shape to styles" ) );
311 groupsCombo->setItemText( allGroup, tr(
"All Legend Patch Shapes" ) );
315 btnSaveSymbol->setText( tr(
"Save 3D Symbol…" ) );
316 btnSaveSymbol->setToolTip( tr(
"Save 3D symbol to styles" ) );
318 groupsCombo->setItemText( allGroup, tr(
"All 3D Symbols" ) );
329 mModel->setEntityFilterEnabled(
true );
330 mModel->setEntityFilters( filters );
333 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
336 btnSaveSymbol->setText( tr(
"Save Settings…" ) );
337 btnSaveSymbol->setToolTip( tr(
"Save label settings or text format to styles" ) );
339 groupsCombo->setItemText( allGroup, tr(
"All Settings" ) );
345 mModel->setSymbolTypeFilterEnabled(
true );
346 mModel->setSymbolType( type );
351 mModel->setLayerType( type );
356 return groupsCombo->currentData().toString() == QLatin1String(
"tag" ) ? groupsCombo->currentText() : QString();
361 return btnAdvanced->menu();
369 btnAdvanced->setMenu( menu );
375 btnAdvanced->setVisible( enabled );
380 const QItemSelection selection = viewSymbols->selectionModel()->selection();
381 if ( selection.isEmpty() )
384 const QModelIndex index = selection.at( 0 ).topLeft();
391 const QItemSelection selection = viewSymbols->selectionModel()->selection();
392 if ( selection.isEmpty() )
395 const QModelIndex index = selection.at( 0 ).topLeft();
406 QWidget::showEvent( event );
408 mSymbolTreeView->header()->restoreState( settings.
value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
411void QgsStyleItemsListWidget::populateGroups()
416 mUpdatingGroups =
true;
417 groupsCombo->blockSignals(
true );
418 groupsCombo->clear();
420 groupsCombo->addItem( tr(
"Favorites" ), QVariant(
"favorite" ) );
422 QString allText = tr(
"All Symbols" );
428 allText = tr(
"All Symbols" );
432 allText = tr(
"All Color Ramps" );
436 allText = tr(
"All Text Formats" );
440 allText = tr(
"All Label Settings" );
444 allText = tr(
"All Legend Patch Shapes" );
448 allText = tr(
"All 3D Symbols" );
457 groupsCombo->addItem( allText, QVariant(
"all" ) );
460 QStringList tags = mStyle->tags();
461 if ( tags.count() > 0 )
464 groupsCombo->insertSeparator( index );
465 const auto constTags = tags;
466 for (
const QString &tag : constTags )
468 groupsCombo->addItem( tag, QVariant(
"tag" ) );
473 QStringList groups = mStyle->smartgroupNames();
474 if ( groups.count() > 0 )
477 groupsCombo->insertSeparator( index + 1 );
478 const auto constGroups = groups;
479 for (
const QString &group : constGroups )
481 groupsCombo->addItem( group, QVariant(
"smartgroup" ) );
484 groupsCombo->blockSignals(
false );
486 const QgsSettings settings;
487 index = settings.
value( QStringLiteral(
"qgis/symbolsListGroupsIndex" ), 0 ).toInt();
488 groupsCombo->setCurrentIndex( index );
490 mUpdatingGroups =
false;
492 updateModelFilters();
495void QgsStyleItemsListWidget::updateModelFilters()
497 if ( mUpdatingGroups || !mModel )
500 const QString text = groupsCombo->currentText();
501 const bool isFreeText = text != groupsCombo->itemText( groupsCombo->currentIndex() );
505 mModel->setFavoritesOnly(
false );
506 mModel->setTagString( QString() );
507 mModel->setSmartGroupId( -1 );
508 mModel->setFilterString( groupsCombo->currentText() );
510 else if ( groupsCombo->currentData().toString() == QLatin1String(
"favorite" ) )
512 mModel->setFavoritesOnly(
true );
513 mModel->setTagString( QString() );
514 mModel->setSmartGroupId( -1 );
515 mModel->setFilterString( QString() );
517 else if ( groupsCombo->currentData().toString() == QLatin1String(
"all" ) )
519 mModel->setFavoritesOnly(
false );
520 mModel->setTagString( QString() );
521 mModel->setSmartGroupId( -1 );
522 mModel->setFilterString( QString() );
524 else if ( groupsCombo->currentData().toString() == QLatin1String(
"smartgroup" ) )
526 mModel->setFavoritesOnly(
false );
527 mModel->setTagString( QString() );
528 mModel->setSmartGroupId( mStyle->smartgroupId( text ) );
529 mModel->setFilterString( QString() );
533 mModel->setFavoritesOnly(
false );
534 mModel->setTagString( text );
535 mModel->setSmartGroupId( -1 );
536 mModel->setFilterString( QString() );
540void QgsStyleItemsListWidget::openStyleManager()
553 std::unique_ptr< QgsStyleManagerDialog > dlg;
556 dlg = std::make_unique< QgsStyleManagerDialog >( mStyle,
this );
560 dlg = std::make_unique< QgsStyleManagerDialog >(
this );
564 updateModelFilters();
568void QgsStyleItemsListWidget::onSelectionChanged(
const QModelIndex &index,
const QModelIndex &previous )
573 if ( index.row() == previous.row() )
576 const QString symbolName = mModel->data( mModel->index( index.row(),
QgsStyleModel::Name ) ).toString();
577 lblSymbolName->setText( symbolName );
585void QgsStyleItemsListWidget::groupsCombo_currentIndexChanged(
int index )
587 QgsSettings settings;
588 settings.
setValue( QStringLiteral(
"qgis/symbolsListGroupsIndex" ), 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.
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.
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.