qwt_plot_marker.cpp 7.68 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * 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),
29
        yValue(0.0) {
pixhawk's avatar
pixhawk committed
30 31 32
        symbol = new QwtSymbol();
    }

33
    ~PrivateData() {
pixhawk's avatar
pixhawk committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        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
78 79 80
double QwtPlotMarker::xValue() const
{
    return d_data->xValue;
pixhawk's avatar
pixhawk committed
81 82 83
}

//! Return y Value
84 85 86
double QwtPlotMarker::yValue() const
{
    return d_data->yValue;
pixhawk's avatar
pixhawk committed
87 88 89 90 91 92 93 94 95
}

//! Set Value
void QwtPlotMarker::setValue(const QwtDoublePoint& pos)
{
    setValue(pos.x(), pos.y());
}

//! Set Value
96
void QwtPlotMarker::setValue(double x, double y)
pixhawk's avatar
pixhawk committed
97
{
98 99 100 101
    if ( x != d_data->xValue || y != d_data->yValue ) {
        d_data->xValue = x;
        d_data->yValue = y;
        itemChanged();
pixhawk's avatar
pixhawk committed
102 103 104 105
    }
}

//! Set X Value
106 107
void QwtPlotMarker::setXValue(double x)
{
pixhawk's avatar
pixhawk committed
108 109 110 111
    setValue(x, d_data->yValue);
}

//! Set Y Value
112 113
void QwtPlotMarker::setYValue(double y)
{
pixhawk's avatar
pixhawk committed
114 115 116 117 118 119 120 121 122 123 124
    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,
125 126
                         const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                         const QRect &r) const
pixhawk's avatar
pixhawk committed
127 128 129 130 131
{
    const int x = xMap.transform(d_data->xValue);
    const int y = yMap.transform(d_data->yValue);

    // draw lines
132
    if (d_data->style != NoLine) {
pixhawk's avatar
pixhawk committed
133 134 135 136 137 138 139 140 141
        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);
142
    if (d_data->symbol->style() != QwtSymbol::NoSymbol) {
pixhawk's avatar
pixhawk committed
143 144 145 146 147
        sSym = d_data->symbol->size();
        d_data->symbol->draw(p, x, y);
    }

    // draw label
148
    if (!d_data->label.isEmpty()) {
pixhawk's avatar
pixhawk committed
149 150 151 152 153
        int xlw = qwtMax(int(d_data->pen.width()), 1);
        int ylw = xlw;
        int xlw1;
        int ylw1;

154
        const int xLabelDist =
pixhawk's avatar
pixhawk committed
155
            QwtPainter::metricsMap().screenToLayoutX(LabelDist);
156
        const int yLabelDist =
pixhawk's avatar
pixhawk committed
157 158
            QwtPainter::metricsMap().screenToLayoutY(LabelDist);

159
        if ((d_data->style == VLine) || (d_data->style == HLine)) {
pixhawk's avatar
pixhawk committed
160 161 162 163
            xlw1 = (xlw + 1) / 2 + xLabelDist;
            xlw = xlw / 2 + xLabelDist;
            ylw1 = (ylw + 1) / 2 + yLabelDist;
            ylw = ylw / 2 + yLabelDist;
164
        } else {
pixhawk's avatar
pixhawk committed
165 166 167 168 169 170 171 172 173 174 175 176
            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;

177
        if (d_data->style == VLine) {
pixhawk's avatar
pixhawk committed
178 179 180 181 182 183
            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;
184
        } else {
pixhawk's avatar
pixhawk committed
185 186 187 188 189 190 191
            if (d_data->align & (int) Qt::AlignTop)
                dy += tr.y() - ylw1;
            else if (d_data->align & (int) Qt::AlignBottom)
                dy -= tr.y() - ylw1;
        }


192
        if (d_data->style == HLine) {
pixhawk's avatar
pixhawk committed
193 194 195 196 197 198
            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;
199
        } else {
pixhawk's avatar
pixhawk committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
            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)
{
223
    if ( st != d_data->style ) {
pixhawk's avatar
pixhawk committed
224 225 226 227 228 229 230 231 232
        d_data->style = st;
        itemChanged();
    }
}

/*!
  \return the line style
  \sa For a description of line styles, see QwtPlotMarker::setLineStyle()
*/
233 234 235
QwtPlotMarker::LineStyle QwtPlotMarker::lineStyle() const
{
    return d_data->style;
pixhawk's avatar
pixhawk committed
236 237 238 239
}

/*!
  \brief Assign a symbol
240
  \param s New symbol
pixhawk's avatar
pixhawk committed
241 242 243 244 245 246 247 248 249 250 251 252 253
  \sa symbol()
*/
void QwtPlotMarker::setSymbol(const QwtSymbol &s)
{
    delete d_data->symbol;
    d_data->symbol = s.clone();
    itemChanged();
}

/*!
  \return the symbol
  \sa setSymbol(), QwtSymbol
*/
254 255 256
const QwtSymbol &QwtPlotMarker::symbol() const
{
    return *d_data->symbol;
pixhawk's avatar
pixhawk committed
257 258 259 260 261 262 263 264 265
}

/*!
  \brief Set the label
  \param label label text
  \sa label()
*/
void QwtPlotMarker::setLabel(const QwtText& label)
{
266
    if ( label != d_data->label ) {
pixhawk's avatar
pixhawk committed
267 268 269 270 271 272 273 274 275
        d_data->label = label;
        itemChanged();
    }
}

/*!
  \return the label
  \sa setLabel()
*/
276 277 278
QwtText QwtPlotMarker::label() const
{
    return d_data->label;
pixhawk's avatar
pixhawk committed
279 280 281 282 283 284 285 286 287 288
}

/*!
  \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,
289
    AlignVCenter.
pixhawk's avatar
pixhawk committed
290 291 292 293 294 295 296 297 298 299
  \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;
300

pixhawk's avatar
pixhawk committed
301 302 303 304 305 306 307 308 309
    d_data->align = align;
    itemChanged();
}

/*!
  \return the label alignment
  \sa setLabelAlignment()
*/
#if QT_VERSION < 0x040000
310
int QwtPlotMarker::labelAlignment() const
pixhawk's avatar
pixhawk committed
311
#else
312
Qt::Alignment QwtPlotMarker::labelAlignment() const
pixhawk's avatar
pixhawk committed
313
#endif
314 315
{
    return d_data->align;
pixhawk's avatar
pixhawk committed
316 317 318 319 320 321 322 323 324
}

/*!
  \brief Specify a pen for the line.
  \param p New pen
  \sa linePen()
*/
void QwtPlotMarker::setLinePen(const QPen &p)
{
325
    if ( p != d_data->pen ) {
pixhawk's avatar
pixhawk committed
326 327 328 329 330 331 332 333 334
        d_data->pen = p;
        itemChanged();
    }
}

/*!
  \return the line pen
  \sa setLinePen()
*/
335 336 337
const QPen &QwtPlotMarker::linePen() const
{
    return d_data->pen;
pixhawk's avatar
pixhawk committed
338 339 340 341 342 343
}

QwtDoubleRect QwtPlotMarker::boundingRect() const
{
    return QwtDoubleRect(d_data->xValue, d_data->yValue, 0.0, 0.0);
}