File indexing completed on 2024-12-22 04:45:35

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "administratorcustomemojicreateorupdatewidget.h"
0008 #include <KLineEditEventHandler>
0009 #include <QFormLayout>
0010 #include <QLabel>
0011 #include <QLineEdit>
0012 #include <QScreen>
0013 
0014 #include <KColorScheme>
0015 #include <KLocalizedString>
0016 #include <KStatefulBrush>
0017 #include <KUrlRequester>
0018 
0019 AdministratorCustomEmojiCreateOrUpdateWidget::AdministratorCustomEmojiCreateOrUpdateWidget(QWidget *parent)
0020     : QWidget(parent)
0021     , mName(new QLineEdit(this))
0022     , mAlias(new QLineEdit(this))
0023     , mSelectFile(new KUrlRequester(this))
0024     , mWarningLabel(new QLabel(i18n("The custom emoji name and their aliases should be different."), this))
0025     , mIconLabel(new QLabel(this))
0026 {
0027     mWarningLabel->setObjectName(QStringLiteral("mWarningLabel"));
0028     const KStatefulBrush bgBrush(KColorScheme::View, KColorScheme::NegativeText);
0029     const QColor color = bgBrush.brush(palette()).color();
0030 
0031     QPalette pal = mWarningLabel->palette();
0032     pal.setColor(QPalette::WindowText, color);
0033     mWarningLabel->setPalette(pal);
0034     mWarningLabel->hide();
0035 
0036     auto mainLayout = new QFormLayout(this);
0037     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0038     mainLayout->setContentsMargins({});
0039 
0040     mIconLabel->setObjectName(QStringLiteral("mIconLabel"));
0041 
0042     mName->setObjectName(QStringLiteral("mName"));
0043     mName->setClearButtonEnabled(true);
0044     mAlias->setObjectName(QStringLiteral("mAlias"));
0045     mAlias->setClearButtonEnabled(true);
0046     mSelectFile->setObjectName(QStringLiteral("mSelectFile"));
0047     KLineEditEventHandler::catchReturnKey(mName);
0048     KLineEditEventHandler::catchReturnKey(mAlias);
0049     mainLayout->addRow(i18n("Name:"), mName);
0050     mainLayout->addRow(i18n("Alias:"), mAlias);
0051     mainLayout->addRow(i18n("File:"), mSelectFile);
0052     mainLayout->addWidget(mIconLabel);
0053     mainLayout->addWidget(mWarningLabel);
0054     connect(mName, &QLineEdit::textChanged, this, &AdministratorCustomEmojiCreateOrUpdateWidget::slotUpdateOkButton);
0055     connect(mAlias, &QLineEdit::textChanged, this, &AdministratorCustomEmojiCreateOrUpdateWidget::slotUpdateOkButton);
0056     connect(mSelectFile, &KUrlRequester::urlSelected, this, &AdministratorCustomEmojiCreateOrUpdateWidget::slotAddNewEmoji);
0057     connect(mSelectFile, &KUrlRequester::textChanged, this, &AdministratorCustomEmojiCreateOrUpdateWidget::slotUpdateOkButton);
0058 }
0059 
0060 AdministratorCustomEmojiCreateOrUpdateWidget::~AdministratorCustomEmojiCreateOrUpdateWidget() = default;
0061 
0062 void AdministratorCustomEmojiCreateOrUpdateWidget::setCustomEmojiInfo(const CustomEmojiCreateInfo &info)
0063 {
0064     mName->setText(info.name);
0065     mAlias->setText(info.alias);
0066     const QSize pixmapAvatarSize = QSize(60, 60) * screen()->devicePixelRatio();
0067     mIconLabel->setPixmap(info.icon.pixmap(pixmapAvatarSize));
0068     // TODO url ???
0069     slotUpdateOkButton();
0070 }
0071 
0072 AdministratorCustomEmojiCreateOrUpdateWidget::CustomEmojiCreateInfo AdministratorCustomEmojiCreateOrUpdateWidget::info() const
0073 {
0074     AdministratorCustomEmojiCreateOrUpdateWidget::CustomEmojiCreateInfo info;
0075     info.name = mName->text().trimmed();
0076     info.alias = mAlias->text().trimmed();
0077     info.fileNameUrl = mSelectFile->url();
0078     return info;
0079 }
0080 
0081 void AdministratorCustomEmojiCreateOrUpdateWidget::slotAddNewEmoji()
0082 {
0083     const QSize pixmapAvatarSize = QSize(60, 60) * screen()->devicePixelRatio();
0084     mIconLabel->setPixmap(QIcon(mSelectFile->url().toLocalFile()).pixmap(pixmapAvatarSize));
0085 
0086     slotUpdateOkButton();
0087 }
0088 
0089 void AdministratorCustomEmojiCreateOrUpdateWidget::slotUpdateOkButton()
0090 {
0091     const QString nameTrimmed{mName->text().trimmed()};
0092     const QString aliasTrimmed{mAlias->text().trimmed()};
0093     if (nameTrimmed.isEmpty() && aliasTrimmed.isEmpty()) {
0094         Q_EMIT updateOkButton(false);
0095         return;
0096     }
0097     if (nameTrimmed != aliasTrimmed) {
0098         mWarningLabel->hide();
0099         switch (mType) {
0100         case Create:
0101             Q_EMIT updateOkButton(!nameTrimmed.isEmpty() && mSelectFile->url().isValid());
0102             break;
0103         case Update:
0104             Q_EMIT updateOkButton(!nameTrimmed.isEmpty());
0105             break;
0106         }
0107     } else {
0108         mWarningLabel->show();
0109         Q_EMIT updateOkButton(false);
0110     }
0111 }
0112 
0113 AdministratorCustomEmojiCreateOrUpdateWidget::AdministratorCustomEmojiCreateOrUpdateType AdministratorCustomEmojiCreateOrUpdateWidget::type() const
0114 {
0115     return mType;
0116 }
0117 
0118 void AdministratorCustomEmojiCreateOrUpdateWidget::setType(AdministratorCustomEmojiCreateOrUpdateType newType)
0119 {
0120     mType = newType;
0121 }
0122 
0123 QDebug operator<<(QDebug d, const AdministratorCustomEmojiCreateOrUpdateWidget::CustomEmojiCreateInfo &t)
0124 {
0125     d << "alias " << t.alias;
0126     d << "name " << t.name;
0127     d << "fileNameUrl " << t.fileNameUrl;
0128     return d;
0129 }
0130 
0131 #include "moc_administratorcustomemojicreateorupdatewidget.cpp"