Skip to content

Commit

Permalink
Fixed bug #799 and converted legend measurements to floating-point.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
StevenB authored and StevenB committed Nov 22, 2007
1 parent 722498b commit 030c580
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
72 changes: 36 additions & 36 deletions src/app/composer/qgscomposervectorlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias) );


QFontMetrics titleMetrics ( titleFont );
QFontMetrics sectionMetrics ( sectionFont );
QFontMetrics metrics ( font );
QFontMetricsF titleMetrics ( titleFont );
QFontMetricsF sectionMetrics ( sectionFont );
QFontMetricsF metrics ( font );

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

int x, y;
double x, y;

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

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

painter->restore();

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

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

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

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

int h = (int) ( scale * pic.height() );
double h = scale * pic.height();
if ( h > itemHeights[icnt] ) {
itemHeights[icnt] = h;
}
Expand Down Expand Up @@ -337,31 +337,31 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
{
height += mSymbolSpace;

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

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

painter->drawText( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE, sectionTitle );
painter->drawText(QPointF(x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE), sectionTitle );
painter->restore();

int w = 3*mMargin + sectionMetrics.width( sectionTitle );
double w = 3*mMargin + sectionMetrics.width( sectionTitle );
if ( w > width ) width = w;
height += sectionMetrics.height();
height += (int) (0.7*mSymbolSpace);
height += (1.5*mSymbolSpace);
}


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

int localHeight = groupStartHeight;
double localHeight = groupStartHeight;

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

int symbolHeight = itemHeights[icnt];
double symbolHeight = itemHeights[icnt];
QgsSymbol* sym = (*it);

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

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

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

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

// Label
Expand All @@ -419,15 +419,15 @@ QRectF QgsComposerVectorLegend::render ( QPainter *p )
}

// drawText (x, y w, h, ...) was cutting last letter (the box was too small)
QRect br = metrics.boundingRect ( lab );
x = (int) ( 2*mMargin + mSymbolWidth );
y = (int) ( localHeight + symbolHeight/2 + ( metrics.height()/2 - metrics.descent()) );
painter->save(); //Save the painter state so we can undo the scaling later
painter->scale(1./FONT_WORKAROUND_SCALE, 1./FONT_WORKAROUND_SCALE); //scale the painter to work around the font bug

painter->drawText( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE, lab );
painter->restore();
int w = 3*mMargin + mSymbolWidth + metrics.width(lab);
QRectF br = metrics.boundingRect ( lab );
x = 2*mMargin + mSymbolWidth;
y = localHeight + symbolHeight/2 + ( metrics.height()/2 - metrics.descent());
painter->save(); //Save the painter state so we can undo the scaling later
painter->scale(1./FONT_WORKAROUND_SCALE, 1./FONT_WORKAROUND_SCALE);//scale the painter to work around the font bug

painter->drawText(QPointF(x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE), lab );
painter->restore();
double w = 3*mMargin + mSymbolWidth + metrics.width(lab);
if ( w > width ) width = w;

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


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

mMargin = (int) ( 0.9 * size );
mSymbolHeight = (int) ( 1.3 * size );
mSymbolWidth = (int) ( 3.5 * size );
mSymbolSpace = (int) ( 0.4 * size );
mMargin = 0.9 * size;
mSymbolHeight = 1.3 * size;
mSymbolWidth = 3.5 * size;
mSymbolSpace = 0.4 * size;

std::cout << "mMargin = " << mMargin << " mSymbolHeight = " << mSymbolHeight
<< "mSymbolWidth = " << mSymbolWidth << " mSymbolSpace = " << mSymbolSpace << std::endl;
Expand Down
8 changes: 4 additions & 4 deletions src/app/composer/qgscomposervectorlegend.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ public slots:
// Pen
QPen mPen;

int mMargin;
int mSymbolHeight;
int mSymbolWidth;
int mSymbolSpace;
double mMargin;
double mSymbolHeight;
double mSymbolWidth;
double mSymbolSpace;

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

0 comments on commit 030c580

Please sign in to comment.