summaryrefslogtreecommitdiff
path: root/src/frontend/settingsdialogs/btfontchooserwidget.cpp
blob: 4d943c4e7ae381e53d31bb21d59d7e683a9224c8 (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
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2011 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/settingsdialogs/btfontchooserwidget.h"

#include <QFontDatabase>
#include <QFrame>
#include <QGridLayout>
#include <QLabel>
#include <QListWidget>
#include <QListWidgetItem>
#include <QWebSettings>
#include <QWebView>
#include "frontend/settingsdialogs/clistwidget.h"


namespace {

class BtFontPreviewWebView: public QWebView {

    public: /* Methods: */

        inline BtFontPreviewWebView(QWidget *parent = 0)
            : QWebView(parent)
        {
            setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
        }

        virtual inline QSize sizeHint() const {
            return QSize(100, 100);
        }

};

const QString DEFAULT_FONT_PREVIEW_TEXT =
    "1 In the beginning God created the heaven and the earth. "
    "2 And the earth was without form, and void; and darkness was on the face of the deep. "
    " And the Spirit of God moved on the face of the waters.";

} // anonymous namespace

BtFontChooserWidget::BtFontChooserWidget(QWidget* parent)
        : QFrame(parent)
        , m_htmlText(DEFAULT_FONT_PREVIEW_TEXT)
{
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    createLayout();
    connectListWidgets();
    loadFonts();
    setFrameStyle(QFrame::Box);
    setFrameShadow(QFrame::Raised);

    retranslateUi();
}

void BtFontChooserWidget::createLayout() {
    m_fontNameLabel = new QLabel(this);

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

    m_fontStyleLabel = new QLabel(this);

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

    m_fontSizeLabel = new QLabel(this);

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

    m_fontPreview = new BtFontPreviewWebView(this);

    QGridLayout *l = new QGridLayout;
    l->addWidget(m_fontNameLabel, 0, 0);
    l->addWidget(m_fontListWidget, 1, 0);
    l->addWidget(m_fontStyleLabel, 0, 1);
    l->addWidget(m_styleListWidget, 1, 1);
    l->addWidget(m_fontSizeLabel, 0, 2);
    l->addWidget(m_sizeListWidget, 1, 2);
    l->addWidget(m_fontPreview, 2, 0, 1, 3);
    setLayout(l);
}

void BtFontChooserWidget::retranslateUi() {
    m_fontNameLabel->setText(tr("Font name:"));
    m_fontStyleLabel->setText(tr("Font style:"));
    m_fontSizeLabel->setText(tr("Size:"));
}

void BtFontChooserWidget::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 BtFontChooserWidget::fontChanged(QListWidgetItem* current, QListWidgetItem* /*previous*/) {
    if (current == 0)
        return;

    const QString fontFamily = current->text();
    m_font.setFamily(fontFamily);
    loadStyles(fontFamily);
    outputHtmlText();
    emit fontSelected(m_font);
}

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

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

void BtFontChooserWidget::loadSizes(const QString& font, const QString& style) {
    const QString saveText = saveListWidgetValue(m_sizeListWidget);

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

    restoreListWidgetValue(m_sizeListWidget, saveText);
}

void BtFontChooserWidget::outputHtmlText() {
    m_fontPreview->setHtml(
        QString(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
            "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
            "<head>"
                "<style type=\"text/css\">"
                    "*{background-color:%1;"
                      "color:%2;"
                      "font-family:%3;"
                      "font-size:%4pt;"
                      "font-weight:%5;"
                      "font-style:%6}"
                "</style>"
                "</head>"
                "<body>"
                    "<div style=\"display: inline;\" lang=\"en\">"
                        "%7"
                    "</div>"
                "</body>"
            "</html>"
        )
        .arg(palette().base().color().name())
        .arg(palette().text().color().name())
        .arg(m_font.family())
        .arg(QString::number(m_font.pointSize()))
        .arg(m_font.bold() ? "bold" : "normal")
        .arg(m_font.italic() ? "italic" : "normal")
        .arg(m_htmlText)
    );
}

void BtFontChooserWidget::restoreListWidgetValue(QListWidget* listWidget, const QString& value) {
    if (!value.isEmpty()) {
        for (int i = 0; i < listWidget->count(); i++) {
            if (listWidget->item(i)->text() == value) {
                listWidget->setCurrentRow(i);
                return;
            }
        }
    }
    listWidget->setCurrentRow(0);
}

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

    return QString();
}

void BtFontChooserWidget::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;
    const QString fontFamily = font.family();
    restoreListWidgetValue(m_fontListWidget, fontFamily);

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

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

    outputHtmlText();
    connectListWidgets();
}

void BtFontChooserWidget::setFontStyle(const QString& styleString, QFont* font) {
    font->setBold(styleString.contains("bold", Qt::CaseInsensitive));
    font->setItalic(styleString.contains("italic", Qt::CaseInsensitive)
                    || styleString.contains("oblique", Qt::CaseInsensitive));
}

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

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

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

    outputHtmlText();
    emit fontSelected(m_font);
}

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

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

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

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

    loadSizes(m_fontListWidget->currentItem()->text(), styleString);

    outputHtmlText();
    emit fontSelected(m_font);
}