Skip to content

Commit

Permalink
Merge pull request #9 from daschuer/beatloop_size
Browse files Browse the repository at this point in the history
Some beat size box improvements
  • Loading branch information
Be-ing authored May 24, 2017
2 parents 6c5722c + 5277cc1 commit a17d590
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions src/widget/wbeatspinbox.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <QLineEdit>

#include "widget/wbeatspinbox.h"

#include "control/controlobject.h"
#include "control/controlproxy.h"
#include "util/math.h"

QRegExp WBeatSpinBox::s_regexpBlacklist("[^0-9./ ]");
QRegExp WBeatSpinBox::s_regexpBlacklist("[^0-9.,/ ]");

WBeatSpinBox::WBeatSpinBox(QWidget * parent, ControlObject* pValueControl,
int decimals, double minimum, double maximum)
Expand Down Expand Up @@ -34,21 +36,31 @@ void WBeatSpinBox::setup(const QDomNode& node, const SkinContext& context) {
}

void WBeatSpinBox::stepBy(int steps) {
double newValue = value() * pow(2, steps);
if (newValue < minimum()) {
newValue = minimum();
} else if (newValue > maximum()) {
newValue = maximum();
double oldValue = m_valueControl.get();
double newValue;
QString temp = text();
int cursorPos = lineEdit()->cursorPosition();
if (validate(temp, cursorPos) == QValidator::Acceptable) {
double editValue = valueFromText(temp);
newValue = editValue * pow(2, steps);
if (newValue < minimum() || newValue > maximum()) {
// don't clamp the value here to not fall out of a measure
newValue = editValue;
}
} else {
// here we have an unacceptable edit, going back to the old value first
newValue = oldValue;
}
// Do not call QDoubleSpinBox::setValue directly in case
// the new value of the ControlObject needs to be confirmed.
// Curiously, m_valueControl.set() does not cause slotControlValueChanged
// to execute for beatjump_size, so call QDoubleSpinBox::setValue in this function.
double oldValue = m_valueControl.get();
m_valueControl.set(newValue);
if (m_valueControl.get() != oldValue) {
setValue(newValue);
double coValue = m_valueControl.get();
if (coValue != value()) {
setValue(coValue);
}
selectAll();
}

void WBeatSpinBox::slotSpinboxValueChanged(double newValue) {
Expand Down Expand Up @@ -138,23 +150,14 @@ QString WBeatSpinBox::textFromValue(double value) const {
sFracPart = fractionString(29, 32);
} else if (dFracPart == 0.96875) {
sFracPart = fractionString(31, 32);
} else {
return locale().toString(value, 'g', 5);
}

if (dWholePart > 0) {
if (sFracPart.isEmpty()) {
if (dFracPart == 0.00000) {
return QString::number(dWholePart, 'f', 0);
} else {
return QString::number(value, 'f', 5);
}
}
return QString::number(dWholePart, 'f', 0) + " " + sFracPart;
} else {
if (sFracPart.isEmpty() ) {
return QString::number(value, 'f', 5);
}
return sFracPart;
return locale().toString(dWholePart, 'f', 0) + " " + sFracPart;
}
return sFracPart;
}

double WBeatSpinBox::valueFromText(const QString& text) const {
Expand All @@ -164,7 +167,7 @@ double WBeatSpinBox::valueFromText(const QString& text) const {

bool conversionWorked = false;
double dValue;
dValue = text.toDouble(&conversionWorked);
dValue = locale().toDouble(text, &conversionWorked);
if (conversionWorked) {
return dValue;
}
Expand All @@ -183,7 +186,8 @@ double WBeatSpinBox::valueFromText(const QString& text) const {
sNumerator = splitFraction.at(0);
sDenominator = splitFraction.at(1);

return sIntPart.toDouble() + sNumerator.toDouble() / sDenominator.toDouble();
return locale().toDouble(sIntPart)
+ locale().toDouble(sNumerator) / locale().toDouble(sDenominator);
}

QValidator::State WBeatSpinBox::validate(QString& input, int& pos) const {
Expand All @@ -200,7 +204,7 @@ QValidator::State WBeatSpinBox::validate(QString& input, int& pos) const {
}

bool conversionWorked = false;
input.toDouble(&conversionWorked);
locale().toDouble(input, &conversionWorked);
if (conversionWorked) {
return QValidator::Acceptable;
}
Expand Down

0 comments on commit a17d590

Please sign in to comment.