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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2022-08-01
* Description : Text edit widgets with spellcheck support and edition limitations.
*
* SPDX-FileCopyrightText: 2021-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "dtextedit_p.h"
namespace Digikam
{
DPlainTextEdit::DPlainTextEdit(QWidget* const parent)
: QPlainTextEdit(parent),
d (new Private)
{
d->init(this);
}
DPlainTextEdit::DPlainTextEdit(const QString& contents, QWidget* const parent)
: QPlainTextEdit(parent),
d (new Private)
{
d->init(this);
setPlainText(contents);
}
DPlainTextEdit::DPlainTextEdit(unsigned int lines, QWidget* const parent)
: QPlainTextEdit(parent),
d (new Private)
{
d->lines = lines;
d->init(this);
}
DPlainTextEdit::~DPlainTextEdit()
{
#ifdef HAVE_SONNET
delete d->spellChecker;
#endif
delete d;
}
bool DPlainTextEdit::isClearButtonEnabled() const
{
return d->clearBtnEnable;
}
void DPlainTextEdit::setClearButtonEnabled(bool enable)
{
d->clearBtnEnable = enable;
}
void DPlainTextEdit::setLinesVisible(unsigned int lines)
{
if (lines == 0)
{
return;
}
d->lines = lines;
// Mimic QLineEdit
if (d->lines == 1)
{
setLineWrapMode(QPlainTextEdit::NoWrap);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
verticalScrollBar()->setFixedHeight(0);
}
}
unsigned int DPlainTextEdit::linesVisible() const
{
return d->lines;
}
QString DPlainTextEdit::text() const
{
return toPlainText();
}
void DPlainTextEdit::setText(const QString& text)<--- Shadow argument
{
QString maskedTxt = text;
for (int i = 0 ; i < d->ignoredMask.size() ; ++i)
{
maskedTxt.remove(d->ignoredMask[i]);
}
if (!d->acceptedMask.isEmpty())
{
QString maskedTxt2;
for (int i = 0 ; i < maskedTxt.size() ; ++i)
{
if (d->acceptedMask.contains(maskedTxt[i]))
{
maskedTxt2.append(maskedTxt[i]);
}
}
maskedTxt = maskedTxt2;
}
setPlainText(maskedTxt);
}
QString DPlainTextEdit::ignoredCharacters() const
{
return d->ignoredMask;
}
void DPlainTextEdit::setIgnoredCharacters(const QString& mask)
{
d->ignoredMask = mask;
}
QString DPlainTextEdit::acceptedCharacters() const
{
return d->acceptedMask;
}
void DPlainTextEdit::setAcceptedCharacters(const QString& mask)
{
d->acceptedMask = mask;
}
void DPlainTextEdit::setCurrentLanguage(const QString& lang)
{
#ifdef HAVE_SONNET
if (!lang.isEmpty())
{
d->spellChecker->highlighter()->setAutoDetectLanguageDisabled(true);
d->spellChecker->highlighter()->setCurrentLanguage(lang);
qCDebug(DIGIKAM_WIDGETS_LOG) << "Spell Checker Language:" << currentLanguage();
d->spellChecker->highlighter()->rehighlight();
}
else if (!d->container.defaultLanguage.isEmpty())
{
d->spellChecker->highlighter()->setAutoDetectLanguageDisabled(true);
d->spellChecker->highlighter()->setCurrentLanguage(d->container.defaultLanguage);
qCDebug(DIGIKAM_WIDGETS_LOG) << "Spell Checker Language:" << currentLanguage();
d->spellChecker->highlighter()->rehighlight();
}
else
{
d->spellChecker->highlighter()->setAutoDetectLanguageDisabled(false);
qCDebug(DIGIKAM_WIDGETS_LOG) << "Spell Checker Language auto-detection enabled";
d->spellChecker->highlighter()->rehighlight();
}
#else
Q_UNUSED(lang);
#endif
}
QString DPlainTextEdit::currentLanguage() const
{
#ifdef HAVE_SONNET
return d->spellChecker->highlighter()->currentLanguage();
#else
return QString();
#endif
}
void DPlainTextEdit::keyPressEvent(QKeyEvent* e)
{
if ((d->maxLength > 0) && (text().length() >= d->maxLength))
{
QString txt = e->text();
int key = e->key();
bool delCondition = (key == Qt::Key_Delete) ||
(key == Qt::Key_Backspace) ||
(key == Qt::Key_Cancel);
if (txt.isEmpty() || delCondition)
{
QPlainTextEdit::keyPressEvent(e);
return;
}
}
if (d->lines == 1)
{
int key = e->key();
if ((key == Qt::Key_Return) || (key == Qt::Key_Enter))
{
e->ignore();
Q_EMIT returnPressed();
return;
}
for (int i = 0 ; i < d->ignoredMask.size() ; ++i)
{
if (key == d->ignoredMask[i].unicode())
{
e->ignore();
return;
}
}
for (int i = 0 ; i < d->acceptedMask.size() ; ++i)
{
if (key != d->acceptedMask[i].unicode())
{
e->ignore();
return;
}
}
Q_EMIT textEdited(text());
}
QPlainTextEdit::keyPressEvent(e);
}
void DPlainTextEdit::insertFromMimeData(const QMimeData* source)
{
QMimeData scopy;
if (source->hasHtml())
{
scopy.setHtml(source->html());
}
if (source->hasText())
{
scopy.setText(source->text());
}
if (source->hasUrls())
{
scopy.setUrls(source->urls());
}
if ((d->lines == 1) && source->hasText())
{
QString textToPaste = source->text();
textToPaste.replace(QLatin1String("\n\r"), QLatin1String(" "));
textToPaste.replace(QLatin1Char('\n'), QLatin1Char(' '));
scopy.setText(textToPaste);
}
QString maskedTxt = scopy.text();
for (int i = 0 ; i < d->ignoredMask.size() ; ++i)
{
maskedTxt.remove(d->ignoredMask[i]);
}
scopy.setText(maskedTxt);
if (!d->acceptedMask.isEmpty())
{
QString maskedTxt2;
for (int i = 0 ; i < maskedTxt.size() ; ++i)
{
if (d->acceptedMask.contains(maskedTxt[i]))
{
maskedTxt2.append(maskedTxt[i]);
}
}
scopy.setText(maskedTxt2);
}
if ((d->maxLength > 0) && source->hasText())
{
QString textToPaste = scopy.text();
QString curText = text();
int totalLength = curText.length() + textToPaste.length();
if (curText.length() == d->maxLength)
{
scopy.setText(QString());
}
else if (totalLength > d->maxLength)
{
int numToDelete = totalLength - d->maxLength;
textToPaste = textToPaste.left(textToPaste.length() - numToDelete);
scopy.setText(textToPaste);
}
}
QPlainTextEdit::insertFromMimeData(&scopy);
}
void DPlainTextEdit::setLocalizeSettings(const LocalizeContainer& settings)
{
d->container = settings;
#ifdef HAVE_SONNET
// Automatic disable spellcheck if too many spelling errors are detected.
d->spellChecker->highlighter()->setAutomatic(!d->container.enableSpellCheck);
// Enable spellchecker globally.
d->spellChecker->highlighter()->setActive(d->container.enableSpellCheck);
for (const QString& word : std::as_const(d->container.ignoredWords))
{
d->spellChecker->highlighter()->ignoreWord(word);
}
d->spellChecker->highlighter()->rehighlight();
#endif
}
LocalizeContainer DPlainTextEdit::spellCheckSettings() const
{
return d->container;
}
int DPlainTextEdit::maxLength() const
{
return d->maxLength;
}
int DPlainTextEdit::leftCharacters() const
{
int left = (d->maxLength - toPlainText().length());
return ((left > 0) ? left : 0);
}
void DPlainTextEdit::setMaxLength(int length)
{
d->maxLength = length;
QString curText = text();
if ((d->maxLength > 0) && (curText.length() > d->maxLength))
{
curText = curText.left(d->maxLength);
setText(curText);
}
}
void DPlainTextEdit::slotChanged()
{
if (d->maxLength <= 0)
{
return;
}
QToolTip::showText(mapToGlobal(QPoint(0, (-1)*(height() + 16))),
i18np("%1 character left", "%1 characters left",
maxLength() - text().size()),
this);
}
QSize DPlainTextEdit::sizeHint() const
{
// NOTE: inspired from QLineEdit::sizeHint()
QFontMetrics fm(font());
QSize sizeHint = QPlainTextEdit::sizeHint();
const int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this);
qreal tm = document()->documentMargin();
int h = qMax(fm.height(), qMax(16, iconSize)) * d->lines +
2 * frameWidth() +
2 * tm;
return QSize(sizeHint.width(), h);
}
} // namespace Digikam
|