Skip to content
Permalink
Browse files

Draw page shadow in layout units

Simplifies code a lot, pixel based size proved to complex for
small result
  • Loading branch information
nyalldawson committed Jul 22, 2017
1 parent 5cfc9cc commit 4cf9827ca2d9aa52b097489c05e9866d17d71932
@@ -79,6 +79,7 @@ class QgsLayoutItemPage : QgsLayoutItem

virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );


};

/************************************************************************
@@ -21,6 +21,16 @@ class QgsLayoutPageCollection : QObject
%End
public:

static const double SPACE_BETWEEN_PAGES;
%Docstring
Space between pages in the layout, in layout coordinates
%End

static const double PAGE_SHADOW_WIDTH;
%Docstring
Size of page shadow, in layout coordinates
%End

explicit QgsLayoutPageCollection( QgsLayout *layout /TransferThis/ );
%Docstring
Constructor for QgsLayoutItemPage, with the specified parent ``layout``.
@@ -21,21 +21,21 @@
#include "qgssymbollayerutils.h"
#include <QPainter>

#define SHADOW_WIDTH_PIXELS 5
QgsLayoutItemPage::QgsLayoutItemPage( QgsLayout *layout )
: QgsLayoutItem( layout )
{
setFlag( QGraphicsItem::ItemIsSelectable, false );
setFlag( QGraphicsItem::ItemIsMovable, false );
setZValue( QgsLayout::ZPage );

// bit hacky - we set a big hidden pen to avoid Qt clipping out the cosmetic shadow for the page
// Unfortunately it's possible to adapt the item's bounding rect based on the view's transform, so it's
// impossible to have a pixel based bounding rect. Instead we just set a big pen to force the page's
// bounding rect to be kinda large enough to handle the shadow at most zoom levels...
QPen shadowPen( QBrush( Qt::transparent ), 30 );
shadowPen.setCosmetic( true );
// use a hidden pen to specify the amount the page "bleeds" outside it's scene bounds,
// (it's a lot easier than reimplementing boundingRect() just to handle this)
QPen shadowPen( QBrush( Qt::transparent ), QgsLayoutPageCollection::PAGE_SHADOW_WIDTH * 2 );
setPen( shadowPen );

QFont font;
QFontMetrics fm( font );
mMaximumShadowWidth = fm.width( "X" );
}

void QgsLayoutItemPage::setPageSize( const QgsLayoutSize &size )
@@ -134,7 +134,8 @@ void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraph
//shadow
painter->setBrush( QBrush( QColor( 150, 150, 150 ) ) );
painter->setPen( Qt::NoPen );
painter->drawRect( pageRect.translated( SHADOW_WIDTH_PIXELS, SHADOW_WIDTH_PIXELS ) );
painter->drawRect( pageRect.translated( qMin( scale * QgsLayoutPageCollection::PAGE_SHADOW_WIDTH, mMaximumShadowWidth ),
qMin( scale * QgsLayoutPageCollection::PAGE_SHADOW_WIDTH, mMaximumShadowWidth ) ) );

//page area
painter->setBrush( QColor( 215, 215, 215 ) );
@@ -88,6 +88,11 @@ class CORE_EXPORT QgsLayoutItemPage : public QgsLayoutItem
protected:

void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;

private:

double mMaximumShadowWidth = -1;

};

#endif //QGSLAYOUTITEMPAGE_H
@@ -17,8 +17,6 @@
#include "qgslayoutpagecollection.h"
#include "qgslayout.h"

#define SPACE_BETWEEN_PAGES 10

QgsLayoutPageCollection::QgsLayoutPageCollection( QgsLayout *layout )
: QObject( layout )
, mLayout( layout )
@@ -39,6 +39,12 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject

public:

//! Space between pages in the layout, in layout coordinates
static constexpr double SPACE_BETWEEN_PAGES = 10;

//! Size of page shadow, in layout coordinates
static constexpr double PAGE_SHADOW_WIDTH = 5; // Must be less than SPACE_BETWEEN_PAGES

/**
* Constructor for QgsLayoutItemPage, with the specified parent \a layout.
*/

0 comments on commit 4cf9827

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