File indexing completed on 2024-09-15 03:43:52
0001 /* 0002 This file is part of the KDE games library 0003 SPDX-FileCopyrightText: 2001 Andreas Beckermann (b_mann@gmx.de) 0004 0005 SPDX-License-Identifier: LGPL-2.0-only 0006 */ 0007 0008 #include "kchatdialog.h" 0009 0010 // own 0011 #include "kfourinline_debug.h" 0012 // KDEGames 0013 #define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API 0014 #include <libkdegamesprivate/kchatbase.h> 0015 // KF 0016 #include <KLocalizedString> 0017 // Qt 0018 #include <QDialogButtonBox> 0019 #include <QFontDialog> 0020 #include <QFrame> 0021 #include <QGridLayout> 0022 #include <QLabel> 0023 #include <QLineEdit> 0024 #include <QPushButton> 0025 0026 class KChatDialogPrivate 0027 { 0028 public: 0029 KChatDialogPrivate() 0030 { 0031 mTextPage = nullptr; 0032 0033 mNamePreview = nullptr; 0034 mTextPreview = nullptr; 0035 mSystemNamePreview = nullptr; 0036 mSystemTextPreview = nullptr; 0037 0038 mChat = nullptr; 0039 } 0040 0041 QFrame *mTextPage; 0042 0043 QLabel *mNamePreview; 0044 QLabel *mTextPreview; 0045 QLabel *mSystemNamePreview; 0046 QLabel *mSystemTextPreview; 0047 0048 QLineEdit *mMaxMessages; 0049 0050 KChatBase *mChat; 0051 }; 0052 0053 KChatDialog::KChatDialog(KChatBase *chat, QWidget *parent, bool modal) 0054 : QDialog(parent) 0055 , d(new KChatDialogPrivate) 0056 { 0057 setModal(modal); 0058 init(); 0059 plugChatWidget(chat); 0060 } 0061 0062 KChatDialog::KChatDialog(QWidget *parent, bool modal) 0063 : QDialog(parent) 0064 , d(new KChatDialogPrivate) 0065 { 0066 setModal(modal); 0067 init(); 0068 } 0069 0070 KChatDialog::~KChatDialog() 0071 { 0072 delete d; 0073 } 0074 0075 void KChatDialog::init() 0076 { 0077 d->mTextPage = new QFrame(this); 0078 QGridLayout *layout = new QGridLayout(d->mTextPage); 0079 0080 setWindowTitle(i18nc("@title:window", "Configure Chat")); 0081 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply); 0082 QWidget *mainWidget = new QWidget(this); 0083 setLayout(layout); 0084 layout->addWidget(mainWidget); 0085 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0086 okButton->setDefault(true); 0087 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0088 connect(buttonBox, &QDialogButtonBox::accepted, this, &KChatDialog::accept); 0089 connect(buttonBox, &QDialogButtonBox::rejected, this, &KChatDialog::reject); 0090 0091 // General fonts 0092 QPushButton *nameFont = new QPushButton(i18n("Name Font..."), d->mTextPage); 0093 connect(nameFont, &QPushButton::pressed, this, &KChatDialog::slotGetNameFont); 0094 layout->addWidget(nameFont, 0, 0); 0095 QPushButton *textFont = new QPushButton(i18n("Text Font..."), d->mTextPage); 0096 connect(textFont, &QPushButton::pressed, this, &KChatDialog::slotGetTextFont); 0097 layout->addWidget(textFont, 0, 1); 0098 0099 QFrame *messagePreview = new QFrame(d->mTextPage); 0100 messagePreview->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken); 0101 QHBoxLayout *messageLayout = new QHBoxLayout(messagePreview); 0102 layout->addWidget(messagePreview, 1, 0, 1, 2); 0103 0104 d->mNamePreview = new QLabel(i18n("Player: "), messagePreview); 0105 messageLayout->addWidget(d->mNamePreview, 0); 0106 d->mTextPreview = new QLabel(i18n("This is a player message"), messagePreview); 0107 messageLayout->addWidget(d->mTextPreview, 1); 0108 0109 layout->addItem(new QSpacerItem(0, 10), 2, 0); 0110 0111 // System Message fonts 0112 QLabel *systemMessages = new QLabel(i18n("System Messages - Messages directly sent from the game"), d->mTextPage); 0113 layout->addWidget(systemMessages, 3, 0, 1, 2); 0114 QPushButton *systemNameFont = new QPushButton(i18n("Name Font..."), d->mTextPage); 0115 connect(systemNameFont, &QPushButton::pressed, this, &KChatDialog::slotGetSystemNameFont); 0116 layout->addWidget(systemNameFont, 4, 0); 0117 QPushButton *systemTextFont = new QPushButton(i18n("Text Font..."), d->mTextPage); 0118 connect(systemTextFont, &QPushButton::pressed, this, &KChatDialog::slotGetSystemTextFont); 0119 layout->addWidget(systemTextFont, 4, 1); 0120 0121 QFrame *systemMessagePreview = new QFrame(d->mTextPage); 0122 systemMessagePreview->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken); 0123 QHBoxLayout *systemMessageLayout = new QHBoxLayout(systemMessagePreview); 0124 layout->addWidget(systemMessagePreview, 5, 0, 1, 2); 0125 0126 d->mSystemNamePreview = new QLabel(i18n("--- Game: "), systemMessagePreview); 0127 systemMessageLayout->addWidget(d->mSystemNamePreview, 0); 0128 d->mSystemTextPreview = new QLabel(i18n("This is a system message"), systemMessagePreview); 0129 systemMessageLayout->addWidget(d->mSystemTextPreview, 1); 0130 0131 // message count 0132 QLabel *maxMessages = new QLabel(i18n("Maximum number of messages (-1 = unlimited):"), d->mTextPage); 0133 layout->addWidget(maxMessages, 6, 0); 0134 d->mMaxMessages = new QLineEdit(d->mTextPage); 0135 d->mMaxMessages->setText(QString::number(-1)); 0136 layout->addWidget(d->mMaxMessages, 6, 1); 0137 0138 layout->addWidget(buttonBox, 7, 0, 1, 2); 0139 connect(buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &KChatDialog::slotApply); 0140 connect(okButton, &QPushButton::clicked, this, &KChatDialog::slotOk); 0141 } 0142 0143 void KChatDialog::slotGetNameFont() 0144 { 0145 QFont font = nameFont(); 0146 bool ok; 0147 font = QFontDialog::getFont(&ok, font, this); 0148 if (ok) 0149 setNameFont(font); 0150 } 0151 0152 void KChatDialog::slotGetTextFont() 0153 { 0154 QFont font = textFont(); 0155 bool ok; 0156 font = QFontDialog::getFont(&ok, font, this); 0157 if (ok) 0158 setTextFont(font); 0159 } 0160 0161 void KChatDialog::slotGetSystemNameFont() 0162 { 0163 QFont font = systemNameFont(); 0164 bool ok; 0165 font = QFontDialog::getFont(&ok, font, this); 0166 if (ok) 0167 setSystemNameFont(font); 0168 } 0169 0170 void KChatDialog::slotGetSystemTextFont() 0171 { 0172 QFont font = systemTextFont(); 0173 bool ok; 0174 font = QFontDialog::getFont(&ok, font, this); 0175 if (ok) 0176 setSystemTextFont(font); 0177 } 0178 0179 QFont KChatDialog::nameFont() const 0180 { 0181 return d->mNamePreview->font(); 0182 } 0183 0184 QFont KChatDialog::textFont() const 0185 { 0186 return d->mTextPreview->font(); 0187 } 0188 0189 QFont KChatDialog::systemNameFont() const 0190 { 0191 return d->mSystemNamePreview->font(); 0192 } 0193 0194 QFont KChatDialog::systemTextFont() const 0195 { 0196 return d->mSystemTextPreview->font(); 0197 } 0198 0199 void KChatDialog::plugChatWidget(KChatBase *widget, bool applyFonts) 0200 { 0201 d->mChat = widget; 0202 if (applyFonts && d->mChat) { 0203 setNameFont(d->mChat->nameFont()); 0204 setTextFont(d->mChat->messageFont()); 0205 setSystemNameFont(d->mChat->systemNameFont()); 0206 setSystemTextFont(d->mChat->systemMessageFont()); 0207 setMaxMessages(d->mChat->maxItems()); 0208 } 0209 } 0210 0211 void KChatDialog::configureChatWidget(KChatBase *widget) 0212 { 0213 if (!widget) { 0214 return; 0215 } 0216 widget->setNameFont(nameFont()); 0217 widget->setMessageFont(textFont()); 0218 0219 widget->setSystemNameFont(systemNameFont()); 0220 widget->setSystemMessageFont(systemTextFont()); 0221 0222 widget->setMaxItems(maxMessages()); 0223 widget->saveConfig(); 0224 qCDebug(KFOURINLINE_LOG) << "Saved configuration"; 0225 } 0226 0227 void KChatDialog::slotOk() 0228 { 0229 slotApply(); 0230 QDialog::accept(); 0231 } 0232 0233 void KChatDialog::slotApply() 0234 { 0235 configureChatWidget(d->mChat); 0236 } 0237 0238 void KChatDialog::setNameFont(const QFont &f) 0239 { 0240 d->mNamePreview->setFont(f); 0241 } 0242 0243 void KChatDialog::setTextFont(const QFont &f) 0244 { 0245 d->mTextPreview->setFont(f); 0246 } 0247 0248 void KChatDialog::setSystemNameFont(const QFont &f) 0249 { 0250 d->mSystemNamePreview->setFont(f); 0251 } 0252 0253 void KChatDialog::setSystemTextFont(const QFont &f) 0254 { 0255 d->mSystemTextPreview->setFont(f); 0256 } 0257 0258 void KChatDialog::setMaxMessages(int max) 0259 { 0260 d->mMaxMessages->setText(QString::number(max)); 0261 } 0262 0263 int KChatDialog::maxMessages() const 0264 { 0265 bool ok; 0266 int max = d->mMaxMessages->text().toInt(&ok); 0267 if (!ok) { 0268 return -1; // unlimited is default 0269 } 0270 return max; 0271 } 0272 0273 #include "moc_kchatdialog.cpp"