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
|
/*
* Copyright 2008 Ben Boeckel <MathStuf@gmail.com>
*
* This program is free software: you can redistribute it 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 program 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/>.
*/
// Header include
#include "FractionWidget.h"
Sigmodr::FractionWidget::FractionWidget(QWidget* parent, const Sigcore::Fraction& value) :
QWidget(parent),
m_behavior(Any),
m_value(value)
{
setupUi(this);
connect(this, SIGNAL(valueChanged(Sigcore::Fraction)), SLOT(updateValue()));
connect(this, SIGNAL(valueChanged(Sigcore::Fraction)), SLOT(resetRanges()));
connect(this, SIGNAL(behaviorChanged(const Behavior)), SLOT(resetRanges()));
resetRanges();
}
Sigmodr::FractionWidget::Behavior Sigmodr::FractionWidget::behavior() const
{
return m_behavior;
}
Sigcore::Fraction Sigmodr::FractionWidget::value() const
{
return m_value;
}
void Sigmodr::FractionWidget::setBehavior(const Behavior behavior)
{
m_behavior = behavior;
emit(behaviorChanged(m_behavior));
}
void Sigmodr::FractionWidget::setValue(const Sigcore::Fraction& value)
{
if (!varValue->text().isEmpty() && (m_value == value))
return;
m_value = value;
emit(valueChanged(m_value));
}
void Sigmodr::FractionWidget::updateValue()
{
varValue->setText(QString::number(double(m_value), 'g', 7));
}
void Sigmodr::FractionWidget::on_varNumerator_valueChanged(const int numerator)
{
m_value.setNumerator(numerator);
emit(valueChanged(m_value));
}
void Sigmodr::FractionWidget::on_varDenominator_valueChanged(const int denominator)
{
m_value.setDenominator(denominator);
emit(valueChanged(m_value));
}
void Sigmodr::FractionWidget::resetRanges()
{
varNumerator->setValue(m_value.numerator());
varDenominator->setValue(m_value.denominator());
int numMin = 0;
int numMax = INT_MAX;
int denomMin = 1;
int denomMax = INT_MAX;
switch (m_behavior)
{
case NonZero:
numMin = 1;
case Proper:
numMax = m_value.denominator();
break;
case Improper:
denomMax = m_value.numerator();
case AnyButZero:
numMin = 1;
default:
break;
}
varNumerator->setRange(numMin, numMax);
varDenominator->setRange(denomMin, denomMax);
}
|