qwt_interval_symbol.cpp 7.85 KB
Newer Older
Bryant's avatar
Bryant committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
/* -*- 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_interval_symbol.h"
#include "qwt_painter.h"
#include "qwt_math.h"
#include <qpainter.h>

#if QT_VERSION < 0x040601
#define qAtan2(y, x) ::atan2(y, x)
#define qFastSin(x) qSin(x)
#define qFastCos(x) qCos(x)
#endif

class QwtIntervalSymbol::PrivateData
{
public:
    PrivateData():
        style( QwtIntervalSymbol::NoSymbol ),
        width( 6 )
    {
    }

    bool operator==( const PrivateData &other ) const
    {
        return ( style == other.style )
            && ( width == other.width )
            && ( brush == other.brush )
            && ( pen == other.pen );
    }

    QwtIntervalSymbol::Style style;
    int width;

    QPen pen;
    QBrush brush;
};

/*!
  Constructor

  \param style Style of the symbol
  \sa setStyle(), style(), Style
*/
QwtIntervalSymbol::QwtIntervalSymbol( Style style )
{
    d_data = new PrivateData();
    d_data->style = style;
}

//! Copy constructor
QwtIntervalSymbol::QwtIntervalSymbol( const QwtIntervalSymbol &other )
{
    d_data = new PrivateData();
    *d_data = *other.d_data;
}

//! Destructor
QwtIntervalSymbol::~QwtIntervalSymbol()
{
    delete d_data;
}

//! \brief Assignment operator
QwtIntervalSymbol &QwtIntervalSymbol::operator=( 
    const QwtIntervalSymbol &other )
{
    *d_data = *other.d_data;
    return *this;
}

//! \brief Compare two symbols
bool QwtIntervalSymbol::operator==( 
    const QwtIntervalSymbol &other ) const
{
    return *d_data == *other.d_data;
}

//! \brief Compare two symbols
bool QwtIntervalSymbol::operator!=( 
    const QwtIntervalSymbol &other ) const
{
    return !( *d_data == *other.d_data );
}

/*!
  Specify the symbol style

  \param style Style
  \sa style(), Style
*/
void QwtIntervalSymbol::setStyle( Style style )
{
    d_data->style = style;
}

/*!
  \return Current symbol style
  \sa setStyle()
*/
QwtIntervalSymbol::Style QwtIntervalSymbol::style() const
{
    return d_data->style;
}

/*!
  Specify the width of the symbol
  It is used depending on the style.

  \param width Width
  \sa width(), setStyle()
*/
void QwtIntervalSymbol::setWidth( int width )
{
    d_data->width = width;
}

/*!
  \return Width of the symbol.
  \sa setWidth(), setStyle()
*/
int QwtIntervalSymbol::width() const
{
    return d_data->width;
}

/*!
  \brief Assign a brush

  The brush is used for the Box style.

  \param brush Brush
  \sa brush()
*/
void QwtIntervalSymbol::setBrush( const QBrush &brush )
{
    d_data->brush = brush;
}

/*!
  \return Brush
  \sa setBrush()
*/
const QBrush& QwtIntervalSymbol::brush() const
{
    return d_data->brush;
}

/*! 
  Build and assign a pen
    
  In Qt5 the default pen width is 1.0 ( 0.0 in Qt4 ) what makes it
  non cosmetic ( see QPen::isCosmetic() ). This method has been introduced
  to hide this incompatibility.

  \param color Pen color
  \param width Pen width
  \param style Pen style
    
  \sa pen(), brush()
 */ 
void QwtIntervalSymbol::setPen( const QColor &color, 
    qreal width, Qt::PenStyle style )
{   
    setPen( QPen( color, width, style ) );
}

/*!
  Assign a pen

  \param pen Pen
  \sa pen(), setBrush()
*/
void QwtIntervalSymbol::setPen( const QPen &pen )
{
    d_data->pen = pen;
}

/*!
  \return Pen
  \sa setPen(), brush()
*/
const QPen& QwtIntervalSymbol::pen() const
{
    return d_data->pen;
}

/*!
  Draw a symbol depending on its style

  \param painter Painter
  \param orientation Orientation
  \param from Start point of the interval in target device coordinates
  \param to End point of the interval in target device coordinates

  \sa setStyle()
*/
void QwtIntervalSymbol::draw( QPainter *painter, Qt::Orientation orientation,
    const QPointF &from, const QPointF &to ) const
{
    const qreal pw = qMax( painter->pen().widthF(), qreal( 1.0 ) );

    QPointF p1 = from;
    QPointF p2 = to;
    if ( QwtPainter::roundingAlignment( painter ) )
    {
        p1 = p1.toPoint();
        p2 = p2.toPoint();
    }

    switch ( d_data->style )
    {
        case QwtIntervalSymbol::Bar:
        {
            QwtPainter::drawLine( painter, p1, p2 );
            if ( d_data->width > pw )
            {
                if ( ( orientation == Qt::Horizontal ) 
                    && ( p1.y() == p2.y() ) )
                {
                    const double sw = d_data->width;

                    const double y = p1.y() - sw / 2;
                    QwtPainter::drawLine( painter,
                        p1.x(), y, p1.x(), y + sw );
                    QwtPainter::drawLine( painter,
                        p2.x(), y, p2.x(), y + sw );
                }
                else if ( ( orientation == Qt::Vertical ) 
                    && ( p1.x() == p2.x() ) )
                {
                    const double sw = d_data->width;

                    const double x = p1.x() - sw / 2;
                    QwtPainter::drawLine( painter,
                        x, p1.y(), x + sw, p1.y() );
                    QwtPainter::drawLine( painter,
                        x, p2.y(), x + sw, p2.y() );
                }
                else
                {
                    const double sw = d_data->width;

                    const double dx = p2.x() - p1.x();
                    const double dy = p2.y() - p1.y();
                    const double angle = qAtan2( dy, dx ) + M_PI_2;
                    double dw2 = sw / 2.0;

                    const double cx = qFastCos( angle ) * dw2;
                    const double sy = qFastSin( angle ) * dw2;

                    QwtPainter::drawLine( painter,
                        p1.x() - cx, p1.y() - sy,
                        p1.x() + cx, p1.y() + sy );
                    QwtPainter::drawLine( painter,
                        p2.x() - cx, p2.y() - sy,
                        p2.x() + cx, p2.y() + sy );
                }
            }
            break;
        }
        case QwtIntervalSymbol::Box:
        {
            if ( d_data->width <= pw )
            {
                QwtPainter::drawLine( painter, p1, p2 );
            }
            else
            {
                if ( ( orientation == Qt::Horizontal ) 
                    && ( p1.y() == p2.y() ) )
                {
                    const double sw = d_data->width;

                    const double y = p1.y() - d_data->width / 2;
                    QwtPainter::drawRect( painter,
                        p1.x(), y, p2.x() - p1.x(),  sw );
                }
                else if ( ( orientation == Qt::Vertical )
                    && ( p1.x() == p2.x() ) )
                {
                    const double sw = d_data->width;

                    const double x = p1.x() - d_data->width / 2;
                    QwtPainter::drawRect( painter,
                        x, p1.y(), sw, p2.y() - p1.y() );
                }
                else
                {
                    const double sw = d_data->width;

                    const double dx = p2.x() - p1.x();
                    const double dy = p2.y() - p1.y();
                    const double angle = qAtan2( dy, dx ) + M_PI_2;
                    double dw2 = sw / 2.0;

                    const double cx = qFastCos( angle ) * dw2;
                    const double sy = qFastSin( angle ) * dw2;

                    QPolygonF polygon;
                    polygon += QPointF( p1.x() - cx, p1.y() - sy );
                    polygon += QPointF( p1.x() + cx, p1.y() + sy );
                    polygon += QPointF( p2.x() + cx, p2.y() + sy );
                    polygon += QPointF( p2.x() - cx, p2.y() - sy );

                    QwtPainter::drawPolygon( painter, polygon );
                }
            }
            break;
        }
        default:;
    }
}