17#include "moc_qgslayertreeviewitemdelegate.cpp" 
   31QgsLayerTreeViewProxyStyle::QgsLayerTreeViewProxyStyle( 
QgsLayerTreeView *treeView )
 
   33  , mLayerTreeView( treeView )
 
   38QRect QgsLayerTreeViewProxyStyle::subElementRect( QStyle::SubElement element, 
const QStyleOption *option, 
const QWidget *widget )
 const 
   40  if ( element == SE_LayerTreeItemIndicator )
 
   42    if ( 
const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>( option ) )
 
   46        const int count = mLayerTreeView->indicators( node ).count();
 
   49          const QRect vpr = mLayerTreeView->viewport()->rect();
 
   50          const QRect r = QProxyStyle::subElementRect( SE_ItemViewItemText, option, widget );
 
   51          const int indiWidth = r.height() * count;
 
   52          const int spacing = r.height() / 10;
 
   53          const int vpIndiWidth = vpr.width() - indiWidth - spacing - mLayerTreeView->layerMarkWidth();
 
   54          return QRect( vpIndiWidth, r.top(), indiWidth, r.height() );
 
   59  return QProxyStyle::subElementRect( element, option, widget );
 
   66QgsLayerTreeViewItemDelegate::QgsLayerTreeViewItemDelegate( 
QgsLayerTreeView *parent )
 
   67  : QStyledItemDelegate( parent )
 
   68  , mLayerTreeView( parent )
 
   70  connect( mLayerTreeView, &QgsLayerTreeView::clicked, 
this, &QgsLayerTreeViewItemDelegate::onClicked );
 
   74void QgsLayerTreeViewItemDelegate::paint( QPainter *painter, 
const QStyleOptionViewItem &option, 
const QModelIndex &index )
 const 
   76  QStyledItemDelegate::paint( painter, option, index );
 
   82  QStyleOptionViewItem opt = option;
 
   83  initStyleOption( &opt, index );
 
   85  const QColor baseColor = opt.palette.base().color();
 
   86  const QRect tRect = mLayerTreeView->style()->subElementRect( QStyle::SE_ItemViewItemText, &opt, mLayerTreeView );
 
   88  const bool shouldShowLayerMark = tRect.left() < 0; 
 
   89  if ( shouldShowLayerMark )
 
   91    const int tPadding = tRect.height() / 10;
 
   92    const QRect mRect( mLayerTreeView->viewport()->rect().right() - mLayerTreeView->layerMarkWidth(), tRect.top() + tPadding, mLayerTreeView->layerMarkWidth(), tRect.height() - tPadding * 2 );
 
   93    const QBrush pb = painter->brush();
 
   94    const QPen pp = painter->pen();
 
   95    painter->setPen( QPen( Qt::NoPen ) );
 
   96    QBrush b = QBrush( opt.palette.mid() );
 
   97    QColor bc = b.color();
 
   99    bc.setRed( 
static_cast<int>( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
 
  100    bc.setGreen( 
static_cast<int>( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
 
  101    bc.setBlue( 
static_cast<int>( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
 
  103    painter->setBrush( b );
 
  104    painter->drawRect( mRect );
 
  105    painter->setBrush( pb );
 
  106    painter->setPen( pp );
 
  109  const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
 
  110  if ( indicators.isEmpty() )
 
  113  const QRect indRect = mLayerTreeView->style()->subElementRect( 
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
 
  114  const int spacing = indRect.height() / 10;
 
  115  const int h = indRect.height();
 
  116  int x = indRect.left();
 
  120    const QRect rect( x + spacing, indRect.top() + spacing, h - spacing * 2, h - spacing * 2 );
 
  122    const QRect iconRect( x + spacing * 2, indRect.top() + spacing * 2, h - spacing * 4, h - spacing * 4 );
 
  125    QIcon::Mode mode = QIcon::Normal;
 
  126    if ( !( opt.state & QStyle::State_Enabled ) )
 
  127      mode = QIcon::Disabled;
 
  128    else if ( opt.state & QStyle::State_Selected )
 
  129      mode = QIcon::Selected;
 
  132    const qreal bradius = spacing;
 
  133    const QBrush pb = painter->brush();
 
  134    const QPen pp = painter->pen();
 
  135    QBrush b = QBrush( opt.palette.midlight() );
 
  136    QColor bc = b.color();
 
  137    bc.setRed( 
static_cast<int>( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
 
  138    bc.setGreen( 
static_cast<int>( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
 
  139    bc.setBlue( 
static_cast<int>( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
 
  141    painter->setBrush( b );
 
  142    painter->setPen( QPen( QBrush( opt.palette.mid() ), 0.25 ) );
 
  143    painter->drawRoundedRect( rect, bradius, bradius );
 
  144    painter->setBrush( pb );
 
  145    painter->setPen( pp );
 
  147    indicator->icon().paint( painter, iconRect, Qt::AlignCenter, mode );
 
  151static void _fixStyleOption( QStyleOptionViewItem &opt )
 
  159  opt.showDecorationSelected = 
true;
 
  162bool QgsLayerTreeViewItemDelegate::helpEvent( QHelpEvent *event, QAbstractItemView *view, 
const QStyleOptionViewItem &option, 
const QModelIndex &index )
 
  164  if ( event && event->type() == QEvent::ToolTip )
 
  169      const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
 
  170      if ( !indicators.isEmpty() )
 
  172        QStyleOptionViewItem opt = option;
 
  173        initStyleOption( &opt, index );
 
  174        _fixStyleOption( opt );
 
  176        const QRect indRect = mLayerTreeView->style()->subElementRect( 
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
 
  178        if ( indRect.contains( event->pos() ) )
 
  180          const int indicatorIndex = ( 
event->pos().x() - indRect.left() ) / indRect.height();
 
  181          if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
 
  183            const QString tooltip = indicators[indicatorIndex]->toolTip();
 
  184            if ( !tooltip.isEmpty() )
 
  186              QToolTip::showText( event->globalPos(), tooltip, view );
 
  194  return QStyledItemDelegate::helpEvent( event, view, option, index );
 
  198void QgsLayerTreeViewItemDelegate::onClicked( 
const QModelIndex &index )
 
  204  const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
 
  205  if ( indicators.isEmpty() )
 
  208#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) 
  209  QStyleOptionViewItem opt( mLayerTreeView->viewOptions() );
 
  211  QStyleOptionViewItem opt;
 
  212  mLayerTreeView->initViewItemOption( &opt );
 
  214  opt.rect = mLayerTreeView->visualRect( index );
 
  215  initStyleOption( &opt, index );
 
  216  _fixStyleOption( opt );
 
  218  const QRect indRect = mLayerTreeView->style()->subElementRect( 
static_cast<QStyle::SubElement
>( QgsLayerTreeViewProxyStyle::SE_LayerTreeItemIndicator ), &opt, mLayerTreeView );
 
  220  const QPoint pos = mLayerTreeView->mLastReleaseMousePos;
 
  221  if ( indRect.contains( pos ) )
 
  223    const int indicatorIndex = ( pos.x() - indRect.left() ) / indRect.height();
 
  224    if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
 
  225      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,...