File indexing completed on 2024-04-14 05:44:09

0001 /*
0002     SPDX-FileCopyrightText: 2018 Mariusz Glebocki <mglb@arccos-1.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // Own
0008 #include "FontDialog.h"
0009 
0010 // Qt
0011 #include <QBoxLayout>
0012 #include <QWhatsThis>
0013 
0014 // KDE
0015 #include <KLocalizedString>
0016 #include <kwidgetsaddons_version.h>
0017 
0018 using namespace Konsole;
0019 
0020 FontDialog::FontDialog(QWidget *parent, bool emoji, const QFont font)
0021     : QDialog(parent)
0022     , _fontChooser(nullptr)
0023     , _showAllFonts(nullptr)
0024     , _buttonBox(nullptr)
0025     , _emoji(emoji)
0026 {
0027     setWindowTitle(i18nc("@title:window", "Select font"));
0028 
0029     KFontChooser::DisplayFlag onlyFixed = _emoji ? KFontChooser::FixedFontsOnly : KFontChooser::FixedFontsOnly;
0030 
0031     _fontChooser = new KFontChooser(onlyFixed, this);
0032     if (_emoji) {
0033         QStringList list = KFontChooser::createFontList(0).filter(QStringLiteral("emoji"), Qt::CaseInsensitive);
0034         _fontChooser->setFont(font);
0035         _fontChooser->setFontListItems(KFontChooser::createFontList(0).filter(QStringLiteral("emoji"), Qt::CaseInsensitive));
0036         _fontChooser->setFont(font);
0037     }
0038 
0039     _showAllFonts = new QCheckBox(i18nc("@action:button", "Show all fonts"), this);
0040     _showAllFontsWarningButton = new QToolButton(this);
0041     _buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
0042 
0043     if (_emoji) {
0044         _fontChooser->setSampleText(
0045             /* clang-format off */
0046             QStringLiteral(" 🏴🀘🚬🌍🌎🌏πŸ₯†πŸ’£πŸ—‘πŸ”«βš—οΈβš›οΈβ˜’οΈβ˜£οΈπŸŒΏπŸŽ±πŸ§πŸ’‰πŸ’ŠπŸ•΄οΈπŸ“‘πŸ€»πŸ¦‘πŸ‡¦πŸ‡ΆπŸ‘©β€πŸ”¬πŸͺ€πŸš±βœŠπŸΏπŸ”¬πŸ§¬πŸ΄β€β˜ οΈπŸ€½\n"
0047                            "0123456789\n"
0048                            "πŸ‘†πŸ» πŸ‘†πŸΌ πŸ‘†πŸ½ πŸ‘†πŸΎ πŸ‘†πŸΏ     πŸ‘¨β€β€οΈβ€πŸ‘¨ πŸ‘¨β€β€οΈβ€πŸ’‹β€πŸ‘¨ πŸ‘©β€πŸ‘©β€πŸ‘§β€πŸ‘§ πŸ‘©πŸ»β€πŸ€β€πŸ‘¨πŸΏ πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘¦ \U0001F468\u200D\u2764\uFE0F\u200D\U0001F468  \U0001F468\u200D\u2764\u200D\U0001F468\n"
0049                            "πŸ‡§πŸ‡² πŸ‡¨πŸ‡­ πŸ‡¨πŸ‡Ώ πŸ‡ͺπŸ‡Ί πŸ‡¬πŸ‡± πŸ‡²πŸ‡¬ πŸ‡²πŸ‡Ή πŸ‡ΈπŸ‡Ώ πŸ‡ΏπŸ‡²"));
0050         /* clang-format on */
0051         _showAllFonts->hide();
0052         _showAllFontsWarningButton->hide();
0053     } else {
0054         _fontChooser->setSampleText(
0055             QStringLiteral("0OQ 1Il!| 5S 8B rnm :; ,. \"'` ~-= ({[<>]})\n"
0056                            "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\n"
0057                            "ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789\n"
0058                            "abcdefghijklmnopqrstuvwxyz"));
0059         _showAllFontsWarningButton->setIcon(QIcon::fromTheme(QStringLiteral("emblem-warning")));
0060         _showAllFontsWarningButton->setAutoRaise(true);
0061 
0062         connect(_showAllFonts, &QCheckBox::toggled, this, [this](bool enable) {
0063             _fontChooser->setFont(_fontChooser->font(), !enable);
0064         });
0065         connect(_showAllFontsWarningButton, &QToolButton::clicked, this, [this](bool) {
0066             const QString message =
0067                 i18nc("@info:status",
0068                       "By its very nature, a terminal program requires font characters that are equal width (monospace). Any non monospaced "
0069                       "font may cause display issues. This should not be necessary except in rare cases.");
0070             const QPoint pos = QPoint(_showAllFonts->width() / 2, _showAllFonts->height());
0071             QWhatsThis::showText(_showAllFonts->mapToGlobal(pos), message, _showAllFonts);
0072         });
0073     }
0074 
0075     auto *showAllFontsLayout = new QHBoxLayout();
0076     showAllFontsLayout->addWidget(_showAllFonts);
0077     showAllFontsLayout->addWidget(_showAllFontsWarningButton);
0078     showAllFontsLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding));
0079     showAllFontsLayout->setContentsMargins(0, 0, 0, 0);
0080     showAllFontsLayout->setSpacing(0);
0081 
0082     auto *layout = new QVBoxLayout(this);
0083     layout->addWidget(_fontChooser, 1);
0084     if (!_emoji) {
0085         layout->addLayout(showAllFontsLayout);
0086     }
0087     layout->addWidget(_buttonBox);
0088     connect(_fontChooser, &KFontChooser::fontSelected, this, &FontDialog::fontChanged);
0089     connect(_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0090     connect(_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0091 }
0092 
0093 void FontDialog::setFont(const QFont &font)
0094 {
0095     _fontChooser->setFont(font, !_showAllFonts->isChecked() && !_emoji);
0096 }
0097 
0098 #include "moc_FontDialog.cpp"