29#include "moc_qgslayertreeviewitemdelegate.cpp"
33QgsLayerTreeViewProxyStyle::QgsLayerTreeViewProxyStyle(
QgsLayerTreeView *treeView )
35 , 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 );
63void QgsLayerTreeViewProxyStyle::drawPrimitive( QStyle::PrimitiveElement element,
const QStyleOption *option, QPainter *painter,
const QWidget *widget )
const
65 if ( element == QStyle::PE_IndicatorItemViewItemCheck )
67 const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>( option );
77 QgsProxyStyle::drawPrimitive( QStyle::PE_IndicatorRadioButton, option, painter, widget );
83 QgsProxyStyle::drawPrimitive( element, option, painter, widget );
90QgsLayerTreeViewItemDelegate::QgsLayerTreeViewItemDelegate(
QgsLayerTreeView *parent )
91 : QStyledItemDelegate( parent )
92 , mLayerTreeView( parent )
94 connect( mLayerTreeView, &QgsLayerTreeView::clicked,
this, &QgsLayerTreeViewItemDelegate::onClicked );
98void QgsLayerTreeViewItemDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
100 QStyledItemDelegate::paint( painter, option, index );
106 QStyleOptionViewItem opt = option;
107 initStyleOption( &opt, index );
109 const QColor baseColor = opt.palette.base().color();
110 const QRect tRect = mLayerTreeView->style()->subElementRect( QStyle::SE_ItemViewItemText, &opt, mLayerTreeView );
112 const bool shouldShowLayerMark = tRect.left() < 0;
113 if ( shouldShowLayerMark )
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();
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 ) );
127 painter->setBrush( b );
128 painter->drawRect( mRect );
129 painter->setBrush( pb );
130 painter->setPen( pp );
133 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
134 if ( indicators.isEmpty() )
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();
144 const QRect rect( x + spacing, indRect.top() + spacing, h - spacing * 2, h - spacing * 2 );
146 const QRect iconRect( x + spacing * 2, indRect.top() + spacing * 2, h - spacing * 4, h - spacing * 4 );
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;
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 ) );
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 );
171 indicator->icon().paint( painter, iconRect, Qt::AlignCenter, mode );
175static void _fixStyleOption( QStyleOptionViewItem &opt )
183 opt.showDecorationSelected =
true;
186bool QgsLayerTreeViewItemDelegate::helpEvent( QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index )
188 if ( event && event->type() == QEvent::ToolTip )
193 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
194 if ( !indicators.isEmpty() )
196 QStyleOptionViewItem opt = option;
197 initStyleOption( &opt, index );
198 _fixStyleOption( opt );
200 const QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
202 if ( indRect.contains( event->pos() ) )
204 const int indicatorIndex = (
event->pos().x() - indRect.left() ) / indRect.height();
205 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
207 const QString tooltip = indicators[indicatorIndex]->toolTip();
208 if ( !tooltip.isEmpty() )
210 QToolTip::showText( event->globalPos(), tooltip, view );
218 return QStyledItemDelegate::helpEvent( event, view, option, index );
222void QgsLayerTreeViewItemDelegate::onClicked(
const QModelIndex &index )
228 const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
229 if ( indicators.isEmpty() )
232 QStyleOptionViewItem opt;
233 mLayerTreeView->initViewItemOption( &opt );
234 opt.rect = mLayerTreeView->visualRect( index );
235 initStyleOption( &opt, index );
236 _fixStyleOption( opt );
238 const QRect indRect = mLayerTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
240 const QPoint pos = mLayerTreeView->mLastReleaseMousePos;
241 if ( indRect.contains( pos ) )
243 const int indicatorIndex = ( pos.x() - indRect.left() ) / indRect.height();
244 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
245 emit indicators[indicatorIndex]->clicked( index );
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,...