Skip to content

Commit

Permalink
Show a label in layout designer for report sections
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 421ef88 commit 015e754
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/gui/layout/qgslayoutview.sip
Expand Up @@ -263,6 +263,12 @@ Returns true if the current clipboard contains layout items.
%Docstring
Returns the delta (in layout coordinates) by which to move items
for the given key ``event``.
%End

void setSectionLabel( const QString &label );
%Docstring
Sets a section ``label``, to display above the first page shown in the
view.
%End

public slots:
Expand Down
1 change: 1 addition & 0 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -4063,6 +4063,7 @@ void QgsLayoutDesignerDialog::setSectionTitle( const QString &title )
{
mSectionTitle = title;
updateWindowTitle();
mView->setSectionLabel( title );
}


2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -166,6 +166,7 @@ SET(QGIS_GUI_SRCS
layout/qgslayoutitemwidget.cpp
layout/qgslayoutmousehandles.cpp
layout/qgslayoutnewitempropertiesdialog.cpp
layout/qgslayoutreportsectionlabel.cpp
layout/qgslayoutruler.cpp
layout/qgslayoutunitscombobox.cpp
layout/qgslayoutview.cpp
Expand Down Expand Up @@ -801,6 +802,7 @@ SET(QGIS_GUI_HDRS
layertree/qgslayertreeembeddedconfigwidget.h
layertree/qgslayertreeembeddedwidgetregistry.h

layout/qgslayoutreportsectionlabel.h
layout/qgslayoutviewmouseevent.h
layout/qgslayoutviewrubberband.h

Expand Down
93 changes: 93 additions & 0 deletions src/gui/layout/qgslayoutreportsectionlabel.cpp
@@ -0,0 +1,93 @@
/***************************************************************************
qgslayoutreportsectionlabel.cpp
------------------------
begin : January 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall.dawson@gmail.com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslayoutreportsectionlabel.h"
#include "qgslayout.h"
#include "qgslayoutview.h"
#include <QGraphicsView>
#include <QPainter>
#include <QWidget>
#include <QBrush>

QgsLayoutReportSectionLabel::QgsLayoutReportSectionLabel( QgsLayout *layout, QgsLayoutView *view )
: QGraphicsRectItem( nullptr )
, mLayout( layout )
, mView( view )
{
setCacheMode( QGraphicsItem::DeviceCoordinateCache );
}

QgsLayoutReportSectionLabel::~QgsLayoutReportSectionLabel()
{

}

void QgsLayoutReportSectionLabel::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * )
{
if ( !mLayout || !mLayout->renderContext().isPreviewRender() )
{
//don't draw label in outputs
return;
}

if ( mLabel.isEmpty() )
return;

QFont f;
f.setPointSizeF( 8 );
QFontMetrics fm( f );
QSize s = fm.size( 0, mLabel );
double margin = fm.height() / 5.0;

double scaleValue = scale() / painter->transform().m11();
painter->save();
painter->setRenderHint( QPainter::Antialiasing, true );
painter->scale( scaleValue, scaleValue );
QRectF r = rect();
QRectF scaledRect( r.left() / scaleValue, r.top() / scaleValue, r.width() / scaleValue, r.height() / scaleValue );

QRectF textRect = QRectF( scaledRect.left() + margin, scaledRect.top() + margin, scaledRect.width() - 2 * margin, scaledRect.height() - 2 * margin );
QRectF boxRect = QRectF( scaledRect.left(), scaledRect.bottom() - ( s.height() + 2 * margin ), s.width() + 2 * margin, s.height() + 2 * margin );

QPainterPath p;
p.moveTo( boxRect.bottomRight() );
p.lineTo( boxRect.right(), boxRect.top() + margin );
p.arcTo( boxRect.right() - 2 * margin, boxRect.top(), 2 * margin, 2 * margin, 0, 90 );
p.lineTo( boxRect.left() + margin, boxRect.top() );
p.arcTo( boxRect.left(), boxRect.top(), 2 * margin, 2 * margin, 90, 90 );
p.lineTo( boxRect.bottomLeft() );
p.lineTo( boxRect.bottomRight() );

painter->setPen( QColor( 150, 150, 150, 150 ) );
QLinearGradient g( 0, boxRect.top(), 0, boxRect.bottom() );
g.setColorAt( 0, QColor( 200, 200, 200, 150 ) );
g.setColorAt( 1, QColor( 150, 150, 150, 150 ) );

painter->setBrush( QBrush( g ) );
painter->drawPath( p );

painter->setPen( QPen( QColor( 0, 0, 0, 100 ) ) );
painter->setFont( f );
painter->drawText( textRect, Qt::AlignBottom, mLabel );
painter->restore();
}

void QgsLayoutReportSectionLabel::setLabel( const QString &label )
{
mLabel = label;
update();
}
63 changes: 63 additions & 0 deletions src/gui/layout/qgslayoutreportsectionlabel.h
@@ -0,0 +1,63 @@
/***************************************************************************
qgslayoutreportsectionlabel.h
-----------------------
begin : January 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall.dawson@gmail.com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSLAYOUTREPORTSECTIONLABEL_H
#define QGSLAYOUTREPORTSECTIONLABEL_H

#define SIP_NO_FILE

#include <QGraphicsRectItem>
#include "qgis_gui.h"
#include "qgslayoutview.h"
#include "qgslayout.h"

///@cond PRIVATE

/**
* \ingroup gui
* Draws a label describing the current report section within a layout designer view.
*
* \note not available in Python bindings
* \since QGIS 3.0
*
*/
class GUI_EXPORT QgsLayoutReportSectionLabel: public QGraphicsRectItem
{

public:

/**
* Constructor for QgsLayoutReportSectionLabel.
*/
QgsLayoutReportSectionLabel( QgsLayout *layout, QgsLayoutView *view );

~QgsLayoutReportSectionLabel();

void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;

void setLabel( const QString &label );

private:

QPointer< QgsLayout > mLayout;
QPointer< QgsLayoutView > mView;
QString mLabel;

};

///@endcond PRIVATE

#endif // QGSLAYOUTREPORTSECTIONLABEL_H
19 changes: 19 additions & 0 deletions src/gui/layout/qgslayoutview.cpp
Expand Up @@ -33,6 +33,7 @@
#include "qgslayoutitemgroup.h"
#include "qgslayoutpagecollection.h"
#include "qgslayoutundostack.h"
#include "qgslayoutreportsectionlabel.h"
#include <memory>
#include <QDesktopWidget>
#include <QMenu>
Expand Down Expand Up @@ -97,6 +98,9 @@ void QgsLayoutView::setCurrentLayout( QgsLayout *layout )
mVerticalSnapLine->hide();
layout->addItem( mVerticalSnapLine );

delete mSectionLabel;
mSectionLabel = nullptr;

if ( mHorizontalRuler )
{
connect( &layout->guides(), &QAbstractItemModel::dataChanged, mHorizontalRuler, [ = ] { mHorizontalRuler->update(); } );
Expand Down Expand Up @@ -480,6 +484,21 @@ void QgsLayoutView::setPaintingEnabled( bool enabled )
update();
}

void QgsLayoutView::setSectionLabel( const QString &label )
{
if ( !currentLayout() )
return;

if ( !mSectionLabel )
{
mSectionLabel = new QgsLayoutReportSectionLabel( currentLayout(), this );
currentLayout()->addItem( mSectionLabel );
mSectionLabel->setRect( 0, -200, 1000, 200 );
mSectionLabel->setZValue( -1 );
}
mSectionLabel->setLabel( label );
}

void QgsLayoutView::zoomFull()
{
if ( !scene() )
Expand Down
8 changes: 8 additions & 0 deletions src/gui/layout/qgslayoutview.h
Expand Up @@ -36,6 +36,7 @@ class QgsLayoutViewToolTemporaryMousePan;
class QgsLayoutRuler;
class QgsLayoutViewMenuProvider;
class QgsLayoutViewSnapMarker;
class QgsLayoutReportSectionLabel;

/**
* \ingroup gui
Expand Down Expand Up @@ -277,6 +278,12 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
*/
void setPaintingEnabled( bool enabled ); SIP_SKIP

/**
* Sets a section \a label, to display above the first page shown in the
* view.
*/
void setSectionLabel( const QString &label );

public slots:

/**
Expand Down Expand Up @@ -543,6 +550,7 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
std::unique_ptr< QgsLayoutViewMenuProvider > mMenuProvider;

QgsLayoutViewSnapMarker *mSnapMarker = nullptr;
QgsLayoutReportSectionLabel *mSectionLabel = nullptr;

QGraphicsLineItem *mHorizontalSnapLine = nullptr;
QGraphicsLineItem *mVerticalSnapLine = nullptr;
Expand Down

0 comments on commit 015e754

Please sign in to comment.