18#include "moc_qgsstyleitemslistwidget.cpp"
33QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyleModel *sourceModel, QObject *parent )
38QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyle *style, QObject *parent )
48Qt::ItemFlags QgsReadOnlyStyleModel::flags(
const QModelIndex &index )
const
50 return QgsStyleProxyModel::flags( index ) & ~Qt::ItemIsEditable;
53QVariant QgsReadOnlyStyleModel::data(
const QModelIndex &index,
int role )
const
55 if ( role == Qt::FontRole )
58 QFont f = QgsStyleProxyModel::data( index, role ).value<QFont>();
63 return QgsStyleProxyModel::data( index, role );
71QgsStyleModelDelegate::QgsStyleModelDelegate( QObject *parent )
72 : QStyledItemDelegate( parent )
76QSize QgsStyleModelDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
78 if (
const QListView *view = qobject_cast<const QListView *>( option.widget ) )
83 QFont f = option.font;
84 f.setPointSizeF( f.pointSizeF() * 1.4 );
85 const QFontMetrics fm( f );
86 return QSize( option.widget->width() - view->verticalScrollBar()->width() * 2, fm.height() );
92 const QSize
iconSize = option.decorationSize;
93 return QSize(
static_cast<int>(
iconSize.width() * 1.4 ),
static_cast<int>(
iconSize.height() * 1.7 ) );
96 else if ( qobject_cast<const QTreeView *>( option.widget ) )
100 QSize defaultSize = QStyledItemDelegate::sizeHint( option, index );
102 return QSize( defaultSize.width(),
static_cast<int>( defaultSize.height() * 1.2 ) );
106 return QStyledItemDelegate::sizeHint( option, index );
109void QgsStyleModelDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
113 QStyleOptionViewItem titleOption( option );
114 initStyleOption( &titleOption, index );
115 if ( qobject_cast<const QListView *>( option.widget ) )
117 titleOption.font.setBold(
true );
118 titleOption.font.setPointSizeF( titleOption.font.pointSizeF() * 1.4 );
121 painter->setBrush( titleOption.palette.windowText() );
122 painter->setFont( titleOption.font );
123 const QRect rect = QRect( titleOption.rect.left(), titleOption.rect.top(), titleOption.rect.width(), titleOption.rect.height() );
125 painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, index.data( Qt::DisplayRole ).toString() );
126 painter->setBrush( Qt::NoBrush );
127 QColor lineColor = titleOption.palette.windowText().color();
128 lineColor.setAlpha( 100 );
129 painter->setPen( QPen( lineColor, 1 ) );
130 painter->drawLine( titleOption.rect.left(), titleOption.rect.bottom(), titleOption.rect.right(), titleOption.rect.bottom() );
134 else if ( qobject_cast<const QTreeView *>( option.widget ) )
137 QColor lineColor = option.palette.windowText().color();
138 lineColor.setAlpha( 100 );
139 painter->setPen( QPen( lineColor, 1 ) );
141 QFont f = option.font;
144 titleOption.font = f;
145 titleOption.fontMetrics = QFontMetrics( titleOption.font );
147 painter->drawLine( index.column() == 0 ? 0 : option.rect.left(), option.rect.bottom(), index.column() == 0 ? option.rect.right() : option.widget->width(), option.rect.bottom() );
150 titleOption.state |= QStyle::State_Enabled;
151 QStyledItemDelegate::paint( painter, titleOption, index );
156 QStyledItemDelegate::paint( painter, option, index );
172 mDelegate =
new QgsStyleModelDelegate(
this );
175 btnAdvanced->setMenu(
new QMenu(
this ) );
178 viewSymbols->setIconSize( QSize(
static_cast<int>( iconSize ),
static_cast<int>( iconSize * 0.9 ) ) );
181 mSymbolTreeView->setIconSize( QSize(
static_cast<int>( treeIconSize ),
static_cast<int>( treeIconSize ) ) );
182 mSymbolTreeView->setMinimumHeight( mSymbolTreeView->fontMetrics().height() * 6 );
184 viewSymbols->setItemDelegate( mDelegate );
185 mSymbolTreeView->setItemDelegate( mDelegate );
187 viewSymbols->setSelectionBehavior( QAbstractItemView::SelectRows );
188 mSymbolTreeView->setSelectionMode( viewSymbols->selectionMode() );
190 connect( openStyleManagerButton, &QToolButton::clicked,
this, &QgsStyleItemsListWidget::openStyleManager );
192 lblSymbolName->clear();
194 connect( mButtonIconView, &QToolButton::toggled,
this, [=](
bool active ) {
197 mSymbolViewStackedWidget->setCurrentIndex( 0 );
202 connect( mButtonListView, &QToolButton::toggled,
this, [=](
bool active ) {
206 mSymbolViewStackedWidget->setCurrentIndex( 1 );
212 const int currentView = settings.
value( QStringLiteral(
"UI/symbolsList/lastIconView" ), 0,
QgsSettings::Gui ).toInt();
213 if ( currentView == 0 )
214 mButtonIconView->setChecked(
true );
216 mButtonListView->setChecked(
true );
218 mSymbolTreeView->header()->restoreState( settings.value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
219 connect( mSymbolTreeView->header(), &QHeaderView::sectionResized,
this, [
this] {
221 QgsSettings().setValue( QStringLiteral(
"UI/symbolsList/treeState" ), mSymbolTreeView->header()->saveState(), QgsSettings::Gui );
227 groupEdit->setPlaceholderText( tr(
"Filter symbols…" ) );
228 groupsCombo->setLineEdit( groupEdit );
238 :
new QgsReadOnlyStyleModel( mStyle,
this );
245 viewSymbols->setTextElideMode( Qt::TextElideMode::ElideRight );
247 viewSymbols->setModel( mModel );
248 mSymbolTreeView->setModel( mModel );
252 mSymbolTreeView->setSelectionModel( viewSymbols->selectionModel() );
253 connect( viewSymbols->selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsStyleItemsListWidget::onSelectionChanged );
256 connect( groupsCombo,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsStyleItemsListWidget::groupsCombo_currentIndexChanged );
257 connect( groupsCombo, &QComboBox::currentTextChanged,
this, &QgsStyleItemsListWidget::updateModelFilters );
260 mSymbolTreeView->header()->restoreState( settings.
value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
267 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
271 btnSaveSymbol->setText( tr(
"Save Symbol…" ) );
272 btnSaveSymbol->setToolTip( tr(
"Save symbol to styles" ) );
274 groupsCombo->setItemText( allGroup, tr(
"All Symbols" ) );
278 btnSaveSymbol->setText( tr(
"Save Color Ramp…" ) );
279 btnSaveSymbol->setToolTip( tr(
"Save color ramp to styles" ) );
281 groupsCombo->setItemText( allGroup, tr(
"All Color Ramps" ) );
285 btnSaveSymbol->setText( tr(
"Save Format…" ) );
286 btnSaveSymbol->setToolTip( tr(
"Save text format to styles" ) );
288 groupsCombo->setItemText( allGroup, tr(
"All Text Formats" ) );
292 btnSaveSymbol->setText( tr(
"Save Label Settings…" ) );
293 btnSaveSymbol->setToolTip( tr(
"Save label settings to styles" ) );
295 groupsCombo->setItemText( allGroup, tr(
"All Label Settings" ) );
299 btnSaveSymbol->setText( tr(
"Save Legend Patch Shape…" ) );
300 btnSaveSymbol->setToolTip( tr(
"Save legend patch shape to styles" ) );
302 groupsCombo->setItemText( allGroup, tr(
"All Legend Patch Shapes" ) );
306 btnSaveSymbol->setText( tr(
"Save 3D Symbol…" ) );
307 btnSaveSymbol->setToolTip( tr(
"Save 3D symbol to styles" ) );
309 groupsCombo->setItemText( allGroup, tr(
"All 3D Symbols" ) );
324 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
327 btnSaveSymbol->setText( tr(
"Save Settings…" ) );
328 btnSaveSymbol->setToolTip( tr(
"Save label settings or text format to styles" ) );
330 groupsCombo->setItemText( allGroup, tr(
"All Settings" ) );
347 return groupsCombo->currentData().toString() == QLatin1String(
"tag" ) ? groupsCombo->currentText() : QString();
352 return btnAdvanced->menu();
360 btnAdvanced->setMenu( menu );
366 btnAdvanced->setVisible( enabled );
371 const QItemSelection selection = viewSymbols->selectionModel()->selection();
372 if ( selection.isEmpty() )
375 const QModelIndex index = selection.at( 0 ).topLeft();
382 const QItemSelection selection = viewSymbols->selectionModel()->selection();
383 if ( selection.isEmpty() )
386 const QModelIndex index = selection.at( 0 ).topLeft();
397 QWidget::showEvent( event );
399 mSymbolTreeView->header()->restoreState( settings.
value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
402void QgsStyleItemsListWidget::populateGroups()
407 mUpdatingGroups =
true;
408 groupsCombo->blockSignals(
true );
409 groupsCombo->clear();
411 groupsCombo->addItem( tr(
"Favorites" ), QVariant(
"favorite" ) );
413 QString allText = tr(
"All Symbols" );
419 allText = tr(
"All Symbols" );
423 allText = tr(
"All Color Ramps" );
427 allText = tr(
"All Text Formats" );
431 allText = tr(
"All Label Settings" );
435 allText = tr(
"All Legend Patch Shapes" );
439 allText = tr(
"All 3D Symbols" );
448 groupsCombo->addItem( allText, QVariant(
"all" ) );
451 QStringList tags = mStyle->
tags();
452 if ( tags.count() > 0 )
455 groupsCombo->insertSeparator( index );
456 const auto constTags = tags;
457 for (
const QString &tag : constTags )
459 groupsCombo->addItem( tag, QVariant(
"tag" ) );
465 if ( groups.count() > 0 )
468 groupsCombo->insertSeparator( index + 1 );
469 const auto constGroups = groups;
470 for (
const QString &group : constGroups )
472 groupsCombo->addItem( group, QVariant(
"smartgroup" ) );
475 groupsCombo->blockSignals(
false );
478 index = settings.
value( QStringLiteral(
"qgis/symbolsListGroupsIndex" ), 0 ).toInt();
479 groupsCombo->setCurrentIndex( index );
481 mUpdatingGroups =
false;
483 updateModelFilters();
486void QgsStyleItemsListWidget::updateModelFilters()
488 if ( mUpdatingGroups || !mModel )
491 const QString text = groupsCombo->currentText();
492 const bool isFreeText = text != groupsCombo->itemText( groupsCombo->currentIndex() );
501 else if ( groupsCombo->currentData().toString() == QLatin1String(
"favorite" ) )
508 else if ( groupsCombo->currentData().toString() == QLatin1String(
"all" ) )
515 else if ( groupsCombo->currentData().toString() == QLatin1String(
"smartgroup" ) )
531void QgsStyleItemsListWidget::openStyleManager()
547 updateModelFilters();
551void QgsStyleItemsListWidget::onSelectionChanged(
const QModelIndex &index )
556 const QString symbolName = mModel->data( mModel->index( index.row(),
QgsStyleModel::Name ) ).toString();
557 lblSymbolName->setText( symbolName );
565void QgsStyleItemsListWidget::groupsCombo_currentIndexChanged(
int index )
568 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.
This class is a composition of two QSettings instances:
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 dialog allowing users to customize and populate a QgsStyle.
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 ...
void setEntityFilter(QgsStyle::StyleEntity filter)
Sets the style entity type filter.
void setSymbolTypeFilterEnabled(bool enabled)
Sets whether filtering by symbol type is enabled.
void setTagString(const QString &tag)
Sets a tag to filter style entities by.
QgsStyle::StyleEntity entityFilter() const
Returns the style entity type filter.
void setEntityFilters(const QList< QgsStyle::StyleEntity > &filters)
Sets the style entity type filters.
void setFavoritesOnly(bool favoritesOnly)
Sets whether the model should show only favorited entities.
void setSymbolType(Qgis::SymbolType type)
Sets the symbol type filter.
bool entityFilterEnabled() const
Returns true if filtering by entity type is enabled.
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
void addTargetScreenProperties(const QgsScreenProperties &properties)
Adds additional target screen properties to use when generating icons for Qt::DecorationRole data.
void setEntityFilterEnabled(bool enabled)
Sets whether filtering by entity type is enabled.
void setSmartGroupId(int id)
Sets a smart group id to filter style entities by.
void setFilterString(const QString &filter)
Sets a filter string, such that only symbol entities with names matching the specified string will be...
void setLayerType(Qgis::GeometryType type)
Sets the layer type filter.
QStringList tags() const
Returns a list of all tags in the style database.
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.
int smartgroupId(const QString &smartgroup)
Returns the database id for the given smartgroup name.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
QStringList smartgroupNames() const
Returns the smart groups list.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.