10#include "qwt_scale_draw.h"
11#include "qwt_scale_div.h"
12#include "qwt_scale_map.h"
14#include "qwt_painter.h"
18#include <qpaintengine.h>
23 return qwtMaxF( scaleDraw->
penWidthF(), 1.0 );
26namespace QwtScaleRendererReal
28 inline qreal penWidth(
const QPainter* painter,
const QwtScaleDraw* scaleDraw )
36 if ( painter->pen().isCosmetic() )
38 const QTransform& transform = painter->transform();
45 width /= transform.m11();
51 width /= transform.m22();
60 inline void drawBackbone( QPainter* painter,
const QwtScaleDraw* scaleDraw )
62 const qreal pw2 = 0.5 * penWidth( painter, scaleDraw );
64 const QPointF pos = scaleDraw->
pos();
65 const qreal length = scaleDraw->
length();
71 const qreal x = pos.x() + 1.0 - pw2;
78 const qreal x = pos.x() - 1.0 + pw2;
85 const qreal y = pos.y() + 1.0 - pw2;
92 const qreal y = pos.y() - 1.0 + pw2;
100 inline void drawTick( QPainter* painter,
101 const QwtScaleDraw* scaleDraw, qreal tickPos, qreal tickLength )
103 const QPointF pos = scaleDraw->
pos();
108 pw = penWidth( painter, scaleDraw );
110 const qreal length = tickLength + pw;
117 const qreal off1 = 1.0;
118 const qreal off2 = ( scaleDraw->
penWidthF() <= 0.0 ) ? 0.5 : 0.0;
124 const qreal x = pos.x() + off1 - off2;
131 const qreal x = pos.x() - off1 + off2;
137 const qreal y = pos.y() + off1 - 2 * off2;
144 const qreal y = pos.y() - off1 + off2;
153namespace QwtScaleRendererInt
155 inline void drawBackbone( QPainter* painter,
const QwtScaleDraw* scaleDraw )
157 const int pw = qMax( qRound( scaleDraw->
penWidthF() ), 1 );
159 const qreal length = scaleDraw->
length();
160 const QPointF pos = scaleDraw->
pos();
166 const qreal x = qRound( pos.x() - ( pw - 1 ) / 2 );
173 const qreal x = qRound( pos.x() + pw / 2 );
180 const qreal y = qRound( pos.y() - ( pw - 1 ) / 2 );
187 const qreal y = qRound( pos.y() + pw / 2 );
195 inline void drawTick( QPainter* painter,
196 const QwtScaleDraw* scaleDraw, qreal tickPos, qreal tickLength )
198 const QPointF pos = scaleDraw->
pos();
199 tickPos = qRound( tickPos );
203 pw = qMax( qRound( scaleDraw->
penWidthF() ), 1 );
205 int len = qMax( qRound( tickLength ), 1 );
210 if ( painter->pen().capStyle() == Qt::FlatCap )
215 if ( painter->paintEngine()->type() == QPaintEngine::X11 )
228 const qreal x1 = qRound( pos.x() ) + 1;
229 const qreal x2 = x1 - len + 1;
237 const qreal x1 = qRound( pos.x() );
238 const qreal x2 = x1 + len - 1;
246 const qreal y1 = qRound( pos.y() );
247 const qreal y2 = y1 + len - 1;
255 const qreal y1 = qRound( pos.y() );
256 const qreal y2 = y1 - len + 1;
266class QwtScaleDraw::PrivateData
272 , labelRotation( 0.0 )
281 Qt::Alignment labelAlignment;
282 double labelRotation;
294 m_data =
new QwtScaleDraw::PrivateData;
311 return m_data->alignment;
324 m_data->alignment = align;
339 switch ( m_data->alignment )
343 return Qt::Horizontal;
362 const QFont& font,
int& start,
int& end )
const
371 if ( ticks.count() == 0 )
378 double minTick = ticks[0];
380 double maxTick = minTick;
381 double maxPos = minPos;
383 for (
int i = 1; i < ticks.count(); i++ )
386 if ( tickPos < minPos )
391 if ( tickPos >
scaleMap().transform( maxTick ) )
403 s -= qAbs( minPos - qRound(
scaleMap().p2() ) );
406 e -= qAbs( maxPos -
scaleMap().p1() );
411 s -= qAbs( minPos -
scaleMap().p1() );
414 e -= qAbs( maxPos -
scaleMap().p2() );
422 start = qwtCeil( s );
442 if ( ticks.isEmpty() )
445 const QFontMetrics fm( font );
447 const bool vertical = (
orientation() == Qt::Vertical );
450 QRectF bRect2 =
labelRect( font, ticks[0] );
453 bRect2.setRect( -bRect2.bottom(), 0.0, bRect2.height(), bRect2.width() );
456 double maxDist = 0.0;
458 for (
int i = 1; i < ticks.count(); i++ )
464 bRect2.setRect( -bRect2.bottom(), 0.0,
465 bRect2.height(), bRect2.width() );
468 double dist = fm.leading();
469 if ( bRect1.right() > 0 )
470 dist += bRect1.right();
471 if ( bRect2.left() < 0 )
472 dist += -bRect2.left();
474 if ( dist > maxDist )
482 const double sinA = qFastSin( angle );
483 if ( qFuzzyCompare( sinA + 1.0, 1.0 ) )
484 return qCeil( maxDist );
486 const int fmHeight = fm.ascent() - 2;
492 double labelDist = fmHeight / qFastSin( angle ) * qFastCos( angle );
494 labelDist = -labelDist;
498 if ( labelDist > maxDist )
504 if ( labelDist < fmHeight )
505 labelDist = fmHeight;
507 return qCeil( labelDist );
545 d += qwtEffectivePenWidth(
this );
562 int startDist, endDist;
567 const uint minorCount =
570 const uint majorCount =
573 int lengthForLabels = 0;
577 int lengthForTicks = 0;
580 const double pw = qwtEffectivePenWidth(
this );
581 lengthForTicks = qCeil( ( majorCount + minorCount ) * ( pw + 1.0 ) );
584 return startDist + endDist + qMax( lengthForLabels, lengthForTicks );
601 dist += qwtEffectivePenWidth(
this );
613 px = m_data->pos.x() + dist;
619 px = m_data->pos.x() - dist;
626 py = m_data->pos.y() + dist;
632 py = m_data->pos.y() - dist;
637 return QPointF( px, py );
657 QwtScaleRendererInt::drawTick( painter,
this, tval, len );
659 QwtScaleRendererReal::drawTick( painter,
this, tval, len );
671 QwtScaleRendererInt::drawBackbone( painter,
this );
673 QwtScaleRendererReal::drawBackbone( painter,
this );
738 if ( length < 0 && length > -10 )
775 painter->setWorldTransform( transform,
true );
777 lbl.
draw ( painter, QRect( QPoint( 0, 0 ),
labelSize.toSize() ) );
804 return transform.mapRect( QRect( QPoint( 0, 0 ),
labelSize.toSize() ) );
818 const QPointF& pos,
const QSizeF& size )
const
820 QTransform transform;
821 transform.translate(
pos.x(),
pos.y() );
832 flags = Qt::AlignRight | Qt::AlignVCenter;
838 flags = Qt::AlignLeft | Qt::AlignVCenter;
844 flags = Qt::AlignHCenter | Qt::AlignBottom;
850 flags = Qt::AlignHCenter | Qt::AlignTop;
858 if ( flags & Qt::AlignLeft )
860 else if ( flags & Qt::AlignRight )
863 x = -( 0.5 * size.width() );
865 if ( flags & Qt::AlignTop )
867 else if ( flags & Qt::AlignBottom )
870 y = -( 0.5 * size.height() );
872 transform.translate( x, y );
891 return QRectF( 0.0, 0.0, 0.0, 0.0 );
898 QRectF br = transform.mapRect( QRectF( QPointF( 0, 0 ),
labelSize ) );
899 br.translate( -
pos.x(), -
pos.y() );
932 m_data->labelRotation = rotation;
941 return m_data->labelRotation;
980 return m_data->labelAlignment;
989 double maxWidth = 0.0;
992 for (
int i = 0; i < ticks.count(); i++ )
994 const double v = ticks[i];
997 const double w =
labelSize( font, ticks[i] ).width();
1003 return qCeil( maxWidth );
1012 double maxHeight = 0.0;
1015 for (
int i = 0; i < ticks.count(); i++ )
1017 const double v = ticks[i];
1020 const double h =
labelSize( font, ticks[i] ).height();
1021 if ( h > maxHeight )
1026 return qCeil( maxHeight );
1029void QwtScaleDraw::updateMap()
1031 const QPointF
pos = m_data->pos;
1032 double len = m_data->len;
A abstract base class for drawing scales.
const QwtScaleMap & scaleMap() const
@ Backbone
Backbone = the line where the ticks are located.
double tickLength(QwtScaleDiv::TickType) const
bool hasComponent(ScaleComponent) const
const QwtText & tickLabel(const QFont &, double value) const
Convert a value into its representing label and cache it.
double maxTickLength() const
double minimumExtent() const
const QwtScaleDiv & scaleDiv() const
double spacing() const
Get the spacing.
static bool roundingAlignment()
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
A class representing a scale division.
QList< double > ticks(int tickType) const
@ MediumTick
Medium ticks.
A class for drawing scales.
void setLength(double length)
QRect boundingLabelRect(const QFont &, double value) const
Find the bounding rectangle for the label.
QTransform labelTransformation(const QPointF &, const QSizeF &) const
Qt::Alignment labelAlignment() const
void move(double x, double y)
double labelRotation() const
virtual void drawLabel(QPainter *, double value) const override
virtual double extent(const QFont &) const override
void getBorderDistHint(const QFont &, int &start, int &end) const
Determine the minimum border distance.
void setLabelAlignment(Qt::Alignment)
Change the label flags.
int minLength(const QFont &) const
virtual void drawTick(QPainter *, double value, double len) const override
QPointF labelPosition(double value) const
void setAlignment(Alignment)
virtual void drawBackbone(QPainter *) const override
Alignment alignment() const
virtual ~QwtScaleDraw()
Destructor.
int maxLabelHeight(const QFont &) const
Qt::Orientation orientation() const
void setLabelRotation(double rotation)
int maxLabelWidth(const QFont &) const
@ BottomScale
The scale is below.
@ TopScale
The scale is above.
@ RightScale
The scale is right.
@ LeftScale
The scale is left.
int minLabelDist(const QFont &) const
QwtScaleDraw()
Constructor.
QRectF labelRect(const QFont &, double value) const
QSizeF labelSize(const QFont &, double value) const
double transform(double s) const
void setPaintInterval(double p1, double p2)
Specify the borders of the paint device interval.
A class representing a text.
void draw(QPainter *painter, const QRectF &rect) const