summaryrefslogtreecommitdiff
path: root/src/frontend/settingsdialogs/cfontchooser.cpp
blob: 4fa88c4ed0947bb2d70bbb960a4d55cea9e98808 (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "cfontchooser.h"
#include "clistwidget.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QListWidget>
#include <QFrame>
#include <QFontDatabase>
#include <QListWidgetItem>
#include <QWebSettings>
#include <QFrame>
#include <QWebView>


// ***********************
// Container for KHTHMView to control its size
class WebViewerWidget : public QWidget 
{
	public:
		WebViewerWidget(QWidget* parent = 0);
		~WebViewerWidget();
		virtual QSize sizeHint () const;
};

WebViewerWidget::WebViewerWidget(QWidget* parent)
	: QWidget(parent)
{
}

WebViewerWidget::~WebViewerWidget() 
{
}

QSize WebViewerWidget::sizeHint () const
{
	return QSize(100,100);
}
// ************************



CFontChooser::CFontChooser(QWidget* parent)
 :  QFrame(parent), m_fontWidget(0),
	m_fontListWidget(0), m_styleListWidget(0), m_sizeListWidget(0)
{
	setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
	createLayout();
	connectListWidgets();
	loadFonts();
	setFrameStyle(QFrame::Box);
	setFrameShadow(QFrame::Raised);
}


CFontChooser::~CFontChooser() 
{
}


void CFontChooser::createFontAreaLayout() 
{
	QHBoxLayout* fontStyleSizeHBoxLayout = new QHBoxLayout();

	// font column
	QVBoxLayout* fontLayout = new QVBoxLayout();
	fontStyleSizeHBoxLayout->addLayout(fontLayout);

	QLabel* fontLabel = new QLabel(tr("Font name:"));
	fontLayout->addWidget(fontLabel);

	m_fontListWidget = new CListWidget();
	m_fontListWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
	m_fontListWidget->setMinimumHeight(50);
	fontLayout->addWidget(m_fontListWidget);

	// style column
	QVBoxLayout* styleLayout = new QVBoxLayout();
	fontStyleSizeHBoxLayout->addLayout(styleLayout);

	QLabel* styleLabel = new QLabel(tr("Font style:"));
	styleLayout->addWidget(styleLabel);

	m_styleListWidget = new CListWidget();
	m_styleListWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
	m_styleListWidget->setMinimumHeight(50);
	m_styleListWidget->setCharWidth(12);
	styleLayout->addWidget(m_styleListWidget);

	// size column
	QVBoxLayout* sizeLayout = new QVBoxLayout();
	fontStyleSizeHBoxLayout->addLayout(sizeLayout);

	QLabel* sizeLabel = new QLabel(tr("Size:"));
	sizeLayout->addWidget(sizeLabel);

	m_sizeListWidget = new CListWidget();
	m_sizeListWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
	m_sizeListWidget->setMinimumHeight(50);
	m_sizeListWidget->setCharWidth(5);
	sizeLayout->addWidget(m_sizeListWidget);

	m_vBoxLayout->addLayout(fontStyleSizeHBoxLayout);
}


void CFontChooser::createLayout()
{
	m_vBoxLayout = new QVBoxLayout(this);
	createFontAreaLayout();
	createTextAreaLayout();
}


void CFontChooser::createTextAreaLayout() 
{
	QWidget* webViewWidget = new WebViewerWidget(this);
	QLayout* webViewLayout = new QVBoxLayout(webViewWidget);

	webViewWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
	
	m_webView = new QWebView(webViewWidget);
	webViewLayout->addWidget(m_webView);
	m_vBoxLayout->addWidget(webViewWidget);
}


void CFontChooser::connectListWidgets() 
{

	bool ok = connect(
		m_fontListWidget,
		SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
		this,
		SLOT(fontChanged(QListWidgetItem *, QListWidgetItem *)));
	Q_ASSERT(ok);

	ok = connect(
		m_styleListWidget,
		SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
		this,
		SLOT(styleChanged(QListWidgetItem *, QListWidgetItem *)));
	Q_ASSERT(ok);

	ok = connect(
		m_sizeListWidget,
		SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
		this,
		SLOT(sizeChanged(QListWidgetItem *, QListWidgetItem *)));
	Q_ASSERT(ok);
}


void CFontChooser::fontChanged(QListWidgetItem* current, QListWidgetItem* /*previous*/) 
{
	if (current == 0)
		return;
	QString fontFamily = current->text();
	m_font.setFamily(fontFamily);
	loadStyles(fontFamily);
	outputHtmlText();
	emit fontSelected(m_font);
}


QString CFontChooser::formatAsHtml(const QString& text) 
{

	QString htmlText;
    htmlText.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    htmlText.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
    htmlText.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
    htmlText.append("<head>\n");
    htmlText.append("    <style type=\"text/css\">\n");
    htmlText.append("        *[lang=en] { font-family:#FONT-FAMILY#; font-size:#FONT-SIZE#pt; font-weight:#FONT-WEIGHT#; font-style:#FONT-STYLE#; }\n");
    htmlText.append("    </style>\n");
    htmlText.append("</head>\n");
    htmlText.append("<body>\n");
    htmlText.append("    <div>\n");
    htmlText.append("        <div style=\"display: inline;\" lang=\"en\">\n");
	htmlText.append(text);
    htmlText.append("        </div>\n");
    htmlText.append("</body>\n");
    htmlText.append("</html>\n");
	return htmlText;
}


void CFontChooser::loadFonts() 
{
	m_fontListWidget->clear();
	QFontDatabase database;
	foreach (QString font, database.families()) {
		m_fontListWidget->addItem(font);
	}
	// This triggers loading the styles for the first font
	m_fontListWidget->setCurrentRow(0);
}


void CFontChooser::loadStyles(const QString& font) 
{
	m_styleListWidget->clear();
	QFontDatabase database;
	foreach (QString style, database.styles(font)) 
	{
		m_styleListWidget->addItem(style);
	// This triggers loading the sizes for the first style
	restoreListWidgetValue(m_styleListWidget,m_choosenStyle);
	}
}


void CFontChooser::loadSizes(const QString& font, const QString& style) 
{

	QString saveText = saveListWidgetValue(m_sizeListWidget);

	// Put new values into listWidget
	m_sizeListWidget->clear();
	QFontDatabase database;
	foreach (int size, database.pointSizes(font, style)) 
	{
		m_sizeListWidget->addItem(QString::number(size));
	}

	restoreListWidgetValue(m_sizeListWidget, saveText);
}


void CFontChooser::outputHtmlText() 
{
	QString text = formatAsHtml(m_htmlText);
	text.replace("#FONT-FAMILY#", m_font.family());
	text.replace("#FONT-SIZE#", QString::number(m_font.pointSize()));
	text.replace("#FONT-WEIGHT#", (m_font.bold() ? "bold" : "normal") );
	text.replace("#FONT-STYLE#", m_font.italic() ? "italic" : "normal");
	m_webView->setHtml(text);
}


void CFontChooser::restoreListWidgetValue(QListWidget* listWidget, const QString& value) 
{
	if (value == "") {
		listWidget->setCurrentRow(0);
		return;
	}

	for (int i=0; i< listWidget->count(); i++)
	{
		if (listWidget->item(i)->text() == value) 
		{
			listWidget->setCurrentRow(i);
			return;
		}
	}
	listWidget->setCurrentRow(0);
}


QString CFontChooser::saveListWidgetValue(QListWidget* listWidget) 
{
	QString saveText;
	int row = listWidget->currentRow();
	if (row >= 0) {
		saveText = listWidget->item(row)->text();
	}
	return saveText;
}


void CFontChooser::setFont(const QFont& font) 
{
	disconnect(m_fontListWidget, 0, 0, 0);
	disconnect(m_styleListWidget, 0, 0, 0);
	disconnect(m_sizeListWidget, 0, 0, 0);

	// set the font
	m_font = font;
	restoreListWidgetValue(m_fontListWidget, m_font.family());

	// set the style
	loadStyles( m_font.family());
	QFontDatabase database;
	QString styleString = database.styleString(m_font);
	m_choosenStyle = styleString;
	restoreListWidgetValue(m_styleListWidget, styleString);

	// set the size
	loadSizes(m_font.family(), styleString);
	restoreListWidgetValue(m_sizeListWidget, QString::number(m_font.pointSize()) );

	outputHtmlText();
	connectListWidgets();
}


void CFontChooser::setFontStyle(const QString& styleString, QFont* font) 
{
	if (styleString.contains("bold",Qt::CaseInsensitive))
		font->setBold(true);
	else 
		font->setBold(false);

	if (styleString.contains("italic",Qt::CaseInsensitive) || styleString.contains("oblique",Qt::CaseInsensitive) )
		font->setItalic(true);
	else 
		font->setItalic(false);
}


void CFontChooser::setSampleText(const QString& htmlText) 
{
	m_htmlText = htmlText;
	outputHtmlText();
}


void CFontChooser::sizeChanged(QListWidgetItem* current, QListWidgetItem* /*previous*/)
{
	if (current == 0)
		return;

	QString size = m_sizeListWidget->currentItem()->text();
	m_font.setPointSize(size.toInt());

	outputHtmlText();
	emit fontSelected(m_font);
}


QSize CFontChooser::sizeHint() const 
{
	return QSize(170,100);
}

void CFontChooser::styleChanged(QListWidgetItem* current, QListWidgetItem* /*previous*/)
{
	if (current == 0)
		return;

	QString styleString = current->text();
	setFontStyle(styleString, &m_font);

	// Save style if the user choose it
	bool focus = m_styleListWidget->hasFocus();
	if (focus)
		m_choosenStyle = styleString;

	QString font  = m_fontListWidget->currentItem()->text();
	loadSizes(font, styleString);

	outputHtmlText();
	emit fontSelected(m_font);
}