diff options
Diffstat (limited to 'src/citra_qt/util/spinbox.cpp')
| -rw-r--r-- | src/citra_qt/util/spinbox.cpp | 63 |
1 files changed, 23 insertions, 40 deletions
diff --git a/src/citra_qt/util/spinbox.cpp b/src/citra_qt/util/spinbox.cpp index 415e7fbec..5868f3b91 100644 --- a/src/citra_qt/util/spinbox.cpp +++ b/src/citra_qt/util/spinbox.cpp | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | // Licensed under GPLv2 or any later version | 1 | // Licensed under GPLv2 or any later version |
| 2 | // Refer to the license.txt file included. | 2 | // Refer to the license.txt file included. |
| 3 | 3 | ||
| 4 | |||
| 5 | // Copyright 2014 Tony Wasserka | 4 | // Copyright 2014 Tony Wasserka |
| 6 | // All rights reserved. | 5 | // All rights reserved. |
| 7 | // | 6 | // |
| @@ -29,15 +28,15 @@ | |||
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | 30 | ||
| 32 | #include <cstdlib> | ||
| 33 | #include <QLineEdit> | 31 | #include <QLineEdit> |
| 34 | #include <QRegExpValidator> | 32 | #include <QRegExpValidator> |
| 33 | #include <cstdlib> | ||
| 35 | 34 | ||
| 36 | #include "citra_qt/util/spinbox.h" | 35 | #include "citra_qt/util/spinbox.h" |
| 37 | #include "common/assert.h" | 36 | #include "common/assert.h" |
| 38 | 37 | ||
| 39 | CSpinBox::CSpinBox(QWidget* parent) : QAbstractSpinBox(parent), min_value(-100), max_value(100), value(0), base(10), num_digits(0) | 38 | CSpinBox::CSpinBox(QWidget* parent) |
| 40 | { | 39 | : QAbstractSpinBox(parent), min_value(-100), max_value(100), value(0), base(10), num_digits(0) { |
| 41 | // TODO: Might be nice to not immediately call the slot. | 40 | // TODO: Might be nice to not immediately call the slot. |
| 42 | // Think of an address that is being replaced by a different one, in which case a lot | 41 | // Think of an address that is being replaced by a different one, in which case a lot |
| 43 | // invalid intermediate addresses would be read from during editing. | 42 | // invalid intermediate addresses would be read from during editing. |
| @@ -46,8 +45,7 @@ CSpinBox::CSpinBox(QWidget* parent) : QAbstractSpinBox(parent), min_value(-100), | |||
| 46 | UpdateText(); | 45 | UpdateText(); |
| 47 | } | 46 | } |
| 48 | 47 | ||
| 49 | void CSpinBox::SetValue(qint64 val) | 48 | void CSpinBox::SetValue(qint64 val) { |
| 50 | { | ||
| 51 | auto old_value = value; | 49 | auto old_value = value; |
| 52 | value = std::max(std::min(val, max_value), min_value); | 50 | value = std::max(std::min(val, max_value), min_value); |
| 53 | 51 | ||
| @@ -57,8 +55,7 @@ void CSpinBox::SetValue(qint64 val) | |||
| 57 | } | 55 | } |
| 58 | } | 56 | } |
| 59 | 57 | ||
| 60 | void CSpinBox::SetRange(qint64 min, qint64 max) | 58 | void CSpinBox::SetRange(qint64 min, qint64 max) { |
| 61 | { | ||
| 62 | min_value = min; | 59 | min_value = min; |
| 63 | max_value = max; | 60 | max_value = max; |
| 64 | 61 | ||
| @@ -66,8 +63,7 @@ void CSpinBox::SetRange(qint64 min, qint64 max) | |||
| 66 | UpdateText(); | 63 | UpdateText(); |
| 67 | } | 64 | } |
| 68 | 65 | ||
| 69 | void CSpinBox::stepBy(int steps) | 66 | void CSpinBox::stepBy(int steps) { |
| 70 | { | ||
| 71 | auto new_value = value; | 67 | auto new_value = value; |
| 72 | // Scale number of steps by the currently selected digit | 68 | // Scale number of steps by the currently selected digit |
| 73 | // TODO: Move this code elsewhere and enable it. | 69 | // TODO: Move this code elsewhere and enable it. |
| @@ -93,8 +89,7 @@ void CSpinBox::stepBy(int steps) | |||
| 93 | UpdateText(); | 89 | UpdateText(); |
| 94 | } | 90 | } |
| 95 | 91 | ||
| 96 | QAbstractSpinBox::StepEnabled CSpinBox::stepEnabled() const | 92 | QAbstractSpinBox::StepEnabled CSpinBox::stepEnabled() const { |
| 97 | { | ||
| 98 | StepEnabled ret = StepNone; | 93 | StepEnabled ret = StepNone; |
| 99 | 94 | ||
| 100 | if (value > min_value) | 95 | if (value > min_value) |
| @@ -106,29 +101,25 @@ QAbstractSpinBox::StepEnabled CSpinBox::stepEnabled() const | |||
| 106 | return ret; | 101 | return ret; |
| 107 | } | 102 | } |
| 108 | 103 | ||
| 109 | void CSpinBox::SetBase(int base) | 104 | void CSpinBox::SetBase(int base) { |
| 110 | { | ||
| 111 | this->base = base; | 105 | this->base = base; |
| 112 | 106 | ||
| 113 | UpdateText(); | 107 | UpdateText(); |
| 114 | } | 108 | } |
| 115 | 109 | ||
| 116 | void CSpinBox::SetNumDigits(int num_digits) | 110 | void CSpinBox::SetNumDigits(int num_digits) { |
| 117 | { | ||
| 118 | this->num_digits = num_digits; | 111 | this->num_digits = num_digits; |
| 119 | 112 | ||
| 120 | UpdateText(); | 113 | UpdateText(); |
| 121 | } | 114 | } |
| 122 | 115 | ||
| 123 | void CSpinBox::SetPrefix(const QString& prefix) | 116 | void CSpinBox::SetPrefix(const QString& prefix) { |
| 124 | { | ||
| 125 | this->prefix = prefix; | 117 | this->prefix = prefix; |
| 126 | 118 | ||
| 127 | UpdateText(); | 119 | UpdateText(); |
| 128 | } | 120 | } |
| 129 | 121 | ||
| 130 | void CSpinBox::SetSuffix(const QString& suffix) | 122 | void CSpinBox::SetSuffix(const QString& suffix) { |
| 131 | { | ||
| 132 | this->suffix = suffix; | 123 | this->suffix = suffix; |
| 133 | 124 | ||
| 134 | UpdateText(); | 125 | UpdateText(); |
| @@ -161,8 +152,7 @@ static QString StringToInputMask(const QString& input) { | |||
| 161 | return mask; | 152 | return mask; |
| 162 | } | 153 | } |
| 163 | 154 | ||
| 164 | void CSpinBox::UpdateText() | 155 | void CSpinBox::UpdateText() { |
| 165 | { | ||
| 166 | // If a fixed number of digits is used, we put the line edit in insertion mode by setting an | 156 | // If a fixed number of digits is used, we put the line edit in insertion mode by setting an |
| 167 | // input mask. | 157 | // input mask. |
| 168 | QString mask; | 158 | QString mask; |
| @@ -179,10 +169,9 @@ void CSpinBox::UpdateText() | |||
| 179 | // The greatest signed 64-bit number has 19 decimal digits. | 169 | // The greatest signed 64-bit number has 19 decimal digits. |
| 180 | // TODO: Could probably make this more generic with some logarithms. | 170 | // TODO: Could probably make this more generic with some logarithms. |
| 181 | // For reference, unsigned 64-bit can have up to 20 decimal digits. | 171 | // For reference, unsigned 64-bit can have up to 20 decimal digits. |
| 182 | int digits = (num_digits != 0) ? num_digits | 172 | int digits = (num_digits != 0) |
| 183 | : (base == 16) ? 16 | 173 | ? num_digits |
| 184 | : (base == 10) ? 19 | 174 | : (base == 16) ? 16 : (base == 10) ? 19 : 0xFF; // fallback case... |
| 185 | : 0xFF; // fallback case... | ||
| 186 | 175 | ||
| 187 | // Match num_digits digits | 176 | // Match num_digits digits |
| 188 | // Digits irrelevant to the chosen number base are filtered in the validator | 177 | // Digits irrelevant to the chosen number base are filtered in the validator |
| @@ -203,29 +192,24 @@ void CSpinBox::UpdateText() | |||
| 203 | lineEdit()->setCursorPosition(cursor_position); | 192 | lineEdit()->setCursorPosition(cursor_position); |
| 204 | } | 193 | } |
| 205 | 194 | ||
| 206 | QString CSpinBox::TextFromValue() | 195 | QString CSpinBox::TextFromValue() { |
| 207 | { | 196 | return prefix + QString(HasSign() ? ((value < 0) ? "-" : "+") : "") + |
| 208 | return prefix | 197 | QString("%1").arg(std::abs(value), num_digits, base, QLatin1Char('0')).toUpper() + |
| 209 | + QString(HasSign() ? ((value < 0) ? "-" : "+") : "") | 198 | suffix; |
| 210 | + QString("%1").arg(std::abs(value), num_digits, base, QLatin1Char('0')).toUpper() | ||
| 211 | + suffix; | ||
| 212 | } | 199 | } |
| 213 | 200 | ||
| 214 | qint64 CSpinBox::ValueFromText() | 201 | qint64 CSpinBox::ValueFromText() { |
| 215 | { | ||
| 216 | unsigned strpos = prefix.length(); | 202 | unsigned strpos = prefix.length(); |
| 217 | 203 | ||
| 218 | QString num_string = text().mid(strpos, text().length() - strpos - suffix.length()); | 204 | QString num_string = text().mid(strpos, text().length() - strpos - suffix.length()); |
| 219 | return num_string.toLongLong(nullptr, base); | 205 | return num_string.toLongLong(nullptr, base); |
| 220 | } | 206 | } |
| 221 | 207 | ||
| 222 | bool CSpinBox::HasSign() const | 208 | bool CSpinBox::HasSign() const { |
| 223 | { | ||
| 224 | return base == 10 && min_value < 0; | 209 | return base == 10 && min_value < 0; |
| 225 | } | 210 | } |
| 226 | 211 | ||
| 227 | void CSpinBox::OnEditingFinished() | 212 | void CSpinBox::OnEditingFinished() { |
| 228 | { | ||
| 229 | // Only update for valid input | 213 | // Only update for valid input |
| 230 | QString input = lineEdit()->text(); | 214 | QString input = lineEdit()->text(); |
| 231 | int pos = 0; | 215 | int pos = 0; |
| @@ -233,8 +217,7 @@ void CSpinBox::OnEditingFinished() | |||
| 233 | SetValue(ValueFromText()); | 217 | SetValue(ValueFromText()); |
| 234 | } | 218 | } |
| 235 | 219 | ||
| 236 | QValidator::State CSpinBox::validate(QString& input, int& pos) const | 220 | QValidator::State CSpinBox::validate(QString& input, int& pos) const { |
| 237 | { | ||
| 238 | if (!prefix.isEmpty() && input.left(prefix.length()) != prefix) | 221 | if (!prefix.isEmpty() && input.left(prefix.length()) != prefix) |
| 239 | return QValidator::Invalid; | 222 | return QValidator::Invalid; |
| 240 | 223 | ||