qwt_plot_layout.cpp 42.9 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * 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_plot_layout.h"
pixhawk's avatar
pixhawk committed
11 12 13
#include "qwt_text.h"
#include "qwt_text_label.h"
#include "qwt_scale_widget.h"
Bryant's avatar
Bryant committed
14 15 16
#include "qwt_abstract_legend.h"
#include <qscrollbar.h>
#include <qmath.h>
pixhawk's avatar
pixhawk committed
17 18 19 20

class QwtPlotLayout::LayoutData
{
public:
Bryant's avatar
Bryant committed
21
    void init( const QwtPlot *, const QRectF &rect );
pixhawk's avatar
pixhawk committed
22

Bryant's avatar
Bryant committed
23 24
    struct t_legendData
    {
pixhawk's avatar
pixhawk committed
25
        int frameWidth;
Bryant's avatar
Bryant committed
26 27
        int hScrollExtent;
        int vScrollExtent;
pixhawk's avatar
pixhawk committed
28 29
        QSize hint;
    } legend;
30

Bryant's avatar
Bryant committed
31 32
    struct t_titleData
    {
pixhawk's avatar
pixhawk committed
33 34 35 36
        QwtText text;
        int frameWidth;
    } title;

Bryant's avatar
Bryant committed
37 38 39 40 41 42 43 44
    struct t_footerData
    {
        QwtText text;
        int frameWidth;
    } footer;

    struct t_scaleData
    {
pixhawk's avatar
pixhawk committed
45 46 47 48 49 50
        bool isEnabled;
        const QwtScaleWidget *scaleWidget;
        QFont scaleFont;
        int start;
        int end;
        int baseLineOffset;
Bryant's avatar
Bryant committed
51
        double tickOffset;
pixhawk's avatar
pixhawk committed
52 53 54
        int dimWithoutTitle;
    } scale[QwtPlot::axisCnt];

Bryant's avatar
Bryant committed
55 56 57 58
    struct t_canvasData
    {
        int contentsMargins[ QwtPlot::axisCnt ];

pixhawk's avatar
pixhawk committed
59 60 61 62 63 64
    } canvas;
};

/*
  Extract all layout relevant data from the plot components
*/
Bryant's avatar
Bryant committed
65
void QwtPlotLayout::LayoutData::init( const QwtPlot *plot, const QRectF &rect )
pixhawk's avatar
pixhawk committed
66 67 68
{
    // legend

Bryant's avatar
Bryant committed
69 70
    if ( plot->legend() )
    {
pixhawk's avatar
pixhawk committed
71
        legend.frameWidth = plot->legend()->frameWidth();
Bryant's avatar
Bryant committed
72 73 74 75
        legend.hScrollExtent =
            plot->legend()->scrollExtent( Qt::Horizontal );
        legend.vScrollExtent =
            plot->legend()->scrollExtent( Qt::Vertical );
pixhawk's avatar
pixhawk committed
76 77 78

        const QSize hint = plot->legend()->sizeHint();

Bryant's avatar
Bryant committed
79 80 81
        int w = qMin( hint.width(), qFloor( rect.width() ) );
        int h = plot->legend()->heightForWidth( w );
        if ( h <= 0 )
pixhawk's avatar
pixhawk committed
82 83 84
            h = hint.height();

        if ( h > rect.height() )
Bryant's avatar
Bryant committed
85
            w += legend.hScrollExtent;
pixhawk's avatar
pixhawk committed
86

Bryant's avatar
Bryant committed
87
        legend.hint = QSize( w, h );
pixhawk's avatar
pixhawk committed
88 89
    }

90
    // title
pixhawk's avatar
pixhawk committed
91 92 93 94

    title.frameWidth = 0;
    title.text = QwtText();

Bryant's avatar
Bryant committed
95 96
    if ( plot->titleLabel() )
    {
pixhawk's avatar
pixhawk committed
97
        const QwtTextLabel *label = plot->titleLabel();
98
        title.text = label->text();
Bryant's avatar
Bryant committed
99 100
        if ( !( title.text.testPaintAttribute( QwtText::PaintUsingTextFont ) ) )
            title.text.setFont( label->font() );
101

pixhawk's avatar
pixhawk committed
102 103 104
        title.frameWidth = plot->titleLabel()->frameWidth();
    }

Bryant's avatar
Bryant committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    // footer

    footer.frameWidth = 0;
    footer.text = QwtText();

    if ( plot->footerLabel() )
    {
        const QwtTextLabel *label = plot->footerLabel();
        footer.text = label->text();
        if ( !( footer.text.testPaintAttribute( QwtText::PaintUsingTextFont ) ) )
            footer.text.setFont( label->font() );

        footer.frameWidth = plot->footerLabel()->frameWidth();
    }

120
    // scales
pixhawk's avatar
pixhawk committed
121

Bryant's avatar
Bryant committed
122 123 124 125 126
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
        if ( plot->axisEnabled( axis ) )
        {
            const QwtScaleWidget *scaleWidget = plot->axisWidget( axis );
pixhawk's avatar
pixhawk committed
127 128 129 130 131 132 133 134 135 136 137 138 139

            scale[axis].isEnabled = true;

            scale[axis].scaleWidget = scaleWidget;

            scale[axis].scaleFont = scaleWidget->font();

            scale[axis].start = scaleWidget->startBorderDist();
            scale[axis].end = scaleWidget->endBorderDist();

            scale[axis].baseLineOffset = scaleWidget->margin();
            scale[axis].tickOffset = scaleWidget->margin();
            if ( scaleWidget->scaleDraw()->hasComponent(
Bryant's avatar
Bryant committed
140 141
                QwtAbstractScaleDraw::Ticks ) )
            {
142
                scale[axis].tickOffset +=
Bryant's avatar
Bryant committed
143
                    scaleWidget->scaleDraw()->maxTickLength();
pixhawk's avatar
pixhawk committed
144 145 146
            }

            scale[axis].dimWithoutTitle = scaleWidget->dimForLength(
Bryant's avatar
Bryant committed
147
                QWIDGETSIZE_MAX, scale[axis].scaleFont );
pixhawk's avatar
pixhawk committed
148

Bryant's avatar
Bryant committed
149 150
            if ( !scaleWidget->title().isEmpty() )
            {
151
                scale[axis].dimWithoutTitle -=
Bryant's avatar
Bryant committed
152
                    scaleWidget->titleHeightForWidth( QWIDGETSIZE_MAX );
pixhawk's avatar
pixhawk committed
153
            }
Bryant's avatar
Bryant committed
154 155 156
        }
        else
        {
pixhawk's avatar
pixhawk committed
157 158 159 160
            scale[axis].isEnabled = false;
            scale[axis].start = 0;
            scale[axis].end = 0;
            scale[axis].baseLineOffset = 0;
Bryant's avatar
Bryant committed
161
            scale[axis].tickOffset = 0.0;
pixhawk's avatar
pixhawk committed
162 163 164 165
            scale[axis].dimWithoutTitle = 0;
        }
    }

166
    // canvas
pixhawk's avatar
pixhawk committed
167

Bryant's avatar
Bryant committed
168 169 170 171 172
    plot->canvas()->getContentsMargins( 
        &canvas.contentsMargins[ QwtPlot::yLeft ], 
        &canvas.contentsMargins[ QwtPlot::xTop ],
        &canvas.contentsMargins[ QwtPlot::yRight ],
        &canvas.contentsMargins[ QwtPlot::xBottom ] );
pixhawk's avatar
pixhawk committed
173 174 175 176 177 178
}

class QwtPlotLayout::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
179 180
        spacing( 5 )
    {
pixhawk's avatar
pixhawk committed
181 182
    }

Bryant's avatar
Bryant committed
183 184 185 186 187
    QRectF titleRect;
    QRectF footerRect;
    QRectF legendRect;
    QRectF scaleRect[QwtPlot::axisCnt];
    QRectF canvasRect;
pixhawk's avatar
pixhawk committed
188 189 190 191 192 193 194

    QwtPlotLayout::LayoutData layoutData;

    QwtPlot::LegendPosition legendPos;
    double legendRatio;
    unsigned int spacing;
    unsigned int canvasMargin[QwtPlot::axisCnt];
Bryant's avatar
Bryant committed
195
    bool alignCanvasToScales[QwtPlot::axisCnt];
pixhawk's avatar
pixhawk committed
196 197 198 199 200 201 202 203 204 205
};

/*!
  \brief Constructor
 */

QwtPlotLayout::QwtPlotLayout()
{
    d_data = new PrivateData;

Bryant's avatar
Bryant committed
206 207 208
    setLegendPosition( QwtPlot::BottomLegend );
    setCanvasMargin( 4 );
    setAlignCanvasToScales( false );
pixhawk's avatar
pixhawk committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222

    invalidate();
}

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

/*!
  Change a margin of the canvas. The margin is the space
  above/below the scale ticks. A negative margin will
  be set to -1, excluding the borders of the scales.
223

pixhawk's avatar
pixhawk committed
224
  \param margin New margin
225
  \param axis One of QwtPlot::Axis. Specifies where the position of the margin.
pixhawk's avatar
pixhawk committed
226
              -1 means margin at all borders.
227
  \sa canvasMargin()
pixhawk's avatar
pixhawk committed
228

Bryant's avatar
Bryant committed
229
  \warning The margin will have no effect when alignCanvasToScale() is true
pixhawk's avatar
pixhawk committed
230 231
*/

Bryant's avatar
Bryant committed
232
void QwtPlotLayout::setCanvasMargin( int margin, int axis )
pixhawk's avatar
pixhawk committed
233 234 235 236
{
    if ( margin < -1 )
        margin = -1;

Bryant's avatar
Bryant committed
237 238 239
    if ( axis == -1 )
    {
        for ( axis = 0; axis < QwtPlot::axisCnt; axis++ )
pixhawk's avatar
pixhawk committed
240
            d_data->canvasMargin[axis] = margin;
Bryant's avatar
Bryant committed
241 242
    }
    else if ( axis >= 0 && axis < QwtPlot::axisCnt )
pixhawk's avatar
pixhawk committed
243 244 245 246
        d_data->canvasMargin[axis] = margin;
}

/*!
Bryant's avatar
Bryant committed
247
    \param axisId Axis index
pixhawk's avatar
pixhawk committed
248 249 250
    \return Margin around the scale tick borders
    \sa setCanvasMargin()
*/
Bryant's avatar
Bryant committed
251
int QwtPlotLayout::canvasMargin( int axisId ) const
pixhawk's avatar
pixhawk committed
252
{
Bryant's avatar
Bryant committed
253
    if ( axisId < 0 || axisId >= QwtPlot::axisCnt )
pixhawk's avatar
pixhawk committed
254 255
        return 0;

Bryant's avatar
Bryant committed
256 257 258 259 260 261 262 263 264 265 266 267 268
    return d_data->canvasMargin[axisId];
}

/*!
  \brief Set the align-canvas-to-axis-scales flag for all axes

  \param on True/False
  \sa setAlignCanvasToScale(), alignCanvasToScale()
*/
void QwtPlotLayout::setAlignCanvasToScales( bool on )
{
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
        d_data->alignCanvasToScales[axis] = on;
pixhawk's avatar
pixhawk committed
269 270 271 272
}

/*!
  Change the align-canvas-to-axis-scales setting. The canvas may:
Bryant's avatar
Bryant committed
273

pixhawk's avatar
pixhawk committed
274 275 276
  - extend beyond the axis scale ends to maximize its size,
  - align with the axis scale ends to control its size.

Bryant's avatar
Bryant committed
277 278 279 280 281 282 283
  The axisId parameter is somehow confusing as it identifies a border
  of the plot and not the axes, that are aligned. F.e when QwtPlot::yLeft
  is set, the left end of the the x-axes ( QwtPlot::xTop, QwtPlot::xBottom )
  is aligned.

  \param axisId Axis index
  \param on New align-canvas-to-axis-scales setting
pixhawk's avatar
pixhawk committed
284

Bryant's avatar
Bryant committed
285 286
  \sa setCanvasMargin(), alignCanvasToScale(), setAlignCanvasToScales()
  \warning In case of on == true canvasMargin() will have no effect
pixhawk's avatar
pixhawk committed
287
*/
Bryant's avatar
Bryant committed
288
void QwtPlotLayout::setAlignCanvasToScale( int axisId, bool on )
pixhawk's avatar
pixhawk committed
289
{
Bryant's avatar
Bryant committed
290 291
    if ( axisId >= 0 && axisId < QwtPlot::axisCnt )
        d_data->alignCanvasToScales[axisId] = on;
pixhawk's avatar
pixhawk committed
292 293 294 295 296 297 298
}

/*!
  Return the align-canvas-to-axis-scales setting. The canvas may:
  - extend beyond the axis scale ends to maximize its size
  - align with the axis scale ends to control its size.

Bryant's avatar
Bryant committed
299
  \param axisId Axis index
pixhawk's avatar
pixhawk committed
300
  \return align-canvas-to-axis-scales setting
Bryant's avatar
Bryant committed
301
  \sa setAlignCanvasToScale(), setAlignCanvasToScale(), setCanvasMargin()
pixhawk's avatar
pixhawk committed
302
*/
Bryant's avatar
Bryant committed
303
bool QwtPlotLayout::alignCanvasToScale( int axisId ) const
pixhawk's avatar
pixhawk committed
304
{
Bryant's avatar
Bryant committed
305 306 307 308
    if ( axisId < 0 || axisId >= QwtPlot::axisCnt )
        return false;

    return d_data->alignCanvasToScales[ axisId ];
pixhawk's avatar
pixhawk committed
309 310 311 312 313
}

/*!
  Change the spacing of the plot. The spacing is the distance
  between the plot components.
314

Bryant's avatar
Bryant committed
315 316
  \param spacing New spacing
  \sa setCanvasMargin(), spacing()
pixhawk's avatar
pixhawk committed
317
*/
Bryant's avatar
Bryant committed
318
void QwtPlotLayout::setSpacing( int spacing )
pixhawk's avatar
pixhawk committed
319
{
Bryant's avatar
Bryant committed
320
    d_data->spacing = qMax( 0, spacing );
pixhawk's avatar
pixhawk committed
321 322 323
}

/*!
Bryant's avatar
Bryant committed
324
  \return Spacing
325
  \sa margin(), setSpacing()
pixhawk's avatar
pixhawk committed
326 327 328 329 330 331 332 333
*/
int QwtPlotLayout::spacing() const
{
    return d_data->spacing;
}

/*!
  \brief Specify the position of the legend
334
  \param pos The legend's position.
Bryant's avatar
Bryant committed
335 336
  \param ratio Ratio between legend and the bounding rectangle
               of title, footer, canvas and axes. The legend will be shrunk
pixhawk's avatar
pixhawk committed
337 338 339
               if it would need more space than the given ratio.
               The ratio is limited to ]0.0 .. 1.0]. In case of <= 0.0
               it will be reset to the default ratio.
340 341
               The default vertical/horizontal ratio is 0.33/0.5.

pixhawk's avatar
pixhawk committed
342 343 344
  \sa QwtPlot::setLegendPosition()
*/

Bryant's avatar
Bryant committed
345
void QwtPlotLayout::setLegendPosition( QwtPlot::LegendPosition pos, double ratio )
pixhawk's avatar
pixhawk committed
346 347 348 349
{
    if ( ratio > 1.0 )
        ratio = 1.0;

Bryant's avatar
Bryant committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    switch ( pos )
    {
        case QwtPlot::TopLegend:
        case QwtPlot::BottomLegend:
            if ( ratio <= 0.0 )
                ratio = 0.33;
            d_data->legendRatio = ratio;
            d_data->legendPos = pos;
            break;
        case QwtPlot::LeftLegend:
        case QwtPlot::RightLegend:
            if ( ratio <= 0.0 )
                ratio = 0.5;
            d_data->legendRatio = ratio;
            d_data->legendPos = pos;
            break;
        default:
            break;
pixhawk's avatar
pixhawk committed
368 369 370 371 372
    }
}

/*!
  \brief Specify the position of the legend
373 374
  \param pos The legend's position. Valid values are
      \c QwtPlot::LeftLegend, \c QwtPlot::RightLegend,
pixhawk's avatar
pixhawk committed
375
      \c QwtPlot::TopLegend, \c QwtPlot::BottomLegend.
376

pixhawk's avatar
pixhawk committed
377 378
  \sa QwtPlot::setLegendPosition()
*/
Bryant's avatar
Bryant committed
379
void QwtPlotLayout::setLegendPosition( QwtPlot::LegendPosition pos )
pixhawk's avatar
pixhawk committed
380
{
Bryant's avatar
Bryant committed
381
    setLegendPosition( pos, 0.0 );
pixhawk's avatar
pixhawk committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395
}

/*!
  \return Position of the legend
  \sa setLegendPosition(), QwtPlot::setLegendPosition(),
      QwtPlot::legendPosition()
*/
QwtPlot::LegendPosition QwtPlotLayout::legendPosition() const
{
    return d_data->legendPos;
}

/*!
  Specify the relative size of the legend in the plot
Bryant's avatar
Bryant committed
396 397
  \param ratio Ratio between legend and the bounding rectangle
               of title, footer, canvas and axes. The legend will be shrunk
pixhawk's avatar
pixhawk committed
398 399 400
               if it would need more space than the given ratio.
               The ratio is limited to ]0.0 .. 1.0]. In case of <= 0.0
               it will be reset to the default ratio.
401
               The default vertical/horizontal ratio is 0.33/0.5.
pixhawk's avatar
pixhawk committed
402
*/
Bryant's avatar
Bryant committed
403
void QwtPlotLayout::setLegendRatio( double ratio )
pixhawk's avatar
pixhawk committed
404
{
Bryant's avatar
Bryant committed
405
    setLegendPosition( legendPosition(), ratio );
pixhawk's avatar
pixhawk committed
406 407 408 409 410 411 412 413 414 415 416
}

/*!
  \return The relative size of the legend in the plot.
  \sa setLegendPosition()
*/
double QwtPlotLayout::legendRatio() const
{
    return d_data->legendRatio;
}

Bryant's avatar
Bryant committed
417 418 419 420 421 422 423 424 425 426 427 428 429
/*!
  \brief Set the geometry for the title

  This method is intended to be used from derived layouts
  overloading activate()

  \sa titleRect(), activate()
 */
void QwtPlotLayout::setTitleRect( const QRectF &rect )
{
    d_data->titleRect = rect;
}

pixhawk's avatar
pixhawk committed
430 431 432 433
/*!
  \return Geometry for the title
  \sa activate(), invalidate()
*/
Bryant's avatar
Bryant committed
434
QRectF QwtPlotLayout::titleRect() const
pixhawk's avatar
pixhawk committed
435 436 437 438 439
{
    return d_data->titleRect;
}

/*!
Bryant's avatar
Bryant committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453
  \brief Set the geometry for the footer

  This method is intended to be used from derived layouts
  overloading activate()

  \sa footerRect(), activate()
 */
void QwtPlotLayout::setFooterRect( const QRectF &rect )
{
    d_data->footerRect = rect;
}

/*!
  \return Geometry for the footer
pixhawk's avatar
pixhawk committed
454 455
  \sa activate(), invalidate()
*/
Bryant's avatar
Bryant committed
456 457 458 459 460 461 462 463 464 465 466 467
QRectF QwtPlotLayout::footerRect() const
{
    return d_data->footerRect;
}

/*!
  \brief Set the geometry for the legend

  This method is intended to be used from derived layouts
  overloading activate()

  \param rect Rectangle for the legend
pixhawk's avatar
pixhawk committed
468

Bryant's avatar
Bryant committed
469 470 471 472 473 474 475 476 477 478 479 480
  \sa legendRect(), activate()
 */
void QwtPlotLayout::setLegendRect( const QRectF &rect )
{
    d_data->legendRect = rect;
}

/*!
  \return Geometry for the legend
  \sa activate(), invalidate()
*/
QRectF QwtPlotLayout::legendRect() const
pixhawk's avatar
pixhawk committed
481 482 483 484
{
    return d_data->legendRect;
}

Bryant's avatar
Bryant committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
/*!
  \brief Set the geometry for an axis

  This method is intended to be used from derived layouts
  overloading activate()

  \param axis Axis index
  \param rect Rectangle for the scale

  \sa scaleRect(), activate()
 */
void QwtPlotLayout::setScaleRect( int axis, const QRectF &rect )
{
    if ( axis >= 0 && axis < QwtPlot::axisCnt )
        d_data->scaleRect[axis] = rect;
}

pixhawk's avatar
pixhawk committed
502 503 504 505 506
/*!
  \param axis Axis index
  \return Geometry for the scale
  \sa activate(), invalidate()
*/
Bryant's avatar
Bryant committed
507
QRectF QwtPlotLayout::scaleRect( int axis ) const
pixhawk's avatar
pixhawk committed
508
{
Bryant's avatar
Bryant committed
509 510 511
    if ( axis < 0 || axis >= QwtPlot::axisCnt )
    {
        static QRectF dummyRect;
pixhawk's avatar
pixhawk committed
512 513 514 515 516
        return dummyRect;
    }
    return d_data->scaleRect[axis];
}

Bryant's avatar
Bryant committed
517 518 519 520 521 522 523 524 525 526 527 528 529
/*!
  \brief Set the geometry for the canvas

  This method is intended to be used from derived layouts
  overloading activate()

  \sa canvasRect(), activate()
 */
void QwtPlotLayout::setCanvasRect( const QRectF &rect )
{
    d_data->canvasRect = rect;
}

pixhawk's avatar
pixhawk committed
530 531 532 533
/*!
  \return Geometry for the canvas
  \sa activate(), invalidate()
*/
Bryant's avatar
Bryant committed
534
QRectF QwtPlotLayout::canvasRect() const
pixhawk's avatar
pixhawk committed
535 536 537 538 539
{
    return d_data->canvasRect;
}

/*!
540
  Invalidate the geometry of all components.
pixhawk's avatar
pixhawk committed
541 542 543 544
  \sa activate()
*/
void QwtPlotLayout::invalidate()
{
Bryant's avatar
Bryant committed
545 546 547 548
    d_data->titleRect = d_data->footerRect
        = d_data->legendRect = d_data->canvasRect = QRect();

    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
pixhawk's avatar
pixhawk committed
549 550 551
        d_data->scaleRect[axis] = QRect();
}

552
/*!
Bryant's avatar
Bryant committed
553 554 555
  \return Minimum size hint
  \param plot Plot widget

pixhawk's avatar
pixhawk committed
556 557 558
  \sa QwtPlot::minimumSizeHint()
*/

Bryant's avatar
Bryant committed
559
QSize QwtPlotLayout::minimumSizeHint( const QwtPlot *plot ) const
pixhawk's avatar
pixhawk committed
560 561 562 563
{
    class ScaleData
    {
    public:
Bryant's avatar
Bryant committed
564 565
        ScaleData()
        {
pixhawk's avatar
pixhawk committed
566 567 568 569 570 571 572 573 574 575 576 577
            w = h = minLeft = minRight = tickOffset = 0;
        }

        int w;
        int h;
        int minLeft;
        int minRight;
        int tickOffset;
    } scaleData[QwtPlot::axisCnt];

    int canvasBorder[QwtPlot::axisCnt];

Bryant's avatar
Bryant committed
578 579 580
    int fw;
    plot->canvas()->getContentsMargins( &fw, NULL, NULL, NULL );

pixhawk's avatar
pixhawk committed
581
    int axis;
Bryant's avatar
Bryant committed
582 583 584 585 586
    for ( axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
        if ( plot->axisEnabled( axis ) )
        {
            const QwtScaleWidget *scl = plot->axisWidget( axis );
pixhawk's avatar
pixhawk committed
587 588 589
            ScaleData &sd = scaleData[axis];

            const QSize hint = scl->minimumSizeHint();
590 591
            sd.w = hint.width();
            sd.h = hint.height();
Bryant's avatar
Bryant committed
592
            scl->getBorderDistHint( sd.minLeft, sd.minRight );
pixhawk's avatar
pixhawk committed
593
            sd.tickOffset = scl->margin();
Bryant's avatar
Bryant committed
594 595
            if ( scl->scaleDraw()->hasComponent( QwtAbstractScaleDraw::Ticks ) )
                sd.tickOffset += qCeil( scl->scaleDraw()->maxTickLength() );
pixhawk's avatar
pixhawk committed
596 597
        }

Bryant's avatar
Bryant committed
598
        canvasBorder[axis] = fw + d_data->canvasMargin[axis] + 1;
pixhawk's avatar
pixhawk committed
599 600 601
    }


Bryant's avatar
Bryant committed
602 603
    for ( axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
pixhawk's avatar
pixhawk committed
604
        ScaleData &sd = scaleData[axis];
Bryant's avatar
Bryant committed
605 606 607 608 609
        if ( sd.w && ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop ) )
        {
            if ( ( sd.minLeft > canvasBorder[QwtPlot::yLeft] )
                && scaleData[QwtPlot::yLeft].w )
            {
pixhawk's avatar
pixhawk committed
610 611 612 613 614 615
                int shiftLeft = sd.minLeft - canvasBorder[QwtPlot::yLeft];
                if ( shiftLeft > scaleData[QwtPlot::yLeft].w )
                    shiftLeft = scaleData[QwtPlot::yLeft].w;

                sd.w -= shiftLeft;
            }
Bryant's avatar
Bryant committed
616 617 618
            if ( ( sd.minRight > canvasBorder[QwtPlot::yRight] )
                && scaleData[QwtPlot::yRight].w )
            {
pixhawk's avatar
pixhawk committed
619 620 621 622 623 624 625 626
                int shiftRight = sd.minRight - canvasBorder[QwtPlot::yRight];
                if ( shiftRight > scaleData[QwtPlot::yRight].w )
                    shiftRight = scaleData[QwtPlot::yRight].w;

                sd.w -= shiftRight;
            }
        }

Bryant's avatar
Bryant committed
627 628 629 630 631
        if ( sd.h && ( axis == QwtPlot::yLeft || axis == QwtPlot::yRight ) )
        {
            if ( ( sd.minLeft > canvasBorder[QwtPlot::xBottom] ) &&
                scaleData[QwtPlot::xBottom].h )
            {
pixhawk's avatar
pixhawk committed
632 633 634 635 636 637
                int shiftBottom = sd.minLeft - canvasBorder[QwtPlot::xBottom];
                if ( shiftBottom > scaleData[QwtPlot::xBottom].tickOffset )
                    shiftBottom = scaleData[QwtPlot::xBottom].tickOffset;

                sd.h -= shiftBottom;
            }
Bryant's avatar
Bryant committed
638 639 640
            if ( ( sd.minLeft > canvasBorder[QwtPlot::xTop] ) &&
                scaleData[QwtPlot::xTop].h )
            {
pixhawk's avatar
pixhawk committed
641 642 643 644 645 646 647 648 649
                int shiftTop = sd.minRight - canvasBorder[QwtPlot::xTop];
                if ( shiftTop > scaleData[QwtPlot::xTop].tickOffset )
                    shiftTop = scaleData[QwtPlot::xTop].tickOffset;

                sd.h -= shiftTop;
            }
        }
    }

Bryant's avatar
Bryant committed
650 651 652 653 654
    const QWidget *canvas = plot->canvas();

    int left, top, right, bottom;
    canvas->getContentsMargins( &left, &top, &right, &bottom );

pixhawk's avatar
pixhawk committed
655 656 657
    const QSize minCanvasSize = canvas->minimumSize();

    int w = scaleData[QwtPlot::yLeft].w + scaleData[QwtPlot::yRight].w;
Bryant's avatar
Bryant committed
658 659 660
    int cw = qMax( scaleData[QwtPlot::xBottom].w, scaleData[QwtPlot::xTop].w )
        + left + 1 + right + 1;
    w += qMax( cw, minCanvasSize.width() );
pixhawk's avatar
pixhawk committed
661 662

    int h = scaleData[QwtPlot::xBottom].h + scaleData[QwtPlot::xTop].h;
Bryant's avatar
Bryant committed
663 664 665 666 667 668 669
    int ch = qMax( scaleData[QwtPlot::yLeft].h, scaleData[QwtPlot::yRight].h )
        + top + 1 + bottom + 1;
    h += qMax( ch, minCanvasSize.height() );

    const QwtTextLabel *labels[2];
    labels[0] = plot->titleLabel();
    labels[1] = plot->footerLabel();
pixhawk's avatar
pixhawk committed
670

Bryant's avatar
Bryant committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    for ( int i = 0; i < 2; i++ )
    {
        const QwtTextLabel *label   = labels[i];
        if ( label && !label->text().isEmpty() )
        {
            // If only QwtPlot::yLeft or QwtPlot::yRight is showing,
            // we center on the plot canvas.
            const bool centerOnCanvas = !( plot->axisEnabled( QwtPlot::yLeft )
                && plot->axisEnabled( QwtPlot::yRight ) );

            int labelW = w;
            if ( centerOnCanvas )
            {
                labelW -= scaleData[QwtPlot::yLeft].w
                    + scaleData[QwtPlot::yRight].w;
pixhawk's avatar
pixhawk committed
686 687
            }

Bryant's avatar
Bryant committed
688 689 690 691 692 693 694 695 696 697 698 699 700
            int labelH = label->heightForWidth( labelW );
            if ( labelH > labelW ) // Compensate for a long title
            {
                w = labelW = labelH;
                if ( centerOnCanvas )
                {
                    w += scaleData[QwtPlot::yLeft].w
                        + scaleData[QwtPlot::yRight].w;
                }

                labelH = label->heightForWidth( labelW );
            }
            h += labelH + d_data->spacing;
pixhawk's avatar
pixhawk committed
701 702 703 704 705
        }
    }

    // Compute the legend contribution

Bryant's avatar
Bryant committed
706 707 708
    const QwtAbstractLegend *legend = plot->legend();
    if ( legend && !legend->isEmpty() )
    {
709
        if ( d_data->legendPos == QwtPlot::LeftLegend
Bryant's avatar
Bryant committed
710 711
            || d_data->legendPos == QwtPlot::RightLegend )
        {
pixhawk's avatar
pixhawk committed
712
            int legendW = legend->sizeHint().width();
Bryant's avatar
Bryant committed
713
            int legendH = legend->heightForWidth( legendW );
pixhawk's avatar
pixhawk committed
714 715 716 717 718

            if ( legend->frameWidth() > 0 )
                w += d_data->spacing;

            if ( legendH > h )
Bryant's avatar
Bryant committed
719
                legendW += legend->scrollExtent( Qt::Horizontal );
pixhawk's avatar
pixhawk committed
720 721

            if ( d_data->legendRatio < 1.0 )
Bryant's avatar
Bryant committed
722
                legendW = qMin( legendW, int( w / ( 1.0 - d_data->legendRatio ) ) );
pixhawk's avatar
pixhawk committed
723

Bryant's avatar
Bryant committed
724 725 726 727 728 729
            w += legendW + d_data->spacing;
        }
        else // QwtPlot::Top, QwtPlot::Bottom
        {
            int legendW = qMin( legend->sizeHint().width(), w );
            int legendH = legend->heightForWidth( legendW );
pixhawk's avatar
pixhawk committed
730 731 732 733 734

            if ( legend->frameWidth() > 0 )
                h += d_data->spacing;

            if ( d_data->legendRatio < 1.0 )
Bryant's avatar
Bryant committed
735
                legendH = qMin( legendH, int( h / ( 1.0 - d_data->legendRatio ) ) );
736

Bryant's avatar
Bryant committed
737
            h += legendH + d_data->spacing;
pixhawk's avatar
pixhawk committed
738 739 740 741 742 743 744 745
        }
    }

    return QSize( w, h );
}

/*!
  Find the geometry for the legend
Bryant's avatar
Bryant committed
746

pixhawk's avatar
pixhawk committed
747 748
  \param options Options how to layout the legend
  \param rect Rectangle where to place the legend
Bryant's avatar
Bryant committed
749

pixhawk's avatar
pixhawk committed
750
  \return Geometry for the legend
Bryant's avatar
Bryant committed
751
  \sa Options
pixhawk's avatar
pixhawk committed
752 753
*/

Bryant's avatar
Bryant committed
754 755
QRectF QwtPlotLayout::layoutLegend( Options options,
    const QRectF &rect ) const
pixhawk's avatar
pixhawk committed
756
{
Bryant's avatar
Bryant committed
757
    const QSize hint( d_data->layoutData.legend.hint );
pixhawk's avatar
pixhawk committed
758 759

    int dim;
760
    if ( d_data->legendPos == QwtPlot::LeftLegend
Bryant's avatar
Bryant committed
761 762
        || d_data->legendPos == QwtPlot::RightLegend )
    {
pixhawk's avatar
pixhawk committed
763 764 765
        // We don't allow vertical legends to take more than
        // half of the available space.

Bryant's avatar
Bryant committed
766
        dim = qMin( hint.width(), int( rect.width() * d_data->legendRatio ) );
pixhawk's avatar
pixhawk committed
767

Bryant's avatar
Bryant committed
768 769 770 771
        if ( !( options & IgnoreScrollbars ) )
        {
            if ( hint.height() > rect.height() )
            {
pixhawk's avatar
pixhawk committed
772
                // The legend will need additional
773
                // space for the vertical scrollbar.
pixhawk's avatar
pixhawk committed
774

Bryant's avatar
Bryant committed
775
                dim += d_data->layoutData.legend.hScrollExtent;
pixhawk's avatar
pixhawk committed
776 777
            }
        }
Bryant's avatar
Bryant committed
778 779 780 781 782
    }
    else
    {
        dim = qMin( hint.height(), int( rect.height() * d_data->legendRatio ) );
        dim = qMax( dim, d_data->layoutData.legend.vScrollExtent );
pixhawk's avatar
pixhawk committed
783 784
    }

Bryant's avatar
Bryant committed
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
    QRectF legendRect = rect;
    switch ( d_data->legendPos )
    {
        case QwtPlot::LeftLegend:
            legendRect.setWidth( dim );
            break;
        case QwtPlot::RightLegend:
            legendRect.setX( rect.right() - dim );
            legendRect.setWidth( dim );
            break;
        case QwtPlot::TopLegend:
            legendRect.setHeight( dim );
            break;
        case QwtPlot::BottomLegend:
            legendRect.setY( rect.bottom() - dim );
            legendRect.setHeight( dim );
            break;
pixhawk's avatar
pixhawk committed
802 803 804 805 806 807 808
    }

    return legendRect;
}

/*!
  Align the legend to the canvas
Bryant's avatar
Bryant committed
809

pixhawk's avatar
pixhawk committed
810 811
  \param canvasRect Geometry of the canvas
  \param legendRect Maximum geometry for the legend
Bryant's avatar
Bryant committed
812

pixhawk's avatar
pixhawk committed
813 814
  \return Geometry for the aligned legend
*/
Bryant's avatar
Bryant committed
815 816
QRectF QwtPlotLayout::alignLegend( const QRectF &canvasRect,
    const QRectF &legendRect ) const
pixhawk's avatar
pixhawk committed
817
{
Bryant's avatar
Bryant committed
818
    QRectF alignedRect = legendRect;
pixhawk's avatar
pixhawk committed
819

820
    if ( d_data->legendPos == QwtPlot::BottomLegend
Bryant's avatar
Bryant committed
821 822 823 824 825 826
        || d_data->legendPos == QwtPlot::TopLegend )
    {
        if ( d_data->layoutData.legend.hint.width() < canvasRect.width() )
        {
            alignedRect.setX( canvasRect.x() );
            alignedRect.setWidth( canvasRect.width() );
pixhawk's avatar
pixhawk committed
827
        }
Bryant's avatar
Bryant committed
828 829 830 831 832 833 834
    }
    else
    {
        if ( d_data->layoutData.legend.hint.height() < canvasRect.height() )
        {
            alignedRect.setY( canvasRect.y() );
            alignedRect.setHeight( canvasRect.height() );
pixhawk's avatar
pixhawk committed
835 836 837 838 839 840 841 842 843 844 845
        }
    }

    return alignedRect;
}

/*!
  Expand all line breaks in text labels, and calculate the height
  of their widgets in orientation of the text.

  \param options Options how to layout the legend
Bryant's avatar
Bryant committed
846
  \param rect Bounding rectangle for title, footer, axes and canvas.
pixhawk's avatar
pixhawk committed
847
  \param dimTitle Expanded height of the title widget
Bryant's avatar
Bryant committed
848
  \param dimFooter Expanded height of the footer widget
pixhawk's avatar
pixhawk committed
849
  \param dimAxis Expanded heights of the axis in axis orientation.
Bryant's avatar
Bryant committed
850 851

  \sa Options
pixhawk's avatar
pixhawk committed
852
*/
Bryant's avatar
Bryant committed
853 854
void QwtPlotLayout::expandLineBreaks( Options options, const QRectF &rect,
    int &dimTitle, int &dimFooter, int dimAxis[QwtPlot::axisCnt] ) const
pixhawk's avatar
pixhawk committed
855
{
Bryant's avatar
Bryant committed
856
    dimTitle = dimFooter = 0;
pixhawk's avatar
pixhawk committed
857 858 859 860
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
        dimAxis[axis] = 0;

    int backboneOffset[QwtPlot::axisCnt];
Bryant's avatar
Bryant committed
861 862
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
pixhawk's avatar
pixhawk committed
863
        backboneOffset[axis] = 0;
Bryant's avatar
Bryant committed
864 865 866 867
        if ( !( options & IgnoreFrames ) )
            backboneOffset[axis] += d_data->layoutData.canvas.contentsMargins[ axis ];

        if ( !d_data->alignCanvasToScales[axis] )
pixhawk's avatar
pixhawk committed
868 869 870 871
            backboneOffset[axis] += d_data->canvasMargin[axis];
    }

    bool done = false;
Bryant's avatar
Bryant committed
872 873
    while ( !done )
    {
pixhawk's avatar
pixhawk committed
874 875 876 877 878 879 880 881 882 883
        done = true;

        // the size for the 4 axis depend on each other. Expanding
        // the height of a horizontal axis will shrink the height
        // for the vertical axis, shrinking the height of a vertical
        // axis will result in a line break what will expand the
        // width and results in shrinking the width of a horizontal
        // axis what might result in a line break of a horizontal
        // axis ... . So we loop as long until no size changes.

Bryant's avatar
Bryant committed
884 885 886 887
        if ( !( ( options & IgnoreTitle ) ||
            d_data->layoutData.title.text.isEmpty() ) )
        {
            double w = rect.width();
pixhawk's avatar
pixhawk committed
888 889

            if ( d_data->layoutData.scale[QwtPlot::yLeft].isEnabled
Bryant's avatar
Bryant committed
890 891
                != d_data->layoutData.scale[QwtPlot::yRight].isEnabled )
            {
pixhawk's avatar
pixhawk committed
892
                // center to the canvas
893
                w -= dimAxis[QwtPlot::yLeft] + dimAxis[QwtPlot::yRight];
pixhawk's avatar
pixhawk committed
894 895
            }

Bryant's avatar
Bryant committed
896 897
            int d = qCeil( d_data->layoutData.title.text.heightForWidth( w ) );
            if ( !( options & IgnoreFrames ) )
pixhawk's avatar
pixhawk committed
898 899
                d += 2 * d_data->layoutData.title.frameWidth;

Bryant's avatar
Bryant committed
900 901
            if ( d > dimTitle )
            {
pixhawk's avatar
pixhawk committed
902 903 904 905 906
                dimTitle = d;
                done = false;
            }
        }

Bryant's avatar
Bryant committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
        if ( !( ( options & IgnoreFooter ) ||
            d_data->layoutData.footer.text.isEmpty() ) )
        {
            double w = rect.width();

            if ( d_data->layoutData.scale[QwtPlot::yLeft].isEnabled
                != d_data->layoutData.scale[QwtPlot::yRight].isEnabled )
            {
                // center to the canvas
                w -= dimAxis[QwtPlot::yLeft] + dimAxis[QwtPlot::yRight];
            }

            int d = qCeil( d_data->layoutData.footer.text.heightForWidth( w ) );
            if ( !( options & IgnoreFrames ) )
                d += 2 * d_data->layoutData.footer.frameWidth;

            if ( d > dimFooter )
            {
                dimFooter = d;
                done = false;
            }
        }

        for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
        {
932
            const struct LayoutData::t_scaleData &scaleData =
Bryant's avatar
Bryant committed
933
                d_data->layoutData.scale[axis];
pixhawk's avatar
pixhawk committed
934

Bryant's avatar
Bryant committed
935 936 937 938 939
            if ( scaleData.isEnabled )
            {
                double length;
                if ( axis == QwtPlot::xTop || axis == QwtPlot::xBottom )
                {
940
                    length = rect.width() - dimAxis[QwtPlot::yLeft]
Bryant's avatar
Bryant committed
941
                        - dimAxis[QwtPlot::yRight];
pixhawk's avatar
pixhawk committed
942 943 944 945 946
                    length -= scaleData.start + scaleData.end;

                    if ( dimAxis[QwtPlot::yRight] > 0 )
                        length -= 1;

Bryant's avatar
Bryant committed
947 948 949 950 951 952 953
                    length += qMin( dimAxis[QwtPlot::yLeft],
                        scaleData.start - backboneOffset[QwtPlot::yLeft] );
                    length += qMin( dimAxis[QwtPlot::yRight],
                        scaleData.end - backboneOffset[QwtPlot::yRight] );
                }
                else // QwtPlot::yLeft, QwtPlot::yRight
                {
954
                    length = rect.height() - dimAxis[QwtPlot::xTop]
Bryant's avatar
Bryant committed
955
                        - dimAxis[QwtPlot::xBottom];
pixhawk's avatar
pixhawk committed
956 957 958 959 960 961 962 963
                    length -= scaleData.start + scaleData.end;
                    length -= 1;

                    if ( dimAxis[QwtPlot::xBottom] <= 0 )
                        length -= 1;
                    if ( dimAxis[QwtPlot::xTop] <= 0 )
                        length -= 1;

Bryant's avatar
Bryant committed
964 965 966 967 968
                    if ( dimAxis[QwtPlot::xBottom] > 0 )
                    {
                        length += qMin(
                            d_data->layoutData.scale[QwtPlot::xBottom].tickOffset,
                            double( scaleData.start - backboneOffset[QwtPlot::xBottom] ) );
pixhawk's avatar
pixhawk committed
969
                    }
Bryant's avatar
Bryant committed
970 971 972 973 974
                    if ( dimAxis[QwtPlot::xTop] > 0 )
                    {
                        length += qMin(
                            d_data->layoutData.scale[QwtPlot::xTop].tickOffset,
                            double( scaleData.end - backboneOffset[QwtPlot::xTop] ) );
pixhawk's avatar
pixhawk committed
975 976 977 978 979 980 981
                    }

                    if ( dimTitle > 0 )
                        length -= dimTitle + d_data->spacing;
                }

                int d = scaleData.dimWithoutTitle;
Bryant's avatar
Bryant committed
982 983 984
                if ( !scaleData.scaleWidget->title().isEmpty() )
                {
                    d += scaleData.scaleWidget->titleHeightForWidth( qFloor( length ) );
pixhawk's avatar
pixhawk committed
985 986 987
                }


Bryant's avatar
Bryant committed
988 989
                if ( d > dimAxis[axis] )
                {
pixhawk's avatar
pixhawk committed
990 991 992 993 994 995 996 997 998 999 1000
                    dimAxis[axis] = d;
                    done = false;
                }
            }
        }
    }
}

/*!
  Align the ticks of the axis to the canvas borders using
  the empty corners.
Bryant's avatar
Bryant committed
1001 1002 1003 1004 1005 1006

  \param options Layout options
  \param canvasRect Geometry of the canvas ( IN/OUT )
  \param scaleRect Geometries of the scales ( IN/OUT )

  \sa Options
pixhawk's avatar
pixhawk committed
1007 1008
*/

Bryant's avatar
Bryant committed
1009 1010
void QwtPlotLayout::alignScales( Options options,
    QRectF &canvasRect, QRectF scaleRect[QwtPlot::axisCnt] ) const
pixhawk's avatar
pixhawk committed
1011 1012
{
    int backboneOffset[QwtPlot::axisCnt];
Bryant's avatar
Bryant committed
1013 1014
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
pixhawk's avatar
pixhawk committed
1015
        backboneOffset[axis] = 0;
Bryant's avatar
Bryant committed
1016 1017 1018

        if ( !d_data->alignCanvasToScales[axis] )
        {
pixhawk's avatar
pixhawk committed
1019
            backboneOffset[axis] += d_data->canvasMargin[axis];
Bryant's avatar
Bryant committed
1020 1021 1022 1023 1024 1025 1026
        }

        if ( !( options & IgnoreFrames ) )
        {
            backboneOffset[axis] += 
                d_data->layoutData.canvas.contentsMargins[axis];
        }
pixhawk's avatar
pixhawk committed
1027 1028
    }

Bryant's avatar
Bryant committed
1029 1030
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
pixhawk's avatar
pixhawk committed
1031 1032 1033 1034 1035 1036
        if ( !scaleRect[axis].isValid() )
            continue;

        const int startDist = d_data->layoutData.scale[axis].start;
        const int endDist = d_data->layoutData.scale[axis].end;

Bryant's avatar
Bryant committed
1037
        QRectF &axisRect = scaleRect[axis];
pixhawk's avatar
pixhawk committed
1038

Bryant's avatar
Bryant committed
1039 1040 1041
        if ( axis == QwtPlot::xTop || axis == QwtPlot::xBottom )
        {
            const QRectF &leftScaleRect = scaleRect[QwtPlot::yLeft];
1042
            const int leftOffset =
pixhawk's avatar
pixhawk committed
1043 1044
                backboneOffset[QwtPlot::yLeft] - startDist;

Bryant's avatar
Bryant committed
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
            if ( leftScaleRect.isValid() )
            {
                const double dx = leftOffset + leftScaleRect.width();
                if ( d_data->alignCanvasToScales[QwtPlot::yLeft] && dx < 0.0 )
                {
                    /*
                      The axis needs more space than the width
                      of the left scale.
                     */
                    const double cLeft = canvasRect.left(); // qreal -> double
                    canvasRect.setLeft( qMax( cLeft, axisRect.left() - dx ) );
                }
                else
                {
                    const double minLeft = leftScaleRect.left();
                    const double left = axisRect.left() + leftOffset;
                    axisRect.setLeft( qMax( left, minLeft ) );
                }
            }
            else
            {
                if ( d_data->alignCanvasToScales[QwtPlot::yLeft] && leftOffset < 0 )
                {
                    canvasRect.setLeft( qMax( canvasRect.left(),
                        axisRect.left() - leftOffset ) );
                }
                else
                {
pixhawk's avatar
pixhawk committed
1073
                    if ( leftOffset > 0 )
Bryant's avatar
Bryant committed
1074
                        axisRect.setLeft( axisRect.left() + leftOffset );
pixhawk's avatar
pixhawk committed
1075 1076 1077
                }
            }

Bryant's avatar
Bryant committed
1078
            const QRectF &rightScaleRect = scaleRect[QwtPlot::yRight];
1079
            const int rightOffset =
pixhawk's avatar
pixhawk committed
1080 1081
                backboneOffset[QwtPlot::yRight] - endDist + 1;

Bryant's avatar
Bryant committed
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
            if ( rightScaleRect.isValid() )
            {
                const double dx = rightOffset + rightScaleRect.width();
                if ( d_data->alignCanvasToScales[QwtPlot::yRight] && dx < 0 )
                {
                    /*
                      The axis needs more space than the width
                      of the right scale.
                     */
                    const double cRight = canvasRect.right(); // qreal -> double
                    canvasRect.setRight( qMin( cRight, axisRect.right() + dx ) );
                }   

                const double maxRight = rightScaleRect.right();
                const double right = axisRect.right() - rightOffset;
                axisRect.setRight( qMin( right, maxRight ) );
            }
            else
            {
                if ( d_data->alignCanvasToScales[QwtPlot::yRight] && rightOffset < 0 )
                {
                    canvasRect.setRight( qMin( canvasRect.right(),
                        axisRect.right() + rightOffset ) );
                }
                else
                {
pixhawk's avatar
pixhawk committed
1108
                    if ( rightOffset > 0 )
Bryant's avatar
Bryant committed
1109
                        axisRect.setRight( axisRect.right() - rightOffset );
pixhawk's avatar
pixhawk committed
1110 1111
                }
            }
Bryant's avatar
Bryant committed
1112 1113 1114 1115
        }
        else // QwtPlot::yLeft, QwtPlot::yRight
        {
            const QRectF &bottomScaleRect = scaleRect[QwtPlot::xBottom];
1116
            const int bottomOffset =
pixhawk's avatar
pixhawk committed
1117 1118
                backboneOffset[QwtPlot::xBottom] - endDist + 1;

Bryant's avatar
Bryant committed
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
            if ( bottomScaleRect.isValid() )
            {
                const double dy = bottomOffset + bottomScaleRect.height();
                if ( d_data->alignCanvasToScales[QwtPlot::xBottom] && dy < 0 )
                {
                    /*
                      The axis needs more space than the height
                      of the bottom scale.
                     */
                    const double cBottom = canvasRect.bottom(); // qreal -> double
                    canvasRect.setBottom( qMin( cBottom, axisRect.bottom() + dy ) );
                }
                else
                {
                    const double maxBottom = bottomScaleRect.top() +
                        d_data->layoutData.scale[QwtPlot::xBottom].tickOffset;
                    const double bottom = axisRect.bottom() - bottomOffset;
                    axisRect.setBottom( qMin( bottom, maxBottom ) );
                }
            }
            else
            {
                if ( d_data->alignCanvasToScales[QwtPlot::xBottom] && bottomOffset < 0 )
                {
                    canvasRect.setBottom( qMin( canvasRect.bottom(),
                        axisRect.bottom() + bottomOffset ) );
                }
                else
                {
pixhawk's avatar
pixhawk committed
1148
                    if ( bottomOffset > 0 )
Bryant's avatar
Bryant committed
1149
                        axisRect.setBottom( axisRect.bottom() - bottomOffset );
pixhawk's avatar
pixhawk committed
1150 1151
                }
            }
1152

Bryant's avatar
Bryant committed
1153
            const QRectF &topScaleRect = scaleRect[QwtPlot::xTop];
pixhawk's avatar
pixhawk committed
1154 1155
            const int topOffset = backboneOffset[QwtPlot::xTop] - startDist;

Bryant's avatar
Bryant committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
            if ( topScaleRect.isValid() )
            {
                const double dy = topOffset + topScaleRect.height();
                if ( d_data->alignCanvasToScales[QwtPlot::xTop] && dy < 0 )
                {
                    /*
                      The axis needs more space than the height
                      of the top scale.
                     */
                    const double cTop = canvasRect.top(); // qreal -> double
                    canvasRect.setTop( qMax( cTop, axisRect.top() - dy ) );
                }
                else
                {
                    const double minTop = topScaleRect.bottom() -
                        d_data->layoutData.scale[QwtPlot::xTop].tickOffset;
                    const double top = axisRect.top() + topOffset;
                    axisRect.setTop( qMax( top, minTop ) );
                }
            }
            else
            {
                if ( d_data->alignCanvasToScales[QwtPlot::xTop] && topOffset < 0 )
                {
                    canvasRect.setTop( qMax( canvasRect.top(),
                        axisRect.top() - topOffset ) );
                }
                else
                {
pixhawk's avatar
pixhawk committed
1185
                    if ( topOffset > 0 )
Bryant's avatar
Bryant committed
1186
                        axisRect.setTop( axisRect.top() + topOffset );
pixhawk's avatar
pixhawk committed
1187 1188 1189 1190 1191
                }
            }
        }
    }

Bryant's avatar
Bryant committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
    /*
      The canvas has been aligned to the scale with largest
      border distances. Now we have to realign the other scale.
     */


    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
        QRectF &sRect = scaleRect[axis];

        if ( !sRect.isValid() )
            continue;

        if ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
        {
            if ( d_data->alignCanvasToScales[QwtPlot::yLeft] )
            {
                double y = canvasRect.left() - d_data->layoutData.scale[axis].start;
                if ( !( options & IgnoreFrames ) )
                    y += d_data->layoutData.canvas.contentsMargins[ QwtPlot::yLeft ];

                sRect.setLeft( y );
            }
            if ( d_data->alignCanvasToScales[QwtPlot::yRight] )
            {
                double y = canvasRect.right() - 1 + d_data->layoutData.scale[axis].end;
                if ( !( options & IgnoreFrames ) )
                    y -= d_data->layoutData.canvas.contentsMargins[ QwtPlot::yRight ];

                sRect.setRight( y );
            }

            if ( d_data->alignCanvasToScales[ axis ] )
            {
                if ( axis == QwtPlot::xTop )
                    sRect.setBottom( canvasRect.top() );
                else
                    sRect.setTop( canvasRect.bottom() );
pixhawk's avatar
pixhawk committed
1230 1231
            }
        }
Bryant's avatar
Bryant committed
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
        else
        {
            if ( d_data->alignCanvasToScales[QwtPlot::xTop] )
            {
                double x = canvasRect.top() - d_data->layoutData.scale[axis].start;
                if ( !( options & IgnoreFrames ) )
                    x += d_data->layoutData.canvas.contentsMargins[ QwtPlot::xTop ];

                sRect.setTop( x );
            }
            if ( d_data->alignCanvasToScales[QwtPlot::xBottom] )
            {
                double x = canvasRect.bottom() - 1 + d_data->layoutData.scale[axis].end;
                if ( !( options & IgnoreFrames ) )
                    x -= d_data->layoutData.canvas.contentsMargins[ QwtPlot::xBottom ];

                sRect.setBottom( x );
            }
pixhawk's avatar
pixhawk committed
1250

Bryant's avatar
Bryant committed
1251 1252 1253 1254 1255 1256
            if ( d_data->alignCanvasToScales[ axis ] )
            {
                if ( axis == QwtPlot::yLeft )
                    sRect.setRight( canvasRect.left() );
                else
                    sRect.setLeft( canvasRect.right() );
pixhawk's avatar
pixhawk committed
1257 1258 1259 1260 1261 1262
            }
        }
    }
}

/*!
1263
  \brief Recalculate the geometry of all components.
pixhawk's avatar
pixhawk committed
1264 1265

  \param plot Plot to be layout
Bryant's avatar
Bryant committed
1266 1267
  \param plotRect Rectangle where to place the components
  \param options Layout options
pixhawk's avatar
pixhawk committed
1268

Bryant's avatar
Bryant committed
1269
  \sa invalidate(), titleRect(), footerRect()
pixhawk's avatar
pixhawk committed
1270 1271
      legendRect(), scaleRect(), canvasRect()
*/
Bryant's avatar
Bryant committed
1272 1273
void QwtPlotLayout::activate( const QwtPlot *plot,
    const QRectF &plotRect, Options options )
pixhawk's avatar
pixhawk committed
1274 1275 1276
{
    invalidate();

Bryant's avatar
Bryant committed
1277
    QRectF rect( plotRect );  // undistributed rest of the plot rect
pixhawk's avatar
pixhawk committed
1278

Bryant's avatar
Bryant committed
1279 1280
    // We extract all layout relevant parameters from the widgets,
    // and save them to d_data->layoutData.
pixhawk's avatar
pixhawk committed
1281

Bryant's avatar
Bryant committed
1282
    d_data->layoutData.init( plot, rect );
pixhawk's avatar
pixhawk committed
1283

Bryant's avatar
Bryant committed
1284 1285 1286 1287
    if ( !( options & IgnoreLegend )
        && plot->legend() && !plot->legend()->isEmpty() )
    {
        d_data->legendRect = layoutLegend( options, rect );
pixhawk's avatar
pixhawk committed
1288 1289 1290

        // subtract d_data->legendRect from rect

Bryant's avatar
Bryant committed
1291 1292
        const QRegion region( rect.toRect() );
        rect = region.subtracted( d_data->legendRect.toRect() ).boundingRect();
pixhawk's avatar
pixhawk committed
1293

Bryant's avatar
Bryant committed
1294 1295
        switch ( d_data->legendPos )
        {
1296
            case QwtPlot::LeftLegend:
Bryant's avatar
Bryant committed
1297
                rect.setLeft( rect.left() + d_data->spacing );
1298 1299
                break;
            case QwtPlot::RightLegend:
Bryant's avatar
Bryant committed
1300
                rect.setRight( rect.right() - d_data->spacing );
1301 1302
                break;
            case QwtPlot::TopLegend:
Bryant's avatar
Bryant committed
1303
                rect.setTop( rect.top() + d_data->spacing );
1304 1305
                break;
            case QwtPlot::BottomLegend:
Bryant's avatar
Bryant committed
1306
                rect.setBottom( rect.bottom() - d_data->spacing );
1307
                break;
pixhawk's avatar
pixhawk committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
        }
    }

    /*
     +---+-----------+---+
     |       Title       |
     +---+-----------+---+
     |   |   Axis    |   |
     +---+-----------+---+
     | A |           | A |
     | x |  Canvas   | x |
     | i |           | i |
     | s |           | s |
     +---+-----------+---+
     |   |   Axis    |   |
     +---+-----------+---+
Bryant's avatar
Bryant committed
1324 1325
     |      Footer       |
     +---+-----------+---+
pixhawk's avatar
pixhawk committed
1326 1327
    */

Bryant's avatar
Bryant committed
1328
    // title, footer and axes include text labels. The height of each
pixhawk's avatar
pixhawk committed
1329 1330
    // label depends on its line breaks, that depend on the width
    // for the label. A line break in a horizontal text will reduce
1331
    // the available width for vertical texts and vice versa.
Bryant's avatar
Bryant committed
1332
    // expandLineBreaks finds the height/width for title, footer and axes
pixhawk's avatar
pixhawk committed
1333 1334
    // including all line breaks.

Bryant's avatar
Bryant committed
1335 1336
    int dimTitle, dimFooter, dimAxes[QwtPlot::axisCnt];
    expandLineBreaks( options, rect, dimTitle, dimFooter, dimAxes );
pixhawk's avatar
pixhawk committed
1337

Bryant's avatar
Bryant committed
1338 1339 1340 1341 1342 1343
    if ( dimTitle > 0 )
    {
        d_data->titleRect.setRect(
            rect.left(), rect.top(), rect.width(), dimTitle );

        rect.setTop( d_data->titleRect.bottom() + d_data->spacing );
pixhawk's avatar
pixhawk committed
1344 1345

        if ( d_data->layoutData.scale[QwtPlot::yLeft].isEnabled !=
Bryant's avatar
Bryant committed
1346 1347
            d_data->layoutData.scale[QwtPlot::yRight].isEnabled )
        {
pixhawk's avatar
pixhawk committed
1348 1349 1350
            // if only one of the y axes is missing we align
            // the title centered to the canvas

Bryant's avatar
Bryant committed
1351 1352 1353
            d_data->titleRect.setX( rect.left() + dimAxes[QwtPlot::yLeft] );
            d_data->titleRect.setWidth( rect.width()
                - dimAxes[QwtPlot::yLeft] - dimAxes[QwtPlot::yRight] );
pixhawk's avatar
pixhawk committed
1354
        }
Bryant's avatar
Bryant committed
1355
    }
pixhawk's avatar
pixhawk committed
1356

Bryant's avatar
Bryant committed
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
    if ( dimFooter > 0 )
    {
        d_data->footerRect.setRect(
            rect.left(), rect.bottom() - dimFooter, rect.width(), dimFooter );

        rect.setBottom( d_data->footerRect.top() - d_data->spacing );

        if ( d_data->layoutData.scale[QwtPlot::yLeft].isEnabled !=
            d_data->layoutData.scale[QwtPlot::yRight].isEnabled )
        {
            // if only one of the y axes is missing we align
            // the footer centered to the canvas

            d_data->footerRect.setX( rect.left() + dimAxes[QwtPlot::yLeft] );
            d_data->footerRect.setWidth( rect.width()
                - dimAxes[QwtPlot::yLeft] - dimAxes[QwtPlot::yRight] );
        }
pixhawk's avatar
pixhawk committed
1374 1375 1376 1377 1378 1379
    }

    d_data->canvasRect.setRect(
        rect.x() + dimAxes[QwtPlot::yLeft],
        rect.y() + dimAxes[QwtPlot::xTop],
        rect.width() - dimAxes[QwtPlot::yRight] - dimAxes[QwtPlot::yLeft],
Bryant's avatar
Bryant committed
1380
        rect.height() - dimAxes[QwtPlot::xBottom] - dimAxes[QwtPlot::xTop] );
pixhawk's avatar
pixhawk committed
1381

Bryant's avatar
Bryant committed
1382 1383
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
pixhawk's avatar
pixhawk committed
1384 1385
        // set the rects for the axes

Bryant's avatar
Bryant committed
1386 1387
        if ( dimAxes[axis] )
        {
pixhawk's avatar
pixhawk committed
1388
            int dim = dimAxes[axis];
Bryant's avatar
Bryant committed
1389
            QRectF &scaleRect = d_data->scaleRect[axis];
pixhawk's avatar
pixhawk committed
1390 1391

            scaleRect = d_data->canvasRect;
Bryant's avatar
Bryant committed
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
            switch ( axis )
            {
                case QwtPlot::yLeft:
                    scaleRect.setX( d_data->canvasRect.left() - dim );
                    scaleRect.setWidth( dim );
                    break;
                case QwtPlot::yRight:
                    scaleRect.setX( d_data->canvasRect.right() );
                    scaleRect.setWidth( dim );
                    break;
                case QwtPlot::xBottom:
                    scaleRect.setY( d_data->canvasRect.bottom() );
                    scaleRect.setHeight( dim );
                    break;
                case QwtPlot::xTop:
                    scaleRect.setY( d_data->canvasRect.top() - dim );
                    scaleRect.setHeight( dim );
                    break;
pixhawk's avatar
pixhawk committed
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
            }
            scaleRect = scaleRect.normalized();
        }
    }

    // +---+-----------+---+
    // |  <-   Axis   ->   |
    // +-^-+-----------+-^-+
    // | | |           | | |
    // |   |           |   |
    // | A |           | A |
    // | x |  Canvas   | x |
    // | i |           | i |
    // | s |           | s |
    // |   |           |   |
    // | | |           | | |
    // +-V-+-----------+-V-+
    // |   <-  Axis   ->   |
    // +---+-----------+---+

    // The ticks of the axes - not the labels above - should
    // be aligned to the canvas. So we try to use the empty
    // corners to extend the axes, so that the label texts
    // left/right of the min/max ticks are moved into them.
1434

Bryant's avatar
Bryant committed
1435
    alignScales( options, d_data->canvasRect, d_data->scaleRect );
pixhawk's avatar
pixhawk committed
1436

Bryant's avatar
Bryant committed
1437 1438
    if ( !d_data->legendRect.isEmpty() )
    {
pixhawk's avatar
pixhawk committed
1439 1440 1441
        // We prefer to align the legend to the canvas - not to
        // the complete plot - if possible.

Bryant's avatar
Bryant committed
1442
        d_data->legendRect = alignLegend( d_data->canvasRect, d_data->legendRect );
pixhawk's avatar
pixhawk committed
1443 1444
    }
}