18#include "moc_qgsstyleitemslistwidget.cpp"
33QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyleModel *sourceModel, QObject *parent )
39QgsReadOnlyStyleModel::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 )
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(),
128 titleOption.rect.width(), titleOption.rect.height() );
130 painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, index.data( Qt::DisplayRole ).toString() );
131 painter->setBrush( Qt::NoBrush );
132 QColor lineColor = titleOption.palette.windowText().color();
133 lineColor.setAlpha( 100 );
134 painter->setPen( QPen( lineColor, 1 ) );
135 painter->drawLine( titleOption.rect.left(), titleOption.rect.bottom(), titleOption.rect.right(), titleOption.rect.bottom() );
139 else if ( qobject_cast< const QTreeView * >( option.widget ) )
142 QColor lineColor = option.palette.windowText().color();
143 lineColor.setAlpha( 100 );
144 painter->setPen( QPen( lineColor, 1 ) );
146 QFont f = option.font;
149 titleOption.font = f;
150 titleOption.fontMetrics = QFontMetrics( titleOption.font );
152 painter->drawLine( index.column() == 0 ? 0 : option.rect.left(),
153 option.rect.bottom(),
154 index.column() == 0 ? option.rect.right() : option.widget->width(),
155 option.rect.bottom() );
158 titleOption.state |= QStyle::State_Enabled;
159 QStyledItemDelegate::paint( painter, titleOption, index );
164 QStyledItemDelegate::paint( painter, option, index );
181 mDelegate =
new QgsStyleModelDelegate(
this );
184 btnAdvanced->setMenu(
new QMenu(
this ) );
187 viewSymbols->setIconSize( QSize(
static_cast< int >( iconSize ),
static_cast< int >( iconSize * 0.9 ) ) );
190 mSymbolTreeView->setIconSize( QSize(
static_cast< int >( treeIconSize ),
static_cast< int >( treeIconSize ) ) );
191 mSymbolTreeView->setMinimumHeight( mSymbolTreeView->fontMetrics().height() * 6 );
193 viewSymbols->setItemDelegate( mDelegate );
194 mSymbolTreeView->setItemDelegate( mDelegate );
196 viewSymbols->setSelectionBehavior( QAbstractItemView::SelectRows );
197 mSymbolTreeView->setSelectionMode( viewSymbols->selectionMode() );
199 connect( openStyleManagerButton, &QToolButton::clicked,
this, &QgsStyleItemsListWidget::openStyleManager );
201 lblSymbolName->clear();
203 connect( mButtonIconView, &QToolButton::toggled,
this, [ = ](
bool active )
207 mSymbolViewStackedWidget->setCurrentIndex( 0 );
212 connect( mButtonListView, &QToolButton::toggled,
this, [ = ](
bool active )
217 mSymbolViewStackedWidget->setCurrentIndex( 1 );
223 const int currentView = settings.
value( QStringLiteral(
"UI/symbolsList/lastIconView" ), 0,
QgsSettings::Gui ).toInt();
224 if ( currentView == 0 )
225 mButtonIconView->setChecked(
true );
227 mButtonListView->setChecked(
true );
229 mSymbolTreeView->header()->restoreState( settings.value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
230 connect( mSymbolTreeView->header(), &QHeaderView::sectionResized,
this, [
this]
233 QgsSettings().setValue( QStringLiteral(
"UI/symbolsList/treeState" ), mSymbolTreeView->header()->saveState(), QgsSettings::Gui );
239 groupEdit->setPlaceholderText( tr(
"Filter symbols…" ) );
240 groupsCombo->setLineEdit( groupEdit );
250 :
new QgsReadOnlyStyleModel( mStyle,
this );
257 viewSymbols->setTextElideMode( Qt::TextElideMode::ElideRight );
259 viewSymbols->setModel( mModel );
260 mSymbolTreeView->setModel( mModel );
264 mSymbolTreeView->setSelectionModel( viewSymbols->selectionModel() );
265 connect( viewSymbols->selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsStyleItemsListWidget::onSelectionChanged );
268 connect( groupsCombo,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsStyleItemsListWidget::groupsCombo_currentIndexChanged );
269 connect( groupsCombo, &QComboBox::currentTextChanged,
this, &QgsStyleItemsListWidget::updateModelFilters );
272 mSymbolTreeView->header()->restoreState( settings.
value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
279 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
283 btnSaveSymbol->setText( tr(
"Save Symbol…" ) );
284 btnSaveSymbol->setToolTip( tr(
"Save symbol to styles" ) );
286 groupsCombo->setItemText( allGroup, tr(
"All Symbols" ) );
290 btnSaveSymbol->setText( tr(
"Save Color Ramp…" ) );
291 btnSaveSymbol->setToolTip( tr(
"Save color ramp to styles" ) );
293 groupsCombo->setItemText( allGroup, tr(
"All Color Ramps" ) );
297 btnSaveSymbol->setText( tr(
"Save Format…" ) );
298 btnSaveSymbol->setToolTip( tr(
"Save text format to styles" ) );
300 groupsCombo->setItemText( allGroup, tr(
"All Text Formats" ) );
304 btnSaveSymbol->setText( tr(
"Save Label Settings…" ) );
305 btnSaveSymbol->setToolTip( tr(
"Save label settings to styles" ) );
307 groupsCombo->setItemText( allGroup, tr(
"All Label Settings" ) );
311 btnSaveSymbol->setText( tr(
"Save Legend Patch Shape…" ) );
312 btnSaveSymbol->setToolTip( tr(
"Save legend patch shape to styles" ) );
314 groupsCombo->setItemText( allGroup, tr(
"All Legend Patch Shapes" ) );
318 btnSaveSymbol->setText( tr(
"Save 3D Symbol…" ) );
319 btnSaveSymbol->setToolTip( tr(
"Save 3D symbol to styles" ) );
321 groupsCombo->setItemText( allGroup, tr(
"All 3D Symbols" ) );
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" ) );
359 return groupsCombo->currentData().toString() == QLatin1String(
"tag" ) ? groupsCombo->currentText() : QString();
364 return btnAdvanced->menu();
372 btnAdvanced->setMenu( menu );
378 btnAdvanced->setVisible( enabled );
383 const QItemSelection selection = viewSymbols->selectionModel()->selection();
384 if ( selection.isEmpty() )
387 const QModelIndex index = selection.at( 0 ).topLeft();
394 const QItemSelection selection = viewSymbols->selectionModel()->selection();
395 if ( selection.isEmpty() )
398 const QModelIndex index = selection.at( 0 ).topLeft();
409 QWidget::showEvent( event );
411 mSymbolTreeView->header()->restoreState( settings.
value( QStringLiteral(
"UI/symbolsList/treeState" ), QByteArray(),
QgsSettings::Gui ).toByteArray() );
414void QgsStyleItemsListWidget::populateGroups()
419 mUpdatingGroups =
true;
420 groupsCombo->blockSignals(
true );
421 groupsCombo->clear();
423 groupsCombo->addItem( tr(
"Favorites" ), QVariant(
"favorite" ) );
425 QString allText = tr(
"All Symbols" );
431 allText = tr(
"All Symbols" );
435 allText = tr(
"All Color Ramps" );
439 allText = tr(
"All Text Formats" );
443 allText = tr(
"All Label Settings" );
447 allText = tr(
"All Legend Patch Shapes" );
451 allText = tr(
"All 3D Symbols" );
460 groupsCombo->addItem( allText, QVariant(
"all" ) );
463 QStringList tags = mStyle->
tags();
464 if ( tags.count() > 0 )
467 groupsCombo->insertSeparator( index );
468 const auto constTags = tags;
469 for (
const QString &tag : constTags )
471 groupsCombo->addItem( tag, QVariant(
"tag" ) );
477 if ( groups.count() > 0 )
480 groupsCombo->insertSeparator( index + 1 );
481 const auto constGroups = groups;
482 for (
const QString &group : constGroups )
484 groupsCombo->addItem( group, QVariant(
"smartgroup" ) );
487 groupsCombo->blockSignals(
false );
490 index = settings.
value( QStringLiteral(
"qgis/symbolsListGroupsIndex" ), 0 ).toInt();
491 groupsCombo->setCurrentIndex( index );
493 mUpdatingGroups =
false;
495 updateModelFilters();
498void QgsStyleItemsListWidget::updateModelFilters()
500 if ( mUpdatingGroups || !mModel )
503 const QString text = groupsCombo->currentText();
504 const bool isFreeText = text != groupsCombo->itemText( groupsCombo->currentIndex() );
513 else if ( groupsCombo->currentData().toString() == QLatin1String(
"favorite" ) )
520 else if ( groupsCombo->currentData().toString() == QLatin1String(
"all" ) )
527 else if ( groupsCombo->currentData().toString() == QLatin1String(
"smartgroup" ) )
543void QgsStyleItemsListWidget::openStyleManager()
559 updateModelFilters();
563void QgsStyleItemsListWidget::onSelectionChanged(
const QModelIndex &index )
568 const QString symbolName = mModel->data( mModel->index( index.row(),
QgsStyleModel::Name ) ).toString();
569 lblSymbolName->setText( symbolName );
577void QgsStyleItemsListWidget::groupsCombo_currentIndexChanged(
int index )
580 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.