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
|
/*
SPDX-FileCopyrightText: 2014-2025 Laurent Montel <montel@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "textmessageindicator.h"
#include <QAbstractScrollArea>
#include <QApplication>
#include <QPainter>
#include <QResizeEvent>
#include <QStyle>
#include <QTimer>
using namespace Qt::Literals::StringLiterals;
using namespace TextCustomEditor;
TextMessageIndicator::TextMessageIndicator(QWidget *parent)
: QWidget(parent)
{
setObjectName(u"TextMessageIndicator"_s);
setFocusPolicy(Qt::NoFocus);
QPalette pal = palette();
pal.setColor(QPalette::Active, QPalette::Window, QApplication::palette().highlight().color());
setPalette(pal);
// if the layout is LTR, we can safely place it in the right position
if (layoutDirection() == Qt::LeftToRight) {
move(10, parentWidget()->height() - 10);
}
resize(0, 0);
hide();
}
void TextMessageIndicator::display(const QString &message, const QString &details, Icon icon, int durationMs)
{
if (message.isEmpty()) {
return;
}
// set text
mMessage = message;
mDetails = details;
// reset vars
mLineSpacing = 0;
// load icon (if set)
mSymbol = QPixmap();
const auto iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
if (icon != None) {
switch (icon) {
case Error:
mSymbol = QIcon::fromTheme(u"dialog-error"_s).pixmap(iconSize);
break;
case Warning:
mSymbol = QIcon::fromTheme(u"dialog-warning"_s).pixmap(iconSize);
break;
default:
mSymbol = QIcon::fromTheme(u"dialog-information"_s).pixmap(iconSize);
break;
}
}
computeSizeAndResize();
// show widget and schedule a repaint
show();
update();
// close the message window after given mS
if (durationMs > 0) {
if (!mTimer) {
mTimer = new QTimer(this);
mTimer->setSingleShot(true);
connect(mTimer, &QTimer::timeout, this, &TextMessageIndicator::hide);
}
mTimer->start(durationMs);
} else if (mTimer) {
mTimer->stop();
}
qobject_cast<QAbstractScrollArea *>(parentWidget())->viewport()->installEventFilter(this);
}
QRect TextMessageIndicator::computeTextRect(const QString &message, int extra_width) const
// Return the QRect which embeds the text
{
int charSize = fontMetrics().averageCharWidth();
/* width of the viewport, minus 20 (~ size removed by further resizing),
minus the extra size (usually the icon width), minus (a bit empirical)
twice the mean width of a character to ensure that the bounding box is
really smaller than the container.
*/
const int boundingWidth =
qobject_cast<QAbstractScrollArea *>(parentWidget())->viewport()->width() - 20 - (extra_width > 0 ? 2 + extra_width : 0) - 2 * charSize;
QRect textRect = fontMetrics().boundingRect(0, 0, boundingWidth, 0, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message);
textRect.translate(-textRect.left(), -textRect.top());
textRect.adjust(0, 0, 2, 2);
return textRect;
}
void TextMessageIndicator::computeSizeAndResize()
{
// determine text rectangle
const QRect textRect = computeTextRect(mMessage, mSymbol.width());
int width = textRect.width();
int height = textRect.height();
if (!mDetails.isEmpty()) {
// determine details text rectangle
const QRect detailsRect = computeTextRect(mDetails, mSymbol.width());
width = qMax(width, detailsRect.width());
height += detailsRect.height();
// plus add a ~60% line spacing
mLineSpacing = static_cast<int>(fontMetrics().height() * 0.6);
height += mLineSpacing;
}
// update geometry with icon information
if (!mSymbol.isNull()) {
width += 2 + mSymbol.width();
height = qMax(height, mSymbol.height());
}
// resize widget
resize(QRect(0, 0, width + 10, height + 8).size());
// if the layout is RtL, we can move it to the right place only after we
// know how much size it will take
int posX = parentWidget()->width() - geometry().width() - 20 - 1;
if (layoutDirection() == Qt::RightToLeft) {
posX = 10;
}
move(posX, parentWidget()->height() - geometry().height() - 20);
}
bool TextMessageIndicator::eventFilter(QObject *obj, QEvent *event)
{
/* if the parent object (scroll area) resizes, the message should
resize as well */
if (event->type() == QEvent::Resize) {
auto resizeEvent = static_cast<QResizeEvent *>(event);
if (resizeEvent->oldSize() != resizeEvent->size()) {
computeSizeAndResize();
}
}
// standard event processing
return QObject::eventFilter(obj, event);
}
void TextMessageIndicator::paintEvent(QPaintEvent * /* e */)
{
const QRect textRect = computeTextRect(mMessage, mSymbol.width());
QRect detailsRect;
if (!mDetails.isEmpty()) {
detailsRect = computeTextRect(mDetails, mSymbol.width());
}
int textXOffset = 0;
// add 2 to account for the reduced drawRoundRect later
int textYOffset = (geometry().height() - textRect.height() - detailsRect.height() - mLineSpacing + 2) / 2;
int iconXOffset = 0;
int iconYOffset = !mSymbol.isNull() ? (geometry().height() - mSymbol.height()) / 2 : 0;
int shadowOffset = 1;
if (layoutDirection() == Qt::RightToLeft) {
iconXOffset = 2 + textRect.width();
} else {
textXOffset = 2 + mSymbol.width();
}
// draw background
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(Qt::black);
painter.setBrush(palette().color(QPalette::Window));
painter.translate(0.5, 0.5);
painter.drawRoundedRect(1, 1, width() - 2, height() - 2, 1600 / width(), 1600 / height(), Qt::RelativeSize);
// draw icon if present
if (!mSymbol.isNull()) {
painter.drawPixmap(5 + iconXOffset, iconYOffset, mSymbol, 0, 0, mSymbol.width(), mSymbol.height());
}
const int xStartPoint = 5 + textXOffset;
const int yStartPoint = textYOffset;
const int textDrawingFlags = Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap;
// draw shadow and text
painter.setPen(palette().color(QPalette::Window).darker(115));
painter.drawText(xStartPoint + shadowOffset, yStartPoint + shadowOffset, textRect.width(), textRect.height(), textDrawingFlags, mMessage);
if (!mDetails.isEmpty()) {
painter.drawText(xStartPoint + shadowOffset,
yStartPoint + textRect.height() + mLineSpacing + shadowOffset,
textRect.width(),
detailsRect.height(),
textDrawingFlags,
mDetails);
}
painter.setPen(palette().color(QPalette::WindowText));
painter.drawText(xStartPoint, yStartPoint, textRect.width(), textRect.height(), textDrawingFlags, mMessage);
if (!mDetails.isEmpty()) {
painter.drawText(xStartPoint + shadowOffset,
yStartPoint + textRect.height() + mLineSpacing,
textRect.width(),
detailsRect.height(),
textDrawingFlags,
mDetails);
}
}
void TextMessageIndicator::mousePressEvent(QMouseEvent * /*e*/)
{
if (mTimer) {
mTimer->stop();
}
hide();
}
#include "moc_textmessageindicator.cpp"
|