@@ -91,44 +91,39 @@ QgsComposerLabel::~QgsComposerLabel()
91
91
}
92
92
93
93
#define FONT_WORKAROUND_SCALE 10
94
+ // This partially resolves a problem with the bounding rectangle for the text
95
+ // being too short for most font sizes.
96
+ #define WIDTH_EXTENSION 1.0
94
97
void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
95
98
{
96
99
// std::cout << "QgsComposerLabel::paint" << std::endl;
97
100
98
- float size = 25.4 * mComposition ->scale () * mFont .pointSizeFloat () / 72 ;
101
+ float size = 25.4 * mComposition ->scale () * mFont .pointSizeF () / 72 ;
102
+
99
103
// mBoxBuffer = size / 10 * mComposition->scale();
100
- mBoxBuffer = 1 ;
104
+ mBoxBuffer = 0 ;
101
105
102
106
QFont font ( mFont );
103
- font.setPointSizeFloat ( size );
104
- QFontMetricsF metrics ( font );
107
+ font.setStyleStrategy ( QFont::StyleStrategy (QFont::PreferOutline | QFont::PreferAntialias) );
108
+ font. setPointSizeF ( size );
105
109
106
- // Not sure about Style Strategy, QFont::PreferMatch ?
107
- // font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias ) );
108
-
109
- double w = metrics.width ( mText );
110
- double h = metrics.height () - metrics.descent ();
110
+ QFontMetricsF metrics ( font );
111
+ QSizeF textSize = metrics.size (0 , mText );
112
+ qreal w = textSize.width () + WIDTH_EXTENSION;
113
+ qreal h = textSize.height ();
111
114
112
115
QRectF r (0 , -h, w, h); // used as the rectangle to draw the selection boxes on the corners of if there is no box
113
116
114
117
QRectF boxRect;
115
118
if ( mBox ) {
116
- // I don't know why the top coordinate is -h rather than -(h+mBoxBuffer), but it seems to work better.
117
- boxRect.setRect (-mBoxBuffer , -h, w + (mBoxBuffer * 2 ), h + (mBoxBuffer * 2 ));
119
+ boxRect.setRect (-mBoxBuffer , -(h+mBoxBuffer ), w + (mBoxBuffer * 2 )+1 , h + (mBoxBuffer * 2 ));
118
120
QBrush brush ( QColor (255 ,255 ,255 ) );
119
121
painter->setBrush ( brush );
120
122
painter->setPen (QPen (QColor (0 , 0 , 0 ), .2 ));
121
123
painter->drawRect ( boxRect );
122
124
}
123
125
124
-
125
- /* This code doesn't do anything...?
126
- // The width is not sufficient in postscript
127
- QRectF tr = r;
128
- tr.setWidth ( r.width() );
129
- */
130
-
131
- font.setPointSizeFloat ( size * FONT_WORKAROUND_SCALE ); // Hack to work around Qt font bug
126
+ font.setPointSizeF ( size * FONT_WORKAROUND_SCALE );
132
127
painter->setFont ( font );
133
128
painter->setPen ( mPen );
134
129
@@ -142,11 +137,14 @@ void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem
142
137
}
143
138
144
139
// Hack to work around the Qt font bug
140
+
145
141
painter->save ();
146
142
painter->scale (1 ./FONT_WORKAROUND_SCALE, 1 ./FONT_WORKAROUND_SCALE);
147
-
148
- painter->drawText (0 , 0 , mText );
149
-
143
+ QRectF scaledBox (r.x () * FONT_WORKAROUND_SCALE,
144
+ r.y () * FONT_WORKAROUND_SCALE,
145
+ (r.width () + WIDTH_EXTENSION) * FONT_WORKAROUND_SCALE,
146
+ r.height () * FONT_WORKAROUND_SCALE);
147
+ painter->drawText (scaledBox, Qt::AlignLeft|Qt::AlignVCenter, mText );
150
148
painter->restore (); // undo our scaling of painter - End of the font workaround
151
149
152
150
// Show selected / Highlight
@@ -157,6 +155,7 @@ void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem
157
155
} else {
158
156
hr = r;
159
157
}
158
+ hr.setWidth (hr.width () + WIDTH_EXTENSION);
160
159
painter->setPen ( mComposition ->selectionPen () );
161
160
painter->setBrush ( mComposition ->selectionBrush () );
162
161
@@ -202,11 +201,12 @@ QRectF QgsComposerLabel::boundingRect ( void ) const
202
201
float size = 25.4 * mComposition ->scale () * mFont .pointSizeFloat () / 72 ;
203
202
204
203
QFont font ( mFont );
205
- font.setPointSizeFloat ( size );
204
+ font.setPointSizeF ( size );
206
205
QFontMetricsF metrics ( font );
207
206
208
- double w = metrics.width ( mText );
209
- double h = metrics.height () - metrics.descent ();
207
+ QSizeF textSize = metrics.size (0 , mText );
208
+ double w = textSize.width () + WIDTH_EXTENSION;
209
+ double h = textSize.height ();
210
210
211
211
/*
212
212
int buf = 0;
@@ -222,7 +222,7 @@ QRectF QgsComposerLabel::boundingRect ( void ) const
222
222
223
223
if (mBox ){
224
224
// what happens if we haven't called paint() first?
225
- r.setRect (-mBoxBuffer , -h , w + (mBoxBuffer * 2 ), h + (mBoxBuffer * 2 ));
225
+ r.setRect (-mBoxBuffer , -(h+ mBoxBuffer ) , w + (mBoxBuffer * 2 ), h + (mBoxBuffer * 2 ));
226
226
}
227
227
else {
228
228
r.setRect (0 , -h, w, h);
@@ -248,15 +248,15 @@ QPolygonF QgsComposerLabel::areaPoints() const
248
248
249
249
void QgsComposerLabel::setOptions ( void )
250
250
{
251
- mTextLineEdit ->setText ( mText );
251
+ mTextEdit ->setText ( mText );
252
252
mBoxCheckBox ->setChecked ( mBox );
253
253
254
254
}
255
255
256
- void QgsComposerLabel::on_mTextLineEdit_returnPressed ()
256
+ void QgsComposerLabel::on_mTextEdit_textChanged ()
257
257
{
258
258
QRectF r = boundingRect ();
259
- mText = mTextLineEdit ->text ();
259
+ mText = mTextEdit ->text ();
260
260
QAbstractGraphicsShapeItem::prepareGeometryChange ();
261
261
QAbstractGraphicsShapeItem::update ();
262
262
writeSettings ();
0 commit comments