[4271] | 1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
| 2 | * Qwt Widget Library
|
---|
| 3 | * Copyright (C) 1997 Josef Wilgen
|
---|
| 4 | * Copyright (C) 2002 Uwe Rathmann
|
---|
| 5 | *
|
---|
| 6 | * This library is free software; you can redistribute it and/or
|
---|
| 7 | * modify it under the terms of the Qwt License, Version 1.0
|
---|
| 8 | *****************************************************************************/
|
---|
| 9 |
|
---|
| 10 | #include "qwt_scale_draw.h"
|
---|
| 11 | #include "qwt_scale_div.h"
|
---|
| 12 | #include "qwt_scale_map.h"
|
---|
| 13 | #include "qwt_math.h"
|
---|
| 14 | #include "qwt_painter.h"
|
---|
| 15 | #include <qpen.h>
|
---|
| 16 | #include <qpainter.h>
|
---|
| 17 | #include <qmath.h>
|
---|
| 18 |
|
---|
| 19 | class QwtScaleDraw::PrivateData
|
---|
| 20 | {
|
---|
| 21 | public:
|
---|
| 22 | PrivateData():
|
---|
| 23 | len( 0 ),
|
---|
| 24 | alignment( QwtScaleDraw::BottomScale ),
|
---|
| 25 | labelAlignment( 0 ),
|
---|
| 26 | labelRotation( 0.0 )
|
---|
| 27 | {
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | QPointF pos;
|
---|
| 31 | double len;
|
---|
| 32 |
|
---|
| 33 | Alignment alignment;
|
---|
| 34 |
|
---|
| 35 | Qt::Alignment labelAlignment;
|
---|
| 36 | double labelRotation;
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | /*!
|
---|
| 40 | \brief Constructor
|
---|
| 41 |
|
---|
| 42 | The range of the scale is initialized to [0, 100],
|
---|
| 43 | The position is at (0, 0) with a length of 100.
|
---|
| 44 | The orientation is QwtAbstractScaleDraw::Bottom.
|
---|
| 45 | */
|
---|
| 46 | QwtScaleDraw::QwtScaleDraw()
|
---|
| 47 | {
|
---|
| 48 | d_data = new QwtScaleDraw::PrivateData;
|
---|
| 49 | setLength( 100 );
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | //! Destructor
|
---|
| 53 | QwtScaleDraw::~QwtScaleDraw()
|
---|
| 54 | {
|
---|
| 55 | delete d_data;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /*!
|
---|
| 59 | Return alignment of the scale
|
---|
| 60 | \sa setAlignment()
|
---|
| 61 | */
|
---|
| 62 | QwtScaleDraw::Alignment QwtScaleDraw::alignment() const
|
---|
| 63 | {
|
---|
| 64 | return d_data->alignment;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /*!
|
---|
| 68 | Set the alignment of the scale
|
---|
| 69 |
|
---|
| 70 | The default alignment is QwtScaleDraw::BottomScale
|
---|
| 71 | \sa alignment()
|
---|
| 72 | */
|
---|
| 73 | void QwtScaleDraw::setAlignment( Alignment align )
|
---|
| 74 | {
|
---|
| 75 | d_data->alignment = align;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /*!
|
---|
| 79 | Return the orientation
|
---|
| 80 |
|
---|
| 81 | TopScale, BottomScale are horizontal (Qt::Horizontal) scales,
|
---|
| 82 | LeftScale, RightScale are vertical (Qt::Vertical) scales.
|
---|
| 83 |
|
---|
| 84 | \sa alignment()
|
---|
| 85 | */
|
---|
| 86 | Qt::Orientation QwtScaleDraw::orientation() const
|
---|
| 87 | {
|
---|
| 88 | switch ( d_data->alignment )
|
---|
| 89 | {
|
---|
| 90 | case TopScale:
|
---|
| 91 | case BottomScale:
|
---|
| 92 | return Qt::Horizontal;
|
---|
| 93 | case LeftScale:
|
---|
| 94 | case RightScale:
|
---|
| 95 | default:
|
---|
| 96 | return Qt::Vertical;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /*!
|
---|
| 101 | \brief Determine the minimum border distance
|
---|
| 102 |
|
---|
| 103 | This member function returns the minimum space
|
---|
| 104 | needed to draw the mark labels at the scale's endpoints.
|
---|
| 105 |
|
---|
| 106 | \param font Font
|
---|
| 107 | \param start Start border distance
|
---|
| 108 | \param end End border distance
|
---|
| 109 | */
|
---|
| 110 | void QwtScaleDraw::getBorderDistHint( const QFont &font,
|
---|
| 111 | int &start, int &end ) const
|
---|
| 112 | {
|
---|
| 113 | start = 0;
|
---|
| 114 | end = 0;
|
---|
| 115 |
|
---|
| 116 | if ( !hasComponent( QwtAbstractScaleDraw::Labels ) )
|
---|
| 117 | return;
|
---|
| 118 |
|
---|
| 119 | const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
|
---|
| 120 | if ( ticks.count() == 0 )
|
---|
| 121 | return;
|
---|
| 122 |
|
---|
| 123 | // Find the ticks, that are mapped to the borders.
|
---|
| 124 | // minTick is the tick, that is mapped to the top/left-most position
|
---|
| 125 | // in widget coordinates.
|
---|
| 126 |
|
---|
| 127 | double minTick = ticks[0];
|
---|
| 128 | double minPos = scaleMap().transform( minTick );
|
---|
| 129 | double maxTick = minTick;
|
---|
| 130 | double maxPos = minPos;
|
---|
| 131 |
|
---|
| 132 | for ( int i = 1; i < ticks.count(); i++ )
|
---|
| 133 | {
|
---|
| 134 | const double tickPos = scaleMap().transform( ticks[i] );
|
---|
| 135 | if ( tickPos < minPos )
|
---|
| 136 | {
|
---|
| 137 | minTick = ticks[i];
|
---|
| 138 | minPos = tickPos;
|
---|
| 139 | }
|
---|
| 140 | if ( tickPos > scaleMap().transform( maxTick ) )
|
---|
| 141 | {
|
---|
| 142 | maxTick = ticks[i];
|
---|
| 143 | maxPos = tickPos;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | double e = 0.0;
|
---|
| 148 | double s = 0.0;
|
---|
| 149 | if ( orientation() == Qt::Vertical )
|
---|
| 150 | {
|
---|
| 151 | s = -labelRect( font, minTick ).top();
|
---|
| 152 | s -= qAbs( minPos - qRound( scaleMap().p2() ) );
|
---|
| 153 |
|
---|
| 154 | e = labelRect( font, maxTick ).bottom();
|
---|
| 155 | e -= qAbs( maxPos - scaleMap().p1() );
|
---|
| 156 | }
|
---|
| 157 | else
|
---|
| 158 | {
|
---|
| 159 | s = -labelRect( font, minTick ).left();
|
---|
| 160 | s -= qAbs( minPos - scaleMap().p1() );
|
---|
| 161 |
|
---|
| 162 | e = labelRect( font, maxTick ).right();
|
---|
| 163 | e -= qAbs( maxPos - scaleMap().p2() );
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | if ( s < 0.0 )
|
---|
| 167 | s = 0.0;
|
---|
| 168 | if ( e < 0.0 )
|
---|
| 169 | e = 0.0;
|
---|
| 170 |
|
---|
| 171 | start = qCeil( s );
|
---|
| 172 | end = qCeil( e );
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /*!
|
---|
| 176 | Determine the minimum distance between two labels, that is necessary
|
---|
| 177 | that the texts don't overlap.
|
---|
| 178 |
|
---|
| 179 | \param font Font
|
---|
| 180 | \return The maximum width of a label
|
---|
| 181 |
|
---|
| 182 | \sa getBorderDistHint()
|
---|
| 183 | */
|
---|
| 184 |
|
---|
| 185 | int QwtScaleDraw::minLabelDist( const QFont &font ) const
|
---|
| 186 | {
|
---|
| 187 | if ( !hasComponent( QwtAbstractScaleDraw::Labels ) )
|
---|
| 188 | return 0;
|
---|
| 189 |
|
---|
| 190 | const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
|
---|
| 191 | if ( ticks.count() == 0 )
|
---|
| 192 | return 0;
|
---|
| 193 |
|
---|
| 194 | const QFontMetrics fm( font );
|
---|
| 195 |
|
---|
| 196 | const bool vertical = ( orientation() == Qt::Vertical );
|
---|
| 197 |
|
---|
| 198 | QRectF bRect1;
|
---|
| 199 | QRectF bRect2 = labelRect( font, ticks[0] );
|
---|
| 200 | if ( vertical )
|
---|
| 201 | {
|
---|
| 202 | bRect2.setRect( -bRect2.bottom(), 0, bRect2.height(), bRect2.width() );
|
---|
| 203 | }
|
---|
| 204 | int maxDist = 0;
|
---|
| 205 |
|
---|
| 206 | for ( int i = 1; i < ticks.count(); i++ )
|
---|
| 207 | {
|
---|
| 208 | bRect1 = bRect2;
|
---|
| 209 | bRect2 = labelRect( font, ticks[i] );
|
---|
| 210 | if ( vertical )
|
---|
| 211 | {
|
---|
| 212 | bRect2.setRect( -bRect2.bottom(), 0,
|
---|
| 213 | bRect2.height(), bRect2.width() );
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | int dist = fm.leading(); // space between the labels
|
---|
| 217 | if ( bRect1.right() > 0 )
|
---|
| 218 | dist += bRect1.right();
|
---|
| 219 | if ( bRect2.left() < 0 )
|
---|
| 220 | dist += -bRect2.left();
|
---|
| 221 |
|
---|
| 222 | if ( dist > maxDist )
|
---|
| 223 | maxDist = dist;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | double angle = labelRotation() / 180.0 * M_PI;
|
---|
| 227 | if ( vertical )
|
---|
| 228 | angle += M_PI / 2;
|
---|
| 229 |
|
---|
| 230 | if ( qSin( angle ) == 0.0 )
|
---|
| 231 | return maxDist;
|
---|
| 232 |
|
---|
| 233 | const int fmHeight = fm.ascent() - 2;
|
---|
| 234 |
|
---|
| 235 | // The distance we need until there is
|
---|
| 236 | // the height of the label font. This height is needed
|
---|
| 237 | // for the neighbour labal.
|
---|
| 238 |
|
---|
| 239 | int labelDist = qFloor( fmHeight / qSin( angle ) * qCos( angle ) );
|
---|
| 240 | if ( labelDist < 0 )
|
---|
| 241 | labelDist = -labelDist;
|
---|
| 242 |
|
---|
| 243 | // The cast above floored labelDist. We want to ceil.
|
---|
| 244 | labelDist++;
|
---|
| 245 |
|
---|
| 246 | // For text orientations close to the scale orientation
|
---|
| 247 |
|
---|
| 248 | if ( labelDist > maxDist )
|
---|
| 249 | labelDist = maxDist;
|
---|
| 250 |
|
---|
| 251 | // For text orientations close to the opposite of the
|
---|
| 252 | // scale orientation
|
---|
| 253 |
|
---|
| 254 | if ( labelDist < fmHeight )
|
---|
| 255 | labelDist = fmHeight;
|
---|
| 256 |
|
---|
| 257 | return labelDist;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | /*!
|
---|
| 261 | Calculate the width/height that is needed for a
|
---|
| 262 | vertical/horizontal scale.
|
---|
| 263 |
|
---|
| 264 | The extent is calculated from the pen width of the backbone,
|
---|
| 265 | the major tick length, the spacing and the maximum width/height
|
---|
| 266 | of the labels.
|
---|
| 267 |
|
---|
| 268 | \param font Font used for painting the labels
|
---|
| 269 |
|
---|
| 270 | \sa minLength()
|
---|
| 271 | */
|
---|
| 272 | double QwtScaleDraw::extent( const QFont &font ) const
|
---|
| 273 | {
|
---|
| 274 | double d = 0;
|
---|
| 275 |
|
---|
| 276 | if ( hasComponent( QwtAbstractScaleDraw::Labels ) )
|
---|
| 277 | {
|
---|
| 278 | if ( orientation() == Qt::Vertical )
|
---|
| 279 | d = maxLabelWidth( font );
|
---|
| 280 | else
|
---|
| 281 | d = maxLabelHeight( font );
|
---|
| 282 |
|
---|
| 283 | if ( d > 0 )
|
---|
| 284 | d += spacing();
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
|
---|
| 288 | {
|
---|
| 289 | d += maxTickLength();
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | if ( hasComponent( QwtAbstractScaleDraw::Backbone ) )
|
---|
| 293 | {
|
---|
| 294 | const double pw = qMax( 1, penWidth() ); // penwidth can be zero
|
---|
| 295 | d += pw;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | d = qMax( d, minimumExtent() );
|
---|
| 299 | return d;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | /*!
|
---|
| 303 | Calculate the minimum length that is needed to draw the scale
|
---|
| 304 |
|
---|
| 305 | \param font Font used for painting the labels
|
---|
| 306 |
|
---|
| 307 | \sa extent()
|
---|
| 308 | */
|
---|
| 309 | int QwtScaleDraw::minLength( const QFont &font ) const
|
---|
| 310 | {
|
---|
| 311 | int startDist, endDist;
|
---|
| 312 | getBorderDistHint( font, startDist, endDist );
|
---|
| 313 |
|
---|
| 314 | const QwtScaleDiv &sd = scaleDiv();
|
---|
| 315 |
|
---|
| 316 | const uint minorCount =
|
---|
| 317 | sd.ticks( QwtScaleDiv::MinorTick ).count() +
|
---|
| 318 | sd.ticks( QwtScaleDiv::MediumTick ).count();
|
---|
| 319 | const uint majorCount =
|
---|
| 320 | sd.ticks( QwtScaleDiv::MajorTick ).count();
|
---|
| 321 |
|
---|
| 322 | int lengthForLabels = 0;
|
---|
| 323 | if ( hasComponent( QwtAbstractScaleDraw::Labels ) )
|
---|
| 324 | lengthForLabels = minLabelDist( font ) * majorCount;
|
---|
| 325 |
|
---|
| 326 | int lengthForTicks = 0;
|
---|
| 327 | if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
|
---|
| 328 | {
|
---|
| 329 | const double pw = qMax( 1, penWidth() ); // penwidth can be zero
|
---|
| 330 | lengthForTicks = qCeil( ( majorCount + minorCount ) * ( pw + 1.0 ) );
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | return startDist + endDist + qMax( lengthForLabels, lengthForTicks );
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | /*!
|
---|
| 337 | Find the position, where to paint a label
|
---|
| 338 |
|
---|
| 339 | The position has a distance of majTickLength() + spacing() + 1
|
---|
| 340 | from the backbone. The direction depends on the alignment()
|
---|
| 341 |
|
---|
| 342 | \param value Value
|
---|
| 343 | */
|
---|
| 344 | QPointF QwtScaleDraw::labelPosition( double value ) const
|
---|
| 345 | {
|
---|
| 346 | const double tval = scaleMap().transform( value );
|
---|
| 347 | double dist = spacing();
|
---|
| 348 | if ( hasComponent( QwtAbstractScaleDraw::Backbone ) )
|
---|
| 349 | dist += qMax( 1, penWidth() );
|
---|
| 350 |
|
---|
| 351 | if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
|
---|
| 352 | dist += tickLength( QwtScaleDiv::MajorTick );
|
---|
| 353 |
|
---|
| 354 | double px = 0;
|
---|
| 355 | double py = 0;
|
---|
| 356 |
|
---|
| 357 | switch ( alignment() )
|
---|
| 358 | {
|
---|
| 359 | case RightScale:
|
---|
| 360 | {
|
---|
| 361 | px = d_data->pos.x() + dist;
|
---|
| 362 | py = tval;
|
---|
| 363 | break;
|
---|
| 364 | }
|
---|
| 365 | case LeftScale:
|
---|
| 366 | {
|
---|
| 367 | px = d_data->pos.x() - dist;
|
---|
| 368 | py = tval;
|
---|
| 369 | break;
|
---|
| 370 | }
|
---|
| 371 | case BottomScale:
|
---|
| 372 | {
|
---|
| 373 | px = tval;
|
---|
| 374 | py = d_data->pos.y() + dist;
|
---|
| 375 | break;
|
---|
| 376 | }
|
---|
| 377 | case TopScale:
|
---|
| 378 | {
|
---|
| 379 | px = tval;
|
---|
| 380 | py = d_data->pos.y() - dist;
|
---|
| 381 | break;
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | return QPointF( px, py );
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | /*!
|
---|
| 389 | Draw a tick
|
---|
| 390 |
|
---|
| 391 | \param painter Painter
|
---|
| 392 | \param value Value of the tick
|
---|
| 393 | \param len Lenght of the tick
|
---|
| 394 |
|
---|
| 395 | \sa drawBackbone(), drawLabel()
|
---|
| 396 | */
|
---|
| 397 | void QwtScaleDraw::drawTick( QPainter *painter, double value, double len ) const
|
---|
| 398 | {
|
---|
| 399 | if ( len <= 0 )
|
---|
| 400 | return;
|
---|
| 401 |
|
---|
| 402 | const bool roundingAlignment = QwtPainter::roundingAlignment( painter );
|
---|
| 403 |
|
---|
| 404 | QPointF pos = d_data->pos;
|
---|
| 405 |
|
---|
| 406 | double tval = scaleMap().transform( value );
|
---|
| 407 | if ( roundingAlignment )
|
---|
| 408 | tval = qRound( tval );
|
---|
| 409 |
|
---|
| 410 | const int pw = penWidth();
|
---|
| 411 | int a = 0;
|
---|
| 412 | if ( pw > 1 && roundingAlignment )
|
---|
| 413 | a = 1;
|
---|
| 414 |
|
---|
| 415 | switch ( alignment() )
|
---|
| 416 | {
|
---|
| 417 | case LeftScale:
|
---|
| 418 | {
|
---|
| 419 | double x1 = pos.x() + a;
|
---|
| 420 | double x2 = pos.x() + a - pw - len;
|
---|
| 421 | if ( roundingAlignment )
|
---|
| 422 | {
|
---|
| 423 | x1 = qRound( x1 );
|
---|
| 424 | x2 = qRound( x2 );
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | QwtPainter::drawLine( painter, x1, tval, x2, tval );
|
---|
| 428 | break;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | case RightScale:
|
---|
| 432 | {
|
---|
| 433 | double x1 = pos.x();
|
---|
| 434 | double x2 = pos.x() + pw + len;
|
---|
| 435 | if ( roundingAlignment )
|
---|
| 436 | {
|
---|
| 437 | x1 = qRound( x1 );
|
---|
| 438 | x2 = qRound( x2 );
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | QwtPainter::drawLine( painter, x1, tval, x2, tval );
|
---|
| 442 | break;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | case BottomScale:
|
---|
| 446 | {
|
---|
| 447 | double y1 = pos.y();
|
---|
| 448 | double y2 = pos.y() + pw + len;
|
---|
| 449 | if ( roundingAlignment )
|
---|
| 450 | {
|
---|
| 451 | y1 = qRound( y1 );
|
---|
| 452 | y2 = qRound( y2 );
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | QwtPainter::drawLine( painter, tval, y1, tval, y2 );
|
---|
| 456 | break;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | case TopScale:
|
---|
| 460 | {
|
---|
| 461 | double y1 = pos.y() + a;
|
---|
| 462 | double y2 = pos.y() - pw - len + a;
|
---|
| 463 | if ( roundingAlignment )
|
---|
| 464 | {
|
---|
| 465 | y1 = qRound( y1 );
|
---|
| 466 | y2 = qRound( y2 );
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | QwtPainter::drawLine( painter, tval, y1, tval, y2 );
|
---|
| 470 | break;
|
---|
| 471 | }
|
---|
| 472 | }
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | /*!
|
---|
| 476 | Draws the baseline of the scale
|
---|
| 477 | \param painter Painter
|
---|
| 478 |
|
---|
| 479 | \sa drawTick(), drawLabel()
|
---|
| 480 | */
|
---|
| 481 | void QwtScaleDraw::drawBackbone( QPainter *painter ) const
|
---|
| 482 | {
|
---|
| 483 | const bool doAlign = QwtPainter::roundingAlignment( painter );
|
---|
| 484 |
|
---|
| 485 | const QPointF &pos = d_data->pos;
|
---|
| 486 | const double len = d_data->len;
|
---|
| 487 | const int pw = qMax( penWidth(), 1 );
|
---|
| 488 |
|
---|
| 489 | // pos indicates a border not the center of the backbone line
|
---|
| 490 | // so we need to shift its position depending on the pen width
|
---|
| 491 | // and the alignment of the scale
|
---|
| 492 |
|
---|
| 493 | double off;
|
---|
| 494 | if ( doAlign )
|
---|
| 495 | {
|
---|
| 496 | if ( alignment() == LeftScale || alignment() == TopScale )
|
---|
| 497 | off = ( pw - 1 ) / 2;
|
---|
| 498 | else
|
---|
| 499 | off = pw / 2;
|
---|
| 500 | }
|
---|
| 501 | else
|
---|
| 502 | {
|
---|
| 503 | off = 0.5 * penWidth();
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | switch ( alignment() )
|
---|
| 507 | {
|
---|
| 508 | case LeftScale:
|
---|
| 509 | {
|
---|
| 510 | double x = pos.x() - off;
|
---|
| 511 | if ( doAlign )
|
---|
| 512 | x = qRound( x );
|
---|
| 513 |
|
---|
| 514 | QwtPainter::drawLine( painter, x, pos.y(), x, pos.y() + len );
|
---|
| 515 | break;
|
---|
| 516 | }
|
---|
| 517 | case RightScale:
|
---|
| 518 | {
|
---|
| 519 | double x = pos.x() + off;
|
---|
| 520 | if ( doAlign )
|
---|
| 521 | x = qRound( x );
|
---|
| 522 |
|
---|
| 523 | QwtPainter::drawLine( painter, x, pos.y(), x, pos.y() + len );
|
---|
| 524 | break;
|
---|
| 525 | }
|
---|
| 526 | case TopScale:
|
---|
| 527 | {
|
---|
| 528 | double y = pos.y() - off;
|
---|
| 529 | if ( doAlign )
|
---|
| 530 | y = qRound( y );
|
---|
| 531 |
|
---|
| 532 | QwtPainter::drawLine( painter, pos.x(), y, pos.x() + len, y );
|
---|
| 533 | break;
|
---|
| 534 | }
|
---|
| 535 | case BottomScale:
|
---|
| 536 | {
|
---|
| 537 | double y = pos.y() + off;
|
---|
| 538 | if ( doAlign )
|
---|
| 539 | y = qRound( y );
|
---|
| 540 |
|
---|
| 541 | QwtPainter::drawLine( painter, pos.x(), y, pos.x() + len, y );
|
---|
| 542 | break;
|
---|
| 543 | }
|
---|
| 544 | }
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | /*!
|
---|
| 548 | \brief Move the position of the scale
|
---|
| 549 |
|
---|
| 550 | The meaning of the parameter pos depends on the alignment:
|
---|
| 551 | <dl>
|
---|
| 552 | <dt>QwtScaleDraw::LeftScale
|
---|
| 553 | <dd>The origin is the topmost point of the
|
---|
| 554 | backbone. The backbone is a vertical line.
|
---|
| 555 | Scale marks and labels are drawn
|
---|
| 556 | at the left of the backbone.
|
---|
| 557 | <dt>QwtScaleDraw::RightScale
|
---|
| 558 | <dd>The origin is the topmost point of the
|
---|
| 559 | backbone. The backbone is a vertical line.
|
---|
| 560 | Scale marks and labels are drawn
|
---|
| 561 | at the right of the backbone.
|
---|
| 562 | <dt>QwtScaleDraw::TopScale
|
---|
| 563 | <dd>The origin is the leftmost point of the
|
---|
| 564 | backbone. The backbone is a horizontal line.
|
---|
| 565 | Scale marks and labels are drawn
|
---|
| 566 | above the backbone.
|
---|
| 567 | <dt>QwtScaleDraw::BottomScale
|
---|
| 568 | <dd>The origin is the leftmost point of the
|
---|
| 569 | backbone. The backbone is a horizontal line
|
---|
| 570 | Scale marks and labels are drawn
|
---|
| 571 | below the backbone.
|
---|
| 572 | </dl>
|
---|
| 573 |
|
---|
| 574 | \param pos Origin of the scale
|
---|
| 575 |
|
---|
| 576 | \sa pos(), setLength()
|
---|
| 577 | */
|
---|
| 578 | void QwtScaleDraw::move( const QPointF &pos )
|
---|
| 579 | {
|
---|
| 580 | d_data->pos = pos;
|
---|
| 581 | updateMap();
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | /*!
|
---|
| 585 | \return Origin of the scale
|
---|
| 586 | \sa move(), length()
|
---|
| 587 | */
|
---|
| 588 | QPointF QwtScaleDraw::pos() const
|
---|
| 589 | {
|
---|
| 590 | return d_data->pos;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | /*!
|
---|
| 594 | Set the length of the backbone.
|
---|
| 595 |
|
---|
| 596 | The length doesn't include the space needed for
|
---|
| 597 | overlapping labels.
|
---|
| 598 |
|
---|
| 599 | \sa move(), minLabelDist()
|
---|
| 600 | */
|
---|
| 601 | void QwtScaleDraw::setLength( double length )
|
---|
| 602 | {
|
---|
| 603 | if ( length >= 0 && length < 10 )
|
---|
| 604 | length = 10;
|
---|
| 605 | if ( length < 0 && length > -10 )
|
---|
| 606 | length = -10;
|
---|
| 607 |
|
---|
| 608 | d_data->len = length;
|
---|
| 609 | updateMap();
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | /*!
|
---|
| 613 | \return the length of the backbone
|
---|
| 614 | \sa setLength(), pos()
|
---|
| 615 | */
|
---|
| 616 | double QwtScaleDraw::length() const
|
---|
| 617 | {
|
---|
| 618 | return d_data->len;
|
---|
| 619 | }
|
---|
| 620 |
|
---|
| 621 | /*!
|
---|
| 622 | Draws the label for a major scale tick
|
---|
| 623 |
|
---|
| 624 | \param painter Painter
|
---|
| 625 | \param value Value
|
---|
| 626 |
|
---|
| 627 | \sa drawTick(), drawBackbone(), boundingLabelRect()
|
---|
| 628 | */
|
---|
| 629 | void QwtScaleDraw::drawLabel( QPainter *painter, double value ) const
|
---|
| 630 | {
|
---|
| 631 | QwtText lbl = tickLabel( painter->font(), value );
|
---|
| 632 | if ( lbl.isEmpty() )
|
---|
| 633 | return;
|
---|
| 634 |
|
---|
| 635 | QPointF pos = labelPosition( value );
|
---|
| 636 |
|
---|
| 637 | QSizeF labelSize = lbl.textSize( painter->font() );
|
---|
| 638 |
|
---|
| 639 | const QTransform transform = labelTransformation( pos, labelSize );
|
---|
| 640 |
|
---|
| 641 | painter->save();
|
---|
| 642 | painter->setWorldTransform( transform, true );
|
---|
| 643 |
|
---|
| 644 | lbl.draw ( painter, QRect( QPoint( 0, 0 ), labelSize.toSize() ) );
|
---|
| 645 |
|
---|
| 646 | painter->restore();
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | /*!
|
---|
| 650 | Find the bounding rect for the label. The coordinates of
|
---|
| 651 | the rect are absolute coordinates ( calculated from pos() ).
|
---|
| 652 | in direction of the tick.
|
---|
| 653 |
|
---|
| 654 | \param font Font used for painting
|
---|
| 655 | \param value Value
|
---|
| 656 |
|
---|
| 657 | \sa labelRect()
|
---|
| 658 | */
|
---|
| 659 | QRect QwtScaleDraw::boundingLabelRect( const QFont &font, double value ) const
|
---|
| 660 | {
|
---|
| 661 | QwtText lbl = tickLabel( font, value );
|
---|
| 662 | if ( lbl.isEmpty() )
|
---|
| 663 | return QRect();
|
---|
| 664 |
|
---|
| 665 | const QPointF pos = labelPosition( value );
|
---|
| 666 | QSizeF labelSize = lbl.textSize( font );
|
---|
| 667 |
|
---|
| 668 | const QTransform transform = labelTransformation( pos, labelSize );
|
---|
| 669 | return transform.mapRect( QRect( QPoint( 0, 0 ), labelSize.toSize() ) );
|
---|
| 670 | }
|
---|
| 671 |
|
---|
| 672 | /*!
|
---|
| 673 | Calculate the transformation that is needed to paint a label
|
---|
| 674 | depending on its alignment and rotation.
|
---|
| 675 |
|
---|
| 676 | \param pos Position where to paint the label
|
---|
| 677 | \param size Size of the label
|
---|
| 678 |
|
---|
| 679 | \sa setLabelAlignment(), setLabelRotation()
|
---|
| 680 | */
|
---|
| 681 | QTransform QwtScaleDraw::labelTransformation(
|
---|
| 682 | const QPointF &pos, const QSizeF &size ) const
|
---|
| 683 | {
|
---|
| 684 | QTransform transform;
|
---|
| 685 | transform.translate( pos.x(), pos.y() );
|
---|
| 686 | transform.rotate( labelRotation() );
|
---|
| 687 |
|
---|
| 688 | int flags = labelAlignment();
|
---|
| 689 | if ( flags == 0 )
|
---|
| 690 | {
|
---|
| 691 | switch ( alignment() )
|
---|
| 692 | {
|
---|
| 693 | case RightScale:
|
---|
| 694 | {
|
---|
| 695 | if ( flags == 0 )
|
---|
| 696 | flags = Qt::AlignRight | Qt::AlignVCenter;
|
---|
| 697 | break;
|
---|
| 698 | }
|
---|
| 699 | case LeftScale:
|
---|
| 700 | {
|
---|
| 701 | if ( flags == 0 )
|
---|
| 702 | flags = Qt::AlignLeft | Qt::AlignVCenter;
|
---|
| 703 | break;
|
---|
| 704 | }
|
---|
| 705 | case BottomScale:
|
---|
| 706 | {
|
---|
| 707 | if ( flags == 0 )
|
---|
| 708 | flags = Qt::AlignHCenter | Qt::AlignBottom;
|
---|
| 709 | break;
|
---|
| 710 | }
|
---|
| 711 | case TopScale:
|
---|
| 712 | {
|
---|
| 713 | if ( flags == 0 )
|
---|
| 714 | flags = Qt::AlignHCenter | Qt::AlignTop;
|
---|
| 715 | break;
|
---|
| 716 | }
|
---|
| 717 | }
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 | double x, y;
|
---|
| 721 |
|
---|
| 722 | if ( flags & Qt::AlignLeft )
|
---|
| 723 | x = -size.width();
|
---|
| 724 | else if ( flags & Qt::AlignRight )
|
---|
| 725 | x = 0.0;
|
---|
| 726 | else // Qt::AlignHCenter
|
---|
| 727 | x = -( 0.5 * size.width() );
|
---|
| 728 |
|
---|
| 729 | if ( flags & Qt::AlignTop )
|
---|
| 730 | y = -size.height();
|
---|
| 731 | else if ( flags & Qt::AlignBottom )
|
---|
| 732 | y = 0;
|
---|
| 733 | else // Qt::AlignVCenter
|
---|
| 734 | y = -( 0.5 * size.height() );
|
---|
| 735 |
|
---|
| 736 | transform.translate( x, y );
|
---|
| 737 |
|
---|
| 738 | return transform;
|
---|
| 739 | }
|
---|
| 740 |
|
---|
| 741 | /*!
|
---|
| 742 | Find the bounding rect for the label. The coordinates of
|
---|
| 743 | the rect are relative to spacing + ticklength from the backbone
|
---|
| 744 | in direction of the tick.
|
---|
| 745 |
|
---|
| 746 | \param font Font used for painting
|
---|
| 747 | \param value Value
|
---|
| 748 | */
|
---|
| 749 | QRectF QwtScaleDraw::labelRect( const QFont &font, double value ) const
|
---|
| 750 | {
|
---|
| 751 | QwtText lbl = tickLabel( font, value );
|
---|
| 752 | if ( lbl.isEmpty() )
|
---|
| 753 | return QRectF( 0.0, 0.0, 0.0, 0.0 );
|
---|
| 754 |
|
---|
| 755 | const QPointF pos = labelPosition( value );
|
---|
| 756 |
|
---|
| 757 | const QSizeF labelSize = lbl.textSize( font );
|
---|
| 758 | const QTransform transform = labelTransformation( pos, labelSize );
|
---|
| 759 |
|
---|
| 760 | QRectF br = transform.mapRect( QRectF( QPointF( 0, 0 ), labelSize ) );
|
---|
| 761 | br.translate( -pos.x(), -pos.y() );
|
---|
| 762 |
|
---|
| 763 | return br;
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | /*!
|
---|
| 767 | Calculate the size that is needed to draw a label
|
---|
| 768 |
|
---|
| 769 | \param font Label font
|
---|
| 770 | \param value Value
|
---|
| 771 | */
|
---|
| 772 | QSizeF QwtScaleDraw::labelSize( const QFont &font, double value ) const
|
---|
| 773 | {
|
---|
| 774 | return labelRect( font, value ).size();
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 | /*!
|
---|
| 778 | Rotate all labels.
|
---|
| 779 |
|
---|
| 780 | When changing the rotation, it might be necessary to
|
---|
| 781 | adjust the label flags too. Finding a useful combination is
|
---|
| 782 | often the result of try and error.
|
---|
| 783 |
|
---|
| 784 | \param rotation Angle in degrees. When changing the label rotation,
|
---|
| 785 | the label flags often needs to be adjusted too.
|
---|
| 786 |
|
---|
| 787 | \sa setLabelAlignment(), labelRotation(), labelAlignment().
|
---|
| 788 |
|
---|
| 789 | */
|
---|
| 790 | void QwtScaleDraw::setLabelRotation( double rotation )
|
---|
| 791 | {
|
---|
| 792 | d_data->labelRotation = rotation;
|
---|
| 793 | }
|
---|
| 794 |
|
---|
| 795 | /*!
|
---|
| 796 | \return the label rotation
|
---|
| 797 | \sa setLabelRotation(), labelAlignment()
|
---|
| 798 | */
|
---|
| 799 | double QwtScaleDraw::labelRotation() const
|
---|
| 800 | {
|
---|
| 801 | return d_data->labelRotation;
|
---|
| 802 | }
|
---|
| 803 |
|
---|
| 804 | /*!
|
---|
| 805 | \brief Change the label flags
|
---|
| 806 |
|
---|
| 807 | Labels are aligned to the point ticklength + spacing away from the backbone.
|
---|
| 808 |
|
---|
| 809 | The alignment is relative to the orientation of the label text.
|
---|
| 810 | In case of an flags of 0 the label will be aligned
|
---|
| 811 | depending on the orientation of the scale:
|
---|
| 812 |
|
---|
| 813 | QwtScaleDraw::TopScale: Qt::AlignHCenter | Qt::AlignTop\n
|
---|
| 814 | QwtScaleDraw::BottomScale: Qt::AlignHCenter | Qt::AlignBottom\n
|
---|
| 815 | QwtScaleDraw::LeftScale: Qt::AlignLeft | Qt::AlignVCenter\n
|
---|
| 816 | QwtScaleDraw::RightScale: Qt::AlignRight | Qt::AlignVCenter\n
|
---|
| 817 |
|
---|
| 818 | Changing the alignment is often necessary for rotated labels.
|
---|
| 819 |
|
---|
| 820 | \param alignment Or'd Qt::AlignmentFlags see <qnamespace.h>
|
---|
| 821 |
|
---|
| 822 | \sa setLabelRotation(), labelRotation(), labelAlignment()
|
---|
| 823 | \warning The various alignments might be confusing.
|
---|
| 824 | The alignment of the label is not the alignment
|
---|
| 825 | of the scale and is not the alignment of the flags
|
---|
| 826 | (QwtText::flags()) returned from QwtAbstractScaleDraw::label().
|
---|
| 827 | */
|
---|
| 828 |
|
---|
| 829 | void QwtScaleDraw::setLabelAlignment( Qt::Alignment alignment )
|
---|
| 830 | {
|
---|
| 831 | d_data->labelAlignment = alignment;
|
---|
| 832 | }
|
---|
| 833 |
|
---|
| 834 | /*!
|
---|
| 835 | \return the label flags
|
---|
| 836 | \sa setLabelAlignment(), labelRotation()
|
---|
| 837 | */
|
---|
| 838 | Qt::Alignment QwtScaleDraw::labelAlignment() const
|
---|
| 839 | {
|
---|
| 840 | return d_data->labelAlignment;
|
---|
| 841 | }
|
---|
| 842 |
|
---|
| 843 | /*!
|
---|
| 844 | \param font Font
|
---|
| 845 | \return the maximum width of a label
|
---|
| 846 | */
|
---|
| 847 | int QwtScaleDraw::maxLabelWidth( const QFont &font ) const
|
---|
| 848 | {
|
---|
| 849 | int maxWidth = 0;
|
---|
| 850 |
|
---|
| 851 | const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
|
---|
| 852 | for ( int i = 0; i < ticks.count(); i++ )
|
---|
| 853 | {
|
---|
| 854 | const double v = ticks[i];
|
---|
| 855 | if ( scaleDiv().contains( v ) )
|
---|
| 856 | {
|
---|
| 857 | const int w = labelSize( font, ticks[i] ).width();
|
---|
| 858 | if ( w > maxWidth )
|
---|
| 859 | maxWidth = w;
|
---|
| 860 | }
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 | return maxWidth;
|
---|
| 864 | }
|
---|
| 865 |
|
---|
| 866 | /*!
|
---|
| 867 | \param font Font
|
---|
| 868 | \return the maximum height of a label
|
---|
| 869 | */
|
---|
| 870 | int QwtScaleDraw::maxLabelHeight( const QFont &font ) const
|
---|
| 871 | {
|
---|
| 872 | int maxHeight = 0;
|
---|
| 873 |
|
---|
| 874 | const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
|
---|
| 875 | for ( int i = 0; i < ticks.count(); i++ )
|
---|
| 876 | {
|
---|
| 877 | const double v = ticks[i];
|
---|
| 878 | if ( scaleDiv().contains( v ) )
|
---|
| 879 | {
|
---|
| 880 | const int h = labelSize( font, ticks[i] ).height();
|
---|
| 881 | if ( h > maxHeight )
|
---|
| 882 | maxHeight = h;
|
---|
| 883 | }
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 | return maxHeight;
|
---|
| 887 | }
|
---|
| 888 |
|
---|
| 889 | void QwtScaleDraw::updateMap()
|
---|
| 890 | {
|
---|
| 891 | const QPointF pos = d_data->pos;
|
---|
| 892 | double len = d_data->len;
|
---|
| 893 |
|
---|
| 894 | QwtScaleMap &sm = scaleMap();
|
---|
| 895 | if ( orientation() == Qt::Vertical )
|
---|
| 896 | sm.setPaintInterval( pos.y() + len, pos.y() );
|
---|
| 897 | else
|
---|
| 898 | sm.setPaintInterval( pos.x(), pos.x() + len );
|
---|
| 899 | }
|
---|