Debian Codegraph @

qwt/playground/symbols/main.cpp

raw symbols SCIP
1 /*****************************************************************************
2 * Qwt Examples - Copyright (C) 2002 Uwe Rathmann
3 * This file may be used under the terms of the 3-clause BSD License
4 *****************************************************************************/
5
6 #include <QwtPlot>
7 #include <QwtPlotCurve>
8 #include <QwtSymbol>
9 #include <QwtGraphic>
10 #include <QwtLegend>
11 #include <QwtMath>
12
13 #include <QApplication>
14 #include <QPainter>
15 #include <QPainterPath>
16 #include <QBuffer>
17 #include <qmath.h>
18
19 #ifdef QT_SVG_LIB
20 #include <QSvgGenerator>
21 #endif
22
23 class MySymbol : public QwtSymbol
24 {
25 public:
26 MySymbol( QwtSymbol::Style style, const QBrush& brush )
27 {
28 QPen pen( Qt::black, 0 );
29 pen.setJoinStyle( Qt::MiterJoin );
30 pen.setCosmetic( true );
31
32 QPainterPath path = createArrow( QSize( 16, 24 ) );
33
34 const QSizeF pathSize = path.boundingRect().size();
35
36 setSize( 0.8 * pathSize.toSize() );
37
38 setPinPoint( QPointF( 0.0, 0.0 ) );
39
40 switch( style )
41 {
42 case QwtSymbol::Pixmap:
43 {
44 const QSize sz = size();
45
46 const double ratio = qMin( sz.width() / pathSize.width(),
47 sz.height() / pathSize.height() );
48
49 QTransform transform;
50 transform.scale( ratio, ratio );
51
52 path = transform.map( path );
53
54 if ( isPinPointEnabled() )
55 {
56 QPointF pos = transform.map( pinPoint() );
57 setPinPoint( pos );
58 }
59
60 const QRectF br = path.boundingRect();
61
62 int m = 2 + qCeil( pen.widthF() );
63
64 QPixmap pm( sz + QSize( 2 * m, 2 * m ) );
65 pm.fill( Qt::transparent );
66
67 QPainter painter( &pm );
68 painter.setRenderHint( QPainter::Antialiasing, true );
69
70 painter.setPen( pen );
71 painter.setBrush( brush );
72
73 painter.translate( m, m );
74 painter.translate( -br.left(), br.top() );
75 painter.drawPath( path );
76
77 setPixmap( pm );
78 setSize( pm.size() );
79 if ( isPinPointEnabled() )
80 setPinPoint( pinPoint() + QPointF( m, m ) );
81
82 break;
83 }
84 case QwtSymbol::Graphic:
85 {
86 QwtGraphic graphic;
87 graphic.setRenderHint( QwtGraphic::RenderPensUnscaled );
88
89 QPainter painter( &graphic );
90 painter.setRenderHint( QPainter::Antialiasing, true );
91 painter.setPen( pen );
92 painter.setBrush( brush );
93
94 painter.drawPath( path );
95 painter.end();
96
97 setGraphic( graphic );
98 break;
99 }
100 case QwtSymbol::SvgDocument:
101 {
102 #ifndef QWT_NO_SVG
103 QBuffer buf;
104
105 QSvgGenerator generator;
106 generator.setOutputDevice( &buf );
107
108 QPainter painter( &generator );
109 painter.setRenderHint( QPainter::Antialiasing, true );
110 painter.setPen( pen );
111 painter.setBrush( brush );
112
113 painter.drawPath( path );
114 painter.end();
115
116 setSvgDocument( buf.data() );
117 #endif
118 break;
119 }
120 case QwtSymbol::Path:
121 default:
122 {
123 setPen( pen );
124 setBrush( brush );
125 setPath( path );
126 }
127 }
128
129 }
130
131 private:
132 QPainterPath createArrow( const QSizeF& size ) const
133 {
134 const double w = size.width();
135 const double h = size.height();
136 const double y0 = 0.6 * h;
137
138 QPainterPath path;
139 path.moveTo( -0.2 * w, h );
140 path.lineTo( 0.2 * w, h );
141 path.moveTo( 0, h );
142 path.lineTo( 0, y0 );
143 path.lineTo( -0.5 * w, y0 );
144 path.lineTo( 0, 0 );
145 path.lineTo( 0.5 * w, y0 );
146 path.lineTo( 0, y0 );
147
148 QTransform transform;
149 transform.rotate( -30.0 );
150 path = transform.map( path );
151
152 return path;
153 }
154 };
155
156 class Plot : public QwtPlot
157 {
158 public:
159 Plot()
160 {
161 setTitle( "Plot Demo" );
162 setCanvasBackground( Qt::white );
163
164 setAxisScale( QwtAxis::XBottom, -1.0, 6.0 );
165
166 QwtLegend* legend = new QwtLegend();
167 insertLegend( legend );
168
169 populate();
170 }
171
172 void populate()
173 {
174 for ( int i = 0; i < 4; i++ )
175 {
176 QwtPlotCurve* curve = new QwtPlotCurve();
177 curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
178 curve->setPen( Qt::blue );
179
180 QBrush brush;
181 QwtSymbol::Style style = QwtSymbol::NoSymbol;
182 QString title;
183 if ( i == 0 )
184 {
185 brush = Qt::magenta;
186 style = QwtSymbol::Path;
187 title = "Path";
188 }
189 else if ( i == 2 )
190 {
191 brush = Qt::red;
192 style = QwtSymbol::Graphic;
193 title = "Graphic";
194 }
195 else if ( i == 1 )
196 {
197 brush = Qt::yellow;
198 style = QwtSymbol::SvgDocument;
199 title = "Svg";
200 }
201 else if ( i == 3 )
202 {
203 brush = Qt::cyan;
204 style = QwtSymbol::Pixmap;
205 title = "Pixmap";
206 }
207
208 MySymbol* symbol = new MySymbol( style, brush );
209
210 curve->setSymbol( symbol );
211 curve->setTitle( title );
212 curve->setLegendAttribute( QwtPlotCurve::LegendShowSymbol, true );
213 curve->setLegendIconSize( QSize( 15, 18 ) );
214
215 QPolygonF points;
216 points << QPointF( 0.0, 4.4 ) << QPointF( 1.0, 3.0 )
217 << QPointF( 2.0, 4.5 ) << QPointF( 3.0, 6.8 )
218 << QPointF( 4.0, 7.9 ) << QPointF( 5.0, 7.1 );
219
220 points.translate( 0.0, i * 2.0 );
221
222 curve->setSamples( points );
223 curve->attach( this );
224 }
225 }
226 };
227
228 int main( int argc, char* argv[] )
229 {
230 QApplication app( argc, argv );
231
232 Plot plot;
233 plot.resize( 600, 400 );
234 plot.show();
235
236 return app.exec();
237 }