Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
const QPixmap &QwtSymbol::pixmap() const
{
return d_data->pixmap.pixmap;
}
/*!
Set a graphic as symbol
\param graphic Graphic
\sa graphic(), setPixmap()
\note the style() is set to QwtSymbol::Graphic
\note brush() and pen() have no effect
*/
void QwtSymbol::setGraphic( const QwtGraphic &graphic )
{
d_data->style = QwtSymbol::Graphic;
d_data->graphic.graphic = graphic;
}
/*!
\return Assigned graphic
\sa setGraphic()
*/
const QwtGraphic &QwtSymbol::graphic() const
{
return d_data->graphic.graphic;
}
#ifndef QWT_NO_SVG
/*!
Set a SVG icon as symbol
\param svgDocument SVG icon
\sa setGraphic(), setPixmap()
\note the style() is set to QwtSymbol::SvgDocument
\note brush() and pen() have no effect
*/
void QwtSymbol::setSvgDocument( const QByteArray &svgDocument )
{
d_data->style = QwtSymbol::SvgDocument;
if ( d_data->svg.renderer == NULL )
d_data->svg.renderer = new QSvgRenderer();
d_data->svg.renderer->load( svgDocument );
}
#endif
/*!
\brief Specify the symbol's size
If the 'h' parameter is left out or less than 0,
and the 'w' parameter is greater than or equal to 0,
the symbol size will be set to (w,w).
\param width Width
\param height Height (defaults to -1)
\sa size()
if ( ( width >= 0 ) && ( height < 0 ) )
height = width;
setSize( QSize( width, height ) );
/*!
Set the symbol's size
\param size Size
\sa size()
*/
void QwtSymbol::setSize( const QSize &size )
if ( size.isValid() && size != d_data->size )
{
d_data->size = size;
invalidateCache();
}
}
/*!
\return Size
\sa setSize()
*/
const QSize& QwtSymbol::size() const
{
return d_data->size;
}
/*!
\brief Assign a brush
The brush is used to draw the interior of the symbol.
if ( brush != d_data->brush )
{
d_data->brush = brush;
invalidateCache();
if ( d_data->style == QwtSymbol::Path )
d_data->path.graphic.reset();
}
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
\return Brush
\sa setBrush()
*/
const QBrush& QwtSymbol::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 QwtSymbol::setPen( const QColor &color,
qreal width, Qt::PenStyle style )
{
setPen( QPen( color, width, style ) );
}
/*!
Assign a pen
\param pen Pen
\sa pen(), setBrush()
*/
void QwtSymbol::setPen( const QPen &pen )
{
if ( pen != d_data->pen )
{
d_data->pen = pen;
invalidateCache();
if ( d_data->style == QwtSymbol::Path )
d_data->path.graphic.reset();
}
}
/*!
\return Pen
\sa setPen(), brush()
\brief Set the color of the symbol
Change the color of the brush for symbol types with a filled area.
For all other symbol types the color will be assigned to the pen.
\param color Color
\sa setBrush(), setPen(), brush(), pen()
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
void QwtSymbol::setColor( const QColor &color )
{
switch ( d_data->style )
{
case QwtSymbol::Ellipse:
case QwtSymbol::Rect:
case QwtSymbol::Diamond:
case QwtSymbol::Triangle:
case QwtSymbol::UTriangle:
case QwtSymbol::DTriangle:
case QwtSymbol::RTriangle:
case QwtSymbol::LTriangle:
case QwtSymbol::Star2:
case QwtSymbol::Hexagon:
{
if ( d_data->brush.color() != color )
{
d_data->brush.setColor( color );
invalidateCache();
}
break;
}
case QwtSymbol::Cross:
case QwtSymbol::XCross:
case QwtSymbol::HLine:
case QwtSymbol::VLine:
case QwtSymbol::Star1:
{
if ( d_data->pen.color() != color )
{
d_data->pen.setColor( color );
invalidateCache();
}
break;
}
default:
{
if ( d_data->brush.color() != color ||
d_data->pen.color() != color )
{
invalidateCache();
}
d_data->brush.setColor( color );
d_data->pen.setColor( color );
}
}
}
/*!
\brief Set and enable a pin point
The position of a complex symbol is not always aligned to its center
( f.e an arrow, where the peak points to a position ). The pin point
defines the position inside of a Pixmap, Graphic, SvgDocument
or PainterPath symbol where the represented point has to
be aligned to.
\param pos Position
\param enable En/Disable the pin point alignment
\sa pinPoint(), setPinPointEnabled()
*/
void QwtSymbol::setPinPoint( const QPointF &pos, bool enable )
{
if ( d_data->pinPoint != pos )
{
d_data->pinPoint = pos;
if ( d_data->isPinPointEnabled )
{
invalidateCache();
}
}
setPinPointEnabled( enable );
}
/*!
\return Pin point
\sa setPinPoint(), setPinPointEnabled()
*/
QPointF QwtSymbol::pinPoint() const
return d_data->pinPoint;
}
/*!
En/Disable the pin point alignment
\param on Enabled, when on is true
\sa setPinPoint(), isPinPointEnabled()
*/
void QwtSymbol::setPinPointEnabled( bool on )
{
if ( d_data->isPinPointEnabled != on )
{
d_data->isPinPointEnabled = on;
invalidateCache();
}
/*!
\return True, when the pin point translation is enabled
\sa setPinPoint(), setPinPointEnabled()
*/
bool QwtSymbol::isPinPointEnabled() const
{
return d_data->isPinPointEnabled;
}
Painting several symbols is more effective than drawing symbols
one by one, as a couple of layout calculations and setting of pen/brush
can be done once for the complete array.
\param points Array of points
\param numPoints Number of points
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
void QwtSymbol::drawSymbols( QPainter *painter,
const QPointF *points, int numPoints ) const
{
if ( numPoints <= 0 )
return;
bool useCache = false;
// Don't use the pixmap, when the paint device
// could generate scalable vectors
if ( QwtPainter::roundingAlignment( painter ) &&
!painter->transform().isScaling() )
{
if ( d_data->cache.policy == QwtSymbol::Cache )
{
useCache = true;
}
else if ( d_data->cache.policy == QwtSymbol::AutoCache )
{
if ( painter->paintEngine()->type() == QPaintEngine::Raster )
{
useCache = true;
}
else
{
switch( d_data->style )
{
case QwtSymbol::XCross:
case QwtSymbol::HLine:
case QwtSymbol::VLine:
case QwtSymbol::Cross:
break;
case QwtSymbol::Pixmap:
{
if ( !d_data->size.isEmpty() &&
d_data->size != d_data->pixmap.pixmap.size() )
{
useCache = true;
}
break;
}
default:
useCache = true;
}
}
}
}
if ( useCache )
{
const QRect br = boundingRect();
const QRect rect( 0, 0, br.width(), br.height() );
if ( d_data->cache.pixmap.isNull() )
{
d_data->cache.pixmap = QwtPainter::backingStore( NULL, br.size() );
d_data->cache.pixmap.fill( Qt::transparent );
QPainter p( &d_data->cache.pixmap );
p.setRenderHints( painter->renderHints() );
p.translate( -br.topLeft() );
const QPointF pos;
renderSymbols( &p, &pos, 1 );
}
const int dx = br.left();
const int dy = br.top();
for ( int i = 0; i < numPoints; i++ )
{
const int left = qRound( points[i].x() ) + dx;
const int top = qRound( points[i].y() ) + dy;
painter->drawPixmap( left, top, d_data->cache.pixmap );
}
}
else
{
painter->save();
renderSymbols( painter, points, numPoints );
painter->restore();
}
}
/*!
\brief Draw the symbol into a rectangle
The symbol is painted centered and scaled into the target rectangle.
It is always painted uncached and the pin point is ignored.
This method is primarily intended for drawing a symbol to
the legend.
void QwtSymbol::drawSymbol( QPainter *painter, const QRectF &rect ) const
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
if ( d_data->style == QwtSymbol::NoSymbol )
return;
if ( d_data->style == QwtSymbol::Graphic )
{
d_data->graphic.graphic.render(
painter, rect, Qt::KeepAspectRatio );
}
else if ( d_data->style == QwtSymbol::Path )
{
if ( d_data->path.graphic.isNull() )
{
d_data->path.graphic = qwtPathGraphic(
d_data->path.path, d_data->pen, d_data->brush );
}
d_data->path.graphic.render(
painter, rect, Qt::KeepAspectRatio );
return;
}
else if ( d_data->style == QwtSymbol::SvgDocument )
{
#ifndef QWT_NO_SVG
if ( d_data->svg.renderer )
{
QRectF scaledRect;
QSizeF sz = d_data->svg.renderer->viewBoxF().size();
if ( !sz.isEmpty() )
{
sz.scale( rect.size(), Qt::KeepAspectRatio );
scaledRect.setSize( sz );
scaledRect.moveCenter( rect.center() );
}
else
{
scaledRect = rect;
}
d_data->svg.renderer->render(
painter, scaledRect );
}
#endif
}
else
{
const QRect br = boundingRect();
// scale the symbol size to fit into rect.
const double ratio = qMin( rect.width() / br.width(),
rect.height() / br.height() );
painter->save();
painter->translate( rect.center() );
painter->scale( ratio, ratio );
const bool isPinPointEnabled = d_data->isPinPointEnabled;
d_data->isPinPointEnabled = false;
const QPointF pos;
renderSymbols( painter, &pos, 1 );
d_data->isPinPointEnabled = isPinPointEnabled;
painter->restore();
}
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
\param painter Qt painter
\param points Positions of the symbols
\param numPoints Number of points
*/
void QwtSymbol::renderSymbols( QPainter *painter,
const QPointF *points, int numPoints ) const
{
switch ( d_data->style )
{
case QwtSymbol::Ellipse:
{
qwtDrawEllipseSymbols( painter, points, numPoints, *this );
break;
}
case QwtSymbol::Rect:
{
qwtDrawRectSymbols( painter, points, numPoints, *this );
break;
}
case QwtSymbol::Diamond:
{
qwtDrawDiamondSymbols( painter, points, numPoints, *this );
break;
}
case QwtSymbol::Cross:
{
qwtDrawLineSymbols( painter, Qt::Horizontal | Qt::Vertical,
points, numPoints, *this );
break;
}
case QwtSymbol::XCross:
{
qwtDrawXCrossSymbols( painter, points, numPoints, *this );
break;
}
case QwtSymbol::Triangle:
case QwtSymbol::UTriangle:
{
qwtDrawTriangleSymbols( painter, QwtTriangle::Up,
points, numPoints, *this );
break;
}
case QwtSymbol::DTriangle:
{
qwtDrawTriangleSymbols( painter, QwtTriangle::Down,
points, numPoints, *this );
break;
}
case QwtSymbol::RTriangle:
{
qwtDrawTriangleSymbols( painter, QwtTriangle::Right,
points, numPoints, *this );
break;
}
case QwtSymbol::LTriangle:
{
qwtDrawTriangleSymbols( painter, QwtTriangle::Left,
points, numPoints, *this );
break;
}
case QwtSymbol::HLine:
{
qwtDrawLineSymbols( painter, Qt::Horizontal,
points, numPoints, *this );
break;
}
case QwtSymbol::VLine:
{
qwtDrawLineSymbols( painter, Qt::Vertical,
points, numPoints, *this );
break;
}
case QwtSymbol::Star1:
{
qwtDrawStar1Symbols( painter, points, numPoints, *this );
break;
}
case QwtSymbol::Star2:
{
qwtDrawStar2Symbols( painter, points, numPoints, *this );
break;
}
case QwtSymbol::Hexagon:
{
qwtDrawHexagonSymbols( painter, points, numPoints, *this );
break;
}
case QwtSymbol::Path:
{
if ( d_data->path.graphic.isNull() )
{
d_data->path.graphic = qwtPathGraphic( d_data->path.path,
d_data->pen, d_data->brush );
}
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
qwtDrawGraphicSymbols( painter, points, numPoints,
d_data->path.graphic, *this );
break;
}
case QwtSymbol::Pixmap:
{
qwtDrawPixmapSymbols( painter, points, numPoints, *this );
break;
}
case QwtSymbol::Graphic:
{
qwtDrawGraphicSymbols( painter, points, numPoints,
d_data->graphic.graphic, *this );
break;
}
case QwtSymbol::SvgDocument:
{
#ifndef QWT_NO_SVG
qwtDrawSvgSymbols( painter, points, numPoints,
d_data->svg.renderer, *this );
#endif
break;
}
default:;
}
}
/*!
Calculate the bounding rectangle for a symbol
at position (0,0).
\return Bounding rectangle
*/
QRect QwtSymbol::boundingRect() const
{
QRectF rect;
switch ( d_data->style )
{
case QwtSymbol::Ellipse:
case QwtSymbol::Rect:
case QwtSymbol::Hexagon:
{
qreal pw = 0.0;
if ( d_data->pen.style() != Qt::NoPen )
pw = qMax( d_data->pen.widthF(), qreal( 1.0 ) );
rect.setSize( d_data->size + QSizeF( pw, pw ) );
rect.moveCenter( QPointF( 0.0, 0.0 ) );
break;
}
case QwtSymbol::XCross:
case QwtSymbol::Diamond:
case QwtSymbol::Triangle:
case QwtSymbol::UTriangle:
case QwtSymbol::DTriangle:
case QwtSymbol::RTriangle:
case QwtSymbol::LTriangle:
case QwtSymbol::Star1:
case QwtSymbol::Star2:
{
qreal pw = 0.0;
if ( d_data->pen.style() != Qt::NoPen )
pw = qMax( d_data->pen.widthF(), qreal( 1.0 ) );
rect.setSize( d_data->size + QSizeF( 2 * pw, 2 * pw ) );
rect.moveCenter( QPointF( 0.0, 0.0 ) );
break;
}
case QwtSymbol::Path:
{
if ( d_data->path.graphic.isNull() )
{
d_data->path.graphic = qwtPathGraphic(
d_data->path.path, d_data->pen, d_data->brush );
}
rect = qwtScaledBoundingRect(
d_data->path.graphic, d_data->size );
break;
}
case QwtSymbol::Pixmap:
{
if ( d_data->size.isEmpty() )
rect.setSize( d_data->pixmap.pixmap.size() );
else
rect.setSize( d_data->size );
rect.moveCenter( QPointF( 0.0, 0.0 ) );
// pinpoint ???
break;
}
case QwtSymbol::Graphic:
{
rect = qwtScaledBoundingRect(
d_data->graphic.graphic, d_data->size );
break;
}
#ifndef QWT_NO_SVG
case QwtSymbol::SvgDocument:
{
if ( d_data->svg.renderer )
rect = d_data->svg.renderer->viewBoxF();
if ( d_data->size.isValid() && !rect.isEmpty() )
{
QSizeF sz = rect.size();
const double sx = d_data->size.width() / sz.width();
const double sy = d_data->size.height() / sz.height();
QTransform transform;
transform.scale( sx, sy );
rect = transform.mapRect( rect );
}
break;
}
#endif
default:
{
rect.setSize( d_data->size );
rect.moveCenter( QPointF( 0.0, 0.0 ) );
}
}
if ( d_data->style == QwtSymbol::Graphic ||
d_data->style == QwtSymbol::SvgDocument || d_data->style == QwtSymbol::Path )
{
QPointF pinPoint( 0.0, 0.0 );
if ( d_data->isPinPointEnabled )
pinPoint = rect.center() - d_data->pinPoint;
rect.moveCenter( pinPoint );
}
QRect r;
r.setLeft( qFloor( rect.left() ) );
r.setTop( qFloor( rect.top() ) );
r.setRight( qCeil( rect.right() ) );
r.setBottom( qCeil( rect.bottom() ) );
if ( d_data->style != QwtSymbol::Pixmap )
r.adjust( -1, -1, 1, 1 ); // for antialiasing
return r;
}
/*!
Invalidate the cached symbol pixmap
The symbol invalidates its cache, whenever an attribute is changed
that has an effect ob how to display a symbol. In case of derived
classes with individual styles ( >= QwtSymbol::UserStyle ) it
might be necessary to call invalidateCache() for attributes
that are relevant for this style.
\sa CachePolicy, setCachePolicy(), drawSymbols()
*/
void QwtSymbol::invalidateCache()
if ( !d_data->cache.pixmap.isNull() )
d_data->cache.pixmap = QPixmap();
/*!
Specify the symbol style
\param style Style
\sa style()
*/
void QwtSymbol::setStyle( QwtSymbol::Style style )
if ( d_data->style != style )
{
d_data->style = style;
invalidateCache();
}
/*!
\return Current symbol style
\sa setStyle()
*/
QwtSymbol::Style QwtSymbol::style() const