0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the tools applications of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
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 |
}
|
|
230 |
}
|
|
231 |
if (m_styleComboBox->currentIndex() == -1 && normalIndex != -1)
|
|
232 |
m_styleComboBox->setCurrentIndex(normalIndex);
|
|
233 |
}
|
|
234 |
updatePointSizes(family, styleString());
|
|
235 |
}
|
|
236 |
|
|
237 |
int FontPanel::closestPointSizeIndex(int desiredPointSize) const
|
|
238 |
{
|
|
239 |
// try to maintain selection or select closest.
|
|
240 |
int closestIndex = -1;
|
|
241 |
int closestAbsError = 0xFFFF;
|
|
242 |
|
|
243 |
const int pointSizeCount = m_pointSizeComboBox->count();
|
|
244 |
for (int i = 0; i < pointSizeCount; i++) {
|
|
245 |
const int itemPointSize = m_pointSizeComboBox->itemData(i).toInt();
|
|
246 |
const int absError = qAbs(desiredPointSize - itemPointSize);
|
|
247 |
if (absError < closestAbsError) {
|
|
248 |
closestIndex = i;
|
|
249 |
closestAbsError = absError;
|
|
250 |
if (closestAbsError == 0)
|
|
251 |
break;
|
|
252 |
} else { // past optimum
|
|
253 |
if (absError > closestAbsError) {
|
|
254 |
break;
|
|
255 |
}
|
|
256 |
}
|
|
257 |
}
|
|
258 |
return closestIndex;
|
|
259 |
}
|
|
260 |
|
|
261 |
|
|
262 |
void FontPanel::updatePointSizes(const QString &family, const QString &styleString)
|
|
263 |
{
|
|
264 |
const int oldPointSize = pointSize();
|
|
265 |
|
|
266 |
QList<int> pointSizes = m_fontDatabase.pointSizes(family, styleString);
|
|
267 |
if (pointSizes.empty())
|
|
268 |
pointSizes = QFontDatabase::standardSizes();
|
|
269 |
|
|
270 |
const bool hasSizes = !pointSizes.empty();
|
|
271 |
m_pointSizeComboBox->clear();
|
|
272 |
m_pointSizeComboBox->setEnabled(hasSizes);
|
|
273 |
m_pointSizeComboBox->setCurrentIndex(-1);
|
|
274 |
|
|
275 |
// try to maintain selection or select closest.
|
|
276 |
if (hasSizes) {
|
|
277 |
QString n;
|
|
278 |
foreach (int pointSize, pointSizes)
|
|
279 |
m_pointSizeComboBox->addItem(n.setNum(pointSize), QVariant(pointSize));
|
|
280 |
const int closestIndex = closestPointSizeIndex(oldPointSize);
|
|
281 |
if (closestIndex != -1)
|
|
282 |
m_pointSizeComboBox->setCurrentIndex(closestIndex);
|
|
283 |
}
|
|
284 |
}
|
|
285 |
|
|
286 |
void FontPanel::slotUpdatePreviewFont()
|
|
287 |
{
|
|
288 |
m_previewLineEdit->setFont(selectedFont());
|
|
289 |
}
|
|
290 |
|
|
291 |
void FontPanel::delayedPreviewFontUpdate()
|
|
292 |
{
|
|
293 |
if (!m_previewFontUpdateTimer) {
|
|
294 |
m_previewFontUpdateTimer = new QTimer(this);
|
|
295 |
connect(m_previewFontUpdateTimer, SIGNAL(timeout()), this, SLOT(slotUpdatePreviewFont()));
|
|
296 |
m_previewFontUpdateTimer->setInterval(0);
|
|
297 |
m_previewFontUpdateTimer->setSingleShot(true);
|
|
298 |
}
|
|
299 |
if (m_previewFontUpdateTimer->isActive())
|
|
300 |
return;
|
|
301 |
m_previewFontUpdateTimer->start();
|
|
302 |
}
|
|
303 |
|
|
304 |
QT_END_NAMESPACE
|