QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 "moc_qgstextpreview.cpp"
18#include "qgstextrenderer.h"
19#include "qgsscreenhelper.h"
20
21#include <QPainter>
22
24 : QLabel( parent )
25{
26 mScreenHelper = new QgsScreenHelper( this );
27 connect( mScreenHelper, &QgsScreenHelper::screenDpiChanged, this, [ = ]( double dpi )
28 {
29 mContext.setScaleFactor( dpi / 25.4 );
30 updateContext();
31 } );
32
33 // initially use a basic transform with no scale
34 QgsMapToPixel newCoordXForm;
35 newCoordXForm.setParameters( 1, 0, 0, 0, 0, 0 );
36 mContext.setMapToPixel( newCoordXForm );
37
38 mContext.setScaleFactor( mScreenHelper->screenDpi() / 25.4 );
39 mContext.setUseAdvancedEffects( true );
40
42
43 mContext.setIsGuiPreview( true );
44}
45
46void QgsTextPreview::paintEvent( QPaintEvent *e )
47{
48 Q_UNUSED( e )
49 QPainter p( this );
50
51 p.setRenderHint( QPainter::Antialiasing );
52
53 // slightly inset text
54 const double fontSize = mContext.convertToPainterUnits( mFormat.size(), mFormat.sizeUnit(), mFormat.sizeMapUnitScale() );
55 double xtrans = 0;
56 if ( mFormat.buffer().enabled() )
57 xtrans = mFormat.buffer().sizeUnit() == Qgis::RenderUnit::Percentage
58 ? fontSize * mFormat.buffer().size() / 100
59 : mContext.convertToPainterUnits( mFormat.buffer().size(), mFormat.buffer().sizeUnit(), mFormat.buffer().sizeMapUnitScale() );
61 xtrans = std::max( xtrans, mContext.convertToPainterUnits( mFormat.background().size().width(), mFormat.background().sizeUnit(), mFormat.background().sizeMapUnitScale() ) );
62 xtrans += 4;
63
64 double ytrans = 0.0;
65 if ( mFormat.buffer().enabled() )
66 ytrans = std::max( ytrans, mFormat.buffer().sizeUnit() == Qgis::RenderUnit::Percentage
67 ? fontSize * mFormat.buffer().size() / 100
68 : mContext.convertToPainterUnits( mFormat.buffer().size(), mFormat.buffer().sizeUnit(), mFormat.buffer().sizeMapUnitScale() ) );
69 if ( mFormat.background().enabled() )
70 ytrans = std::max( ytrans, mContext.convertToPainterUnits( mFormat.background().size().height(), mFormat.background().sizeUnit(), mFormat.background().sizeMapUnitScale() ) );
71 ytrans += 4;
72
73 QRectF textRect = rect();
74 textRect.setLeft( xtrans );
75 textRect.setWidth( textRect.width() - xtrans );
76 textRect.setTop( ytrans );
77 if ( textRect.height() > 300 )
78 textRect.setHeight( 300 );
79 if ( textRect.width() > 2000 )
80 textRect.setWidth( 2000 );
81
82 mContext.setPainter( &p );
83 QgsTextRenderer::drawText( textRect, 0, Qgis::TextHorizontalAlignment::Left, QStringList() << text(),
84 mContext, mFormat );
85}
86
88{
89 mFormat = format;
90 update();
91}
92
93void QgsTextPreview::updateContext()
94{
95 if ( mScale >= 0 )
96 {
97 const QgsMapToPixel newCoordXForm = QgsMapToPixel::fromScale( mScale, mMapUnits, mScreenHelper->screenDpi() );
98 mContext.setMapToPixel( newCoordXForm );
99 }
100 update();
101}
102
103void QgsTextPreview::setScale( double scale )
104{
105 mScale = scale;
106 updateContext();
107}
108
110{
111 mMapUnits = unit;
112 updateContext();
113}
DistanceUnit
Units of distance.
Definition qgis.h:4677
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size)
@ Antialiasing
Use antialiasing while drawing.
Perform transforms between map coordinates and device coordinates.
static QgsMapToPixel fromScale(double scale, Qgis::DistanceUnit mapUnits, double dpi=96)
Returns a new QgsMapToPixel created using a specified scale and distance unit.
void setParameters(double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation)
Sets parameters for use in transforming coordinates.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
void setUseAdvancedEffects(bool enabled)
Used to enable or disable advanced effects such as blend modes.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
void setIsGuiPreview(bool preview)
Sets GUI preview mode.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
A utility class for dynamic handling of changes to screen properties.
double screenDpi() const
Returns the current screen DPI for the screen that the parent widget appears on.
void screenDpiChanged(double dpi)
Emitted whenever the screen dpi associated with the widget is changed.
QSizeF size() const
Returns the size of the background shape.
bool enabled() const
Returns whether the background is enabled.
SizeType sizeType() const
Returns the method used to determine the size of the background shape (e.g., fixed size or buffer aro...
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the shape size.
Qgis::RenderUnit sizeUnit() const
Returns the units used for the shape's size.
Qgis::RenderUnit sizeUnit() const
Returns the units for the buffer size.
double size() const
Returns the size of the buffer.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
bool enabled() const
Returns whether the buffer is enabled.
Container for all settings relating to text rendering.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the size.
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
Qgis::RenderUnit sizeUnit() const
Returns the units for the size of rendered text.
double size() const
Returns the size for rendered text.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
void setScale(double scale)
Sets the scale to use for previewing format sizes in map units.
void paintEvent(QPaintEvent *e) override
QgsTextFormat format
QgsTextPreview(QWidget *parent=nullptr)
Constructor for QgsTextPreview.
void setMapUnits(Qgis::DistanceUnit unit)
Sets the map unit type for previewing format sizes in map units.
void setFormat(const QgsTextFormat &format)
Sets the text format for previewing in the widget.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.