/*! Calculate the transformation that is needed to paint a label depending on its alignment and rotation. \param pos Position where to paint the label \param size Size of the label \sa setLabelAlignment(), setLabelRotation() */ QTransform QwtScaleDraw::labelTransformation( const QPointF &pos, const QSizeF &size ) const { QTransform transform; transform.translate( pos.x(), pos.y() ); transform.rotate( labelRotation() ); int flags = labelAlignment(); if ( flags == 0 ) { switch ( alignment() ) { case RightScale: { if ( flags == 0 ) flags = Qt::AlignRight | Qt::AlignVCenter; break; } case LeftScale: { if ( flags == 0 ) flags = Qt::AlignLeft | Qt::AlignVCenter; break; } case BottomScale: { if ( flags == 0 ) flags = Qt::AlignHCenter | Qt::AlignBottom; break; } case TopScale: { if ( flags == 0 ) flags = Qt::AlignHCenter | Qt::AlignTop; break; } } } double x, y; if ( flags & Qt::AlignLeft ) x = -size.width(); else if ( flags & Qt::AlignRight ) x = 0.0; else // Qt::AlignHCenter x = -( 0.5 * size.width() ); if ( flags & Qt::AlignTop ) y = -size.height(); else if ( flags & Qt::AlignBottom ) y = 0; else // Qt::AlignVCenter y = -( 0.5 * size.height() ); transform.translate( x, y ); return transform; }
/*! Calculate the matrix that is needed to paint a label depending on its alignment and rotation. \param pos Position where to paint the label \param size Size of the label \sa setLabelAlignment(), setLabelRotation() */ QwtMatrix QwtScaleDraw::labelMatrix( const QPoint &pos, const QSize &size) const { QwtMatrix m; m.translate(pos.x(), pos.y()); m.rotate(labelRotation()); int flags = labelAlignment(); if ( flags == 0 ) { switch(alignment()) { case RightScale: { if ( flags == 0 ) flags = Qt::AlignRight | Qt::AlignVCenter; break; } case LeftScale: { if ( flags == 0 ) flags = Qt::AlignLeft | Qt::AlignVCenter; break; } case BottomScale: { if ( flags == 0 ) flags = Qt::AlignHCenter | Qt::AlignBottom; break; } case TopScale: { if ( flags == 0 ) flags = Qt::AlignHCenter | Qt::AlignTop; break; } } } const int w = size.width(); const int h = size.height(); int x, y; if ( flags & Qt::AlignLeft ) x = -w; else if ( flags & Qt::AlignRight ) x = -(w % 2); else // Qt::AlignHCenter x = -(w / 2); if ( flags & Qt::AlignTop ) y = -h ; else if ( flags & Qt::AlignBottom ) y = -(h % 2); else // Qt::AlignVCenter y = -(h/2); m.translate(x, y); return m; }
int QwtScaleDraw::minLabelDist(const QFont &font) const { if ( !hasComponent(QwtAbstractScaleDraw::Labels) ) return 0; const QwtValueList &ticks = scaleDiv().ticks(QwtScaleDiv::MajorTick); if (ticks.count() == 0) return 0; const QFontMetrics fm(font); const bool vertical = (orientation() == Qt::Vertical); QRect bRect1; QRect bRect2 = labelRect(font, ticks[0]); if ( vertical ) { bRect2.setRect(-bRect2.bottom(), 0, bRect2.height(), bRect2.width()); } int maxDist = 0; for (uint i = 1; i < (uint)ticks.count(); i++ ) { bRect1 = bRect2; bRect2 = labelRect(font, ticks[i]); if ( vertical ) { bRect2.setRect(-bRect2.bottom(), 0, bRect2.height(), bRect2.width()); } int dist = fm.leading(); // space between the labels if ( bRect1.right() > 0 ) dist += bRect1.right(); if ( bRect2.left() < 0 ) dist += -bRect2.left(); if ( dist > maxDist ) maxDist = dist; } double angle = labelRotation() / 180.0 * M_PI; if ( vertical ) angle += M_PI / 2; if ( sin(angle) == 0.0 ) return maxDist; const int fmHeight = fm.ascent() - 2; // The distance we need until there is // the height of the label font. This height is needed // for the neighbour labal. int labelDist = (int)(fmHeight / sin(angle) * cos(angle)); if ( labelDist < 0 ) labelDist = -labelDist; // The cast above floored labelDist. We want to ceil. labelDist++; // For text orientations close to the scale orientation if ( labelDist > maxDist ) labelDist = maxDist; // For text orientations close to the opposite of the // scale orientation if ( labelDist < fmHeight ) labelDist = fmHeight; return labelDist; }
int QwtScaleDraw::minLabelDist( const QFont &font ) const { if ( !hasComponent( QwtAbstractScaleDraw::Labels ) ) return 0; const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick ); if ( ticks.isEmpty() ) return 0; const QFontMetrics fm( font ); const bool vertical = ( orientation() == Qt::Vertical ); QRectF bRect1; QRectF bRect2 = labelRect( font, ticks[0] ); if ( vertical ) { bRect2.setRect( -bRect2.bottom(), 0.0, bRect2.height(), bRect2.width() ); } double maxDist = 0.0; for ( int i = 1; i < ticks.count(); i++ ) { bRect1 = bRect2; bRect2 = labelRect( font, ticks[i] ); if ( vertical ) { bRect2.setRect( -bRect2.bottom(), 0.0, bRect2.height(), bRect2.width() ); } double dist = fm.leading(); // space between the labels if ( bRect1.right() > 0 ) dist += bRect1.right(); if ( bRect2.left() < 0 ) dist += -bRect2.left(); if ( dist > maxDist ) maxDist = dist; } double angle = qwtRadians( labelRotation() ); if ( vertical ) angle += M_PI / 2; const double sinA = qFastSin( angle ); // qreal -> double if ( qFuzzyCompare( sinA + 1.0, 1.0 ) ) return qCeil( maxDist ); const int fmHeight = fm.ascent() - 2; // The distance we need until there is // the height of the label font. This height is needed // for the neighbored label. double labelDist = fmHeight / qFastSin( angle ) * qFastCos( angle ); if ( labelDist < 0 ) labelDist = -labelDist; // For text orientations close to the scale orientation if ( labelDist > maxDist ) labelDist = maxDist; // For text orientations close to the opposite of the // scale orientation if ( labelDist < fmHeight ) labelDist = fmHeight; return qCeil( labelDist ); }