Skip to content
Permalink
Browse files

[layers-panel] Changes to layer mark in layer tree view

Avoid flicker when scrolling horizontally the Layer Tree View
Only show the layer mark when the node icon is not visible anymore
  • Loading branch information
gacarrillor committed Jun 2, 2020
1 parent 1f565a3 commit c6b7aed81e14b9ecffbb8cc1ac2a14e97b764128
@@ -29,6 +29,7 @@
#include <QMenu>
#include <QContextMenuEvent>
#include <QHeaderView>
#include <QScrollBar>

#include "qgslayertreeviewindicator.h"
#include "qgslayertreeviewitemdelegate.h"
@@ -64,6 +65,8 @@ QgsLayerTreeView::QgsLayerTreeView( QWidget *parent )

connect( this, &QTreeView::collapsed, this, &QgsLayerTreeView::updateExpandedStateToNode );
connect( this, &QTreeView::expanded, this, &QgsLayerTreeView::updateExpandedStateToNode );

connect( horizontalScrollBar(), &QScrollBar::valueChanged, this, &QgsLayerTreeView::onHorizontalScroll );
}

QgsLayerTreeView::~QgsLayerTreeView()
@@ -577,3 +580,9 @@ void QgsLayerTreeView::resizeEvent( QResizeEvent *event )
header()->setMinimumSectionSize( viewport()->width() );
QTreeView::resizeEvent( event );
}

void QgsLayerTreeView::onHorizontalScroll( int value )
{
Q_UNUSED( value )
viewport()->update();
}
@@ -245,6 +245,8 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView

private slots:
void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
//! Handles updating the viewport to avoid flicker
void onHorizontalScroll( int value );

protected:
//! helper class with default actions. Lazily initialized.
@@ -81,26 +81,29 @@ void QgsLayerTreeViewItemDelegate::paint( QPainter *painter, const QStyleOptionV
QStyleOptionViewItem opt = option;
initStyleOption( &opt, index );

QRect tRect = mLayerTreeView->style()->subElementRect( QStyle::SE_ItemViewItemText, &opt, mLayerTreeView );
int tPadding = tRect.height() / 10;

// Draw layer context menu mark
QRect mRect( mLayerTreeView->viewport()->rect().right() - mLayerTreeView->layerMarkWidth(), tRect.top() + tPadding, mLayerTreeView->layerMarkWidth(), tRect.height() - tPadding * 2 );
QBrush pb = painter->brush();
QPen pp = painter->pen();
painter->setPen( QPen( Qt::NoPen ) );
QBrush b = QBrush( opt.palette.mid() );
QColor bc = b.color();
// mix mid color with base color for a less dominant, yet still opaque, version of the color
const QColor baseColor = opt.palette.base().color();
bc.setRed( static_cast< int >( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
bc.setGreen( static_cast< int >( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
bc.setBlue( static_cast< int >( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
b.setColor( bc );
painter->setBrush( b );
painter->drawRect( mRect );
painter->setBrush( pb );
painter->setPen( pp );
QRect tRect = mLayerTreeView->style()->subElementRect( QStyle::SE_ItemViewItemText, &opt, mLayerTreeView );

const bool shouldShowLayerMark = tRect.left() < 0; // Layer/group node icon not visible anymore?
if ( shouldShowLayerMark )
{
int tPadding = tRect.height() / 10;
QRect mRect( mLayerTreeView->viewport()->rect().right() - mLayerTreeView->layerMarkWidth(), tRect.top() + tPadding, mLayerTreeView->layerMarkWidth(), tRect.height() - tPadding * 2 );
QBrush pb = painter->brush();
QPen pp = painter->pen();
painter->setPen( QPen( Qt::NoPen ) );
QBrush b = QBrush( opt.palette.mid() );
QColor bc = b.color();
// mix mid color with base color for a less dominant, yet still opaque, version of the color
bc.setRed( static_cast< int >( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
bc.setGreen( static_cast< int >( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
bc.setBlue( static_cast< int >( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
b.setColor( bc );
painter->setBrush( b );
painter->drawRect( mRect );
painter->setBrush( pb );
painter->setPen( pp );
}

const QList<QgsLayerTreeViewIndicator *> indicators = mLayerTreeView->indicators( node );
if ( indicators.isEmpty() )

0 comments on commit c6b7aed

Please sign in to comment.
You can’t perform that action at this time.