Skip to content

Commit 030c580

Browse files
StevenBStevenB
authored andcommittedNov 22, 2007
Fixed bug #799 and converted legend measurements to floating-point.
Also fixed a bug where the legend lines would extend past their endpoints. git-svn-id: http://svn.osgeo.org/qgis/trunk@7638 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 722498b commit 030c580

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed
 

‎src/app/composer/qgscomposervectorlegend.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
187187
font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias) );
188188

189189

190-
QFontMetrics titleMetrics ( titleFont );
191-
QFontMetrics sectionMetrics ( sectionFont );
192-
QFontMetrics metrics ( font );
190+
QFontMetricsF titleMetrics ( titleFont );
191+
QFontMetricsF sectionMetrics ( sectionFont );
192+
QFontMetricsF metrics ( font );
193193

194194
if ( plotStyle() == QgsComposition::Postscript) //do we need seperate PostScript rendering settings?
195195
{
@@ -209,7 +209,7 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
209209
font.setPointSizeFloat ( size * FONT_WORKAROUND_SCALE );
210210
}
211211

212-
int x, y;
212+
double x, y;
213213

214214
// Legend title -if we do this later, we can center it
215215
y = mMargin + titleMetrics.height();
@@ -220,13 +220,13 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
220220
painter->save(); //Save the painter state so we can undo the scaling later
221221
painter->scale(1./FONT_WORKAROUND_SCALE, 1./FONT_WORKAROUND_SCALE); //scale the painter to work around the font bug
222222

223-
painter->drawText( 2 * mMargin * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE, mTitle );
223+
painter->drawText( QPointF(2 * mMargin * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE), mTitle );
224224

225225
painter->restore();
226226

227-
//used to keep track of total width and height - probably should be changed to float/double
228-
int width = 4 * mMargin + titleMetrics.width ( mTitle );
229-
int height = mMargin + mSymbolSpace + titleMetrics.height(); // mSymbolSpace?
227+
//used to keep track of total width and height
228+
double width = 4 * mMargin + titleMetrics.width ( mTitle );
229+
double height = mMargin + mSymbolSpace + titleMetrics.height(); // mSymbolSpace?
230230

231231
// Layers
232232
QgsComposerMap *map = mComposition->map ( mMap ); //Get the map from the composition by ID number
@@ -255,7 +255,7 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
255255

256256
// Make list of all layers in the group and count section items
257257
std::vector<int> groupLayers; // vector of layers
258-
std::vector<int> itemHeights; // maximum item sizes
258+
std::vector<double> itemHeights; // maximum item sizes
259259
std::vector<QString> itemLabels; // item labels
260260
int sectionItemsCount = 0;
261261
QString sectionTitle;
@@ -309,7 +309,7 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
309309

310310
QPixmap pic = QPixmap::fromImage(sym->getPointSymbolAsImage(widthScale, false));
311311

312-
int h = (int) ( scale * pic.height() );
312+
double h = scale * pic.height();
313313
if ( h > itemHeights[icnt] ) {
314314
itemHeights[icnt] = h;
315315
}
@@ -337,31 +337,31 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
337337
{
338338
height += mSymbolSpace;
339339

340-
x = (int) ( 2*mMargin );
341-
y = (int) ( height + sectionMetrics.height() );
340+
x = 2*mMargin;
341+
y = height + sectionMetrics.height();
342342
painter->setPen ( mPen );
343343
painter->setFont ( sectionFont );
344344

345345
painter->save(); //Save the painter state so we can undo the scaling later
346346
painter->scale(1./FONT_WORKAROUND_SCALE, 1./FONT_WORKAROUND_SCALE); //scale the painter to work around the font bug
347347

348-
painter->drawText( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE, sectionTitle );
348+
painter->drawText(QPointF(x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE), sectionTitle );
349349
painter->restore();
350350

351-
int w = 3*mMargin + sectionMetrics.width( sectionTitle );
351+
double w = 3*mMargin + sectionMetrics.width( sectionTitle );
352352
if ( w > width ) width = w;
353353
height += sectionMetrics.height();
354-
height += (int) (0.7*mSymbolSpace);
354+
height += (1.5*mSymbolSpace);
355355
}
356356

357357

358358
// Draw all layers in group
359-
int groupStartHeight = height;
359+
double groupStartHeight = height;
360360
for ( int j = groupLayers.size()-1; j >= 0; j-- )
361361
{
362362
std::cout << "layer = " << groupLayers[j] << std::endl;
363363

364-
int localHeight = groupStartHeight;
364+
double localHeight = groupStartHeight;
365365

366366
layer = mMapCanvas->getZpos(groupLayers[j]);
367367
QgsVectorLayer *vector = dynamic_cast <QgsVectorLayer*> (layer);
@@ -375,13 +375,14 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
375375
for ( QList<QgsSymbol*>::iterator it = symbols.begin(); it != symbols.end(); ++it ) {
376376
localHeight += mSymbolSpace;
377377

378-
int symbolHeight = itemHeights[icnt];
378+
double symbolHeight = itemHeights[icnt];
379379
QgsSymbol* sym = (*it);
380380

381381
QPen pen = sym->pen();
382382
double widthScale = map->widthScale();
383383

384384
pen.setWidthF( ( widthScale * pen.widthF() ) );
385+
pen.setCapStyle(Qt::FlatCap); //make sure that the line doesn't extend past its endpoints
385386
painter->setPen ( pen );
386387
painter->setBrush ( sym->brush() );
387388

@@ -399,13 +400,12 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
399400
painter->restore();
400401

401402
} else if ( vector->vectorType() == QGis::Line ) {
402-
painter->drawLine ( mMargin, localHeight+mSymbolHeight/2,
403-
mMargin+mSymbolWidth, localHeight+mSymbolHeight/2 );
403+
painter->drawLine (QPointF(mMargin, localHeight+mSymbolHeight/2),
404+
QPointF(mMargin+mSymbolWidth, localHeight+mSymbolHeight/2));
404405
} else if ( vector->vectorType() == QGis::Polygon ) {
405-
//pen.setWidth(0); //use a cosmetic pen to outline the fill box
406406
pen.setCapStyle(Qt::FlatCap);
407407
painter->setPen ( pen );
408-
painter->drawRect ( mMargin, localHeight, mSymbolWidth, mSymbolHeight );
408+
painter->drawRect (QRectF(mMargin, localHeight, mSymbolWidth, mSymbolHeight));
409409
}
410410

411411
// Label
@@ -419,15 +419,15 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
419419
}
420420

421421
// drawText (x, y w, h, ...) was cutting last letter (the box was too small)
422-
QRect br = metrics.boundingRect ( lab );
423-
x = (int) ( 2*mMargin + mSymbolWidth );
424-
y = (int) ( localHeight + symbolHeight/2 + ( metrics.height()/2 - metrics.descent()) );
425-
painter->save(); //Save the painter state so we can undo the scaling later
426-
painter->scale(1./FONT_WORKAROUND_SCALE, 1./FONT_WORKAROUND_SCALE); //scale the painter to work around the font bug
427-
428-
painter->drawText( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE, lab );
429-
painter->restore();
430-
int w = 3*mMargin + mSymbolWidth + metrics.width(lab);
422+
QRectF br = metrics.boundingRect ( lab );
423+
x = 2*mMargin + mSymbolWidth;
424+
y = localHeight + symbolHeight/2 + ( metrics.height()/2 - metrics.descent());
425+
painter->save(); //Save the painter state so we can undo the scaling later
426+
painter->scale(1./FONT_WORKAROUND_SCALE, 1./FONT_WORKAROUND_SCALE);//scale the painter to work around the font bug
427+
428+
painter->drawText(QPointF(x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE), lab );
429+
painter->restore();
430+
double w = 3*mMargin + mSymbolWidth + metrics.width(lab);
431431
if ( w > width ) width = w;
432432

433433
localHeight += symbolHeight;
@@ -456,7 +456,7 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
456456
painter->setPen( pen );
457457
painter->setBrush( QBrush( QColor(255,255,255), Qt::NoBrush));
458458
painter->setRenderHint(QPainter::Antialiasing, true);//turn on antialiasing
459-
painter->drawRect ( 0, 0, width, height );
459+
painter->drawRect(QRectF(0, 0, width, height));
460460
}
461461

462462

@@ -611,10 +611,10 @@ void QgsComposerVectorLegend::recalculate ( void )
611611
// Font size in canvas units
612612
float size = 25.4 * mComposition->scale() * mFont.pointSizeFloat() / 72;
613613

614-
mMargin = (int) ( 0.9 * size );
615-
mSymbolHeight = (int) ( 1.3 * size );
616-
mSymbolWidth = (int) ( 3.5 * size );
617-
mSymbolSpace = (int) ( 0.4 * size );
614+
mMargin = 0.9 * size;
615+
mSymbolHeight = 1.3 * size;
616+
mSymbolWidth = 3.5 * size;
617+
mSymbolSpace = 0.4 * size;
618618

619619
std::cout << "mMargin = " << mMargin << " mSymbolHeight = " << mSymbolHeight
620620
<< "mSymbolWidth = " << mSymbolWidth << " mSymbolSpace = " << mSymbolSpace << std::endl;

‎src/app/composer/qgscomposervectorlegend.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ public slots:
185185
// Pen
186186
QPen mPen;
187187

188-
int mMargin;
189-
int mSymbolHeight;
190-
int mSymbolWidth;
191-
int mSymbolSpace;
188+
double mMargin;
189+
double mSymbolHeight;
190+
double mSymbolWidth;
191+
double mSymbolSpace;
192192

193193
// Cache used in composer preview
194194
// NOTE: QCanvasView is slow with bigger images but the spped does not decrease with image size.

0 commit comments

Comments
 (0)
Please sign in to comment.