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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "encodingdialog.h"
#if QT_CONFIG(action)
# include <QAction>
#endif
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#if QT_CONFIG(clipboard)
# include <QGuiApplication>
# include <QClipboard>
#endif
#include <QTextStream>
// Helpers for formatting character sequences
// Format a special character like '\x0a'
template <class Int>
static void formatEscapedNumber(QTextStream &str, Int value, int base,
int width = 0,char prefix = 0)
{
str << '\\';
if (prefix)
str << prefix;
const auto oldPadChar = str.padChar();
const auto oldFieldWidth = str.fieldWidth();
const auto oldFieldAlignment = str.fieldAlignment();
const auto oldIntegerBase = str.integerBase();
str.setPadChar(QLatin1Char('0'));
str.setFieldWidth(width);
str.setFieldAlignment(QTextStream::AlignRight);
str.setIntegerBase(base);
str << value;
str.setIntegerBase(oldIntegerBase);
str.setFieldAlignment(oldFieldAlignment);
str.setFieldWidth(oldFieldWidth);
str.setPadChar(oldPadChar);
}
template <class Int>
static bool formatSpecialCharacter(QTextStream &str, Int value)
{
bool result = true;
switch (value) {
case '\\':
str << "\\\\";
break;
case '\"':
str << "\\\"";
break;
case '\n':
str << "\\n";
break;
default:
result = false;
break;
}
return result;
}
// Format a sequence of characters (QChar, ushort (UTF-16), uint (UTF-32)
// or just char (Latin1, Utf-8)) with the help of traits specifying
// how to obtain the code for checking the printable-ness and how to
// stream out the plain ASCII values.
template <EncodingDialog::Encoding>
struct FormattingTraits
{
};
template <>
struct FormattingTraits<EncodingDialog::Unicode>
{
static ushort code(QChar c) { return c.unicode(); }
static char toAscii(QChar c) { return c.toLatin1(); }
};
template <>
struct FormattingTraits<EncodingDialog::Utf8>
{
static ushort code(char c) { return uchar(c); }
static char toAscii(char c) { return c; }
};
template <>
struct FormattingTraits<EncodingDialog::Utf16>
{
static ushort code(ushort c) { return c; }
static char toAscii(ushort c) { return char(c); }
};
template <>
struct FormattingTraits<EncodingDialog::Utf32>
{
static uint code(uint c) { return c; }
static char toAscii(uint c) { return char(c); }
};
template <>
struct FormattingTraits<EncodingDialog::Latin1>
{
static uchar code(char c) { return uchar(c); }
static char toAscii(char c) { return c; }
};
static bool isHexDigit(char c)
{
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')
|| (c >= 'A' && c <= 'F');
}
template <EncodingDialog::Encoding encoding, class Iterator>
static void formatStringSequence(QTextStream &str, Iterator i1, Iterator i2,
int escapeIntegerBase, int escapeWidth,
char escapePrefix = 0)
{
str << '"';
bool separateHexEscape = false;
for (; i1 != i2; ++i1) {
const auto code = FormattingTraits<encoding>::code(*i1);
if (code >= 0x80) {
formatEscapedNumber(str, code, escapeIntegerBase, escapeWidth, escapePrefix);
separateHexEscape = escapeIntegerBase == 16 && escapeWidth == 0;
} else {
if (!formatSpecialCharacter(str, code)) {
const char c = FormattingTraits<encoding>::toAscii(*i1);
// For variable width/hex: Terminate the literal to stop digit parsing
// ("\x12" "34...").
if (separateHexEscape && isHexDigit(c))
str << "\" \"";
str << c;
}
separateHexEscape = false;
}
}
str << '"';
}
static QString encodedString(const QString &value, EncodingDialog::Encoding e)
{
QString result;
QTextStream str(&result);
switch (e) {
case EncodingDialog::Unicode:
formatStringSequence<EncodingDialog::Unicode>(str, value.cbegin(), value.cend(),
16, 4, 'u');
break;
case EncodingDialog::Utf8: {
const QByteArray utf8 = value.toUtf8();
str << "u8";
formatStringSequence<EncodingDialog::Utf8>(str, utf8.cbegin(), utf8.cend(),
8, 3);
}
break;
case EncodingDialog::Utf16: {
auto utf16 = value.utf16();
auto utf16End = utf16 + value.size();
str << 'u';
formatStringSequence<EncodingDialog::Utf16>(str, utf16, utf16End,
16, 0, 'x');
}
break;
case EncodingDialog::Utf32: {
auto utf32 = value.toUcs4();
str << 'U';
formatStringSequence<EncodingDialog::Utf32>(str, utf32.cbegin(), utf32.cend(),
16, 0, 'x');
}
break;
case EncodingDialog::Latin1: {
const QByteArray latin1 = value.toLatin1();
formatStringSequence<EncodingDialog::Latin1>(str, latin1.cbegin(), latin1.cend(),
16, 0, 'x');
}
break;
case EncodingDialog::EncodingCount:
break;
}
return result;
}
// Dialog helpers
static const char *encodingLabels[]
{
QT_TRANSLATE_NOOP("EncodingDialog", "Unicode:"),
QT_TRANSLATE_NOOP("EncodingDialog", "UTF-8:"),
QT_TRANSLATE_NOOP("EncodingDialog", "UTF-16:"),
QT_TRANSLATE_NOOP("EncodingDialog", "UTF-32:"),
QT_TRANSLATE_NOOP("EncodingDialog", "Latin1:")
};
static const char *encodingToolTips[]
{
QT_TRANSLATE_NOOP("EncodingDialog", "Unicode points for use with any encoding (C++, Python)"),
QT_TRANSLATE_NOOP("EncodingDialog", "QString::fromUtf8()"),
QT_TRANSLATE_NOOP("EncodingDialog", "wchar_t on Windows, char16_t everywhere"),
QT_TRANSLATE_NOOP("EncodingDialog", "wchar_t on Unix (Ucs4)"),
QT_TRANSLATE_NOOP("EncodingDialog", "QLatin1String")
};
// A read-only line edit with a tool button to copy the contents
class DisplayLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit DisplayLineEdit(const QIcon &icon, QWidget *parent = nullptr);
public slots:
void copyAll();
};
DisplayLineEdit::DisplayLineEdit(const QIcon &icon, QWidget *parent) :
QLineEdit(parent)
{
setReadOnly(true);
#if QT_CONFIG(clipboard) && QT_CONFIG(action)
auto copyAction = addAction(icon, QLineEdit::TrailingPosition);
connect(copyAction, &QAction::triggered, this, &DisplayLineEdit::copyAll);
#endif
}
void DisplayLineEdit::copyAll()
{
#if QT_CONFIG(clipboard)
QGuiApplication::clipboard()->setText(text());
#endif
}
static void addFormLayoutRow(QFormLayout *formLayout, const QString &text,
QWidget *w, const QString &toolTip)
{
auto label = new QLabel(text);
label->setToolTip(toolTip);
w->setToolTip(toolTip);
label->setBuddy(w);
formLayout->addRow(label, w);
}
EncodingDialog::EncodingDialog(QWidget *parent) :
QDialog(parent)
{
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
setWindowTitle(tr("Encodings"));
auto formLayout = new QFormLayout;
auto sourceLineEdit = new QLineEdit(this);
sourceLineEdit->setClearButtonEnabled(true);
connect(sourceLineEdit, &QLineEdit::textChanged, this, &EncodingDialog::textChanged);
addFormLayoutRow(formLayout, tr("&Source:"), sourceLineEdit, tr("Enter text"));
const auto copyIcon = QIcon::fromTheme(QLatin1String("edit-copy"),
QIcon(QLatin1String(":/images/editcopy")));
for (int i = 0; i < EncodingCount; ++i) {
m_lineEdits[i] = new DisplayLineEdit(copyIcon, this);
addFormLayoutRow(formLayout, tr(encodingLabels[i]),
m_lineEdits[i], tr(encodingToolTips[i]));
}
auto mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(formLayout);
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
mainLayout->addWidget(buttonBox);
}
void EncodingDialog::textChanged(const QString &t)
{
if (t.isEmpty()) {
for (auto lineEdit : m_lineEdits)
lineEdit->clear();
} else {
for (int i = 0; i < EncodingCount; ++i)
m_lineEdits[i]->setText(encodedString(t, static_cast<Encoding>(i)));
}
}
#include "encodingdialog.moc"
|