File: kchatdialog.cpp

package info (click to toggle)
kfourinline 4:18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,784 kB
  • sloc: cpp: 7,339; sh: 8; makefile: 5
file content (285 lines) | stat: -rw-r--r-- 7,635 bytes parent folder | download
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
    This file is part of the KDE games library
    Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kchatdialog.h"
#include "kfourinline_debug.h"

#define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
#include <libkdegamesprivate/kchatbase.h>

#include <KConfigGroup>
#include <KLocalizedString>

#include <QDialogButtonBox>
#include <QFontDialog>
#include <QFrame>
#include <QGridLayout>
#include <QLabel>
#include <QLayout>
#include <QLineEdit>
#include <QPushButton>

class KChatDialogPrivate
{
 public:
	KChatDialogPrivate()
	{
		mTextPage = 0;

		mNamePreview = 0;
		mTextPreview = 0;
		mSystemNamePreview = 0;
		mSystemTextPreview = 0;

		mChat = 0;
	}

	QFrame* mTextPage;

	QLabel* mNamePreview;
	QLabel* mTextPreview;
	QLabel* mSystemNamePreview;
	QLabel* mSystemTextPreview;

	QLineEdit* mMaxMessages;

	KChatBase* mChat;
};

KChatDialog::KChatDialog(KChatBase* chat, QWidget* parent, bool modal) 
    : QDialog(parent),
      d( new KChatDialogPrivate )
{
 setModal(modal);
 init();
 plugChatWidget(chat);
}

KChatDialog::KChatDialog(QWidget* parent, bool modal) 
    : QDialog(parent),
      d( new KChatDialogPrivate )
{
 setModal(modal);
 init();
}

KChatDialog::~KChatDialog()
{
 delete d;
}

void KChatDialog::init()
{
 d->mTextPage = new QFrame( this );
 QGridLayout* layout = new QGridLayout(d->mTextPage);

 setWindowTitle(i18n("Configure Chat"));
 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::Apply);
 QWidget *mainWidget = new QWidget(this);
 setLayout(layout);
 layout->addWidget(mainWidget);
 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
 okButton->setDefault(true);
 okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
 connect(buttonBox, &QDialogButtonBox::accepted, this, &KChatDialog::accept);
 connect(buttonBox, &QDialogButtonBox::rejected, this, &KChatDialog::reject);

// General fonts
 QPushButton* nameFont = new QPushButton(i18n("Name Font..."), d->mTextPage);
 connect(nameFont, &QPushButton::pressed, this, &KChatDialog::slotGetNameFont);
 layout->addWidget(nameFont, 0, 0);
 QPushButton* textFont = new QPushButton(i18n("Text Font..."), d->mTextPage);
 connect(textFont, &QPushButton::pressed, this, &KChatDialog::slotGetTextFont);
 layout->addWidget(textFont, 0, 1);

 QFrame* messagePreview = new QFrame(d->mTextPage);
 messagePreview->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
 QHBoxLayout* messageLayout = new QHBoxLayout(messagePreview);
 layout->addWidget(messagePreview, 1, 0, 1, 2);

 d->mNamePreview = new QLabel(i18n("Player: "), messagePreview);
 messageLayout->addWidget(d->mNamePreview, 0);
 d->mTextPreview = new QLabel(i18n("This is a player message"), messagePreview);
 messageLayout->addWidget(d->mTextPreview, 1);

 layout->addItem(new QSpacerItem(0, 10), 2, 0);
 
// System Message fonts
 QLabel* systemMessages = new QLabel(i18n("System Messages - Messages directly sent from the game"), d->mTextPage);
 layout->addWidget(systemMessages, 3, 0, 1, 2);
 QPushButton* systemNameFont = new QPushButton(i18n("Name Font..."), d->mTextPage);
 connect(systemNameFont, &QPushButton::pressed, this, &KChatDialog::slotGetSystemNameFont);
 layout->addWidget(systemNameFont, 4, 0);
 QPushButton* systemTextFont = new QPushButton(i18n("Text Font..."), d->mTextPage);
 connect(systemTextFont, &QPushButton::pressed, this, &KChatDialog::slotGetSystemTextFont);
 layout->addWidget(systemTextFont, 4, 1);

 QFrame* systemMessagePreview = new QFrame(d->mTextPage);
 systemMessagePreview->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
 QHBoxLayout* systemMessageLayout = new QHBoxLayout(systemMessagePreview);
 layout->addWidget(systemMessagePreview, 5, 0, 1, 2);
 
 d->mSystemNamePreview = new QLabel(i18n("--- Game: "), systemMessagePreview);
 systemMessageLayout->addWidget(d->mSystemNamePreview, 0);
 d->mSystemTextPreview = new QLabel(i18n("This is a system message"), systemMessagePreview);
 systemMessageLayout->addWidget(d->mSystemTextPreview, 1);

// message count
 QLabel* maxMessages = new QLabel(i18n("Maximum number of messages (-1 = unlimited):"), d->mTextPage);
 layout->addWidget(maxMessages, 6, 0);
 d->mMaxMessages = new QLineEdit(d->mTextPage);
 d->mMaxMessages->setText(QString::number(-1));
 layout->addWidget(d->mMaxMessages, 6, 1);

 layout->addWidget(buttonBox, 7, 0, 1, 2);
 connect(buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &KChatDialog::slotApply);
 connect(okButton, &QPushButton::clicked, this, &KChatDialog::slotOk);
}

void KChatDialog::slotGetNameFont()
{
 QFont font = nameFont();
 bool ok;
 font = QFontDialog::getFont(&ok, font, this);
 if (ok)
    setNameFont(font);
}

void KChatDialog::slotGetTextFont()
{
 QFont font = textFont();
 bool ok;
 font = QFontDialog::getFont(&ok, font, this);
 if (ok)
   setTextFont(font);
}

void KChatDialog::slotGetSystemNameFont()
{
 QFont font = systemNameFont();
 bool ok;
 font = QFontDialog::getFont(&ok, font, this);
 if (ok)
   setSystemNameFont(font);
}

void KChatDialog::slotGetSystemTextFont()
{
 QFont font = systemTextFont();
 bool ok;
 font = QFontDialog::getFont(&ok, font, this);
 if (ok)
   setSystemTextFont(font);
}

QFont KChatDialog::nameFont() const
{
 return d->mNamePreview->font();
}

QFont KChatDialog::textFont() const
{
 return d->mTextPreview->font();
}

QFont KChatDialog::systemNameFont() const
{
 return d->mSystemNamePreview->font();
}

QFont KChatDialog::systemTextFont() const
{
 return d->mSystemTextPreview->font();
}

void KChatDialog::plugChatWidget(KChatBase* widget, bool applyFonts)
{
 d->mChat = widget;
 if (applyFonts && d->mChat) {
	setNameFont(d->mChat->nameFont());
	setTextFont(d->mChat->messageFont());
	setSystemNameFont(d->mChat->systemNameFont());
	setSystemTextFont(d->mChat->systemMessageFont());
	setMaxMessages(d->mChat->maxItems());
 }
}

void KChatDialog::configureChatWidget(KChatBase* widget)
{
 if (!widget) {
	return;
 }
 widget->setNameFont(nameFont());
 widget->setMessageFont(textFont());

 widget->setSystemNameFont(systemNameFont());
 widget->setSystemMessageFont(systemTextFont());
 
 widget->setMaxItems(maxMessages());
 widget->saveConfig();
 qCDebug(KFOURINLINE_LOG) << "Saved configuration";
}

void KChatDialog::slotOk()
{
 slotApply();
 QDialog::accept();
}

void KChatDialog::slotApply()
{
 configureChatWidget(d->mChat);
}

void KChatDialog::setNameFont(QFont f)
{
 d->mNamePreview->setFont(f);
}

void KChatDialog::setTextFont(QFont f)
{
 d->mTextPreview->setFont(f);
}

void KChatDialog::setSystemNameFont(QFont f)
{
 d->mSystemNamePreview->setFont(f);
}

void KChatDialog::setSystemTextFont(QFont f)
{
 d->mSystemTextPreview->setFont(f);
}

void KChatDialog::setMaxMessages(int max)
{
 d->mMaxMessages->setText(QString::number(max));
}

int KChatDialog::maxMessages() const
{
 bool ok;
 int max = d->mMaxMessages->text().toInt(&ok);
 if (!ok) {
	return -1; // unlimited is default
 }
 return max;
}