QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgsscreenhelper.h"
19#include "qgstextrenderer.h"
20
21#include <QPainter>
22
23#include "moc_qgstextpreview.cpp"
24
26 : QLabel( parent )
27{
28 mScreenHelper = new QgsScreenHelper( this );
29 connect( mScreenHelper, &QgsScreenHelper::screenDpiChanged, this, [this]( double dpi ) {
30 mContext.setScaleFactor( dpi / 25.4 );
31 updateContext();
32 } );
33
34 // initially use a basic transform with no scale
35 QgsMapToPixel newCoordXForm;
36 newCoordXForm.setParameters( 1, 0, 0, 0, 0, 0 );
37 mContext.setMapToPixel( newCoordXForm );
38
39 mContext.setScaleFactor( mScreenHelper->screenDpi() / 25.4 );
40 mContext.setRasterizedRenderingPolicy( Qgis::RasterizedRenderingPolicy::PreferVector );
41
42 mContext.setFlag( Qgis::RenderContextFlag::Antialiasing, true );
43
44 mContext.setIsGuiPreview( true );
45}
46
47void QgsTextPreview::paintEvent( QPaintEvent *e )
48{
49 Q_UNUSED( e )
50 QPainter p( this );
51
52 p.setRenderHint( QPainter::Antialiasing );
53
54 // slightly inset text
55 const double fontSize = mContext.convertToPainterUnits( mFormat.size(), mFormat.sizeUnit(), mFormat.sizeMapUnitScale() );
56 double xtrans = 0;
57 if ( mFormat.buffer().enabled() )
58 xtrans = mFormat.buffer().sizeUnit() == Qgis::RenderUnit::Percentage
59 ? fontSize * mFormat.buffer().size() / 100
60 : mContext.convertToPainterUnits( mFormat.buffer().size(), mFormat.buffer().sizeUnit(), mFormat.buffer().sizeMapUnitScale() );
61 if ( mFormat.background().enabled() && mFormat.background().sizeType() != QgsTextBackgroundSettings::SizeFixed )
62 xtrans = std::max( xtrans, mContext.convertToPainterUnits( mFormat.background().size().width(), mFormat.background().sizeUnit(), mFormat.background().sizeMapUnitScale() ) );
63 xtrans += 4;
64
65 double ytrans = 0.0;
66 if ( mFormat.buffer().enabled() )
67 ytrans = std::max( ytrans, mFormat.buffer().sizeUnit() == Qgis::RenderUnit::Percentage ? fontSize * mFormat.buffer().size() / 100 : mContext.convertToPainterUnits( mFormat.buffer().size(), mFormat.buffer().sizeUnit(), mFormat.buffer().sizeMapUnitScale() ) );
68 if ( mFormat.background().enabled() )
69 ytrans = std::max( ytrans, mContext.convertToPainterUnits( mFormat.background().size().height(), mFormat.background().sizeUnit(), mFormat.background().sizeMapUnitScale() ) );
70 ytrans += 4;
71
72 QRectF textRect = rect();
73 textRect.setLeft( xtrans );
74 textRect.setWidth( textRect.width() - xtrans );
75 textRect.setTop( ytrans );
76 if ( textRect.height() > 300 )
77 textRect.setHeight( 300 );
78 if ( textRect.width() > 2000 )
79 textRect.setWidth( 2000 );
80
81 mContext.setPainter( &p );
82 QgsTextRenderer::drawText( textRect, 0, Qgis::TextHorizontalAlignment::Left, QStringList() << text(), mContext, mFormat );
83}
84
86{
87 mFormat = format;
88 update();
89}
90
91void QgsTextPreview::updateContext()
92{
93 if ( mScale >= 0 )
94 {
95 const QgsMapToPixel newCoordXForm = QgsMapToPixel::fromScale( mScale, mMapUnits, mScreenHelper->screenDpi() );
96 mContext.setMapToPixel( newCoordXForm );
97 }
98 update();
99}
100
102{
103 mScale = scale;
104 updateContext();
105}
106
108{
109 mMapUnits = unit;
110 updateContext();
111}
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
Definition qgis.h:2705
DistanceUnit
Units of distance.
Definition qgis.h:5013
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size).
Definition qgis.h:5187
@ Antialiasing
Use antialiasing while drawing.
Definition qgis.h:2756
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 setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
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.
Container for all settings relating to text rendering.
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.