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 const int count = mLayerTreeView->indicators( node ).count();
48 const QRect vpr = mLayerTreeView->viewport()->rect();
49 const QRect r = QProxyStyle::subElementRect( SE_ItemViewItemText, option, widget );
50 const int indiWidth = r.height() * count;
51 const int spacing = r.height() / 10;
52 const 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 const QRect tRect = mLayerTreeView->style()->subElementRect( QStyle::SE_ItemViewItemText, &opt, mLayerTreeView );
87 const bool shouldShowLayerMark = tRect.left() < 0;
88 if ( shouldShowLayerMark )
90 const int tPadding = tRect.height() / 10;
91 const QRect mRect( mLayerTreeView->viewport()->rect().right() - mLayerTreeView->layerMarkWidth(), tRect.top() + tPadding, mLayerTreeView->layerMarkWidth(), tRect.height() - tPadding * 2 );
92 const QBrush pb = painter->brush();
93 const 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 const QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
113 const int spacing = indRect.height() / 10;
114 const int h = indRect.height();
115 int x = indRect.left();
119 const QRect rect( x + spacing, indRect.top() + spacing, h - spacing * 2, h - spacing * 2 );
121 const 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 const qreal bradius = spacing;
132 const QBrush pb = painter->brush();
133 const 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 const QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
177 if ( indRect.contains( event->pos() ) )
179 const 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 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
208 QStyleOptionViewItem opt( mLayerTreeView->viewOptions() );
210 QStyleOptionViewItem opt;
211 mLayerTreeView->initViewItemOption( &opt );
213 opt.rect = mLayerTreeView->visualRect( index );
214 initStyleOption( &opt, index );
215 _fixStyleOption( opt );
217 const QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
219 const QPoint pos = mLayerTreeView->mLastReleaseMousePos;
220 if ( indRect.contains( pos ) )
222 const int indicatorIndex = ( pos.x() - indRect.left() ) / indRect.height();
223 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
224 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,...