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