Skip to content

Commit fd7bf2d

Browse files
StevenBStevenB
StevenB
authored and
StevenB
committed
Implemented a hack to work around the Qt font bug for composer labels.
It doesn't fix composer map labels or the composer legend text yet. git-svn-id: http://svn.osgeo.org/qgis/trunk@7364 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 122259d commit fd7bf2d

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

src/app/composer/qgscomposerlabel.cpp

+31-30
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
3131
{
3232
setupUi(this);
3333

34-
std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
34+
//std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
3535

3636
mComposition = composition;
3737
mId = id;
@@ -55,7 +55,6 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
5555
mComposition->canvas()->addItem(this);
5656

5757
QAbstractGraphicsShapeItem::setZValue(100);
58-
//setActive(true); //no equivalent
5958
QAbstractGraphicsShapeItem::show();
6059
QAbstractGraphicsShapeItem::update();
6160

@@ -65,7 +64,7 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
6564
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id )
6665
: QAbstractGraphicsShapeItem(0)
6766
{
68-
std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
67+
//std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
6968

7069
setupUi(this);
7170

@@ -80,43 +79,32 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id )
8079
// Add to canvas
8180
mComposition->canvas()->addItem(this);
8281
QAbstractGraphicsShapeItem::setZValue(100);
83-
//setActive(true);//no equivalent
8482
QAbstractGraphicsShapeItem::show();
8583
QAbstractGraphicsShapeItem::update();
8684

8785
}
8886

8987
QgsComposerLabel::~QgsComposerLabel()
9088
{
91-
std::cout << "QgsComposerLabel::~QgsComposerLabel" << std::endl;
89+
//std::cout << "QgsComposerLabel::~QgsComposerLabel" << std::endl;
9290
QGraphicsItem::hide();
9391
}
94-
/*
95-
void QgsComposerLabel::drawShape ( QPainter & painter, const QStyleOptionGraphicsItem* item, QWidget* pWidget)
96-
{
97-
std::cout << "QgsComposerLabel::drawShape" << std::endl;
98-
paint ( painter, item, pWidget );
99-
}
100-
*/
10192

93+
#define FONT_WORKAROUND_SCALE 10
10294
void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
10395
{
104-
std::cout << "QgsComposerLabel::paint" << std::endl;
96+
//std::cout << "QgsComposerLabel::paint" << std::endl;
10597

10698
float size = 25.4 * mComposition->scale() * mFont.pointSizeFloat() / 72;
107-
mBoxBuffer = (int) ( size / 10 * mComposition->scale() );
99+
//mBoxBuffer = size / 10 * mComposition->scale();
108100
mBoxBuffer = 1;
109101

110-
111102
QFont font ( mFont );
112103
font.setPointSizeFloat ( size );
113104
QFontMetricsF metrics ( font );
114105

115106
// Not sure about Style Strategy, QFont::PreferMatch ?
116107
//font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias ) );
117-
118-
painter->setPen ( mPen );
119-
painter->setFont ( font );
120108

121109
double w = metrics.width ( mText );
122110
double h = metrics.height() - metrics.descent();
@@ -126,28 +114,41 @@ void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem
126114
QRectF boxRect;
127115
if ( mBox ) {
128116
//I don't know why the top coordinate is -h rather than -(h+mBoxBuffer), but it seems to work better.
129-
boxRect.setRect(-mBoxBuffer, -h, w + (2 * mBoxBuffer), h + (2 * mBoxBuffer));
117+
boxRect.setRect(-mBoxBuffer, -h, w + (mBoxBuffer * 2), h + (mBoxBuffer * 2));
130118
QBrush brush ( QColor(255,255,255) );
131119
painter->setBrush ( brush );
132120
painter->setPen(QPen(QColor(0, 0, 0), .2));
133121
painter->drawRect ( boxRect );
134122
}
135-
painter->setPen ( mPen );
136123

124+
125+
/*This code doesn't do anything...?
137126
// The width is not sufficient in postscript
138127
QRectF tr = r;
139128
tr.setWidth ( r.width() );
129+
*/
130+
131+
font.setPointSizeFloat ( size * FONT_WORKAROUND_SCALE ); //Hack to work around Qt font bug
132+
painter->setFont ( font );
133+
painter->setPen ( mPen );
140134

141135
if ( plotStyle() == QgsComposition::Postscript )
142136
{
143137
// This metrics.ascent() is empirical
144-
size = metrics.ascent() * 72.0 / mComposition->resolution();
138+
size = metrics.ascent() * 72.0 / mComposition->resolution() * FONT_WORKAROUND_SCALE;
145139
font.setPointSizeF ( size );
146140
painter->setFont ( font );
147-
std::cout << "label using PS render size" << std::endl;
148-
}
141+
//std::cout << "label using PS render size" << std::endl;
142+
}
143+
144+
//Hack to work around the Qt font bug
145+
painter->save();
146+
painter->scale(1./FONT_WORKAROUND_SCALE, 1./FONT_WORKAROUND_SCALE);
147+
149148
painter->drawText(0, 0, mText);
150149

150+
painter->restore(); //undo our scaling of painter - End of the font workaround
151+
151152
// Show selected / Highlight
152153
if ( mSelected && plotStyle() == QgsComposition::Preview ) {
153154
QRectF hr;
@@ -202,10 +203,10 @@ QRectF QgsComposerLabel::boundingRect ( void ) const
202203

203204
QFont font ( mFont );
204205
font.setPointSizeFloat ( size );
205-
QFontMetrics metrics ( font );
206+
QFontMetricsF metrics ( font );
206207

207-
int w = metrics.width ( mText );
208-
int h = metrics.height() - metrics.descent();
208+
double w = metrics.width ( mText );
209+
double h = metrics.height() - metrics.descent();
209210

210211
/*
211212
int buf = 0;
@@ -217,11 +218,11 @@ QRectF QgsComposerLabel::boundingRect ( void ) const
217218
QRectF r ( (int)(x - w/2 - 1.5*buf), (int) (y - h/2 - buf), (int)(w+3*buf), h+2*buf );
218219
*/
219220

220-
QRectF r;
221+
QRectF r;
221222

222223
if(mBox){
223224
//what happens if we haven't called paint() first?
224-
r.setRect(-mBoxBuffer, -h, w + (2 * mBoxBuffer), h + (2 * mBoxBuffer));
225+
r.setRect(-mBoxBuffer, -h, w + (mBoxBuffer * 2), h + (mBoxBuffer * 2));
225226
}
226227
else{
227228
r.setRect(0, -h, w, h);
@@ -233,7 +234,7 @@ QRectF r;
233234

234235
QPolygonF QgsComposerLabel::areaPoints() const
235236
{
236-
std::cout << "QgsComposerLabel::areaPoints" << std::endl;
237+
//std::cout << "QgsComposerLabel::areaPoints" << std::endl;
237238
QRectF r = boundingRect();
238239

239240
QPolygonF pa;
@@ -263,7 +264,7 @@ void QgsComposerLabel::on_mTextLineEdit_returnPressed()
263264

264265
void QgsComposerLabel::setSelected ( bool s )
265266
{
266-
std::cout << "QgsComposerLabel::setSelected" << std::endl;
267+
//std::cout << "QgsComposerLabel::setSelected" << std::endl;
267268
mSelected = s;
268269
QAbstractGraphicsShapeItem::update(); // show highlight
269270

src/app/composer/qgscomposerlabel.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class QgsComposerLabel : public QWidget, private Ui::QgsComposerLabelBase, publi
6868
/** \brief Reimplementation of QGraphicsItem::paint() - draw on canvas */
6969
void paint ( QPainter*, const QStyleOptionGraphicsItem*, QWidget* );
7070

71-
// void drawShape(QPainter&, const QStyleOptionGraphicsItem*, QWidget*);
7271
QPolygonF areaPoints() const;
7372

7473
/** \brief Set values in GUI to current values */
@@ -102,13 +101,14 @@ public slots:
102101
int mMargin;
103102

104103
// Current bounding box
105-
QRect mBoundingRect;
104+
// Not used - would it be more effecient if paint() updated the bounding box, and boundingRect returned this?
105+
//QRect mBoundingRect;
106106

107107
// Draw box around the label
108108
bool mBox;
109109

110110
// Box buffer
111-
int mBoxBuffer;
111+
double mBoxBuffer;
112112
};
113113

114114
#endif

0 commit comments

Comments
 (0)