30#include "moc_qgsstyleitemslistwidget.cpp"
32using namespace Qt::StringLiterals;
39QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyleModel *sourceModel, QObject *parent )
44QgsReadOnlyStyleModel::QgsReadOnlyStyleModel(
QgsStyle *style, QObject *parent )
54Qt::ItemFlags QgsReadOnlyStyleModel::flags(
const QModelIndex &index )
const
56 return QgsStyleProxyModel::flags( index ) & ~Qt::ItemIsEditable;
59QVariant QgsReadOnlyStyleModel::data(
const QModelIndex &index,
int role )
const
61 if ( role == Qt::FontRole )
64 QFont f = QgsStyleProxyModel::data( index, role ).value<QFont>();
69 return QgsStyleProxyModel::data( index, role );
77QgsStyleModelDelegate::QgsStyleModelDelegate( QObject *parent )
78 : QStyledItemDelegate( parent )
82QSize QgsStyleModelDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
84 if (
const QListView *view = qobject_cast<const QListView *>( option.widget ) )
89 QFont f = option.font;
90 f.setPointSizeF( f.pointSizeF() * 1.4 );
91 const QFontMetrics fm( f );
92 return QSize( option.widget->width() - view->verticalScrollBar()->width() * 2, fm.height() );
98 const QSize
iconSize = option.decorationSize;
99 return QSize(
static_cast<int>(
iconSize.width() * 1.4 ),
static_cast<int>(
iconSize.height() * 1.7 ) );
102 else if ( qobject_cast<const QTreeView *>( option.widget ) )
106 QSize defaultSize = QStyledItemDelegate::sizeHint( option, index );
108 return QSize( defaultSize.width(),
static_cast<int>( defaultSize.height() * 1.2 ) );
112 return QStyledItemDelegate::sizeHint( option, index );
115void QgsStyleModelDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
119 QStyleOptionViewItem titleOption( option );
120 initStyleOption( &titleOption, index );
121 if ( qobject_cast<const QListView *>( option.widget ) )
123 titleOption.font.setBold(
true );
124 titleOption.font.setPointSizeF( titleOption.font.pointSizeF() * 1.4 );
127 painter->setBrush( titleOption.palette.windowText() );
128 painter->setFont( titleOption.font );
129 const QRect rect = QRect( titleOption.rect.left(), titleOption.rect.top(), titleOption.rect.width(), titleOption.rect.height() );
131 painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, index.data( Qt::DisplayRole ).toString() );
132 painter->setBrush( Qt::NoBrush );
133 QColor lineColor = titleOption.palette.windowText().color();
134 lineColor.setAlpha( 100 );
135 painter->setPen( QPen( lineColor, 1 ) );
136 painter->drawLine( titleOption.rect.left(), titleOption.rect.bottom(), titleOption.rect.right(), titleOption.rect.bottom() );
140 else if ( qobject_cast<const QTreeView *>( option.widget ) )
143 QColor lineColor = option.palette.windowText().color();
144 lineColor.setAlpha( 100 );
145 painter->setPen( QPen( lineColor, 1 ) );
147 QFont f = option.font;
150 titleOption.font = f;
151 titleOption.fontMetrics = QFontMetrics( titleOption.font );
153 painter->drawLine( index.column() == 0 ? 0 : option.rect.left(), option.rect.bottom(), index.column() == 0 ? option.rect.right() : option.widget->width(), option.rect.bottom() );
156 titleOption.state |= QStyle::State_Enabled;
157 QStyledItemDelegate::paint( painter, titleOption, index );
162 QStyledItemDelegate::paint( painter, option, index );
178 mDelegate =
new QgsStyleModelDelegate(
this );
181 btnAdvanced->setMenu(
new QMenu(
this ) );
184 viewSymbols->setIconSize( QSize(
static_cast<int>( iconSize ),
static_cast<int>( iconSize * 0.9 ) ) );
187 mSymbolTreeView->setIconSize( QSize(
static_cast<int>( treeIconSize ),
static_cast<int>( treeIconSize ) ) );
188 mSymbolTreeView->setMinimumHeight( mSymbolTreeView->fontMetrics().height() * 6 );
190 viewSymbols->setItemDelegate( mDelegate );
191 mSymbolTreeView->setItemDelegate( mDelegate );
193 viewSymbols->setSelectionBehavior( QAbstractItemView::SelectRows );
194 mSymbolTreeView->setSelectionMode( viewSymbols->selectionMode() );
196 connect( openStyleManagerButton, &QToolButton::clicked,
this, &QgsStyleItemsListWidget::openStyleManager );
198 lblSymbolName->clear();
200 connect( mButtonIconView, &QToolButton::toggled,
this, [
this](
bool active ) {
203 mSymbolViewStackedWidget->setCurrentIndex( 0 );
208 connect( mButtonListView, &QToolButton::toggled,
this, [
this](
bool active ) {
212 mSymbolViewStackedWidget->setCurrentIndex( 1 );
218 const int currentView = settings.
value( u
"UI/symbolsList/lastIconView"_s, 0,
QgsSettings::Gui ).toInt();
219 if ( currentView == 0 )
220 mButtonIconView->setChecked(
true );
222 mButtonListView->setChecked(
true );
224 mSymbolTreeView->header()->restoreState( settings.value( u
"UI/symbolsList/treeState"_s, QByteArray(),
QgsSettings::Gui ).toByteArray() );
225 connect( mSymbolTreeView->header(), &QHeaderView::sectionResized,
this, [
this] {
227 QgsSettings().setValue( u
"UI/symbolsList/treeState"_s, mSymbolTreeView->header()->saveState(), QgsSettings::Gui );
233 groupEdit->setPlaceholderText( tr(
"Filter symbols…" ) );
234 groupsCombo->setLineEdit( groupEdit );
244 :
new QgsReadOnlyStyleModel( mStyle,
this );
246 mModel->addDesiredIconSize( viewSymbols->iconSize() );
247 mModel->addDesiredIconSize( mSymbolTreeView->iconSize() );
251 viewSymbols->setTextElideMode( Qt::TextElideMode::ElideRight );
253 viewSymbols->setModel( mModel );
254 mSymbolTreeView->setModel( mModel );
258 mSymbolTreeView->setSelectionModel( viewSymbols->selectionModel() );
259 connect( viewSymbols->selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsStyleItemsListWidget::onSelectionChanged );
260 connect( viewSymbols, &QListView::activated,
this, [
this](
const QModelIndex &index ) {
261 onSelectionChanged( index, QModelIndex() );
263 connect( mSymbolTreeView, &QTreeView::activated,
this, [
this](
const QModelIndex &index ) {
264 onSelectionChanged( index, QModelIndex() );
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( u
"UI/symbolsList/treeState"_s, QByteArray(),
QgsSettings::Gui ).toByteArray() );
277 mModel->setEntityFilterEnabled(
true );
278 mModel->setEntityFilter( type );
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" ) );
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" ) );
348 mModel->setSymbolTypeFilterEnabled(
true );
349 mModel->setSymbolType( type );
354 mModel->setLayerType( type );
359 return groupsCombo->currentData().toString() ==
"tag"_L1 ? 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( u
"UI/symbolsList/treeState"_s, 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" ) );
476 QStringList groups = mStyle->smartgroupNames();
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 );
489 const QgsSettings settings;
490 index = settings.
value( u
"qgis/symbolsListGroupsIndex"_s, 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() );
508 mModel->setFavoritesOnly(
false );
509 mModel->setTagString( QString() );
510 mModel->setSmartGroupId( -1 );
511 mModel->setFilterString( groupsCombo->currentText() );
513 else if ( groupsCombo->currentData().toString() ==
"favorite"_L1 )
515 mModel->setFavoritesOnly(
true );
516 mModel->setTagString( QString() );
517 mModel->setSmartGroupId( -1 );
518 mModel->setFilterString( QString() );
520 else if ( groupsCombo->currentData().toString() ==
"all"_L1 )
522 mModel->setFavoritesOnly(
false );
523 mModel->setTagString( QString() );
524 mModel->setSmartGroupId( -1 );
525 mModel->setFilterString( QString() );
527 else if ( groupsCombo->currentData().toString() ==
"smartgroup"_L1 )
529 mModel->setFavoritesOnly(
false );
530 mModel->setTagString( QString() );
531 mModel->setSmartGroupId( mStyle->smartgroupId( text ) );
532 mModel->setFilterString( QString() );
536 mModel->setFavoritesOnly(
false );
537 mModel->setTagString( text );
538 mModel->setSmartGroupId( -1 );
539 mModel->setFilterString( QString() );
543void QgsStyleItemsListWidget::openStyleManager()
556 std::unique_ptr< QgsStyleManagerDialog > dlg;
559 dlg = std::make_unique< QgsStyleManagerDialog >( mStyle,
this );
563 dlg = std::make_unique< QgsStyleManagerDialog >(
this );
567 updateModelFilters();
571void QgsStyleItemsListWidget::onSelectionChanged(
const QModelIndex &index,
const QModelIndex &previous )
576 if ( index.row() == previous.row() )
579 const QString symbolName = mModel->data( mModel->index( index.row(),
QgsStyleModel::Name ) ).toString();
580 lblSymbolName->setText( symbolName );
588void QgsStyleItemsListWidget::groupsCombo_currentIndexChanged(
int index )
590 QgsSettings settings;
591 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.