QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgslayoutreportsectionlabel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutreportsectionlabel.cpp
3 ------------------------
4 begin : January 2018
5 copyright : (C) 2018 by Nyall Dawson
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
19
20#include "qgslayout.h"
22#include "qgslayoutview.h"
23
24#include <QBrush>
25#include <QGraphicsView>
26#include <QPainter>
27#include <QWidget>
28
30
31QgsLayoutReportSectionLabel::QgsLayoutReportSectionLabel( QgsLayout *layout, QgsLayoutView *view )
32 : QGraphicsRectItem( nullptr )
33 , mLayout( layout )
34 , mView( view )
35{
36 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
37}
38
39void QgsLayoutReportSectionLabel::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
40{
41 Q_UNUSED( option )
42 Q_UNUSED( widget )
43
44 if ( !mLayout || !mLayout->renderContext().isPreviewRender() )
45 {
46 //don't draw label in outputs
47 return;
48 }
49
50 if ( mLabel.isEmpty() )
51 return;
52
53 QFont f;
54 f.setPointSizeF( 8 );
55 const QFontMetrics fm( f );
56 const QSize s = fm.size( 0, mLabel );
57 const double margin = fm.height() / 5.0;
58
59 const double scaleValue = scale() / painter->transform().m11();
60 const QgsScopedQPainterState painterState( painter );
61 painter->setRenderHint( QPainter::Antialiasing, true );
62 painter->scale( scaleValue, scaleValue );
63 const QRectF r = rect();
64 const QRectF scaledRect( r.left() / scaleValue, r.top() / scaleValue, r.width() / scaleValue, r.height() / scaleValue );
65
66 if ( scaledRect.width() < s.width() + 2 * margin || scaledRect.height() < s.height() + 2 * margin )
67 {
68 // zoomed out too far to fully draw label inside item rect
69 return;
70 }
71
72 const QRectF textRect = QRectF( scaledRect.left() + margin, scaledRect.top() + margin, scaledRect.width() - 2 * margin, scaledRect.height() - 2 * margin );
73 const QRectF boxRect = QRectF( scaledRect.left(), scaledRect.bottom() - ( s.height() + 2 * margin ), s.width() + 2 * margin, s.height() + 2 * margin );
74
75 QPainterPath p;
76 p.moveTo( boxRect.bottomRight() );
77 p.lineTo( boxRect.right(), boxRect.top() + margin );
78 p.arcTo( boxRect.right() - 2 * margin, boxRect.top(), 2 * margin, 2 * margin, 0, 90 );
79 p.lineTo( boxRect.left() + margin, boxRect.top() );
80 p.arcTo( boxRect.left(), boxRect.top(), 2 * margin, 2 * margin, 90, 90 );
81 p.lineTo( boxRect.bottomLeft() );
82 p.lineTo( boxRect.bottomRight() );
83
84 painter->setPen( QColor( 150, 150, 150, 150 ) );
85 QLinearGradient g( 0, boxRect.top(), 0, boxRect.bottom() );
86 g.setColorAt( 0, QColor( 200, 200, 200, 150 ) );
87 g.setColorAt( 1, QColor( 150, 150, 150, 150 ) );
88
89 painter->setBrush( QBrush( g ) );
90 painter->drawPath( p );
91
92 painter->setPen( QPen( QColor( 0, 0, 0, 100 ) ) );
93 painter->setFont( f );
94 painter->drawText( textRect, Qt::AlignBottom, mLabel );
95}
96
97void QgsLayoutReportSectionLabel::setLabel( const QString &label )
98{
99 mLabel = label;
100 update();
101}
102
A graphical widget to display and interact with QgsLayouts.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:50
Scoped object for saving and restoring a QPainter object's state.