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