QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 "qgstextrenderer.h"
19 #include <QDesktopWidget>
20 #include <QPainter>
21 
23  : QLabel( parent )
24 {
25  // initially use a basic transform with no scale
26  QgsMapToPixel newCoordXForm;
27  newCoordXForm.setParameters( 1, 0, 0, 0, 0, 0 );
28  mContext.setMapToPixel( newCoordXForm );
29 
30  mContext.setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
31  mContext.setUseAdvancedEffects( true );
32 
33  mContext.setIsGuiPreview( true );
34 }
35 
36 
37 void QgsTextPreview::paintEvent( QPaintEvent *e )
38 {
39  Q_UNUSED( e )
40  QPainter p( this );
41 
42  p.setRenderHint( QPainter::Antialiasing );
43 
44  // slightly inset text
45  double xtrans = 0;
46  if ( mFormat.buffer().enabled() )
47  xtrans = mContext.convertToPainterUnits( mFormat.buffer().size(), mFormat.buffer().sizeUnit(), mFormat.buffer().sizeMapUnitScale() );
49  xtrans = std::max( xtrans, mContext.convertToPainterUnits( mFormat.background().size().width(), mFormat.background().sizeUnit(), mFormat.background().sizeMapUnitScale() ) );
50  xtrans += 4;
51 
52  double ytrans = 0.0;
53  if ( mFormat.buffer().enabled() )
54  ytrans = std::max( ytrans, mContext.convertToPainterUnits( mFormat.buffer().size(), mFormat.buffer().sizeUnit(), mFormat.buffer().sizeMapUnitScale() ) );
55  if ( mFormat.background().enabled() )
56  ytrans = std::max( ytrans, mContext.convertToPainterUnits( mFormat.background().size().height(), mFormat.background().sizeUnit(), mFormat.background().sizeMapUnitScale() ) );
57  ytrans += 4;
58 
59  QRectF textRect = rect();
60  textRect.setLeft( xtrans );
61  textRect.setWidth( textRect.width() - xtrans );
62  textRect.setTop( ytrans );
63  if ( textRect.height() > 300 )
64  textRect.setHeight( 300 );
65  if ( textRect.width() > 2000 )
66  textRect.setWidth( 2000 );
67 
68  mContext.setPainter( &p );
69  QgsTextRenderer::drawText( textRect, 0, QgsTextRenderer::AlignLeft, QStringList() << text(),
70  mContext, mFormat );
71 }
72 
74 {
75  mFormat = format;
76  update();
77 }
78 
79 void QgsTextPreview::updateContext()
80 {
81  if ( mScale >= 0 )
82  {
83  QgsMapToPixel newCoordXForm = QgsMapToPixel::fromScale( mScale, mMapUnits, QgsApplication::desktop()->logicalDpiX() );
84  mContext.setMapToPixel( newCoordXForm );
85  }
86  update();
87 }
88 
89 void QgsTextPreview::setScale( double scale )
90 {
91  mScale = scale;
92  updateContext();
93 }
94 
96 {
97  mMapUnits = unit;
98  updateContext();
99 }
QgsTextFormat::buffer
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
Definition: qgstextformat.h:66
QgsRenderContext::setIsGuiPreview
void setIsGuiPreview(bool preview)
Sets GUI preview mode.
Definition: qgsrendercontext.h:750
QgsRenderContext::convertToPainterUnits
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
Definition: qgsrendercontext.cpp:287
QgsTextBufferSettings::sizeMapUnitScale
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
Definition: qgstextbuffersettings.cpp:77
QgsTextBackgroundSettings::enabled
bool enabled() const
Returns whether the background is enabled.
Definition: qgstextbackgroundsettings.cpp:47
qgstextrenderer.h
QgsMapToPixel::setParameters
void setParameters(double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation)
Set parameters for use in transforming coordinates.
Definition: qgsmaptopixel.cpp:163
QgsRenderContext::setPainter
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
Definition: qgsrendercontext.h:475
QgsTextPreview::paintEvent
void paintEvent(QPaintEvent *e) override
Definition: qgstextpreview.cpp:37
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
QgsTextBackgroundSettings::sizeType
SizeType sizeType() const
Returns the method used to determine the size of the background shape (e.g., fixed size or buffer aro...
Definition: qgstextbackgroundsettings.cpp:87
QgsTextPreview::setMapUnits
void setMapUnits(QgsUnitTypes::DistanceUnit unit)
Sets the map unit type for previewing format sizes in map units.
Definition: qgstextpreview.cpp:95
qgsapplication.h
QgsTextRenderer::drawText
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.
Definition: qgstextrenderer.cpp:45
QgsTextBackgroundSettings::sizeMapUnitScale
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the shape size.
Definition: qgstextbackgroundsettings.cpp:117
QgsTextFormat
Definition: qgstextformat.h:38
QgsTextBackgroundSettings::size
QSizeF size() const
Returns the size of the background shape.
Definition: qgstextbackgroundsettings.cpp:97
qgstextpreview.h
QgsRenderContext::setMapToPixel
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
Definition: qgsrendercontext.h:404
QgsTextRenderer::AlignLeft
@ AlignLeft
Left align.
Definition: qgstextrenderer.h:60
QgsTextPreview::QgsTextPreview
QgsTextPreview(QWidget *parent=nullptr)
Constructor for QgsTextPreview.
Definition: qgstextpreview.cpp:22
QgsTextBufferSettings::sizeUnit
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the buffer size.
Definition: qgstextbuffersettings.cpp:67
QgsTextBufferSettings::size
double size() const
Returns the size of the buffer.
Definition: qgstextbuffersettings.cpp:57
QgsTextPreview::scale
double scale
Definition: qgstextpreview.h:44
QgsTextBufferSettings::enabled
bool enabled() const
Returns whether the buffer is enabled.
Definition: qgstextbuffersettings.cpp:47
QgsMapToPixel
Definition: qgsmaptopixel.h:37
QgsTextBackgroundSettings::SizeFixed
@ SizeFixed
Fixed size.
Definition: qgstextbackgroundsettings.h:68
QgsTextFormat::background
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
Definition: qgstextformat.h:85
QgsTextBackgroundSettings::sizeUnit
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units used for the shape's size.
Definition: qgstextbackgroundsettings.cpp:107
QgsRenderContext::setUseAdvancedEffects
void setUseAdvancedEffects(bool enabled)
Used to enable or disable advanced effects such as blend modes.
Definition: qgsrendercontext.cpp:225
QgsTextPreview::setFormat
void setFormat(const QgsTextFormat &format)
Sets the text format for previewing in the widget.
Definition: qgstextpreview.cpp:73
QgsMapToPixel::fromScale
static QgsMapToPixel fromScale(double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi=96)
Returns a new QgsMapToPixel created using a specified scale and distance unit.
Definition: qgsmaptopixel.cpp:57
QgsTextPreview::setScale
void setScale(double scale)
Sets the scale to use for previewing format sizes in map units.
Definition: qgstextpreview.cpp:89
QgsRenderContext::setScaleFactor
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
Definition: qgsrendercontext.h:460
QgsTextPreview::format
QgsTextFormat format
Definition: qgstextpreview.h:43