28#include "moc_qgslayertreeviewitemdelegate.cpp"
32QgsLayerTreeViewProxyStyle::QgsLayerTreeViewProxyStyle(
QgsLayerTreeView *treeView )
34 , mLayerTreeView( treeView )
39QRect QgsLayerTreeViewProxyStyle::subElementRect( QStyle::SubElement element,
const QStyleOption *option,
const QWidget *widget )
const
41 if ( element == SE_LayerTreeItemIndicator )
43 if (
const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>( option ) )
47 const int count = mLayerTreeView->indicators( node ).count();
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() );
60 return QProxyStyle::subElementRect( element, option, widget );
67QgsLayerTreeViewItemDelegate::QgsLayerTreeViewItemDelegate(
QgsLayerTreeView *parent )
68 : QStyledItemDelegate( parent )
69 , mLayerTreeView( parent )
71 connect( mLayerTreeView, &QgsLayerTreeView::clicked,
this, &QgsLayerTreeViewItemDelegate::onClicked );
75void QgsLayerTreeViewItemDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
77 QStyledItemDelegate::paint( painter, option, index );
83 QStyleOptionViewItem opt = option;
84 initStyleOption( &opt, index );
86 const QColor baseColor = opt.palette.base().color();
87 const QRect tRect = mLayerTreeView->style()->subElementRect( QStyle::SE_ItemViewItemText, &opt, mLayerTreeView );
89 const bool shouldShowLayerMark = tRect.left() < 0;
90 if ( shouldShowLayerMark )
92 const int tPadding = tRect.height() / 10;
93 const QRect mRect( mLayerTreeView->viewport()->rect().right() - mLayerTreeView->layerMarkWidth(), tRect.top() + tPadding, mLayerTreeView->layerMarkWidth(), tRect.height() - tPadding * 2 );
94 const QBrush pb = painter->brush();
95 const QPen pp = painter->pen();
96 painter->setPen( QPen( Qt::NoPen ) );
97 QBrush b = QBrush( opt.palette.mid() );
98 QColor bc = b.color();
100 bc.setRed(
static_cast<int>( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
101 bc.setGreen(
static_cast<int>( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
102 bc.setBlue(
static_cast<int>( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
104 painter->setBrush( b );
105 painter->drawRect( mRect );
106 painter->setBrush( pb );
107 painter->setPen( pp );
110 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
111 if ( indicators.isEmpty() )
114 const QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
115 const int spacing = indRect.height() / 10;
116 const int h = indRect.height();
117 int x = indRect.left();
121 const QRect rect( x + spacing, indRect.top() + spacing, h - spacing * 2, h - spacing * 2 );
123 const QRect iconRect( x + spacing * 2, indRect.top() + spacing * 2, h - spacing * 4, h - spacing * 4 );
126 QIcon::Mode mode = QIcon::Normal;
127 if ( !( opt.state & QStyle::State_Enabled ) )
128 mode = QIcon::Disabled;
129 else if ( opt.state & QStyle::State_Selected )
130 mode = QIcon::Selected;
133 const qreal bradius = spacing;
134 const QBrush pb = painter->brush();
135 const QPen pp = painter->pen();
136 QBrush b = QBrush( opt.palette.midlight() );
137 QColor bc = b.color();
138 bc.setRed(
static_cast<int>( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
139 bc.setGreen(
static_cast<int>( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
140 bc.setBlue(
static_cast<int>( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
142 painter->setBrush( b );
143 painter->setPen( QPen( QBrush( opt.palette.mid() ), 0.25 ) );
144 painter->drawRoundedRect( rect, bradius, bradius );
145 painter->setBrush( pb );
146 painter->setPen( pp );
148 indicator->icon().paint( painter, iconRect, Qt::AlignCenter, mode );
152static void _fixStyleOption( QStyleOptionViewItem &opt )
160 opt.showDecorationSelected =
true;
163bool QgsLayerTreeViewItemDelegate::helpEvent( QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index )
165 if ( event && event->type() == QEvent::ToolTip )
170 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
171 if ( !indicators.isEmpty() )
173 QStyleOptionViewItem opt = option;
174 initStyleOption( &opt, index );
175 _fixStyleOption( opt );
177 const QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
179 if ( indRect.contains( event->pos() ) )
181 const int indicatorIndex = (
event->pos().x() - indRect.left() ) / indRect.height();
182 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
184 const QString tooltip = indicators[indicatorIndex]->toolTip();
185 if ( !tooltip.isEmpty() )
187 QToolTip::showText( event->globalPos(), tooltip, view );
195 return QStyledItemDelegate::helpEvent( event, view, option, index );
199void QgsLayerTreeViewItemDelegate::onClicked(
const QModelIndex &index )
205 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
206 if ( indicators.isEmpty() )
209#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
210 QStyleOptionViewItem opt( mLayerTreeView->viewOptions() );
212 QStyleOptionViewItem opt;
213 mLayerTreeView->initViewItemOption( &opt );
215 opt.rect = mLayerTreeView->visualRect( index );
216 initStyleOption( &opt, index );
217 _fixStyleOption( opt );
219 const QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
221 const QPoint pos = mLayerTreeView->mLastReleaseMousePos;
222 if ( indRect.contains( pos ) )
224 const int indicatorIndex = ( pos.x() - indRect.left() ) / indRect.height();
225 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
226 emit indicators[indicatorIndex]->clicked( index );
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.
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,...