File: testplot_widget.cpp

package info (click to toggle)
kf6-kplotting 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 368 kB
  • sloc: cpp: 2,011; sh: 13; makefile: 7
file content (183 lines) | stat: -rw-r--r-- 5,629 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
    testplot_widget.cpp
    SPDX-FileCopyrightText: 2006 Jason Harris <kstars@30doradus.org>

    SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <QComboBox>
#include <QPen>
#include <QVBoxLayout>
#include <math.h>

#include "kplotaxis.h"
#include "kplotobject.h"
#include "kplotwidget.h"
#include "testplot_widget.h"

TestPlot::TestPlot(QWidget *p)
    : QMainWindow(p)
    , po1(nullptr)
    , po2(nullptr)
{
    QWidget *w = new QWidget(this);
    vlay = new QVBoxLayout(w);

    PlotSelector = new QComboBox(w);
    PlotSelector->addItem(QStringLiteral("Points plot"));
    PlotSelector->addItem(QStringLiteral("Lines plot"));
    PlotSelector->addItem(QStringLiteral("Bars plot"));
    PlotSelector->addItem(QStringLiteral("Points plot with labels"));
    PlotSelector->addItem(QStringLiteral("Points, lines and bars"));
    PlotSelector->addItem(QStringLiteral("Points, lines and bars with labels"));

    plot = new KPlotWidget(w);
    plot->setMinimumSize(400, 400);
    plot->setAntialiasing(true);
    vlay->addWidget(PlotSelector);
    vlay->addWidget(plot);

    setCentralWidget(w);

    connect(PlotSelector, qOverload<int>(&QComboBox::activated), this, &TestPlot::slotSelectPlot);

    slotSelectPlot(PlotSelector->currentIndex());
}

void TestPlot::slotSelectPlot(int n)
{
    plot->resetPlot();

    switch (n) {
    case 0: { // Points plot
        plot->setLimits(-6.0, 11.0, -10.0, 110.0);

        po1 = new KPlotObject(Qt::white, KPlotObject::Points, 4, KPlotObject::Asterisk);
        po2 = new KPlotObject(Qt::green, KPlotObject::Points, 4, KPlotObject::Triangle);

        for (float x = -5.0; x <= 10.0; x += 1.0) {
            po1->addPoint(x, x * x);
            po2->addPoint(x, 50.0 - 5.0 * x);
        }

        plot->addPlotObject(po1);
        plot->addPlotObject(po2);

        plot->update();
        break;
    }

    case 1: { // Lines plot
        plot->setLimits(-0.1, 6.38, -1.1, 1.1);
        plot->setSecondaryLimits(-5.73, 365.55, -1.1, 1.1);
        plot->axis(KPlotWidget::TopAxis)->setTickLabelsShown(true);
        plot->axis(KPlotWidget::BottomAxis)->setLabel(QStringLiteral("Angle [radians]"));
        plot->axis(KPlotWidget::TopAxis)->setLabel(QStringLiteral("Angle [degrees]"));

        po1 = new KPlotObject(Qt::red, KPlotObject::Lines, 2);
        po2 = new KPlotObject(Qt::cyan, KPlotObject::Lines, 2);

        for (float t = 0.0; t <= 6.28; t += 0.04) {
            po1->addPoint(t, sin(t));
            po2->addPoint(t, cos(t));
        }

        plot->addPlotObject(po1);
        plot->addPlotObject(po2);

        plot->update();
        break;
    }

    case 2: { // Bars plot
        plot->setLimits(-7.0, 7.0, -5.0, 105.0);

        po1 = new KPlotObject(Qt::white, KPlotObject::Bars, 2);
        po1->setBarBrush(QBrush(Qt::green, Qt::Dense4Pattern));

        for (float x = -6.5; x <= 6.5; x += 0.5) {
            po1->addPoint(x, 100 * exp(-0.5 * x * x), QLatin1String(""), 0.5);
        }

        plot->addPlotObject(po1);

        plot->update();
        break;
    }

    case 3: { // Points plot with labels
        plot->setLimits(-1.1, 1.1, -1.1, 1.1);

        po1 = new KPlotObject(Qt::yellow, KPlotObject::Points, 10, KPlotObject::Star);
        po1->setLabelPen(QPen(Qt::green));

        po1->addPoint(0.0, 0.8, QStringLiteral("North"));
        po1->addPoint(0.57, 0.57, QStringLiteral("Northeast"));
        po1->addPoint(0.8, 0.0, QStringLiteral("East"));
        po1->addPoint(0.57, -0.57, QStringLiteral("Southeast"));
        po1->addPoint(0.0, -0.8, QStringLiteral("South"));
        po1->addPoint(-0.57, -0.57, QStringLiteral("Southwest"));
        po1->addPoint(-0.8, 0.0, QStringLiteral("West"));
        po1->addPoint(-0.57, 0.57, QStringLiteral("Northwest"));

        plot->addPlotObject(po1);

        plot->update();
        break;
    }

    case 4: { // Points, Lines and Bars plot
        plot->setLimits(-2.1, 2.1, -0.1, 4.1);

        po1 = new KPlotObject(Qt::white, KPlotObject::Points, 10, KPlotObject::Pentagon);

        po1->setShowLines(true);
        po1->setShowBars(true);
        po1->setLabelPen(QPen(QColor("#AA8800")));
        po1->setLinePen(QPen(Qt::red, 3.0, Qt::DashDotLine));
        po1->setBarBrush(QBrush(Qt::blue, Qt::BDiagPattern));

        po1->addPoint(-1.75, 0.5);
        po1->addPoint(-1.25, 1.0);
        po1->addPoint(-0.75, 1.25);
        po1->addPoint(-0.25, 1.5);
        po1->addPoint(0.25, 2.5);
        po1->addPoint(0.75, 3.0);
        po1->addPoint(1.25, 1.5);
        po1->addPoint(1.75, 1.75);

        plot->addPlotObject(po1);

        update();
        break;
    }

    case 5: { // Points, Lines and Bars plot with labels
        plot->setLimits(-2.1, 2.1, -0.1, 4.1);

        po1 = new KPlotObject(Qt::white, KPlotObject::Points, 10, KPlotObject::Pentagon);

        po1->setShowLines(true);
        po1->setShowBars(true);
        po1->setLabelPen(QPen(QColor("#AA8800")));
        po1->setLinePen(QPen(Qt::red, 3.0, Qt::DashDotLine));
        po1->setBarBrush(QBrush(Qt::blue, Qt::BDiagPattern));

        po1->addPoint(-1.75, 0.5, QStringLiteral("A"));
        po1->addPoint(-1.25, 1.0, QStringLiteral("B"));
        po1->addPoint(-0.75, 1.25, QStringLiteral("C"));
        po1->addPoint(-0.25, 1.5, QStringLiteral("D"));
        po1->addPoint(0.25, 2.5, QStringLiteral("E"));
        po1->addPoint(0.75, 3.0, QStringLiteral("F"));
        po1->addPoint(1.25, 1.5, QStringLiteral("G"));
        po1->addPoint(1.75, 1.75, QStringLiteral("H"));

        plot->addPlotObject(po1);

        update();
        break;
    }
    }
}

#include "moc_testplot_widget.cpp"