qwt_plot_axis.cpp 17.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
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_plot.h"
#include "qwt_math.h"
#include "qwt_scale_widget.h"
#include "qwt_scale_div.h"
#include "qwt_scale_engine.h"

class QwtPlot::AxisData
{
public:
    bool isEnabled;
    bool doAutoScale;

    double minValue;
    double maxValue;
    double stepSize;

    int maxMajor;
    int maxMinor;

Bryant's avatar
Bryant committed
29 30
    bool isValid;

pixhawk's avatar
pixhawk committed
31 32 33 34 35 36 37 38 39 40
    QwtScaleDiv scaleDiv;
    QwtScaleEngine *scaleEngine;
    QwtScaleWidget *scaleWidget;
};

//! Initialize axes
void QwtPlot::initAxesData()
{
    int axisId;

Bryant's avatar
Bryant committed
41
    for ( axisId = 0; axisId < axisCnt; axisId++ )
pixhawk's avatar
pixhawk committed
42 43
        d_axisData[axisId] = new AxisData;

44
    d_axisData[yLeft]->scaleWidget =
Bryant's avatar
Bryant committed
45
        new QwtScaleWidget( QwtScaleDraw::LeftScale, this );
46
    d_axisData[yRight]->scaleWidget =
Bryant's avatar
Bryant committed
47
        new QwtScaleWidget( QwtScaleDraw::RightScale, this );
48
    d_axisData[xTop]->scaleWidget =
Bryant's avatar
Bryant committed
49
        new QwtScaleWidget( QwtScaleDraw::TopScale, this );
50
    d_axisData[xBottom]->scaleWidget =
Bryant's avatar
Bryant committed
51
        new QwtScaleWidget( QwtScaleDraw::BottomScale, this );
pixhawk's avatar
pixhawk committed
52

Bryant's avatar
Bryant committed
53 54 55 56
    d_axisData[yLeft]->scaleWidget->setObjectName( "QwtPlotAxisYLeft" );
    d_axisData[yRight]->scaleWidget->setObjectName( "QwtPlotAxisYRight" );
    d_axisData[xTop]->scaleWidget->setObjectName( "QwtPlotAxisXTop" );
    d_axisData[xBottom]->scaleWidget->setObjectName( "QwtPlotAxisXBottom" );
pixhawk's avatar
pixhawk committed
57

Bryant's avatar
Bryant committed
58 59 60 61 62
#if 1
    // better find the font sizes from the application font
    QFont fscl( fontInfo().family(), 10 );
    QFont fttl( fontInfo().family(), 12, QFont::Bold );
#endif
pixhawk's avatar
pixhawk committed
63

Bryant's avatar
Bryant committed
64 65
    for ( axisId = 0; axisId < axisCnt; axisId++ )
    {
pixhawk's avatar
pixhawk committed
66 67
        AxisData &d = *d_axisData[axisId];

Bryant's avatar
Bryant committed
68 69 70 71 72 73 74
        d.scaleEngine = new QwtLinearScaleEngine;

        d.scaleWidget->setTransformation( 
            d.scaleEngine->transformation() );

        d.scaleWidget->setFont( fscl );
        d.scaleWidget->setMargin( 2 );
pixhawk's avatar
pixhawk committed
75 76

        QwtText text = d.scaleWidget->title();
Bryant's avatar
Bryant committed
77 78
        text.setFont( fttl );
        d.scaleWidget->setTitle( text );
pixhawk's avatar
pixhawk committed
79 80 81 82 83 84 85 86 87 88 89

        d.doAutoScale = true;

        d.minValue = 0.0;
        d.maxValue = 1000.0;
        d.stepSize = 0.0;

        d.maxMinor = 5;
        d.maxMajor = 8;


Bryant's avatar
Bryant committed
90
        d.isValid = false;
pixhawk's avatar
pixhawk committed
91 92 93 94 95 96 97 98 99 100
    }

    d_axisData[yLeft]->isEnabled = true;
    d_axisData[yRight]->isEnabled = false;
    d_axisData[xBottom]->isEnabled = true;
    d_axisData[xTop]->isEnabled = false;
}

void QwtPlot::deleteAxesData()
{
Bryant's avatar
Bryant committed
101 102
    for ( int axisId = 0; axisId < axisCnt; axisId++ )
    {
pixhawk's avatar
pixhawk committed
103 104 105 106 107 108 109
        delete d_axisData[axisId]->scaleEngine;
        delete d_axisData[axisId];
        d_axisData[axisId] = NULL;
    }
}

/*!
Bryant's avatar
Bryant committed
110 111
  \return Scale widget of the specified axis, or NULL if axisId is invalid.
  \param axisId Axis index
pixhawk's avatar
pixhawk committed
112
*/
Bryant's avatar
Bryant committed
113
const QwtScaleWidget *QwtPlot::axisWidget( int axisId ) const
pixhawk's avatar
pixhawk committed
114
{
Bryant's avatar
Bryant committed
115
    if ( axisValid( axisId ) )
pixhawk's avatar
pixhawk committed
116 117 118 119 120 121
        return d_axisData[axisId]->scaleWidget;

    return NULL;
}

/*!
Bryant's avatar
Bryant committed
122 123
  \return Scale widget of the specified axis, or NULL if axisId is invalid.
  \param axisId Axis index
pixhawk's avatar
pixhawk committed
124
*/
Bryant's avatar
Bryant committed
125
QwtScaleWidget *QwtPlot::axisWidget( int axisId )
pixhawk's avatar
pixhawk committed
126
{
Bryant's avatar
Bryant committed
127
    if ( axisValid( axisId ) )
pixhawk's avatar
pixhawk committed
128 129 130 131 132 133
        return d_axisData[axisId]->scaleWidget;

    return NULL;
}

/*!
Bryant's avatar
Bryant committed
134
  Change the scale engine for an axis
pixhawk's avatar
pixhawk committed
135

Bryant's avatar
Bryant committed
136
  \param axisId Axis index
pixhawk's avatar
pixhawk committed
137 138 139 140
  \param scaleEngine Scale engine

  \sa axisScaleEngine()
*/
Bryant's avatar
Bryant committed
141
void QwtPlot::setAxisScaleEngine( int axisId, QwtScaleEngine *scaleEngine )
pixhawk's avatar
pixhawk committed
142
{
Bryant's avatar
Bryant committed
143 144
    if ( axisValid( axisId ) && scaleEngine != NULL )
    {
pixhawk's avatar
pixhawk committed
145 146 147 148 149
        AxisData &d = *d_axisData[axisId];

        delete d.scaleEngine;
        d.scaleEngine = scaleEngine;

Bryant's avatar
Bryant committed
150 151 152 153
        d_axisData[axisId]->scaleWidget->setTransformation( 
            scaleEngine->transformation() );

        d.isValid = false;
pixhawk's avatar
pixhawk committed
154 155 156 157 158

        autoRefresh();
    }
}

Bryant's avatar
Bryant committed
159 160 161 162 163
/*!
  \param axisId Axis index
  \return Scale engine for a specific axis
*/
QwtScaleEngine *QwtPlot::axisScaleEngine( int axisId )
pixhawk's avatar
pixhawk committed
164
{
Bryant's avatar
Bryant committed
165
    if ( axisValid( axisId ) )
pixhawk's avatar
pixhawk committed
166 167 168 169 170
        return d_axisData[axisId]->scaleEngine;
    else
        return NULL;
}

Bryant's avatar
Bryant committed
171 172 173 174 175
/*!
  \param axisId Axis index
  \return Scale engine for a specific axis
*/
const QwtScaleEngine *QwtPlot::axisScaleEngine( int axisId ) const
pixhawk's avatar
pixhawk committed
176
{
Bryant's avatar
Bryant committed
177
    if ( axisValid( axisId ) )
pixhawk's avatar
pixhawk committed
178 179 180 181 182
        return d_axisData[axisId]->scaleEngine;
    else
        return NULL;
}
/*!
Bryant's avatar
Bryant committed
183 184
  \return \c True, if autoscaling is enabled
  \param axisId Axis index
pixhawk's avatar
pixhawk committed
185
*/
Bryant's avatar
Bryant committed
186
bool QwtPlot::axisAutoScale( int axisId ) const
pixhawk's avatar
pixhawk committed
187
{
Bryant's avatar
Bryant committed
188
    if ( axisValid( axisId ) )
pixhawk's avatar
pixhawk committed
189 190 191
        return d_axisData[axisId]->doAutoScale;
    else
        return false;
192

pixhawk's avatar
pixhawk committed
193 194 195
}

/*!
Bryant's avatar
Bryant committed
196 197
  \return \c True, if a specified axis is enabled
  \param axisId Axis index
pixhawk's avatar
pixhawk committed
198
*/
Bryant's avatar
Bryant committed
199
bool QwtPlot::axisEnabled( int axisId ) const
pixhawk's avatar
pixhawk committed
200
{
Bryant's avatar
Bryant committed
201
    if ( axisValid( axisId ) )
pixhawk's avatar
pixhawk committed
202 203 204 205 206 207
        return d_axisData[axisId]->isEnabled;
    else
        return false;
}

/*!
Bryant's avatar
Bryant committed
208 209
  \return The font of the scale labels for a specified axis
  \param axisId Axis index
pixhawk's avatar
pixhawk committed
210
*/
Bryant's avatar
Bryant committed
211
QFont QwtPlot::axisFont( int axisId ) const
pixhawk's avatar
pixhawk committed
212
{
Bryant's avatar
Bryant committed
213 214
    if ( axisValid( axisId ) )
        return axisWidget( axisId )->font();
pixhawk's avatar
pixhawk committed
215 216
    else
        return QFont();
217

pixhawk's avatar
pixhawk committed
218 219 220
}

/*!
Bryant's avatar
Bryant committed
221 222 223
  \return The maximum number of major ticks for a specified axis
  \param axisId Axis index
  \sa setAxisMaxMajor(), QwtScaleEngine::divideScale()
pixhawk's avatar
pixhawk committed
224
*/
Bryant's avatar
Bryant committed
225
int QwtPlot::axisMaxMajor( int axisId ) const
pixhawk's avatar
pixhawk committed
226
{
Bryant's avatar
Bryant committed
227
    if ( axisValid( axisId ) )
pixhawk's avatar
pixhawk committed
228 229 230 231 232 233 234
        return d_axisData[axisId]->maxMajor;
    else
        return 0;
}

/*!
  \return the maximum number of minor ticks for a specified axis
Bryant's avatar
Bryant committed
235 236
  \param axisId Axis index
  \sa setAxisMaxMinor(), QwtScaleEngine::divideScale()
pixhawk's avatar
pixhawk committed
237
*/
Bryant's avatar
Bryant committed
238
int QwtPlot::axisMaxMinor( int axisId ) const
pixhawk's avatar
pixhawk committed
239
{
Bryant's avatar
Bryant committed
240
    if ( axisValid( axisId ) )
pixhawk's avatar
pixhawk committed
241 242 243 244 245 246 247 248
        return d_axisData[axisId]->maxMinor;
    else
        return 0;
}

/*!
  \brief Return the scale division of a specified axis

Bryant's avatar
Bryant committed
249
  axisScaleDiv(axisId).lowerBound(), axisScaleDiv(axisId).upperBound()
pixhawk's avatar
pixhawk committed
250 251
  are the current limits of the axis scale.

Bryant's avatar
Bryant committed
252
  \param axisId Axis index
253
  \return Scale division
pixhawk's avatar
pixhawk committed
254

Bryant's avatar
Bryant committed
255
  \sa QwtScaleDiv, setAxisScaleDiv(), QwtScaleEngine::divideScale()
pixhawk's avatar
pixhawk committed
256
*/
Bryant's avatar
Bryant committed
257
const QwtScaleDiv &QwtPlot::axisScaleDiv( int axisId ) const
pixhawk's avatar
pixhawk committed
258
{
Bryant's avatar
Bryant committed
259
    return d_axisData[axisId]->scaleDiv;
pixhawk's avatar
pixhawk committed
260 261 262
}

/*!
Bryant's avatar
Bryant committed
263
  \brief Return the scale draw of a specified axis
pixhawk's avatar
pixhawk committed
264

Bryant's avatar
Bryant committed
265 266
  \param axisId Axis index
  \return Specified scaleDraw for axis, or NULL if axis is invalid.
pixhawk's avatar
pixhawk committed
267
*/
Bryant's avatar
Bryant committed
268
const QwtScaleDraw *QwtPlot::axisScaleDraw( int axisId ) const
pixhawk's avatar
pixhawk committed
269
{
Bryant's avatar
Bryant committed
270
    if ( !axisValid( axisId ) )
pixhawk's avatar
pixhawk committed
271 272
        return NULL;

Bryant's avatar
Bryant committed
273
    return axisWidget( axisId )->scaleDraw();
pixhawk's avatar
pixhawk committed
274 275 276
}

/*!
Bryant's avatar
Bryant committed
277 278 279 280
  \brief Return the scale draw of a specified axis

  \param axisId Axis index
  \return Specified scaleDraw for axis, or NULL if axis is invalid.
pixhawk's avatar
pixhawk committed
281
*/
Bryant's avatar
Bryant committed
282
QwtScaleDraw *QwtPlot::axisScaleDraw( int axisId )
pixhawk's avatar
pixhawk committed
283
{
Bryant's avatar
Bryant committed
284
    if ( !axisValid( axisId ) )
pixhawk's avatar
pixhawk committed
285 286
        return NULL;

Bryant's avatar
Bryant committed
287
    return axisWidget( axisId )->scaleDraw();
pixhawk's avatar
pixhawk committed
288 289 290
}

/*!
Bryant's avatar
Bryant committed
291 292 293 294 295 296 297 298
  \brief Return the step size parameter that has been set in setAxisScale. 

  This doesn't need to be the step size of the current scale.

  \param axisId Axis index
  \return step size parameter value

   \sa setAxisScale(), QwtScaleEngine::divideScale()
pixhawk's avatar
pixhawk committed
299
*/
Bryant's avatar
Bryant committed
300
double QwtPlot::axisStepSize( int axisId ) const
pixhawk's avatar
pixhawk committed
301
{
Bryant's avatar
Bryant committed
302 303
    if ( !axisValid( axisId ) )
        return 0;
pixhawk's avatar
pixhawk committed
304

Bryant's avatar
Bryant committed
305
    return d_axisData[axisId]->stepSize;
pixhawk's avatar
pixhawk committed
306 307 308
}

/*!
Bryant's avatar
Bryant committed
309
  \brief Return the current interval of the specified axis
pixhawk's avatar
pixhawk committed
310

Bryant's avatar
Bryant committed
311 312 313 314
  This is only a convenience function for axisScaleDiv( axisId )->interval();
  
  \param axisId Axis index
  \return Scale interval
pixhawk's avatar
pixhawk committed
315

Bryant's avatar
Bryant committed
316
  \sa QwtScaleDiv, axisScaleDiv()
317
*/
Bryant's avatar
Bryant committed
318
QwtInterval QwtPlot::axisInterval( int axisId ) const
pixhawk's avatar
pixhawk committed
319
{
Bryant's avatar
Bryant committed
320 321
    if ( !axisValid( axisId ) )
        return QwtInterval();
pixhawk's avatar
pixhawk committed
322

Bryant's avatar
Bryant committed
323
    return d_axisData[axisId]->scaleDiv.interval();
pixhawk's avatar
pixhawk committed
324 325 326
}

/*!
Bryant's avatar
Bryant committed
327 328
  \return Title of a specified axis
  \param axisId Axis index
pixhawk's avatar
pixhawk committed
329
*/
Bryant's avatar
Bryant committed
330
QwtText QwtPlot::axisTitle( int axisId ) const
pixhawk's avatar
pixhawk committed
331
{
Bryant's avatar
Bryant committed
332 333
    if ( axisValid( axisId ) )
        return axisWidget( axisId )->title();
pixhawk's avatar
pixhawk committed
334 335 336 337 338 339 340 341 342 343 344 345 346
    else
        return QwtText();
}

/*!
  \brief Enable or disable a specified axis

  When an axis is disabled, this only means that it is not
  visible on the screen. Curves, markers and can be attached
  to disabled axes, and transformation of screen coordinates
  into values works as normal.

  Only xBottom and yLeft are enabled by default.
Bryant's avatar
Bryant committed
347 348

  \param axisId Axis index
pixhawk's avatar
pixhawk committed
349 350
  \param tf \c true (enabled) or \c false (disabled)
*/
Bryant's avatar
Bryant committed
351
void QwtPlot::enableAxis( int axisId, bool tf )
pixhawk's avatar
pixhawk committed
352
{
Bryant's avatar
Bryant committed
353 354
    if ( axisValid( axisId ) && tf != d_axisData[axisId]->isEnabled )
    {
pixhawk's avatar
pixhawk committed
355 356 357 358 359 360 361 362
        d_axisData[axisId]->isEnabled = tf;
        updateLayout();
    }
}

/*!
  Transform the x or y coordinate of a position in the
  drawing region into a value.
Bryant's avatar
Bryant committed
363 364

  \param axisId Axis index
pixhawk's avatar
pixhawk committed
365
  \param pos position
Bryant's avatar
Bryant committed
366 367 368

  \return Position as axis coordinate

pixhawk's avatar
pixhawk committed
369 370 371
  \warning The position can be an x or a y coordinate,
           depending on the specified axis.
*/
Bryant's avatar
Bryant committed
372
double QwtPlot::invTransform( int axisId, int pos ) const
pixhawk's avatar
pixhawk committed
373
{
Bryant's avatar
Bryant committed
374 375
    if ( axisValid( axisId ) )
        return( canvasMap( axisId ).invTransform( pos ) );
pixhawk's avatar
pixhawk committed
376
    else
377
        return 0.0;
pixhawk's avatar
pixhawk committed
378 379 380 381 382
}


/*!
  \brief Transform a value into a coordinate in the plotting region
Bryant's avatar
Bryant committed
383 384

  \param axisId Axis index
pixhawk's avatar
pixhawk committed
385
  \param value value
Bryant's avatar
Bryant committed
386
  \return X or Y coordinate in the plotting region corresponding
pixhawk's avatar
pixhawk committed
387 388
          to the value.
*/
Bryant's avatar
Bryant committed
389
double QwtPlot::transform( int axisId, double value ) const
pixhawk's avatar
pixhawk committed
390
{
Bryant's avatar
Bryant committed
391 392
    if ( axisValid( axisId ) )
        return( canvasMap( axisId ).transform( value ) );
pixhawk's avatar
pixhawk committed
393
    else
Bryant's avatar
Bryant committed
394
        return 0.0;
pixhawk's avatar
pixhawk committed
395 396 397 398
}

/*!
  \brief Change the font of an axis
Bryant's avatar
Bryant committed
399 400 401

  \param axisId Axis index
  \param font Font
pixhawk's avatar
pixhawk committed
402 403 404
  \warning This function changes the font of the tick labels,
           not of the axis title.
*/
Bryant's avatar
Bryant committed
405
void QwtPlot::setAxisFont( int axisId, const QFont &font )
pixhawk's avatar
pixhawk committed
406
{
Bryant's avatar
Bryant committed
407 408
    if ( axisValid( axisId ) )
        axisWidget( axisId )->setFont( font );
pixhawk's avatar
pixhawk committed
409 410 411 412 413 414 415 416
}

/*!
  \brief Enable autoscaling for a specified axis

  This member function is used to switch back to autoscaling mode
  after a fixed scale has been set. Autoscaling is enabled by default.

Bryant's avatar
Bryant committed
417 418 419 420 421 422
  \param axisId Axis index
  \param on On/Off
  \sa setAxisScale(), setAxisScaleDiv(), updateAxes()

  \note The autoscaling flag has no effect until updateAxes() is executed
        ( called by replot() ).
pixhawk's avatar
pixhawk committed
423
*/
Bryant's avatar
Bryant committed
424
void QwtPlot::setAxisAutoScale( int axisId, bool on )
pixhawk's avatar
pixhawk committed
425
{
Bryant's avatar
Bryant committed
426 427 428
    if ( axisValid( axisId ) && ( d_axisData[axisId]->doAutoScale != on ) )
    {
        d_axisData[axisId]->doAutoScale = on;
pixhawk's avatar
pixhawk committed
429 430 431 432 433 434
        autoRefresh();
    }
}

/*!
  \brief Disable autoscaling and specify a fixed scale for a selected axis.
Bryant's avatar
Bryant committed
435 436 437 438 439 440 441 442

  In updateAxes() the scale engine calculates a scale division from the 
  specified parameters, that will be assigned to the scale widget. So 
  updates of the scale widget usually happen delayed with the next replot.

  \param axisId Axis index
  \param min Minimum of the scale
  \param max Maximum of the scale
pixhawk's avatar
pixhawk committed
443
  \param stepSize Major step size. If <code>step == 0</code>, the step size is
Bryant's avatar
Bryant committed
444 445 446
                  calculated automatically using the maxMajor setting.

  \sa setAxisMaxMajor(), setAxisAutoScale(), axisStepSize(), QwtScaleEngine::divideScale()
pixhawk's avatar
pixhawk committed
447
*/
Bryant's avatar
Bryant committed
448
void QwtPlot::setAxisScale( int axisId, double min, double max, double stepSize )
pixhawk's avatar
pixhawk committed
449
{
Bryant's avatar
Bryant committed
450 451
    if ( axisValid( axisId ) )
    {
pixhawk's avatar
pixhawk committed
452 453 454
        AxisData &d = *d_axisData[axisId];

        d.doAutoScale = false;
Bryant's avatar
Bryant committed
455
        d.isValid = false;
pixhawk's avatar
pixhawk committed
456 457 458 459

        d.minValue = min;
        d.maxValue = max;
        d.stepSize = stepSize;
460

pixhawk's avatar
pixhawk committed
461 462 463 464 465 466
        autoRefresh();
    }
}

/*!
  \brief Disable autoscaling and specify a fixed scale for a selected axis.
Bryant's avatar
Bryant committed
467 468 469 470 471 472

  The scale division will be stored locally only until the next call
  of updateAxes(). So updates of the scale widget usually happen delayed with 
  the next replot.

  \param axisId Axis index
pixhawk's avatar
pixhawk committed
473
  \param scaleDiv Scale division
Bryant's avatar
Bryant committed
474

pixhawk's avatar
pixhawk committed
475 476
  \sa setAxisScale(), setAxisAutoScale()
*/
Bryant's avatar
Bryant committed
477
void QwtPlot::setAxisScaleDiv( int axisId, const QwtScaleDiv &scaleDiv )
pixhawk's avatar
pixhawk committed
478
{
Bryant's avatar
Bryant committed
479 480
    if ( axisValid( axisId ) )
    {
pixhawk's avatar
pixhawk committed
481 482 483 484
        AxisData &d = *d_axisData[axisId];

        d.doAutoScale = false;
        d.scaleDiv = scaleDiv;
Bryant's avatar
Bryant committed
485
        d.isValid = true;
pixhawk's avatar
pixhawk committed
486 487 488 489 490 491 492

        autoRefresh();
    }
}

/*!
  \brief Set a scale draw
Bryant's avatar
Bryant committed
493 494 495

  \param axisId Axis index
  \param scaleDraw Object responsible for drawing scales.
pixhawk's avatar
pixhawk committed
496 497 498 499 500 501 502

  By passing scaleDraw it is possible to extend QwtScaleDraw
  functionality and let it take place in QwtPlot. Please note
  that scaleDraw has to be created with new and will be deleted
  by the corresponding QwtScale member ( like a child object ).

  \sa QwtScaleDraw, QwtScaleWidget
503 504
  \warning The attributes of scaleDraw will be overwritten by those of the
           previous QwtScaleDraw.
pixhawk's avatar
pixhawk committed
505 506
*/

Bryant's avatar
Bryant committed
507
void QwtPlot::setAxisScaleDraw( int axisId, QwtScaleDraw *scaleDraw )
pixhawk's avatar
pixhawk committed
508
{
Bryant's avatar
Bryant committed
509 510 511
    if ( axisValid( axisId ) )
    {
        axisWidget( axisId )->setScaleDraw( scaleDraw );
pixhawk's avatar
pixhawk committed
512 513 514 515 516 517
        autoRefresh();
    }
}

/*!
  Change the alignment of the tick labels
Bryant's avatar
Bryant committed
518 519 520 521

  \param axisId Axis index
  \param alignment Or'd Qt::AlignmentFlags see <qnamespace.h>

pixhawk's avatar
pixhawk committed
522 523
  \sa QwtScaleDraw::setLabelAlignment()
*/
Bryant's avatar
Bryant committed
524
void QwtPlot::setAxisLabelAlignment( int axisId, Qt::Alignment alignment )
pixhawk's avatar
pixhawk committed
525
{
Bryant's avatar
Bryant committed
526 527
    if ( axisValid( axisId ) )
        axisWidget( axisId )->setLabelAlignment( alignment );
pixhawk's avatar
pixhawk committed
528 529 530 531
}

/*!
  Rotate all tick labels
Bryant's avatar
Bryant committed
532 533

  \param axisId Axis index
pixhawk's avatar
pixhawk committed
534 535
  \param rotation Angle in degrees. When changing the label rotation,
                  the label alignment might be adjusted too.
Bryant's avatar
Bryant committed
536 537

  \sa QwtScaleDraw::setLabelRotation(), setAxisLabelAlignment()
pixhawk's avatar
pixhawk committed
538
*/
Bryant's avatar
Bryant committed
539
void QwtPlot::setAxisLabelRotation( int axisId, double rotation )
pixhawk's avatar
pixhawk committed
540
{
Bryant's avatar
Bryant committed
541 542
    if ( axisValid( axisId ) )
        axisWidget( axisId )->setLabelRotation( rotation );
pixhawk's avatar
pixhawk committed
543 544 545 546 547
}

/*!
  Set the maximum number of minor scale intervals for a specified axis

Bryant's avatar
Bryant committed
548 549 550
  \param axisId Axis index
  \param maxMinor Maximum number of minor steps

pixhawk's avatar
pixhawk committed
551 552
  \sa axisMaxMinor()
*/
Bryant's avatar
Bryant committed
553
void QwtPlot::setAxisMaxMinor( int axisId, int maxMinor )
pixhawk's avatar
pixhawk committed
554
{
Bryant's avatar
Bryant committed
555 556 557
    if ( axisValid( axisId ) )
    {
        maxMinor = qBound( 0, maxMinor, 100 );
558

pixhawk's avatar
pixhawk committed
559
        AxisData &d = *d_axisData[axisId];
Bryant's avatar
Bryant committed
560 561
        if ( maxMinor != d.maxMinor )
        {
pixhawk's avatar
pixhawk committed
562
            d.maxMinor = maxMinor;
Bryant's avatar
Bryant committed
563
            d.isValid = false;
pixhawk's avatar
pixhawk committed
564 565 566 567 568 569 570 571
            autoRefresh();
        }
    }
}

/*!
  Set the maximum number of major scale intervals for a specified axis

Bryant's avatar
Bryant committed
572 573 574
  \param axisId Axis index
  \param maxMajor Maximum number of major steps

pixhawk's avatar
pixhawk committed
575 576
  \sa axisMaxMajor()
*/
Bryant's avatar
Bryant committed
577
void QwtPlot::setAxisMaxMajor( int axisId, int maxMajor )
pixhawk's avatar
pixhawk committed
578
{
Bryant's avatar
Bryant committed
579 580 581
    if ( axisValid( axisId ) )
    {
        maxMajor = qBound( 1, maxMajor, 10000 );
582

pixhawk's avatar
pixhawk committed
583
        AxisData &d = *d_axisData[axisId];
Bryant's avatar
Bryant committed
584 585
        if ( maxMajor != d.maxMajor )
        {
pixhawk's avatar
pixhawk committed
586
            d.maxMajor = maxMajor;
Bryant's avatar
Bryant committed
587
            d.isValid = false;
pixhawk's avatar
pixhawk committed
588 589 590 591 592 593 594
            autoRefresh();
        }
    }
}

/*!
  \brief Change the title of a specified axis
Bryant's avatar
Bryant committed
595 596

  \param axisId Axis index
pixhawk's avatar
pixhawk committed
597 598
  \param title axis title
*/
Bryant's avatar
Bryant committed
599
void QwtPlot::setAxisTitle( int axisId, const QString &title )
pixhawk's avatar
pixhawk committed
600
{
Bryant's avatar
Bryant committed
601 602
    if ( axisValid( axisId ) )
        axisWidget( axisId )->setTitle( title );
pixhawk's avatar
pixhawk committed
603 604 605 606
}

/*!
  \brief Change the title of a specified axis
Bryant's avatar
Bryant committed
607 608 609

  \param axisId Axis index
  \param title Axis title
pixhawk's avatar
pixhawk committed
610
*/
Bryant's avatar
Bryant committed
611
void QwtPlot::setAxisTitle( int axisId, const QwtText &title )
pixhawk's avatar
pixhawk committed
612
{
Bryant's avatar
Bryant committed
613 614
    if ( axisValid( axisId ) )
        axisWidget( axisId )->setTitle( title );
pixhawk's avatar
pixhawk committed
615 616
}

Bryant's avatar
Bryant committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
/*! 
  \brief Rebuild the axes scales

  In case of autoscaling the boundaries of a scale are calculated 
  from the bounding rectangles of all plot items, having the 
  QwtPlotItem::AutoScale flag enabled ( QwtScaleEngine::autoScale() ). 
  Then a scale division is calculated ( QwtScaleEngine::didvideScale() ) 
  and assigned to scale widget.

  When the scale boundaries have been assigned with setAxisScale() a 
  scale division is calculated ( QwtScaleEngine::didvideScale() )
  for this interval and assigned to the scale widget.

  When the scale has been set explicitly by setAxisScaleDiv() the 
  locally stored scale division gets assigned to the scale widget.

  The scale widget indicates modifications by emitting a 
  QwtScaleWidget::scaleDivChanged() signal.

  updateAxes() is usually called by replot(). 

  \sa setAxisAutoScale(), setAxisScale(), setAxisScaleDiv(), replot()
      QwtPlotItem::boundingRect()
 */
641
void QwtPlot::updateAxes()
pixhawk's avatar
pixhawk committed
642 643 644
{
    // Find bounding interval of the item data
    // for all axes, where autoscaling is enabled
645

Bryant's avatar
Bryant committed
646
    QwtInterval intv[axisCnt];
pixhawk's avatar
pixhawk committed
647 648 649 650

    const QwtPlotItemList& itmList = itemList();

    QwtPlotItemIterator it;
Bryant's avatar
Bryant committed
651 652
    for ( it = itmList.begin(); it != itmList.end(); ++it )
    {
pixhawk's avatar
pixhawk committed
653 654
        const QwtPlotItem *item = *it;

Bryant's avatar
Bryant committed
655 656 657 658
        if ( !item->testItemAttribute( QwtPlotItem::AutoScale ) )
            continue;

        if ( !item->isVisible() )
pixhawk's avatar
pixhawk committed
659 660
            continue;

Bryant's avatar
Bryant committed
661 662 663 664 665 666 667 668 669
        if ( axisAutoScale( item->xAxis() ) || axisAutoScale( item->yAxis() ) )
        {
            const QRectF rect = item->boundingRect();

            if ( rect.width() >= 0.0 )
                intv[item->xAxis()] |= QwtInterval( rect.left(), rect.right() );

            if ( rect.height() >= 0.0 )
                intv[item->yAxis()] |= QwtInterval( rect.top(), rect.bottom() );
pixhawk's avatar
pixhawk committed
670 671 672 673 674
        }
    }

    // Adjust scales

Bryant's avatar
Bryant committed
675 676
    for ( int axisId = 0; axisId < axisCnt; axisId++ )
    {
pixhawk's avatar
pixhawk committed
677 678 679 680 681 682
        AxisData &d = *d_axisData[axisId];

        double minValue = d.minValue;
        double maxValue = d.maxValue;
        double stepSize = d.stepSize;

Bryant's avatar
Bryant committed
683 684 685
        if ( d.doAutoScale && intv[axisId].isValid() )
        {
            d.isValid = false;
pixhawk's avatar
pixhawk committed
686 687 688 689

            minValue = intv[axisId].minValue();
            maxValue = intv[axisId].maxValue();

Bryant's avatar
Bryant committed
690 691
            d.scaleEngine->autoScale( d.maxMajor,
                minValue, maxValue, stepSize );
pixhawk's avatar
pixhawk committed
692
        }
Bryant's avatar
Bryant committed
693 694
        if ( !d.isValid )
        {
pixhawk's avatar
pixhawk committed
695
            d.scaleDiv = d.scaleEngine->divideScale(
Bryant's avatar
Bryant committed
696 697 698
                minValue, maxValue,
                d.maxMajor, d.maxMinor, stepSize );
            d.isValid = true;
pixhawk's avatar
pixhawk committed
699 700
        }

Bryant's avatar
Bryant committed
701 702
        QwtScaleWidget *scaleWidget = axisWidget( axisId );
        scaleWidget->setScaleDiv( d.scaleDiv );
pixhawk's avatar
pixhawk committed
703 704

        int startDist, endDist;
Bryant's avatar
Bryant committed
705 706
        scaleWidget->getBorderDistHint( startDist, endDist );
        scaleWidget->setBorderDist( startDist, endDist );
pixhawk's avatar
pixhawk committed
707 708
    }

Bryant's avatar
Bryant committed
709 710
    for ( it = itmList.begin(); it != itmList.end(); ++it )
    {
pixhawk's avatar
pixhawk committed
711
        QwtPlotItem *item = *it;
Bryant's avatar
Bryant committed
712 713 714 715 716
        if ( item->testItemInterest( QwtPlotItem::ScaleInterest ) )
        {
            item->updateScaleDiv( axisScaleDiv( item->xAxis() ),
                axisScaleDiv( item->yAxis() ) );
        }
pixhawk's avatar
pixhawk committed
717 718 719
    }
}