| 1 | /******************************************************************************
|
|---|
| 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_plot_intervalcurve.h"
|
|---|
| 11 | #include "qwt_interval_symbol.h"
|
|---|
| 12 | #include "qwt_scale_map.h"
|
|---|
| 13 | #include "qwt_clipper.h"
|
|---|
| 14 | #include "qwt_painter.h"
|
|---|
| 15 | #include "qwt_graphic.h"
|
|---|
| 16 | #include "qwt_text.h"
|
|---|
| 17 |
|
|---|
| 18 | #include <qpainter.h>
|
|---|
| 19 | #include <qmath.h>
|
|---|
| 20 | #include <cstring>
|
|---|
| 21 |
|
|---|
| 22 | static inline bool qwtIsHSampleInside( const QwtIntervalSample& sample,
|
|---|
| 23 | double xMin, double xMax, double yMin, double yMax )
|
|---|
| 24 | {
|
|---|
| 25 | const double y = sample.value;
|
|---|
| 26 | const double x1 = sample.interval.minValue();
|
|---|
| 27 | const double x2 = sample.interval.maxValue();
|
|---|
| 28 |
|
|---|
| 29 | const bool isOffScreen = ( y < yMin ) || ( y > yMax )
|
|---|
| 30 | || ( x1 < xMin && x2 < xMin ) || ( x1 > xMax && x2 > xMax );
|
|---|
| 31 |
|
|---|
| 32 | return !isOffScreen;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | static inline bool qwtIsVSampleInside( const QwtIntervalSample& sample,
|
|---|
| 36 | double xMin, double xMax, double yMin, double yMax )
|
|---|
| 37 | {
|
|---|
| 38 | const double x = sample.value;
|
|---|
| 39 | const double y1 = sample.interval.minValue();
|
|---|
| 40 | const double y2 = sample.interval.maxValue();
|
|---|
| 41 |
|
|---|
| 42 | const bool isOffScreen = ( x < xMin ) || ( x > xMax )
|
|---|
| 43 | || ( y1 < yMin && y2 < yMin ) || ( y1 > yMax && y2 > yMax );
|
|---|
| 44 |
|
|---|
| 45 | return !isOffScreen;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | static inline int qwtInterpolated( int from, int to, double ratio )
|
|---|
| 49 | {
|
|---|
| 50 | return qRound( from + ratio * ( to - from ) );
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | static int qwtStartIndex( int from, double min, double max,
|
|---|
| 54 | const QwtSeriesData< QwtIntervalSample >* samples )
|
|---|
| 55 | {
|
|---|
| 56 | /*
|
|---|
| 57 | As we know, that the value coordinates are montonically
|
|---|
| 58 | increasing/decreasing we can find the first sample before
|
|---|
| 59 | entering the interval using a binary search algo.
|
|---|
| 60 | */
|
|---|
| 61 |
|
|---|
| 62 | struct lessThan
|
|---|
| 63 | {
|
|---|
| 64 | inline bool operator()( const double value,
|
|---|
| 65 | const QwtIntervalSample& sample ) const
|
|---|
| 66 | {
|
|---|
| 67 | return value < sample.value;
|
|---|
| 68 | }
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | struct greaterThan
|
|---|
| 72 | {
|
|---|
| 73 | inline bool operator()( const double value,
|
|---|
| 74 | const QwtIntervalSample& sample ) const
|
|---|
| 75 | {
|
|---|
| 76 | return value > sample.value;
|
|---|
| 77 | }
|
|---|
| 78 | };
|
|---|
| 79 |
|
|---|
| 80 | int idx;
|
|---|
| 81 |
|
|---|
| 82 | if ( samples->firstSample().value < samples->lastSample().value )
|
|---|
| 83 | {
|
|---|
| 84 | idx = qwtUpperSampleIndex< QwtIntervalSample >(
|
|---|
| 85 | *samples, min, lessThan() );
|
|---|
| 86 | }
|
|---|
| 87 | else
|
|---|
| 88 | {
|
|---|
| 89 | idx = qwtUpperSampleIndex< QwtIntervalSample >(
|
|---|
| 90 | *samples, max, greaterThan() );
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | if ( idx >= 0 )
|
|---|
| 94 | {
|
|---|
| 95 | idx = qMax( from, idx );
|
|---|
| 96 | if ( idx > 0 )
|
|---|
| 97 | {
|
|---|
| 98 | // we need to fill the area before the first sample
|
|---|
| 99 | idx--;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | return idx;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | namespace
|
|---|
| 107 | {
|
|---|
| 108 | class LinesRenderer
|
|---|
| 109 | {
|
|---|
| 110 | public:
|
|---|
| 111 | LinesRenderer( Qt::Orientation,
|
|---|
| 112 | const QwtScaleMap&, const QwtScaleMap&, const QRectF& );
|
|---|
| 113 |
|
|---|
| 114 | void addSamples( const QwtSeriesData< QwtIntervalSample >*, int from, int to );
|
|---|
| 115 |
|
|---|
| 116 | QVector< QLine > fillLines;
|
|---|
| 117 | QVector< QLine > borderLines;
|
|---|
| 118 |
|
|---|
| 119 | private:
|
|---|
| 120 | bool addSample( const QwtIntervalSample& );
|
|---|
| 121 | void flush();
|
|---|
| 122 |
|
|---|
| 123 | bool append( int value, int min, int max );
|
|---|
| 124 |
|
|---|
| 125 | void addFillLineAt( int value, int min, int max );
|
|---|
| 126 | void addFillLines( int value, int min, int max );
|
|---|
| 127 |
|
|---|
| 128 | void addBorderLineAt( int value, int min, int max );
|
|---|
| 129 | void addBorderLine( int, int, int, int );
|
|---|
| 130 |
|
|---|
| 131 | const bool m_vertical;
|
|---|
| 132 |
|
|---|
| 133 | const QwtScaleMap& m_valueMap;
|
|---|
| 134 | const QwtScaleMap& m_intervalMap;
|
|---|
| 135 |
|
|---|
| 136 | const bool m_inverting;
|
|---|
| 137 | bool m_pending;
|
|---|
| 138 |
|
|---|
| 139 | // all ints are in paint device coordinates
|
|---|
| 140 |
|
|---|
| 141 | int m_intvMin, m_intvMax; // interval boundaries of the canvas
|
|---|
| 142 | int m_valueMin, m_valueMax; // value boundaries of the canvas
|
|---|
| 143 |
|
|---|
| 144 | int m_value; // value of the last sample being processed
|
|---|
| 145 |
|
|---|
| 146 | struct
|
|---|
| 147 | {
|
|---|
| 148 | inline void reset( int value )
|
|---|
| 149 | {
|
|---|
| 150 | min = max = last = value;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | inline void extend( int value )
|
|---|
| 154 | {
|
|---|
| 155 | if ( value < min )
|
|---|
| 156 | min = value;
|
|---|
| 157 | else if ( value > max )
|
|---|
| 158 | max = value;
|
|---|
| 159 |
|
|---|
| 160 | last = value;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | int min, max, last;
|
|---|
| 164 |
|
|---|
| 165 | } m_lower, m_upper;
|
|---|
| 166 | };
|
|---|
| 167 |
|
|---|
| 168 | LinesRenderer::LinesRenderer( Qt::Orientation orientation,
|
|---|
| 169 | const QwtScaleMap& xMap, const QwtScaleMap& yMap, const QRectF& rect )
|
|---|
| 170 | : m_vertical( orientation == Qt::Vertical )
|
|---|
| 171 | , m_valueMap( m_vertical ? xMap : yMap )
|
|---|
| 172 | , m_intervalMap( m_vertical ? yMap : xMap )
|
|---|
| 173 | , m_inverting( m_intervalMap.isInverting() )
|
|---|
| 174 | , m_pending( false )
|
|---|
| 175 | , m_value( 0 )
|
|---|
| 176 | {
|
|---|
| 177 | QRectF r = rect;
|
|---|
| 178 | if ( !m_vertical )
|
|---|
| 179 | r.setSize( QSizeF( r.height(), r.width() ) );
|
|---|
| 180 |
|
|---|
| 181 | m_intvMin = qFloor( r.top() );
|
|---|
| 182 | m_intvMax = qCeil( r.bottom() );
|
|---|
| 183 |
|
|---|
| 184 | m_valueMin = qFloor( r.left() );
|
|---|
| 185 | m_valueMax = qCeil( r.right() );
|
|---|
| 186 |
|
|---|
| 187 | const int steps = m_valueMax - m_valueMin + 1;
|
|---|
| 188 |
|
|---|
| 189 | // one line for each pixel
|
|---|
| 190 | fillLines.reserve( steps );
|
|---|
| 191 |
|
|---|
| 192 | /*
|
|---|
| 193 | for both border lines:
|
|---|
| 194 | one line ( min/max ) of the samples lying on the same pixel
|
|---|
| 195 | one line connecting the pixels
|
|---|
| 196 | */
|
|---|
| 197 | borderLines.reserve( 4 * steps );
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | void LinesRenderer::addSamples(
|
|---|
| 201 | const QwtSeriesData< QwtIntervalSample >* samples, int from, int to )
|
|---|
| 202 | {
|
|---|
| 203 | const double v1 = m_valueMap.invTransform( m_valueMin );
|
|---|
| 204 | const double v2 = m_valueMap.invTransform( m_valueMax );
|
|---|
| 205 |
|
|---|
| 206 | from = qwtStartIndex( from, qMin( v1, v2 ), qMax( v1, v2 ), samples );
|
|---|
| 207 |
|
|---|
| 208 | if ( from >= 0 )
|
|---|
| 209 | {
|
|---|
| 210 | for ( int i = from; i <= to; i++ )
|
|---|
| 211 | {
|
|---|
| 212 | if ( addSample( samples->sample( i ) ) )
|
|---|
| 213 | break;
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | flush();
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | inline bool LinesRenderer::addSample( const QwtIntervalSample& sample )
|
|---|
| 221 | {
|
|---|
| 222 | const double value = qRound( m_valueMap.transform( sample.value ) );
|
|---|
| 223 |
|
|---|
| 224 | const int min = qRound( m_intervalMap.transform( sample.interval.minValue() ) );
|
|---|
| 225 | const int max = qRound( m_intervalMap.transform( sample.interval.maxValue() ) );
|
|---|
| 226 |
|
|---|
| 227 | return m_inverting ? append( value, max, min ) : append( value, max, min );
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | inline bool LinesRenderer::append(
|
|---|
| 231 | const int value, const int lower, const int upper )
|
|---|
| 232 | {
|
|---|
| 233 | if ( !m_pending )
|
|---|
| 234 | {
|
|---|
| 235 | m_value = value;
|
|---|
| 236 | m_lower.reset( lower );
|
|---|
| 237 | m_upper.reset( upper );
|
|---|
| 238 |
|
|---|
| 239 | m_pending = true;
|
|---|
| 240 |
|
|---|
| 241 | return false;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | if ( value != m_value )
|
|---|
| 245 | {
|
|---|
| 246 | addFillLines( value, lower, upper );
|
|---|
| 247 | addBorderLineAt( m_value, m_lower.min, m_lower.max );
|
|---|
| 248 | addBorderLineAt( m_value, m_upper.min, m_upper.max );
|
|---|
| 249 |
|
|---|
| 250 | addBorderLine( m_value, m_lower.last, value, lower );
|
|---|
| 251 | addBorderLine( m_value, m_upper.last, value, upper );
|
|---|
| 252 |
|
|---|
| 253 | if ( ( value > m_value ) ? ( value > m_valueMax ) : ( value < m_valueMin ) )
|
|---|
| 254 | {
|
|---|
| 255 | m_pending = false;
|
|---|
| 256 | return true;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | m_value = value;
|
|---|
| 260 |
|
|---|
| 261 | m_lower.reset( lower );
|
|---|
| 262 | m_upper.reset( upper );
|
|---|
| 263 | }
|
|---|
| 264 | else
|
|---|
| 265 | {
|
|---|
| 266 | m_lower.extend( lower );
|
|---|
| 267 | m_upper.extend( upper );
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | return false;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | inline void LinesRenderer::flush()
|
|---|
| 274 | {
|
|---|
| 275 | if ( m_pending )
|
|---|
| 276 | {
|
|---|
| 277 | addFillLineAt( m_value, m_lower.min, m_upper.max );
|
|---|
| 278 | addBorderLineAt( m_value, m_lower.min, m_lower.max );
|
|---|
| 279 | addBorderLineAt( m_value, m_upper.min, m_upper.max );
|
|---|
| 280 | }
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | inline void LinesRenderer::addFillLines(
|
|---|
| 284 | const int value, const int lower, const int upper )
|
|---|
| 285 | {
|
|---|
| 286 | addFillLineAt( m_value, m_lower.min, m_upper.max );
|
|---|
| 287 |
|
|---|
| 288 | const double delta = value - m_value;
|
|---|
| 289 |
|
|---|
| 290 | if ( value > m_value )
|
|---|
| 291 | {
|
|---|
| 292 | for ( int v = m_value + 1; v < value; v++ )
|
|---|
| 293 | {
|
|---|
| 294 | const double ratio = ( v - m_value ) / delta;
|
|---|
| 295 |
|
|---|
| 296 | addFillLineAt( v,
|
|---|
| 297 | qwtInterpolated( m_lower.last, lower, ratio ),
|
|---|
| 298 | qwtInterpolated( m_upper.last, upper, ratio ) );
|
|---|
| 299 | }
|
|---|
| 300 | }
|
|---|
| 301 | else
|
|---|
| 302 | {
|
|---|
| 303 | for ( int v = m_value - 1; v > value; v-- )
|
|---|
| 304 | {
|
|---|
| 305 | const double ratio = ( v - m_value ) / delta;
|
|---|
| 306 |
|
|---|
| 307 | addFillLineAt( v,
|
|---|
| 308 | qwtInterpolated( m_lower.last, lower, ratio ),
|
|---|
| 309 | qwtInterpolated( m_upper.last, upper, ratio ) );
|
|---|
| 310 | }
|
|---|
| 311 | }
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | inline void LinesRenderer::addFillLineAt( int value, int min, int max )
|
|---|
| 315 | {
|
|---|
| 316 | if ( ( min != max ) && ( max > m_intvMin ) && ( min < m_intvMax ) )
|
|---|
| 317 | {
|
|---|
| 318 | min = qMax( min, m_intvMin );
|
|---|
| 319 | max = qMin( max, m_intvMax );
|
|---|
| 320 |
|
|---|
| 321 | if( m_vertical )
|
|---|
| 322 | fillLines += QLine( value, min, value, max );
|
|---|
| 323 | else
|
|---|
| 324 | fillLines += QLine( min, value, max, value );
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | inline void LinesRenderer::addBorderLineAt( int value, int min, int max )
|
|---|
| 329 | {
|
|---|
| 330 | if ( min != max )
|
|---|
| 331 | addBorderLine( value, min, value, max );
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | inline void LinesRenderer::addBorderLine( int x1, int y1, int x2, int y2 )
|
|---|
| 335 | {
|
|---|
| 336 | if( m_vertical )
|
|---|
| 337 | borderLines += QLine( x1, y1, x2, y2 );
|
|---|
| 338 | else
|
|---|
| 339 | borderLines += QLine( y1, x1, y2, x2 );
|
|---|
| 340 | }
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | static void qwtDrawTubeLines(
|
|---|
| 344 | const QwtPlotIntervalCurve* curve, QPainter* painter,
|
|---|
| 345 | const QwtScaleMap& xMap, const QwtScaleMap& yMap,
|
|---|
| 346 | const QRectF& canvasRect, int from, int to)
|
|---|
| 347 | {
|
|---|
| 348 | LinesRenderer renderer( curve->orientation(), xMap, yMap, canvasRect );
|
|---|
| 349 | renderer.addSamples( curve->data(), from, to );
|
|---|
| 350 |
|
|---|
| 351 | if ( curve->brush().style() != Qt::NoBrush )
|
|---|
| 352 | {
|
|---|
| 353 | painter->save();
|
|---|
| 354 |
|
|---|
| 355 | painter->setPen( curve->brush().color() );
|
|---|
| 356 | painter->setRenderHint( QPainter::Antialiasing, false );
|
|---|
| 357 | painter->drawLines( renderer.fillLines );
|
|---|
| 358 |
|
|---|
| 359 | painter->restore();
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | if ( curve->pen().style() != Qt::NoPen )
|
|---|
| 363 | {
|
|---|
| 364 | painter->save();
|
|---|
| 365 |
|
|---|
| 366 | painter->setPen( curve->pen() );
|
|---|
| 367 | painter->drawLines( renderer.borderLines );
|
|---|
| 368 |
|
|---|
| 369 | painter->restore();
|
|---|
| 370 | }
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | static void qwtDrawTube(
|
|---|
| 374 | const QwtPlotIntervalCurve* curve, QPainter* painter,
|
|---|
| 375 | const QwtScaleMap& xMap, const QwtScaleMap& yMap,
|
|---|
| 376 | const QRectF& canvasRect, int from, int to )
|
|---|
| 377 | {
|
|---|
| 378 | const bool doAlign = QwtPainter::roundingAlignment( painter );
|
|---|
| 379 |
|
|---|
| 380 | painter->save();
|
|---|
| 381 |
|
|---|
| 382 | const size_t size = to - from + 1;
|
|---|
| 383 | QPolygonF polygon( 2 * size );
|
|---|
| 384 | QPointF* points = polygon.data();
|
|---|
| 385 |
|
|---|
| 386 | for ( uint i = 0; i < size; i++ )
|
|---|
| 387 | {
|
|---|
| 388 | QPointF& minValue = points[i];
|
|---|
| 389 | QPointF& maxValue = points[2 * size - 1 - i];
|
|---|
| 390 |
|
|---|
| 391 | const QwtIntervalSample intervalSample = curve->sample( from + i );
|
|---|
| 392 | if ( curve->orientation() == Qt::Vertical )
|
|---|
| 393 | {
|
|---|
| 394 | double x = xMap.transform( intervalSample.value );
|
|---|
| 395 | double y1 = yMap.transform( intervalSample.interval.minValue() );
|
|---|
| 396 | double y2 = yMap.transform( intervalSample.interval.maxValue() );
|
|---|
| 397 |
|
|---|
| 398 | if ( doAlign )
|
|---|
| 399 | {
|
|---|
| 400 | x = qRound( x );
|
|---|
| 401 | y1 = qRound( y1 );
|
|---|
| 402 | y2 = qRound( y2 );
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | minValue.rx() = x;
|
|---|
| 406 | minValue.ry() = y1;
|
|---|
| 407 | maxValue.rx() = x;
|
|---|
| 408 | maxValue.ry() = y2;
|
|---|
| 409 | }
|
|---|
| 410 | else
|
|---|
| 411 | {
|
|---|
| 412 | double y = yMap.transform( intervalSample.value );
|
|---|
| 413 | double x1 = xMap.transform( intervalSample.interval.minValue() );
|
|---|
| 414 | double x2 = xMap.transform( intervalSample.interval.maxValue() );
|
|---|
| 415 |
|
|---|
| 416 | if ( doAlign )
|
|---|
| 417 | {
|
|---|
| 418 | y = qRound( y );
|
|---|
| 419 | x1 = qRound( x1 );
|
|---|
| 420 | x2 = qRound( x2 );
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | minValue.rx() = x1;
|
|---|
| 424 | minValue.ry() = y;
|
|---|
| 425 | maxValue.rx() = x2;
|
|---|
| 426 | maxValue.ry() = y;
|
|---|
| 427 | }
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | const bool doClip = curve->testPaintAttribute( QwtPlotIntervalCurve::ClipPolygons );
|
|---|
| 431 |
|
|---|
| 432 | if ( curve->brush().style() != Qt::NoBrush )
|
|---|
| 433 | {
|
|---|
| 434 | painter->setPen( QPen( Qt::NoPen ) );
|
|---|
| 435 | painter->setBrush( curve->brush() );
|
|---|
| 436 |
|
|---|
| 437 | if ( doClip )
|
|---|
| 438 | {
|
|---|
| 439 | const qreal m = 1.0;
|
|---|
| 440 | const QPolygonF p = QwtClipper::clippedPolygonF(
|
|---|
| 441 | canvasRect.adjusted( -m, -m, m, m ), polygon, true );
|
|---|
| 442 |
|
|---|
| 443 | QwtPainter::drawPolygon( painter, p );
|
|---|
| 444 | }
|
|---|
| 445 | else
|
|---|
| 446 | {
|
|---|
| 447 | QwtPainter::drawPolygon( painter, polygon );
|
|---|
| 448 | }
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | if ( curve->pen().style() != Qt::NoPen )
|
|---|
| 452 | {
|
|---|
| 453 | painter->setPen( curve->pen() );
|
|---|
| 454 | painter->setBrush( Qt::NoBrush );
|
|---|
| 455 |
|
|---|
| 456 | if ( doClip )
|
|---|
| 457 | {
|
|---|
| 458 | qreal pw = QwtPainter::effectivePenWidth( painter->pen() );
|
|---|
| 459 | const QRectF clipRect = canvasRect.adjusted( -pw, -pw, pw, pw );
|
|---|
| 460 |
|
|---|
| 461 | QPolygonF p( size );
|
|---|
| 462 |
|
|---|
| 463 | std::memcpy( p.data(), points, size * sizeof( QPointF ) );
|
|---|
| 464 | QwtPainter::drawPolyline( painter,
|
|---|
| 465 | QwtClipper::clippedPolygonF( clipRect, p ) );
|
|---|
| 466 |
|
|---|
| 467 | std::memcpy( p.data(), points + size, size * sizeof( QPointF ) );
|
|---|
| 468 | QwtPainter::drawPolyline( painter,
|
|---|
| 469 | QwtClipper::clippedPolygonF( clipRect, p ) );
|
|---|
| 470 | }
|
|---|
| 471 | else
|
|---|
| 472 | {
|
|---|
| 473 | QwtPainter::drawPolyline( painter, points, size );
|
|---|
| 474 | QwtPainter::drawPolyline( painter, points + size, size );
|
|---|
| 475 | }
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | painter->restore();
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 | class QwtPlotIntervalCurve::PrivateData
|
|---|
| 482 | {
|
|---|
| 483 | public:
|
|---|
| 484 | PrivateData():
|
|---|
| 485 | style( QwtPlotIntervalCurve::Tube ),
|
|---|
| 486 | symbol( NULL ),
|
|---|
| 487 | pen( Qt::black ),
|
|---|
| 488 | brush( Qt::white )
|
|---|
| 489 | {
|
|---|
| 490 | paintAttributes = QwtPlotIntervalCurve::ClipPolygons;
|
|---|
| 491 | paintAttributes |= QwtPlotIntervalCurve::ClipSymbol;
|
|---|
| 492 |
|
|---|
| 493 | pen.setCapStyle( Qt::FlatCap );
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | ~PrivateData()
|
|---|
| 497 | {
|
|---|
| 498 | delete symbol;
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | QwtPlotIntervalCurve::CurveStyle style;
|
|---|
| 502 | const QwtIntervalSymbol* symbol;
|
|---|
| 503 |
|
|---|
| 504 | QPen pen;
|
|---|
| 505 | QBrush brush;
|
|---|
| 506 |
|
|---|
| 507 | QwtPlotIntervalCurve::PaintAttributes paintAttributes;
|
|---|
| 508 | };
|
|---|
| 509 |
|
|---|
| 510 | /*!
|
|---|
| 511 | Constructor
|
|---|
| 512 | \param title Title of the curve
|
|---|
| 513 | */
|
|---|
| 514 | QwtPlotIntervalCurve::QwtPlotIntervalCurve( const QwtText& title )
|
|---|
| 515 | : QwtPlotSeriesItem( title )
|
|---|
| 516 | {
|
|---|
| 517 | init();
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | /*!
|
|---|
| 521 | Constructor
|
|---|
| 522 | \param title Title of the curve
|
|---|
| 523 | */
|
|---|
| 524 | QwtPlotIntervalCurve::QwtPlotIntervalCurve( const QString& title )
|
|---|
| 525 | : QwtPlotSeriesItem( QwtText( title ) )
|
|---|
| 526 | {
|
|---|
| 527 | init();
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | //! Destructor
|
|---|
| 531 | QwtPlotIntervalCurve::~QwtPlotIntervalCurve()
|
|---|
| 532 | {
|
|---|
| 533 | delete m_data;
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | //! Initialize internal members
|
|---|
| 537 | void QwtPlotIntervalCurve::init()
|
|---|
| 538 | {
|
|---|
| 539 | setItemAttribute( QwtPlotItem::Legend, true );
|
|---|
| 540 | setItemAttribute( QwtPlotItem::AutoScale, true );
|
|---|
| 541 |
|
|---|
| 542 | m_data = new PrivateData;
|
|---|
| 543 | setData( new QwtIntervalSeriesData() );
|
|---|
| 544 |
|
|---|
| 545 | setZ( 19.0 );
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | //! \return QwtPlotItem::Rtti_PlotIntervalCurve
|
|---|
| 549 | int QwtPlotIntervalCurve::rtti() const
|
|---|
| 550 | {
|
|---|
| 551 | return QwtPlotIntervalCurve::Rtti_PlotIntervalCurve;
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | /*!
|
|---|
| 555 | Specify an attribute how to draw the curve
|
|---|
| 556 |
|
|---|
| 557 | \param attribute Paint attribute
|
|---|
| 558 | \param on On/Off
|
|---|
| 559 | \sa testPaintAttribute()
|
|---|
| 560 | */
|
|---|
| 561 | void QwtPlotIntervalCurve::setPaintAttribute(
|
|---|
| 562 | PaintAttribute attribute, bool on )
|
|---|
| 563 | {
|
|---|
| 564 | if ( on )
|
|---|
| 565 | m_data->paintAttributes |= attribute;
|
|---|
| 566 | else
|
|---|
| 567 | m_data->paintAttributes &= ~attribute;
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | /*!
|
|---|
| 571 | \return True, when attribute is enabled
|
|---|
| 572 | \sa PaintAttribute, setPaintAttribute()
|
|---|
| 573 | */
|
|---|
| 574 | bool QwtPlotIntervalCurve::testPaintAttribute(
|
|---|
| 575 | PaintAttribute attribute ) const
|
|---|
| 576 | {
|
|---|
| 577 | return ( m_data->paintAttributes & attribute );
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | /*!
|
|---|
| 581 | Initialize data with an array of samples.
|
|---|
| 582 | \param samples Vector of samples
|
|---|
| 583 | */
|
|---|
| 584 | void QwtPlotIntervalCurve::setSamples(
|
|---|
| 585 | const QVector< QwtIntervalSample >& samples )
|
|---|
| 586 | {
|
|---|
| 587 | setData( new QwtIntervalSeriesData( samples ) );
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | /*!
|
|---|
| 591 | Assign a series of samples
|
|---|
| 592 |
|
|---|
| 593 | setSamples() is just a wrapper for setData() without any additional
|
|---|
| 594 | value - beside that it is easier to find for the developer.
|
|---|
| 595 |
|
|---|
| 596 | \param data Data
|
|---|
| 597 | \warning The item takes ownership of the data object, deleting
|
|---|
| 598 | it when its not used anymore.
|
|---|
| 599 | */
|
|---|
| 600 | void QwtPlotIntervalCurve::setSamples(
|
|---|
| 601 | QwtSeriesData< QwtIntervalSample >* data )
|
|---|
| 602 | {
|
|---|
| 603 | setData( data );
|
|---|
| 604 | }
|
|---|
| 605 |
|
|---|
| 606 | /*!
|
|---|
| 607 | Set the curve's drawing style
|
|---|
| 608 |
|
|---|
| 609 | \param style Curve style
|
|---|
| 610 | \sa CurveStyle, style()
|
|---|
| 611 | */
|
|---|
| 612 | void QwtPlotIntervalCurve::setStyle( CurveStyle style )
|
|---|
| 613 | {
|
|---|
| 614 | if ( style != m_data->style )
|
|---|
| 615 | {
|
|---|
| 616 | m_data->style = style;
|
|---|
| 617 |
|
|---|
| 618 | legendChanged();
|
|---|
| 619 | itemChanged();
|
|---|
| 620 | }
|
|---|
| 621 | }
|
|---|
| 622 |
|
|---|
| 623 | /*!
|
|---|
| 624 | \return Style of the curve
|
|---|
| 625 | \sa setStyle()
|
|---|
| 626 | */
|
|---|
| 627 | QwtPlotIntervalCurve::CurveStyle QwtPlotIntervalCurve::style() const
|
|---|
| 628 | {
|
|---|
| 629 | return m_data->style;
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | /*!
|
|---|
| 633 | Assign a symbol.
|
|---|
| 634 |
|
|---|
| 635 | \param symbol Symbol
|
|---|
| 636 | \sa symbol()
|
|---|
| 637 | */
|
|---|
| 638 | void QwtPlotIntervalCurve::setSymbol( const QwtIntervalSymbol* symbol )
|
|---|
| 639 | {
|
|---|
| 640 | if ( symbol != m_data->symbol )
|
|---|
| 641 | {
|
|---|
| 642 | delete m_data->symbol;
|
|---|
| 643 | m_data->symbol = symbol;
|
|---|
| 644 |
|
|---|
| 645 | legendChanged();
|
|---|
| 646 | itemChanged();
|
|---|
| 647 | }
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | /*!
|
|---|
| 651 | \return Current symbol or NULL, when no symbol has been assigned
|
|---|
| 652 | \sa setSymbol()
|
|---|
| 653 | */
|
|---|
| 654 | const QwtIntervalSymbol* QwtPlotIntervalCurve::symbol() const
|
|---|
| 655 | {
|
|---|
| 656 | return m_data->symbol;
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 | /*!
|
|---|
| 660 | Build and assign a pen
|
|---|
| 661 |
|
|---|
| 662 | In Qt5 the default pen width is 1.0 ( 0.0 in Qt4 ) what makes it
|
|---|
| 663 | non cosmetic ( see QPen::isCosmetic() ). This method has been introduced
|
|---|
| 664 | to hide this incompatibility.
|
|---|
| 665 |
|
|---|
| 666 | \param color Pen color
|
|---|
| 667 | \param width Pen width
|
|---|
| 668 | \param style Pen style
|
|---|
| 669 |
|
|---|
| 670 | \sa pen(), brush()
|
|---|
| 671 | */
|
|---|
| 672 | void QwtPlotIntervalCurve::setPen( const QColor& color, qreal width, Qt::PenStyle style )
|
|---|
| 673 | {
|
|---|
| 674 | setPen( QPen( color, width, style ) );
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | /*!
|
|---|
| 678 | \brief Assign a pen
|
|---|
| 679 | \param pen New pen
|
|---|
| 680 | \sa pen(), brush()
|
|---|
| 681 | */
|
|---|
| 682 | void QwtPlotIntervalCurve::setPen( const QPen& pen )
|
|---|
| 683 | {
|
|---|
| 684 | if ( pen != m_data->pen )
|
|---|
| 685 | {
|
|---|
| 686 | m_data->pen = pen;
|
|---|
| 687 |
|
|---|
| 688 | legendChanged();
|
|---|
| 689 | itemChanged();
|
|---|
| 690 | }
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | /*!
|
|---|
| 694 | \return Pen used to draw the lines
|
|---|
| 695 | \sa setPen(), brush()
|
|---|
| 696 | */
|
|---|
| 697 | const QPen& QwtPlotIntervalCurve::pen() const
|
|---|
| 698 | {
|
|---|
| 699 | return m_data->pen;
|
|---|
| 700 | }
|
|---|
| 701 |
|
|---|
| 702 | /*!
|
|---|
| 703 | Assign a brush.
|
|---|
| 704 |
|
|---|
| 705 | The brush is used to fill the area in Tube style().
|
|---|
| 706 |
|
|---|
| 707 | \param brush Brush
|
|---|
| 708 | \sa brush(), pen(), setStyle(), CurveStyle
|
|---|
| 709 | */
|
|---|
| 710 | void QwtPlotIntervalCurve::setBrush( const QBrush& brush )
|
|---|
| 711 | {
|
|---|
| 712 | if ( brush != m_data->brush )
|
|---|
| 713 | {
|
|---|
| 714 | m_data->brush = brush;
|
|---|
| 715 |
|
|---|
| 716 | legendChanged();
|
|---|
| 717 | itemChanged();
|
|---|
| 718 | }
|
|---|
| 719 | }
|
|---|
| 720 |
|
|---|
| 721 | /*!
|
|---|
| 722 | \return Brush used to fill the area in Tube style()
|
|---|
| 723 | \sa setBrush(), setStyle(), CurveStyle
|
|---|
| 724 | */
|
|---|
| 725 | const QBrush& QwtPlotIntervalCurve::brush() const
|
|---|
| 726 | {
|
|---|
| 727 | return m_data->brush;
|
|---|
| 728 | }
|
|---|
| 729 |
|
|---|
| 730 | /*!
|
|---|
| 731 | \return Bounding rectangle of all samples.
|
|---|
| 732 | For an empty series the rectangle is invalid.
|
|---|
| 733 | */
|
|---|
| 734 | QRectF QwtPlotIntervalCurve::boundingRect() const
|
|---|
| 735 | {
|
|---|
| 736 | QRectF rect = QwtPlotSeriesItem::boundingRect();
|
|---|
| 737 | if ( orientation() == Qt::Vertical )
|
|---|
| 738 | rect.setRect( rect.y(), rect.x(), rect.height(), rect.width() );
|
|---|
| 739 |
|
|---|
| 740 | return rect;
|
|---|
| 741 | }
|
|---|
| 742 |
|
|---|
| 743 | /*!
|
|---|
| 744 | Draw a subset of the samples
|
|---|
| 745 |
|
|---|
| 746 | \param painter Painter
|
|---|
| 747 | \param xMap Maps x-values into pixel coordinates.
|
|---|
| 748 | \param yMap Maps y-values into pixel coordinates.
|
|---|
| 749 | \param canvasRect Contents rectangle of the canvas
|
|---|
| 750 | \param from Index of the first sample to be painted
|
|---|
| 751 | \param to Index of the last sample to be painted. If to < 0 the
|
|---|
| 752 | series will be painted to its last sample.
|
|---|
| 753 |
|
|---|
| 754 | \sa drawTube(), drawSymbols()
|
|---|
| 755 | */
|
|---|
| 756 | void QwtPlotIntervalCurve::drawSeries( QPainter* painter,
|
|---|
| 757 | const QwtScaleMap& xMap, const QwtScaleMap& yMap,
|
|---|
| 758 | const QRectF& canvasRect, int from, int to ) const
|
|---|
| 759 | {
|
|---|
| 760 | if ( to < 0 )
|
|---|
| 761 | to = dataSize() - 1;
|
|---|
| 762 |
|
|---|
| 763 | if ( from < 0 )
|
|---|
| 764 | from = 0;
|
|---|
| 765 |
|
|---|
| 766 | if ( from > to )
|
|---|
| 767 | return;
|
|---|
| 768 |
|
|---|
| 769 | if ( m_data->style == Tube )
|
|---|
| 770 | drawTube( painter, xMap, yMap, canvasRect, from, to );
|
|---|
| 771 |
|
|---|
| 772 | if ( m_data->symbol &&
|
|---|
| 773 | ( m_data->symbol->style() != QwtIntervalSymbol::NoSymbol ) )
|
|---|
| 774 | {
|
|---|
| 775 | drawSymbols( painter, *m_data->symbol,
|
|---|
| 776 | xMap, yMap, canvasRect, from, to );
|
|---|
| 777 | }
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | /*!
|
|---|
| 781 | Draw a tube
|
|---|
| 782 |
|
|---|
| 783 | Builds 2 curves from the upper and lower limits of the intervals
|
|---|
| 784 | and draws them with the pen(). The area between the curves is
|
|---|
| 785 | filled with the brush().
|
|---|
| 786 |
|
|---|
| 787 | \param painter Painter
|
|---|
| 788 | \param xMap Maps x-values into pixel coordinates.
|
|---|
| 789 | \param yMap Maps y-values into pixel coordinates.
|
|---|
| 790 | \param canvasRect Contents rectangle of the canvas
|
|---|
| 791 | \param from Index of the first sample to be painted
|
|---|
| 792 | \param to Index of the last sample to be painted. If to < 0 the
|
|---|
| 793 | series will be painted to its last sample.
|
|---|
| 794 |
|
|---|
| 795 | \sa drawSeries(), drawSymbols()
|
|---|
| 796 | */
|
|---|
| 797 | void QwtPlotIntervalCurve::drawTube(
|
|---|
| 798 | QPainter* painter, const QwtScaleMap& xMap,
|
|---|
| 799 | const QwtScaleMap& yMap, const QRectF& canvasRect, int from, int to) const
|
|---|
| 800 | {
|
|---|
| 801 | if ( ( m_data->pen.style() == Qt::NoPen ) &&
|
|---|
| 802 | ( m_data->brush.style() == Qt::NoBrush ) )
|
|---|
| 803 | {
|
|---|
| 804 | return;
|
|---|
| 805 | }
|
|---|
| 806 |
|
|---|
| 807 | const bool useLines = testPaintAttribute( TubeAsLines )
|
|---|
| 808 | && QwtPainter::roundingAlignment( painter );
|
|---|
| 809 |
|
|---|
| 810 | if ( useLines )
|
|---|
| 811 | qwtDrawTubeLines( this, painter, xMap, yMap, canvasRect, from, to );
|
|---|
| 812 | else
|
|---|
| 813 | qwtDrawTube( this, painter, xMap, yMap, canvasRect, from, to );
|
|---|
| 814 | }
|
|---|
| 815 |
|
|---|
| 816 | /*!
|
|---|
| 817 | Draw symbols for a subset of the samples
|
|---|
| 818 |
|
|---|
| 819 | \param painter Painter
|
|---|
| 820 | \param symbol Interval symbol
|
|---|
| 821 | \param xMap x map
|
|---|
| 822 | \param yMap y map
|
|---|
| 823 | \param canvasRect Contents rectangle of the canvas
|
|---|
| 824 | \param from Index of the first sample to be painted
|
|---|
| 825 | \param to Index of the last sample to be painted
|
|---|
| 826 |
|
|---|
| 827 | \sa setSymbol(), drawSeries(), drawTube()
|
|---|
| 828 | */
|
|---|
| 829 | void QwtPlotIntervalCurve::drawSymbols(
|
|---|
| 830 | QPainter* painter, const QwtIntervalSymbol& symbol,
|
|---|
| 831 | const QwtScaleMap& xMap, const QwtScaleMap& yMap,
|
|---|
| 832 | const QRectF& canvasRect, int from, int to ) const
|
|---|
| 833 | {
|
|---|
| 834 | painter->save();
|
|---|
| 835 |
|
|---|
| 836 | QPen pen = symbol.pen();
|
|---|
| 837 | pen.setCapStyle( Qt::FlatCap );
|
|---|
| 838 |
|
|---|
| 839 | painter->setPen( pen );
|
|---|
| 840 | painter->setBrush( symbol.brush() );
|
|---|
| 841 |
|
|---|
| 842 | const QRectF tr = QwtScaleMap::invTransform( xMap, yMap, canvasRect );
|
|---|
| 843 |
|
|---|
| 844 | const double xMin = tr.left();
|
|---|
| 845 | const double xMax = tr.right();
|
|---|
| 846 | const double yMin = tr.top();
|
|---|
| 847 | const double yMax = tr.bottom();
|
|---|
| 848 |
|
|---|
| 849 | const bool doClip = m_data->paintAttributes & ClipSymbol;
|
|---|
| 850 |
|
|---|
| 851 | for ( int i = from; i <= to; i++ )
|
|---|
| 852 | {
|
|---|
| 853 | const QwtIntervalSample s = sample( i );
|
|---|
| 854 |
|
|---|
| 855 | if ( orientation() == Qt::Vertical )
|
|---|
| 856 | {
|
|---|
| 857 | if ( !doClip || qwtIsVSampleInside( s, xMin, xMax, yMin, yMax ) )
|
|---|
| 858 | {
|
|---|
| 859 | const double x = xMap.transform( s.value );
|
|---|
| 860 | const double y1 = yMap.transform( s.interval.minValue() );
|
|---|
| 861 | const double y2 = yMap.transform( s.interval.maxValue() );
|
|---|
| 862 |
|
|---|
| 863 | symbol.draw( painter, orientation(),
|
|---|
| 864 | QPointF( x, y1 ), QPointF( x, y2 ) );
|
|---|
| 865 | }
|
|---|
| 866 | }
|
|---|
| 867 | else
|
|---|
| 868 | {
|
|---|
| 869 | if ( !doClip || qwtIsHSampleInside( s, xMin, xMax, yMin, yMax ) )
|
|---|
| 870 | {
|
|---|
| 871 | const double y = yMap.transform( s.value );
|
|---|
| 872 | const double x1 = xMap.transform( s.interval.minValue() );
|
|---|
| 873 | const double x2 = xMap.transform( s.interval.maxValue() );
|
|---|
| 874 |
|
|---|
| 875 | symbol.draw( painter, orientation(),
|
|---|
| 876 | QPointF( x1, y ), QPointF( x2, y ) );
|
|---|
| 877 | }
|
|---|
| 878 | }
|
|---|
| 879 | }
|
|---|
| 880 |
|
|---|
| 881 | painter->restore();
|
|---|
| 882 | }
|
|---|
| 883 |
|
|---|
| 884 | /*!
|
|---|
| 885 | \return Icon for the legend
|
|---|
| 886 |
|
|---|
| 887 | In case of Tube style() the icon is a plain rectangle filled with the brush().
|
|---|
| 888 | If a symbol is assigned it is scaled to size.
|
|---|
| 889 |
|
|---|
| 890 | \param index Index of the legend entry
|
|---|
| 891 | ( ignored as there is only one )
|
|---|
| 892 | \param size Icon size
|
|---|
| 893 |
|
|---|
| 894 | \sa QwtPlotItem::setLegendIconSize(), QwtPlotItem::legendData()
|
|---|
| 895 | */
|
|---|
| 896 | QwtGraphic QwtPlotIntervalCurve::legendIcon(
|
|---|
| 897 | int index, const QSizeF& size ) const
|
|---|
| 898 | {
|
|---|
| 899 | Q_UNUSED( index );
|
|---|
| 900 |
|
|---|
| 901 | if ( size.isEmpty() )
|
|---|
| 902 | return QwtGraphic();
|
|---|
| 903 |
|
|---|
| 904 | QwtGraphic icon;
|
|---|
| 905 | icon.setDefaultSize( size );
|
|---|
| 906 | icon.setRenderHint( QwtGraphic::RenderPensUnscaled, true );
|
|---|
| 907 |
|
|---|
| 908 | QPainter painter( &icon );
|
|---|
| 909 | painter.setRenderHint( QPainter::Antialiasing,
|
|---|
| 910 | testRenderHint( QwtPlotItem::RenderAntialiased ) );
|
|---|
| 911 |
|
|---|
| 912 | if ( m_data->style == Tube )
|
|---|
| 913 | {
|
|---|
| 914 | QRectF r( 0, 0, size.width(), size.height() );
|
|---|
| 915 | painter.fillRect( r, m_data->brush );
|
|---|
| 916 | }
|
|---|
| 917 |
|
|---|
| 918 | if ( m_data->symbol &&
|
|---|
| 919 | ( m_data->symbol->style() != QwtIntervalSymbol::NoSymbol ) )
|
|---|
| 920 | {
|
|---|
| 921 | QPen pen = m_data->symbol->pen();
|
|---|
| 922 | pen.setWidthF( pen.widthF() );
|
|---|
| 923 | pen.setCapStyle( Qt::FlatCap );
|
|---|
| 924 |
|
|---|
| 925 | painter.setPen( pen );
|
|---|
| 926 | painter.setBrush( m_data->symbol->brush() );
|
|---|
| 927 |
|
|---|
| 928 | if ( orientation() == Qt::Vertical )
|
|---|
| 929 | {
|
|---|
| 930 | const double x = 0.5 * size.width();
|
|---|
| 931 |
|
|---|
| 932 | m_data->symbol->draw( &painter, orientation(),
|
|---|
| 933 | QPointF( x, 0 ), QPointF( x, size.height() - 1.0 ) );
|
|---|
| 934 | }
|
|---|
| 935 | else
|
|---|
| 936 | {
|
|---|
| 937 | const double y = 0.5 * size.height();
|
|---|
| 938 |
|
|---|
| 939 | m_data->symbol->draw( &painter, orientation(),
|
|---|
| 940 | QPointF( 0.0, y ), QPointF( size.width() - 1.0, y ) );
|
|---|
| 941 | }
|
|---|
| 942 | }
|
|---|
| 943 |
|
|---|
| 944 | return icon;
|
|---|
| 945 | }
|
|---|