378
|
1 module simple;
|
|
2
|
|
3 import std.math;
|
|
4
|
|
5 import qt.gui.QApplication;
|
|
6 import qt.qwt.QwtPlot;
|
|
7 import qt.qwt.QwtPlotMarker;
|
|
8 import qt.qwt.QwtPlotCurve;
|
|
9 import qt.qwt.QwtLegend;
|
|
10 import qt.qwt.QwtData;
|
|
11 import qt.qwt.QwtText;
|
|
12
|
|
13 //-----------------------------------------------------------------
|
|
14 // simple.d
|
|
15 //
|
|
16 // A simple example which shows how to use QwtPlot and QwtData
|
|
17 //-----------------------------------------------------------------
|
|
18
|
|
19 real mysin(real arg)
|
|
20 {
|
|
21 return sin(arg);
|
|
22 }
|
|
23
|
|
24 real mycos(real arg)
|
|
25 {
|
|
26 return cos(arg);
|
|
27 }
|
|
28
|
|
29 alias real function(real) double_func;
|
|
30
|
|
31 class SimpleData: QwtData
|
|
32 {
|
|
33 // The x values depend on its index and the y values
|
|
34 // can be calculated from the corresponding x value.
|
|
35 // So we don't need to store the values.
|
|
36 // Such an implementation is slower because every point
|
|
37 // has to be recalculated for every replot, but it demonstrates how
|
|
38 // QwtData can be used.
|
|
39
|
|
40 public:
|
|
41 this(const double_func y, size_t size)
|
|
42 {
|
|
43 d_size = size;
|
|
44 d_y = y;
|
|
45 }
|
|
46
|
|
47 QwtData copy() const
|
|
48 {
|
|
49 return new SimpleData(d_y, d_size);
|
|
50 }
|
|
51
|
|
52 size_t size() const
|
|
53 {
|
|
54 return d_size;
|
|
55 }
|
|
56
|
|
57 double x(size_t i) const
|
|
58 {
|
|
59 return 0.1 * i;
|
|
60 }
|
|
61
|
|
62 double y(size_t i) const
|
|
63 {
|
|
64 return d_y(x(i));
|
|
65 }
|
|
66 private:
|
|
67 size_t d_size;
|
|
68 const double_func d_y;
|
|
69 };
|
|
70
|
|
71 class Plot : QwtPlot
|
|
72 {
|
|
73 public:
|
|
74 this()
|
|
75 {
|
|
76 super(cast(QWidget)null);
|
|
77 setTitle("A Simple QwtPlot Demonstration");
|
|
78 insertLegend(new QwtLegend(cast(QWidget)null), QwtPlot.RightLegend);
|
|
79
|
|
80 // Set axis titles
|
|
81 setAxisTitle(xBottom, "x -.");
|
|
82 setAxisTitle(yLeft, "y -.");
|
|
83
|
|
84 // Insert new curves
|
|
85 auto cSin = new QwtPlotCurve("y = sin(x)");
|
|
86 cSin.setRenderHint(QwtPlotItem.RenderAntialiased);
|
|
87 cSin.setPen(new QPen(new QColor(Qt.red)));
|
|
88 cSin.attach(this);
|
|
89
|
|
90 auto cCos = new QwtPlotCurve("y = cos(x)");
|
|
91 cCos.setRenderHint(QwtPlotItem.RenderAntialiased);
|
|
92 cCos.setPen(new QPen(new QColor(Qt.blue)));
|
|
93 cCos.attach(this);
|
|
94
|
|
95 // Create sin and cos data
|
|
96 const int nPoints = 100;
|
|
97 cSin.setData(new SimpleData(&mysin, nPoints));
|
|
98 cCos.setData(new SimpleData(&mycos, nPoints));
|
|
99
|
|
100 // Insert markers
|
|
101
|
|
102 // ...a horizontal line at y = 0...
|
|
103 auto mY = new QwtPlotMarker();
|
|
104 mY.setLabel(new QwtText("y = 0"));
|
|
105 mY.setLabelAlignment(Qt.AlignRight | Qt.AlignTop);
|
|
106 mY.setLineStyle(QwtPlotMarker.HLine);
|
|
107 mY.setYValue(0.0);
|
|
108 mY.attach(this);
|
|
109
|
|
110 // ...a vertical line at x = 2 * pi
|
|
111 auto mX = new QwtPlotMarker();
|
|
112 mX.setLabel(new QwtText("x = 2 pi"));
|
|
113 mX.setLabelAlignment(Qt.AlignLeft | Qt.AlignBottom);
|
|
114 mX.setLabelOrientation(Qt.Vertical);
|
|
115 mX.setLineStyle(QwtPlotMarker.VLine);
|
|
116 mX.setLinePen(new QPen(new QBrush(Qt.black), 0, Qt.DashDotLine));
|
|
117 mX.setXValue(2.0 * PI);
|
|
118 mX.attach(this);
|
|
119 }
|
|
120
|
|
121 };
|
|
122
|
|
123 int main(string[] args)
|
|
124 {
|
|
125 auto a = new QApplication(args);
|
|
126
|
|
127 scope plot = new Plot;
|
|
128 plot.resize(600,400);
|
|
129 plot.show();
|
|
130 return a.exec();
|
|
131 }
|