QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 : [email protected]
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"
21 #include <QGraphicsView>
22 #include <QPainter>
23 #include <QWidget>
24 #include <QBrush>
25 
27 
28 QgsLayoutReportSectionLabel::QgsLayoutReportSectionLabel( QgsLayout *layout, QgsLayoutView *view )
29  : QGraphicsRectItem( nullptr )
30  , mLayout( layout )
31  , mView( view )
32 {
33  setCacheMode( QGraphicsItem::DeviceCoordinateCache );
34 }
35 
36 void QgsLayoutReportSectionLabel::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
37 {
38  Q_UNUSED( option );
39  Q_UNUSED( widget );
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  if ( scaledRect.width() < s.width() + 2 * margin || scaledRect.height() < s.height() + 2 * margin )
64  {
65  // zoomed out too far to fully draw label inside item rect
66  return;
67  }
68 
69  QRectF textRect = QRectF( scaledRect.left() + margin, scaledRect.top() + margin, scaledRect.width() - 2 * margin, scaledRect.height() - 2 * margin );
70  QRectF boxRect = QRectF( scaledRect.left(), scaledRect.bottom() - ( s.height() + 2 * margin ), s.width() + 2 * margin, s.height() + 2 * margin );
71 
72  QPainterPath p;
73  p.moveTo( boxRect.bottomRight() );
74  p.lineTo( boxRect.right(), boxRect.top() + margin );
75  p.arcTo( boxRect.right() - 2 * margin, boxRect.top(), 2 * margin, 2 * margin, 0, 90 );
76  p.lineTo( boxRect.left() + margin, boxRect.top() );
77  p.arcTo( boxRect.left(), boxRect.top(), 2 * margin, 2 * margin, 90, 90 );
78  p.lineTo( boxRect.bottomLeft() );
79  p.lineTo( boxRect.bottomRight() );
80 
81  painter->setPen( QColor( 150, 150, 150, 150 ) );
82  QLinearGradient g( 0, boxRect.top(), 0, boxRect.bottom() );
83  g.setColorAt( 0, QColor( 200, 200, 200, 150 ) );
84  g.setColorAt( 1, QColor( 150, 150, 150, 150 ) );
85 
86  painter->setBrush( QBrush( g ) );
87  painter->drawPath( p );
88 
89  painter->setPen( QPen( QColor( 0, 0, 0, 100 ) ) );
90  painter->setFont( f );
91  painter->drawText( textRect, Qt::AlignBottom, mLabel );
92  painter->restore();
93 }
94 
95 void QgsLayoutReportSectionLabel::setLabel( const QString &label )
96 {
97  mLabel = label;
98  update();
99 }
100 
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:49
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout&#39;s render context, which stores information relating to the current ...
Definition: qgslayout.cpp:356
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
bool isPreviewRender() const
Returns true if the render current being conducted is a preview render, i.e.