/* * Copyright 2008 Ben Boeckel * * 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 . */ // 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); }