Commit 9abd51b5 authored by Aurélien Gâteau's avatar Aurélien Gâteau

Factorize tick and text drawing code

parent df4999a1
...@@ -1043,9 +1043,7 @@ void KLineal::drawScale( QPainter &painter ) ...@@ -1043,9 +1043,7 @@ void KLineal::drawScale( QPainter &painter )
painter.setPen( Qt::black ); painter.setPen( Qt::black );
QFont font = mScaleFont; QFont font = mScaleFont;
painter.setFont( font ); painter.setFont( font );
QFontMetrics metrics = painter.fontMetrics();
int longLen; int longLen;
int shortStart = 0;
int w = width(); int w = width();
int h = height(); int h = height();
...@@ -1088,35 +1086,10 @@ void KLineal::drawScale( QPainter &painter ) ...@@ -1088,35 +1086,10 @@ void KLineal::drawScale( QPainter &painter )
if ( digit % 100 == 0 ) { if ( digit % 100 == 0 ) {
QString units; QString units;
units.sprintf( "%d", digit ); units.sprintf( "%d", digit );
QSize textSize = metrics.size( Qt::TextSingleLine, units ); drawScaleText( painter, x, units );
int tw = textSize.width();
int th = metrics.ascent();
switch ( mOrientation ) {
case North:
case South:
painter.drawText( x - tw / 2, (h + th) / 2, units );
break;
case East:
case West:
painter.drawText( (w - tw) / 2, x + th / 2, units );
break;
}
} }
switch( mOrientation ) { drawScaleTick( painter, x, len );
case North:
case South:
painter.drawLine( x, 0, x, len );
painter.drawLine( x, h, x, h - len );
break;
case East:
case West:
painter.drawLine( 0, x, len, x );
painter.drawLine( w, x, w - len, x );
break;
}
} }
} else { } else {
float step = longLen / 100.f; float step = longLen / 100.f;
...@@ -1130,47 +1103,54 @@ void KLineal::drawScale( QPainter &painter ) ...@@ -1130,47 +1103,54 @@ void KLineal::drawScale( QPainter &painter )
QString units; QString units;
int value = mLeftToRight ? i : ( 100 - i ); int value = mLeftToRight ? i : ( 100 - i );
units.sprintf( "%d%%", value ); units.sprintf( "%d%%", value );
QSize textSize = metrics.size( Qt::TextSingleLine, units ); drawScaleText( painter, x, units );
int tw = textSize.width();
int th = textSize.height();
switch ( mOrientation ) {
case North:
painter.drawText( x - tw / 2, shortStart + len + th, units );
break;
case South:
painter.drawText( x - tw / 2, shortStart - len - 2, units );
break;
case East:
painter.drawText( shortStart - len - tw - 2, x + th / 2 - 2, units );
break;
case West:
painter.drawText( shortStart + len + 2, x + th / 2 - 2, units );
break;
}
} }
switch( mOrientation ) { drawScaleTick( painter, x, len );
case North:
painter.drawLine( x, shortStart, x, shortStart + len );
break;
case South:
painter.drawLine( x, shortStart, x, shortStart - len );
break;
case East:
painter.drawLine( shortStart, x, shortStart - len, x );
break;
case West:
painter.drawLine( shortStart, x, shortStart + len, x );
break;
}
} }
} }
} }
void KLineal::drawScaleText( QPainter &painter, int x, const QString &text )
{
QFontMetrics metrics = painter.fontMetrics();
QSize textSize = metrics.size( Qt::TextSingleLine, text );
int w = width();
int h = height();
int tw = textSize.width();
int th = metrics.ascent();
switch ( mOrientation ) {
case North:
case South:
painter.drawText( x - tw / 2, (h + th) / 2, text );
break;
case East:
case West:
painter.drawText( (w - tw) / 2, x + th / 2, text );
break;
}
}
void KLineal::drawScaleTick( QPainter &painter, int x, int len )
{
int w = width();
int h = height();
switch( mOrientation ) {
case North:
case South:
painter.drawLine( x, 0, x, len );
painter.drawLine( x, h, x, h - len );
break;
case East:
case West:
painter.drawLine( 0, x, len, x );
painter.drawLine( w, x, w - len, x );
break;
}
}
/** /**
* actually draws the ruler * actually draws the ruler
*/ */
......
...@@ -62,6 +62,8 @@ private: ...@@ -62,6 +62,8 @@ private:
const QKeySequence &shortcut, const QString& name ); const QKeySequence &shortcut, const QString& name );
void drawScale( QPainter &painter ); void drawScale( QPainter &painter );
void drawBackground( QPainter &painter ); void drawBackground( QPainter &painter );
void drawScaleText( QPainter &painter, int x, const QString &text );
void drawScaleTick( QPainter &painter, int x, int length );
void reLength( int percentOfScreen ); void reLength( int percentOfScreen );
void reLengthAbsolute( int length ); void reLengthAbsolute( int length );
void updateScaleDirectionMenuItem(); void updateScaleDirectionMenuItem();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment