File: shapefactory.cpp

package info (click to toggle)
qwt 6.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 23,808 kB
  • sloc: cpp: 57,687; xml: 182; makefile: 32
file content (113 lines) | stat: -rw-r--r-- 3,089 bytes parent folder | download | duplicates (2)
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
#include "shapefactory.h"

QPainterPath ShapeFactory::path( Shape shape,
    const QPointF &pos, const QSizeF &size )
{
    QRectF rect;
    rect.setSize( size );
    rect.moveCenter( pos );

    QPainterPath path;

    switch( shape )
    {
        case Rect:
        {
            path.addRect( rect );
            break;
        }
        case Triangle:
        {
            QPolygonF triangle;
            triangle += rect.bottomLeft();
            triangle += QPointF( rect.center().x(), rect.top() );
            triangle += rect.bottomRight();

            path.addPolygon( triangle );
            break;
        }
        case Ellipse:
        {
            path.addEllipse( rect );
            break;
        }
        case Ring:
        {
            path.addEllipse( rect );

            const double w = 0.25 * rect.width();
            path.addEllipse( rect.adjusted( w, w, -w, -w ) );
            break;
        }
        case Star:
        {
            const double cos30 = 0.866025;

            const double dy = 0.25 * size.height();
            const double dx = 0.5 * size.width() * cos30 / 3.0;

            double x1 = pos.x() - 3 * dx;
            double y1 = pos.y() - 2 * dy;

            const double x2 = x1 + 1 * dx;
            const double x3 = x1 + 2 * dx;
            const double x4 = x1 + 3 * dx;
            const double x5 = x1 + 4 * dx;
            const double x6 = x1 + 5 * dx;
            const double x7 = x1 + 6 * dx;

            const double y2 = y1 + 1 * dy;
            const double y3 = y1 + 2 * dy;
            const double y4 = y1 + 3 * dy;
            const double y5 = y1 + 4 * dy;

            QPolygonF star;
            star += QPointF( x4, y1 );
            star += QPointF( x5, y2 );
            star += QPointF( x7, y2 );
            star += QPointF( x6, y3 );
            star += QPointF( x7, y4 );
            star += QPointF( x5, y4 );
            star += QPointF( x4, y5 );
            star += QPointF( x3, y4 );
            star += QPointF( x1, y4 );
            star += QPointF( x2, y3 );
            star += QPointF( x1, y2 );
            star += QPointF( x3, y2 );

            path.addPolygon( star );
            break;
        }
        case Hexagon:
        {
            const double cos30 = 0.866025;

            const double dx = 0.5 * size.width() - cos30;
            const double dy = 0.25 * size.height();

            double x1 = pos.x() - dx;
            double y1 = pos.y() - 2 * dy;

            const double x2 = x1 + 1 * dx;
            const double x3 = x1 + 2 * dx;

            const double y2 = y1 + 1 * dy;
            const double y3 = y1 + 3 * dy;
            const double y4 = y1 + 4 * dy;

            QPolygonF hexagon;
            hexagon += QPointF( x2, y1 );
            hexagon += QPointF( x3, y2 );
            hexagon += QPointF( x3, y3 );
            hexagon += QPointF( x2, y4 );
            hexagon += QPointF( x1, y3 );
            hexagon += QPointF( x1, y2 );

            path.addPolygon( hexagon );
            break;
        }
    };

    path.closeSubpath();
    return path;
}