qwt_scale_draw.cpp 22.1 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

Bryant's avatar
Bryant committed
10
#include "qwt_scale_draw.h"
pixhawk's avatar
pixhawk committed
11 12
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"
Bryant's avatar
Bryant committed
13 14 15 16 17
#include "qwt_math.h"
#include "qwt_painter.h"
#include <qpen.h>
#include <qpainter.h>
#include <qmath.h>
pixhawk's avatar
pixhawk committed
18

Bryant's avatar
Bryant committed
19 20 21
#if QT_VERSION < 0x040601
#define qFastSin(x) qSin(x)
#define qFastCos(x) qCos(x)
pixhawk's avatar
pixhawk committed
22 23 24 25 26 27
#endif

class QwtScaleDraw::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
28 29 30 31 32
        len( 0 ),
        alignment( QwtScaleDraw::BottomScale ),
        labelAlignment( 0 ),
        labelRotation( 0.0 )
    {
pixhawk's avatar
pixhawk committed
33 34
    }

Bryant's avatar
Bryant committed
35 36
    QPointF pos;
    double len;
pixhawk's avatar
pixhawk committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

    Alignment alignment;

    Qt::Alignment labelAlignment;
    double labelRotation;
};

/*!
  \brief Constructor

  The range of the scale is initialized to [0, 100],
  The position is at (0, 0) with a length of 100.
  The orientation is QwtAbstractScaleDraw::Bottom.
*/
QwtScaleDraw::QwtScaleDraw()
{
    d_data = new QwtScaleDraw::PrivateData;
Bryant's avatar
Bryant committed
54
    setLength( 100 );
pixhawk's avatar
pixhawk committed
55 56 57 58 59 60 61 62
}

//! Destructor
QwtScaleDraw::~QwtScaleDraw()
{
    delete d_data;
}

63
/*!
pixhawk's avatar
pixhawk committed
64 65
   Return alignment of the scale
   \sa setAlignment()
Bryant's avatar
Bryant committed
66
   \return Alignment of the scale
pixhawk's avatar
pixhawk committed
67
*/
68
QwtScaleDraw::Alignment QwtScaleDraw::alignment() const
pixhawk's avatar
pixhawk committed
69
{
70
    return d_data->alignment;
pixhawk's avatar
pixhawk committed
71 72 73 74 75
}

/*!
   Set the alignment of the scale

Bryant's avatar
Bryant committed
76 77
   \param align Alignment of the scale

pixhawk's avatar
pixhawk committed
78 79 80
   The default alignment is QwtScaleDraw::BottomScale
   \sa alignment()
*/
Bryant's avatar
Bryant committed
81
void QwtScaleDraw::setAlignment( Alignment align )
pixhawk's avatar
pixhawk committed
82 83 84 85 86 87 88 89 90 91
{
    d_data->alignment = align;
}

/*!
  Return the orientation

  TopScale, BottomScale are horizontal (Qt::Horizontal) scales,
  LeftScale, RightScale are vertical (Qt::Vertical) scales.

Bryant's avatar
Bryant committed
92 93
  \return Orientation of the scale

pixhawk's avatar
pixhawk committed
94 95 96 97
  \sa alignment()
*/
Qt::Orientation QwtScaleDraw::orientation() const
{
Bryant's avatar
Bryant committed
98 99 100 101 102 103 104 105 106
    switch ( d_data->alignment )
    {
        case TopScale:
        case BottomScale:
            return Qt::Horizontal;
        case LeftScale:
        case RightScale:
        default:
            return Qt::Vertical;
pixhawk's avatar
pixhawk committed
107 108 109 110 111 112 113 114 115 116 117 118 119
    }
}

/*!
  \brief Determine the minimum border distance

  This member function returns the minimum space
  needed to draw the mark labels at the scale's endpoints.

  \param font Font
  \param start Start border distance
  \param end End border distance
*/
Bryant's avatar
Bryant committed
120 121
void QwtScaleDraw::getBorderDistHint( 
    const QFont &font, int &start, int &end ) const
pixhawk's avatar
pixhawk committed
122 123 124
{
    start = 0;
    end = 0;
125

Bryant's avatar
Bryant committed
126
    if ( !hasComponent( QwtAbstractScaleDraw::Labels ) )
pixhawk's avatar
pixhawk committed
127 128
        return;

Bryant's avatar
Bryant committed
129
    const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
130
    if ( ticks.count() == 0 )
pixhawk's avatar
pixhawk committed
131 132
        return;

Bryant's avatar
Bryant committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    // Find the ticks, that are mapped to the borders.
    // minTick is the tick, that is mapped to the top/left-most position
    // in widget coordinates.

    double minTick = ticks[0];
    double minPos = scaleMap().transform( minTick );
    double maxTick = minTick;
    double maxPos = minPos;

    for ( int i = 1; i < ticks.count(); i++ )
    {
        const double tickPos = scaleMap().transform( ticks[i] );
        if ( tickPos < minPos )
        {
            minTick = ticks[i];
            minPos = tickPos;
        }
        if ( tickPos > scaleMap().transform( maxTick ) )
        {
            maxTick = ticks[i];
            maxPos = tickPos;
        }
    }
pixhawk's avatar
pixhawk committed
156

Bryant's avatar
Bryant committed
157 158
    double e = 0.0;
    double s = 0.0;
pixhawk's avatar
pixhawk committed
159
    if ( orientation() == Qt::Vertical )
Bryant's avatar
Bryant committed
160 161 162
    {
        s = -labelRect( font, minTick ).top();
        s -= qAbs( minPos - qRound( scaleMap().p2() ) );
pixhawk's avatar
pixhawk committed
163

Bryant's avatar
Bryant committed
164 165 166
        e = labelRect( font, maxTick ).bottom();
        e -= qAbs( maxPos - scaleMap().p1() );
    }
pixhawk's avatar
pixhawk committed
167
    else
Bryant's avatar
Bryant committed
168 169 170
    {
        s = -labelRect( font, minTick ).left();
        s -= qAbs( minPos - scaleMap().p1() );
pixhawk's avatar
pixhawk committed
171

Bryant's avatar
Bryant committed
172 173 174
        e = labelRect( font, maxTick ).right();
        e -= qAbs( maxPos - scaleMap().p2() );
    }
pixhawk's avatar
pixhawk committed
175

Bryant's avatar
Bryant committed
176 177 178 179 180 181 182
    if ( s < 0.0 )
        s = 0.0;
    if ( e < 0.0 )
        e = 0.0;

    start = qCeil( s );
    end = qCeil( e );
pixhawk's avatar
pixhawk committed
183 184 185 186 187 188 189 190 191 192 193 194
}

/*!
  Determine the minimum distance between two labels, that is necessary
  that the texts don't overlap.

  \param font Font
  \return The maximum width of a label

  \sa getBorderDistHint()
*/

Bryant's avatar
Bryant committed
195
int QwtScaleDraw::minLabelDist( const QFont &font ) const
pixhawk's avatar
pixhawk committed
196
{
Bryant's avatar
Bryant committed
197
    if ( !hasComponent( QwtAbstractScaleDraw::Labels ) )
pixhawk's avatar
pixhawk committed
198 199
        return 0;

Bryant's avatar
Bryant committed
200 201
    const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
    if ( ticks.isEmpty() )
pixhawk's avatar
pixhawk committed
202 203
        return 0;

Bryant's avatar
Bryant committed
204
    const QFontMetrics fm( font );
pixhawk's avatar
pixhawk committed
205

Bryant's avatar
Bryant committed
206
    const bool vertical = ( orientation() == Qt::Vertical );
pixhawk's avatar
pixhawk committed
207

Bryant's avatar
Bryant committed
208 209 210 211 212
    QRectF bRect1;
    QRectF bRect2 = labelRect( font, ticks[0] );
    if ( vertical )
    {
        bRect2.setRect( -bRect2.bottom(), 0.0, bRect2.height(), bRect2.width() );
pixhawk's avatar
pixhawk committed
213 214
    }

Bryant's avatar
Bryant committed
215 216 217 218
    double maxDist = 0.0;

    for ( int i = 1; i < ticks.count(); i++ )
    {
pixhawk's avatar
pixhawk committed
219
        bRect1 = bRect2;
Bryant's avatar
Bryant committed
220 221 222 223 224
        bRect2 = labelRect( font, ticks[i] );
        if ( vertical )
        {
            bRect2.setRect( -bRect2.bottom(), 0.0,
                bRect2.height(), bRect2.width() );
pixhawk's avatar
pixhawk committed
225 226
        }

Bryant's avatar
Bryant committed
227
        double dist = fm.leading(); // space between the labels
pixhawk's avatar
pixhawk committed
228 229 230 231 232 233 234 235 236
        if ( bRect1.right() > 0 )
            dist += bRect1.right();
        if ( bRect2.left() < 0 )
            dist += -bRect2.left();

        if ( dist > maxDist )
            maxDist = dist;
    }

Bryant's avatar
Bryant committed
237
    double angle = qwtRadians( labelRotation() ); 
pixhawk's avatar
pixhawk committed
238 239 240
    if ( vertical )
        angle += M_PI / 2;

Bryant's avatar
Bryant committed
241 242 243
    const double sinA = qFastSin( angle ); // qreal -> double
    if ( qFuzzyCompare( sinA + 1.0, 1.0 ) )
        return qCeil( maxDist );
pixhawk's avatar
pixhawk committed
244

245
    const int fmHeight = fm.ascent() - 2;
pixhawk's avatar
pixhawk committed
246 247 248

    // The distance we need until there is
    // the height of the label font. This height is needed
Bryant's avatar
Bryant committed
249
    // for the neighbored label.
pixhawk's avatar
pixhawk committed
250

Bryant's avatar
Bryant committed
251
    double labelDist = fmHeight / qFastSin( angle ) * qFastCos( angle );
pixhawk's avatar
pixhawk committed
252 253 254
    if ( labelDist < 0 )
        labelDist = -labelDist;

255
    // For text orientations close to the scale orientation
pixhawk's avatar
pixhawk committed
256 257 258 259

    if ( labelDist > maxDist )
        labelDist = maxDist;

260
    // For text orientations close to the opposite of the
pixhawk's avatar
pixhawk committed
261 262 263 264 265
    // scale orientation

    if ( labelDist < fmHeight )
        labelDist = fmHeight;

Bryant's avatar
Bryant committed
266
    return qCeil( labelDist );
pixhawk's avatar
pixhawk committed
267 268 269 270 271 272 273 274 275 276 277
}

/*!
   Calculate the width/height that is needed for a
   vertical/horizontal scale.

   The extent is calculated from the pen width of the backbone,
   the major tick length, the spacing and the maximum width/height
   of the labels.

   \param font Font used for painting the labels
Bryant's avatar
Bryant committed
278
   \return Extent
pixhawk's avatar
pixhawk committed
279 280 281

   \sa minLength()
*/
Bryant's avatar
Bryant committed
282
double QwtScaleDraw::extent( const QFont &font ) const
pixhawk's avatar
pixhawk committed
283
{
Bryant's avatar
Bryant committed
284
    double d = 0;
pixhawk's avatar
pixhawk committed
285

Bryant's avatar
Bryant committed
286 287
    if ( hasComponent( QwtAbstractScaleDraw::Labels ) )
    {
pixhawk's avatar
pixhawk committed
288
        if ( orientation() == Qt::Vertical )
Bryant's avatar
Bryant committed
289
            d = maxLabelWidth( font );
pixhawk's avatar
pixhawk committed
290
        else
Bryant's avatar
Bryant committed
291
            d = maxLabelHeight( font );
pixhawk's avatar
pixhawk committed
292 293 294 295 296

        if ( d > 0 )
            d += spacing();
    }

Bryant's avatar
Bryant committed
297 298 299
    if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
    {
        d += maxTickLength();
pixhawk's avatar
pixhawk committed
300 301
    }

Bryant's avatar
Bryant committed
302 303 304
    if ( hasComponent( QwtAbstractScaleDraw::Backbone ) )
    {
        const double pw = qMax( 1, penWidth() );  // pen width can be zero
pixhawk's avatar
pixhawk committed
305 306 307
        d += pw;
    }

Bryant's avatar
Bryant committed
308
    d = qMax( d, minimumExtent() );
pixhawk's avatar
pixhawk committed
309 310 311 312 313 314 315
    return d;
}

/*!
   Calculate the minimum length that is needed to draw the scale

   \param font Font used for painting the labels
Bryant's avatar
Bryant committed
316
   \return Minimum length that is needed to draw the scale
pixhawk's avatar
pixhawk committed
317 318 319

   \sa extent()
*/
Bryant's avatar
Bryant committed
320
int QwtScaleDraw::minLength( const QFont &font ) const
pixhawk's avatar
pixhawk committed
321 322
{
    int startDist, endDist;
Bryant's avatar
Bryant committed
323
    getBorderDistHint( font, startDist, endDist );
pixhawk's avatar
pixhawk committed
324 325 326 327

    const QwtScaleDiv &sd = scaleDiv();

    const uint minorCount =
Bryant's avatar
Bryant committed
328 329
        sd.ticks( QwtScaleDiv::MinorTick ).count() +
        sd.ticks( QwtScaleDiv::MediumTick ).count();
pixhawk's avatar
pixhawk committed
330
    const uint majorCount =
Bryant's avatar
Bryant committed
331
        sd.ticks( QwtScaleDiv::MajorTick ).count();
pixhawk's avatar
pixhawk committed
332 333

    int lengthForLabels = 0;
Bryant's avatar
Bryant committed
334 335
    if ( hasComponent( QwtAbstractScaleDraw::Labels ) )
        lengthForLabels = minLabelDist( font ) * majorCount;
pixhawk's avatar
pixhawk committed
336 337

    int lengthForTicks = 0;
Bryant's avatar
Bryant committed
338 339 340 341
    if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
    {
        const double pw = qMax( 1, penWidth() );  // penwidth can be zero
        lengthForTicks = qCeil( ( majorCount + minorCount ) * ( pw + 1.0 ) );
pixhawk's avatar
pixhawk committed
342 343
    }

Bryant's avatar
Bryant committed
344
    return startDist + endDist + qMax( lengthForLabels, lengthForTicks );
pixhawk's avatar
pixhawk committed
345 346 347 348 349
}

/*!
   Find the position, where to paint a label

Bryant's avatar
Bryant committed
350 351
   The position has a distance that depends on the length of the ticks 
   in direction of the alignment().
pixhawk's avatar
pixhawk committed
352 353

   \param value Value
Bryant's avatar
Bryant committed
354
   \return Position, where to paint a label
pixhawk's avatar
pixhawk committed
355
*/
Bryant's avatar
Bryant committed
356
QPointF QwtScaleDraw::labelPosition( double value ) const
pixhawk's avatar
pixhawk committed
357
{
Bryant's avatar
Bryant committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    const double tval = scaleMap().transform( value );
    double dist = spacing();
    if ( hasComponent( QwtAbstractScaleDraw::Backbone ) )
        dist += qMax( 1, penWidth() );

    if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
        dist += tickLength( QwtScaleDiv::MajorTick );

    double px = 0;
    double py = 0;

    switch ( alignment() )
    {
        case RightScale:
        {
            px = d_data->pos.x() + dist;
            py = tval;
            break;
        }
        case LeftScale:
        {
            px = d_data->pos.x() - dist;
            py = tval;
            break;
        }
        case BottomScale:
        {
            px = tval;
            py = d_data->pos.y() + dist;
            break;
        }
        case TopScale:
        {
            px = tval;
            py = d_data->pos.y() - dist;
            break;
        }
pixhawk's avatar
pixhawk committed
395 396
    }

Bryant's avatar
Bryant committed
397
    return QPointF( px, py );
pixhawk's avatar
pixhawk committed
398 399 400 401 402 403 404
}

/*!
   Draw a tick

   \param painter Painter
   \param value Value of the tick
Bryant's avatar
Bryant committed
405
   \param len Length of the tick
pixhawk's avatar
pixhawk committed
406 407 408

   \sa drawBackbone(), drawLabel()
*/
Bryant's avatar
Bryant committed
409
void QwtScaleDraw::drawTick( QPainter *painter, double value, double len ) const
pixhawk's avatar
pixhawk committed
410 411 412 413
{
    if ( len <= 0 )
        return;

Bryant's avatar
Bryant committed
414
    const bool roundingAlignment = QwtPainter::roundingAlignment( painter );
pixhawk's avatar
pixhawk committed
415

Bryant's avatar
Bryant committed
416
    QPointF pos = d_data->pos;
pixhawk's avatar
pixhawk committed
417

Bryant's avatar
Bryant committed
418 419 420
    double tval = scaleMap().transform( value );
    if ( roundingAlignment )
        tval = qRound( tval );
pixhawk's avatar
pixhawk committed
421

Bryant's avatar
Bryant committed
422 423 424 425
    const int pw = penWidth();
    int a = 0;
    if ( pw > 1 && roundingAlignment )
        a = 1;
426

Bryant's avatar
Bryant committed
427 428 429 430 431 432 433 434 435 436 437
    switch ( alignment() )
    {
        case LeftScale:
        {
            double x1 = pos.x() + a;
            double x2 = pos.x() + a - pw - len;
            if ( roundingAlignment )
            {
                x1 = qRound( x1 );
                x2 = qRound( x2 );
            }
pixhawk's avatar
pixhawk committed
438

Bryant's avatar
Bryant committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
            QwtPainter::drawLine( painter, x1, tval, x2, tval );
            break;
        }

        case RightScale:
        {
            double x1 = pos.x();
            double x2 = pos.x() + pw + len;
            if ( roundingAlignment )
            {
                x1 = qRound( x1 );
                x2 = qRound( x2 );
            }

            QwtPainter::drawLine( painter, x1, tval, x2, tval );
            break;
        }

        case BottomScale:
        {
            double y1 = pos.y();
            double y2 = pos.y() + pw + len;
            if ( roundingAlignment )
            {
                y1 = qRound( y1 );
                y2 = qRound( y2 );
            }

            QwtPainter::drawLine( painter, tval, y1, tval, y2 );
            break;
        }

        case TopScale:
        {
            double y1 = pos.y() + a;
            double y2 = pos.y() - pw - len + a;
            if ( roundingAlignment )
            {
                y1 = qRound( y1 );
                y2 = qRound( y2 );
            }

            QwtPainter::drawLine( painter, tval, y1, tval, y2 );
            break;
        }
pixhawk's avatar
pixhawk committed
484 485 486
    }
}

487
/*!
pixhawk's avatar
pixhawk committed
488 489 490 491 492
   Draws the baseline of the scale
   \param painter Painter

   \sa drawTick(), drawLabel()
*/
Bryant's avatar
Bryant committed
493
void QwtScaleDraw::drawBackbone( QPainter *painter ) const
pixhawk's avatar
pixhawk committed
494
{
Bryant's avatar
Bryant committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    const bool doAlign = QwtPainter::roundingAlignment( painter );

    const QPointF &pos = d_data->pos;
    const double len = d_data->len;
    const int pw = qMax( penWidth(), 1 );

    // pos indicates a border not the center of the backbone line
    // so we need to shift its position depending on the pen width
    // and the alignment of the scale

    double off;
    if ( doAlign )
    {
        if ( alignment() == LeftScale || alignment() == TopScale )
            off = ( pw - 1 ) / 2;
        else
            off = pw / 2;
    }
    else
    {
        off = 0.5 * penWidth();
    }

    switch ( alignment() )
    {
        case LeftScale:
        {
            double x = pos.x() - off;
            if ( doAlign )
                x = qRound( x );

            QwtPainter::drawLine( painter, x, pos.y(), x, pos.y() + len );
            break;
        }
        case RightScale:
        {
            double x = pos.x() + off;
            if ( doAlign )
                x = qRound( x );

            QwtPainter::drawLine( painter, x, pos.y(), x, pos.y() + len );
            break;
        }
        case TopScale:
        {
            double y = pos.y() - off;
            if ( doAlign )
                y = qRound( y );

            QwtPainter::drawLine( painter, pos.x(), y, pos.x() + len, y );
            break;
        }
        case BottomScale:
        {
            double y = pos.y() + off;
            if ( doAlign )
                y = qRound( y );

            QwtPainter::drawLine( painter, pos.x(), y, pos.x() + len, y );
            break;
        }
pixhawk's avatar
pixhawk committed
556 557 558 559 560 561 562 563 564 565
    }
}

/*!
  \brief Move the position of the scale

  The meaning of the parameter pos depends on the alignment:
  <dl>
  <dt>QwtScaleDraw::LeftScale
  <dd>The origin is the topmost point of the
566 567
      backbone. The backbone is a vertical line.
      Scale marks and labels are drawn
pixhawk's avatar
pixhawk committed
568 569 570
      at the left of the backbone.
  <dt>QwtScaleDraw::RightScale
  <dd>The origin is the topmost point of the
571
      backbone. The backbone is a vertical line.
pixhawk's avatar
pixhawk committed
572 573 574 575
      Scale marks and labels are drawn
      at the right of the backbone.
  <dt>QwtScaleDraw::TopScale
  <dd>The origin is the leftmost point of the
576
      backbone. The backbone is a horizontal line.
pixhawk's avatar
pixhawk committed
577 578 579 580
      Scale marks and labels are drawn
      above the backbone.
  <dt>QwtScaleDraw::BottomScale
  <dd>The origin is the leftmost point of the
581
      backbone. The backbone is a horizontal line
pixhawk's avatar
pixhawk committed
582 583 584 585 586 587 588 589
      Scale marks and labels are drawn
      below the backbone.
  </dl>

  \param pos Origin of the scale

  \sa pos(), setLength()
*/
Bryant's avatar
Bryant committed
590
void QwtScaleDraw::move( const QPointF &pos )
pixhawk's avatar
pixhawk committed
591 592 593 594 595
{
    d_data->pos = pos;
    updateMap();
}

596
/*!
pixhawk's avatar
pixhawk committed
597 598 599
   \return Origin of the scale
   \sa move(), length()
*/
Bryant's avatar
Bryant committed
600
QPointF QwtScaleDraw::pos() const
pixhawk's avatar
pixhawk committed
601 602 603 604 605 606
{
    return d_data->pos;
}

/*!
  Set the length of the backbone.
607

pixhawk's avatar
pixhawk committed
608 609 610
  The length doesn't include the space needed for
  overlapping labels.

Bryant's avatar
Bryant committed
611 612
  \param length Length of the backbone

pixhawk's avatar
pixhawk committed
613 614
  \sa move(), minLabelDist()
*/
Bryant's avatar
Bryant committed
615
void QwtScaleDraw::setLength( double length )
pixhawk's avatar
pixhawk committed
616
{
Bryant's avatar
Bryant committed
617
#if 1
pixhawk's avatar
pixhawk committed
618 619
    if ( length >= 0 && length < 10 )
        length = 10;
Bryant's avatar
Bryant committed
620 621

    // why should we accept negative lengths ???
pixhawk's avatar
pixhawk committed
622 623
    if ( length < 0 && length > -10 )
        length = -10;
Bryant's avatar
Bryant committed
624 625 626
#else
    length = qMax( length, 10 );
#endif
627

pixhawk's avatar
pixhawk committed
628 629 630 631
    d_data->len = length;
    updateMap();
}

632
/*!
pixhawk's avatar
pixhawk committed
633 634 635
   \return the length of the backbone
   \sa setLength(), pos()
*/
Bryant's avatar
Bryant committed
636
double QwtScaleDraw::length() const
pixhawk's avatar
pixhawk committed
637 638 639 640
{
    return d_data->len;
}

641
/*!
pixhawk's avatar
pixhawk committed
642 643 644 645 646 647 648
   Draws the label for a major scale tick

   \param painter Painter
   \param value Value

   \sa drawTick(), drawBackbone(), boundingLabelRect()
*/
Bryant's avatar
Bryant committed
649
void QwtScaleDraw::drawLabel( QPainter *painter, double value ) const
pixhawk's avatar
pixhawk committed
650
{
Bryant's avatar
Bryant committed
651
    QwtText lbl = tickLabel( painter->font(), value );
pixhawk's avatar
pixhawk committed
652
    if ( lbl.isEmpty() )
653
        return;
pixhawk's avatar
pixhawk committed
654

Bryant's avatar
Bryant committed
655
    QPointF pos = labelPosition( value );
pixhawk's avatar
pixhawk committed
656

Bryant's avatar
Bryant committed
657
    QSizeF labelSize = lbl.textSize( painter->font() );
658

Bryant's avatar
Bryant committed
659
    const QTransform transform = labelTransformation( pos, labelSize );
pixhawk's avatar
pixhawk committed
660 661

    painter->save();
Bryant's avatar
Bryant committed
662 663 664
    painter->setWorldTransform( transform, true );

    lbl.draw ( painter, QRect( QPoint( 0, 0 ), labelSize.toSize() ) );
pixhawk's avatar
pixhawk committed
665 666 667 668 669

    painter->restore();
}

/*!
Bryant's avatar
Bryant committed
670 671 672
  \brief Find the bounding rectangle for the label.

  The coordinates of the rectangle are absolute ( calculated from pos() ).
pixhawk's avatar
pixhawk committed
673 674 675 676 677
  in direction of the tick.

  \param font Font used for painting
  \param value Value

Bryant's avatar
Bryant committed
678
  \return Bounding rectangle
pixhawk's avatar
pixhawk committed
679 680
  \sa labelRect()
*/
Bryant's avatar
Bryant committed
681
QRect QwtScaleDraw::boundingLabelRect( const QFont &font, double value ) const
pixhawk's avatar
pixhawk committed
682
{
Bryant's avatar
Bryant committed
683
    QwtText lbl = tickLabel( font, value );
pixhawk's avatar
pixhawk committed
684
    if ( lbl.isEmpty() )
685
        return QRect();
pixhawk's avatar
pixhawk committed
686

Bryant's avatar
Bryant committed
687 688
    const QPointF pos = labelPosition( value );
    QSizeF labelSize = lbl.textSize( font );
pixhawk's avatar
pixhawk committed
689

Bryant's avatar
Bryant committed
690 691
    const QTransform transform = labelTransformation( pos, labelSize );
    return transform.mapRect( QRect( QPoint( 0, 0 ), labelSize.toSize() ) );
pixhawk's avatar
pixhawk committed
692 693 694
}

/*!
Bryant's avatar
Bryant committed
695
   Calculate the transformation that is needed to paint a label
pixhawk's avatar
pixhawk committed
696 697 698 699 700
   depending on its alignment and rotation.

   \param pos Position where to paint the label
   \param size Size of the label

Bryant's avatar
Bryant committed
701
   \return Transformation matrix
pixhawk's avatar
pixhawk committed
702 703
   \sa setLabelAlignment(), setLabelRotation()
*/
Bryant's avatar
Bryant committed
704 705
QTransform QwtScaleDraw::labelTransformation(
    const QPointF &pos, const QSizeF &size ) const
706
{
Bryant's avatar
Bryant committed
707 708 709
    QTransform transform;
    transform.translate( pos.x(), pos.y() );
    transform.rotate( labelRotation() );
710

pixhawk's avatar
pixhawk committed
711
    int flags = labelAlignment();
Bryant's avatar
Bryant committed
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
    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;
            }
pixhawk's avatar
pixhawk committed
740 741 742
        }
    }

Bryant's avatar
Bryant committed
743
    double x, y;
744

pixhawk's avatar
pixhawk committed
745
    if ( flags & Qt::AlignLeft )
Bryant's avatar
Bryant committed
746
        x = -size.width();
pixhawk's avatar
pixhawk committed
747
    else if ( flags & Qt::AlignRight )
Bryant's avatar
Bryant committed
748
        x = 0.0;
pixhawk's avatar
pixhawk committed
749
    else // Qt::AlignHCenter
Bryant's avatar
Bryant committed
750
        x = -( 0.5 * size.width() );
751

pixhawk's avatar
pixhawk committed
752
    if ( flags & Qt::AlignTop )
Bryant's avatar
Bryant committed
753
        y = -size.height();
pixhawk's avatar
pixhawk committed
754
    else if ( flags & Qt::AlignBottom )
Bryant's avatar
Bryant committed
755
        y = 0;
pixhawk's avatar
pixhawk committed
756
    else // Qt::AlignVCenter
Bryant's avatar
Bryant committed
757
        y = -( 0.5 * size.height() );
758

Bryant's avatar
Bryant committed
759
    transform.translate( x, y );
760

Bryant's avatar
Bryant committed
761
    return transform;
762
}
pixhawk's avatar
pixhawk committed
763 764

/*!
Bryant's avatar
Bryant committed
765 766
  Find the bounding rectangle for the label. The coordinates of
  the rectangle are relative to spacing + tick length from the backbone
pixhawk's avatar
pixhawk committed
767 768 769 770
  in direction of the tick.

  \param font Font used for painting
  \param value Value
Bryant's avatar
Bryant committed
771 772

   \return Bounding rectangle that is needed to draw a label
pixhawk's avatar
pixhawk committed
773
*/
Bryant's avatar
Bryant committed
774
QRectF QwtScaleDraw::labelRect( const QFont &font, double value ) const
775
{
Bryant's avatar
Bryant committed
776
    QwtText lbl = tickLabel( font, value );
pixhawk's avatar
pixhawk committed
777
    if ( lbl.isEmpty() )
Bryant's avatar
Bryant committed
778
        return QRectF( 0.0, 0.0, 0.0, 0.0 );
pixhawk's avatar
pixhawk committed
779

Bryant's avatar
Bryant committed
780
    const QPointF pos = labelPosition( value );
pixhawk's avatar
pixhawk committed
781

Bryant's avatar
Bryant committed
782 783
    const QSizeF labelSize = lbl.textSize( font );
    const QTransform transform = labelTransformation( pos, labelSize );
pixhawk's avatar
pixhawk committed
784

Bryant's avatar
Bryant committed
785 786
    QRectF br = transform.mapRect( QRectF( QPointF( 0, 0 ), labelSize ) );
    br.translate( -pos.x(), -pos.y() );
pixhawk's avatar
pixhawk committed
787 788 789 790 791 792 793 794 795

    return br;
}

/*!
   Calculate the size that is needed to draw a label

   \param font Label font
   \param value Value
Bryant's avatar
Bryant committed
796 797

   \return Size that is needed to draw a label
pixhawk's avatar
pixhawk committed
798
*/
Bryant's avatar
Bryant committed
799
QSizeF QwtScaleDraw::labelSize( const QFont &font, double value ) const
pixhawk's avatar
pixhawk committed
800
{
Bryant's avatar
Bryant committed
801
    return labelRect( font, value ).size();
pixhawk's avatar
pixhawk committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
}

/*!
  Rotate all labels.

  When changing the rotation, it might be necessary to
  adjust the label flags too. Finding a useful combination is
  often the result of try and error.

  \param rotation Angle in degrees. When changing the label rotation,
                  the label flags often needs to be adjusted too.

  \sa setLabelAlignment(), labelRotation(), labelAlignment().

*/
Bryant's avatar
Bryant committed
817
void QwtScaleDraw::setLabelRotation( double rotation )
pixhawk's avatar
pixhawk committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
{
    d_data->labelRotation = rotation;
}

/*!
  \return the label rotation
  \sa setLabelRotation(), labelAlignment()
*/
double QwtScaleDraw::labelRotation() const
{
    return d_data->labelRotation;
}

/*!
  \brief Change the label flags

Bryant's avatar
Bryant committed
834
  Labels are aligned to the point tick length + spacing away from the backbone.
pixhawk's avatar
pixhawk committed
835 836

  The alignment is relative to the orientation of the label text.
837 838 839
  In case of an flags of 0 the label will be aligned
  depending on the orientation of the scale:

pixhawk's avatar
pixhawk committed
840 841 842 843
      QwtScaleDraw::TopScale: Qt::AlignHCenter | Qt::AlignTop\n
      QwtScaleDraw::BottomScale: Qt::AlignHCenter | Qt::AlignBottom\n
      QwtScaleDraw::LeftScale: Qt::AlignLeft | Qt::AlignVCenter\n
      QwtScaleDraw::RightScale: Qt::AlignRight | Qt::AlignVCenter\n
844

pixhawk's avatar
pixhawk committed
845
  Changing the alignment is often necessary for rotated labels.
846

Bryant's avatar
Bryant committed
847
  \param alignment Or'd Qt::AlignmentFlags see <qnamespace.h>
pixhawk's avatar
pixhawk committed
848 849

  \sa setLabelRotation(), labelRotation(), labelAlignment()
850
  \warning The various alignments might be confusing.
pixhawk's avatar
pixhawk committed
851 852
           The alignment of the label is not the alignment
           of the scale and is not the alignment of the flags
Bryant's avatar
Bryant committed
853
           ( QwtText::flags() ) returned from QwtAbstractScaleDraw::label().
854 855
*/

Bryant's avatar
Bryant committed
856
void QwtScaleDraw::setLabelAlignment( Qt::Alignment alignment )
pixhawk's avatar
pixhawk committed
857 858
{
    d_data->labelAlignment = alignment;
859
}
pixhawk's avatar
pixhawk committed
860 861 862 863 864 865 866 867 868 869 870 871 872 873

/*!
  \return the label flags
  \sa setLabelAlignment(), labelRotation()
*/
Qt::Alignment QwtScaleDraw::labelAlignment() const
{
    return d_data->labelAlignment;
}

/*!
  \param font Font
  \return the maximum width of a label
*/
Bryant's avatar
Bryant committed
874
int QwtScaleDraw::maxLabelWidth( const QFont &font ) const
pixhawk's avatar
pixhawk committed
875
{
Bryant's avatar
Bryant committed
876
    double maxWidth = 0.0;
pixhawk's avatar
pixhawk committed
877

Bryant's avatar
Bryant committed
878 879 880
    const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
    for ( int i = 0; i < ticks.count(); i++ )
    {
pixhawk's avatar
pixhawk committed
881
        const double v = ticks[i];
Bryant's avatar
Bryant committed
882 883 884
        if ( scaleDiv().contains( v ) )
        {
            const double w = labelSize( font, ticks[i] ).width();
pixhawk's avatar
pixhawk committed
885 886 887 888 889
            if ( w > maxWidth )
                maxWidth = w;
        }
    }

Bryant's avatar
Bryant committed
890
    return qCeil( maxWidth );
pixhawk's avatar
pixhawk committed
891 892 893 894 895 896
}

/*!
  \param font Font
  \return the maximum height of a label
*/
Bryant's avatar
Bryant committed
897
int QwtScaleDraw::maxLabelHeight( const QFont &font ) const
pixhawk's avatar
pixhawk committed
898
{
Bryant's avatar
Bryant committed
899
    double maxHeight = 0.0;
900

Bryant's avatar
Bryant committed
901 902 903
    const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
    for ( int i = 0; i < ticks.count(); i++ )
    {
pixhawk's avatar
pixhawk committed
904
        const double v = ticks[i];
Bryant's avatar
Bryant committed
905 906 907
        if ( scaleDiv().contains( v ) )
        {
            const double h = labelSize( font, ticks[i] ).height();
pixhawk's avatar
pixhawk committed
908
            if ( h > maxHeight )
909 910 911 912
                maxHeight = h;
        }
    }

Bryant's avatar
Bryant committed
913
    return qCeil( maxHeight );
914
}
pixhawk's avatar
pixhawk committed
915 916 917

void QwtScaleDraw::updateMap()
{
Bryant's avatar
Bryant committed
918 919 920
    const QPointF pos = d_data->pos;
    double len = d_data->len;

pixhawk's avatar
pixhawk committed
921 922
    QwtScaleMap &sm = scaleMap();
    if ( orientation() == Qt::Vertical )
Bryant's avatar
Bryant committed
923
        sm.setPaintInterval( pos.y() + len, pos.y() );
pixhawk's avatar
pixhawk committed
924
    else
Bryant's avatar
Bryant committed
925
        sm.setPaintInterval( pos.x(), pos.x() + len );
pixhawk's avatar
pixhawk committed
926
}