Skip to content
qwt_plot_layout.cpp 34.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
 *****************************************************************************/

// 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);

    struct t_legendData {
pixhawk's avatar
pixhawk committed
        int frameWidth;
        int vScrollBarWidth;
        int hScrollBarHeight;
        QSize hint;
    } legend;

    struct t_titleData {
pixhawk's avatar
pixhawk committed
        QwtText text;
        int frameWidth;
    } title;

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

    struct t_canvasData {
pixhawk's avatar
pixhawk committed
        int frameWidth;
    } canvas;
};

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

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

    if ( plot->plotLayout()->legendPosition() != QwtPlot::ExternalLegend
            && plot->legend() ) {
pixhawk's avatar
pixhawk committed
        legend.frameWidth = plot->legend()->frameWidth();
        legend.vScrollBarWidth =
pixhawk's avatar
pixhawk committed
            plot->legend()->verticalScrollBar()->sizeHint().width();
        legend.hScrollBarHeight =
pixhawk's avatar
pixhawk committed
            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);
    }

pixhawk's avatar
pixhawk committed

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

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

pixhawk's avatar
pixhawk committed

    for (int axis = 0; axis < QwtPlot::axisCnt; axis++ ) {
        if ( plot->axisEnabled(axis) ) {
pixhawk's avatar
pixhawk committed
            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(
                        QwtAbstractScaleDraw::Ticks) ) {
                scale[axis].tickOffset +=
pixhawk's avatar
pixhawk committed
                    (int)scaleWidget->scaleDraw()->majTickLength();
            }

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

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

pixhawk's avatar
pixhawk committed

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

class QwtPlotLayout::PrivateData
{
public:
    PrivateData():
        margin(0),
        spacing(5),
        alignCanvasToScales(false) {
pixhawk's avatar
pixhawk committed
    }

    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.
pixhawk's avatar
pixhawk committed
  \param margin new margin
  \sa margin(), setSpacing(),
      QwtPlot::setMargin()
*/
void QwtPlotLayout::setMargin(int margin)
{
    if ( margin < 0 )
        margin = 0;
    d_data->margin = margin;
}

/*!
Loading
Loading full blame...