Skip to content

Commit 015e754

Browse files
committedJan 5, 2018
Show a label in layout designer for report sections
1 parent 421ef88 commit 015e754

File tree

7 files changed

+192
-0
lines changed

7 files changed

+192
-0
lines changed
 

‎python/gui/layout/qgslayoutview.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ Returns true if the current clipboard contains layout items.
263263
%Docstring
264264
Returns the delta (in layout coordinates) by which to move items
265265
for the given key ``event``.
266+
%End
267+
268+
void setSectionLabel( const QString &label );
269+
%Docstring
270+
Sets a section ``label``, to display above the first page shown in the
271+
view.
266272
%End
267273

268274
public slots:

‎src/app/layout/qgslayoutdesignerdialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4063,6 +4063,7 @@ void QgsLayoutDesignerDialog::setSectionTitle( const QString &title )
40634063
{
40644064
mSectionTitle = title;
40654065
updateWindowTitle();
4066+
mView->setSectionLabel( title );
40664067
}
40674068

40684069

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ SET(QGIS_GUI_SRCS
166166
layout/qgslayoutitemwidget.cpp
167167
layout/qgslayoutmousehandles.cpp
168168
layout/qgslayoutnewitempropertiesdialog.cpp
169+
layout/qgslayoutreportsectionlabel.cpp
169170
layout/qgslayoutruler.cpp
170171
layout/qgslayoutunitscombobox.cpp
171172
layout/qgslayoutview.cpp
@@ -801,6 +802,7 @@ SET(QGIS_GUI_HDRS
801802
layertree/qgslayertreeembeddedconfigwidget.h
802803
layertree/qgslayertreeembeddedwidgetregistry.h
803804

805+
layout/qgslayoutreportsectionlabel.h
804806
layout/qgslayoutviewmouseevent.h
805807
layout/qgslayoutviewrubberband.h
806808

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/***************************************************************************
2+
qgslayoutreportsectionlabel.cpp
3+
------------------------
4+
begin : January 2018
5+
copyright : (C) 2018 by Nyall Dawson
6+
email : nyall.dawson@gmail.com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgslayoutreportsectionlabel.h"
19+
#include "qgslayout.h"
20+
#include "qgslayoutview.h"
21+
#include <QGraphicsView>
22+
#include <QPainter>
23+
#include <QWidget>
24+
#include <QBrush>
25+
26+
QgsLayoutReportSectionLabel::QgsLayoutReportSectionLabel( QgsLayout *layout, QgsLayoutView *view )
27+
: QGraphicsRectItem( nullptr )
28+
, mLayout( layout )
29+
, mView( view )
30+
{
31+
setCacheMode( QGraphicsItem::DeviceCoordinateCache );
32+
}
33+
34+
QgsLayoutReportSectionLabel::~QgsLayoutReportSectionLabel()
35+
{
36+
37+
}
38+
39+
void QgsLayoutReportSectionLabel::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * )
40+
{
41+
if ( !mLayout || !mLayout->renderContext().isPreviewRender() )
42+
{
43+
//don't draw label in outputs
44+
return;
45+
}
46+
47+
if ( mLabel.isEmpty() )
48+
return;
49+
50+
QFont f;
51+
f.setPointSizeF( 8 );
52+
QFontMetrics fm( f );
53+
QSize s = fm.size( 0, mLabel );
54+
double margin = fm.height() / 5.0;
55+
56+
double scaleValue = scale() / painter->transform().m11();
57+
painter->save();
58+
painter->setRenderHint( QPainter::Antialiasing, true );
59+
painter->scale( scaleValue, scaleValue );
60+
QRectF r = rect();
61+
QRectF scaledRect( r.left() / scaleValue, r.top() / scaleValue, r.width() / scaleValue, r.height() / scaleValue );
62+
63+
QRectF textRect = QRectF( scaledRect.left() + margin, scaledRect.top() + margin, scaledRect.width() - 2 * margin, scaledRect.height() - 2 * margin );
64+
QRectF boxRect = QRectF( scaledRect.left(), scaledRect.bottom() - ( s.height() + 2 * margin ), s.width() + 2 * margin, s.height() + 2 * margin );
65+
66+
QPainterPath p;
67+
p.moveTo( boxRect.bottomRight() );
68+
p.lineTo( boxRect.right(), boxRect.top() + margin );
69+
p.arcTo( boxRect.right() - 2 * margin, boxRect.top(), 2 * margin, 2 * margin, 0, 90 );
70+
p.lineTo( boxRect.left() + margin, boxRect.top() );
71+
p.arcTo( boxRect.left(), boxRect.top(), 2 * margin, 2 * margin, 90, 90 );
72+
p.lineTo( boxRect.bottomLeft() );
73+
p.lineTo( boxRect.bottomRight() );
74+
75+
painter->setPen( QColor( 150, 150, 150, 150 ) );
76+
QLinearGradient g( 0, boxRect.top(), 0, boxRect.bottom() );
77+
g.setColorAt( 0, QColor( 200, 200, 200, 150 ) );
78+
g.setColorAt( 1, QColor( 150, 150, 150, 150 ) );
79+
80+
painter->setBrush( QBrush( g ) );
81+
painter->drawPath( p );
82+
83+
painter->setPen( QPen( QColor( 0, 0, 0, 100 ) ) );
84+
painter->setFont( f );
85+
painter->drawText( textRect, Qt::AlignBottom, mLabel );
86+
painter->restore();
87+
}
88+
89+
void QgsLayoutReportSectionLabel::setLabel( const QString &label )
90+
{
91+
mLabel = label;
92+
update();
93+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/***************************************************************************
2+
qgslayoutreportsectionlabel.h
3+
-----------------------
4+
begin : January 2018
5+
copyright : (C) 2018 by Nyall Dawson
6+
email : nyall.dawson@gmail.com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
#ifndef QGSLAYOUTREPORTSECTIONLABEL_H
18+
#define QGSLAYOUTREPORTSECTIONLABEL_H
19+
20+
#define SIP_NO_FILE
21+
22+
#include <QGraphicsRectItem>
23+
#include "qgis_gui.h"
24+
#include "qgslayoutview.h"
25+
#include "qgslayout.h"
26+
27+
///@cond PRIVATE
28+
29+
/**
30+
* \ingroup gui
31+
* Draws a label describing the current report section within a layout designer view.
32+
*
33+
* \note not available in Python bindings
34+
* \since QGIS 3.0
35+
*
36+
*/
37+
class GUI_EXPORT QgsLayoutReportSectionLabel: public QGraphicsRectItem
38+
{
39+
40+
public:
41+
42+
/**
43+
* Constructor for QgsLayoutReportSectionLabel.
44+
*/
45+
QgsLayoutReportSectionLabel( QgsLayout *layout, QgsLayoutView *view );
46+
47+
~QgsLayoutReportSectionLabel();
48+
49+
void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
50+
51+
void setLabel( const QString &label );
52+
53+
private:
54+
55+
QPointer< QgsLayout > mLayout;
56+
QPointer< QgsLayoutView > mView;
57+
QString mLabel;
58+
59+
};
60+
61+
///@endcond PRIVATE
62+
63+
#endif // QGSLAYOUTREPORTSECTIONLABEL_H

‎src/gui/layout/qgslayoutview.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qgslayoutitemgroup.h"
3434
#include "qgslayoutpagecollection.h"
3535
#include "qgslayoutundostack.h"
36+
#include "qgslayoutreportsectionlabel.h"
3637
#include <memory>
3738
#include <QDesktopWidget>
3839
#include <QMenu>
@@ -97,6 +98,9 @@ void QgsLayoutView::setCurrentLayout( QgsLayout *layout )
9798
mVerticalSnapLine->hide();
9899
layout->addItem( mVerticalSnapLine );
99100

101+
delete mSectionLabel;
102+
mSectionLabel = nullptr;
103+
100104
if ( mHorizontalRuler )
101105
{
102106
connect( &layout->guides(), &QAbstractItemModel::dataChanged, mHorizontalRuler, [ = ] { mHorizontalRuler->update(); } );
@@ -480,6 +484,21 @@ void QgsLayoutView::setPaintingEnabled( bool enabled )
480484
update();
481485
}
482486

487+
void QgsLayoutView::setSectionLabel( const QString &label )
488+
{
489+
if ( !currentLayout() )
490+
return;
491+
492+
if ( !mSectionLabel )
493+
{
494+
mSectionLabel = new QgsLayoutReportSectionLabel( currentLayout(), this );
495+
currentLayout()->addItem( mSectionLabel );
496+
mSectionLabel->setRect( 0, -200, 1000, 200 );
497+
mSectionLabel->setZValue( -1 );
498+
}
499+
mSectionLabel->setLabel( label );
500+
}
501+
483502
void QgsLayoutView::zoomFull()
484503
{
485504
if ( !scene() )

‎src/gui/layout/qgslayoutview.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class QgsLayoutViewToolTemporaryMousePan;
3636
class QgsLayoutRuler;
3737
class QgsLayoutViewMenuProvider;
3838
class QgsLayoutViewSnapMarker;
39+
class QgsLayoutReportSectionLabel;
3940

4041
/**
4142
* \ingroup gui
@@ -277,6 +278,12 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
277278
*/
278279
void setPaintingEnabled( bool enabled ); SIP_SKIP
279280

281+
/**
282+
* Sets a section \a label, to display above the first page shown in the
283+
* view.
284+
*/
285+
void setSectionLabel( const QString &label );
286+
280287
public slots:
281288

282289
/**
@@ -543,6 +550,7 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
543550
std::unique_ptr< QgsLayoutViewMenuProvider > mMenuProvider;
544551

545552
QgsLayoutViewSnapMarker *mSnapMarker = nullptr;
553+
QgsLayoutReportSectionLabel *mSectionLabel = nullptr;
546554

547555
QGraphicsLineItem *mHorizontalSnapLine = nullptr;
548556
QGraphicsLineItem *mVerticalSnapLine = nullptr;

0 commit comments

Comments
 (0)
Please sign in to comment.