-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsizeedit.cpp
165 lines (148 loc) · 4.46 KB
/
sizeedit.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
#include "sizeedit.h"
#include <QRegularExpressionValidator>
#include <QLocale>
#include "util.h"
#include <QKeyEvent>
#include <cmath>
SizeEdit::SizeEdit(QWidget *parent) : QLineEdit(parent)
{
QString dec = QRegularExpression::escape(QLocale().decimalPoint());
// validator is lax to allow users maximum flexibility when editing; fix it up on editingFinished
setValidator(new QRegularExpressionValidator(QRegularExpression(QString("^\\d*(") + dec + "\\d*)?[BKMGTPE]?$"), this));
connect(this, &SizeEdit::editingFinished, this, &SizeEdit::onEditingFinished);
updateActive = false;
connect(this, &SizeEdit::textEdited, this, &SizeEdit::onTextEdited);
}
void SizeEdit::setUnit(QChar unit)
{
QLocale l;
l.setNumberOptions(QLocale::OmitGroupSeparator);
auto sVal = text();
if(sVal.isEmpty() || sVal == l.decimalPoint()) sVal = "1";
// strip unit from text if present
auto cUnit = sVal.at(sVal.size()-1);
if(cUnit < '0' || cUnit > '9')
sVal = sVal.left(sVal.length()-1);
double val = l.toDouble(sVal);
if(unit == 'B') {
val = floor(val);
}
auto newText = l.toString(val) + unit.toUpper();
setText(newText);
setSelection(newText.length()-1, 1);
emit textEdited(newText);
}
void SizeEdit::keyPressEvent(QKeyEvent* event)
{
switch(event->key()) {
case Qt::Key_B:
setUnit('B');
return;
case Qt::Key_K:
setUnit('K');
return;
case Qt::Key_M:
setUnit('M');
return;
case Qt::Key_G:
setUnit('G');
return;
case Qt::Key_T:
setUnit('T');
return;
case Qt::Key_P:
setUnit('P');
return;
case Qt::Key_E:
setUnit('E');
return;
default:
QLineEdit::keyPressEvent(event);
}
}
quint64 SizeEdit::getBytes() const
{
return sizeToBytes(text());
}
QString SizeEdit::getSizeString() const
{
auto ret = text();
QLocale l;
ret.replace(l.groupSeparator(), "").replace(l.decimalPoint(), ".");
// append B for pure sizes (needed for ParPar to know it's a size)
if(ret.isEmpty()) return ret;
auto cUnit = ret.at(ret.size()-1).toUpper();
if(cUnit >= 'A' && cUnit <= 'Z')
return ret;
return ret + "B";
}
void SizeEdit::setBytesApprox(quint64 bytes, bool noTrigger)
{
QString s = friendlySize(bytes);
s.replace(QLocale().groupSeparator(), "");
if(noTrigger) blockSignals(true);
setText(s);
if(noTrigger) blockSignals(false);
}
void SizeEdit::setBytes(quint64 bytes, bool noTrigger)
{
QLocale l;
l.setNumberOptions(QLocale::OmitGroupSeparator);
// modification to friendlySize: only pick a larger unit if it can be represented exactly
QStringList units{"B", "K", "M", "G", "T", "P", "E"};
quint64 v = bytes;
QString s;
int ui = 0;
for(; ui < units.size(); ui++) {
if(v < 10000) break;
if((v % 1024) != 0) {
// if fractional part can be represented exactly, do the divide and stop
if((v*100) % 1024 == 0) {
v = (v*100) / 1024;
s = l.toString(static_cast<double>(v) / 100, 'f', 2) + units[ui+1];
}
break;
}
v /= 1024;
}
if(s.isEmpty())
s = l.toString(v) + units[ui];
if(noTrigger) blockSignals(true);
setText(s.replace(l.groupSeparator(), ""));
if(noTrigger) blockSignals(false);
}
void SizeEdit::onTextEdited(const QString& text)
{
updateActive = true;
emit valueChanged(getBytes(), false);
}
void SizeEdit::onEditingFinished()
{
if(updateActive) {
updateActive = false;
QLocale l;
l.setNumberOptions(QLocale::OmitGroupSeparator);
// fix text
auto sVal = text();
if(sVal.isEmpty() || sVal == l.decimalPoint()) sVal = "1";
// strip unit from text if present
auto cUnit = sVal.at(sVal.size()-1);
if(cUnit < '0' || cUnit > '9') {
sVal = sVal.left(sVal.length()-1);
if(cUnit == '.') cUnit = QChar(0);
if(sVal.isEmpty() || sVal == l.decimalPoint()) sVal = "1";
} else
cUnit = QChar(0);
double val = l.toDouble(sVal);
if(cUnit == 'B' || cUnit == 0) {
val = floor(val);
}
auto newText = l.toString(val);
if(cUnit != 0) newText += cUnit.toUpper();
blockSignals(true);
setText(newText);
blockSignals(false);
emit updateFinished();
emit valueChanged(getBytes(), true);
}
}