Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
New widget QgsTextPreview for previewing all formatting for QgsTextRe…
…nderer Switch the labeling gui to use this widget, which has the benefits: - previews all label settings, including shadow and background - previews at a specified scale, so that any sizes using map units will be correct
- Loading branch information
Showing
8 changed files
with
576 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** \class QgsTextPreview | ||
* \ingroup gui | ||
* A widget for previewing text formatting settings. | ||
* | ||
* QgsTextPreview provides a widget for previewing the appearance of text rendered | ||
* using QgsTextRenderer. The preview includes all settings contained within | ||
* a QgsTextFormat, including shadow, background and buffer. | ||
* | ||
* In order to preview the exact appearance of text which uses sizes in map units, | ||
* the scale and map units must be set by calling setScale() and setMapUnits(). | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
|
||
class QgsTextPreview : public QLabel | ||
{ | ||
%TypeHeaderCode | ||
#include <qgstextpreview.h> | ||
%End | ||
public: | ||
|
||
/** Constructor for QgsTextPreview | ||
* @param parent parent widget | ||
*/ | ||
QgsTextPreview( QWidget* parent = nullptr ); | ||
|
||
void paintEvent( QPaintEvent* e ); | ||
|
||
/** Sets the text format for previewing in the widget. | ||
* @param format text format | ||
* @see format() | ||
*/ | ||
void setFormat( const QgsTextFormat& format ); | ||
|
||
/** Returns the text format used for previewing text in the widget. | ||
* @see setFormat() | ||
*/ | ||
QgsTextFormat format() const; | ||
|
||
/** Sets the scale to use for previewing format sizes in map units. | ||
* @param scale preview map scale | ||
* @see scale() | ||
* @see setMapUnits() | ||
*/ | ||
void setScale( double scale ); | ||
|
||
/** Returns the scale used for previewing format sizes in map units. | ||
* @see setScale() | ||
* @see mapUnits() | ||
*/ | ||
double scale() const; | ||
|
||
/** Sets the map unit type for previewing format sizes in map units. | ||
* @param unit map units | ||
* @see mapUnits() | ||
* @see setScale() | ||
*/ | ||
void setMapUnits( QgsUnitTypes::DistanceUnit unit ); | ||
|
||
/** Returns the map unit type used for previewing format sizes in map units. | ||
* @see setMapUnits() | ||
* @see scale() | ||
*/ | ||
QgsUnitTypes::DistanceUnit mapUnits() const; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/*************************************************************************** | ||
qgstextpreview.cpp | ||
------------------ | ||
begin : October 2016 | ||
copyright : (C) 2016 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgstextpreview.h" | ||
#include <QDesktopWidget> | ||
#include <QPainter> | ||
|
||
QgsTextPreview::QgsTextPreview( QWidget* parent ) | ||
: QLabel( parent ) | ||
, mScale( -1 ) | ||
, mMapUnits( QgsUnitTypes::DistanceMeters ) | ||
{ | ||
// initially use a basic transform with no scale | ||
QgsMapToPixel newCoordXForm; | ||
newCoordXForm.setParameters( 1, 0, 0, 0, 0, 0 ); | ||
mContext.setMapToPixel( newCoordXForm ); | ||
|
||
mContext.setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 ); | ||
mContext.setUseAdvancedEffects( true ); | ||
} | ||
|
||
|
||
void QgsTextPreview::paintEvent( QPaintEvent *e ) | ||
{ | ||
Q_UNUSED( e ); | ||
QPainter p( this ); | ||
|
||
p.setRenderHint( QPainter::Antialiasing ); | ||
|
||
// slightly inset text | ||
double xtrans = 0; | ||
if ( mFormat.buffer().enabled() ) | ||
xtrans = QgsTextRenderer::scaleToPixelContext( mFormat.buffer().size(), mContext, mFormat.buffer().sizeUnit(), false, mFormat.buffer().sizeMapUnitScale() ); | ||
if ( mFormat.background().enabled() && mFormat.background().sizeType() != QgsTextBackgroundSettings::SizeFixed ) | ||
xtrans = qMax( xtrans, QgsTextRenderer::scaleToPixelContext( mFormat.background().size().width(), mContext, mFormat.background().sizeUnit(), false, mFormat.background().sizeMapUnitScale() ) ); | ||
xtrans += 4; | ||
|
||
double ytrans = 0.0; | ||
if ( mFormat.buffer().enabled() ) | ||
ytrans = qMax( ytrans, QgsTextRenderer::scaleToPixelContext( mFormat.buffer().size(), mContext, mFormat.buffer().sizeUnit(), false, mFormat.buffer().sizeMapUnitScale() ) ); | ||
if ( mFormat.background().enabled() ) | ||
ytrans = qMax( ytrans, QgsTextRenderer::scaleToPixelContext( mFormat.background().size().height(), mContext, mFormat.background().sizeUnit(), false, mFormat.background().sizeMapUnitScale() ) ); | ||
ytrans += 4; | ||
|
||
QRectF textRect = rect(); | ||
textRect.setLeft( xtrans ); | ||
textRect.setWidth( textRect.width() - xtrans ); | ||
textRect.setTop( ytrans ); | ||
if ( textRect.height() > 300 ) | ||
textRect.setHeight( 300 ); | ||
if ( textRect.width() > 2000 ) | ||
textRect.setWidth( 2000 ); | ||
|
||
mContext.setPainter( &p ); | ||
QgsTextRenderer::drawText( textRect, 0 , QgsTextRenderer::AlignLeft, QStringList() << text(), | ||
mContext, mFormat ); | ||
} | ||
|
||
void QgsTextPreview::setFormat( const QgsTextFormat& format ) | ||
{ | ||
mFormat = format; | ||
update(); | ||
} | ||
|
||
void QgsTextPreview::updateContext() | ||
{ | ||
if ( mScale >= 0 ) | ||
{ | ||
QgsMapToPixel newCoordXForm = QgsMapToPixel::fromScale( mScale, mMapUnits, QgsApplication::desktop()->logicalDpiX() ); | ||
mContext.setMapToPixel( newCoordXForm ); | ||
} | ||
update(); | ||
} | ||
|
||
void QgsTextPreview::setScale( double scale ) | ||
{ | ||
mScale = scale; | ||
updateContext(); | ||
} | ||
|
||
void QgsTextPreview::setMapUnits( QgsUnitTypes::DistanceUnit unit ) | ||
{ | ||
mMapUnits = unit; | ||
updateContext(); | ||
} |
Oops, something went wrong.