qwt_scale_draw.cpp 21.7 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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

// vim: expandtab

#include <qpen.h>
#include <qpainter.h>
#include "qwt_math.h"
#include "qwt_painter.h"
#include "qwt_polygon.h"
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"
#include "qwt_scale_draw.h"

#if QT_VERSION < 0x040000
#include <qwmatrix.h>
#define QwtMatrix QWMatrix
#else
#include <qmatrix.h>
#define QwtMatrix QMatrix
#endif

class QwtScaleDraw::PrivateData
{
public:
    PrivateData():
        len(0),
        alignment(QwtScaleDraw::BottomScale),
        labelAlignment(0),
36
        labelRotation(0.0) {
pixhawk's avatar
pixhawk committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    }

    QPoint pos;
    int len;

    Alignment alignment;

#if QT_VERSION < 0x040000
    int labelAlignment;
#else
    Qt::Alignment labelAlignment;
#endif
    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;
    setLength(100);
}

//! Copy constructor
QwtScaleDraw::QwtScaleDraw(const QwtScaleDraw &other):
    QwtAbstractScaleDraw(other)
{
    d_data = new QwtScaleDraw::PrivateData(*other.d_data);
}

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

//! Assignment operator
QwtScaleDraw &QwtScaleDraw::operator=(const QwtScaleDraw &other)
{
    *(QwtAbstractScaleDraw*)this = (const QwtAbstractScaleDraw &)other;
    *d_data = *other.d_data;
    return *this;
}

86
/*!
pixhawk's avatar
pixhawk committed
87 88 89
   Return alignment of the scale
   \sa setAlignment()
*/
90
QwtScaleDraw::Alignment QwtScaleDraw::alignment() const
pixhawk's avatar
pixhawk committed
91
{
92
    return d_data->alignment;
pixhawk's avatar
pixhawk committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
}

/*!
   Set the alignment of the scale

   The default alignment is QwtScaleDraw::BottomScale
   \sa alignment()
*/
void QwtScaleDraw::setAlignment(Alignment align)
{
    d_data->alignment = align;
}

/*!
  Return the orientation

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

  \sa alignment()
*/
Qt::Orientation QwtScaleDraw::orientation() const
{
116 117 118 119 120 121 122 123
    switch(d_data->alignment) {
    case TopScale:
    case BottomScale:
        return Qt::Horizontal;
    case LeftScale:
    case RightScale:
    default:
        return Qt::Vertical;
pixhawk's avatar
pixhawk committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137
    }
}

/*!
  \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
*/
void QwtScaleDraw::getBorderDistHint(const QFont &font,
138
                                     int &start, int &end ) const
pixhawk's avatar
pixhawk committed
139 140 141
{
    start = 0;
    end = 0;
142

pixhawk's avatar
pixhawk committed
143 144 145 146
    if ( !hasComponent(QwtAbstractScaleDraw::Labels) )
        return;

    const QwtValueList &ticks = scaleDiv().ticks(QwtScaleDiv::MajorTick);
147
    if ( ticks.count() == 0 )
pixhawk's avatar
pixhawk committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        return;

    QRect lr = labelRect(font, ticks[0]);

    // find the distance between tick and border
    int off = qwtAbs(map().transform(ticks[0]) - qRound(map().p1()));

    if ( orientation() == Qt::Vertical )
        end = lr.bottom() + 1 - off;
    else
        start = -lr.left() - off;

    const int lastTick = ticks.count() - 1;
    lr = labelRect(font, ticks[lastTick]);

    // find the distance between tick and border
    off = qwtAbs(map().transform(ticks[lastTick]) - qRound(map().p2()));

    if ( orientation() == Qt::Vertical )
        start = -lr.top() - off;
    else
        end = lr.right() + 1 - off;

    // if the distance between tick and border is larger
    // than half of the label width/height, we set to 0

    if ( start < 0 )
        start = 0;
    if ( end < 0 )
        end = 0;
}

/*!
  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()
*/

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]);
205
    if ( vertical ) {
pixhawk's avatar
pixhawk committed
206 207 208 209
        bRect2.setRect(-bRect2.bottom(), 0, bRect2.height(), bRect2.width());
    }
    int maxDist = 0;

210
    for (uint i = 1; i < (uint)ticks.count(); i++ ) {
pixhawk's avatar
pixhawk committed
211 212
        bRect1 = bRect2;
        bRect2 = labelRect(font, ticks[i]);
213
        if ( vertical ) {
pixhawk's avatar
pixhawk committed
214
            bRect2.setRect(-bRect2.bottom(), 0,
215
                           bRect2.height(), bRect2.width());
pixhawk's avatar
pixhawk committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
        }

        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;

235
    const int fmHeight = fm.ascent() - 2;
pixhawk's avatar
pixhawk committed
236 237 238 239 240 241 242 243 244 245

    // 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.
246
    labelDist++;
pixhawk's avatar
pixhawk committed
247

248
    // For text orientations close to the scale orientation
pixhawk's avatar
pixhawk committed
249 250 251 252

    if ( labelDist > maxDist )
        labelDist = maxDist;

253
    // For text orientations close to the opposite of the
pixhawk's avatar
pixhawk committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    // scale orientation

    if ( labelDist < fmHeight )
        labelDist = fmHeight;

    return labelDist;
}

/*!
   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 pen Pen that is used for painting backbone and ticks
   \param font Font used for painting the labels

   \sa minLength()
*/
int QwtScaleDraw::extent(const QPen &pen, const QFont &font) const
{
    int d = 0;

279
    if ( hasComponent(QwtAbstractScaleDraw::Labels) ) {
pixhawk's avatar
pixhawk committed
280 281 282 283 284 285 286 287 288
        if ( orientation() == Qt::Vertical )
            d = maxLabelWidth(font);
        else
            d = maxLabelHeight(font);

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

289
    if ( hasComponent(QwtAbstractScaleDraw::Ticks) ) {
pixhawk's avatar
pixhawk committed
290 291 292
        d += majTickLength();
    }

293
    if ( hasComponent(QwtAbstractScaleDraw::Backbone) ) {
pixhawk's avatar
pixhawk committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
        const int pw = qwtMax( 1, pen.width() );  // penwidth can be zero
        d += pw;
    }

    d = qwtMax(d, minimumExtent());
    return d;
}

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

   \param pen Pen that is used for painting backbone and ticks
   \param font Font used for painting the labels

   \sa extent()
*/
int QwtScaleDraw::minLength(const QPen &pen, const QFont &font) const
{
    int startDist, endDist;
    getBorderDistHint(font, startDist, endDist);

    const QwtScaleDiv &sd = scaleDiv();

    const uint minorCount =
        sd.ticks(QwtScaleDiv::MinorTick).count() +
        sd.ticks(QwtScaleDiv::MediumTick).count();
    const uint majorCount =
        sd.ticks(QwtScaleDiv::MajorTick).count();

    int lengthForLabels = 0;
324
    if ( hasComponent(QwtAbstractScaleDraw::Labels) ) {
pixhawk's avatar
pixhawk committed
325 326 327 328 329
        if ( majorCount >= 2 )
            lengthForLabels = minLabelDist(font) * (majorCount - 1);
    }

    int lengthForTicks = 0;
330
    if ( hasComponent(QwtAbstractScaleDraw::Ticks) ) {
pixhawk's avatar
pixhawk committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        const int pw = qwtMax( 1, pen.width() );  // penwidth can be zero
        lengthForTicks = 2 * (majorCount + minorCount) * pw;
    }

    return startDist + endDist + qwtMax(lengthForLabels, lengthForTicks);
}

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

   The position has a distance of majTickLength() + spacing() + 1
   from the backbone. The direction depends on the alignment()

   \param value Value
*/
QPoint QwtScaleDraw::labelPosition( double value) const
{
    const int tval = map().transform(value);
    int dist = spacing() + 1;
    if ( hasComponent(QwtAbstractScaleDraw::Ticks) )
        dist += majTickLength();

    int px = 0;
    int py = 0;

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    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
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    }

    return QPoint(px, py);
}

/*!
   Draw a tick

   \param painter Painter
   \param value Value of the tick
   \param len Lenght of the tick

   \sa drawBackbone(), drawLabel()
*/
void QwtScaleDraw::drawTick(QPainter *painter, double value, int len) const
{
    if ( len <= 0 )
        return;

    int pw2 = qwtMin((int)painter->pen().width(), len) / 2;
397

pixhawk's avatar
pixhawk committed
398 399 400 401
    QwtScaleMap scaleMap = map();
    const QwtMetricsMap metricsMap = QwtPainter::metricsMap();
    QPoint pos = d_data->pos;

402
    if ( !metricsMap.isIdentity() ) {
pixhawk's avatar
pixhawk committed
403 404
        /*
           The perfect position of the ticks is important.
405
           To avoid rounding errors we have to use
pixhawk's avatar
pixhawk committed
406 407 408 409 410
           device coordinates.
         */
        QwtPainter::resetMetricsMap();

        pos = metricsMap.layoutToDevice(pos);
411 412

        if ( orientation() == Qt::Vertical ) {
pixhawk's avatar
pixhawk committed
413 414 415 416 417
            scaleMap.setPaintInterval(
                metricsMap.layoutToDeviceY((int)scaleMap.p1()),
                metricsMap.layoutToDeviceY((int)scaleMap.p2())
            );
            len = metricsMap.layoutToDeviceX(len);
418
        } else {
pixhawk's avatar
pixhawk committed
419 420 421 422 423 424 425 426 427 428
            scaleMap.setPaintInterval(
                metricsMap.layoutToDeviceX((int)scaleMap.p1()),
                metricsMap.layoutToDeviceX((int)scaleMap.p2())
            );
            len = metricsMap.layoutToDeviceY(len);
        }
    }

    const int tval = scaleMap.transform(value);

429 430
    switch(alignment()) {
    case LeftScale: {
pixhawk's avatar
pixhawk committed
431
#if QT_VERSION < 0x040000
432 433
        QwtPainter::drawLine(painter, pos.x() + pw2, tval,
                             pos.x() - len - 2 * pw2, tval);
pixhawk's avatar
pixhawk committed
434
#else
435 436
        QwtPainter::drawLine(painter, pos.x() - pw2, tval,
                             pos.x() - len, tval);
pixhawk's avatar
pixhawk committed
437
#endif
438 439
        break;
    }
pixhawk's avatar
pixhawk committed
440

441
    case RightScale: {
pixhawk's avatar
pixhawk committed
442
#if QT_VERSION < 0x040000
443 444
        QwtPainter::drawLine(painter, pos.x(), tval,
                             pos.x() + len + pw2, tval);
pixhawk's avatar
pixhawk committed
445
#else
446 447
        QwtPainter::drawLine(painter, pos.x() + pw2, tval,
                             pos.x() + len, tval);
pixhawk's avatar
pixhawk committed
448
#endif
449 450 451 452
        break;
    }

    case BottomScale: {
pixhawk's avatar
pixhawk committed
453
#if QT_VERSION < 0x040000
454 455
        QwtPainter::drawLine(painter, tval, pos.y(),
                             tval, pos.y() + len + 2 * pw2);
pixhawk's avatar
pixhawk committed
456
#else
457 458
        QwtPainter::drawLine(painter, tval, pos.y() + pw2,
                             tval, pos.y() + len);
pixhawk's avatar
pixhawk committed
459
#endif
460 461
        break;
    }
pixhawk's avatar
pixhawk committed
462

463
    case TopScale: {
pixhawk's avatar
pixhawk committed
464
#if QT_VERSION < 0x040000
465 466
        QwtPainter::drawLine(painter, tval, pos.y() + pw2,
                             tval, pos.y() - len - 2 * pw2);
pixhawk's avatar
pixhawk committed
467
#else
468 469
        QwtPainter::drawLine(painter, tval, pos.y() - pw2,
                             tval, pos.y() - len);
pixhawk's avatar
pixhawk committed
470
#endif
471 472
        break;
    }
pixhawk's avatar
pixhawk committed
473 474 475 476
    }
    QwtPainter::setMetricsMap(metricsMap); // restore metrics map
}

477
/*!
pixhawk's avatar
pixhawk committed
478 479 480 481 482 483 484 485 486 487 488 489
   Draws the baseline of the scale
   \param painter Painter

   \sa drawTick(), drawLabel()
*/
void QwtScaleDraw::drawBackbone(QPainter *painter) const
{
    const int bw2 = painter->pen().width() / 2;

    const QPoint &pos = d_data->pos;
    const int len = d_data->len - 1;

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    switch(alignment()) {
    case LeftScale:
        QwtPainter::drawLine(painter, pos.x() - bw2,
                             pos.y(), pos.x() - bw2, pos.y() + len );
        break;
    case RightScale:
        QwtPainter::drawLine(painter, pos.x() + bw2,
                             pos.y(), pos.x() + bw2, pos.y() + len);
        break;
    case TopScale:
        QwtPainter::drawLine(painter, pos.x(), pos.y() - bw2,
                             pos.x() + len, pos.y() - bw2);
        break;
    case BottomScale:
        QwtPainter::drawLine(painter, pos.x(), pos.y() + bw2,
                             pos.x() + len, pos.y() + bw2);
        break;
pixhawk's avatar
pixhawk committed
507 508 509 510 511 512 513 514 515 516
    }
}

/*!
  \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
517 518
      backbone. The backbone is a vertical line.
      Scale marks and labels are drawn
pixhawk's avatar
pixhawk committed
519 520 521
      at the left of the backbone.
  <dt>QwtScaleDraw::RightScale
  <dd>The origin is the topmost point of the
522
      backbone. The backbone is a vertical line.
pixhawk's avatar
pixhawk committed
523 524 525 526
      Scale marks and labels are drawn
      at the right of the backbone.
  <dt>QwtScaleDraw::TopScale
  <dd>The origin is the leftmost point of the
527
      backbone. The backbone is a horizontal line.
pixhawk's avatar
pixhawk committed
528 529 530 531
      Scale marks and labels are drawn
      above the backbone.
  <dt>QwtScaleDraw::BottomScale
  <dd>The origin is the leftmost point of the
532
      backbone. The backbone is a horizontal line
pixhawk's avatar
pixhawk committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546
      Scale marks and labels are drawn
      below the backbone.
  </dl>

  \param pos Origin of the scale

  \sa pos(), setLength()
*/
void QwtScaleDraw::move(const QPoint &pos)
{
    d_data->pos = pos;
    updateMap();
}

547
/*!
pixhawk's avatar
pixhawk committed
548 549 550 551 552 553 554 555 556 557
   \return Origin of the scale
   \sa move(), length()
*/
QPoint QwtScaleDraw::pos() const
{
    return d_data->pos;
}

/*!
  Set the length of the backbone.
558

pixhawk's avatar
pixhawk committed
559 560 561 562 563 564 565 566 567 568 569
  The length doesn't include the space needed for
  overlapping labels.

  \sa move(), minLabelDist()
*/
void QwtScaleDraw::setLength(int length)
{
    if ( length >= 0 && length < 10 )
        length = 10;
    if ( length < 0 && length > -10 )
        length = -10;
570

pixhawk's avatar
pixhawk committed
571 572 573 574
    d_data->len = length;
    updateMap();
}

575
/*!
pixhawk's avatar
pixhawk committed
576 577 578 579 580 581 582 583
   \return the length of the backbone
   \sa setLength(), pos()
*/
int QwtScaleDraw::length() const
{
    return d_data->len;
}

584
/*!
pixhawk's avatar
pixhawk committed
585 586 587 588 589 590 591 592 593 594 595
   Draws the label for a major scale tick

   \param painter Painter
   \param value Value

   \sa drawTick(), drawBackbone(), boundingLabelRect()
*/
void QwtScaleDraw::drawLabel(QPainter *painter, double value) const
{
    QwtText lbl = tickLabel(painter->font(), value);
    if ( lbl.isEmpty() )
596
        return;
pixhawk's avatar
pixhawk committed
597 598 599 600 601 602

    const QPoint pos = labelPosition(value);

    QSize labelSize = lbl.textSize(painter->font());
    if ( labelSize.height() % 2 )
        labelSize.setHeight(labelSize.height() + 1);
603

pixhawk's avatar
pixhawk committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    const QwtMatrix m = labelMatrix( pos, labelSize);

    painter->save();
#if QT_VERSION < 0x040000
    painter->setWorldMatrix(m, true);
#else
    painter->setMatrix(m, true);
#endif

    lbl.draw (painter, QRect(QPoint(0, 0), labelSize) );
    painter->restore();
}

/*!
  Find the bounding rect for the label. The coordinates of
  the rect are absolute coordinates ( calculated from pos() ).
  in direction of the tick.

  \param font Font used for painting
  \param value Value

  \sa labelRect()
*/
QRect QwtScaleDraw::boundingLabelRect(const QFont &font, double value) const
{
    QwtText lbl = tickLabel(font, value);
    if ( lbl.isEmpty() )
631
        return QRect();
pixhawk's avatar
pixhawk committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

    const QPoint pos = labelPosition(value);
    QSize labelSize = lbl.textSize(font);
    if ( labelSize.height() % 2 )
        labelSize.setHeight(labelSize.height() + 1);

    const QwtMatrix m = labelMatrix( pos, labelSize);
    return m.mapRect(QRect(QPoint(0, 0), labelSize));
}

/*!
   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()
*/
651
QwtMatrix QwtScaleDraw::labelMatrix(
pixhawk's avatar
pixhawk committed
652
    const QPoint &pos, const QSize &size) const
653
{
pixhawk's avatar
pixhawk committed
654 655 656
    QwtMatrix m;
    m.translate(pos.x(), pos.y());
    m.rotate(labelRotation());
657

pixhawk's avatar
pixhawk committed
658
    int flags = labelAlignment();
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
    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
681 682 683 684 685 686 687
        }
    }

    const int w = size.width();
    const int h = size.height();

    int x, y;
688

pixhawk's avatar
pixhawk committed
689 690 691
    if ( flags & Qt::AlignLeft )
        x = -w;
    else if ( flags & Qt::AlignRight )
692
        x = -(w % 2);
pixhawk's avatar
pixhawk committed
693 694
    else // Qt::AlignHCenter
        x = -(w / 2);
695

pixhawk's avatar
pixhawk committed
696 697 698
    if ( flags & Qt::AlignTop )
        y =  -h ;
    else if ( flags & Qt::AlignBottom )
699
        y = -(h % 2);
pixhawk's avatar
pixhawk committed
700 701
    else // Qt::AlignVCenter
        y = -(h/2);
702

pixhawk's avatar
pixhawk committed
703
    m.translate(x, y);
704

pixhawk's avatar
pixhawk committed
705
    return m;
706
}
pixhawk's avatar
pixhawk committed
707 708 709 710 711 712 713 714 715 716

/*!
  Find the bounding rect for the label. The coordinates of
  the rect are relative to spacing + ticklength from the backbone
  in direction of the tick.

  \param font Font used for painting
  \param value Value
*/
QRect QwtScaleDraw::labelRect(const QFont &font, double value) const
717
{
pixhawk's avatar
pixhawk committed
718 719 720 721 722 723 724
    QwtText lbl = tickLabel(font, value);
    if ( lbl.isEmpty() )
        return QRect(0, 0, 0, 0);

    const QPoint pos = labelPosition(value);

    QSize labelSize = lbl.textSize(font);
725
    if ( labelSize.height() % 2 ) {
pixhawk's avatar
pixhawk committed
726 727 728 729 730 731 732 733 734
        labelSize.setHeight(labelSize.height() + 1);
    }

    const QwtMatrix m = labelMatrix(pos, labelSize);

#if 0
    QRect br = QwtMetricsMap::translate(m, QRect(QPoint(0, 0), labelSize));
#else
    QwtPolygon pol(4);
735
    pol.setPoint(0, 0, 0);
pixhawk's avatar
pixhawk committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
    pol.setPoint(1, 0, labelSize.height() - 1 );
    pol.setPoint(2, labelSize.width() - 1, 0);
    pol.setPoint(3, labelSize.width() - 1, labelSize.height() - 1 );

    pol = QwtMetricsMap::translate(m, pol);
    QRect br = pol.boundingRect();
#endif

#if QT_VERSION < 0x040000
    br.moveBy(-pos.x(), -pos.y());
#else
    br.translate(-pos.x(), -pos.y());
#endif

    return br;
}

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

   \param font Label font
   \param value Value
*/
QSize QwtScaleDraw::labelSize(const QFont &font, double value) const
{
    return labelRect(font, value).size();
}

/*!
  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().

*/
void QwtScaleDraw::setLabelRotation(double rotation)
{
    d_data->labelRotation = rotation;
}

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

/*!
  \brief Change the label flags

  Labels are aligned to the point ticklength + spacing away from the backbone.

  The alignment is relative to the orientation of the label text.
797 798 799
  In case of an flags of 0 the label will be aligned
  depending on the orientation of the scale:

pixhawk's avatar
pixhawk committed
800 801 802 803
      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
804

pixhawk's avatar
pixhawk committed
805
  Changing the alignment is often necessary for rotated labels.
806

pixhawk's avatar
pixhawk committed
807 808 809
  \param alignment Or'd Qt::AlignmentFlags <see qnamespace.h>

  \sa setLabelRotation(), labelRotation(), labelAlignment()
810
  \warning The various alignments might be confusing.
pixhawk's avatar
pixhawk committed
811 812 813
           The alignment of the label is not the alignment
           of the scale and is not the alignment of the flags
           (QwtText::flags()) returned from QwtAbstractScaleDraw::label().
814 815
*/

pixhawk's avatar
pixhawk committed
816 817 818 819 820 821 822
#if QT_VERSION < 0x040000
void QwtScaleDraw::setLabelAlignment(int alignment)
#else
void QwtScaleDraw::setLabelAlignment(Qt::Alignment alignment)
#endif
{
    d_data->labelAlignment = alignment;
823
}
pixhawk's avatar
pixhawk committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846

/*!
  \return the label flags
  \sa setLabelAlignment(), labelRotation()
*/
#if QT_VERSION < 0x040000
int QwtScaleDraw::labelAlignment() const
#else
Qt::Alignment QwtScaleDraw::labelAlignment() const
#endif
{
    return d_data->labelAlignment;
}

/*!
  \param font Font
  \return the maximum width of a label
*/
int QwtScaleDraw::maxLabelWidth(const QFont &font) const
{
    int maxWidth = 0;

    const QwtValueList &ticks = scaleDiv().ticks(QwtScaleDiv::MajorTick);
847
    for (uint i = 0; i < (uint)ticks.count(); i++) {
pixhawk's avatar
pixhawk committed
848
        const double v = ticks[i];
849
        if ( scaleDiv().contains(v) ) {
pixhawk's avatar
pixhawk committed
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
            const int w = labelSize(font, ticks[i]).width();
            if ( w > maxWidth )
                maxWidth = w;
        }
    }

    return maxWidth;
}

/*!
  \param font Font
  \return the maximum height of a label
*/
int QwtScaleDraw::maxLabelHeight(const QFont &font) const
{
    int maxHeight = 0;
866

pixhawk's avatar
pixhawk committed
867
    const QwtValueList &ticks = scaleDiv().ticks(QwtScaleDiv::MajorTick);
868
    for (uint i = 0; i < (uint)ticks.count(); i++) {
pixhawk's avatar
pixhawk committed
869
        const double v = ticks[i];
870
        if ( scaleDiv().contains(v) ) {
pixhawk's avatar
pixhawk committed
871 872
            const int h = labelSize(font, ticks[i]).height();
            if ( h > maxHeight )
873 874 875 876
                maxHeight = h;
        }
    }

pixhawk's avatar
pixhawk committed
877
    return maxHeight;
878
}
pixhawk's avatar
pixhawk committed
879 880 881 882 883 884 885 886 887

void QwtScaleDraw::updateMap()
{
    QwtScaleMap &sm = scaleMap();
    if ( orientation() == Qt::Vertical )
        sm.setPaintInterval(d_data->pos.y() + d_data->len, d_data->pos.y());
    else
        sm.setPaintInterval(d_data->pos.x(), d_data->pos.x() + d_data->len);
}