File: polynomialmodelwidget.cpp

package info (click to toggle)
zegrapher 3.1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,820 kB
  • sloc: cpp: 12,779; xml: 136; sh: 9; makefile: 4
file content (265 lines) | stat: -rw-r--r-- 9,233 bytes parent folder | download | duplicates (5)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/****************************************************************************
**  Copyright (c) 2016, Adel Kara Slimane <adel.ks@zegrapher.com>
**
**  This file is part of ZeGrapher's source code.
**
**  ZeGrapher is free software: you may copy, redistribute and/or modify it
**  under the terms of the GNU General Public License as published by the
**  Free Software Foundation, either version 3 of the License, or (at your
**  option) any later version.
**
**  This file is distributed in the hope that it will be useful, but
**  WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/


#include "polynomialmodelwidget.h"
#include "ui_polynomialmodelwidget.h"

PolynomialModelWidget::PolynomialModelWidget(const QList<Point> &dat, Information *info, QString xname, QString yname, bool pol, QWidget *parent) :
    QWidget(parent), ui(new Ui::PolynomialModelWidget)
{
    ui->setupUi(this);

    abscissa = xname;
    ordinate = yname; //the abscissa and ordinate would mean the polar angle and radius respectively   

    polar = pol;
    data = dat;
    information = info;

    addWidgetsToUI();

    regression = new PolynomialRegression(ui->degree->value(), (ApproxMethod)ui->approachPoints->isChecked(), LimitedToData,
                                          ui->relativeExtrapol->value(), ui->drawModel->isChecked(), pol);

    connect(regression, SIGNAL(rangeUpdated()), this, SLOT(updateManualRangeFields()));
    connect(regression, SIGNAL(coefsUpdated(QList<double>)), this, SLOT(updatePolynomialCoefs(QList<double>)));
    connect(regression, SIGNAL(regressionModified()), information, SIGNAL(dataUpdated()));   

    information->addDataRegression(regression);

    regression->setColor(information->getSettingsVals().defaultColor);
    regression->setData(dat); //

    connect(ui->drawModel, SIGNAL(toggled(bool)), regression, SLOT(setDrawState(bool)));
    connect(colorButton, SIGNAL(colorChanged(QColor)), regression, SLOT(setColor(QColor)));
    connect(ui->degree, SIGNAL(valueChanged(int)), regression, SLOT(setPolynomialRegressionDegree(int)));
    connect(ui->relativeExtrapol, SIGNAL(valueChanged(double)), regression, SLOT(setRelativeRangeCoef(double)));

    connect(ui->approachPoints, SIGNAL(toggled(bool)), this, SLOT(updateApproxMethod()));
    connect(ui->dataInterval, SIGNAL(clicked(bool)), this, SLOT(updateRangeOption()));
    connect(ui->relativeExtrapolation, SIGNAL(clicked(bool)), this, SLOT(updateRangeOption()));
    connect(ui->manualInterval, SIGNAL(clicked(bool)), this, SLOT(updateRangeOption()));
}

void PolynomialModelWidget::manualRangeEdited()
{
    if(startVal->isValid() && endVal->isValid())
    {
        Range range;
        range.step = 1;
        range.start = startVal->getValue();
        range.end = endVal->getValue();

        regression->setRange(range);
    }
}

void PolynomialModelWidget::updatePolynomialCoefs(QList<double> coefs)
{
    for(int pos = 0 ; pos < coefs.size() && pos < coefWidgets.size() ; pos++)
    {
        coefWidgets[pos].container->show();
        coefWidgets[pos].line->setText(QString::number(coefs[pos]));
    }

    for(int pos = coefWidgets.size()-1 ; pos > coefs.size()-1 ; pos--)
    {
        coefWidgets[pos].container->hide();
    }

    for(int pos = coefWidgets.size() ; pos < coefs.size() ; pos++)
    {
        CoefWidgetStruct coefWidget;
        coefWidget.container = new QWidget();
        coefWidget.name = new QLabel("a<sub>" + QString::number(pos) + "</sub> =");
        coefWidget.line = new QLineEdit();

        coefWidget.line->setText(QString::number(coefs[pos]));
        coefWidget.line->setMaximumHeight(25);
        coefWidget.line->setReadOnly(true);
        coefWidget.line->setAlignment(Qt::AlignLeft);

        QHBoxLayout *layout = new QHBoxLayout();
        layout->setMargin(3);

        layout->addWidget(coefWidget.name);
        layout->addWidget(coefWidget.line);

        coefWidget.container->setLayout(layout);

        ui->coefWidgetsLayout_2->addWidget(coefWidget.container);

        coefWidgets << coefWidget;
    }

    QString genericExpr = "P = a<sub>0</sub> + ";
    if(coefs.size() == 2)
        genericExpr += "a<sub>1</sub> X";
    else if(coefs.size() > 1)
        genericExpr += "a<sub>1</sub> X + ";
    if(coefs.size() == 3)
        genericExpr += "a<sub>2</sub> X<sup>2</sup>";
    else if(coefs.size() > 2)
        genericExpr += "a<sub>2</sub> X<sup>2</sup>";
    if(coefs.size() > 3)
    {
        if(coefs.size() > 4)
        {
            genericExpr += " + ... + ";
            genericExpr += "a<sub>" + QString::number(coefs.size()-2) + "</sub> X<sup>" + QString::number(coefs.size()-2) + "</sup> ";
        }

        genericExpr += "+ a<sub>" + QString::number(coefs.size()-1) + "</sub> X<sup>" + QString::number(coefs.size()-1) + "</sup>";
    }

    ui->polynomialGenericExpression->setText(genericExpr);

    QString calculableExpr = QString::number(coefs.first(), 'g', 8);
    if(coefs.size() > 1)
    {
        if(coefs[1] < 0)
            calculableExpr += " " + QString::number(coefs[1], 'g', 8) + "*x";
        else calculableExpr += " + " + QString::number(coefs[1], 'g', 8) + "*x";
    }

    for(int coef = 2 ; coef < coefs.size() ; coef++)
    {
        if(coefs[coef] < 0)
            calculableExpr += " " + QString::number(coefs[coef], 'g', 8) + "*x^" + QString::number(coef);
        else calculableExpr += " + " + QString::number(coefs[coef], 'g', 8) + "*x^" + QString::number(coef);
    }

    ui->expressionLineEdit->setText(calculableExpr);
}

void PolynomialModelWidget::updateManualRangeFields()
{
    startVal->setNumber(regression->getDrawRange().start);
    endVal->setNumber(regression->getDrawRange().end);
}

void PolynomialModelWidget::updateApproxMethod()
{
    if(ui->approachPoints->isChecked())
        regression->setApproxMethod(ApproachPoints);
    else regression->setApproxMethod(ApproachSegments);
}

void PolynomialModelWidget::updateRangeOption()
{
    if(ui->dataInterval->isChecked())
        regression->setDrawRangeCalculusMethod(LimitedToData);

    else if(ui->relativeExtrapolation->isChecked())
        regression->setDrawRangeCalculusMethod(RelativeExtrapolation);

    else if(ui->manualInterval->isChecked())
        regression->setDrawRangeCalculusMethod(Manual);
}

void PolynomialModelWidget::addWidgetsToUI()
{
    QHBoxLayout *layout = new QHBoxLayout();
    layout->setMargin(0);

    startVal = new NumberLineEdit(false, information->getFuncsList());
    startVal->setEnabled(false);
    startVal->setMaximumHeight(22);
    startVal->setMaximumWidth(150);

    connect(startVal, SIGNAL(newVal(double)), this, SLOT(manualRangeEdited()));

    endVal = new NumberLineEdit(false, information->getFuncsList());
    endVal->setEnabled(false);
    endVal->setMaximumHeight(22);
    endVal->setMaximumWidth(150);

    connect(endVal, SIGNAL(newVal(double)), this, SLOT(manualRangeEdited()));

    QLabel *start = new QLabel(tr("Start:"));
    start->setEnabled(false);

    QLabel *end = new QLabel(tr("End:"));
    end->setEnabled(false);

    layout->addSpacing(30);
    layout->addWidget(start);
    layout->addWidget(startVal);
    layout->addSpacing(10);
    layout->addWidget(end);
    layout->addWidget(endVal);
    layout->addStretch();

    ui->manualEntryLayout->addLayout(layout);

    connect(ui->manualInterval, SIGNAL(toggled(bool)), start, SLOT(setEnabled(bool)));
    connect(ui->manualInterval, SIGNAL(toggled(bool)), end, SLOT(setEnabled(bool)));
    connect(ui->manualInterval, SIGNAL(toggled(bool)), startVal, SLOT(setEnabled(bool)));
    connect(ui->manualInterval, SIGNAL(toggled(bool)), endVal, SLOT(setEnabled(bool)));

    colorButton = new QColorButton(information->getSettingsVals().defaultColor);
    QLabel *colorLabel = new QLabel(tr("color:"));
    ui->drawOptionsLayout->addWidget(colorLabel);
    ui->drawOptionsLayout->addWidget(colorButton);
    ui->drawOptionsLayout->addStretch();

    connect(colorButton, SIGNAL(colorChanged(QColor)), this, SIGNAL(regressionEdited()));

    QPushButton *removeButton = new QPushButton();
    removeButton->setIcon(QIcon(":/icons/remove.png"));
    removeButton->setFlat(true);

    ui->valuesTab->setCornerWidget(removeButton);

    connect(removeButton, SIGNAL(released()), this, SIGNAL(removeMe()));
}

void PolynomialModelWidget::setAbscissaName(QString name)
{
    abscissa = name;
    regression->setAbscissaName(name);  
}

void PolynomialModelWidget::setOrdinateName(QString name)
{
    ordinate = name;
    regression->setOrdinateName(name);   
}

void PolynomialModelWidget::setData(const QList<Point> &dat)
{    
    regression->setData(dat);
}

void PolynomialModelWidget::setPolar(bool pol)
{   
    polar = pol;
    regression->setPolar(pol);
}

PolynomialModelWidget::~PolynomialModelWidget()
{
    delete ui;

    information->removeDataRegression(regression);

    delete regression;
}