source: trunk/tools/shared/fontpanel/fontpanel.cpp@ 987

Last change on this file since 987 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 10.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia 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
52QT_BEGIN_NAMESPACE
53
54FontPanel::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
93QFont 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 if (styleDescription.contains(QLatin1String("Italic")))
100 rc.setStyle(QFont::StyleItalic);
101 else if (styleDescription.contains(QLatin1String("Oblique")))
102 rc.setStyle(QFont::StyleOblique);
103 else
104 rc.setStyle(QFont::StyleNormal);
105 rc.setBold(m_fontDatabase.bold(family, styleDescription));
106
107 // Weight < 0 asserts...
108 const int weight = m_fontDatabase.weight(family, styleDescription);
109 if (weight >= 0)
110 rc.setWeight(weight);
111 return rc;
112}
113
114void FontPanel::setSelectedFont(const QFont &f)
115{
116 m_familyComboBox->setCurrentFont(f);
117 if (m_familyComboBox->currentIndex() < 0) {
118 // family not in writing system - find the corresponding one?
119 QList<QFontDatabase::WritingSystem> familyWritingSystems = m_fontDatabase.writingSystems(f.family());
120 if (familyWritingSystems.empty())
121 return;
122
123 setWritingSystem(familyWritingSystems.front());
124 m_familyComboBox->setCurrentFont(f);
125 }
126
127 updateFamily(family());
128
129 const int pointSizeIndex = closestPointSizeIndex(f.pointSize());
130 m_pointSizeComboBox->setCurrentIndex( pointSizeIndex);
131
132 const QString styleString = m_fontDatabase.styleString(f);
133 const int styleIndex = m_styleComboBox->findText(styleString);
134 m_styleComboBox->setCurrentIndex(styleIndex);
135 slotUpdatePreviewFont();
136}
137
138
139QFontDatabase::WritingSystem FontPanel::writingSystem() const
140{
141 const int currentIndex = m_writingSystemComboBox->currentIndex();
142 if ( currentIndex == -1)
143 return QFontDatabase::Latin;
144 return static_cast<QFontDatabase::WritingSystem>(m_writingSystemComboBox->itemData(currentIndex).toInt());
145}
146
147QString FontPanel::family() const
148{
149 const int currentIndex = m_familyComboBox->currentIndex();
150 return currentIndex != -1 ? m_familyComboBox->currentFont().family() : QString();
151}
152
153int FontPanel::pointSize() const
154{
155 const int currentIndex = m_pointSizeComboBox->currentIndex();
156 return currentIndex != -1 ? m_pointSizeComboBox->itemData(currentIndex).toInt() : 9;
157}
158
159QString FontPanel::styleString() const
160{
161 const int currentIndex = m_styleComboBox->currentIndex();
162 return currentIndex != -1 ? m_styleComboBox->itemText(currentIndex) : QString();
163}
164
165void FontPanel::setWritingSystem(QFontDatabase::WritingSystem ws)
166{
167 m_writingSystemComboBox->setCurrentIndex(m_writingSystemComboBox->findData(QVariant(ws)));
168 updateWritingSystem(ws);
169}
170
171
172void FontPanel::slotWritingSystemChanged(int)
173{
174 updateWritingSystem(writingSystem());
175 delayedPreviewFontUpdate();
176}
177
178void FontPanel::slotFamilyChanged(const QFont &)
179{
180 updateFamily(family());
181 delayedPreviewFontUpdate();
182}
183
184void FontPanel::slotStyleChanged(int)
185{
186 updatePointSizes(family(), styleString());
187 delayedPreviewFontUpdate();
188}
189
190void FontPanel::slotPointSizeChanged(int)
191{
192 delayedPreviewFontUpdate();
193}
194
195void FontPanel::updateWritingSystem(QFontDatabase::WritingSystem ws)
196{
197
198 m_previewLineEdit->setText(QFontDatabase::writingSystemSample(ws));
199 m_familyComboBox->setWritingSystem (ws);
200 // Current font not in WS ... set index 0.
201 if (m_familyComboBox->currentIndex() < 0) {
202 m_familyComboBox->setCurrentIndex(0);
203 updateFamily(family());
204 }
205}
206
207void FontPanel::updateFamily(const QString &family)
208{
209 // Update styles and trigger update of point sizes.
210 // Try to maintain selection or select normal
211 const QString oldStyleString = styleString();
212
213 const QStringList styles = m_fontDatabase.styles(family);
214 const bool hasStyles = !styles.empty();
215
216 m_styleComboBox->setCurrentIndex(-1);
217 m_styleComboBox->clear();
218 m_styleComboBox->setEnabled(hasStyles);
219
220 int normalIndex = -1;
221 const QString normalStyle = QLatin1String("Normal");
222
223 if (hasStyles) {
224 foreach (const QString &style, styles) {
225 // try to maintain selection or select 'normal' preferably
226 const int newIndex = m_styleComboBox->count();
227 m_styleComboBox->addItem(style);
228 if (oldStyleString == style) {
229 m_styleComboBox->setCurrentIndex(newIndex);
230 } else {
231 if (oldStyleString == normalStyle)
232 normalIndex = newIndex;
233 }
234 }
235 if (m_styleComboBox->currentIndex() == -1 && normalIndex != -1)
236 m_styleComboBox->setCurrentIndex(normalIndex);
237 }
238 updatePointSizes(family, styleString());
239}
240
241int FontPanel::closestPointSizeIndex(int desiredPointSize) const
242{
243 // try to maintain selection or select closest.
244 int closestIndex = -1;
245 int closestAbsError = 0xFFFF;
246
247 const int pointSizeCount = m_pointSizeComboBox->count();
248 for (int i = 0; i < pointSizeCount; i++) {
249 const int itemPointSize = m_pointSizeComboBox->itemData(i).toInt();
250 const int absError = qAbs(desiredPointSize - itemPointSize);
251 if (absError < closestAbsError) {
252 closestIndex = i;
253 closestAbsError = absError;
254 if (closestAbsError == 0)
255 break;
256 } else { // past optimum
257 if (absError > closestAbsError) {
258 break;
259 }
260 }
261 }
262 return closestIndex;
263}
264
265
266void FontPanel::updatePointSizes(const QString &family, const QString &styleString)
267{
268 const int oldPointSize = pointSize();
269
270 QList<int> pointSizes = m_fontDatabase.pointSizes(family, styleString);
271 if (pointSizes.empty())
272 pointSizes = QFontDatabase::standardSizes();
273
274 const bool hasSizes = !pointSizes.empty();
275 m_pointSizeComboBox->clear();
276 m_pointSizeComboBox->setEnabled(hasSizes);
277 m_pointSizeComboBox->setCurrentIndex(-1);
278
279 // try to maintain selection or select closest.
280 if (hasSizes) {
281 QString n;
282 foreach (int pointSize, pointSizes)
283 m_pointSizeComboBox->addItem(n.setNum(pointSize), QVariant(pointSize));
284 const int closestIndex = closestPointSizeIndex(oldPointSize);
285 if (closestIndex != -1)
286 m_pointSizeComboBox->setCurrentIndex(closestIndex);
287 }
288}
289
290void FontPanel::slotUpdatePreviewFont()
291{
292 m_previewLineEdit->setFont(selectedFont());
293}
294
295void FontPanel::delayedPreviewFontUpdate()
296{
297 if (!m_previewFontUpdateTimer) {
298 m_previewFontUpdateTimer = new QTimer(this);
299 connect(m_previewFontUpdateTimer, SIGNAL(timeout()), this, SLOT(slotUpdatePreviewFont()));
300 m_previewFontUpdateTimer->setInterval(0);
301 m_previewFontUpdateTimer->setSingleShot(true);
302 }
303 if (m_previewFontUpdateTimer->isActive())
304 return;
305 m_previewFontUpdateTimer->start();
306}
307
308QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.