qwt_plot_layout.cpp 34.9 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* -*- 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
 *****************************************************************************/

// vim: expandtab

#include <qscrollbar.h>
#include "qwt_text.h"
#include "qwt_text_label.h"
#include "qwt_plot_canvas.h"
#include "qwt_scale_widget.h"
#include "qwt_legend.h"
#include "qwt_plot_layout.h"

class QwtPlotLayout::LayoutData
{
public:
    void init(const QwtPlot *, const QRect &rect);

25
    struct t_legendData {
pixhawk's avatar
pixhawk committed
26 27 28 29 30
        int frameWidth;
        int vScrollBarWidth;
        int hScrollBarHeight;
        QSize hint;
    } legend;
31 32

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

37
    struct t_scaleData {
pixhawk's avatar
pixhawk committed
38 39 40 41 42 43
        bool isEnabled;
        const QwtScaleWidget *scaleWidget;
        QFont scaleFont;
        int start;
        int end;
        int baseLineOffset;
44
        int tickOffset;
pixhawk's avatar
pixhawk committed
45 46 47
        int dimWithoutTitle;
    } scale[QwtPlot::axisCnt];

48
    struct t_canvasData {
pixhawk's avatar
pixhawk committed
49 50 51 52 53 54 55 56 57 58 59 60
        int frameWidth;
    } canvas;
};

/*
  Extract all layout relevant data from the plot components
*/

void QwtPlotLayout::LayoutData::init(const QwtPlot *plot, const QRect &rect)
{
    // legend

61 62
    if ( plot->plotLayout()->legendPosition() != QwtPlot::ExternalLegend
            && plot->legend() ) {
pixhawk's avatar
pixhawk committed
63
        legend.frameWidth = plot->legend()->frameWidth();
64
        legend.vScrollBarWidth =
pixhawk's avatar
pixhawk committed
65
            plot->legend()->verticalScrollBar()->sizeHint().width();
66
        legend.hScrollBarHeight =
pixhawk's avatar
pixhawk committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
            plot->legend()->horizontalScrollBar()->sizeHint().height();

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

        int w = qwtMin(hint.width(), rect.width());
        int h = plot->legend()->heightForWidth(w);
        if ( h == 0 )
            h = hint.height();

        if ( h > rect.height() )
            w += legend.vScrollBarWidth;

        legend.hint = QSize(w, h);
    }

82
    // title
pixhawk's avatar
pixhawk committed
83 84 85 86

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

87
    if (plot->titleLabel() ) {
pixhawk's avatar
pixhawk committed
88
        const QwtTextLabel *label = plot->titleLabel();
89
        title.text = label->text();
pixhawk's avatar
pixhawk committed
90 91
        if ( !(title.text.testPaintAttribute(QwtText::PaintUsingTextFont)) )
            title.text.setFont(label->font());
92

pixhawk's avatar
pixhawk committed
93 94 95
        title.frameWidth = plot->titleLabel()->frameWidth();
    }

96
    // scales
pixhawk's avatar
pixhawk committed
97

98 99
    for (int axis = 0; axis < QwtPlot::axisCnt; axis++ ) {
        if ( plot->axisEnabled(axis) ) {
pixhawk's avatar
pixhawk committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113
            const QwtScaleWidget *scaleWidget = plot->axisWidget(axis);

            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(
114 115
                        QwtAbstractScaleDraw::Ticks) ) {
                scale[axis].tickOffset +=
pixhawk's avatar
pixhawk committed
116 117 118 119
                    (int)scaleWidget->scaleDraw()->majTickLength();
            }

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

122 123
            if ( !scaleWidget->title().isEmpty() ) {
                scale[axis].dimWithoutTitle -=
pixhawk's avatar
pixhawk committed
124 125
                    scaleWidget->titleHeightForWidth(QWIDGETSIZE_MAX);
            }
126
        } else {
pixhawk's avatar
pixhawk committed
127 128 129 130 131 132 133 134 135
            scale[axis].isEnabled = false;
            scale[axis].start = 0;
            scale[axis].end = 0;
            scale[axis].baseLineOffset = 0;
            scale[axis].tickOffset = 0;
            scale[axis].dimWithoutTitle = 0;
        }
    }

136
    // canvas
pixhawk's avatar
pixhawk committed
137 138 139 140 141 142 143 144 145 146

    canvas.frameWidth = plot->canvas()->frameWidth();
}

class QwtPlotLayout::PrivateData
{
public:
    PrivateData():
        margin(0),
        spacing(5),
147
        alignCanvasToScales(false) {
pixhawk's avatar
pixhawk committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    }

    QRect titleRect;
    QRect legendRect;
    QRect scaleRect[QwtPlot::axisCnt];
    QRect canvasRect;

    QwtPlotLayout::LayoutData layoutData;

    QwtPlot::LegendPosition legendPos;
    double legendRatio;
    unsigned int margin;
    unsigned int spacing;
    unsigned int canvasMargin[QwtPlot::axisCnt];
    bool alignCanvasToScales;
};

/*!
  \brief Constructor
 */

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

    setLegendPosition(QwtPlot::BottomLegend);
    setCanvasMargin(4);

    invalidate();
}

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

/*!
  Change the margin of the plot. The margin is the space
  around all components.
188

pixhawk's avatar
pixhawk committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  \param margin new margin
  \sa margin(), setSpacing(),
      QwtPlot::setMargin()
*/
void QwtPlotLayout::setMargin(int margin)
{
    if ( margin < 0 )
        margin = 0;
    d_data->margin = margin;
}

/*!
    \return margin
    \sa setMargin(), spacing(), QwtPlot::margin()
*/
int QwtPlotLayout::margin() const
{
    return d_data->margin;
}

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

pixhawk's avatar
pixhawk committed
214
  \param margin New margin
215
  \param axis One of QwtPlot::Axis. Specifies where the position of the margin.
pixhawk's avatar
pixhawk committed
216
              -1 means margin at all borders.
217
  \sa canvasMargin()
pixhawk's avatar
pixhawk committed
218 219 220 221 222 223 224 225 226

  \warning The margin will have no effect when alignCanvasToScales is true
*/

void QwtPlotLayout::setCanvasMargin(int margin, int axis)
{
    if ( margin < -1 )
        margin = -1;

227
    if ( axis == -1 ) {
pixhawk's avatar
pixhawk committed
228 229
        for (axis = 0; axis < QwtPlot::axisCnt; axis++)
            d_data->canvasMargin[axis] = margin;
230
    } else if ( axis >= 0 && axis < QwtPlot::axisCnt )
pixhawk's avatar
pixhawk committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        d_data->canvasMargin[axis] = margin;
}

/*!
    \return Margin around the scale tick borders
    \sa setCanvasMargin()
*/
int QwtPlotLayout::canvasMargin(int axis) const
{
    if ( axis < 0 || axis >= QwtPlot::axisCnt )
        return 0;

    return d_data->canvasMargin[axis];
}

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

  \param alignCanvasToScales New align-canvas-to-axis-scales setting

253
  \sa setCanvasMargin()
pixhawk's avatar
pixhawk committed
254
  \note In this context the term 'scale' means the backbone of a scale.
255
  \warning In case of alignCanvasToScales == true canvasMargin will have
pixhawk's avatar
pixhawk committed
256 257 258 259 260 261 262 263 264 265 266 267 268
           no effect
*/
void QwtPlotLayout::setAlignCanvasToScales(bool alignCanvasToScales)
{
    d_data->alignCanvasToScales = alignCanvasToScales;
}

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

  \return align-canvas-to-axis-scales setting
269
  \sa setAlignCanvasToScales, setCanvasMargin()
pixhawk's avatar
pixhawk committed
270 271 272 273 274 275 276 277 278 279
  \note In this context the term 'scale' means the backbone of a scale.
*/
bool QwtPlotLayout::alignCanvasToScales() const
{
    return d_data->alignCanvasToScales;
}

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

pixhawk's avatar
pixhawk committed
281
  \param spacing new spacing
282
  \sa setMargin(), spacing()
pixhawk's avatar
pixhawk committed
283 284 285 286 287 288 289 290
*/
void QwtPlotLayout::setSpacing(int spacing)
{
    d_data->spacing = qwtMax(0, spacing);
}

/*!
  \return spacing
291
  \sa margin(), setSpacing()
pixhawk's avatar
pixhawk committed
292 293 294 295 296 297 298 299
*/
int QwtPlotLayout::spacing() const
{
    return d_data->spacing;
}

/*!
  \brief Specify the position of the legend
300 301
  \param pos The legend's position.
  \param ratio Ratio between legend and the bounding rect
pixhawk's avatar
pixhawk committed
302 303 304 305
               of title, canvas and axes. The legend will be shrinked
               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.
306 307
               The default vertical/horizontal ratio is 0.33/0.5.

pixhawk's avatar
pixhawk committed
308 309 310 311 312 313 314 315
  \sa QwtPlot::setLegendPosition()
*/

void QwtPlotLayout::setLegendPosition(QwtPlot::LegendPosition pos, double ratio)
{
    if ( ratio > 1.0 )
        ratio = 1.0;

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    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;
    case QwtPlot::ExternalLegend:
        d_data->legendRatio = ratio; // meaningless
        d_data->legendPos = pos;
    default:
        break;
pixhawk's avatar
pixhawk committed
336 337 338 339 340
    }
}

/*!
  \brief Specify the position of the legend
341 342
  \param pos The legend's position. Valid values are
      \c QwtPlot::LeftLegend, \c QwtPlot::RightLegend,
pixhawk's avatar
pixhawk committed
343
      \c QwtPlot::TopLegend, \c QwtPlot::BottomLegend.
344

pixhawk's avatar
pixhawk committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  \sa QwtPlot::setLegendPosition()
*/
void QwtPlotLayout::setLegendPosition(QwtPlot::LegendPosition pos)
{
    setLegendPosition(pos, 0.0);
}

/*!
  \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
364
  \param ratio Ratio between legend and the bounding rect
pixhawk's avatar
pixhawk committed
365 366 367 368
               of title, canvas and axes. The legend will be shrinked
               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.
369
               The default vertical/horizontal ratio is 0.33/0.5.
pixhawk's avatar
pixhawk committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
*/
void QwtPlotLayout::setLegendRatio(double ratio)
{
    setLegendPosition(legendPosition(), ratio);
}

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

/*!
  \return Geometry for the title
  \sa activate(), invalidate()
*/

const QRect &QwtPlotLayout::titleRect() const
{
    return d_data->titleRect;
}

/*!
  \return Geometry for the legend
  \sa activate(), invalidate()
*/

const QRect &QwtPlotLayout::legendRect() const
{
    return d_data->legendRect;
}

/*!
  \param axis Axis index
  \return Geometry for the scale
  \sa activate(), invalidate()
*/

const QRect &QwtPlotLayout::scaleRect(int axis) const
{
413
    if ( axis < 0 || axis >= QwtPlot::axisCnt ) {
pixhawk's avatar
pixhawk committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
        static QRect dummyRect;
        return dummyRect;
    }
    return d_data->scaleRect[axis];
}

/*!
  \return Geometry for the canvas
  \sa activate(), invalidate()
*/

const QRect &QwtPlotLayout::canvasRect() const
{
    return d_data->canvasRect;
}

/*!
431
  Invalidate the geometry of all components.
pixhawk's avatar
pixhawk committed
432 433 434 435 436 437 438 439 440
  \sa activate()
*/
void QwtPlotLayout::invalidate()
{
    d_data->titleRect = d_data->legendRect = d_data->canvasRect = QRect();
    for (int axis = 0; axis < QwtPlot::axisCnt; axis++ )
        d_data->scaleRect[axis] = QRect();
}

441
/*!
pixhawk's avatar
pixhawk committed
442 443 444 445 446 447 448 449 450
  \brief Return a minimum size hint
  \sa QwtPlot::minimumSizeHint()
*/

QSize QwtPlotLayout::minimumSizeHint(const QwtPlot *plot) const
{
    class ScaleData
    {
    public:
451
        ScaleData() {
pixhawk's avatar
pixhawk committed
452 453 454 455 456 457 458 459 460 461 462 463 464
            w = h = minLeft = minRight = tickOffset = 0;
        }

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

    int canvasBorder[QwtPlot::axisCnt];

    int axis;
465 466
    for ( axis = 0; axis < QwtPlot::axisCnt; axis++ ) {
        if ( plot->axisEnabled(axis) ) {
pixhawk's avatar
pixhawk committed
467 468 469 470
            const QwtScaleWidget *scl = plot->axisWidget(axis);
            ScaleData &sd = scaleData[axis];

            const QSize hint = scl->minimumSizeHint();
471 472
            sd.w = hint.width();
            sd.h = hint.height();
pixhawk's avatar
pixhawk committed
473 474 475 476 477 478 479
            scl->getBorderDistHint(sd.minLeft, sd.minRight);
            sd.tickOffset = scl->margin();
            if ( scl->scaleDraw()->hasComponent(QwtAbstractScaleDraw::Ticks) )
                sd.tickOffset += scl->scaleDraw()->majTickLength();
        }

        canvasBorder[axis] = plot->canvas()->frameWidth() +
480 481
                             d_data->canvasMargin[axis] + 1;

pixhawk's avatar
pixhawk committed
482 483 484
    }


485
    for ( axis = 0; axis < QwtPlot::axisCnt; axis++ ) {
pixhawk's avatar
pixhawk committed
486
        ScaleData &sd = scaleData[axis];
487 488 489
        if ( sd.w && (axis == QwtPlot::xBottom || axis == QwtPlot::xTop) ) {
            if ( (sd.minLeft > canvasBorder[QwtPlot::yLeft])
                    && scaleData[QwtPlot::yLeft].w ) {
pixhawk's avatar
pixhawk committed
490 491 492 493 494 495
                int shiftLeft = sd.minLeft - canvasBorder[QwtPlot::yLeft];
                if ( shiftLeft > scaleData[QwtPlot::yLeft].w )
                    shiftLeft = scaleData[QwtPlot::yLeft].w;

                sd.w -= shiftLeft;
            }
496 497
            if ( (sd.minRight > canvasBorder[QwtPlot::yRight])
                    && scaleData[QwtPlot::yRight].w ) {
pixhawk's avatar
pixhawk committed
498 499 500 501 502 503 504 505
                int shiftRight = sd.minRight - canvasBorder[QwtPlot::yRight];
                if ( shiftRight > scaleData[QwtPlot::yRight].w )
                    shiftRight = scaleData[QwtPlot::yRight].w;

                sd.w -= shiftRight;
            }
        }

506
        if ( sd.h && (axis == QwtPlot::yLeft || axis == QwtPlot::yRight) ) {
pixhawk's avatar
pixhawk committed
507
            if ( (sd.minLeft > canvasBorder[QwtPlot::xBottom]) &&
508
                    scaleData[QwtPlot::xBottom].h ) {
pixhawk's avatar
pixhawk committed
509 510 511 512 513 514 515
                int shiftBottom = sd.minLeft - canvasBorder[QwtPlot::xBottom];
                if ( shiftBottom > scaleData[QwtPlot::xBottom].tickOffset )
                    shiftBottom = scaleData[QwtPlot::xBottom].tickOffset;

                sd.h -= shiftBottom;
            }
            if ( (sd.minLeft > canvasBorder[QwtPlot::xTop]) &&
516
                    scaleData[QwtPlot::xTop].h ) {
pixhawk's avatar
pixhawk committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530
                int shiftTop = sd.minRight - canvasBorder[QwtPlot::xTop];
                if ( shiftTop > scaleData[QwtPlot::xTop].tickOffset )
                    shiftTop = scaleData[QwtPlot::xTop].tickOffset;

                sd.h -= shiftTop;
            }
        }
    }

    const QwtPlotCanvas *canvas = plot->canvas();
    const QSize minCanvasSize = canvas->minimumSize();

    int w = scaleData[QwtPlot::yLeft].w + scaleData[QwtPlot::yRight].w;
    int cw = qwtMax(scaleData[QwtPlot::xBottom].w, scaleData[QwtPlot::xTop].w)
531
             + 2 * (canvas->frameWidth() + 1);
pixhawk's avatar
pixhawk committed
532 533 534 535
    w += qwtMax(cw, minCanvasSize.width());

    int h = scaleData[QwtPlot::xBottom].h + scaleData[QwtPlot::xTop].h;
    int ch = qwtMax(scaleData[QwtPlot::yLeft].h, scaleData[QwtPlot::yRight].h)
536
             + 2 * (canvas->frameWidth() + 1);
pixhawk's avatar
pixhawk committed
537 538 539
    h += qwtMax(ch, minCanvasSize.height());

    const QwtTextLabel *title = plot->titleLabel();
540 541
    if (title && !title->text().isEmpty()) {
        // If only QwtPlot::yLeft or QwtPlot::yRight is showing,
pixhawk's avatar
pixhawk committed
542
        // we center on the plot canvas.
543 544
        const bool centerOnCanvas = !(plot->axisEnabled(QwtPlot::yLeft)
                                      && plot->axisEnabled(QwtPlot::yRight));
pixhawk's avatar
pixhawk committed
545 546

        int titleW = w;
547 548 549
        if ( centerOnCanvas ) {
            titleW -= scaleData[QwtPlot::yLeft].w
                      + scaleData[QwtPlot::yRight].w;
pixhawk's avatar
pixhawk committed
550 551 552
        }

        int titleH = title->heightForWidth(titleW);
553
        if ( titleH > titleW ) { // Compensate for a long title
pixhawk's avatar
pixhawk committed
554
            w = titleW = titleH;
555
            if ( centerOnCanvas ) {
pixhawk's avatar
pixhawk committed
556
                w += scaleData[QwtPlot::yLeft].w
557
                     + scaleData[QwtPlot::yRight].w;
pixhawk's avatar
pixhawk committed
558 559 560 561 562 563 564 565 566 567 568
            }

            titleH = title->heightForWidth(titleW);
        }
        h += titleH + d_data->spacing;
    }

    // Compute the legend contribution

    const QwtLegend *legend = plot->legend();
    if ( d_data->legendPos != QwtPlot::ExternalLegend
569 570 571
            && legend && !legend->isEmpty() ) {
        if ( d_data->legendPos == QwtPlot::LeftLegend
                || d_data->legendPos == QwtPlot::RightLegend ) {
pixhawk's avatar
pixhawk committed
572
            int legendW = legend->sizeHint().width();
573
            int legendH = legend->heightForWidth(legendW);
pixhawk's avatar
pixhawk committed
574 575 576 577 578 579 580 581 582 583 584

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

            if ( legendH > h )
                legendW += legend->verticalScrollBar()->sizeHint().height();

            if ( d_data->legendRatio < 1.0 )
                legendW = qwtMin(legendW, int(w / (1.0 - d_data->legendRatio)));

            w += legendW;
585
        } else { // QwtPlot::Top, QwtPlot::Bottom
pixhawk's avatar
pixhawk committed
586
            int legendW = qwtMin(legend->sizeHint().width(), w);
587
            int legendH = legend->heightForWidth(legendW);
pixhawk's avatar
pixhawk committed
588 589 590 591 592 593

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

            if ( d_data->legendRatio < 1.0 )
                legendH = qwtMin(legendH, int(h / (1.0 - d_data->legendRatio)));
594

pixhawk's avatar
pixhawk committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
            h += legendH;
        }
    }

    w += 2 * d_data->margin;
    h += 2 * d_data->margin;

    return QSize( w, h );
}

/*!
  Find the geometry for the legend
  \param options Options how to layout the legend
  \param rect Rectangle where to place the legend
  \return Geometry for the legend
*/

612 613
QRect QwtPlotLayout::layoutLegend(int options,
                                  const QRect &rect) const
pixhawk's avatar
pixhawk committed
614 615 616 617
{
    const QSize hint(d_data->layoutData.legend.hint);

    int dim;
618 619
    if ( d_data->legendPos == QwtPlot::LeftLegend
            || d_data->legendPos == QwtPlot::RightLegend ) {
pixhawk's avatar
pixhawk committed
620 621 622 623 624
        // We don't allow vertical legends to take more than
        // half of the available space.

        dim = qwtMin(hint.width(), int(rect.width() * d_data->legendRatio));

625 626
        if ( !(options & IgnoreScrollbars) ) {
            if ( hint.height() > rect.height() ) {
pixhawk's avatar
pixhawk committed
627
                // The legend will need additional
628
                // space for the vertical scrollbar.
pixhawk's avatar
pixhawk committed
629 630 631 632

                dim += d_data->layoutData.legend.vScrollBarWidth;
            }
        }
633
    } else {
pixhawk's avatar
pixhawk committed
634 635 636 637 638
        dim = qwtMin(hint.height(), int(rect.height() * d_data->legendRatio));
        dim = qwtMax(dim, d_data->layoutData.legend.hScrollBarHeight);
    }

    QRect legendRect = rect;
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    switch(d_data->legendPos) {
    case QwtPlot::LeftLegend:
        legendRect.setWidth(dim);
        break;
    case QwtPlot::RightLegend:
        legendRect.setX(rect.right() - dim + 1);
        legendRect.setWidth(dim);
        break;
    case QwtPlot::TopLegend:
        legendRect.setHeight(dim);
        break;
    case QwtPlot::BottomLegend:
        legendRect.setY(rect.bottom() - dim + 1);
        legendRect.setHeight(dim);
        break;
    case QwtPlot::ExternalLegend:
        break;
pixhawk's avatar
pixhawk committed
656 657 658 659 660 661 662 663 664 665 666
    }

    return legendRect;
}

/*!
  Align the legend to the canvas
  \param canvasRect Geometry of the canvas
  \param legendRect Maximum geometry for the legend
  \return Geometry for the aligned legend
*/
667 668
QRect QwtPlotLayout::alignLegend(const QRect &canvasRect,
                                 const QRect &legendRect) const
pixhawk's avatar
pixhawk committed
669 670 671
{
    QRect alignedRect = legendRect;

672 673 674
    if ( d_data->legendPos == QwtPlot::BottomLegend
            || d_data->legendPos == QwtPlot::TopLegend ) {
        if ( d_data->layoutData.legend.hint.width() < canvasRect.width() ) {
pixhawk's avatar
pixhawk committed
675 676 677
            alignedRect.setX(canvasRect.x());
            alignedRect.setWidth(canvasRect.width());
        }
678 679
    } else {
        if ( d_data->layoutData.legend.hint.height() < canvasRect.height() ) {
pixhawk's avatar
pixhawk committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
            alignedRect.setY(canvasRect.y());
            alignedRect.setHeight(canvasRect.height());
        }
    }

    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
  \param rect Bounding rect for title, axes and canvas.
  \param dimTitle Expanded height of the title widget
  \param dimAxis Expanded heights of the axis in axis orientation.
*/
697 698
void QwtPlotLayout::expandLineBreaks(int options, const QRect &rect,
                                     int &dimTitle, int dimAxis[QwtPlot::axisCnt]) const
pixhawk's avatar
pixhawk committed
699 700 701 702 703 704
{
    dimTitle = 0;
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
        dimAxis[axis] = 0;

    int backboneOffset[QwtPlot::axisCnt];
705
    for (int axis = 0; axis < QwtPlot::axisCnt; axis++ ) {
pixhawk's avatar
pixhawk committed
706 707 708 709 710 711 712 713
        backboneOffset[axis] = 0;
        if ( !d_data->alignCanvasToScales )
            backboneOffset[axis] += d_data->canvasMargin[axis];
        if ( !(options & IgnoreFrames) )
            backboneOffset[axis] += d_data->layoutData.canvas.frameWidth;
    }

    bool done = false;
714
    while (!done) {
pixhawk's avatar
pixhawk committed
715 716 717 718 719 720 721 722 723 724
        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.

725
        if ( !d_data->layoutData.title.text.isEmpty() ) {
pixhawk's avatar
pixhawk committed
726 727 728
            int w = rect.width();

            if ( d_data->layoutData.scale[QwtPlot::yLeft].isEnabled
729
                    != d_data->layoutData.scale[QwtPlot::yRight].isEnabled ) {
pixhawk's avatar
pixhawk committed
730
                // center to the canvas
731
                w -= dimAxis[QwtPlot::yLeft] + dimAxis[QwtPlot::yRight];
pixhawk's avatar
pixhawk committed
732 733 734 735 736 737
            }

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

738
            if ( d > dimTitle ) {
pixhawk's avatar
pixhawk committed
739 740 741 742 743
                dimTitle = d;
                done = false;
            }
        }

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

748
            if (scaleData.isEnabled) {
pixhawk's avatar
pixhawk committed
749
                int length;
750 751 752
                if ( axis == QwtPlot::xTop || axis == QwtPlot::xBottom ) {
                    length = rect.width() - dimAxis[QwtPlot::yLeft]
                             - dimAxis[QwtPlot::yRight];
pixhawk's avatar
pixhawk committed
753 754 755 756 757
                    length -= scaleData.start + scaleData.end;

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

758 759 760 761 762 763 764
                    length += qwtMin(dimAxis[QwtPlot::yLeft],
                                     scaleData.start - backboneOffset[QwtPlot::yLeft]);
                    length += qwtMin(dimAxis[QwtPlot::yRight],
                                     scaleData.end - backboneOffset[QwtPlot::yRight]);
                } else { // QwtPlot::yLeft, QwtPlot::yRight
                    length = rect.height() - dimAxis[QwtPlot::xTop]
                             - dimAxis[QwtPlot::xBottom];
pixhawk's avatar
pixhawk committed
765 766 767 768 769 770 771 772
                    length -= scaleData.start + scaleData.end;
                    length -= 1;

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

773
                    if ( dimAxis[QwtPlot::xBottom] > 0 ) {
pixhawk's avatar
pixhawk committed
774
                        length += qwtMin(
775 776
                                      d_data->layoutData.scale[QwtPlot::xBottom].tickOffset,
                                      scaleData.start - backboneOffset[QwtPlot::xBottom]);
pixhawk's avatar
pixhawk committed
777
                    }
778
                    if ( dimAxis[QwtPlot::xTop] > 0 ) {
pixhawk's avatar
pixhawk committed
779
                        length += qwtMin(
780 781
                                      d_data->layoutData.scale[QwtPlot::xTop].tickOffset,
                                      scaleData.end - backboneOffset[QwtPlot::xTop]);
pixhawk's avatar
pixhawk committed
782 783 784 785 786 787 788
                    }

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

                int d = scaleData.dimWithoutTitle;
789
                if ( !scaleData.scaleWidget->title().isEmpty() ) {
pixhawk's avatar
pixhawk committed
790 791 792 793
                    d += scaleData.scaleWidget->titleHeightForWidth(length);
                }


794
                if ( d > dimAxis[axis] ) {
pixhawk's avatar
pixhawk committed
795 796 797 798 799 800 801 802 803 804 805 806 807 808
                    dimAxis[axis] = d;
                    done = false;
                }
            }
        }
    }
}

/*!
  Align the ticks of the axis to the canvas borders using
  the empty corners.
*/

void QwtPlotLayout::alignScales(int options,
809
                                QRect &canvasRect, QRect scaleRect[QwtPlot::axisCnt]) const
pixhawk's avatar
pixhawk committed
810 811 812 813
{
    int axis;

    int backboneOffset[QwtPlot::axisCnt];
814
    for (axis = 0; axis < QwtPlot::axisCnt; axis++ ) {
pixhawk's avatar
pixhawk committed
815 816 817 818 819 820 821
        backboneOffset[axis] = 0;
        if ( !d_data->alignCanvasToScales )
            backboneOffset[axis] += d_data->canvasMargin[axis];
        if ( !(options & IgnoreFrames) )
            backboneOffset[axis] += d_data->layoutData.canvas.frameWidth;
    }

822
    for (axis = 0; axis < QwtPlot::axisCnt; axis++ ) {
pixhawk's avatar
pixhawk committed
823 824 825 826 827 828 829 830
        if ( !scaleRect[axis].isValid() )
            continue;

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

        QRect &axisRect = scaleRect[axis];

831 832
        if ( axis == QwtPlot::xTop || axis == QwtPlot::xBottom ) {
            const int leftOffset =
pixhawk's avatar
pixhawk committed
833 834
                backboneOffset[QwtPlot::yLeft] - startDist;

835
            if ( scaleRect[QwtPlot::yLeft].isValid() ) {
pixhawk's avatar
pixhawk committed
836 837 838
                int minLeft = scaleRect[QwtPlot::yLeft].left();
                int left = axisRect.left() + leftOffset;
                axisRect.setLeft(qwtMax(left, minLeft));
839 840 841 842 843
            } else {
                if ( d_data->alignCanvasToScales && leftOffset < 0 ) {
                    canvasRect.setLeft(qwtMax(canvasRect.left(),
                                              axisRect.left() - leftOffset));
                } else {
pixhawk's avatar
pixhawk committed
844 845 846 847 848
                    if ( leftOffset > 0 )
                        axisRect.setLeft(axisRect.left() + leftOffset);
                }
            }

849
            const int rightOffset =
pixhawk's avatar
pixhawk committed
850 851
                backboneOffset[QwtPlot::yRight] - endDist + 1;

852
            if ( scaleRect[QwtPlot::yRight].isValid() ) {
pixhawk's avatar
pixhawk committed
853 854 855
                int maxRight = scaleRect[QwtPlot::yRight].right();
                int right = axisRect.right() - rightOffset;
                axisRect.setRight(qwtMin(right, maxRight));
856 857 858 859 860
            } else {
                if ( d_data->alignCanvasToScales && rightOffset < 0 ) {
                    canvasRect.setRight( qwtMin(canvasRect.right(),
                                                axisRect.right() + rightOffset) );
                } else {
pixhawk's avatar
pixhawk committed
861 862 863 864
                    if ( rightOffset > 0 )
                        axisRect.setRight(axisRect.right() - rightOffset);
                }
            }
865 866
        } else { // QwtPlot::yLeft, QwtPlot::yRight
            const int bottomOffset =
pixhawk's avatar
pixhawk committed
867 868
                backboneOffset[QwtPlot::xBottom] - endDist + 1;

869 870 871
            if ( scaleRect[QwtPlot::xBottom].isValid() ) {
                int maxBottom = scaleRect[QwtPlot::xBottom].top() +
                                d_data->layoutData.scale[QwtPlot::xBottom].tickOffset;
pixhawk's avatar
pixhawk committed
872 873 874

                int bottom = axisRect.bottom() - bottomOffset;
                axisRect.setBottom(qwtMin(bottom, maxBottom));
875 876 877 878 879
            } else {
                if ( d_data->alignCanvasToScales && bottomOffset < 0 ) {
                    canvasRect.setBottom(qwtMin(canvasRect.bottom(),
                                                axisRect.bottom() + bottomOffset));
                } else {
pixhawk's avatar
pixhawk committed
880 881 882 883
                    if ( bottomOffset > 0 )
                        axisRect.setBottom(axisRect.bottom() - bottomOffset);
                }
            }
884

pixhawk's avatar
pixhawk committed
885 886
            const int topOffset = backboneOffset[QwtPlot::xTop] - startDist;

887
            if ( scaleRect[QwtPlot::xTop].isValid() ) {
pixhawk's avatar
pixhawk committed
888
                int minTop = scaleRect[QwtPlot::xTop].bottom() -
889
                             d_data->layoutData.scale[QwtPlot::xTop].tickOffset;
pixhawk's avatar
pixhawk committed
890 891 892

                int top = axisRect.top() + topOffset;
                axisRect.setTop(qwtMax(top, minTop));
893 894 895 896 897
            } else {
                if ( d_data->alignCanvasToScales && topOffset < 0 ) {
                    canvasRect.setTop(qwtMax(canvasRect.top(),
                                             axisRect.top() - topOffset));
                } else {
pixhawk's avatar
pixhawk committed
898 899 900 901 902 903 904
                    if ( topOffset > 0 )
                        axisRect.setTop(axisRect.top() + topOffset);
                }
            }
        }
    }

905
    if ( d_data->alignCanvasToScales ) {
pixhawk's avatar
pixhawk committed
906 907 908 909 910 911 912 913 914 915
        /*
          The canvas has been aligned to the scale with largest
          border distances. Now we have to realign the other scale.
         */

        int fw = 0;
        if ( !(options & IgnoreFrames) )
            fw = d_data->layoutData.canvas.frameWidth;

        if ( scaleRect[QwtPlot::xBottom].isValid() &&
916 917
                scaleRect[QwtPlot::xTop].isValid() ) {
            for ( int axis = QwtPlot::xBottom; axis <= QwtPlot::xTop; axis++ ) {
pixhawk's avatar
pixhawk committed
918
                scaleRect[axis].setLeft(canvasRect.left() + fw
919
                                        - d_data->layoutData.scale[axis].start);
pixhawk's avatar
pixhawk committed
920
                scaleRect[axis].setRight(canvasRect.right() - fw - 1
921
                                         + d_data->layoutData.scale[axis].end);
pixhawk's avatar
pixhawk committed
922 923 924 925
            }
        }

        if ( scaleRect[QwtPlot::yLeft].isValid() &&
926 927
                scaleRect[QwtPlot::yRight].isValid() ) {
            for ( int axis = QwtPlot::yLeft; axis <= QwtPlot::yRight; axis++ ) {
pixhawk's avatar
pixhawk committed
928
                scaleRect[axis].setTop(canvasRect.top() + fw
929
                                       - d_data->layoutData.scale[axis].start);
pixhawk's avatar
pixhawk committed
930
                scaleRect[axis].setBottom(canvasRect.bottom() - fw - 1
931
                                          + d_data->layoutData.scale[axis].end);
pixhawk's avatar
pixhawk committed
932 933 934 935 936 937
            }
        }
    }
}

/*!
938
  \brief Recalculate the geometry of all components.
pixhawk's avatar
pixhawk committed
939 940 941 942 943 944 945 946 947

  \param plot Plot to be layout
  \param plotRect Rect where to place the components
  \param options Options

  \sa invalidate(), titleRect(),
      legendRect(), scaleRect(), canvasRect()
*/
void QwtPlotLayout::activate(const QwtPlot *plot,
948
                             const QRect &plotRect, int options)
pixhawk's avatar
pixhawk committed
949 950 951 952 953
{
    invalidate();

    QRect rect(plotRect);  // undistributed rest of the plot rect

954
    if ( !(options & IgnoreMargin) ) {
pixhawk's avatar
pixhawk committed
955 956 957
        // subtract the margin

        rect.setRect(
958
            rect.x() + d_data->margin,
pixhawk's avatar
pixhawk committed
959
            rect.y() + d_data->margin,
960
            rect.width() - 2 * d_data->margin,
pixhawk's avatar
pixhawk committed
961 962 963 964 965 966 967 968 969
            rect.height() - 2 * d_data->margin
        );
    }

    // We extract all layout relevant data from the widgets,
    // filter them through pfilter and save them to d_data->layoutData.

    d_data->layoutData.init(plot, rect);

970 971 972
    if (!(options & IgnoreLegend)
            && d_data->legendPos != QwtPlot::ExternalLegend
            && plot->legend() && !plot->legend()->isEmpty() ) {
pixhawk's avatar
pixhawk committed
973 974 975 976 977
        d_data->legendRect = layoutLegend(options, rect);

        // subtract d_data->legendRect from rect

        const QRegion region(rect);
978
        rect = region.subtract(d_data->legendRect).boundingRect();
pixhawk's avatar
pixhawk committed
979

980 981
        if ( d_data->layoutData.legend.frameWidth &&
                !(options & IgnoreFrames ) ) {
pixhawk's avatar
pixhawk committed
982 983 984 985
            // In case of a frame we have to insert a spacing.
            // Otherwise the leading of the font separates
            // legend and scale/canvas

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
            switch(d_data->legendPos) {
            case QwtPlot::LeftLegend:
                rect.setLeft(rect.left() + d_data->spacing);
                break;
            case QwtPlot::RightLegend:
                rect.setRight(rect.right() - d_data->spacing);
                break;
            case QwtPlot::TopLegend:
                rect.setTop(rect.top() + d_data->spacing);
                break;
            case QwtPlot::BottomLegend:
                rect.setBottom(rect.bottom() - d_data->spacing);
                break;
            case QwtPlot::ExternalLegend:
                break; // suppress compiler warning
pixhawk's avatar
pixhawk committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
            }
        }
    }

#ifdef __GNUC__
#endif
    /*
     +---+-----------+---+
     |       Title       |
     +---+-----------+---+
     |   |   Axis    |   |
     +---+-----------+---+
     | A |           | A |
     | x |  Canvas   | x |
     | i |           | i |
     | s |           | s |
     +---+-----------+---+
     |   |   Axis    |   |
     +---+-----------+---+
    */

    // axes and title include text labels. The height of each
    // label depends on its line breaks, that depend on the width
    // for the label. A line break in a horizontal text will reduce
1025
    // the available width for vertical texts and vice versa.
pixhawk's avatar
pixhawk committed
1026 1027 1028 1029 1030 1031
    // expandLineBreaks finds the height/width for title and axes
    // including all line breaks.

    int dimTitle, dimAxes[QwtPlot::axisCnt];
    expandLineBreaks(options, rect, dimTitle, dimAxes);

1032
    if (dimTitle > 0 ) {
pixhawk's avatar
pixhawk committed
1033
        d_data->titleRect = QRect(rect.x(), rect.y(),
1034
                                  rect.width(), dimTitle);
pixhawk's avatar
pixhawk committed
1035 1036

        if ( d_data->layoutData.scale[QwtPlot::yLeft].isEnabled !=
1037
                d_data->layoutData.scale[QwtPlot::yRight].isEnabled ) {
pixhawk's avatar
pixhawk committed
1038 1039 1040 1041
            // if only one of the y axes is missing we align
            // the title centered to the canvas

            d_data->titleRect.setX(rect.x() + dimAxes[QwtPlot::yLeft]);
1042 1043
            d_data->titleRect.setWidth(rect.width()
                                       - dimAxes[QwtPlot::yLeft] - dimAxes[QwtPlot::yRight]);
pixhawk's avatar
pixhawk committed
1044 1045
        }

1046
        // subtract title
pixhawk's avatar
pixhawk committed
1047 1048 1049 1050 1051 1052 1053 1054 1055
        rect.setTop(rect.top() + dimTitle + d_data->spacing);
    }

    d_data->canvasRect.setRect(
        rect.x() + dimAxes[QwtPlot::yLeft],
        rect.y() + dimAxes[QwtPlot::xTop],
        rect.width() - dimAxes[QwtPlot::yRight] - dimAxes[QwtPlot::yLeft],
        rect.height() - dimAxes[QwtPlot::xBottom] - dimAxes[QwtPlot::xTop]);

1056
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) {
pixhawk's avatar
pixhawk committed
1057 1058
        // set the rects for the axes

1059
        if ( dimAxes[axis] ) {
pixhawk's avatar
pixhawk committed
1060 1061 1062 1063
            int dim = dimAxes[axis];
            QRect &scaleRect = d_data->scaleRect[axis];

            scaleRect = d_data->canvasRect;
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
            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() + 1);
                scaleRect.setWidth(dim);
                break;
            case QwtPlot::xBottom:
                scaleRect.setY(d_data->canvasRect.bottom() + 1);
                scaleRect.setHeight(dim);
                break;
            case QwtPlot::xTop:
                scaleRect.setY(d_data->canvasRect.top() - dim);
                scaleRect.setHeight(dim);
                break;
pixhawk's avatar
pixhawk committed
1081 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 1108
            }
#if QT_VERSION < 0x040000
            scaleRect = scaleRect.normalize();
#else
            scaleRect = scaleRect.normalized();
#endif
        }
    }

    // +---+-----------+---+
    // |  <-   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.
1109

pixhawk's avatar
pixhawk committed
1110 1111
    alignScales(options, d_data->canvasRect, d_data->scaleRect);

1112
    if (!d_data->legendRect.isEmpty() ) {
pixhawk's avatar
pixhawk committed
1113 1114 1115 1116 1117 1118
        // We prefer to align the legend to the canvas - not to
        // the complete plot - if possible.

        d_data->legendRect = alignLegend(d_data->canvasRect, d_data->legendRect);
    }
}