Skip to content
Permalink
Browse files
Data defined font marker character
  • Loading branch information
nyalldawson committed May 22, 2015
1 parent 97de55d commit ef0d4138196c6026dc59930183a13381e8ef0a57
@@ -159,6 +159,8 @@ class QgsFontMarkerSymbolLayerV2 : QgsMarkerSymbolLayerV2
QColor color = DEFAULT_FONTMARKER_COLOR,
double angle = DEFAULT_FONTMARKER_ANGLE );

~QgsFontMarkerSymbolLayerV2();

// static stuff

static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() ) /Factory/;
@@ -181,6 +181,7 @@ class QgsDataDefinedButton : QToolButton
* Common descriptions for expected input values
*/
static QString trString();
static QString charDesc();
static QString boolDesc();
static QString anyStringDesc();
static QString intDesc();
@@ -1637,6 +1637,7 @@ bool QgsSvgMarkerSymbolLayerV2::writeDxf( QgsDxfExport& e, double mmMapUnitScale
//////////

QgsFontMarkerSymbolLayerV2::QgsFontMarkerSymbolLayerV2( QString fontFamily, QChar chr, double pointSize, QColor color, double angle )
: mFontMetrics( 0 )
{
mFontFamily = fontFamily;
mChr = chr;
@@ -1649,6 +1650,11 @@ QgsFontMarkerSymbolLayerV2::QgsFontMarkerSymbolLayerV2( QString fontFamily, QCha
mOffsetUnit = QgsSymbolV2::MM;
}

QgsFontMarkerSymbolLayerV2::~QgsFontMarkerSymbolLayerV2()
{
delete mFontMetrics;
}

QgsSymbolLayerV2* QgsFontMarkerSymbolLayerV2::create( const QgsStringMap& props )
{
QString fontFamily = DEFAULT_FONTMARKER_FONT;
@@ -1702,8 +1708,9 @@ void QgsFontMarkerSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context
{
mFont = QFont( mFontFamily );
mFont.setPixelSize( mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mSizeUnit, mSizeMapUnitScale ) );
QFontMetrics fm( mFont );
mChrOffset = QPointF( fm.width( mChr ) / 2, -fm.ascent() / 2 );
delete mFontMetrics;
mFontMetrics = new QFontMetrics( mFont );
mChrOffset = QPointF( mFontMetrics->width( mChr ) / 2.0, -mFontMetrics->ascent() / 2.0 );
mOrigSize = mSize; // save in case the size would be data defined
prepareExpressions( context.fields(), context.renderContext().rendererScale() );
}
@@ -1735,6 +1742,17 @@ void QgsFontMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2R

p->save();

QPointF chrOffset = mChrOffset;
QString charToRender = mChr;
if ( hasDataDefinedProperty( "char" ) )
{
charToRender = evaluateDataDefinedProperty( "char", context.feature(), mChr ).toString();
if ( charToRender != mChr )
{
chrOffset = QPointF( mFontMetrics->width( charToRender ) / 2.0, -mFontMetrics->ascent() / 2.0 );
}
}

double scaledSize = mSize;

bool hasDataDefinedSize = context.renderHints() & QgsSymbolV2::DataDefinedSizeScale || hasDataDefinedProperty( "size" );
@@ -1803,7 +1821,7 @@ void QgsFontMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2R
if ( rotated )
p->rotate( angle );

p->drawText( -mChrOffset, mChr );
p->drawText( -chrOffset, charToRender );
p->restore();
}

@@ -231,6 +231,8 @@ class CORE_EXPORT QgsFontMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
QColor color = DEFAULT_FONTMARKER_COLOR,
double angle = DEFAULT_FONTMARKER_ANGLE );

~QgsFontMarkerSymbolLayerV2();

// static stuff

static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
@@ -263,11 +265,13 @@ class CORE_EXPORT QgsFontMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
protected:

QString mFontFamily;
QFontMetrics* mFontMetrics;
QChar mChr;

QPointF mChrOffset;
QFont mFont;
double mOrigSize;

};


@@ -663,6 +663,11 @@ QString QgsDataDefinedButton::trString()
return tr( "string " );
}

QString QgsDataDefinedButton::charDesc()
{
return tr( "single character" );
}

QString QgsDataDefinedButton::boolDesc()
{
return tr( "bool [<b>1</b>=True|<b>0</b>=False]" );
@@ -207,6 +207,7 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
* Common descriptions for expected input values
*/
static QString trString();
static QString charDesc();
static QString boolDesc();
static QString anyStringDesc();
static QString intDesc();
@@ -2575,6 +2575,7 @@ void QgsFontMarkerSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer )
registerDataDefinedButton( mOffsetDDBtn, "offset", QgsDataDefinedButton::String, QgsDataDefinedButton::doubleXYDesc() );
registerDataDefinedButton( mHorizontalAnchorDDBtn, "horizontal_anchor_point", QgsDataDefinedButton::String, QgsDataDefinedButton::horizontalAnchorDesc() );
registerDataDefinedButton( mVerticalAnchorDDBtn, "vertical_anchor_point", QgsDataDefinedButton::String, QgsDataDefinedButton::verticalAnchorDesc() );
registerDataDefinedButton( mCharDDBtn, "char", QgsDataDefinedButton::String, QgsDataDefinedButton::charDesc() );

updateAssistantSymbol();
}
@@ -13,8 +13,8 @@
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
@@ -290,19 +290,43 @@
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QScrollArea" name="scrollArea">
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>378</width>
<height>158</height>
</rect>
</property>
</widget>
</widget>
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="1">
<widget class="QgsDataDefinedButton" name="mCharDDBtn">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0" rowspan="2">
<widget class="QScrollArea" name="scrollArea">
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>378</width>
<height>158</height>
</rect>
</property>
</widget>
</widget>
</item>
</layout>
</item>
</layout>
</widget>

0 comments on commit ef0d413

Please sign in to comment.