You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
344 lines
0 B
344 lines
0 B
15 years ago
|
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
||
|
* Qwt Widget Library
|
||
|
* Copyright (C) 1997 Josef Wilgen
|
||
|
* Copyright (C) 2002 Uwe Rathmann
|
||
14 years ago
|
*
|
||
15 years ago
|
* 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 <qpainter.h>
|
||
|
#include "qwt_painter.h"
|
||
|
#include "qwt_scale_map.h"
|
||
|
#include "qwt_plot_marker.h"
|
||
|
#include "qwt_symbol.h"
|
||
|
#include "qwt_text.h"
|
||
|
#include "qwt_math.h"
|
||
|
|
||
|
static const int LabelDist = 2;
|
||
|
|
||
|
class QwtPlotMarker::PrivateData
|
||
|
{
|
||
|
public:
|
||
|
PrivateData():
|
||
|
align(Qt::AlignCenter),
|
||
|
style(NoLine),
|
||
|
xValue(0.0),
|
||
14 years ago
|
yValue(0.0) {
|
||
15 years ago
|
symbol = new QwtSymbol();
|
||
|
}
|
||
|
|
||
14 years ago
|
~PrivateData() {
|
||
15 years ago
|
delete symbol;
|
||
|
}
|
||
|
|
||
|
QwtText label;
|
||
|
#if QT_VERSION < 0x040000
|
||
|
int align;
|
||
|
#else
|
||
|
Qt::Alignment align;
|
||
|
#endif
|
||
|
QPen pen;
|
||
|
QwtSymbol *symbol;
|
||
|
LineStyle style;
|
||
|
|
||
|
double xValue;
|
||
|
double yValue;
|
||
|
};
|
||
|
|
||
|
//! Sets alignment to Qt::AlignCenter, and style to NoLine
|
||
|
QwtPlotMarker::QwtPlotMarker():
|
||
|
QwtPlotItem(QwtText("Marker"))
|
||
|
{
|
||
|
d_data = new PrivateData;
|
||
|
setZ(30.0);
|
||
|
}
|
||
|
|
||
|
//! Destructor
|
||
|
QwtPlotMarker::~QwtPlotMarker()
|
||
|
{
|
||
|
delete d_data;
|
||
|
}
|
||
|
|
||
|
//! \return QwtPlotItem::Rtti_PlotMarker
|
||
|
int QwtPlotMarker::rtti() const
|
||
|
{
|
||
|
return QwtPlotItem::Rtti_PlotMarker;
|
||
|
}
|
||
|
|
||
|
//! Return Value
|
||
|
QwtDoublePoint QwtPlotMarker::value() const
|
||
|
{
|
||
|
return QwtDoublePoint(d_data->xValue, d_data->yValue);
|
||
|
}
|
||
|
|
||
|
//! Return x Value
|
||
14 years ago
|
double QwtPlotMarker::xValue() const
|
||
|
{
|
||
|
return d_data->xValue;
|
||
15 years ago
|
}
|
||
|
|
||
|
//! Return y Value
|
||
14 years ago
|
double QwtPlotMarker::yValue() const
|
||
|
{
|
||
|
return d_data->yValue;
|
||
15 years ago
|
}
|
||
|
|
||
|
//! Set Value
|
||
|
void QwtPlotMarker::setValue(const QwtDoublePoint& pos)
|
||
|
{
|
||
|
setValue(pos.x(), pos.y());
|
||
|
}
|
||
|
|
||
|
//! Set Value
|
||
14 years ago
|
void QwtPlotMarker::setValue(double x, double y)
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( x != d_data->xValue || y != d_data->yValue ) {
|
||
|
d_data->xValue = x;
|
||
|
d_data->yValue = y;
|
||
|
itemChanged();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
//! Set X Value
|
||
14 years ago
|
void QwtPlotMarker::setXValue(double x)
|
||
|
{
|
||
15 years ago
|
setValue(x, d_data->yValue);
|
||
|
}
|
||
|
|
||
|
//! Set Y Value
|
||
14 years ago
|
void QwtPlotMarker::setYValue(double y)
|
||
|
{
|
||
15 years ago
|
setValue(d_data->xValue, y);
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
\brief Draw the marker
|
||
|
\param p Painter
|
||
|
\param xMap x Scale Map
|
||
|
\param yMap y Scale Map
|
||
|
\param r Bounding rect, where to paint
|
||
|
*/
|
||
|
void QwtPlotMarker::draw(QPainter *p,
|
||
14 years ago
|
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
|
||
|
const QRect &r) const
|
||
15 years ago
|
{
|
||
|
const int x = xMap.transform(d_data->xValue);
|
||
|
const int y = yMap.transform(d_data->yValue);
|
||
|
|
||
|
// draw lines
|
||
14 years ago
|
if (d_data->style != NoLine) {
|
||
15 years ago
|
p->setPen(d_data->pen);
|
||
|
if ((d_data->style == HLine) || (d_data->style == Cross))
|
||
|
QwtPainter::drawLine(p, r.left(), y, r.right(), y);
|
||
|
if ((d_data->style == VLine)||(d_data->style == Cross))
|
||
|
QwtPainter::drawLine(p, x, r.top(), x, r.bottom());
|
||
|
}
|
||
|
|
||
|
// draw symbol
|
||
|
QSize sSym(0, 0);
|
||
14 years ago
|
if (d_data->symbol->style() != QwtSymbol::NoSymbol) {
|
||
15 years ago
|
sSym = d_data->symbol->size();
|
||
|
d_data->symbol->draw(p, x, y);
|
||
|
}
|
||
|
|
||
|
// draw label
|
||
14 years ago
|
if (!d_data->label.isEmpty()) {
|
||
15 years ago
|
int xlw = qwtMax(int(d_data->pen.width()), 1);
|
||
|
int ylw = xlw;
|
||
|
int xlw1;
|
||
|
int ylw1;
|
||
|
|
||
14 years ago
|
const int xLabelDist =
|
||
15 years ago
|
QwtPainter::metricsMap().screenToLayoutX(LabelDist);
|
||
14 years ago
|
const int yLabelDist =
|
||
15 years ago
|
QwtPainter::metricsMap().screenToLayoutY(LabelDist);
|
||
|
|
||
14 years ago
|
if ((d_data->style == VLine) || (d_data->style == HLine)) {
|
||
15 years ago
|
xlw1 = (xlw + 1) / 2 + xLabelDist;
|
||
|
xlw = xlw / 2 + xLabelDist;
|
||
|
ylw1 = (ylw + 1) / 2 + yLabelDist;
|
||
|
ylw = ylw / 2 + yLabelDist;
|
||
14 years ago
|
} else {
|
||
15 years ago
|
xlw1 = qwtMax((xlw + 1) / 2, (sSym.width() + 1) / 2) + xLabelDist;
|
||
|
xlw = qwtMax(xlw / 2, (sSym.width() + 1) / 2) + xLabelDist;
|
||
|
ylw1 = qwtMax((ylw + 1) / 2, (sSym.height() + 1) / 2) + yLabelDist;
|
||
|
ylw = qwtMax(ylw / 2, (sSym. height() + 1) / 2) + yLabelDist;
|
||
|
}
|
||
|
|
||
|
QRect tr(QPoint(0, 0), d_data->label.textSize(p->font()));
|
||
|
tr.moveCenter(QPoint(0, 0));
|
||
|
|
||
|
int dx = x;
|
||
|
int dy = y;
|
||
|
|
||
14 years ago
|
if (d_data->style == VLine) {
|
||
15 years ago
|
if (d_data->align & (int) Qt::AlignTop)
|
||
|
dy = r.top() + yLabelDist - tr.y();
|
||
|
else if (d_data->align & (int) Qt::AlignBottom)
|
||
|
dy = r.bottom() - yLabelDist + tr.y();
|
||
|
else
|
||
|
dy = r.top() + r.height() / 2;
|
||
14 years ago
|
} else {
|
||
15 years ago
|
if (d_data->align & (int) Qt::AlignTop)
|
||
|
dy += tr.y() - ylw1;
|
||
|
else if (d_data->align & (int) Qt::AlignBottom)
|
||
|
dy -= tr.y() - ylw1;
|
||
|
}
|
||
|
|
||
|
|
||
14 years ago
|
if (d_data->style == HLine) {
|
||
15 years ago
|
if (d_data->align & (int) Qt::AlignLeft)
|
||
|
dx = r.left() + xLabelDist - tr.x();
|
||
|
else if (d_data->align & (int) Qt::AlignRight)
|
||
|
dx = r.right() - xLabelDist + tr.x();
|
||
|
else
|
||
|
dx = r.left() + r.width() / 2;
|
||
14 years ago
|
} else {
|
||
15 years ago
|
if (d_data->align & (int) Qt::AlignLeft)
|
||
|
dx += tr.x() - xlw1;
|
||
|
else if (d_data->align & (int) Qt::AlignRight)
|
||
|
dx -= tr.x() - xlw1;
|
||
|
}
|
||
|
|
||
|
#if QT_VERSION < 0x040000
|
||
|
tr.moveBy(dx, dy);
|
||
|
#else
|
||
|
tr.translate(dx, dy);
|
||
|
#endif
|
||
|
d_data->label.draw(p, tr);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
\brief Set the line style
|
||
|
\param st Line style. Can be one of QwtPlotMarker::NoLine,
|
||
|
HLine, VLine or Cross
|
||
|
\sa lineStyle()
|
||
|
*/
|
||
|
void QwtPlotMarker::setLineStyle(QwtPlotMarker::LineStyle st)
|
||
|
{
|
||
14 years ago
|
if ( st != d_data->style ) {
|
||
15 years ago
|
d_data->style = st;
|
||
|
itemChanged();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
\return the line style
|
||
|
\sa For a description of line styles, see QwtPlotMarker::setLineStyle()
|
||
|
*/
|
||
14 years ago
|
QwtPlotMarker::LineStyle QwtPlotMarker::lineStyle() const
|
||
|
{
|
||
|
return d_data->style;
|
||
15 years ago
|
}
|
||
|
|
||
|
/*!
|
||
|
\brief Assign a symbol
|
||
14 years ago
|
\param s New symbol
|
||
15 years ago
|
\sa symbol()
|
||
|
*/
|
||
|
void QwtPlotMarker::setSymbol(const QwtSymbol &s)
|
||
|
{
|
||
|
delete d_data->symbol;
|
||
|
d_data->symbol = s.clone();
|
||
|
itemChanged();
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
\return the symbol
|
||
|
\sa setSymbol(), QwtSymbol
|
||
|
*/
|
||
14 years ago
|
const QwtSymbol &QwtPlotMarker::symbol() const
|
||
|
{
|
||
|
return *d_data->symbol;
|
||
15 years ago
|
}
|
||
|
|
||
|
/*!
|
||
|
\brief Set the label
|
||
|
\param label label text
|
||
|
\sa label()
|
||
|
*/
|
||
|
void QwtPlotMarker::setLabel(const QwtText& label)
|
||
|
{
|
||
14 years ago
|
if ( label != d_data->label ) {
|
||
15 years ago
|
d_data->label = label;
|
||
|
itemChanged();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
\return the label
|
||
|
\sa setLabel()
|
||
|
*/
|
||
14 years ago
|
QwtText QwtPlotMarker::label() const
|
||
|
{
|
||
|
return d_data->label;
|
||
15 years ago
|
}
|
||
|
|
||
|
/*!
|
||
|
\brief Set the alignment of the label
|
||
|
|
||
|
The alignment determines where the label is drawn relative to
|
||
|
the marker's position.
|
||
|
|
||
|
\param align Alignment. A combination of AlignTop, AlignBottom,
|
||
|
AlignLeft, AlignRight, AlignCenter, AlgnHCenter,
|
||
14 years ago
|
AlignVCenter.
|
||
15 years ago
|
\sa labelAlignment()
|
||
|
*/
|
||
|
#if QT_VERSION < 0x040000
|
||
|
void QwtPlotMarker::setLabelAlignment(int align)
|
||
|
#else
|
||
|
void QwtPlotMarker::setLabelAlignment(Qt::Alignment align)
|
||
|
#endif
|
||
|
{
|
||
|
if ( align == d_data->align )
|
||
|
return;
|
||
14 years ago
|
|
||
15 years ago
|
d_data->align = align;
|
||
|
itemChanged();
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
\return the label alignment
|
||
|
\sa setLabelAlignment()
|
||
|
*/
|
||
|
#if QT_VERSION < 0x040000
|
||
14 years ago
|
int QwtPlotMarker::labelAlignment() const
|
||
15 years ago
|
#else
|
||
14 years ago
|
Qt::Alignment QwtPlotMarker::labelAlignment() const
|
||
15 years ago
|
#endif
|
||
14 years ago
|
{
|
||
|
return d_data->align;
|
||
15 years ago
|
}
|
||
|
|
||
|
/*!
|
||
|
\brief Specify a pen for the line.
|
||
|
\param p New pen
|
||
|
\sa linePen()
|
||
|
*/
|
||
|
void QwtPlotMarker::setLinePen(const QPen &p)
|
||
|
{
|
||
14 years ago
|
if ( p != d_data->pen ) {
|
||
15 years ago
|
d_data->pen = p;
|
||
|
itemChanged();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
\return the line pen
|
||
|
\sa setLinePen()
|
||
|
*/
|
||
14 years ago
|
const QPen &QwtPlotMarker::linePen() const
|
||
|
{
|
||
|
return d_data->pen;
|
||
15 years ago
|
}
|
||
|
|
||
|
QwtDoubleRect QwtPlotMarker::boundingRect() const
|
||
|
{
|
||
|
return QwtDoubleRect(d_data->xValue, d_data->yValue, 0.0, 0.0);
|
||
|
}
|