Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 c6b7aed
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/gui/layertree/qgslayertreeview.cpp
Expand Up @@ -29,6 +29,7 @@
#include <QMenu>
#include <QContextMenuEvent>
#include <QHeaderView>
#include <QScrollBar>

#include "qgslayertreeviewindicator.h"
#include "qgslayertreeviewitemdelegate.h"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
}
2 changes: 2 additions & 0 deletions src/gui/layertree/qgslayertreeview.h
Expand Up @@ -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.
Expand Down
41 changes: 22 additions & 19 deletions src/gui/layertree/qgslayertreeviewitemdelegate.cpp
Expand Up @@ -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() )
Expand Down

0 comments on commit c6b7aed

Please sign in to comment.