QGIS API Documentation 4.5.0-Master (3b5d115045b)
Loading...
Searching...
No Matches
qgslayertreeviewitemdelegate.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreeviewitemdelegate.cpp
3 --------------------------------------
4 Date : January 2018
5 Copyright : (C) 2018 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgslayertreegroup.h"
19#include "qgslayertreemodel.h"
20#include "qgslayertreeview.h"
22
23#include <QBrush>
24#include <QHelpEvent>
25#include <QMenu>
26#include <QPen>
27#include <QToolTip>
28
29#include "moc_qgslayertreeviewitemdelegate.cpp"
30
32
33QgsLayerTreeViewProxyStyle::QgsLayerTreeViewProxyStyle( QgsLayerTreeView *treeView )
34 : QgsProxyStyle( treeView )
35 , mLayerTreeView( treeView )
36{}
37
38
39QRect QgsLayerTreeViewProxyStyle::subElementRect( QStyle::SubElement element, const QStyleOption *option, const QWidget *widget ) const
40{
41 if ( element == SE_LayerTreeItemIndicator )
42 {
43 if ( const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>( option ) )
44 {
45 if ( QgsLayerTreeNode *node = mLayerTreeView->index2node( vopt->index ) )
46 {
47 const int count = mLayerTreeView->indicators( node ).count();
48 if ( count )
49 {
50 const QRect vpr = mLayerTreeView->viewport()->rect();
51 const QRect r = QProxyStyle::subElementRect( SE_ItemViewItemText, option, widget );
52 const int indiWidth = r.height() * count;
53 const int spacing = r.height() / 10;
54 const int vpIndiWidth = vpr.width() - indiWidth - spacing - mLayerTreeView->layerMarkWidth();
55 return QRect( vpIndiWidth, r.top(), indiWidth, r.height() );
56 }
57 }
58 }
59 }
60 return QProxyStyle::subElementRect( element, option, widget );
61}
62
63void QgsLayerTreeViewProxyStyle::drawPrimitive( QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget ) const
64{
65 if ( element == QStyle::PE_IndicatorItemViewItemCheck )
66 {
67 const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>( option );
68 if ( vopt )
69 {
70 QgsLayerTreeNode *node = mLayerTreeView->index2node( vopt->index );
71 if ( node )
72 {
73 QgsLayerTreeGroup *group = qobject_cast<QgsLayerTreeGroup *>( node->parent() );
74 if ( group && group->isMutuallyExclusive() )
75 {
76 // Draw a radio button instead of a checkbox if we're drawing an item inside a mutually exclusive group
77 QgsProxyStyle::drawPrimitive( QStyle::PE_IndicatorRadioButton, option, painter, widget );
78 return;
79 }
80 }
81 }
82 }
83 QgsProxyStyle::drawPrimitive( element, option, painter, widget );
84}
85
86
87// -----
88
89
90QgsLayerTreeViewItemDelegate::QgsLayerTreeViewItemDelegate( QgsLayerTreeView *parent )
91 : QStyledItemDelegate( parent )
92 , mLayerTreeView( parent )
93{
94 connect( mLayerTreeView, &QgsLayerTreeView::clicked, this, &QgsLayerTreeViewItemDelegate::onClicked );
95}
96
97
98void QgsLayerTreeViewItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
99{
100 QStyledItemDelegate::paint( painter, option, index );
101
102 QgsLayerTreeNode *node = mLayerTreeView->index2node( index );
103 if ( !node )
104 return;
105
106 QStyleOptionViewItem opt = option;
107 initStyleOption( &opt, index );
108
109 const QColor baseColor = opt.palette.base().color();
110 const QRect tRect = mLayerTreeView->style()->subElementRect( QStyle::SE_ItemViewItemText, &opt, mLayerTreeView );
111
112 const bool shouldShowLayerMark = tRect.left() < 0; // Layer/group node icon not visible anymore?
113 if ( shouldShowLayerMark )
114 {
115 const int tPadding = tRect.height() / 10;
116 const QRect mRect( mLayerTreeView->viewport()->rect().right() - mLayerTreeView->layerMarkWidth(), tRect.top() + tPadding, mLayerTreeView->layerMarkWidth(), tRect.height() - tPadding * 2 );
117 const QBrush pb = painter->brush();
118 const QPen pp = painter->pen();
119 painter->setPen( QPen( Qt::NoPen ) );
120 QBrush b = QBrush( opt.palette.mid() );
121 QColor bc = b.color();
122 // mix mid color with base color for a less dominant, yet still opaque, version of the color
123 bc.setRed( static_cast<int>( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
124 bc.setGreen( static_cast<int>( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
125 bc.setBlue( static_cast<int>( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
126 b.setColor( bc );
127 painter->setBrush( b );
128 painter->drawRect( mRect );
129 painter->setBrush( pb );
130 painter->setPen( pp );
131 }
132
133 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
134 if ( indicators.isEmpty() )
135 return;
136
137 const QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
138 const int spacing = indRect.height() / 10;
139 const int h = indRect.height();
140 int x = indRect.left();
141
142 for ( QgsLayerTreeViewIndicator *indicator : indicators )
143 {
144 const QRect rect( x + spacing, indRect.top() + spacing, h - spacing * 2, h - spacing * 2 );
145 // Add a little more padding so the icon does not look misaligned to background
146 const QRect iconRect( x + spacing * 2, indRect.top() + spacing * 2, h - spacing * 4, h - spacing * 4 );
147 x += h;
148
149 QIcon::Mode mode = QIcon::Normal;
150 if ( !( opt.state & QStyle::State_Enabled ) )
151 mode = QIcon::Disabled;
152 else if ( opt.state & QStyle::State_Selected )
153 mode = QIcon::Selected;
154
155 // Draw indicator background, for when floating over text content
156 const qreal bradius = spacing;
157 const QBrush pb = painter->brush();
158 const QPen pp = painter->pen();
159 QBrush b = QBrush( opt.palette.midlight() );
160 QColor bc = b.color();
161 bc.setRed( static_cast<int>( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
162 bc.setGreen( static_cast<int>( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
163 bc.setBlue( static_cast<int>( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
164 b.setColor( bc );
165 painter->setBrush( b );
166 painter->setPen( QPen( QBrush( opt.palette.mid() ), 0.25 ) );
167 painter->drawRoundedRect( rect, bradius, bradius );
168 painter->setBrush( pb );
169 painter->setPen( pp );
170
171 indicator->icon().paint( painter, iconRect, Qt::AlignCenter, mode );
172 }
173}
174
175static void _fixStyleOption( QStyleOptionViewItem &opt )
176{
177 // This makes sure our delegate behaves correctly across different styles. Unfortunately there is inconsistency
178 // in how QStyleOptionViewItem::showDecorationSelected is prepared for paint() vs what is returned from view's viewOptions():
179 // - viewOptions() returns it based on style's SH_ItemView_ShowDecorationSelected hint
180 // - for paint() there is extra call to QTreeViewPrivate::adjustViewOptionsForIndex() which makes it
181 // always true if view's selection behavior is SelectRows (which is the default and our case with layer tree view)
182 // So for consistency between different calls we override it to what we get in paint() method ... phew!
183 opt.showDecorationSelected = true;
184}
185
186bool QgsLayerTreeViewItemDelegate::helpEvent( QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index )
187{
188 if ( event && event->type() == QEvent::ToolTip )
189 {
190 QgsLayerTreeNode *node = mLayerTreeView->index2node( index );
191 if ( node )
192 {
193 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
194 if ( !indicators.isEmpty() )
195 {
196 QStyleOptionViewItem opt = option;
197 initStyleOption( &opt, index );
198 _fixStyleOption( opt );
199
200 const QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
201
202 if ( indRect.contains( event->pos() ) )
203 {
204 const int indicatorIndex = ( event->pos().x() - indRect.left() ) / indRect.height();
205 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
206 {
207 const QString tooltip = indicators[indicatorIndex]->toolTip();
208 if ( !tooltip.isEmpty() )
209 {
210 QToolTip::showText( event->globalPos(), tooltip, view );
211 return true;
212 }
213 }
214 }
215 }
216 }
217 }
218 return QStyledItemDelegate::helpEvent( event, view, option, index );
219}
220
221
222void QgsLayerTreeViewItemDelegate::onClicked( const QModelIndex &index )
223{
224 QgsLayerTreeNode *node = mLayerTreeView->index2node( index );
225 if ( !node )
226 return;
227
228 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
229 if ( indicators.isEmpty() )
230 return;
231
232 QStyleOptionViewItem opt;
233 mLayerTreeView->initViewItemOption( &opt );
234 opt.rect = mLayerTreeView->visualRect( index );
235 initStyleOption( &opt, index );
236 _fixStyleOption( opt );
237
238 const QRect indRect = mLayerTreeView->style()->subElementRect( static_cast<QStyle::SubElement>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
239
240 const QPoint pos = mLayerTreeView->mLastReleaseMousePos;
241 if ( indRect.contains( pos ) )
242 {
243 const int indicatorIndex = ( pos.x() - indRect.left() ) / indRect.height();
244 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
245 emit indicators[indicatorIndex]->clicked( index );
246 }
247}
248
Layer tree group node serves as a container for layers and further groups.
bool isMutuallyExclusive() const
Returns whether the group is mutually exclusive (only one child can be checked at a time).
Base class for nodes in a layer tree.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
Indicator that can be used in a layer tree view to display icons next to items of the layer tree.
Extends QTreeView and provides additional functionality when working with a layer tree.
A QProxyStyle subclass which correctly sets the base style to match the QGIS application style,...