30#include "moc_qgsstyleitemslistwidget.cpp"
32using namespace Qt::StringLiterals;
39QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyleModel *sourceModel, QObject *parent )
43QgsReadOnlyStyleModel::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 )
78QSize QgsStyleModelDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
80 if (
const QListView *view = qobject_cast<const QListView *>( option.widget ) )
85 QFont f = option.font;
86 f.setPointSizeF( f.pointSizeF() * 1.4 );
87 const QFontMetrics fm( f );
88 return QSize( option.widget->width() - view->verticalScrollBar()->width() * 2, fm.height() );
94 const QSize
iconSize = option.decorationSize;
95 return QSize(
static_cast<int>(
iconSize.width() * 1.4 ),
static_cast<int>(
iconSize.height() * 1.7 ) );
98 else if ( qobject_cast<const QTreeView *>( option.widget ) )
102 QSize defaultSize = QStyledItemDelegate::sizeHint( option, index );
104 return QSize( defaultSize.width(),
static_cast<int>( defaultSize.height() * 1.2 ) );
108 return QStyledItemDelegate::sizeHint( option, index );
111void QgsStyleModelDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
115 QStyleOptionViewItem titleOption( option );
116 initStyleOption( &titleOption, index );
117 if ( qobject_cast<const QListView *>( option.widget ) )
119 titleOption.font.setBold(
true );
120 titleOption.font.setPointSizeF( titleOption.font.pointSizeF() * 1.4 );
123 painter->setBrush( titleOption.palette.windowText() );
124 painter->setFont( titleOption.font );
125 const QRect rect = QRect( titleOption.rect.left(), titleOption.rect.top(), titleOption.rect.width(), titleOption.rect.height() );
127 painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, index.data( Qt::DisplayRole ).toString() );
128 painter->setBrush( Qt::NoBrush );
129 QColor lineColor = titleOption.palette.windowText().color();
130 lineColor.setAlpha( 100 );
131 painter->setPen( QPen( lineColor, 1 ) );
132 painter->drawLine( titleOption.rect.left(), titleOption.rect.bottom(), titleOption.rect.right(), titleOption.rect.bottom() );
136 else if ( qobject_cast<const QTreeView *>( option.widget ) )
139 QColor lineColor = option.palette.windowText().color();
140 lineColor.setAlpha( 100 );
141 painter->setPen( QPen( lineColor, 1 ) );
143 QFont f = option.font;
146 titleOption.font = f;
147 titleOption.fontMetrics = QFontMetrics( titleOption.font );
149 painter->drawLine( index.column() == 0 ? 0 : option.rect.left(), option.rect.bottom(), index.column() == 0 ? option.rect.right() : option.widget->width(), option.rect.bottom() );
152 titleOption.state |= QStyle::State_Enabled;
153 QStyledItemDelegate::paint( painter, titleOption, index );
158 QStyledItemDelegate::paint( painter, option, index );
174 mDelegate =
new QgsStyleModelDelegate(
this );
177 btnAdvanced->setMenu(
new QMenu(
this ) );
180 viewSymbols->setIconSize( QSize(
static_cast<int>( iconSize ),
static_cast<int>( iconSize * 0.9 ) ) );
183 mSymbolTreeView->setIconSize( QSize(
static_cast<int>( treeIconSize ),
static_cast<int>( treeIconSize ) ) );
184 mSymbolTreeView->setMinimumHeight( mSymbolTreeView->fontMetrics().height() * 6 );
186 viewSymbols->setItemDelegate( mDelegate );
187 mSymbolTreeView->setItemDelegate( mDelegate );
189 viewSymbols->setSelectionBehavior( QAbstractItemView::SelectRows );
190 mSymbolTreeView->setSelectionMode( viewSymbols->selectionMode() );
192 connect( openStyleManagerButton, &QToolButton::clicked,
this, &QgsStyleItemsListWidget::openStyleManager );
194 lblSymbolName->clear();
196 connect( mButtonIconView, &QToolButton::toggled,
this, [
this](
bool active ) {
199 mSymbolViewStackedWidget->setCurrentIndex( 0 );
204 connect( mButtonListView, &QToolButton::toggled,
this, [
this](
bool active ) {
208 mSymbolViewStackedWidget->setCurrentIndex( 1 );
214 const int currentView = settings.
value( u
"UI/symbolsList/lastIconView"_s, 0,
QgsSettings::Gui ).toInt();
215 if ( currentView == 0 )
216 mButtonIconView->setChecked(
true );
218 mButtonListView->setChecked(
true );
220 mSymbolTreeView->header()->restoreState( settings.value( u
"UI/symbolsList/treeState"_s, QByteArray(),
QgsSettings::Gui ).toByteArray() );
221 connect( mSymbolTreeView->header(), &QHeaderView::sectionResized,
this, [
this] {
223 QgsSettings().setValue( u
"UI/symbolsList/treeState"_s, mSymbolTreeView->header()->saveState(), QgsSettings::Gui );
229 groupEdit->setPlaceholderText( tr(
"Filter symbols…" ) );
230 groupsCombo->setLineEdit( groupEdit );
241 mModel->addDesiredIconSize( viewSymbols->iconSize() );
242 mModel->addDesiredIconSize( mSymbolTreeView->iconSize() );
246 viewSymbols->setTextElideMode( Qt::TextElideMode::ElideRight );
248 viewSymbols->setModel( mModel );
249 mSymbolTreeView->setModel( mModel );
253 mSymbolTreeView->setSelectionModel( viewSymbols->selectionModel() );
254 connect( viewSymbols->selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsStyleItemsListWidget::onSelectionChanged );
255 connect( viewSymbols, &QListView::activated,
this, [
this](
const QModelIndex &index ) { onSelectionChanged( index, QModelIndex() ); } );
256 connect( mSymbolTreeView, &QTreeView::activated,
this, [
this](
const QModelIndex &index ) { onSelectionChanged( index, QModelIndex() ); } );
259 connect( groupsCombo,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsStyleItemsListWidget::groupsCombo_currentIndexChanged );
260 connect( groupsCombo, &QComboBox::currentTextChanged,
this, &QgsStyleItemsListWidget::updateModelFilters );
263 mSymbolTreeView->header()->restoreState( settings.
value( u
"UI/symbolsList/treeState"_s, QByteArray(),
QgsSettings::Gui ).toByteArray() );
268 mModel->setEntityFilterEnabled(
true );
269 mModel->setEntityFilter( type );
270 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
274 btnSaveSymbol->setText( tr(
"Save Symbol…" ) );
275 btnSaveSymbol->setToolTip( tr(
"Save symbol to styles" ) );
277 groupsCombo->setItemText( allGroup, tr(
"All Symbols" ) );
281 btnSaveSymbol->setText( tr(
"Save Color Ramp…" ) );
282 btnSaveSymbol->setToolTip( tr(
"Save color ramp to styles" ) );
284 groupsCombo->setItemText( allGroup, tr(
"All Color Ramps" ) );
288 btnSaveSymbol->setText( tr(
"Save Format…" ) );
289 btnSaveSymbol->setToolTip( tr(
"Save text format to styles" ) );
291 groupsCombo->setItemText( allGroup, tr(
"All Text Formats" ) );
295 btnSaveSymbol->setText( tr(
"Save Label Settings…" ) );
296 btnSaveSymbol->setToolTip( tr(
"Save label settings to styles" ) );
298 groupsCombo->setItemText( allGroup, tr(
"All Label Settings" ) );
302 btnSaveSymbol->setText( tr(
"Save Legend Patch Shape…" ) );
303 btnSaveSymbol->setToolTip( tr(
"Save legend patch shape to styles" ) );
305 groupsCombo->setItemText( allGroup, tr(
"All Legend Patch Shapes" ) );
309 btnSaveSymbol->setText( tr(
"Save 3D Symbol…" ) );
310 btnSaveSymbol->setToolTip( tr(
"Save 3D symbol to styles" ) );
312 groupsCombo->setItemText( allGroup, tr(
"All 3D Symbols" ) );
323 mModel->setEntityFilterEnabled(
true );
324 mModel->setEntityFilters( filters );
327 const int allGroup = groupsCombo->findData( QVariant(
"all" ) );
330 btnSaveSymbol->setText( tr(
"Save Settings…" ) );
331 btnSaveSymbol->setToolTip( tr(
"Save label settings or text format to styles" ) );
333 groupsCombo->setItemText( allGroup, tr(
"All Settings" ) );
339 mModel->setSymbolTypeFilterEnabled(
true );
340 mModel->setSymbolType( type );
345 mModel->setLayerType( type );
350 return groupsCombo->currentData().toString() ==
"tag"_L1 ? groupsCombo->currentText() : QString();
355 return btnAdvanced->menu();
363 btnAdvanced->setMenu( menu );
369 btnAdvanced->setVisible( enabled );
374 const QItemSelection selection = viewSymbols->selectionModel()->selection();
375 if ( selection.isEmpty() )
378 const QModelIndex index = selection.at( 0 ).topLeft();
385 const QItemSelection selection = viewSymbols->selectionModel()->selection();
386 if ( selection.isEmpty() )
389 const QModelIndex index = selection.at( 0 ).topLeft();
400 QWidget::showEvent( event );
402 mSymbolTreeView->header()->restoreState( settings.
value( u
"UI/symbolsList/treeState"_s, QByteArray(),
QgsSettings::Gui ).toByteArray() );
405void QgsStyleItemsListWidget::populateGroups()
410 mUpdatingGroups =
true;
411 groupsCombo->blockSignals(
true );
412 groupsCombo->clear();
414 groupsCombo->addItem( tr(
"Favorites" ), QVariant(
"favorite" ) );
416 QString allText = tr(
"All Symbols" );
422 allText = tr(
"All Symbols" );
426 allText = tr(
"All Color Ramps" );
430 allText = tr(
"All Text Formats" );
434 allText = tr(
"All Label Settings" );
438 allText = tr(
"All Legend Patch Shapes" );
442 allText = tr(
"All 3D Symbols" );
451 groupsCombo->addItem( allText, QVariant(
"all" ) );
454 QStringList tags = mStyle->tags();
455 if ( tags.count() > 0 )
458 groupsCombo->insertSeparator( index );
459 const auto constTags = tags;
460 for (
const QString &tag : constTags )
462 groupsCombo->addItem( tag, QVariant(
"tag" ) );
467 QStringList groups = mStyle->smartgroupNames();
468 if ( groups.count() > 0 )
471 groupsCombo->insertSeparator( index + 1 );
472 const auto constGroups = groups;
473 for (
const QString &group : constGroups )
475 groupsCombo->addItem( group, QVariant(
"smartgroup" ) );
478 groupsCombo->blockSignals(
false );
480 const QgsSettings settings;
481 index = settings.
value( u
"qgis/symbolsListGroupsIndex"_s, 0 ).toInt();
482 groupsCombo->setCurrentIndex( index );
484 mUpdatingGroups =
false;
486 updateModelFilters();
489void QgsStyleItemsListWidget::updateModelFilters()
491 if ( mUpdatingGroups || !mModel )
494 const QString text = groupsCombo->currentText();
495 const bool isFreeText = text != groupsCombo->itemText( groupsCombo->currentIndex() );
499 mModel->setFavoritesOnly(
false );
500 mModel->setTagString( QString() );
501 mModel->setSmartGroupId( -1 );
502 mModel->setFilterString( groupsCombo->currentText() );
504 else if ( groupsCombo->currentData().toString() ==
"favorite"_L1 )
506 mModel->setFavoritesOnly(
true );
507 mModel->setTagString( QString() );
508 mModel->setSmartGroupId( -1 );
509 mModel->setFilterString( QString() );
511 else if ( groupsCombo->currentData().toString() ==
"all"_L1 )
513 mModel->setFavoritesOnly(
false );
514 mModel->setTagString( QString() );
515 mModel->setSmartGroupId( -1 );
516 mModel->setFilterString( QString() );
518 else if ( groupsCombo->currentData().toString() ==
"smartgroup"_L1 )
520 mModel->setFavoritesOnly(
false );
521 mModel->setTagString( QString() );
522 mModel->setSmartGroupId( mStyle->smartgroupId( text ) );
523 mModel->setFilterString( QString() );
527 mModel->setFavoritesOnly(
false );
528 mModel->setTagString( text );
529 mModel->setSmartGroupId( -1 );
530 mModel->setFilterString( QString() );
534void QgsStyleItemsListWidget::openStyleManager()
545 std::unique_ptr< QgsStyleManagerDialog > dlg;
548 dlg = std::make_unique< QgsStyleManagerDialog >( mStyle,
this );
552 dlg = std::make_unique< QgsStyleManagerDialog >(
this );
556 updateModelFilters();
560void QgsStyleItemsListWidget::onSelectionChanged(
const QModelIndex &index,
const QModelIndex &previous )
565 if ( index.row() == previous.row() )
568 const QString symbolName = mModel->data( mModel->index( index.row(),
QgsStyleModel::Name ) ).toString();
569 lblSymbolName->setText( symbolName );
577void QgsStyleItemsListWidget::groupsCombo_currentIndexChanged(
int index )
579 QgsSettings settings;
580 settings.
setValue( u
"qgis/symbolsListGroupsIndex"_s, 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.