forked from linuxdeepin/dtkwidget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlabel.cpp
More file actions
472 lines (431 loc) · 15.6 KB
/
dlabel.cpp
File metadata and controls
472 lines (431 loc) · 15.6 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// SPDX-FileCopyrightText: 2019 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "dlabel.h"
#include "private/dlabel_p.h"
#include "dtooltip.h"
#include <DPaletteHelper>
#include <private/qhexstring_p.h>
#include <private/qlabel_p.h>
DWIDGET_BEGIN_NAMESPACE
/*!
@~english
\class Dtk::Widget::DLabel
\inmodule dtkwidget
@brief DLabel a re-implementation QLabel.
DLabel provides a function to display the DLabel at a specified location
DLabel provides a function to change the font color
*/
/*!
@~english
@brief Constructor for DLabel.
\a parent The argument is sent to the QLabel constructor.
*/
DLabel::DLabel(QWidget *parent, Qt::WindowFlags f)
: QLabel(parent, f)
, DObject(*new DLabelPrivate(this))
{
D_D(DLabel);
d->init();
}
/*!
@~english
@brief Constructor for DLabel.
\a text Text message
\a parent Specifying the parent object.
*/
DLabel::DLabel(const QString &text, QWidget *parent)
: QLabel(text, parent)
, DObject(*new DLabelPrivate(this))
{
D_D(DLabel);
d->init();
}
DLabel::~DLabel()
{
}
/*!
@~english
@brief DLabel::setForegroundRole The font color displayed
\a role Font color(QPalette::ColorRole)
*/
void DLabel::setForegroundRole(QPalette::ColorRole role)
{
D_D(DLabel);
d->color = DPalette::NoType;
QLabel::setForegroundRole(role);
}
/*!
@~english
@brief DLabel::setForegroundRole The font color displayed
\a color Font color
*/
void DLabel::setForegroundRole(DPalette::ColorType color)
{
D_D(DLabel);
d->color = color;
}
/*!
@~english
@brief DLabel::setElideMode Set the mode of ellipsis display
\a elideMode Omitted schema enumeration
*/
void DLabel::setElideMode(Qt::TextElideMode elideMode)
{
D_D(DLabel);
if (d->elideMode == elideMode) {
return;
}
d->elideMode = elideMode;
update();
}
/*!
@~english
@brief DLabel::elideMode Gets the pattern of the ellipsis
\return Returns the pattern of ellipses
*/
Qt::TextElideMode DLabel::elideMode() const
{
D_DC(DLabel);
return d->elideMode;
}
/*!
@~english
@brief DLabel::DLabel Constructor function
\a dd Private class member variables
\a parent Parent control
*/
DLabel::DLabel(DLabelPrivate &dd, QWidget *parent)
: QLabel(parent)
, DObject(dd)
{
dd.init();
}
/*!
@~english
@brief DLabel::initPainter Initialization painter
\a painter painter parameter
*/
void DLabel::initPainter(QPainter *painter) const
{
D_DC(DLabel);
QLabel::initPainter(painter);
if (d->color != DPalette::NoType) {
QBrush color = DPaletteHelper::instance()->palette(this).brush(d->color);
painter->setPen(QPen(color.color()));
}
}
/*!
@~english
@brief DLabel::paintEvent
\a event Message event
\sa QLabel::paintEvent()
*/
void DLabel::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QLabelPrivate *d = static_cast<QLabelPrivate*>(d_ptr.data());
QStyle *style = QWidget::style();
QPainter painter(this);
drawFrame(&painter);
QRect cr = contentsRect();
cr.adjust(d->margin, d->margin, -d->margin, -d->margin);
int align = QStyle::visualAlignment(d->isTextLabel ? DLabelPrivate::textDirection(d)
: layoutDirection(), QFlag(d->align));
#if QT_CONFIG(movie)
if (d->movie && !d->movie->currentPixmap().isNull()) {
if (d->scaledcontents)
style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap().scaled(cr.size()));
else
style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap());
}
else
#endif
if (d->isTextLabel) {
QRectF lr = DLabelPrivate::layoutRect(d).toAlignedRect();
QStyleOption opt;
opt.initFrom(this);
if (d->control) {
#ifndef QT_NO_SHORTCUT
const bool underline = (bool)style->styleHint(QStyle::SH_UnderlineShortcut, 0, this, 0);
if (d->shortcutId != 0
&& underline != d->shortcutCursor.charFormat().fontUnderline()) {
QTextCharFormat fmt;
fmt.setFontUnderline(underline);
d->shortcutCursor.mergeCharFormat(fmt);
}
#endif
DLabelPrivate::ensureTextLayouted(d);
QAbstractTextDocumentLayout::PaintContext context;
// Adjust the palette
context.palette = opt.palette;
if (d_func()->color != DPalette::NoType) {
context.palette.setBrush(QPalette::Text, DPaletteHelper::instance()->palette(this).brush(d_func()->color));
} else if (foregroundRole() != QPalette::Text && isEnabled()) {
context.palette.setColor(QPalette::Text, context.palette.color(foregroundRole()));
}
painter.save();
painter.translate(lr.topLeft());
painter.setClipRect(lr.translated(-lr.x(), -lr.y()));
d->control->setPalette(context.palette);
d->control->drawContents(&painter, QRectF(), this);
painter.restore();
} else {
int flags = align | (DLabelPrivate::textDirection(d) == Qt::LeftToRight ? Qt::TextForceLeftToRight
: Qt::TextForceRightToLeft);
if (d->hasShortcut) {
flags |= Qt::TextShowMnemonic;
if (!style->styleHint(QStyle::SH_UnderlineShortcut, &opt, this))
flags |= Qt::TextHideMnemonic;
}
QPalette palette = opt.palette;
if (d_func()->color != DPalette::NoType) {
palette.setBrush(foregroundRole(), DPaletteHelper::instance()->palette(this).brush(d_func()->color));
}
if (d->text != d_func()->lastText) {
// clear flag when text changed, avoid tooltip don't show new text
DToolTip::setShowToolTip(this, false);
d_func()->lastText = d->text;
}
QString text = d->text;
if (elideMode() != Qt::ElideNone) {
const QFontMetrics fm(fontMetrics());
text = fm.elidedText(text, elideMode(), width(), flags);
}
const DToolTip::ToolTipShowMode &toolTipShowMode = DToolTip::toolTipShowMode(this);
if (toolTipShowMode != DToolTip::Default) {
const bool showToolTip = (toolTipShowMode == DToolTip::AlwaysShow)
|| ((toolTipShowMode == DToolTip::ShowWhenElided) && (d->text != text));
if (DToolTip::needUpdateToolTip(this, showToolTip)) {
QString toolTip;
if (showToolTip) {
QTextOption textOption;
textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
textOption.setTextDirection(opt.direction);
textOption.setAlignment(Qt::Alignment(align));
toolTip = DToolTip::wrapToolTipText(d->text, textOption);
}
this->setToolTip(toolTip);
DToolTip::setShowToolTip(this, showToolTip);
}
}
style->drawItemText(&painter, lr.toRect(), flags, palette, isEnabled(), text, foregroundRole());
}
} else
#ifndef QT_NO_PICTURE
if (d->picture) {
QRect br = d->picture->boundingRect();
int rw = br.width();
int rh = br.height();
if (d->scaledcontents) {
painter.save();
painter.translate(cr.x(), cr.y());
painter.scale((double)cr.width()/rw, (double)cr.height()/rh);
painter.drawPicture(-br.x(), -br.y(), *d->picture);
painter.restore();
} else {
int xo = 0;
int yo = 0;
if (align & Qt::AlignVCenter)
yo = (cr.height()-rh)/2;
else if (align & Qt::AlignBottom)
yo = cr.height()-rh;
if (align & Qt::AlignRight)
xo = cr.width()-rw;
else if (align & Qt::AlignHCenter)
xo = (cr.width()-rw)/2;
painter.drawPicture(cr.x()+xo-br.x(), cr.y()+yo-br.y(), *d->picture);
}
} else
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 9, 0)
if (d->pixmap && !d->pixmap->isNull()) {
#else
if (d->icon && !d->icon->isNull()) {
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 9, 0)
QPixmap pix;
if (d->scaledcontents) {
QSize scaledSize = cr.size() * devicePixelRatioF();
if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) {
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
if (!d->cachedimage)
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 2)
d->cachedimage = new QImage(d->pixmap->toImage());
delete d->scaledpixmap;
#else
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
d->cachedimage = QImage(d->pixmap->toImage());
#endif
d->scaledpixmap.reset();
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
QImage scaledImage =
d->cachedimage->scaled(scaledSize,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
#else
d->scaledpixmap = d->pixmap->scaled(scaledSize,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 2)
d->scaledpixmap = new QPixmap(QPixmap::fromImage(scaledImage));
#else
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
d->scaledpixmap = QPixmap(QPixmap::fromImage(scaledImage));
#endif
#endif
d->scaledpixmap->setDevicePixelRatio(devicePixelRatioF());
}
pix = *d->scaledpixmap;
} else
pix = *d->pixmap;
QStyleOption opt;
opt.initFrom(this);
if (!isEnabled())
pix = style->generatedIconPixmap(QIcon::Disabled, pix, &opt);
style->drawItemPixmap(&painter, cr, align, pix);
#else
// pick up from
// https://github.com/qt/qtbase/blob/25986746947798e1a22d0830d3bcb11a55fcd3ae/src/widgets/widgets/qlabel.cpp#L1052
const qreal dpr = devicePixelRatio();
const QSize size = d->scaledcontents ? cr.size() : d->pixmapSize;
const auto mode = isEnabled() ? QIcon::Normal : QIcon::Disabled;
QPixmap pix = d->icon->pixmap(size, dpr, mode);
if (d->scaledcontents && pix.size() != size * dpr) {
using namespace Qt::StringLiterals;
const QString key =
"qt_label_"_L1 % HexString<quint64>(pix.cacheKey()) %
HexString<quint8>(mode) % HexString<uint>(size.width()) %
HexString<uint>(size.height()) %
HexString<quint16>(qRound(dpr * 1000));
if (!QPixmapCache::find(key, &pix)) {
pix = pix.scaled(size * dpr, Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
pix.setDevicePixelRatio(dpr);
// using QIcon to cache the newly create pixmap is not possible
// because QIcon does not clear this cache (so we grow indefinitely)
// and also uses the newly added pixmap as starting point for new
// scaled pixmap which makes it very blurry.
// Therefore use QPixmapCache here.
QPixmapCache::insert(key, pix);
}
}
QStyleOption opt;
opt.initFrom(this);
style->drawItemPixmap(&painter, cr, align, pix);
#endif
}
}
DLabelPrivate::DLabelPrivate(DLabel *q)
: DObjectPrivate(q)
{
}
void DLabelPrivate::init()
{
}
Qt::LayoutDirection DLabelPrivate::textDirection(QLabelPrivate *d)
{
if (d->control) {
QTextOption opt = d->control->document()->defaultTextOption();
return opt.textDirection();
}
return d->text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight;
}
QRectF DLabelPrivate::documentRect(QLabelPrivate *d)
{
QLabel *q = qobject_cast<QLabel*>(d->q_ptr);
Q_ASSERT_X(d->isTextLabel, "documentRect", "document rect called for label that is not a text label!");
QRect cr = q->contentsRect();
cr.adjust(d->margin, d->margin, -d->margin, -d->margin);
const int align = QStyle::visualAlignment(d->isTextLabel ? textDirection(d)
: q->layoutDirection(), QFlag(d->align));
int m = d->indent;
if (m < 0 && q->frameWidth()) // no indent, but we do have a frame
m = q->fontMetrics().horizontalAdvance(QLatin1Char('x')) / 2 - d->margin;
if (m > 0) {
if (align & Qt::AlignLeft)
cr.setLeft(cr.left() + m);
if (align & Qt::AlignRight)
cr.setRight(cr.right() - m);
if (align & Qt::AlignTop)
cr.setTop(cr.top() + m);
if (align & Qt::AlignBottom)
cr.setBottom(cr.bottom() - m);
}
return cr;
}
QRectF DLabelPrivate::layoutRect(QLabelPrivate *d)
{
QRectF cr = documentRect(d);
if (!d->control)
return cr;
ensureTextLayouted(d);
// Caculate y position manually
qreal rh = d->control->document()->documentLayout()->documentSize().height();
qreal yo = 0;
if (d->align & Qt::AlignVCenter)
yo = qMax((cr.height()-rh)/2, qreal(0));
else if (d->align & Qt::AlignBottom)
yo = qMax(cr.height()-rh, qreal(0));
return QRectF(cr.x(), yo + cr.y(), cr.width(), cr.height());
}
void DLabelPrivate::ensureTextLayouted(QLabelPrivate *d)
{
if (d->textLayoutDirty) {
if (d->textDirty) {
if (d->control) {
QTextDocument *doc = d->control->document();
if (d->textDirty) {
#ifndef QT_NO_TEXTHTMLPARSER
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
if (d->textformat == Qt::TextFormat::RichText)
#else
if (d->isRichText)
#endif
doc->setHtml(d->text);
else
doc->setPlainText(d->text);
#else
doc->setPlainText(d->text);
#endif
doc->setUndoRedoEnabled(false);
#ifndef QT_NO_SHORTCUT
if (d->hasShortcut) {
// Underline the first character that follows an ampersand (and remove the others ampersands)
int from = 0;
bool found = false;
QTextCursor cursor;
while (!(cursor = d->control->document()->find((QLatin1String("&")), from)).isNull()) {
cursor.deleteChar(); // remove the ampersand
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
from = cursor.position();
if (!found && cursor.selectedText() != QLatin1String("&")) { //not a second &
found = true;
d->shortcutCursor = cursor;
}
}
}
#endif
}
}
d->textDirty = false;
}
if (d->control) {
QTextDocument *doc = d->control->document();
QTextOption opt = doc->defaultTextOption();
opt.setAlignment(QFlag(d->align));
if (d->align & Qt::TextWordWrap)
opt.setWrapMode(QTextOption::WordWrap);
else
opt.setWrapMode(QTextOption::ManualWrap);
doc->setDefaultTextOption(opt);
QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
fmt.setMargin(0);
doc->rootFrame()->setFrameFormat(fmt);
doc->setTextWidth(DLabelPrivate::documentRect(d).width());
}
d->textLayoutDirty = false;
}
}
DWIDGET_END_NAMESPACE