Newer
Older
/* -*- 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
*****************************************************************************/
#include "qwt_text.h"
#include "qwt_text_label.h"
#include "qwt_scale_widget.h"
#include "qwt_abstract_legend.h"
#include <qscrollbar.h>
#include <qmath.h>
struct t_footerData
{
QwtText text;
int frameWidth;
} footer;
struct t_scaleData
{
bool isEnabled;
const QwtScaleWidget *scaleWidget;
QFont scaleFont;
int start;
int end;
int baseLineOffset;
struct t_canvasData
{
int contentsMargins[ QwtPlot::axisCnt ];
} canvas;
};
/*
Extract all layout relevant data from the plot components
*/
void QwtPlotLayout::LayoutData::init( const QwtPlot *plot, const QRectF &rect )
legend.hScrollExtent =
plot->legend()->scrollExtent( Qt::Horizontal );
legend.vScrollExtent =
plot->legend()->scrollExtent( Qt::Vertical );
int w = qMin( hint.width(), qFloor( rect.width() ) );
int h = plot->legend()->heightForWidth( w );
if ( h <= 0 )
if ( !( title.text.testPaintAttribute( QwtText::PaintUsingTextFont ) ) )
title.text.setFont( label->font() );
// 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();
}
for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
{
if ( plot->axisEnabled( axis ) )
{
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(
}
scale[axis].dimWithoutTitle = scaleWidget->dimForLength(
scale[axis].isEnabled = false;
scale[axis].start = 0;
scale[axis].end = 0;
scale[axis].baseLineOffset = 0;
plot->canvas()->getContentsMargins(
&canvas.contentsMargins[ QwtPlot::yLeft ],
&canvas.contentsMargins[ QwtPlot::xTop ],
&canvas.contentsMargins[ QwtPlot::yRight ],
&canvas.contentsMargins[ QwtPlot::xBottom ] );
}
class QwtPlotLayout::PrivateData
{
public:
PrivateData():
QRectF titleRect;
QRectF footerRect;
QRectF legendRect;
QRectF scaleRect[QwtPlot::axisCnt];
QRectF canvasRect;
QwtPlotLayout::LayoutData layoutData;
QwtPlot::LegendPosition legendPos;
double legendRatio;
unsigned int spacing;
unsigned int canvasMargin[QwtPlot::axisCnt];
};
/*!
\brief Constructor
*/
QwtPlotLayout::QwtPlotLayout()
{
d_data = new PrivateData;
setLegendPosition( QwtPlot::BottomLegend );
setCanvasMargin( 4 );
setAlignCanvasToScales( false );
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.
\param axis One of QwtPlot::Axis. Specifies where the position of the margin.
\warning The margin will have no effect when alignCanvasToScale() is true
if ( axis == -1 )
{
for ( axis = 0; axis < QwtPlot::axisCnt; axis++ )
\return Margin around the scale tick borders
\sa setCanvasMargin()
*/
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;
}
/*!
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.
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
\sa setCanvasMargin(), alignCanvasToScale(), setAlignCanvasToScales()
\warning In case of on == true canvasMargin() will have no effect
void QwtPlotLayout::setAlignCanvasToScale( int axisId, bool on )
if ( axisId >= 0 && axisId < QwtPlot::axisCnt )
d_data->alignCanvasToScales[axisId] = on;
}
/*!
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.
\sa setAlignCanvasToScale(), setAlignCanvasToScale(), setCanvasMargin()
if ( axisId < 0 || axisId >= QwtPlot::axisCnt )
return false;
return d_data->alignCanvasToScales[ axisId ];
}
/*!
Change the spacing of the plot. The spacing is the distance
between the plot components.
\param spacing New spacing
\sa setCanvasMargin(), spacing()
*/
int QwtPlotLayout::spacing() const
{
return d_data->spacing;
}
/*!
\brief Specify the position of the legend
\param ratio Ratio between legend and the bounding rectangle
of title, footer, canvas and axes. The legend will be shrunk
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.
The default vertical/horizontal ratio is 0.33/0.5.
void QwtPlotLayout::setLegendPosition( QwtPlot::LegendPosition pos, double ratio )
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;
}
}
/*!
\brief Specify the position of the legend
\param pos The legend's position. Valid values are
\c QwtPlot::LeftLegend, \c QwtPlot::RightLegend,
void QwtPlotLayout::setLegendPosition( QwtPlot::LegendPosition pos )
}
/*!
\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
\param ratio Ratio between legend and the bounding rectangle
of title, footer, canvas and axes. The legend will be shrunk
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.
The default vertical/horizontal ratio is 0.33/0.5.
}
/*!
\return The relative size of the legend in the plot.
\sa setLegendPosition()
*/
double QwtPlotLayout::legendRatio() const
{
return d_data->legendRatio;
}
/*!
\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;
}
/*!
\return Geometry for the title
\sa activate(), invalidate()
*/
\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
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
\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
/*!
\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;
}
/*!
\param axis Axis index
\return Geometry for the scale
\sa activate(), invalidate()
*/
if ( axis < 0 || axis >= QwtPlot::axisCnt )
{
static QRectF dummyRect;
return dummyRect;
}
return d_data->scaleRect[axis];
}
/*!
\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;
}
/*!
\return Geometry for the canvas
\sa activate(), invalidate()
*/
Invalidate the geometry of all components.
\sa activate()
*/
void QwtPlotLayout::invalidate()
{
d_data->titleRect = d_data->footerRect
= d_data->legendRect = d_data->canvasRect = QRect();
for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
QSize QwtPlotLayout::minimumSizeHint( const QwtPlot *plot ) const
w = h = minLeft = minRight = tickOffset = 0;
}
int w;
int h;
int minLeft;
int minRight;
int tickOffset;
} scaleData[QwtPlot::axisCnt];
int canvasBorder[QwtPlot::axisCnt];
int fw;
plot->canvas()->getContentsMargins( &fw, NULL, NULL, NULL );
for ( axis = 0; axis < QwtPlot::axisCnt; axis++ )
{
if ( plot->axisEnabled( axis ) )
{
const QwtScaleWidget *scl = plot->axisWidget( axis );
ScaleData &sd = scaleData[axis];
const QSize hint = scl->minimumSizeHint();
sd.w = hint.width();
sd.h = hint.height();
if ( scl->scaleDraw()->hasComponent( QwtAbstractScaleDraw::Ticks ) )
sd.tickOffset += qCeil( scl->scaleDraw()->maxTickLength() );
if ( sd.w && ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop ) )
{
if ( ( sd.minLeft > canvasBorder[QwtPlot::yLeft] )
&& scaleData[QwtPlot::yLeft].w )
{
int shiftLeft = sd.minLeft - canvasBorder[QwtPlot::yLeft];
if ( shiftLeft > scaleData[QwtPlot::yLeft].w )
shiftLeft = scaleData[QwtPlot::yLeft].w;
sd.w -= shiftLeft;
}
if ( ( sd.minRight > canvasBorder[QwtPlot::yRight] )
&& scaleData[QwtPlot::yRight].w )
{
int shiftRight = sd.minRight - canvasBorder[QwtPlot::yRight];
if ( shiftRight > scaleData[QwtPlot::yRight].w )
shiftRight = scaleData[QwtPlot::yRight].w;
sd.w -= shiftRight;
}
}
if ( sd.h && ( axis == QwtPlot::yLeft || axis == QwtPlot::yRight ) )
{
if ( ( sd.minLeft > canvasBorder[QwtPlot::xBottom] ) &&
scaleData[QwtPlot::xBottom].h )
{
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] ) &&
scaleData[QwtPlot::xTop].h )
{
int shiftTop = sd.minRight - canvasBorder[QwtPlot::xTop];
if ( shiftTop > scaleData[QwtPlot::xTop].tickOffset )
shiftTop = scaleData[QwtPlot::xTop].tickOffset;
sd.h -= shiftTop;
}
}
}
const QWidget *canvas = plot->canvas();
int left, top, right, bottom;
canvas->getContentsMargins( &left, &top, &right, &bottom );
const QSize minCanvasSize = canvas->minimumSize();
int w = scaleData[QwtPlot::yLeft].w + scaleData[QwtPlot::yRight].w;
int cw = qMax( scaleData[QwtPlot::xBottom].w, scaleData[QwtPlot::xTop].w )
+ left + 1 + right + 1;
w += qMax( cw, minCanvasSize.width() );
int h = scaleData[QwtPlot::xBottom].h + scaleData[QwtPlot::xTop].h;
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();
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;
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;
const QwtAbstractLegend *legend = plot->legend();
if ( legend && !legend->isEmpty() )
{
if ( d_data->legendPos == QwtPlot::LeftLegend
if ( legend->frameWidth() > 0 )
w += d_data->spacing;
if ( legendH > h )
legendW = qMin( legendW, int( w / ( 1.0 - d_data->legendRatio ) ) );
w += legendW + d_data->spacing;
}
else // QwtPlot::Top, QwtPlot::Bottom
{
int legendW = qMin( legend->sizeHint().width(), w );
int legendH = legend->heightForWidth( legendW );
if ( legend->frameWidth() > 0 )
h += d_data->spacing;
if ( d_data->legendRatio < 1.0 )
legendH = qMin( legendH, int( h / ( 1.0 - d_data->legendRatio ) ) );
}
}
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
QRectF QwtPlotLayout::layoutLegend( Options options,
const QRectF &rect ) const
if ( d_data->legendPos == QwtPlot::LeftLegend
// We don't allow vertical legends to take more than
// half of the available space.
dim = qMin( hint.width(), int( rect.width() * d_data->legendRatio ) );
if ( !( options & IgnoreScrollbars ) )
{
if ( hint.height() > rect.height() )
{
// space for the vertical scrollbar.
}
else
{
dim = qMin( hint.height(), int( rect.height() * d_data->legendRatio ) );
dim = qMax( dim, d_data->layoutData.legend.vScrollExtent );
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;
}
return legendRect;
}
/*!
Align the legend to the canvas
\param canvasRect Geometry of the canvas
\param legendRect Maximum geometry for the legend
QRectF QwtPlotLayout::alignLegend( const QRectF &canvasRect,
const QRectF &legendRect ) const
if ( d_data->legendPos == QwtPlot::BottomLegend
|| d_data->legendPos == QwtPlot::TopLegend )
{
if ( d_data->layoutData.legend.hint.width() < canvasRect.width() )
{
alignedRect.setX( canvasRect.x() );
alignedRect.setWidth( canvasRect.width() );
}
else
{
if ( d_data->layoutData.legend.hint.height() < canvasRect.height() )
{
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 rectangle for title, footer, axes and canvas.
\param dimAxis Expanded heights of the axis in axis orientation.
void QwtPlotLayout::expandLineBreaks( Options options, const QRectF &rect,
int &dimTitle, int &dimFooter, int dimAxis[QwtPlot::axisCnt] ) const
for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
dimAxis[axis] = 0;
int backboneOffset[QwtPlot::axisCnt];
if ( !( options & IgnoreFrames ) )
backboneOffset[axis] += d_data->layoutData.canvas.contentsMargins[ axis ];
if ( !d_data->alignCanvasToScales[axis] )
backboneOffset[axis] += d_data->canvasMargin[axis];
}
bool done = false;
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.
if ( !( ( options & IgnoreTitle ) ||
d_data->layoutData.title.text.isEmpty() ) )
{
double w = rect.width();
!= d_data->layoutData.scale[QwtPlot::yRight].isEnabled )
{
w -= dimAxis[QwtPlot::yLeft] + dimAxis[QwtPlot::yRight];
int d = qCeil( d_data->layoutData.title.text.heightForWidth( w ) );
if ( !( options & IgnoreFrames ) )
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++ )
{
const struct LayoutData::t_scaleData &scaleData =
if ( scaleData.isEnabled )
{
double length;
if ( axis == QwtPlot::xTop || axis == QwtPlot::xBottom )
{
length = rect.width() - dimAxis[QwtPlot::yLeft]
length -= scaleData.start + scaleData.end;
if ( dimAxis[QwtPlot::yRight] > 0 )
length -= 1;
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
{
length = rect.height() - dimAxis[QwtPlot::xTop]
length -= scaleData.start + scaleData.end;
length -= 1;
if ( dimAxis[QwtPlot::xBottom] <= 0 )
length -= 1;
if ( dimAxis[QwtPlot::xTop] <= 0 )
length -= 1;
if ( dimAxis[QwtPlot::xBottom] > 0 )
{
length += qMin(
d_data->layoutData.scale[QwtPlot::xBottom].tickOffset,
double( scaleData.start - backboneOffset[QwtPlot::xBottom] ) );
if ( dimAxis[QwtPlot::xTop] > 0 )
{
length += qMin(
d_data->layoutData.scale[QwtPlot::xTop].tickOffset,
double( scaleData.end - backboneOffset[QwtPlot::xTop] ) );
}
if ( dimTitle > 0 )
length -= dimTitle + d_data->spacing;
}
int d = scaleData.dimWithoutTitle;
if ( !scaleData.scaleWidget->title().isEmpty() )
{
d += scaleData.scaleWidget->titleHeightForWidth( qFloor( length ) );