[4271] | 1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
| 2 | * Qwt Widget Library
|
---|
| 3 | * Copyright (C) 1997 Josef Wilgen
|
---|
| 4 | * Copyright (C) 2002 Uwe Rathmann
|
---|
| 5 | *
|
---|
| 6 | * This library is free software; you can redistribute it and/or
|
---|
| 7 | * modify it under the terms of the Qwt License, Version 1.0
|
---|
| 8 | *****************************************************************************/
|
---|
| 9 |
|
---|
| 10 | #include "qwt_interval_symbol.h"
|
---|
| 11 | #include "qwt_painter.h"
|
---|
| 12 | #include "qwt_math.h"
|
---|
| 13 | #include <qpainter.h>
|
---|
| 14 |
|
---|
| 15 | #if QT_VERSION < 0x040601
|
---|
| 16 | #define qAtan2(y, x) ::atan2(y, x)
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
| 19 | class QwtIntervalSymbol::PrivateData
|
---|
| 20 | {
|
---|
| 21 | public:
|
---|
| 22 | PrivateData():
|
---|
| 23 | style( QwtIntervalSymbol::NoSymbol ),
|
---|
| 24 | width( 6 )
|
---|
| 25 | {
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | bool operator==( const PrivateData &other ) const
|
---|
| 29 | {
|
---|
| 30 | return ( style == other.style )
|
---|
| 31 | && ( width == other.width )
|
---|
| 32 | && ( brush == other.brush )
|
---|
| 33 | && ( pen == other.pen );
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | QwtIntervalSymbol::Style style;
|
---|
| 37 | int width;
|
---|
| 38 |
|
---|
| 39 | QPen pen;
|
---|
| 40 | QBrush brush;
|
---|
| 41 | };
|
---|
| 42 |
|
---|
| 43 | /*!
|
---|
| 44 | Constructor
|
---|
| 45 |
|
---|
| 46 | \param style Style of the symbol
|
---|
| 47 | \sa setStyle(), style(), Style
|
---|
| 48 | */
|
---|
| 49 | QwtIntervalSymbol::QwtIntervalSymbol( Style style )
|
---|
| 50 | {
|
---|
| 51 | d_data = new PrivateData();
|
---|
| 52 | d_data->style = style;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | //! Copy constructor
|
---|
| 56 | QwtIntervalSymbol::QwtIntervalSymbol( const QwtIntervalSymbol &other )
|
---|
| 57 | {
|
---|
| 58 | d_data = new PrivateData();
|
---|
| 59 | *d_data = *other.d_data;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | //! Destructor
|
---|
| 63 | QwtIntervalSymbol::~QwtIntervalSymbol()
|
---|
| 64 | {
|
---|
| 65 | delete d_data;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | //! \brief Assignment operator
|
---|
| 69 | QwtIntervalSymbol &QwtIntervalSymbol::operator=(
|
---|
| 70 | const QwtIntervalSymbol &other )
|
---|
| 71 | {
|
---|
| 72 | *d_data = *other.d_data;
|
---|
| 73 | return *this;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | //! \brief Compare two symbols
|
---|
| 77 | bool QwtIntervalSymbol::operator==(
|
---|
| 78 | const QwtIntervalSymbol &other ) const
|
---|
| 79 | {
|
---|
| 80 | return *d_data == *other.d_data;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | //! \brief Compare two symbols
|
---|
| 84 | bool QwtIntervalSymbol::operator!=(
|
---|
| 85 | const QwtIntervalSymbol &other ) const
|
---|
| 86 | {
|
---|
| 87 | return !( *d_data == *other.d_data );
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /*!
|
---|
| 91 | Specify the symbol style
|
---|
| 92 |
|
---|
| 93 | \param style Style
|
---|
| 94 | \sa style(), Style
|
---|
| 95 | */
|
---|
| 96 | void QwtIntervalSymbol::setStyle( Style style )
|
---|
| 97 | {
|
---|
| 98 | d_data->style = style;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /*!
|
---|
| 102 | \return Current symbol style
|
---|
| 103 | \sa setStyle()
|
---|
| 104 | */
|
---|
| 105 | QwtIntervalSymbol::Style QwtIntervalSymbol::style() const
|
---|
| 106 | {
|
---|
| 107 | return d_data->style;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /*!
|
---|
| 111 | Specify the width of the symbol
|
---|
| 112 | It is used depending on the style.
|
---|
| 113 |
|
---|
| 114 | \param width Width
|
---|
| 115 | \sa width(), setStyle()
|
---|
| 116 | */
|
---|
| 117 | void QwtIntervalSymbol::setWidth( int width )
|
---|
| 118 | {
|
---|
| 119 | d_data->width = width;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /*!
|
---|
| 123 | \return Width of the symbol.
|
---|
| 124 | \sa setWidth(), setStyle()
|
---|
| 125 | */
|
---|
| 126 | int QwtIntervalSymbol::width() const
|
---|
| 127 | {
|
---|
| 128 | return d_data->width;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /*!
|
---|
| 132 | \brief Assign a brush
|
---|
| 133 |
|
---|
| 134 | The brush is used for the Box style.
|
---|
| 135 |
|
---|
| 136 | \param brush Brush
|
---|
| 137 | \sa brush()
|
---|
| 138 | */
|
---|
| 139 | void QwtIntervalSymbol::setBrush( const QBrush &brush )
|
---|
| 140 | {
|
---|
| 141 | d_data->brush = brush;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /*!
|
---|
| 145 | \return Brush
|
---|
| 146 | \sa setBrush()
|
---|
| 147 | */
|
---|
| 148 | const QBrush& QwtIntervalSymbol::brush() const
|
---|
| 149 | {
|
---|
| 150 | return d_data->brush;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /*!
|
---|
| 154 | Assign a pen
|
---|
| 155 |
|
---|
| 156 | \param pen Pen
|
---|
| 157 | \sa pen(), setBrush()
|
---|
| 158 | */
|
---|
| 159 | void QwtIntervalSymbol::setPen( const QPen &pen )
|
---|
| 160 | {
|
---|
| 161 | d_data->pen = pen;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /*!
|
---|
| 165 | \return Pen
|
---|
| 166 | \sa setPen(), brush()
|
---|
| 167 | */
|
---|
| 168 | const QPen& QwtIntervalSymbol::pen() const
|
---|
| 169 | {
|
---|
| 170 | return d_data->pen;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /*!
|
---|
| 174 | Draw a symbol depending on its style
|
---|
| 175 |
|
---|
| 176 | \param painter Painter
|
---|
| 177 | \param orientation Orientation
|
---|
| 178 | \param from Start point of the interval in target device coordinates
|
---|
| 179 | \param to End point of the interval in target device coordinates
|
---|
| 180 |
|
---|
| 181 | \sa setStyle()
|
---|
| 182 | */
|
---|
| 183 | void QwtIntervalSymbol::draw( QPainter *painter, Qt::Orientation orientation,
|
---|
| 184 | const QPointF &from, const QPointF &to ) const
|
---|
| 185 | {
|
---|
| 186 | const qreal pw = qMax( painter->pen().widthF(), qreal( 1.0 ) );
|
---|
| 187 |
|
---|
| 188 | QPointF p1 = from;
|
---|
| 189 | QPointF p2 = to;
|
---|
| 190 | if ( QwtPainter::roundingAlignment( painter ) )
|
---|
| 191 | {
|
---|
| 192 | p1 = p1.toPoint();
|
---|
| 193 | p2 = p2.toPoint();
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | switch ( d_data->style )
|
---|
| 197 | {
|
---|
| 198 | case QwtIntervalSymbol::Bar:
|
---|
| 199 | {
|
---|
| 200 | QwtPainter::drawLine( painter, p1, p2 );
|
---|
| 201 | if ( d_data->width > pw )
|
---|
| 202 | {
|
---|
| 203 | if ( ( orientation == Qt::Horizontal )
|
---|
| 204 | && ( p1.y() == p2.y() ) )
|
---|
| 205 | {
|
---|
| 206 | const double sw = d_data->width;
|
---|
| 207 |
|
---|
| 208 | const double y = p1.y() - sw / 2;
|
---|
| 209 | QwtPainter::drawLine( painter,
|
---|
| 210 | p1.x(), y, p1.x(), y + sw );
|
---|
| 211 | QwtPainter::drawLine( painter,
|
---|
| 212 | p2.x(), y, p2.x(), y + sw );
|
---|
| 213 | }
|
---|
| 214 | else if ( ( orientation == Qt::Vertical )
|
---|
| 215 | && ( p1.x() == p2.x() ) )
|
---|
| 216 | {
|
---|
| 217 | const double sw = d_data->width;
|
---|
| 218 |
|
---|
| 219 | const double x = p1.x() - sw / 2;
|
---|
| 220 | QwtPainter::drawLine( painter,
|
---|
| 221 | x, p1.y(), x + sw, p1.y() );
|
---|
| 222 | QwtPainter::drawLine( painter,
|
---|
| 223 | x, p2.y(), x + sw, p2.y() );
|
---|
| 224 | }
|
---|
| 225 | else
|
---|
| 226 | {
|
---|
| 227 | const double sw = d_data->width;
|
---|
| 228 |
|
---|
| 229 | const double dx = p2.x() - p1.x();
|
---|
| 230 | const double dy = p2.y() - p1.y();
|
---|
| 231 | const double angle = qAtan2( dy, dx ) + M_PI_2;
|
---|
| 232 | double dw2 = sw / 2.0;
|
---|
| 233 |
|
---|
| 234 | const double cx = qCos( angle ) * dw2;
|
---|
| 235 | const double sy = qSin( angle ) * dw2;
|
---|
| 236 |
|
---|
| 237 | QwtPainter::drawLine( painter,
|
---|
| 238 | p1.x() - cx, p1.y() - sy,
|
---|
| 239 | p1.x() + cx, p1.y() + sy );
|
---|
| 240 | QwtPainter::drawLine( painter,
|
---|
| 241 | p2.x() - cx, p2.y() - sy,
|
---|
| 242 | p2.x() + cx, p2.y() + sy );
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | break;
|
---|
| 246 | }
|
---|
| 247 | case QwtIntervalSymbol::Box:
|
---|
| 248 | {
|
---|
| 249 | if ( d_data->width <= pw )
|
---|
| 250 | {
|
---|
| 251 | QwtPainter::drawLine( painter, p1, p2 );
|
---|
| 252 | }
|
---|
| 253 | else
|
---|
| 254 | {
|
---|
| 255 | if ( ( orientation == Qt::Horizontal )
|
---|
| 256 | && ( p1.y() == p2.y() ) )
|
---|
| 257 | {
|
---|
| 258 | const double sw = d_data->width;
|
---|
| 259 |
|
---|
| 260 | const double y = p1.y() - d_data->width / 2;
|
---|
| 261 | QwtPainter::drawRect( painter,
|
---|
| 262 | p1.x(), y, p2.x() - p1.x(), sw );
|
---|
| 263 | }
|
---|
| 264 | else if ( ( orientation == Qt::Vertical )
|
---|
| 265 | && ( p1.x() == p2.x() ) )
|
---|
| 266 | {
|
---|
| 267 | const double sw = d_data->width;
|
---|
| 268 |
|
---|
| 269 | const double x = p1.x() - d_data->width / 2;
|
---|
| 270 | QwtPainter::drawRect( painter,
|
---|
| 271 | x, p1.y(), sw, p2.y() - p1.y() );
|
---|
| 272 | }
|
---|
| 273 | else
|
---|
| 274 | {
|
---|
| 275 | const double sw = d_data->width;
|
---|
| 276 |
|
---|
| 277 | const double dx = p2.x() - p1.x();
|
---|
| 278 | const double dy = p2.y() - p1.y();
|
---|
| 279 | const double angle = qAtan2( dy, dx ) + M_PI_2;
|
---|
| 280 | double dw2 = sw / 2.0;
|
---|
| 281 |
|
---|
| 282 | const int cx = qCos( angle ) * dw2;
|
---|
| 283 | const int sy = qSin( angle ) * dw2;
|
---|
| 284 |
|
---|
| 285 | QPolygonF polygon;
|
---|
| 286 | polygon += QPointF( p1.x() - cx, p1.y() - sy );
|
---|
| 287 | polygon += QPointF( p1.x() + cx, p1.y() + sy );
|
---|
| 288 | polygon += QPointF( p2.x() + cx, p2.y() + sy );
|
---|
| 289 | polygon += QPointF( p2.x() - cx, p2.y() - sy );
|
---|
| 290 |
|
---|
| 291 | QwtPainter::drawPolygon( painter, polygon );
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 | break;
|
---|
| 295 | }
|
---|
| 296 | default:;
|
---|
| 297 | }
|
---|
| 298 | }
|
---|