Skip to content
qwt_plot_layout.cpp 42.9 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/* -*- 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
#include "qwt_plot_layout.h"
pixhawk's avatar
pixhawk committed
#include "qwt_text.h"
#include "qwt_text_label.h"
#include "qwt_scale_widget.h"
Bryant's avatar
Bryant committed
#include "qwt_abstract_legend.h"
#include <qscrollbar.h>
#include <qmath.h>
pixhawk's avatar
pixhawk committed

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

Bryant's avatar
Bryant committed
    struct t_legendData
    {
pixhawk's avatar
pixhawk committed
        int frameWidth;
Bryant's avatar
Bryant committed
        int hScrollExtent;
        int vScrollExtent;
pixhawk's avatar
pixhawk committed
        QSize hint;
    } legend;
Bryant's avatar
Bryant committed
    struct t_titleData
    {
pixhawk's avatar
pixhawk committed
        QwtText text;
        int frameWidth;
    } title;

Bryant's avatar
Bryant committed
    struct t_footerData
    {
        QwtText text;
        int frameWidth;
    } footer;

    struct t_scaleData
    {
pixhawk's avatar
pixhawk committed
        bool isEnabled;
        const QwtScaleWidget *scaleWidget;
        QFont scaleFont;
        int start;
        int end;
        int baseLineOffset;
Bryant's avatar
Bryant committed
        double tickOffset;
pixhawk's avatar
pixhawk committed
        int dimWithoutTitle;
    } scale[QwtPlot::axisCnt];

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

pixhawk's avatar
pixhawk committed
    } canvas;
};

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

Bryant's avatar
Bryant committed
    if ( plot->legend() )
    {
pixhawk's avatar
pixhawk committed
        legend.frameWidth = plot->legend()->frameWidth();
Bryant's avatar
Bryant committed
        legend.hScrollExtent =
            plot->legend()->scrollExtent( Qt::Horizontal );
        legend.vScrollExtent =
            plot->legend()->scrollExtent( Qt::Vertical );
pixhawk's avatar
pixhawk committed

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

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

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

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

pixhawk's avatar
pixhawk committed

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

Bryant's avatar
Bryant committed
    if ( plot->titleLabel() )
    {
pixhawk's avatar
pixhawk committed
        const QwtTextLabel *label = plot->titleLabel();
        title.text = label->text();
Bryant's avatar
Bryant committed
        if ( !( title.text.testPaintAttribute( QwtText::PaintUsingTextFont ) ) )
            title.text.setFont( label->font() );
pixhawk's avatar
pixhawk committed
        title.frameWidth = plot->titleLabel()->frameWidth();
    }

Bryant's avatar
Bryant committed
    // 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();
    }

pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
        if ( plot->axisEnabled( axis ) )
        {
            const QwtScaleWidget *scaleWidget = plot->axisWidget( axis );
pixhawk's avatar
pixhawk committed

            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
                QwtAbstractScaleDraw::Ticks ) )
            {
                scale[axis].tickOffset +=
Bryant's avatar
Bryant committed
                    scaleWidget->scaleDraw()->maxTickLength();
pixhawk's avatar
pixhawk committed
            }

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

Bryant's avatar
Bryant committed
            if ( !scaleWidget->title().isEmpty() )
            {
                scale[axis].dimWithoutTitle -=
Bryant's avatar
Bryant committed
                    scaleWidget->titleHeightForWidth( QWIDGETSIZE_MAX );
pixhawk's avatar
pixhawk committed
            }
Bryant's avatar
Bryant committed
        }
        else
        {
pixhawk's avatar
pixhawk committed
            scale[axis].isEnabled = false;
            scale[axis].start = 0;
            scale[axis].end = 0;
            scale[axis].baseLineOffset = 0;
Bryant's avatar
Bryant committed
            scale[axis].tickOffset = 0.0;
pixhawk's avatar
pixhawk committed
            scale[axis].dimWithoutTitle = 0;
        }
    }

pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    plot->canvas()->getContentsMargins( 
        &canvas.contentsMargins[ QwtPlot::yLeft ], 
        &canvas.contentsMargins[ QwtPlot::xTop ],
        &canvas.contentsMargins[ QwtPlot::yRight ],
        &canvas.contentsMargins[ QwtPlot::xBottom ] );
pixhawk's avatar
pixhawk committed
}

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

Bryant's avatar
Bryant committed
    QRectF titleRect;
    QRectF footerRect;
    QRectF legendRect;
    QRectF scaleRect[QwtPlot::axisCnt];
    QRectF canvasRect;
pixhawk's avatar
pixhawk committed

    QwtPlotLayout::LayoutData layoutData;

    QwtPlot::LegendPosition legendPos;
    double legendRatio;
    unsigned int spacing;
    unsigned int canvasMargin[QwtPlot::axisCnt];
Bryant's avatar
Bryant committed
    bool alignCanvasToScales[QwtPlot::axisCnt];
pixhawk's avatar
pixhawk committed
};

/*!
  \brief Constructor
 */
Loading
Loading full blame...