27 QgsLayerTreeViewProxyStyle::QgsLayerTreeViewProxyStyle(
QgsLayerTreeView *treeView )
29 , mLayerTreeView( treeView )
34 QRect QgsLayerTreeViewProxyStyle::subElementRect( QStyle::SubElement element,
const QStyleOption *option,
const QWidget *widget )
const 36 if ( element == SE_ItemViewItemText || element == SE_LayerTreeItemIndicator )
38 if (
const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>( option ) )
40 if (
QgsLayerTreeNode *node = mLayerTreeView->layerTreeModel()->index2node( vopt->index ) )
42 int count = mLayerTreeView->indicators( node ).count();
45 QRect r = QProxyStyle::subElementRect( SE_ItemViewItemText, option, widget );
46 int indiWidth = r.height() * count;
47 int textWidth = r.width() - indiWidth;
48 if ( element == SE_LayerTreeItemIndicator )
50 return QRect( r.left() + textWidth, r.top(), indiWidth, r.height() );
52 else if ( element == SE_ItemViewItemText )
54 return QRect( r.left(), r.top(), textWidth, r.height() );
60 return QProxyStyle::subElementRect( element, option, widget );
67 QgsLayerTreeViewItemDelegate::QgsLayerTreeViewItemDelegate(
QgsLayerTreeView *parent )
68 : QStyledItemDelegate( parent )
69 , mLayerTreeView( parent )
71 connect( mLayerTreeView, &QgsLayerTreeView::clicked,
this, &QgsLayerTreeViewItemDelegate::onClicked );
75 void QgsLayerTreeViewItemDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const 77 QStyledItemDelegate::paint( painter, option, index );
79 QgsLayerTreeNode *node = mLayerTreeView->layerTreeModel()->index2node( index );
83 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
84 if ( indicators.isEmpty() )
87 QStyleOptionViewItem opt = option;
88 initStyleOption( &opt, index );
90 QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
91 int spacing = indRect.height() / 10;
92 int h = indRect.height();
93 int x = indRect.left();
97 QRect rect( x + spacing, indRect.top() + spacing, h - spacing * 2, h - spacing * 2 );
100 QIcon::Mode mode = QIcon::Normal;
101 if ( !( opt.state & QStyle::State_Enabled ) )
102 mode = QIcon::Disabled;
103 else if ( opt.state & QStyle::State_Selected )
104 mode = QIcon::Selected;
106 indicator->icon().paint( painter, rect, Qt::AlignCenter, mode );
110 static void _fixStyleOption( QStyleOptionViewItem &opt )
118 opt.showDecorationSelected =
true;
121 bool QgsLayerTreeViewItemDelegate::helpEvent( QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index )
123 if ( event && event->type() == QEvent::ToolTip )
125 QHelpEvent *he =
static_cast<QHelpEvent *
>( event );
127 QgsLayerTreeNode *node = mLayerTreeView->layerTreeModel()->index2node( index );
130 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
131 if ( !indicators.isEmpty() )
133 QStyleOptionViewItem opt = option;
134 initStyleOption( &opt, index );
135 _fixStyleOption( opt );
137 QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
139 if ( indRect.contains( he->pos() ) )
141 int indicatorIndex = ( he->pos().x() - indRect.left() ) / indRect.height();
142 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
144 const QString tooltip = indicators[indicatorIndex]->toolTip();
145 if ( !tooltip.isEmpty() )
147 QToolTip::showText( he->globalPos(), tooltip, view );
155 return QStyledItemDelegate::helpEvent( event, view, option, index );
159 void QgsLayerTreeViewItemDelegate::onClicked(
const QModelIndex &index )
161 QgsLayerTreeNode *node = mLayerTreeView->layerTreeModel()->index2node( index );
165 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
166 if ( indicators.isEmpty() )
169 QStyleOptionViewItem opt( mLayerTreeView->viewOptions() );
170 opt.rect = mLayerTreeView->visualRect( index );
171 initStyleOption( &opt, index );
172 _fixStyleOption( opt );
174 QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
176 QPoint pos = mLayerTreeView->mLastReleaseMousePos;
177 if ( indRect.contains( pos ) )
179 int indicatorIndex = ( pos.x() - indRect.left() ) / indRect.height();
180 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
181 emit indicators[indicatorIndex]->
clicked( index );
The QgsLayerTreeView class extends QTreeView and provides some additional functionality when working ...
A QProxyStyle subclass which correctly sets the base style to match the QGIS application style...
This class is a base class for nodes in a layer tree.
void clicked(const QModelIndex &index)
Emitted when user clicks on the indicator.
Indicator that can be used in a layer tree view to display icons next to items of the layer tree...