| 1 |
/* |
| 2 |
* Audacity: A Digital Audio Editor |
| 3 |
*/ |
| 4 |
#include "bpmmodel.h" |
| 5 |
|
| 6 |
#include <QKeyEvent> |
| 7 |
|
| 8 |
#include "numericformatter.h" |
| 9 |
|
| 10 |
#include "log.h" |
| 11 |
|
| 12 |
static constexpr double BPM_MAX = 999.0; |
| 13 |
static constexpr double BPM_MIN = 1.0; |
| 14 |
|
| 15 |
using namespace au::playback; |
| 16 |
|
| 17 |
static bool isFieldEditable(const QChar& fieldSymbol) |
| 18 |
{ |
| 19 |
return fieldSymbol.isDigit(); |
| 20 |
} |
| 21 |
|
| 22 |
BPMModel::BPMModel(QObject* parent) |
| 23 |
: QAbstractListModel(parent) |
| 24 |
{ |
| 25 |
initFieldInteractionController(); |
| 26 |
|
| 27 |
reloadFormatter(); |
| 28 |
|
| 29 |
setValue(0.0); |
| 30 |
} |
| 31 |
|
| 32 |
void BPMModel::upValue() |
| 33 |
{ |
| 34 |
setValue(std::min(m_value + 1, BPM_MAX)); |
| 35 |
} |
| 36 |
|
| 37 |
void BPMModel::downValue() |
| 38 |
{ |
| 39 |
setValue(std::max(m_value - 1, BPM_MIN)); |
| 40 |
} |
| 41 |
|
| 42 |
int BPMModel::rowCount(const QModelIndex& parent) const |
| 43 |
{ |
| 44 |
UNUSED(parent); |
| 45 |
return m_valueString.size(); |
| 46 |
} |
| 47 |
|
| 48 |
QVariant BPMModel::data(const QModelIndex& index, int role) const |
| 49 |
{ |
| 50 |
if (!index.isValid()) { |
| 51 |
return QVariant(); |
| 52 |
} |
| 53 |
|
| 54 |
QChar ch = m_valueString[index.row()]; |
| 55 |
|
| 56 |
switch (role) { |
| 57 |
case rSymbol: return QVariant::fromValue(ch); |
| 58 |
case rIsEditable: return isFieldEditable(ch); |
| 59 |
} |
| 60 |
|
| 61 |
return QVariant(); |
| 62 |
} |
| 63 |
|
| 64 |
QHash<int, QByteArray> BPMModel::roleNames() const |
| 65 |
{ |
| 66 |
static const QHash<int, QByteArray> roles = { |
| 67 |
{ rSymbol, "symbol" }, |
| 68 |
{ rIsEditable, "editable" } |
| 69 |
}; |
| 70 |
|
| 71 |
return roles; |
| 72 |
} |
| 73 |
|
| 74 |
double BPMModel::value() const |
| 75 |
{ |
| 76 |
return m_value; |
| 77 |
} |
| 78 |
|
| 79 |
void BPMModel::setValue(double value) |
| 80 |
{ |
| 81 |
if (value < BPM_MIN || value > BPM_MAX) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if (qFuzzyCompare(m_value, value)) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
m_value = value; |
| 90 |
|
| 91 |
updateValueString(); |
| 92 |
|
| 93 |
emit valueChanged(); |
| 94 |
} |
| 95 |
|
| 96 |
int BPMModel::currentEditedFieldIndex() const |
| 97 |
{ |
| 98 |
return m_fieldsInteractionController->currentEditedFieldIndex(); |
| 99 |
} |
| 100 |
|
| 101 |
void BPMModel::setCurrentEditedFieldIndex(int index) |
| 102 |
{ |
| 103 |
m_fieldsInteractionController->setCurrentEditedFieldIndex(index); |
| 104 |
} |
| 105 |
|
| 106 |
QQuickItem* BPMModel::visualItem() const |
| 107 |
{ |
| 108 |
return m_fieldsInteractionController->visualItem(); |
| 109 |
} |
| 110 |
|
| 111 |
void BPMModel::setVisualItem(QQuickItem* item) |
| 112 |
{ |
| 113 |
m_fieldsInteractionController->setVisualItem(item); |
| 114 |
} |
| 115 |
|
| 116 |
void BPMModel::reloadFormatter() |
| 117 |
{ |
| 118 |
static const QString FORMAT = "1000bpm"; // translate |
| 119 |
|
| 120 |
m_formatter = std::make_shared<NumericFormatter>(FORMAT); |
| 121 |
|
| 122 |
initFormatter(); |
| 123 |
|
| 124 |
m_fieldsInteractionController->setFormatter(m_formatter); |
| 125 |
} |
| 126 |
|
| 127 |
void BPMModel::initFormatter() |
| 128 |
{ |
| 129 |
m_formatter->init(); |
| 130 |
} |
| 131 |
|
| 132 |
void BPMModel::initFieldInteractionController() |
| 133 |
{ |
| 134 |
m_fieldsInteractionController = std::make_shared<FieldsInteractionController>(this); |
| 135 |
|
| 136 |
connect(m_fieldsInteractionController.get(), &FieldsInteractionController::currentEditedFieldIndexChanged, |
| 137 |
this, &BPMModel::currentEditedFieldIndexChanged); |
| 138 |
|
| 139 |
connect(m_fieldsInteractionController.get(), &FieldsInteractionController::valueChanged, |
| 140 |
this, &BPMModel::setValue); |
| 141 |
} |
| 142 |
|
| 143 |
void BPMModel::updateValueString() |
| 144 |
{ |
| 145 |
QString newValueString = m_formatter->valueToString(m_value, false).valueString; |
| 146 |
|
| 147 |
if (newValueString.size() != m_valueString.size()) { |
| 148 |
beginResetModel(); |
| 149 |
m_valueString = newValueString; |
| 150 |
endResetModel(); |
| 151 |
} else { |
| 152 |
for (int i = 0; i < newValueString.size(); ++i) { |
| 153 |
if (newValueString[i] != m_valueString[i]) { |
| 154 |
m_valueString[i] = newValueString[i]; |
| 155 |
|
| 156 |
QModelIndex index = createIndex(i, 0); |
| 157 |
emit dataChanged(index, index, { rSymbol }); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
m_fieldsInteractionController->setValueString(m_valueString); |
| 163 |
} |