-
Notifications
You must be signed in to change notification settings - Fork 12
/
ContextAwareSpinBox.cpp
209 lines (172 loc) · 5.16 KB
/
ContextAwareSpinBox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// ContextAwareSpinBox.cpp: description
// Copyright (C) 2023 Gonzalo José Carracedo Carballal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>
//
#include <QLineEdit>
#include <cmath>
#include "ContextAwareSpinBox.h"
#include <QProxyStyle>
class BlockCursorStyle : public QProxyStyle
{
ContextAwareSpinBox *m_spinBox = nullptr;
public:
BlockCursorStyle(QStyle *style, ContextAwareSpinBox *spinbox) : QProxyStyle(style)
{
m_spinBox = spinbox;
}
int pixelMetric(
PixelMetric,
const QStyleOption *option = nullptr,
const QWidget *widget = nullptr) const override;
};
int BlockCursorStyle::pixelMetric(
PixelMetric metric,
const QStyleOption *option,
const QWidget *widget) const
{
switch (metric) {
case PM_TextCursorWidth:
return m_spinBox->blockEnabled()
? -9
: QProxyStyle::pixelMetric(metric, option, widget);
default:
return QProxyStyle::pixelMetric(metric, option, widget);
}
}
void
ContextAwareSpinBox::focusInEvent(QFocusEvent *event)
{
int prefixLen = static_cast<int>(prefix().size());
int suffixLen = static_cast<int>(suffix().size());
int textLen = static_cast<int>(lineEdit()->text().length());
int decSize = decimalLength();
int intLen = textLen - decSize - (prefixLen + suffixLen);
lineEdit()->setCursorPosition(prefixLen + intLen);
QDoubleSpinBox::focusInEvent(event);
}
void
ContextAwareSpinBox::focusOutEvent(QFocusEvent *event)
{
QDoubleSpinBox::focusOutEvent(event);
}
ContextAwareSpinBox::ContextAwareSpinBox(QWidget *parent) : QDoubleSpinBox(parent)
{
QLocale curLocale;
m_baseStyle = lineEdit()->style();
m_blockStyle = new BlockCursorStyle(m_baseStyle, this);
m_decimSep = curLocale.decimalPoint();
lineEdit()->setStyle(m_blockStyle);
}
ContextAwareSpinBox::~ContextAwareSpinBox()
{
}
int
ContextAwareSpinBox::decimalLength() const
{
int prefixLen = static_cast<int>(prefix().size());
int suffixLen = static_cast<int>(suffix().size());
int textLen = static_cast<int>(lineEdit()->text().size());
QString numberText = lineEdit()->text().mid(prefixLen, textLen - prefixLen - suffixLen);
int decPos = numberText.indexOf(m_decimSep);
if (decPos >= 0)
return numberText.size() - decPos;
else
return 0;
}
qreal
ContextAwareSpinBox::currentStep() const
{
int prefixLen = static_cast<int>(prefix().size());
int suffixLen = static_cast<int>(suffix().size());
int textLen = static_cast<int>(lineEdit()->text().length());
int decSize = decimalLength();
int intLen = textLen - decSize - (prefixLen + suffixLen);
int pos = lineEdit()->cursorPosition() - prefixLen;
if (pos < 0)
pos = intLen;
else if (pos > intLen + decSize)
pos = intLen + decSize;
// 1387.01 --> LEN = 7, DECIMALS = 2
// INT = 7 - 2 - 1 = 4
// Pos 0: increment in 10000 (|1387.01)
// Pos 1: increment in 1000 (1|387.01)
// Pos 2: increment in 100 (13|87.01)
// Pos 3: increment in 10 (138|7.01)
// Pos 4, 5: increment in 1 (1387|.01)
// Pos 6: increment in 0.1 (1387.0|1)
// Pos 7: increment in 0.01 (1387.01|)
// Past the floating point, we ignore its position
if (pos > intLen)
--pos;
return pow(10., intLen - pos);
}
int
ContextAwareSpinBox::stepToCursor(qreal step) const
{
int decimPos = static_cast<int>(floor(log10(step)));
int prefixLen = static_cast<int>(prefix().size());
int suffixLen = static_cast<int>(suffix().size());
int textLen = static_cast<int>(lineEdit()->text().length());
int decSize = decimalLength();
int intLen = textLen - decSize - (prefixLen + suffixLen);
// 1387.01 --> LEN = 7, DECIMALS = 2
// INT = 7 - 2 - 1 = 4
// 10000: 0
// 1000 1
// 100 2
// 10 3
// 1 4
// .1 6
// .01 7
int pos = intLen - decimPos;
if (decimPos < 0)
++pos;
return qBound(0, pos, textLen) + prefixLen;
}
void
ContextAwareSpinBox::stepBy(int steps)
{
qreal step = currentStep();
QDoubleSpinBox::setSingleStep(step);
QAbstractSpinBox::stepBy(steps);
lineEdit()->setCursorPosition(stepToCursor(step));
}
void
ContextAwareSpinBox::setSingleStep(qreal step)
{
QDoubleSpinBox::setSingleStep(step);
lineEdit()->setCursorPosition(stepToCursor(step));
}
void
ContextAwareSpinBox::setMinimumStep()
{
int textLen = static_cast<int>(lineEdit()->text().length());
lineEdit()->setCursorPosition(textLen);
}
void
ContextAwareSpinBox::setBlockEnabled(bool en)
{
if (en != m_blockEnabled) {
m_blockEnabled = en;
update();
}
}
bool
ContextAwareSpinBox::blockEnabled() const
{
return m_blockEnabled;
}