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 );
111 bool QgsLayerTreeViewItemDelegate::helpEvent( QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index )
113 if ( event && event->type() == QEvent::ToolTip )
115 QHelpEvent *he =
static_cast<QHelpEvent *
>( event );
117 QgsLayerTreeNode *node = mLayerTreeView->layerTreeModel()->index2node( index );
120 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
121 if ( !indicators.isEmpty() )
123 QStyleOptionViewItem opt = option;
124 initStyleOption( &opt, index );
125 QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
127 if ( indRect.contains( he->pos() ) )
129 int indicatorIndex = ( he->pos().x() - indRect.left() ) / indRect.height();
130 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
132 const QString tooltip = indicators[indicatorIndex]->toolTip();
133 if ( !tooltip.isEmpty() )
135 QToolTip::showText( he->globalPos(), tooltip, view );
143 return QStyledItemDelegate::helpEvent( event, view, option, index );
147 void QgsLayerTreeViewItemDelegate::onClicked(
const QModelIndex &index )
149 QgsLayerTreeNode *node = mLayerTreeView->layerTreeModel()->index2node( index );
153 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
154 if ( indicators.isEmpty() )
157 QStyleOptionViewItem opt( mLayerTreeView->viewOptions() );
158 opt.rect = mLayerTreeView->visualRect( index );
159 initStyleOption( &opt, index );
160 QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
162 QPoint pos = mLayerTreeView->mLastReleaseMousePos;
163 if ( indRect.contains( pos ) )
165 int indicatorIndex = ( pos.x() - indRect.left() ) / indRect.height();
166 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
167 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)
Signal that is 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...