QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
34 
35  mContext.setIsGuiPreview( true );
36 }
37 
38 
39 void QgsTextPreview::paintEvent( QPaintEvent *e )
40 {
41  Q_UNUSED( e )
42  QPainter p( this );
43 
44  p.setRenderHint( QPainter::Antialiasing );
45 
46  // slightly inset text
47  double xtrans = 0;
48  if ( mFormat.buffer().enabled() )
49  xtrans = mContext.convertToPainterUnits( mFormat.buffer().size(), mFormat.buffer().sizeUnit(), mFormat.buffer().sizeMapUnitScale() );
51  xtrans = std::max( xtrans, mContext.convertToPainterUnits( mFormat.background().size().width(), mFormat.background().sizeUnit(), mFormat.background().sizeMapUnitScale() ) );
52  xtrans += 4;
53 
54  double ytrans = 0.0;
55  if ( mFormat.buffer().enabled() )
56  ytrans = std::max( ytrans, mContext.convertToPainterUnits( mFormat.buffer().size(), mFormat.buffer().sizeUnit(), mFormat.buffer().sizeMapUnitScale() ) );
57  if ( mFormat.background().enabled() )
58  ytrans = std::max( ytrans, mContext.convertToPainterUnits( mFormat.background().size().height(), mFormat.background().sizeUnit(), mFormat.background().sizeMapUnitScale() ) );
59  ytrans += 4;
60 
61  QRectF textRect = rect();
62  textRect.setLeft( xtrans );
63  textRect.setWidth( textRect.width() - xtrans );
64  textRect.setTop( ytrans );
65  if ( textRect.height() > 300 )
66  textRect.setHeight( 300 );
67  if ( textRect.width() > 2000 )
68  textRect.setWidth( 2000 );
69 
70  mContext.setPainter( &p );
71  QgsTextRenderer::drawText( textRect, 0, QgsTextRenderer::AlignLeft, QStringList() << text(),
72  mContext, mFormat );
73 }
74 
76 {
77  mFormat = format;
78  update();
79 }
80 
81 void QgsTextPreview::updateContext()
82 {
83  if ( mScale >= 0 )
84  {
85  const QgsMapToPixel newCoordXForm = QgsMapToPixel::fromScale( mScale, mMapUnits, QgsApplication::desktop()->logicalDpiX() );
86  mContext.setMapToPixel( newCoordXForm );
87  }
88  update();
89 }
90 
91 void QgsTextPreview::setScale( double scale )
92 {
93  mScale = scale;
94  updateContext();
95 }
96 
98 {
99  mMapUnits = unit;
100  updateContext();
101 }
@ Antialiasing
Use antialiasing while drawing.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
static QgsMapToPixel fromScale(double scale, QgsUnitTypes::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, QgsUnitTypes::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.
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.
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units used for the shape's 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.
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the buffer size.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
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 setFormat(const QgsTextFormat &format)
Sets the text format for previewing in the widget.
void setMapUnits(QgsUnitTypes::DistanceUnit unit)
Sets the map unit type for previewing format sizes in map units.
@ AlignLeft
Left align.
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, VAlignment vAlignment=AlignTop)
Draws text within a rectangle using the specified settings.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68