-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show a label in layout designer for report sections
- Loading branch information
1 parent
421ef88
commit 015e754
Showing
7 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/*************************************************************************** | ||
qgslayoutreportsectionlabel.cpp | ||
------------------------ | ||
begin : January 2018 | ||
copyright : (C) 2018 by Nyall Dawson | ||
email : nyall.dawson@gmail.com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgslayoutreportsectionlabel.h" | ||
#include "qgslayout.h" | ||
#include "qgslayoutview.h" | ||
#include <QGraphicsView> | ||
#include <QPainter> | ||
#include <QWidget> | ||
#include <QBrush> | ||
|
||
QgsLayoutReportSectionLabel::QgsLayoutReportSectionLabel( QgsLayout *layout, QgsLayoutView *view ) | ||
: QGraphicsRectItem( nullptr ) | ||
, mLayout( layout ) | ||
, mView( view ) | ||
{ | ||
setCacheMode( QGraphicsItem::DeviceCoordinateCache ); | ||
} | ||
|
||
QgsLayoutReportSectionLabel::~QgsLayoutReportSectionLabel() | ||
{ | ||
|
||
} | ||
|
||
void QgsLayoutReportSectionLabel::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * ) | ||
{ | ||
if ( !mLayout || !mLayout->renderContext().isPreviewRender() ) | ||
{ | ||
//don't draw label in outputs | ||
return; | ||
} | ||
|
||
if ( mLabel.isEmpty() ) | ||
return; | ||
|
||
QFont f; | ||
f.setPointSizeF( 8 ); | ||
QFontMetrics fm( f ); | ||
QSize s = fm.size( 0, mLabel ); | ||
double margin = fm.height() / 5.0; | ||
|
||
double scaleValue = scale() / painter->transform().m11(); | ||
painter->save(); | ||
painter->setRenderHint( QPainter::Antialiasing, true ); | ||
painter->scale( scaleValue, scaleValue ); | ||
QRectF r = rect(); | ||
QRectF scaledRect( r.left() / scaleValue, r.top() / scaleValue, r.width() / scaleValue, r.height() / scaleValue ); | ||
|
||
QRectF textRect = QRectF( scaledRect.left() + margin, scaledRect.top() + margin, scaledRect.width() - 2 * margin, scaledRect.height() - 2 * margin ); | ||
QRectF boxRect = QRectF( scaledRect.left(), scaledRect.bottom() - ( s.height() + 2 * margin ), s.width() + 2 * margin, s.height() + 2 * margin ); | ||
|
||
QPainterPath p; | ||
p.moveTo( boxRect.bottomRight() ); | ||
p.lineTo( boxRect.right(), boxRect.top() + margin ); | ||
p.arcTo( boxRect.right() - 2 * margin, boxRect.top(), 2 * margin, 2 * margin, 0, 90 ); | ||
p.lineTo( boxRect.left() + margin, boxRect.top() ); | ||
p.arcTo( boxRect.left(), boxRect.top(), 2 * margin, 2 * margin, 90, 90 ); | ||
p.lineTo( boxRect.bottomLeft() ); | ||
p.lineTo( boxRect.bottomRight() ); | ||
|
||
painter->setPen( QColor( 150, 150, 150, 150 ) ); | ||
QLinearGradient g( 0, boxRect.top(), 0, boxRect.bottom() ); | ||
g.setColorAt( 0, QColor( 200, 200, 200, 150 ) ); | ||
g.setColorAt( 1, QColor( 150, 150, 150, 150 ) ); | ||
|
||
painter->setBrush( QBrush( g ) ); | ||
painter->drawPath( p ); | ||
|
||
painter->setPen( QPen( QColor( 0, 0, 0, 100 ) ) ); | ||
painter->setFont( f ); | ||
painter->drawText( textRect, Qt::AlignBottom, mLabel ); | ||
painter->restore(); | ||
} | ||
|
||
void QgsLayoutReportSectionLabel::setLabel( const QString &label ) | ||
{ | ||
mLabel = label; | ||
update(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/*************************************************************************** | ||
qgslayoutreportsectionlabel.h | ||
----------------------- | ||
begin : January 2018 | ||
copyright : (C) 2018 by Nyall Dawson | ||
email : nyall.dawson@gmail.com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
#ifndef QGSLAYOUTREPORTSECTIONLABEL_H | ||
#define QGSLAYOUTREPORTSECTIONLABEL_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include <QGraphicsRectItem> | ||
#include "qgis_gui.h" | ||
#include "qgslayoutview.h" | ||
#include "qgslayout.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* \ingroup gui | ||
* Draws a label describing the current report section within a layout designer view. | ||
* | ||
* \note not available in Python bindings | ||
* \since QGIS 3.0 | ||
* | ||
*/ | ||
class GUI_EXPORT QgsLayoutReportSectionLabel: public QGraphicsRectItem | ||
{ | ||
|
||
public: | ||
|
||
/** | ||
* Constructor for QgsLayoutReportSectionLabel. | ||
*/ | ||
QgsLayoutReportSectionLabel( QgsLayout *layout, QgsLayoutView *view ); | ||
|
||
~QgsLayoutReportSectionLabel(); | ||
|
||
void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override; | ||
|
||
void setLabel( const QString &label ); | ||
|
||
private: | ||
|
||
QPointer< QgsLayout > mLayout; | ||
QPointer< QgsLayoutView > mView; | ||
QString mLabel; | ||
|
||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSLAYOUTREPORTSECTIONLABEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters