@@ -31,7 +31,7 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
31
31
{
32
32
setupUi (this );
33
33
34
- std::cout << " QgsComposerLabel::QgsComposerLabel()" << std::endl;
34
+ // std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
35
35
36
36
mComposition = composition;
37
37
mId = id;
@@ -55,7 +55,6 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
55
55
mComposition ->canvas ()->addItem (this );
56
56
57
57
QAbstractGraphicsShapeItem::setZValue (100 );
58
- // setActive(true); //no equivalent
59
58
QAbstractGraphicsShapeItem::show ();
60
59
QAbstractGraphicsShapeItem::update ();
61
60
@@ -65,7 +64,7 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
65
64
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id )
66
65
: QAbstractGraphicsShapeItem(0 )
67
66
{
68
- std::cout << " QgsComposerLabel::QgsComposerLabel()" << std::endl;
67
+ // std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
69
68
70
69
setupUi (this );
71
70
@@ -80,43 +79,32 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id )
80
79
// Add to canvas
81
80
mComposition ->canvas ()->addItem (this );
82
81
QAbstractGraphicsShapeItem::setZValue (100 );
83
- // setActive(true);//no equivalent
84
82
QAbstractGraphicsShapeItem::show ();
85
83
QAbstractGraphicsShapeItem::update ();
86
84
87
85
}
88
86
89
87
QgsComposerLabel::~QgsComposerLabel ()
90
88
{
91
- std::cout << " QgsComposerLabel::~QgsComposerLabel" << std::endl;
89
+ // std::cout << "QgsComposerLabel::~QgsComposerLabel" << std::endl;
92
90
QGraphicsItem::hide ();
93
91
}
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
- */
101
92
93
+ #define FONT_WORKAROUND_SCALE 10
102
94
void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
103
95
{
104
- std::cout << " QgsComposerLabel::paint" << std::endl;
96
+ // std::cout << "QgsComposerLabel::paint" << std::endl;
105
97
106
98
float size = 25.4 * mComposition ->scale () * mFont .pointSizeFloat () / 72 ;
107
- mBoxBuffer = ( int ) ( size / 10 * mComposition ->scale () );
99
+ // mBoxBuffer = size / 10 * mComposition->scale();
108
100
mBoxBuffer = 1 ;
109
101
110
-
111
102
QFont font ( mFont );
112
103
font.setPointSizeFloat ( size );
113
104
QFontMetricsF metrics ( font );
114
105
115
106
// Not sure about Style Strategy, QFont::PreferMatch ?
116
107
// font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias ) );
117
-
118
- painter->setPen ( mPen );
119
- painter->setFont ( font );
120
108
121
109
double w = metrics.width ( mText );
122
110
double h = metrics.height () - metrics.descent ();
@@ -126,28 +114,41 @@ void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem
126
114
QRectF boxRect;
127
115
if ( mBox ) {
128
116
// 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 ));
130
118
QBrush brush ( QColor (255 ,255 ,255 ) );
131
119
painter->setBrush ( brush );
132
120
painter->setPen (QPen (QColor (0 , 0 , 0 ), .2 ));
133
121
painter->drawRect ( boxRect );
134
122
}
135
- painter->setPen ( mPen );
136
123
124
+
125
+ /* This code doesn't do anything...?
137
126
// The width is not sufficient in postscript
138
127
QRectF tr = r;
139
128
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 );
140
134
141
135
if ( plotStyle () == QgsComposition::Postscript )
142
136
{
143
137
// This metrics.ascent() is empirical
144
- size = metrics.ascent () * 72.0 / mComposition ->resolution ();
138
+ size = metrics.ascent () * 72.0 / mComposition ->resolution () * FONT_WORKAROUND_SCALE ;
145
139
font.setPointSizeF ( size );
146
140
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
+
149
148
painter->drawText (0 , 0 , mText );
150
149
150
+ painter->restore (); // undo our scaling of painter - End of the font workaround
151
+
151
152
// Show selected / Highlight
152
153
if ( mSelected && plotStyle () == QgsComposition::Preview ) {
153
154
QRectF hr;
@@ -202,10 +203,10 @@ QRectF QgsComposerLabel::boundingRect ( void ) const
202
203
203
204
QFont font ( mFont );
204
205
font.setPointSizeFloat ( size );
205
- QFontMetrics metrics ( font );
206
+ QFontMetricsF metrics ( font );
206
207
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 ();
209
210
210
211
/*
211
212
int buf = 0;
@@ -217,11 +218,11 @@ QRectF QgsComposerLabel::boundingRect ( void ) const
217
218
QRectF r ( (int)(x - w/2 - 1.5*buf), (int) (y - h/2 - buf), (int)(w+3*buf), h+2*buf );
218
219
*/
219
220
220
- QRectF r;
221
+ QRectF r;
221
222
222
223
if (mBox ){
223
224
// 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 ));
225
226
}
226
227
else {
227
228
r.setRect (0 , -h, w, h);
@@ -233,7 +234,7 @@ QRectF r;
233
234
234
235
QPolygonF QgsComposerLabel::areaPoints () const
235
236
{
236
- std::cout << " QgsComposerLabel::areaPoints" << std::endl;
237
+ // std::cout << "QgsComposerLabel::areaPoints" << std::endl;
237
238
QRectF r = boundingRect ();
238
239
239
240
QPolygonF pa;
@@ -263,7 +264,7 @@ void QgsComposerLabel::on_mTextLineEdit_returnPressed()
263
264
264
265
void QgsComposerLabel::setSelected ( bool s )
265
266
{
266
- std::cout << " QgsComposerLabel::setSelected" << std::endl;
267
+ // std::cout << "QgsComposerLabel::setSelected" << std::endl;
267
268
mSelected = s;
268
269
QAbstractGraphicsShapeItem::update (); // show highlight
269
270
0 commit comments