30 QgsLayerTreeViewProxyStyle::QgsLayerTreeViewProxyStyle(
QgsLayerTreeView *treeView )
32 , mLayerTreeView( treeView )
37 QRect QgsLayerTreeViewProxyStyle::subElementRect( QStyle::SubElement element,
const QStyleOption *option,
const QWidget *widget )
const 39 if ( element == SE_LayerTreeItemIndicator )
41 if (
const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>( option ) )
43 if (
QgsLayerTreeNode *node = mLayerTreeView->layerTreeModel()->index2node( vopt->index ) )
45 int count = mLayerTreeView->indicators( node ).count();
48 QRect vpr = mLayerTreeView->viewport()->rect();
49 QRect r = QProxyStyle::subElementRect( SE_ItemViewItemText, option, widget );
50 int indiWidth = r.height() * count;
51 int spacing = r.height() / 10;
52 int vpIndiWidth = vpr.width() - indiWidth - spacing - mLayerTreeView->layerMarkWidth();
53 return QRect( vpIndiWidth, r.top(), indiWidth, r.height() );
58 return QProxyStyle::subElementRect( element, option, widget );
65 QgsLayerTreeViewItemDelegate::QgsLayerTreeViewItemDelegate(
QgsLayerTreeView *parent )
66 : QStyledItemDelegate( parent )
67 , mLayerTreeView( parent )
69 connect( mLayerTreeView, &QgsLayerTreeView::clicked,
this, &QgsLayerTreeViewItemDelegate::onClicked );
73 void QgsLayerTreeViewItemDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const 75 QStyledItemDelegate::paint( painter, option, index );
77 QgsLayerTreeNode *node = mLayerTreeView->layerTreeModel()->index2node( index );
81 QStyleOptionViewItem opt = option;
82 initStyleOption( &opt, index );
84 QRect tRect = mLayerTreeView->style()->subElementRect( QStyle::SE_ItemViewItemText, &opt, mLayerTreeView );
85 int tPadding = tRect.height() / 10;
88 QRect mRect( mLayerTreeView->viewport()->rect().right() - mLayerTreeView->layerMarkWidth(), tRect.top() + tPadding, mLayerTreeView->layerMarkWidth(), tRect.height() - tPadding * 2 );
89 QBrush pb = painter->brush();
90 QPen pp = painter->pen();
91 painter->setPen( QPen( Qt::NoPen ) );
92 QBrush b = QBrush( opt.palette.mid() );
93 QColor bc = b.color();
95 const QColor baseColor = opt.palette.base().color();
96 bc.setRed( static_cast< int >( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
97 bc.setGreen( static_cast< int >( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
98 bc.setBlue( static_cast< int >( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
100 painter->setBrush( b );
101 painter->drawRect( mRect );
102 painter->setBrush( pb );
103 painter->setPen( pp );
105 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
106 if ( indicators.isEmpty() )
109 QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
110 int spacing = indRect.height() / 10;
111 int h = indRect.height();
112 int x = indRect.left();
116 QRect rect( x + spacing, indRect.top() + spacing, h - spacing * 2, h - spacing * 2 );
118 QRect iconRect( x + spacing * 2, indRect.top() + spacing * 2, h - spacing * 4, h - spacing * 4 );
121 QIcon::Mode mode = QIcon::Normal;
122 if ( !( opt.state & QStyle::State_Enabled ) )
123 mode = QIcon::Disabled;
124 else if ( opt.state & QStyle::State_Selected )
125 mode = QIcon::Selected;
128 qreal bradius = spacing;
129 QBrush pb = painter->brush();
130 QPen pp = painter->pen();
131 QBrush b = QBrush( opt.palette.midlight() );
132 QColor bc = b.color();
133 bc.setRed( static_cast< int >( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
134 bc.setGreen( static_cast< int >( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
135 bc.setBlue( static_cast< int >( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
137 painter->setBrush( b );
138 painter->setPen( QPen( QBrush( opt.palette.mid() ), 0.25 ) );
139 painter->drawRoundedRect( rect, bradius, bradius );
140 painter->setBrush( pb );
141 painter->setPen( pp );
143 indicator->icon().paint( painter, iconRect, Qt::AlignCenter, mode );
147 static void _fixStyleOption( QStyleOptionViewItem &opt )
155 opt.showDecorationSelected =
true;
158 bool QgsLayerTreeViewItemDelegate::helpEvent( QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index )
160 if ( event && event->type() == QEvent::ToolTip )
162 QgsLayerTreeNode *node = mLayerTreeView->layerTreeModel()->index2node( index );
165 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
166 if ( !indicators.isEmpty() )
168 QStyleOptionViewItem opt = option;
169 initStyleOption( &opt, index );
170 _fixStyleOption( opt );
172 QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
174 if ( indRect.contains( event->pos() ) )
176 int indicatorIndex = (
event->pos().x() - indRect.left() ) / indRect.height();
177 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
179 const QString tooltip = indicators[indicatorIndex]->toolTip();
180 if ( !tooltip.isEmpty() )
182 QToolTip::showText( event->globalPos(), tooltip, view );
190 return QStyledItemDelegate::helpEvent( event, view, option, index );
194 void QgsLayerTreeViewItemDelegate::onClicked(
const QModelIndex &index )
196 QgsLayerTreeNode *node = mLayerTreeView->layerTreeModel()->index2node( index );
200 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
201 if ( indicators.isEmpty() )
204 QStyleOptionViewItem opt( mLayerTreeView->viewOptions() );
205 opt.rect = mLayerTreeView->visualRect( index );
206 initStyleOption( &opt, index );
207 _fixStyleOption( opt );
209 QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
211 QPoint pos = mLayerTreeView->mLastReleaseMousePos;
212 if ( indRect.contains( pos ) )
214 int indicatorIndex = ( pos.x() - indRect.left() ) / indRect.height();
215 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
216 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...