1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the tools applications of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "fontpanel.h"
|
---|
43 |
|
---|
44 | #include <QtGui/QLabel>
|
---|
45 | #include <QtGui/QComboBox>
|
---|
46 | #include <QtGui/QFormLayout>
|
---|
47 | #include <QtGui/QSpacerItem>
|
---|
48 | #include <QtGui/QFontComboBox>
|
---|
49 | #include <QtCore/QTimer>
|
---|
50 | #include <QtGui/QLineEdit>
|
---|
51 |
|
---|
52 | QT_BEGIN_NAMESPACE
|
---|
53 |
|
---|
54 | FontPanel::FontPanel(QWidget *parentWidget) :
|
---|
55 | QGroupBox(parentWidget),
|
---|
56 | m_previewLineEdit(new QLineEdit),
|
---|
57 | m_writingSystemComboBox(new QComboBox),
|
---|
58 | m_familyComboBox(new QFontComboBox),
|
---|
59 | m_styleComboBox(new QComboBox),
|
---|
60 | m_pointSizeComboBox(new QComboBox),
|
---|
61 | m_previewFontUpdateTimer(0)
|
---|
62 | {
|
---|
63 | setTitle(tr("Font"));
|
---|
64 |
|
---|
65 | QFormLayout *formLayout = new QFormLayout(this);
|
---|
66 | // writing systems
|
---|
67 | m_writingSystemComboBox->setEditable(false);
|
---|
68 |
|
---|
69 | QList<QFontDatabase::WritingSystem> writingSystems = m_fontDatabase.writingSystems();
|
---|
70 | writingSystems.push_front(QFontDatabase::Any);
|
---|
71 | foreach (QFontDatabase::WritingSystem ws, writingSystems)
|
---|
72 | m_writingSystemComboBox->addItem(QFontDatabase::writingSystemName(ws), QVariant(ws));
|
---|
73 | connect(m_writingSystemComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotWritingSystemChanged(int)));
|
---|
74 | formLayout->addRow(tr("&Writing system"), m_writingSystemComboBox);
|
---|
75 |
|
---|
76 | connect(m_familyComboBox, SIGNAL( currentFontChanged(QFont)), this, SLOT(slotFamilyChanged(QFont)));
|
---|
77 | formLayout->addRow(tr("&Family"), m_familyComboBox);
|
---|
78 |
|
---|
79 | m_styleComboBox->setEditable(false);
|
---|
80 | connect(m_styleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotStyleChanged(int)));
|
---|
81 | formLayout->addRow(tr("&Style"), m_styleComboBox);
|
---|
82 |
|
---|
83 | m_pointSizeComboBox->setEditable(false);
|
---|
84 | connect(m_pointSizeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotPointSizeChanged(int)));
|
---|
85 | formLayout->addRow(tr("&Point size"), m_pointSizeComboBox);
|
---|
86 |
|
---|
87 | m_previewLineEdit->setReadOnly(true);
|
---|
88 | formLayout->addRow(m_previewLineEdit);
|
---|
89 |
|
---|
90 | setWritingSystem(QFontDatabase::Any);
|
---|
91 | }
|
---|
92 |
|
---|
93 | QFont FontPanel::selectedFont() const
|
---|
94 | {
|
---|
95 | QFont rc = m_familyComboBox->currentFont();
|
---|
96 | const QString family = rc.family();
|
---|
97 | rc.setPointSize(pointSize());
|
---|
98 | const QString styleDescription = styleString();
|
---|
99 | rc.setItalic(m_fontDatabase.italic(family, styleDescription));
|
---|
100 |
|
---|
101 | rc.setBold(m_fontDatabase.bold(family, styleDescription));
|
---|
102 |
|
---|
103 | // Weight < 0 asserts...
|
---|
104 | const int weight = m_fontDatabase.weight(family, styleDescription);
|
---|
105 | if (weight >= 0)
|
---|
106 | rc.setWeight(weight);
|
---|
107 | return rc;
|
---|
108 | }
|
---|
109 |
|
---|
110 | void FontPanel::setSelectedFont(const QFont &f)
|
---|
111 | {
|
---|
112 | m_familyComboBox->setCurrentFont(f);
|
---|
113 | if (m_familyComboBox->currentIndex() < 0) {
|
---|
114 | // family not in writing system - find the corresponding one?
|
---|
115 | QList<QFontDatabase::WritingSystem> familyWritingSystems = m_fontDatabase.writingSystems(f.family());
|
---|
116 | if (familyWritingSystems.empty())
|
---|
117 | return;
|
---|
118 |
|
---|
119 | setWritingSystem(familyWritingSystems.front());
|
---|
120 | m_familyComboBox->setCurrentFont(f);
|
---|
121 | }
|
---|
122 |
|
---|
123 | updateFamily(family());
|
---|
124 |
|
---|
125 | const int pointSizeIndex = closestPointSizeIndex(f.pointSize());
|
---|
126 | m_pointSizeComboBox->setCurrentIndex( pointSizeIndex);
|
---|
127 |
|
---|
128 | const QString styleString = m_fontDatabase.styleString(f);
|
---|
129 | const int styleIndex = m_styleComboBox->findText(styleString);
|
---|
130 | m_styleComboBox->setCurrentIndex(styleIndex);
|
---|
131 | slotUpdatePreviewFont();
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | QFontDatabase::WritingSystem FontPanel::writingSystem() const
|
---|
136 | {
|
---|
137 | const int currentIndex = m_writingSystemComboBox->currentIndex();
|
---|
138 | if ( currentIndex == -1)
|
---|
139 | return QFontDatabase::Latin;
|
---|
140 | return static_cast<QFontDatabase::WritingSystem>(m_writingSystemComboBox->itemData(currentIndex).toInt());
|
---|
141 | }
|
---|
142 |
|
---|
143 | QString FontPanel::family() const
|
---|
144 | {
|
---|
145 | const int currentIndex = m_familyComboBox->currentIndex();
|
---|
146 | return currentIndex != -1 ? m_familyComboBox->currentFont().family() : QString();
|
---|
147 | }
|
---|
148 |
|
---|
149 | int FontPanel::pointSize() const
|
---|
150 | {
|
---|
151 | const int currentIndex = m_pointSizeComboBox->currentIndex();
|
---|
152 | return currentIndex != -1 ? m_pointSizeComboBox->itemData(currentIndex).toInt() : 9;
|
---|
153 | }
|
---|
154 |
|
---|
155 | QString FontPanel::styleString() const
|
---|
156 | {
|
---|
157 | const int currentIndex = m_styleComboBox->currentIndex();
|
---|
158 | return currentIndex != -1 ? m_styleComboBox->itemText(currentIndex) : QString();
|
---|
159 | }
|
---|
160 |
|
---|
161 | void FontPanel::setWritingSystem(QFontDatabase::WritingSystem ws)
|
---|
162 | {
|
---|
163 | m_writingSystemComboBox->setCurrentIndex(m_writingSystemComboBox->findData(QVariant(ws)));
|
---|
164 | updateWritingSystem(ws);
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | void FontPanel::slotWritingSystemChanged(int)
|
---|
169 | {
|
---|
170 | updateWritingSystem(writingSystem());
|
---|
171 | delayedPreviewFontUpdate();
|
---|
172 | }
|
---|
173 |
|
---|
174 | void FontPanel::slotFamilyChanged(const QFont &)
|
---|
175 | {
|
---|
176 | updateFamily(family());
|
---|
177 | delayedPreviewFontUpdate();
|
---|
178 | }
|
---|
179 |
|
---|
180 | void FontPanel::slotStyleChanged(int)
|
---|
181 | {
|
---|
182 | updatePointSizes(family(), styleString());
|
---|
183 | delayedPreviewFontUpdate();
|
---|
184 | }
|
---|
185 |
|
---|
186 | void FontPanel::slotPointSizeChanged(int)
|
---|
187 | {
|
---|
188 | delayedPreviewFontUpdate();
|
---|
189 | }
|
---|
190 |
|
---|
191 | void FontPanel::updateWritingSystem(QFontDatabase::WritingSystem ws)
|
---|
192 | {
|
---|
193 |
|
---|
194 | m_previewLineEdit->setText(QFontDatabase::writingSystemSample(ws));
|
---|
195 | m_familyComboBox->setWritingSystem (ws);
|
---|
196 | // Current font not in WS ... set index 0.
|
---|
197 | if (m_familyComboBox->currentIndex() < 0) {
|
---|
198 | m_familyComboBox->setCurrentIndex(0);
|
---|
199 | updateFamily(family());
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | void FontPanel::updateFamily(const QString &family)
|
---|
204 | {
|
---|
205 | // Update styles and trigger update of point sizes.
|
---|
206 | // Try to maintain selection or select normal
|
---|
207 | const QString oldStyleString = styleString();
|
---|
208 |
|
---|
209 | const QStringList styles = m_fontDatabase.styles(family);
|
---|
210 | const bool hasStyles = !styles.empty();
|
---|
211 |
|
---|
212 | m_styleComboBox->setCurrentIndex(-1);
|
---|
213 | m_styleComboBox->clear();
|
---|
214 | m_styleComboBox->setEnabled(hasStyles);
|
---|
215 |
|
---|
216 | int normalIndex = -1;
|
---|
217 | const QString normalStyle = QLatin1String("Normal");
|
---|
218 |
|
---|
219 | if (hasStyles) {
|
---|
220 | foreach (QString style, styles) {
|
---|
221 | // try to maintain selection or select 'normal' preferably
|
---|
222 | const int newIndex = m_styleComboBox->count();
|
---|
223 | m_styleComboBox->addItem(style);
|
---|
224 | if (oldStyleString == style) {
|
---|
225 | m_styleComboBox->setCurrentIndex(newIndex);
|
---|
226 | } else {
|
---|
227 | if (oldStyleString == normalStyle)
|
---|
228 | normalIndex = newIndex;
|
---|
229 | }
|
---|
|
---|