📄 qwt_scale_draw.cpp
字号:
/* -*- 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 <qpen.h>
#include <qpainter.h>
#include "qwt_math.h"
#include "qwt_painter.h"
#include "qwt_polygon.h"
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"
#include "qwt_scale_draw.h"
#if QT_VERSION < 0x040000
#include <qwmatrix.h>
#define QwtMatrix QWMatrix
#else
#include <qmatrix.h>
#define QwtMatrix QMatrix
#endif
class QwtScaleDraw::PrivateData
{
public:
PrivateData():
len(0),
alignment(QwtScaleDraw::BottomScale),
labelAlignment(0),
labelRotation(0.0)
{
}
QPoint pos;
int len;
Alignment alignment;
#if QT_VERSION < 0x040000
int labelAlignment;
#else
Qt::Alignment labelAlignment;
#endif
double labelRotation;
};
/*!
\brief Constructor
The range of the scale is initialized to [0, 100],
The position is at (0, 0) with a length of 100.
The orientation is QwtAbstractScaleDraw::Bottom.
*/
QwtScaleDraw::QwtScaleDraw()
{
d_data = new QwtScaleDraw::PrivateData;
setLength(100);
}
//! Copy constructor
QwtScaleDraw::QwtScaleDraw(const QwtScaleDraw &other):
QwtAbstractScaleDraw(other)
{
d_data = new QwtScaleDraw::PrivateData(*other.d_data);
}
//! Destructor
QwtScaleDraw::~QwtScaleDraw()
{
delete d_data;
}
//! Assignment operator
QwtScaleDraw &QwtScaleDraw::operator=(const QwtScaleDraw &other)
{
*(QwtAbstractScaleDraw*)this = (const QwtAbstractScaleDraw &)other;
*d_data = *other.d_data;
return *this;
}
/*!
Return alignment of the scale
\sa setAlignment()
*/
QwtScaleDraw::Alignment QwtScaleDraw::alignment() const
{
return d_data->alignment;
}
/*!
Set the alignment of the scale
The default alignment is QwtScaleDraw::BottomScale
\sa alignment()
*/
void QwtScaleDraw::setAlignment(Alignment align)
{
d_data->alignment = align;
}
/*!
Return the orientation
TopScale, BottomScale are horizontal (Qt::Horizontal) scales,
LeftScale, RightScale are vertical (Qt::Vertical) scales.
\sa alignment()
*/
Qt::Orientation QwtScaleDraw::orientation() const
{
switch(d_data->alignment)
{
case TopScale:
case BottomScale:
return Qt::Horizontal;
case LeftScale:
case RightScale:
default:
return Qt::Vertical;
}
}
/*!
\brief Determine the minimum border distance
This member function returns the minimum space
needed to draw the mark labels at the scale's endpoints.
\param font Font
\param start Start border distance
\param end End border distance
*/
void QwtScaleDraw::getBorderDistHint(const QFont &font,
int &start, int &end ) const
{
start = 0;
end = 0;
if ( !hasComponent(QwtAbstractScaleDraw::Labels) )
return;
const QwtValueList &ticks = scaleDiv().ticks(QwtScaleDiv::MajorTick);
if ( ticks.count() == 0 )
return;
QRect lr = labelRect(font, ticks[0]);
// find the distance between tick and border
int off = qwtAbs(map().transform(ticks[0]) - qRound(map().p1()));
if ( orientation() == Qt::Vertical )
end = lr.bottom() + 1 - off;
else
start = -lr.left() - off;
const int lastTick = ticks.count() - 1;
lr = labelRect(font, ticks[lastTick]);
// find the distance between tick and border
off = qwtAbs(map().transform(ticks[lastTick]) - qRound(map().p2()));
if ( orientation() == Qt::Vertical )
start = -lr.top() - off;
else
end = lr.right() + 1 - off;
// if the distance between tick and border is larger
// than half of the label width/height, we set to 0
if ( start < 0 )
start = 0;
if ( end < 0 )
end = 0;
}
/*!
Determine the minimum distance between two labels, that is necessary
that the texts don't overlap.
\param font Font
\return The maximum width of a label
\sa getBorderDistHint()
*/
int QwtScaleDraw::minLabelDist(const QFont &font) const
{
if ( !hasComponent(QwtAbstractScaleDraw::Labels) )
return 0;
const QwtValueList &ticks = scaleDiv().ticks(QwtScaleDiv::MajorTick);
if (ticks.count() == 0)
return 0;
const QFontMetrics fm(font);
const bool vertical = (orientation() == Qt::Vertical);
QRect bRect1;
QRect bRect2 = labelRect(font, ticks[0]);
if ( vertical )
{
bRect2.setRect(-bRect2.bottom(), 0, bRect2.height(), bRect2.width());
}
int maxDist = 0;
for (uint i = 1; i < (uint)ticks.count(); i++ )
{
bRect1 = bRect2;
bRect2 = labelRect(font, ticks[i]);
if ( vertical )
{
bRect2.setRect(-bRect2.bottom(), 0,
bRect2.height(), bRect2.width());
}
int dist = fm.leading(); // space between the labels
if ( bRect1.right() > 0 )
dist += bRect1.right();
if ( bRect2.left() < 0 )
dist += -bRect2.left();
if ( dist > maxDist )
maxDist = dist;
}
double angle = labelRotation() / 180.0 * M_PI;
if ( vertical )
angle += M_PI / 2;
if ( sin(angle) == 0.0 )
return maxDist;
const int fmHeight = fm.ascent() - 2;
// The distance we need until there is
// the height of the label font. This height is needed
// for the neighbour labal.
int labelDist = (int)(fmHeight / sin(angle) * cos(angle));
if ( labelDist < 0 )
labelDist = -labelDist;
// The cast above floored labelDist. We want to ceil.
labelDist++;
// For text orientations close to the scale orientation
if ( labelDist > maxDist )
labelDist = maxDist;
// For text orientations close to the opposite of the
// scale orientation
if ( labelDist < fmHeight )
labelDist = fmHeight;
return labelDist;
}
/*!
Calculate the width/height that is needed for a
vertical/horizontal scale.
The extent is calculated from the pen width of the backbone,
the major tick length, the spacing and the maximum width/height
of the labels.
\param pen Pen that is used for painting backbone and ticks
\param font Font used for painting the labels
\sa minLength()
*/
int QwtScaleDraw::extent(const QPen &pen, const QFont &font) const
{
int d = 0;
if ( hasComponent(QwtAbstractScaleDraw::Labels) )
{
if ( orientation() == Qt::Vertical )
d = maxLabelWidth(font);
else
d = maxLabelHeight(font);
if ( d > 0 )
d += spacing();
}
if ( hasComponent(QwtAbstractScaleDraw::Ticks) )
{
d += majTickLength();
}
if ( hasComponent(QwtAbstractScaleDraw::Backbone) )
{
const int pw = qwtMax( 1, pen.width() ); // penwidth can be zero
d += pw;
}
d = qwtMax(d, minimumExtent());
return d;
}
/*!
Calculate the minimum length that is needed to draw the scale
\param pen Pen that is used for painting backbone and ticks
\param font Font used for painting the labels
\sa extent()
*/
int QwtScaleDraw::minLength(const QPen &pen, const QFont &font) const
{
int startDist, endDist;
getBorderDistHint(font, startDist, endDist);
const QwtScaleDiv &sd = scaleDiv();
const uint minorCount =
sd.ticks(QwtScaleDiv::MinorTick).count() +
sd.ticks(QwtScaleDiv::MediumTick).count();
const uint majorCount =
sd.ticks(QwtScaleDiv::MajorTick).count();
int lengthForLabels = 0;
if ( hasComponent(QwtAbstractScaleDraw::Labels) )
{
if ( majorCount >= 2 )
lengthForLabels = minLabelDist(font) * (majorCount - 1);
}
int lengthForTicks = 0;
if ( hasComponent(QwtAbstractScaleDraw::Ticks) )
{
const int pw = qwtMax( 1, pen.width() ); // penwidth can be zero
lengthForTicks = 2 * (majorCount + minorCount) * pw;
}
return startDist + endDist + qwtMax(lengthForLabels, lengthForTicks);
}
/*!
Find the position, where to paint a label
The position has a distance of majTickLength() + spacing() + 1
from the backbone. The direction depends on the alignment()
\param value Value
*/
QPoint QwtScaleDraw::labelPosition( double value) const
{
const int tval = map().transform(value);
int dist = spacing() + 1;
if ( hasComponent(QwtAbstractScaleDraw::Ticks) )
dist += majTickLength();
int px = 0;
int py = 0;
switch(alignment())
{
case RightScale:
{
px = d_data->pos.x() + dist;
py = tval;
break;
}
case LeftScale:
{
px = d_data->pos.x() - dist;
py = tval;
break;
}
case BottomScale:
{
px = tval;
py = d_data->pos.y() + dist;
break;
}
case TopScale:
{
px = tval;
py = d_data->pos.y() - dist;
break;
}
}
return QPoint(px, py);
}
/*!
Draw a tick
\param painter Painter
\param value Value of the tick
\param len Lenght of the tick
\sa drawBackbone(), drawLabel()
*/
void QwtScaleDraw::drawTick(QPainter *painter, double value, int len) const
{
if ( len <= 0 )
return;
int pw2 = qwtMin((int)painter->pen().width(), len) / 2;
QwtScaleMap scaleMap = map();
const QwtMetricsMap metricsMap = QwtPainter::metricsMap();
QPoint pos = d_data->pos;
if ( !metricsMap.isIdentity() )
{
/*
The perfect position of the ticks is important.
To avoid rounding errors we have to use
device coordinates.
*/
QwtPainter::resetMetricsMap();
pos = metricsMap.layoutToDevice(pos);
if ( orientation() == Qt::Vertical )
{
scaleMap.setPaintInterval(
metricsMap.layoutToDeviceY((int)scaleMap.p1()),
metricsMap.layoutToDeviceY((int)scaleMap.p2())
);
len = metricsMap.layoutToDeviceX(len);
}
else
{
scaleMap.setPaintInterval(
metricsMap.layoutToDeviceX((int)scaleMap.p1()),
metricsMap.layoutToDeviceX((int)scaleMap.p2())
);
len = metricsMap.layoutToDeviceY(len);
}
}
const int tval = scaleMap.transform(value);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -