Skip to content

Commit

Permalink
Draw page shadow in layout units
Browse files Browse the repository at this point in the history
Simplifies code a lot, pixel based size proved to complex for
small result
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent 5cfc9cc commit 4cf9827
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions python/core/layout/qgslayoutitempage.sip
Expand Up @@ -79,6 +79,7 @@ class QgsLayoutItemPage : QgsLayoutItem

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


};

/************************************************************************
Expand Down
10 changes: 10 additions & 0 deletions python/core/layout/qgslayoutpagecollection.sip
Expand Up @@ -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``.
Expand Down
17 changes: 9 additions & 8 deletions src/core/layout/qgslayoutitempage.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 ) );
Expand Down
5 changes: 5 additions & 0 deletions src/core/layout/qgslayoutitempage.h
Expand Up @@ -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
2 changes: 0 additions & 2 deletions src/core/layout/qgslayoutpagecollection.cpp
Expand Up @@ -17,8 +17,6 @@
#include "qgslayoutpagecollection.h"
#include "qgslayout.h"

#define SPACE_BETWEEN_PAGES 10

QgsLayoutPageCollection::QgsLayoutPageCollection( QgsLayout *layout )
: QObject( layout )
, mLayout( layout )
Expand Down
6 changes: 6 additions & 0 deletions src/core/layout/qgslayoutpagecollection.h
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 4cf9827

Please sign in to comment.