Skip to content

Commit 886e591

Browse files
author
wonder
committed
[FEATURE] Added font marker symbol layer
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12794 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 869735a commit 886e591

12 files changed

+625
-3
lines changed

src/core/symbology-ng/qgsmarkersymbollayerv2.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,85 @@ QString QgsSvgMarkerSymbolLayerV2::symbolPathToName( QString path )
467467

468468
return path;
469469
}
470+
471+
472+
//////////
473+
474+
QgsFontMarkerSymbolLayerV2::QgsFontMarkerSymbolLayerV2( QString fontFamily, QChar chr, double pointSize, QColor color, double angle )
475+
{
476+
mFontFamily = fontFamily;
477+
mChr = chr;
478+
mColor = color;
479+
mAngle = angle;
480+
mSize = pointSize;
481+
}
482+
483+
QgsSymbolLayerV2* QgsFontMarkerSymbolLayerV2::create( const QgsStringMap& props )
484+
{
485+
QString fontFamily = DEFAULT_FONTMARKER_FONT;
486+
QChar chr = DEFAULT_FONTMARKER_CHR;
487+
double pointSize = DEFAULT_FONTMARKER_SIZE;
488+
QColor color = DEFAULT_FONTMARKER_COLOR;
489+
double angle = DEFAULT_FONTMARKER_ANGLE;
490+
491+
if ( props.contains( "font" ) )
492+
fontFamily = props["font"];
493+
if ( props.contains( "chr" ) && props["chr"].length() > 0 )
494+
chr = props["chr"].at( 0 );
495+
if ( props.contains( "size" ) )
496+
pointSize = props["size"].toDouble();
497+
if ( props.contains( "color" ) )
498+
color = QgsSymbolLayerV2Utils::decodeColor( props["color"] );
499+
if ( props.contains( "angle" ) )
500+
angle = props["angle"].toDouble();
501+
502+
return new QgsFontMarkerSymbolLayerV2( fontFamily, chr, pointSize, color, angle );
503+
}
504+
505+
QString QgsFontMarkerSymbolLayerV2::layerType() const
506+
{
507+
return "FontMarker";
508+
}
509+
510+
void QgsFontMarkerSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
511+
{
512+
mFont = QFont( mFontFamily, MM2POINT( mSize ) );
513+
QFontMetrics fm( mFont );
514+
mChrOffset = QPointF( fm.width( mChr ) / 2, -fm.ascent() / 2 );
515+
}
516+
517+
void QgsFontMarkerSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
518+
{
519+
}
520+
521+
void QgsFontMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
522+
{
523+
QPainter* p = context.renderContext().painter();
524+
p->setPen( mColor );
525+
p->setFont( mFont );
526+
527+
if ( mAngle != 0 )
528+
{
529+
p->save();
530+
p->rotate( mAngle );
531+
}
532+
p->drawText( point - mChrOffset, mChr );
533+
if ( mAngle != 0 )
534+
p->restore();
535+
}
536+
537+
QgsStringMap QgsFontMarkerSymbolLayerV2::properties() const
538+
{
539+
QgsStringMap props;
540+
props["font"] = mFontFamily;
541+
props["chr"] = mChr;
542+
props["size"] = QString::number( mSize );
543+
props["color"] = QgsSymbolLayerV2Utils::encodeColor( mColor );
544+
props["angle"] = QString::number( mAngle );
545+
return props;
546+
}
547+
548+
QgsSymbolLayerV2* QgsFontMarkerSymbolLayerV2::clone() const
549+
{
550+
return new QgsFontMarkerSymbolLayerV2( mFontFamily, mChr, mSize, mColor, mAngle );
551+
}

src/core/symbology-ng/qgsmarkersymbollayerv2.h

+58
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <QBrush>
1515
#include <QPicture>
1616
#include <QPolygonF>
17+
#include <QFont>
1718

1819
class CORE_EXPORT QgsSimpleMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
1920
{
@@ -111,4 +112,61 @@ class CORE_EXPORT QgsSvgMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
111112
QPicture mPicture;
112113
};
113114

115+
116+
//////////
117+
118+
#define POINT2MM(x) ( (x) * 25.4 / 72 ) // point is 1/72 of inch
119+
#define MM2POINT(x) ( (x) * 72 / 25.4 )
120+
121+
#define DEFAULT_FONTMARKER_FONT "Dingbats"
122+
#define DEFAULT_FONTMARKER_CHR QChar('A')
123+
#define DEFAULT_FONTMARKER_SIZE POINT2MM(12)
124+
#define DEFAULT_FONTMARKER_COLOR QColor(Qt::black)
125+
#define DEFAULT_FONTMARKER_ANGLE 0
126+
127+
class CORE_EXPORT QgsFontMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
128+
{
129+
public:
130+
QgsFontMarkerSymbolLayerV2( QString fontFamily = DEFAULT_FONTMARKER_FONT,
131+
QChar chr = DEFAULT_FONTMARKER_CHR,
132+
double pointSize = DEFAULT_FONTMARKER_SIZE,
133+
QColor color = DEFAULT_FONTMARKER_COLOR,
134+
double angle = DEFAULT_FONTMARKER_ANGLE );
135+
136+
// static stuff
137+
138+
static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
139+
140+
// implemented from base classes
141+
142+
QString layerType() const;
143+
144+
void startRender( QgsSymbolV2RenderContext& context );
145+
146+
void stopRender( QgsSymbolV2RenderContext& context );
147+
148+
void renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context );
149+
150+
QgsStringMap properties() const;
151+
152+
QgsSymbolLayerV2* clone() const;
153+
154+
// new methods
155+
156+
QString fontFamily() const { return mFontFamily; }
157+
void setFontFamily( QString family ) { mFontFamily = family; }
158+
159+
QChar character() const { return mChr; }
160+
void setCharacter( QChar ch ) { mChr = ch; }
161+
162+
protected:
163+
164+
QString mFontFamily;
165+
QChar mChr;
166+
167+
QPointF mChrOffset;
168+
QFont mFont;
169+
};
170+
171+
114172
#endif

src/core/symbology-ng/qgssymbollayerv2registry.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ QgsSymbolLayerV2Registry::QgsSymbolLayerV2Registry()
2121
QgsSimpleMarkerSymbolLayerV2::create ) );
2222
addSymbolLayerType( new QgsSymbolLayerV2Metadata( "SvgMarker", QgsSymbolV2::Marker,
2323
QgsSvgMarkerSymbolLayerV2::create ) );
24+
addSymbolLayerType( new QgsSymbolLayerV2Metadata( "FontMarker", QgsSymbolV2::Marker,
25+
QgsFontMarkerSymbolLayerV2::create ) );
2426

2527
addSymbolLayerType( new QgsSymbolLayerV2Metadata( "SimpleFill", QgsSymbolV2::Fill,
2628
QgsSimpleFillSymbolLayerV2::create ) );
27-
2829
addSymbolLayerType( new QgsSymbolLayerV2Metadata( "SVGFill", QgsSymbolV2::Fill, QgsSVGFillSymbolLayer::create ) );
2930
}
3031

src/core/symbology-ng/qgssymbolv2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ QImage QgsSymbolV2::bigSymbolPreviewImage()
180180

181181
if ( mType == QgsSymbolV2::Marker )
182182
{
183-
p.setPen( QPen( QColor( 230, 230, 230 ) ) );
183+
p.setPen( QPen( Qt::gray ) );
184184
p.drawLine( 0, 50, 100, 50 );
185185
p.drawLine( 50, 0, 50, 100 );
186186
}

src/gui/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ symbology-ng/qgssymbolv2selectordialog.cpp
1717
symbology-ng/qgsvectorgradientcolorrampv2dialog.cpp
1818
symbology-ng/qgsvectorrandomcolorrampv2dialog.cpp
1919
symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.cpp
20+
symbology-ng/characterwidget.cpp
2021

2122
qgisgui.cpp
2223
qgisinterface.cpp
@@ -64,6 +65,7 @@ symbology-ng/qgssymbolv2selectordialog.h
6465
symbology-ng/qgsvectorgradientcolorrampv2dialog.h
6566
symbology-ng/qgsvectorrandomcolorrampv2dialog.h
6667
symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.h
68+
symbology-ng/characterwidget.h
6769

6870
qgscomposerview.h
6971
qgsdetaileditemdelegate.h
+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4+
** All rights reserved.
5+
** Contact: Nokia Corporation (qt-info@nokia.com)
6+
**
7+
** This file is part of the examples of the Qt Toolkit.
8+
**
9+
** $QT_BEGIN_LICENSE:LGPL$
10+
** Commercial Usage
11+
** Licensees holding valid Qt Commercial licenses may use this file in
12+
** accordance with the Qt Commercial License Agreement provided with the
13+
** Software or, alternatively, in accordance with the terms contained in
14+
** a written agreement between you and Nokia.
15+
**
16+
** GNU Lesser General Public License Usage
17+
** Alternatively, this file may be used under the terms of the GNU Lesser
18+
** General Public License version 2.1 as published by the Free Software
19+
** Foundation and appearing in the file LICENSE.LGPL included in the
20+
** packaging of this file. Please review the following information to
21+
** ensure the GNU Lesser General Public License version 2.1 requirements
22+
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23+
**
24+
** In addition, as a special exception, Nokia gives you certain additional
25+
** rights. These rights are described in the Nokia Qt LGPL Exception
26+
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27+
**
28+
** GNU General Public License Usage
29+
** Alternatively, this file may be used under the terms of the GNU
30+
** General Public License version 3.0 as published by the Free Software
31+
** Foundation and appearing in the file LICENSE.GPL included in the
32+
** packaging of this file. Please review the following information to
33+
** ensure the GNU General Public License version 3.0 requirements will be
34+
** met: http://www.gnu.org/copyleft/gpl.html.
35+
**
36+
** If you have questions regarding the use of this file, please contact
37+
** Nokia at qt-info@nokia.com.
38+
** $QT_END_LICENSE$
39+
**
40+
****************************************************************************/
41+
42+
#include <QtGui>
43+
44+
#include "characterwidget.h"
45+
46+
//! [0]
47+
CharacterWidget::CharacterWidget( QWidget *parent )
48+
: QWidget( parent )
49+
{
50+
squareSize = 24;
51+
columns = 16;
52+
lastKey = -1;
53+
setMouseTracking( true );
54+
}
55+
//! [0]
56+
57+
//! [1]
58+
void CharacterWidget::updateFont( const QFont &font )
59+
{
60+
displayFont.setFamily( font.family() );
61+
squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
62+
adjustSize();
63+
update();
64+
}
65+
//! [1]
66+
67+
//! [2]
68+
void CharacterWidget::updateSize( double fontSize )
69+
{
70+
displayFont.setPointSizeF( fontSize );
71+
squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
72+
adjustSize();
73+
update();
74+
}
75+
//! [2]
76+
77+
void CharacterWidget::updateStyle( const QString &fontStyle )
78+
{
79+
QFontDatabase fontDatabase;
80+
const QFont::StyleStrategy oldStrategy = displayFont.styleStrategy();
81+
displayFont = fontDatabase.font( displayFont.family(), fontStyle, displayFont.pointSize() );
82+
displayFont.setStyleStrategy( oldStrategy );
83+
squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
84+
adjustSize();
85+
update();
86+
}
87+
88+
void CharacterWidget::updateFontMerging( bool enable )
89+
{
90+
if ( enable )
91+
displayFont.setStyleStrategy( QFont::PreferDefault );
92+
else
93+
displayFont.setStyleStrategy( QFont::NoFontMerging );
94+
adjustSize();
95+
update();
96+
}
97+
98+
//! [3]
99+
QSize CharacterWidget::sizeHint() const
100+
{
101+
return QSize( columns*squareSize, ( 65536 / columns )*squareSize );
102+
}
103+
//! [3]
104+
105+
//! [4]
106+
void CharacterWidget::mouseMoveEvent( QMouseEvent *event )
107+
{
108+
QPoint widgetPosition = mapFromGlobal( event->globalPos() );
109+
uint key = ( widgetPosition.y() / squareSize ) * columns + widgetPosition.x() / squareSize;
110+
111+
QString text = QString::fromLatin1( "<p>Character: <span style=\"font-size: 24pt; font-family: %1\">" ).arg( displayFont.family() )
112+
+ QChar( key )
113+
+ QString::fromLatin1( "</span><p>Value: 0x" )
114+
+ QString::number( key, 16 );
115+
QToolTip::showText( event->globalPos(), text, this );
116+
}
117+
//! [4]
118+
119+
//! [5]
120+
void CharacterWidget::mousePressEvent( QMouseEvent *event )
121+
{
122+
if ( event->button() == Qt::LeftButton )
123+
{
124+
lastKey = ( event->y() / squareSize ) * columns + event->x() / squareSize;
125+
if ( QChar( lastKey ).category() != QChar::NoCategory )
126+
emit characterSelected( QChar( lastKey ) );
127+
update();
128+
}
129+
else
130+
QWidget::mousePressEvent( event );
131+
}
132+
//! [5]
133+
134+
//! [6]
135+
void CharacterWidget::paintEvent( QPaintEvent *event )
136+
{
137+
QPainter painter( this );
138+
painter.fillRect( event->rect(), QBrush( Qt::white ) );
139+
painter.setFont( displayFont );
140+
//! [6]
141+
142+
//! [7]
143+
QRect redrawRect = event->rect();
144+
int beginRow = redrawRect.top() / squareSize;
145+
int endRow = redrawRect.bottom() / squareSize;
146+
int beginColumn = redrawRect.left() / squareSize;
147+
int endColumn = redrawRect.right() / squareSize;
148+
//! [7]
149+
150+
//! [8]
151+
painter.setPen( QPen( Qt::gray ) );
152+
for ( int row = beginRow; row <= endRow; ++row )
153+
{
154+
for ( int column = beginColumn; column <= endColumn; ++column )
155+
{
156+
painter.drawRect( column*squareSize, row*squareSize, squareSize, squareSize );
157+
}
158+
//! [8] //! [9]
159+
}
160+
//! [9]
161+
162+
//! [10]
163+
QFontMetrics fontMetrics( displayFont );
164+
painter.setPen( QPen( Qt::black ) );
165+
for ( int row = beginRow; row <= endRow; ++row )
166+
{
167+
168+
for ( int column = beginColumn; column <= endColumn; ++column )
169+
{
170+
171+
int key = row * columns + column;
172+
painter.setClipRect( column*squareSize, row*squareSize, squareSize, squareSize );
173+
174+
if ( key == lastKey )
175+
painter.fillRect( column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush( Qt::red ) );
176+
177+
painter.drawText( column*squareSize + ( squareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2,
178+
row*squareSize + 4 + fontMetrics.ascent(),
179+
QString( QChar( key ) ) );
180+
}
181+
}
182+
}
183+
//! [10]

0 commit comments

Comments
 (0)