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