|
| 1 | +#include "KeySignature.h" |
| 2 | +#include "KeySignature_p.h" |
| 3 | + |
| 4 | +#include <QVariant> |
| 5 | +#include <QJsonObject> |
| 6 | + |
| 7 | +#include <dspxmodel/Model.h> |
| 8 | +#include <dspxmodel/ModelStrategy.h> |
| 9 | +#include <dspxmodel/KeySignatureSequence.h> |
| 10 | + |
| 11 | +namespace dspx { |
| 12 | + |
| 13 | + void KeySignaturePrivate::setKeySignatureSequence(KeySignature *item, KeySignatureSequence *keySignatureSequence) { |
| 14 | + auto d = item->d_func(); |
| 15 | + if (d->keySignatureSequence != keySignatureSequence) { |
| 16 | + d->keySignatureSequence = keySignatureSequence; |
| 17 | + Q_EMIT item->keySignatureSequenceChanged(); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + void KeySignaturePrivate::setPreviousItem(KeySignature *item, KeySignature *previousItem) { |
| 22 | + auto d = item->d_func(); |
| 23 | + if (d->previousItem != previousItem) { |
| 24 | + d->previousItem = previousItem; |
| 25 | + Q_EMIT item->previousItemChanged(); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + void KeySignaturePrivate::setNextItem(KeySignature *item, KeySignature *nextItem) { |
| 30 | + auto d = item->d_func(); |
| 31 | + if (d->nextItem != nextItem) { |
| 32 | + d->nextItem = nextItem; |
| 33 | + Q_EMIT item->nextItemChanged(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + KeySignature::KeySignature(Handle handle, Model *model) : EntityObject(handle, model), d_ptr(new KeySignaturePrivate) { |
| 38 | + Q_D(KeySignature); |
| 39 | + Q_ASSERT(model->strategy()->getEntityType(handle) == ModelStrategy::EI_KeySignature); |
| 40 | + d->q_ptr = this; |
| 41 | + d->pos = model->strategy()->getEntityProperty(handle, ModelStrategy::P_Position).toInt(); |
| 42 | + d->mode = model->strategy()->getEntityProperty(handle, ModelStrategy::P_Mode).toInt(); |
| 43 | + d->tonality = model->strategy()->getEntityProperty(handle, ModelStrategy::P_Tonality).toInt(); |
| 44 | + d->accidentalType = static_cast<AccidentalType>(model->strategy()->getEntityProperty(handle, ModelStrategy::P_AccidentalType).toInt()); |
| 45 | + } |
| 46 | + |
| 47 | + KeySignature::~KeySignature() = default; |
| 48 | + |
| 49 | + int KeySignature::pos() const { |
| 50 | + Q_D(const KeySignature); |
| 51 | + return d->pos; |
| 52 | + } |
| 53 | + |
| 54 | + void KeySignature::setPos(int pos) { |
| 55 | + Q_D(KeySignature); |
| 56 | + Q_ASSERT(pos >= 0); |
| 57 | + model()->strategy()->setEntityProperty(handle(), ModelStrategy::P_Position, pos); |
| 58 | + } |
| 59 | + |
| 60 | + int KeySignature::mode() const { |
| 61 | + Q_D(const KeySignature); |
| 62 | + return d->mode; |
| 63 | + } |
| 64 | + |
| 65 | + void KeySignature::setMode(int mode) { |
| 66 | + Q_D(KeySignature); |
| 67 | + Q_ASSERT(mode >= 0 && mode <= 4095); |
| 68 | + model()->strategy()->setEntityProperty(handle(), ModelStrategy::P_Mode, mode); |
| 69 | + } |
| 70 | + |
| 71 | + int KeySignature::tonality() const { |
| 72 | + Q_D(const KeySignature); |
| 73 | + return d->tonality; |
| 74 | + } |
| 75 | + |
| 76 | + void KeySignature::setTonality(int tonality) { |
| 77 | + Q_D(KeySignature); |
| 78 | + Q_ASSERT(tonality >= 0 && tonality <= 11); |
| 79 | + model()->strategy()->setEntityProperty(handle(), ModelStrategy::P_Tonality, tonality); |
| 80 | + } |
| 81 | + |
| 82 | + KeySignature::AccidentalType KeySignature::accidentalType() const { |
| 83 | + Q_D(const KeySignature); |
| 84 | + return d->accidentalType; |
| 85 | + } |
| 86 | + |
| 87 | + void KeySignature::setAccidentalType(AccidentalType accidentalType) { |
| 88 | + Q_D(KeySignature); |
| 89 | + Q_ASSERT(accidentalType == Flat || accidentalType == Sharp); |
| 90 | + model()->strategy()->setEntityProperty(handle(), ModelStrategy::P_AccidentalType, static_cast<int>(accidentalType)); |
| 91 | + } |
| 92 | + |
| 93 | + KeySignatureSequence *KeySignature::keySignatureSequence() const { |
| 94 | + Q_D(const KeySignature); |
| 95 | + return d->keySignatureSequence; |
| 96 | + } |
| 97 | + |
| 98 | + KeySignature *KeySignature::previousItem() const { |
| 99 | + Q_D(const KeySignature); |
| 100 | + return d->previousItem; |
| 101 | + } |
| 102 | + |
| 103 | + KeySignature *KeySignature::nextItem() const { |
| 104 | + Q_D(const KeySignature); |
| 105 | + return d->nextItem; |
| 106 | + } |
| 107 | + |
| 108 | + QJsonObject KeySignature::toQDspx() const { |
| 109 | + return { |
| 110 | + {"pos", pos()}, |
| 111 | + {"mode", mode()}, |
| 112 | + {"tonality", tonality()}, |
| 113 | + {"accidentalType", accidentalType()} |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + void KeySignature::fromQDspx(const QJsonObject &keySignature) { |
| 118 | + auto pos = keySignature.value("pos").toInt(); |
| 119 | + pos = qMax(0, pos); |
| 120 | + setPos(pos); |
| 121 | + auto mode = keySignature.value("mode").toInt(); |
| 122 | + if (mode < 0 || mode > 4095) { |
| 123 | + mode = 0; |
| 124 | + } |
| 125 | + setMode(mode); |
| 126 | + auto tonality = keySignature.value("tonality").toInt(); |
| 127 | + if (tonality < 0 || tonality > 11) { |
| 128 | + tonality = 0; |
| 129 | + } |
| 130 | + setTonality(tonality); |
| 131 | + auto accidentalType = keySignature.value("accidentalType").toInt(); |
| 132 | + if (accidentalType != Flat && accidentalType != Sharp) { |
| 133 | + accidentalType = Flat; |
| 134 | + } |
| 135 | + setAccidentalType(static_cast<AccidentalType>(accidentalType)); |
| 136 | + } |
| 137 | + |
| 138 | + void KeySignature::handleSetEntityProperty(int property, const QVariant &value) { |
| 139 | + Q_D(KeySignature); |
| 140 | + switch (property) { |
| 141 | + case ModelStrategy::P_Position: { |
| 142 | + d->pos = value.toInt(); |
| 143 | + Q_EMIT posChanged(d->pos); |
| 144 | + break; |
| 145 | + } |
| 146 | + case ModelStrategy::P_Mode: { |
| 147 | + d->mode = value.toInt(); |
| 148 | + Q_EMIT modeChanged(d->mode); |
| 149 | + break; |
| 150 | + } |
| 151 | + case ModelStrategy::P_Tonality: { |
| 152 | + d->tonality = value.toInt(); |
| 153 | + Q_EMIT tonalityChanged(d->tonality); |
| 154 | + break; |
| 155 | + } |
| 156 | + case ModelStrategy::P_AccidentalType: { |
| 157 | + d->accidentalType = static_cast<AccidentalType>(value.toInt()); |
| 158 | + Q_EMIT accidentalTypeChanged(d->accidentalType); |
| 159 | + break; |
| 160 | + } |
| 161 | + default: |
| 162 | + Q_UNREACHABLE(); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | +} |
| 167 | + |
| 168 | +#include "moc_KeySignature.cpp" |
0 commit comments