| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "draglabel.h" |
| 5 |
|
| 6 |
#include <QtWidgets> |
| 7 |
|
| 8 |
//! [0] |
| 9 |
DragLabel::DragLabel(const QString &text, QWidget *parent) |
| 10 |
: QLabel(parent) |
| 11 |
{ |
| 12 |
QFontMetrics metric(font()); |
| 13 |
QSize size = metric.size(Qt::TextSingleLine, text); |
| 14 |
|
| 15 |
QImage image(size.width() + 12, size.height() + 12, QImage::Format_ARGB32_Premultiplied); |
| 16 |
image.fill(qRgba(0, 0, 0, 0)); |
| 17 |
|
| 18 |
QFont font; |
| 19 |
font.setStyleStrategy(QFont::ForceOutline); |
| 20 |
//! [0] |
| 21 |
|
| 22 |
//! [1] |
| 23 |
QLinearGradient gradient(0, 0, 0, image.height()-1); |
| 24 |
gradient.setColorAt(0.0, Qt::white); |
| 25 |
gradient.setColorAt(0.2, QColor(200, 200, 255)); |
| 26 |
gradient.setColorAt(0.8, QColor(200, 200, 255)); |
| 27 |
gradient.setColorAt(1.0, QColor(127, 127, 200)); |
| 28 |
|
| 29 |
QPainter painter; |
| 30 |
painter.begin(&image); |
| 31 |
painter.setRenderHint(QPainter::Antialiasing); |
| 32 |
painter.setBrush(gradient); |
| 33 |
painter.drawRoundedRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1), |
| 34 |
25, 25, Qt::RelativeSize); |
| 35 |
|
| 36 |
painter.setFont(font); |
| 37 |
painter.setBrush(Qt::black); |
| 38 |
painter.drawText(QRect(QPoint(6, 6), size), Qt::AlignCenter, text); |
| 39 |
painter.end(); |
| 40 |
//! [1] |
| 41 |
|
| 42 |
//! [2] |
| 43 |
setPixmap(QPixmap::fromImage(image)); |
| 44 |
m_labelText = text; |
| 45 |
} |
| 46 |
//! [2] |
| 47 |
|
| 48 |
QString DragLabel::labelText() const |
| 49 |
{ |
| 50 |
return m_labelText; |
| 51 |
} |