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