Skip to content
Permalink
Browse files

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/qgis@7638 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
StevenB StevenB
StevenB authored and StevenB committed Nov 22, 2007
1 parent b57e59d commit c7ae4fe1d45226c300d027fb15fd703672226b29
Showing with 40 additions and 40 deletions.
  1. +36 −36 src/app/composer/qgscomposervectorlegend.cpp
  2. +4 −4 src/app/composer/qgscomposervectorlegend.h
@@ -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?
{
@@ -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();
@@ -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
@@ -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;
@@ -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;
}
@@ -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);
@@ -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() );

@@ -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
@@ -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;
@@ -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));
}


@@ -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;
@@ -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.

0 comments on commit c7ae4fe

Please sign in to comment.
You can’t perform that action at this time.