/
editstringcell.cpp
297 lines (248 loc) · 10.2 KB
/
editstringcell.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
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
287
288
289
290
291
292
293
294
295
296
297
#include "editstringcell.h"
#include "tblstructure.h"
#include "editcolorsdialog.h"
#include "qtcompat.h"
#include <QMenu>
#include <QStack>
#include <QQueue>
#include <QTextCodec>
#include <utility>
// convenience function
QString colorHexString(const QColor &c)
{
QChar zeroChar = QChar('0');
return QString("#%1%2%3").arg(c.red(), 2, 16, zeroChar).arg(c.green(), 2, 16, zeroChar).arg(c.blue(), 2, 16, zeroChar);
}
extern QList<QChar> colorCodes;
extern QStringList colorStrings;
extern QList<QColor> colors;
extern int colorsNum;
QString colorsRegexPattern()
{
QStringList realColorStrings = colorStrings.mid(1);
for (int i = 0; i < realColorStrings.size(); ++i)
realColorStrings[i] = qtcompat::regexEscape(realColorStrings[i]);
return QString("(?:%1)").arg(realColorStrings.join(QChar('|')));
}
static const QString kGenderNumberMenuName("GenderNumberMenu");
EditStringCell::EditStringCell(QWidget *parent, const KeyValueItemsPair &keyValueItemsPairToEdit, bool renderGreyAsWhite) : QWidget(parent), _renderGreyAsWhite(renderGreyAsWhite)
{
ui.setupUi(this);
static const QStringList kGenderAbbreviations = QStringList() << "[ms]" << "[fs]" << "[ns]" << "[nl]" << "[pl]" << "[mp]" << "[fp]";
static const QStringList kGenders = QStringList() << tr("Masculine singular") << tr("Feminine singular") << tr("Neutral singular") << tr("Neutral") << tr("Plural") << tr("Masculine plural") << tr("Feminine plural");
QMenu *genderNumberMenu = new QMenu(ui.genderNumberButton);
genderNumberMenu->setObjectName(kGenderNumberMenuName);
for (int i = 0; i < kGenderAbbreviations.size(); i++)
genderNumberMenu->addAction(QString("%1 %2").arg(kGenderAbbreviations.at(i), kGenders.at(i)), this, SLOT(insertText()));
ui.genderNumberButton->setMenu(genderNumberMenu);
_colorMenu = new QMenu(ui.colorButton);
_colorMenu->setObjectName("ColorMenu");
updateColorsMenu();
ui.colorButton->setMenu(_colorMenu);
ui.stringPreview->viewport()->setStyleSheet("background-color: black;");
connect(ui.wrapCheckBox, SIGNAL(toggled(bool)), SLOT(wrapModeChanged(bool)));
connect(ui.keyLineEdit, SIGNAL(textChanged(QString)), SLOT(calculateKeyHashValue()));
connect(ui.stringEdit, SIGNAL(textChanged()), SLOT(updateCharsEditCounter()));
connect(ui.stringEdit, SIGNAL(textChanged()), SLOT(setPreviewText()));
connect(ui.stringEdit, SIGNAL(cursorPositionChanged()), SLOT(updateCurrentEditColumn()));
connect(ui.reversePreviewTextCheckBox, SIGNAL(toggled(bool)), SLOT(setPreviewText()));
connect(ui.centerPreviewTextCheckBox, SIGNAL(toggled(bool)), SLOT(setPreviewText()));
setItem(keyValueItemsPairToEdit);
}
void EditStringCell::wrapModeChanged(bool state)
{
ui.stringEdit ->setLineWrapMode(state ? QPlainTextEdit::WidgetWidth : QPlainTextEdit::NoWrap);
ui.stringPreview->setLineWrapMode(state ? QTextEdit::WidgetWidth : QTextEdit::NoWrap);
}
void EditStringCell::insertText()
{
QAction *menuItem = qobject_cast<QAction *>(sender());
if (menuItem->parentWidget()->objectName() == kGenderNumberMenuName)
ui.stringEdit->insertPlainText(menuItem->text().left(4));
else
ui.stringEdit->insertPlainText(menuItem->text());
}
void EditStringCell::saveChanges()
{
_keyValueItemsPair.first->setText(ui.keyLineEdit->text());
_keyValueItemsPair.second->setText(ui.stringEdit->toPlainText());
}
void EditStringCell::changeItem(bool toNext)
{
int newItemIndexOffset = toNext ? 1 : -1, row = _keyValueItemsPair.first->row();
QTableWidget *w = _keyValueItemsPair.first->tableWidget();
setItem(KeyValueItemsPair(w->item(row + newItemIndexOffset, 0), w->item(row + newItemIndexOffset, 1)));
}
void EditStringCell::setItem(KeyValueItemsPair newKeyValueItemsPair)
{
_keyValueItemsPair = newKeyValueItemsPair;
resetText();
}
void EditStringCell::resetText()
{
ui.keyLineEdit->setText(_keyValueItemsPair.first->text());
ui.stringEdit->setPlainText(_keyValueItemsPair.second->text());
updateCharsEditCounter();
}
void EditStringCell::calculateKeyHashValue()
{
ui.hashValueLabel->setText(QString("0x%1").arg(TblStructure::hashValue(TblStructure::encodeKey(ui.keyLineEdit->text()).data(), _keyValueItemsPair.first->tableWidget()->rowCount()), 0, 16));
}
void EditStringCell::setPreviewText()
{
ui.stringPreview->clear();
// stupid HTML
QString text = ui.stringEdit->toPlainText().replace('<', "<").replace('>', ">");
const QString nonBlankSpace(" ");
#if IS_QT5
QRegularExpression emptyRe(" {2,}");
do
{
QRegularExpressionMatch match = emptyRe.match(text);
if (!match.hasMatch())
break;
int matchedLength = match.capturedLength();
text.replace(match.capturedStart(), matchedLength, nonBlankSpace.repeated(matchedLength));
} while (true);
#else
int emptyMatchIndex;
QRegExp emptyRe(" {2,}");
while ((emptyMatchIndex = emptyRe.indexIn(text)) != -1)
{
int matchedLength = emptyRe.matchedLength();
text.replace(emptyMatchIndex, matchedLength, nonBlankSpace.repeated(matchedLength));
}
#endif
bool shouldCenterAlign = ui.centerPreviewTextCheckBox->isChecked();
const QString defaultColor = colorStrings.at(1); // text is white by default
if (ui.reversePreviewTextCheckBox->isChecked())
{
// text and colors flow top-to-bottom, but D2 renders text bottom-to-top
// HTML renders top-to-bottom
#if IS_QT5
static QRegularExpression colorRegex(colorsRegexPattern()); // one of the colors
#else
static QRegExp colorRegex(colorsRegexPattern()); // one of the colors
#endif
QString currentGlobalColor = defaultColor;
QStringList lines = text.split('\n');
typedef std::pair<QString, QString> ColoredText; // first - text, second - color
typedef QQueue<ColoredText> LineColoredQueue;
QStack<LineColoredQueue> lineStack;
lineStack.reserve(lines.size());
foreach (const QString &line, lines)
{
LineColoredQueue lineQueue;
int nextColorStart = 0, previousColorEnd = 0;
do
{
#if IS_QT5
QRegularExpressionMatch match = colorRegex.match(line, nextColorStart);
nextColorStart = match.capturedStart();
#else
nextColorStart = colorRegex.indexIn(line, nextColorStart);
#endif
int textLength = nextColorStart;
if (textLength != -1)
textLength -= previousColorEnd;
lineQueue.enqueue(ColoredText(line.mid(previousColorEnd, textLength), currentGlobalColor));
if (nextColorStart == -1)
break;
#if IS_QT5
currentGlobalColor = match.captured();
nextColorStart += match.capturedLength();
#else
currentGlobalColor = colorRegex.cap();
nextColorStart += colorRegex.matchedLength();
#endif
previousColorEnd = nextColorStart;
} while (true);
lineStack.push(lineQueue);
}
QStringList htmlLines;
while (!lineStack.isEmpty())
{
QString htmlLine;
LineColoredQueue queue = lineStack.pop();
while (!queue.isEmpty())
{
ColoredText pair = queue.dequeue();
htmlLine += textWithHtmlColor(pair.first, pair.second);
}
htmlLines += htmlLine;
}
QString html = htmlLines.join("<br>");
if (shouldCenterAlign)
html = QString("<body align=\"center\">%1</body>").arg(html);
ui.stringPreview->setHtml(html);
}
else
{
for (int i = 0; i < colors.size(); i++) // replace color codes with their hex values for HTML
text.replace(colorStrings.at(i + 1), QString("</font><font color = \"%1\">").arg(colorHexString(colorAt(i))));
ui.stringPreview->setHtml(QString("<body style='color: %1' %3>%2</body>").arg(colorHexFromColorString(defaultColor), text.replace('\n', "<br>"),
shouldCenterAlign ? QLatin1String("align=\"center\"") : QString()));
}
int length = ui.stringPreview->toPlainText().length();
ui.charsPreviewCountLabel->setText(QString::number(length));
// show warning only once
static bool showWarning = true;
if (showWarning)
{
showWarning = false;
const int kMaxLengthPatch110 = 255;
if (length > kMaxLengthPatch110)
emit maxLengthExceededFor110(tr("Patch 1.10 has limitation of %1 characters per string").arg(kMaxLengthPatch110));
}
}
void EditStringCell::showEditColorsDialog()
{
EditColorsDialog dlg(this, colorStrings, colorCodes, colors);
if (dlg.exec())
{
// remove all custom colors that were before
for (int i = colorsNum, n = colors.size(); i < n; i++)
{
colorCodes.removeLast();
colorStrings.removeLast();
colors.removeLast();
}
foreach (const EditColorsDialog::ColorInfo &ci, dlg.colorsInfo())
{
colorCodes.append(ci.Code);
colorStrings.append(ci.Name);
colors.append(ci.Color);
}
updateColorsMenu();
emit colorMenuChanged();
}
}
void EditStringCell::updateColorsMenu()
{
_colorMenu->clear();
QPixmap pix(24, 24);
for (int i = 0; i < colors.size(); i++)
{
pix.fill(colorAt(i));
if (i == colorsNum)
_colorMenu->addSeparator();
_colorMenu->addAction(QIcon(pix), colorStrings.at(i + 1), this, SLOT(insertText()))->setIconVisibleInMenu(true);
}
_colorMenu->addSeparator();
_colorMenu->addAction(tr("Edit..."), this, SLOT(showEditColorsDialog()));
setPreviewText();
}
QString EditStringCell::colorHexFromColorString(const QString &colorString)
{
return colorHexString(colorAt(colorStrings.indexOf(colorString) - 1));
}
QString EditStringCell::textWithHtmlColor(const QString &text, const QString &colorString)
{
return QString("<font color = \"%1\">%2</font>").arg(colorHexFromColorString(colorString), text);
}
QColor EditStringCell::colorAt(int i)
{
if (i == 1 && _renderGreyAsWhite)
i = 0;
return colors.at(i);
}