| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/NetPlay/ClickBlurLabel.h" |
| 5 | |
| 6 | #include <QChar> |
| 7 | #include <QGraphicsBlurEffect> |
| 8 | #include <QLabel> |
| 9 | #include <QMouseEvent> |
| 10 | #include <QStackedWidget> |
| 11 | #include <QString> |
| 12 | #include <QWidget> |
| 13 | |
| 14 | #include "Common/CommonTypes.h" |
| 15 | #include "Common/Random.h" |
| 16 | |
| 17 | ClickBlurLabel::ClickBlurLabel(QWidget* parent) |
| 18 | : QStackedWidget(parent), m_normal_label(new QLabel(this)), m_blurred_label(new QLabel(this)) |
| 19 | { |
| 20 | setCursor(Qt::PointingHandCursor); |
| 21 | |
| 22 | // We use a QStackedWidget with a pre-blurred label instead of applying QGraphicsBlurEffect on |
| 23 | // click, because creating the blur effect on demand can cause a visible delay on lower-end |
| 24 | // hardware. |
| 25 | auto* blur = new QGraphicsBlurEffect(m_blurred_label); |
| 26 | blur->setBlurRadius(7); |
| 27 | m_blurred_label->setGraphicsEffect(blur); |
| 28 | |
| 29 | // We don't want to take up more space than the labels take |
| 30 | setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 31 | |
| 32 | addWidget(m_blurred_label); |
| 33 | addWidget(m_normal_label); |
| 34 | } |
| 35 | |
| 36 | void ClickBlurLabel::setText(const QString& text) |
| 37 | { |
| 38 | if (this->text() == text) |
| 39 | return; |
| 40 | |
| 41 | m_normal_label->setText(text); |
| 42 | m_blurred_label->setText(GenerateBlurredText(text)); |
| 43 | } |
| 44 | |
| 45 | void ClickBlurLabel::mousePressEvent(QMouseEvent* event) |
| 46 | { |
| 47 | int current = currentIndex(); |
| 48 | setCurrentIndex(current == 0 ? 1 : 0); |
| 49 | QWidget::mousePressEvent(event); |
| 50 | } |
| 51 | |
| 52 | QString ClickBlurLabel::GenerateBlurredText(const QString& text) |
| 53 | { |
| 54 | QString blurred_text; |
| 55 | blurred_text.reserve(text.size()); |
| 56 | for (const QChar& c : text) |
| 57 | { |
| 58 | if (c.isLetter()) |
| 59 | blurred_text += QChar((c.isUpper() ? 'A' : 'a') + (Common::Random::GenerateValue<u8>() % 26)); |
| 60 | else if (c.isDigit()) |
| 61 | blurred_text += QChar('0' + (Common::Random::GenerateValue<u8>() % 10)); |
| 62 | else |
| 63 | blurred_text += c; |
| 64 | } |
| 65 | return blurred_text; |
| 66 | } |