QGIS API Documentation  3.13.0-Master (5a3b1ced84)
qgstextpreview.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstextpreview.cpp
3  ------------------
4  begin : October 2016
5  copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgstextpreview.h"
17 #include "qgsapplication.h"
18 #include <QDesktopWidget>
19 #include <QPainter>
20 
22  : QLabel( parent )
23 {
24  // initially use a basic transform with no scale
25  QgsMapToPixel newCoordXForm;
26  newCoordXForm.setParameters( 1, 0, 0, 0, 0, 0 );
27  mContext.setMapToPixel( newCoordXForm );
28 
29  mContext.setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
30  mContext.setUseAdvancedEffects( true );
31 
32  mContext.setIsGuiPreview( true );
33 }
34 
35 
36 void QgsTextPreview::paintEvent( QPaintEvent *e )
37 {
38  Q_UNUSED( e )
39  QPainter p( this );
40 
41  p.setRenderHint( QPainter::Antialiasing );
42 
43  // slightly inset text
44  double xtrans = 0;
45  if ( mFormat.buffer().enabled() )
46  xtrans = mContext.convertToPainterUnits( mFormat.buffer().size(), mFormat.buffer().sizeUnit(), mFormat.buffer().sizeMapUnitScale() );
48  xtrans = std::max( xtrans, mContext.convertToPainterUnits( mFormat.background().size().width(), mFormat.background().sizeUnit(), mFormat.background().sizeMapUnitScale() ) );
49  xtrans += 4;
50 
51  double ytrans = 0.0;
52  if ( mFormat.buffer().enabled() )
53  ytrans = std::max( ytrans, mContext.convertToPainterUnits( mFormat.buffer().size(), mFormat.buffer().sizeUnit(), mFormat.buffer().sizeMapUnitScale() ) );
54  if ( mFormat.background().enabled() )
55  ytrans = std::max( ytrans, mContext.convertToPainterUnits( mFormat.background().size().height(), mFormat.background().sizeUnit(), mFormat.background().sizeMapUnitScale() ) );
56  ytrans += 4;
57 
58  QRectF textRect = rect();
59  textRect.setLeft( xtrans );
60  textRect.setWidth( textRect.width() - xtrans );
61  textRect.setTop( ytrans );
62  if ( textRect.height() > 300 )
63  textRect.setHeight( 300 );
64  if ( textRect.width() > 2000 )
65  textRect.setWidth( 2000 );
66 
67  mContext.setPainter( &p );
68  QgsTextRenderer::drawText( textRect, 0, QgsTextRenderer::AlignLeft, QStringList() << text(),
69  mContext, mFormat );
70 }
71 
73 {
74  mFormat = format;
75  update();
76 }
77 
78 void QgsTextPreview::updateContext()
79 {
80  if ( mScale >= 0 )
81  {
82  QgsMapToPixel newCoordXForm = QgsMapToPixel::fromScale( mScale, mMapUnits, QgsApplication::desktop()->logicalDpiX() );
83  mContext.setMapToPixel( newCoordXForm );
84  }
85  update();
86 }
87 
89 {
90  mScale = scale;
91  updateContext();
92 }
93 
95 {
96  mMapUnits = unit;
97  updateContext();
98 }
QSizeF size() const
Returns the size of the background shape.
QgsTextPreview(QWidget *parent=nullptr)
Constructor for QgsTextPreview.
void setScale(double scale)
Sets the scale to use for previewing format sizes in map units.
void setIsGuiPreview(bool preview)
Sets GUI preview mode.
double scale() const
Returns the scale used for previewing format sizes in map units.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:37
void setUseAdvancedEffects(bool enabled)
Used to enable or disable advanced effects such as blend modes.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
static QgsMapToPixel fromScale(double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi=96)
Returns a new QgsMapToPixel created using a specified scale and distance unit.
void setMapUnits(QgsUnitTypes::DistanceUnit unit)
Sets the map unit type for previewing format sizes in map units.
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
QgsTextFormat format() const
Returns the text format used for previewing text in the widget.
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true)
Draws text within a rectangle using the specified settings.
void setFormat(const QgsTextFormat &format)
Sets the text format for previewing in the widget.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
void setParameters(double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation)
Set parameters for use in transforming coordinates.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the buffer size.
double size() const
Returns the size of the buffer.
bool enabled() const
Returns whether the background is enabled.
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context&#39;s map to pixel transform, which transforms between map coordinates and device coordi...
SizeType sizeType() const
Returns the method used to determine the size of the background shape (e.g., fixed size or buffer aro...
bool enabled() const
Returns whether the buffer is enabled.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the shape size.
Container for all settings relating to text rendering.
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units used for the shape&#39;s size.
void paintEvent(QPaintEvent *e) override