| 1 |
/* |
| 2 |
* Copyright (C) 2023 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "adpcm_address_spin_box.hpp" |
| 27 |
#include <QIntValidator> |
| 28 |
|
| 29 |
namespace |
| 30 |
{ |
| 31 |
constexpr int ADDR_SHIFT = 5; |
| 32 |
} |
| 33 |
|
| 34 |
namespace |
| 35 |
{ |
| 36 |
namespace start |
| 37 |
{ |
| 38 |
constexpr int addressToPosition(int a) |
| 39 |
{ |
| 40 |
return a << (ADDR_SHIFT + 1); |
| 41 |
} |
| 42 |
|
| 43 |
constexpr int bytesToPosition(int b) |
| 44 |
{ |
| 45 |
return addressToPosition(b >> ADDR_SHIFT); |
| 46 |
} |
| 47 |
|
| 48 |
constexpr int positionToAddress(int p) |
| 49 |
{ |
| 50 |
return p >> (ADDR_SHIFT + 1); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
class StartAddressValidator : public QIntValidator |
| 55 |
{ |
| 56 |
public: |
| 57 |
StartAddressValidator(int bottom, int top, QObject *parent = nullptr) : QIntValidator(bottom, top, parent) {} |
| 58 |
QValidator::State validate(QString& input, int& pos) const override |
| 59 |
{ |
| 60 |
auto state = QIntValidator::validate(input, pos); |
| 61 |
if (state == QIntValidator::Invalid) { |
| 62 |
return QValidator::Invalid; |
| 63 |
} |
| 64 |
|
| 65 |
return !(input.toInt() % (1 << (ADDR_SHIFT + 1))) ? QValidator::Acceptable : QValidator::Intermediate; |
| 66 |
} |
| 67 |
}; |
| 68 |
} |
| 69 |
|
| 70 |
AdpcmStartAddressSpinBox::AdpcmStartAddressSpinBox(QWidget* parent) |
| 71 |
: QSpinBox(parent) |
| 72 |
{ |
| 73 |
setSingleStep(1 << (ADDR_SHIFT + 1)); |
| 74 |
setMinimum(start::addressToPosition(0)); |
| 75 |
setValue(minimum()); |
| 76 |
setMaximum(minimum()); |
| 77 |
} |
| 78 |
|
| 79 |
int AdpcmStartAddressSpinBox::valueByAddress() const |
| 80 |
{ |
| 81 |
return start::positionToAddress(value()); |
| 82 |
} |
| 83 |
|
| 84 |
void AdpcmStartAddressSpinBox::setValueByAddress(int a) |
| 85 |
{ |
| 86 |
setValue(start::addressToPosition(a)); |
| 87 |
} |
| 88 |
|
| 89 |
void AdpcmStartAddressSpinBox::setValueByBytes(int b) |
| 90 |
{ |
| 91 |
setValue(start::bytesToPosition(b)); |
| 92 |
} |
| 93 |
|
| 94 |
void AdpcmStartAddressSpinBox::setMaximumByBytes(int b) |
| 95 |
{ |
| 96 |
setMaximum(start::bytesToPosition(b)); |
| 97 |
} |
| 98 |
|
| 99 |
QValidator::State AdpcmStartAddressSpinBox::validate(QString& text, int& pos) const |
| 100 |
{ |
| 101 |
return StartAddressValidator(minimum(), maximum()).validate(text, pos); |
| 102 |
} |
| 103 |
|
| 104 |
namespace |
| 105 |
{ |
| 106 |
namespace stop |
| 107 |
{ |
| 108 |
constexpr int addressToPosition(int a) |
| 109 |
{ |
| 110 |
// (((a << ADDR_SHIFT) & 31) << 1) + 1 |
| 111 |
return (a << (ADDR_SHIFT + 1)) + 63; |
| 112 |
} |
| 113 |
|
| 114 |
constexpr int bytesToPosition(int b) |
| 115 |
{ |
| 116 |
return addressToPosition(b >> ADDR_SHIFT); |
| 117 |
} |
| 118 |
|
| 119 |
constexpr int positionToAddress(int p) |
| 120 |
{ |
| 121 |
return p >> (ADDR_SHIFT + 1); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
class StopAddressValidator : public QIntValidator |
| 126 |
{ |
| 127 |
public: |
| 128 |
StopAddressValidator(int bottom, int top, QObject *parent = nullptr) : QIntValidator(bottom, top, parent) {} |
| 129 |
QValidator::State validate(QString& input, int& pos) const override |
| 130 |
{ |
| 131 |
auto state = QIntValidator::validate(input, pos); |
| 132 |
if (state == QIntValidator::Invalid) { |
| 133 |
return QValidator::Invalid; |
| 134 |
} |
| 135 |
|
| 136 |
return !((input.toInt() + 1) % (1 << (ADDR_SHIFT + 1))) ? QValidator::Acceptable : QValidator::Intermediate; |
| 137 |
} |
| 138 |
}; |
| 139 |
} |
| 140 |
|
| 141 |
AdpcmStopAddressSpinBox::AdpcmStopAddressSpinBox(QWidget* parent) |
| 142 |
: QSpinBox(parent) |
| 143 |
{ |
| 144 |
setSingleStep(1 << (ADDR_SHIFT + 1)); |
| 145 |
setMinimum(stop::addressToPosition(0)); |
| 146 |
setValue(minimum()); |
| 147 |
setMaximum(minimum()); |
| 148 |
} |
| 149 |
|
| 150 |
int AdpcmStopAddressSpinBox::valueByAddress() const |
| 151 |
{ |
| 152 |
return stop::positionToAddress(value()); |
| 153 |
} |
| 154 |
|
| 155 |
void AdpcmStopAddressSpinBox::setValueByAddress(int a) |
| 156 |
{ |
| 157 |
setValue(stop::addressToPosition(a)); |
| 158 |
} |
| 159 |
|
| 160 |
void AdpcmStopAddressSpinBox::setValueByBytes(int b) |
| 161 |
{ |
| 162 |
setValue(stop::bytesToPosition(b)); |
| 163 |
} |
| 164 |
|
| 165 |
void AdpcmStopAddressSpinBox::setMaximumByBytes(int b) |
| 166 |
{ |
| 167 |
setMaximum(stop::bytesToPosition(b)); |
| 168 |
} |
| 169 |
|
| 170 |
QValidator::State AdpcmStopAddressSpinBox::validate(QString& text, int& pos) const |
| 171 |
{ |
| 172 |
return StopAddressValidator(minimum(), maximum()).validate(text, pos); |
| 173 |
} |