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 ) )
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 );
81 QStyleOptionViewItem opt = option;
82 initStyleOption( &opt, index );
84 const QColor baseColor = opt.palette.base().color();
85 QRect tRect = mLayerTreeView->style()->subElementRect( QStyle::SE_ItemViewItemText, &opt, mLayerTreeView );
87 const bool shouldShowLayerMark = tRect.left() < 0;
88 if ( shouldShowLayerMark )
90 int tPadding = tRect.height() / 10;
91 QRect mRect( mLayerTreeView->viewport()->rect().right() - mLayerTreeView->layerMarkWidth(), tRect.top() + tPadding, mLayerTreeView->layerMarkWidth(), tRect.height() - tPadding * 2 );
92 QBrush pb = painter->brush();
93 QPen pp = painter->pen();
94 painter->setPen( QPen( Qt::NoPen ) );
95 QBrush b = QBrush( opt.palette.mid() );
96 QColor bc = b.color();
98 bc.setRed(
static_cast< int >( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
99 bc.setGreen(
static_cast< int >( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
100 bc.setBlue(
static_cast< int >( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
102 painter->setBrush( b );
103 painter->drawRect( mRect );
104 painter->setBrush( pb );
105 painter->setPen( pp );
108 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
109 if ( indicators.isEmpty() )
112 QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
113 int spacing = indRect.height() / 10;
114 int h = indRect.height();
115 int x = indRect.left();
119 QRect rect( x + spacing, indRect.top() + spacing, h - spacing * 2, h - spacing * 2 );
121 QRect iconRect( x + spacing * 2, indRect.top() + spacing * 2, h - spacing * 4, h - spacing * 4 );
124 QIcon::Mode mode = QIcon::Normal;
125 if ( !( opt.state & QStyle::State_Enabled ) )
126 mode = QIcon::Disabled;
127 else if ( opt.state & QStyle::State_Selected )
128 mode = QIcon::Selected;
131 qreal bradius = spacing;
132 QBrush pb = painter->brush();
133 QPen pp = painter->pen();
134 QBrush b = QBrush( opt.palette.midlight() );
135 QColor bc = b.color();
136 bc.setRed(
static_cast< int >( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
137 bc.setGreen(
static_cast< int >( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
138 bc.setBlue(
static_cast< int >( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
140 painter->setBrush( b );
141 painter->setPen( QPen( QBrush( opt.palette.mid() ), 0.25 ) );
142 painter->drawRoundedRect( rect, bradius, bradius );
143 painter->setBrush( pb );
144 painter->setPen( pp );
146 indicator->icon().paint( painter, iconRect, Qt::AlignCenter, mode );
150 static void _fixStyleOption( QStyleOptionViewItem &opt )
158 opt.showDecorationSelected =
true;
161 bool QgsLayerTreeViewItemDelegate::helpEvent( QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index )
163 if ( event && event->type() == QEvent::ToolTip )
168 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
169 if ( !indicators.isEmpty() )
171 QStyleOptionViewItem opt = option;
172 initStyleOption( &opt, index );
173 _fixStyleOption( opt );
175 QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
177 if ( indRect.contains( event->pos() ) )
179 int indicatorIndex = (
event->pos().x() - indRect.left() ) / indRect.height();
180 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
182 const QString tooltip = indicators[indicatorIndex]->toolTip();
183 if ( !tooltip.isEmpty() )
185 QToolTip::showText( event->globalPos(), tooltip, view );
193 return QStyledItemDelegate::helpEvent( event, view, option, index );
197 void QgsLayerTreeViewItemDelegate::onClicked(
const QModelIndex &index )
203 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
204 if ( indicators.isEmpty() )
207 QStyleOptionViewItem opt( mLayerTreeView->viewOptions() );
208 opt.rect = mLayerTreeView->visualRect( index );
209 initStyleOption( &opt, index );
210 _fixStyleOption( opt );
212 QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
214 QPoint pos = mLayerTreeView->mLastReleaseMousePos;
215 if ( indRect.contains( pos ) )
217 int indicatorIndex = ( pos.x() - indRect.left() ) / indRect.height();
218 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
219 emit indicators[indicatorIndex]->clicked( index );
This class is a base class for nodes in a layer tree.
Indicator that can be used in a layer tree view to display icons next to items of the layer tree.
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,...