Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qfontcombobox.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
16 | ** | - | ||||||||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include "qfontcombobox.h" | - | ||||||||||||||||||
35 | - | |||||||||||||||||||
36 | #ifndef QT_NO_FONTCOMBOBOX | - | ||||||||||||||||||
37 | - | |||||||||||||||||||
38 | #include <qstringlistmodel.h> | - | ||||||||||||||||||
39 | #include <qitemdelegate.h> | - | ||||||||||||||||||
40 | #include <qlistview.h> | - | ||||||||||||||||||
41 | #include <qpainter.h> | - | ||||||||||||||||||
42 | #include <qevent.h> | - | ||||||||||||||||||
43 | #include <qapplication.h> | - | ||||||||||||||||||
44 | #include <private/qcombobox_p.h> | - | ||||||||||||||||||
45 | #include <QDesktopWidget> | - | ||||||||||||||||||
46 | #include <qdebug.h> | - | ||||||||||||||||||
47 | - | |||||||||||||||||||
48 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
49 | - | |||||||||||||||||||
50 | static QFontDatabase::WritingSystem writingSystemFromScript(QLocale::Script script) | - | ||||||||||||||||||
51 | { | - | ||||||||||||||||||
52 | switch (script) { | - | ||||||||||||||||||
53 | case QLocale::ArabicScript: never executed: case QLocale::ArabicScript: | 0 | ||||||||||||||||||
54 | return QFontDatabase::Arabic; never executed: return QFontDatabase::Arabic; | 0 | ||||||||||||||||||
55 | case QLocale::CyrillicScript: never executed: case QLocale::CyrillicScript: | 0 | ||||||||||||||||||
56 | return QFontDatabase::Cyrillic; never executed: return QFontDatabase::Cyrillic; | 0 | ||||||||||||||||||
57 | case QLocale::GurmukhiScript: never executed: case QLocale::GurmukhiScript: | 0 | ||||||||||||||||||
58 | return QFontDatabase::Gurmukhi; never executed: return QFontDatabase::Gurmukhi; | 0 | ||||||||||||||||||
59 | case QLocale::SimplifiedHanScript: never executed: case QLocale::SimplifiedHanScript: | 0 | ||||||||||||||||||
60 | return QFontDatabase::SimplifiedChinese; never executed: return QFontDatabase::SimplifiedChinese; | 0 | ||||||||||||||||||
61 | case QLocale::TraditionalHanScript: never executed: case QLocale::TraditionalHanScript: | 0 | ||||||||||||||||||
62 | return QFontDatabase::TraditionalChinese; never executed: return QFontDatabase::TraditionalChinese; | 0 | ||||||||||||||||||
63 | case QLocale::LatinScript: never executed: case QLocale::LatinScript: | 0 | ||||||||||||||||||
64 | return QFontDatabase::Latin; never executed: return QFontDatabase::Latin; | 0 | ||||||||||||||||||
65 | case QLocale::ArmenianScript: never executed: case QLocale::ArmenianScript: | 0 | ||||||||||||||||||
66 | return QFontDatabase::Armenian; never executed: return QFontDatabase::Armenian; | 0 | ||||||||||||||||||
67 | case QLocale::BengaliScript: never executed: case QLocale::BengaliScript: | 0 | ||||||||||||||||||
68 | return QFontDatabase::Bengali; never executed: return QFontDatabase::Bengali; | 0 | ||||||||||||||||||
69 | case QLocale::DevanagariScript: never executed: case QLocale::DevanagariScript: | 0 | ||||||||||||||||||
70 | return QFontDatabase::Devanagari; never executed: return QFontDatabase::Devanagari; | 0 | ||||||||||||||||||
71 | case QLocale::GeorgianScript: never executed: case QLocale::GeorgianScript: | 0 | ||||||||||||||||||
72 | return QFontDatabase::Georgian; never executed: return QFontDatabase::Georgian; | 0 | ||||||||||||||||||
73 | case QLocale::GreekScript: never executed: case QLocale::GreekScript: | 0 | ||||||||||||||||||
74 | return QFontDatabase::Greek; never executed: return QFontDatabase::Greek; | 0 | ||||||||||||||||||
75 | case QLocale::GujaratiScript: never executed: case QLocale::GujaratiScript: | 0 | ||||||||||||||||||
76 | return QFontDatabase::Gujarati; never executed: return QFontDatabase::Gujarati; | 0 | ||||||||||||||||||
77 | case QLocale::HebrewScript: never executed: case QLocale::HebrewScript: | 0 | ||||||||||||||||||
78 | return QFontDatabase::Hebrew; never executed: return QFontDatabase::Hebrew; | 0 | ||||||||||||||||||
79 | case QLocale::JapaneseScript: never executed: case QLocale::JapaneseScript: | 0 | ||||||||||||||||||
80 | return QFontDatabase::Japanese; never executed: return QFontDatabase::Japanese; | 0 | ||||||||||||||||||
81 | case QLocale::KhmerScript: never executed: case QLocale::KhmerScript: | 0 | ||||||||||||||||||
82 | return QFontDatabase::Khmer; never executed: return QFontDatabase::Khmer; | 0 | ||||||||||||||||||
83 | case QLocale::KannadaScript: never executed: case QLocale::KannadaScript: | 0 | ||||||||||||||||||
84 | return QFontDatabase::Kannada; never executed: return QFontDatabase::Kannada; | 0 | ||||||||||||||||||
85 | case QLocale::KoreanScript: never executed: case QLocale::KoreanScript: | 0 | ||||||||||||||||||
86 | return QFontDatabase::Korean; never executed: return QFontDatabase::Korean; | 0 | ||||||||||||||||||
87 | case QLocale::LaoScript: never executed: case QLocale::LaoScript: | 0 | ||||||||||||||||||
88 | return QFontDatabase::Lao; never executed: return QFontDatabase::Lao; | 0 | ||||||||||||||||||
89 | case QLocale::MalayalamScript: never executed: case QLocale::MalayalamScript: | 0 | ||||||||||||||||||
90 | return QFontDatabase::Malayalam; never executed: return QFontDatabase::Malayalam; | 0 | ||||||||||||||||||
91 | case QLocale::MyanmarScript: never executed: case QLocale::MyanmarScript: | 0 | ||||||||||||||||||
92 | return QFontDatabase::Myanmar; never executed: return QFontDatabase::Myanmar; | 0 | ||||||||||||||||||
93 | case QLocale::TamilScript: never executed: case QLocale::TamilScript: | 0 | ||||||||||||||||||
94 | return QFontDatabase::Tamil; never executed: return QFontDatabase::Tamil; | 0 | ||||||||||||||||||
95 | case QLocale::TeluguScript: never executed: case QLocale::TeluguScript: | 0 | ||||||||||||||||||
96 | return QFontDatabase::Telugu; never executed: return QFontDatabase::Telugu; | 0 | ||||||||||||||||||
97 | case QLocale::ThaanaScript: never executed: case QLocale::ThaanaScript: | 0 | ||||||||||||||||||
98 | return QFontDatabase::Thaana; never executed: return QFontDatabase::Thaana; | 0 | ||||||||||||||||||
99 | case QLocale::ThaiScript: never executed: case QLocale::ThaiScript: | 0 | ||||||||||||||||||
100 | return QFontDatabase::Thai; never executed: return QFontDatabase::Thai; | 0 | ||||||||||||||||||
101 | case QLocale::TibetanScript: never executed: case QLocale::TibetanScript: | 0 | ||||||||||||||||||
102 | return QFontDatabase::Tibetan; never executed: return QFontDatabase::Tibetan; | 0 | ||||||||||||||||||
103 | case QLocale::SinhalaScript: never executed: case QLocale::SinhalaScript: | 0 | ||||||||||||||||||
104 | return QFontDatabase::Sinhala; never executed: return QFontDatabase::Sinhala; | 0 | ||||||||||||||||||
105 | case QLocale::SyriacScript: never executed: case QLocale::SyriacScript: | 0 | ||||||||||||||||||
106 | return QFontDatabase::Syriac; never executed: return QFontDatabase::Syriac; | 0 | ||||||||||||||||||
107 | case QLocale::OriyaScript: never executed: case QLocale::OriyaScript: | 0 | ||||||||||||||||||
108 | return QFontDatabase::Oriya; never executed: return QFontDatabase::Oriya; | 0 | ||||||||||||||||||
109 | case QLocale::OghamScript: never executed: case QLocale::OghamScript: | 0 | ||||||||||||||||||
110 | return QFontDatabase::Ogham; never executed: return QFontDatabase::Ogham; | 0 | ||||||||||||||||||
111 | case QLocale::RunicScript: never executed: case QLocale::RunicScript: | 0 | ||||||||||||||||||
112 | return QFontDatabase::Runic; never executed: return QFontDatabase::Runic; | 0 | ||||||||||||||||||
113 | case QLocale::NkoScript: never executed: case QLocale::NkoScript: | 0 | ||||||||||||||||||
114 | return QFontDatabase::Nko; never executed: return QFontDatabase::Nko; | 0 | ||||||||||||||||||
115 | default: never executed: default: | 0 | ||||||||||||||||||
116 | return QFontDatabase::Any; never executed: return QFontDatabase::Any; | 0 | ||||||||||||||||||
117 | } | - | ||||||||||||||||||
118 | } | - | ||||||||||||||||||
119 | - | |||||||||||||||||||
120 | static QFontDatabase::WritingSystem writingSystemFromLocale() | - | ||||||||||||||||||
121 | { | - | ||||||||||||||||||
122 | QStringList uiLanguages = QLocale::system().uiLanguages(); | - | ||||||||||||||||||
123 | QLocale::Script script; | - | ||||||||||||||||||
124 | if (!uiLanguages.isEmpty())
| 0 | ||||||||||||||||||
125 | script = QLocale(uiLanguages.at(0)).script(); never executed: script = QLocale(uiLanguages.at(0)).script(); | 0 | ||||||||||||||||||
126 | else | - | ||||||||||||||||||
127 | script = QLocale::system().script(); never executed: script = QLocale::system().script(); | 0 | ||||||||||||||||||
128 | - | |||||||||||||||||||
129 | return writingSystemFromScript(script); never executed: return writingSystemFromScript(script); | 0 | ||||||||||||||||||
130 | } | - | ||||||||||||||||||
131 | - | |||||||||||||||||||
132 | static QFontDatabase::WritingSystem writingSystemForFont(const QFont &font, bool *hasLatin) | - | ||||||||||||||||||
133 | { | - | ||||||||||||||||||
134 | QList<QFontDatabase::WritingSystem> writingSystems = QFontDatabase().writingSystems(font.family()); | - | ||||||||||||||||||
135 | // qDebug() << font.family() << writingSystems; | - | ||||||||||||||||||
136 | - | |||||||||||||||||||
137 | // this just confuses the algorithm below. Vietnamese is Latin with lots of special chars | - | ||||||||||||||||||
138 | writingSystems.removeOne(QFontDatabase::Vietnamese); | - | ||||||||||||||||||
139 | *hasLatin = writingSystems.removeOne(QFontDatabase::Latin); | - | ||||||||||||||||||
140 | - | |||||||||||||||||||
141 | if (writingSystems.isEmpty())
| 0 | ||||||||||||||||||
142 | return QFontDatabase::Any; never executed: return QFontDatabase::Any; | 0 | ||||||||||||||||||
143 | - | |||||||||||||||||||
144 | QFontDatabase::WritingSystem system = writingSystemFromLocale(); | - | ||||||||||||||||||
145 | - | |||||||||||||||||||
146 | if (writingSystems.contains(system))
| 0 | ||||||||||||||||||
147 | return system; never executed: return system; | 0 | ||||||||||||||||||
148 | - | |||||||||||||||||||
149 | if (system == QFontDatabase::TraditionalChinese
| 0 | ||||||||||||||||||
150 | && writingSystems.contains(QFontDatabase::SimplifiedChinese)) {
| 0 | ||||||||||||||||||
151 | return QFontDatabase::SimplifiedChinese; never executed: return QFontDatabase::SimplifiedChinese; | 0 | ||||||||||||||||||
152 | } | - | ||||||||||||||||||
153 | - | |||||||||||||||||||
154 | if (system == QFontDatabase::SimplifiedChinese
| 0 | ||||||||||||||||||
155 | && writingSystems.contains(QFontDatabase::TraditionalChinese)) {
| 0 | ||||||||||||||||||
156 | return QFontDatabase::TraditionalChinese; never executed: return QFontDatabase::TraditionalChinese; | 0 | ||||||||||||||||||
157 | } | - | ||||||||||||||||||
158 | - | |||||||||||||||||||
159 | system = writingSystems.last(); | - | ||||||||||||||||||
160 | - | |||||||||||||||||||
161 | if (!*hasLatin) {
| 0 | ||||||||||||||||||
162 | // we need to show something | - | ||||||||||||||||||
163 | return system; never executed: return system; | 0 | ||||||||||||||||||
164 | } | - | ||||||||||||||||||
165 | - | |||||||||||||||||||
166 | if (writingSystems.count() == 1 && system > QFontDatabase::Cyrillic)
| 0 | ||||||||||||||||||
167 | return system; never executed: return system; | 0 | ||||||||||||||||||
168 | - | |||||||||||||||||||
169 | if (writingSystems.count() <= 2 && system > QFontDatabase::Armenian && system < QFontDatabase::Vietnamese)
| 0 | ||||||||||||||||||
170 | return system; never executed: return system; | 0 | ||||||||||||||||||
171 | - | |||||||||||||||||||
172 | if (writingSystems.count() <= 5 && system >= QFontDatabase::SimplifiedChinese && system <= QFontDatabase::Korean)
| 0 | ||||||||||||||||||
173 | return system; never executed: return system; | 0 | ||||||||||||||||||
174 | - | |||||||||||||||||||
175 | return QFontDatabase::Any; never executed: return QFontDatabase::Any; | 0 | ||||||||||||||||||
176 | } | - | ||||||||||||||||||
177 | - | |||||||||||||||||||
178 | class QFontFamilyDelegate : public QAbstractItemDelegate | - | ||||||||||||||||||
179 | { | - | ||||||||||||||||||
180 | Q_OBJECT | - | ||||||||||||||||||
181 | public: | - | ||||||||||||||||||
182 | explicit QFontFamilyDelegate(QObject *parent); | - | ||||||||||||||||||
183 | - | |||||||||||||||||||
184 | // painting | - | ||||||||||||||||||
185 | void paint(QPainter *painter, | - | ||||||||||||||||||
186 | const QStyleOptionViewItem &option, | - | ||||||||||||||||||
187 | const QModelIndex &index) const Q_DECL_OVERRIDE; | - | ||||||||||||||||||
188 | - | |||||||||||||||||||
189 | QSize sizeHint(const QStyleOptionViewItem &option, | - | ||||||||||||||||||
190 | const QModelIndex &index) const Q_DECL_OVERRIDE; | - | ||||||||||||||||||
191 | - | |||||||||||||||||||
192 | QIcon truetype; | - | ||||||||||||||||||
193 | QIcon bitmap; | - | ||||||||||||||||||
194 | QFontDatabase::WritingSystem writingSystem; | - | ||||||||||||||||||
195 | }; | - | ||||||||||||||||||
196 | - | |||||||||||||||||||
197 | QFontFamilyDelegate::QFontFamilyDelegate(QObject *parent) | - | ||||||||||||||||||
198 | : QAbstractItemDelegate(parent) | - | ||||||||||||||||||
199 | { | - | ||||||||||||||||||
200 | truetype = QIcon(QLatin1String(":/qt-project.org/styles/commonstyle/images/fonttruetype-16.png")); | - | ||||||||||||||||||
201 | bitmap = QIcon(QLatin1String(":/qt-project.org/styles/commonstyle/images/fontbitmap-16.png")); | - | ||||||||||||||||||
202 | writingSystem = QFontDatabase::Any; | - | ||||||||||||||||||
203 | } never executed: end of block | 0 | ||||||||||||||||||
204 | - | |||||||||||||||||||
205 | void QFontFamilyDelegate::paint(QPainter *painter, | - | ||||||||||||||||||
206 | const QStyleOptionViewItem &option, | - | ||||||||||||||||||
207 | const QModelIndex &index) const | - | ||||||||||||||||||
208 | { | - | ||||||||||||||||||
209 | QString text = index.data(Qt::DisplayRole).toString(); | - | ||||||||||||||||||
210 | QFont font(option.font); | - | ||||||||||||||||||
211 | font.setPointSize(QFontInfo(font).pointSize() * 3 / 2); | - | ||||||||||||||||||
212 | QFont font2 = font; | - | ||||||||||||||||||
213 | font2.setFamily(text); | - | ||||||||||||||||||
214 | - | |||||||||||||||||||
215 | bool hasLatin; | - | ||||||||||||||||||
216 | QFontDatabase::WritingSystem system = writingSystemForFont(font2, &hasLatin); | - | ||||||||||||||||||
217 | if (hasLatin)
| 0 | ||||||||||||||||||
218 | font = font2; never executed: font = font2; | 0 | ||||||||||||||||||
219 | - | |||||||||||||||||||
220 | QRect r = option.rect; | - | ||||||||||||||||||
221 | - | |||||||||||||||||||
222 | if (option.state & QStyle::State_Selected) {
| 0 | ||||||||||||||||||
223 | painter->save(); | - | ||||||||||||||||||
224 | painter->setBrush(option.palette.highlight()); | - | ||||||||||||||||||
225 | painter->setPen(Qt::NoPen); | - | ||||||||||||||||||
226 | painter->drawRect(option.rect); | - | ||||||||||||||||||
227 | painter->setPen(QPen(option.palette.highlightedText(), 0)); | - | ||||||||||||||||||
228 | } never executed: end of block | 0 | ||||||||||||||||||
229 | - | |||||||||||||||||||
230 | const QIcon *icon = &bitmap; | - | ||||||||||||||||||
231 | if (QFontDatabase().isSmoothlyScalable(text)) {
| 0 | ||||||||||||||||||
232 | icon = &truetype; | - | ||||||||||||||||||
233 | } never executed: end of block | 0 | ||||||||||||||||||
234 | QSize actualSize = icon->actualSize(r.size()); | - | ||||||||||||||||||
235 | - | |||||||||||||||||||
236 | icon->paint(painter, r, Qt::AlignLeft|Qt::AlignVCenter); | - | ||||||||||||||||||
237 | if (option.direction == Qt::RightToLeft)
| 0 | ||||||||||||||||||
238 | r.setRight(r.right() - actualSize.width() - 4); never executed: r.setRight(r.right() - actualSize.width() - 4); | 0 | ||||||||||||||||||
239 | else | - | ||||||||||||||||||
240 | r.setLeft(r.left() + actualSize.width() + 4); never executed: r.setLeft(r.left() + actualSize.width() + 4); | 0 | ||||||||||||||||||
241 | - | |||||||||||||||||||
242 | QFont old = painter->font(); | - | ||||||||||||||||||
243 | painter->setFont(font); | - | ||||||||||||||||||
244 | - | |||||||||||||||||||
245 | // If the ascent of the font is larger than the height of the rect, | - | ||||||||||||||||||
246 | // we will clip the text, so it's better to align the tight bounding rect in this case | - | ||||||||||||||||||
247 | // This is specifically for fonts where the ascent is very large compared to | - | ||||||||||||||||||
248 | // the descent, like certain of the Stix family. | - | ||||||||||||||||||
249 | QFontMetricsF fontMetrics(font); | - | ||||||||||||||||||
250 | if (fontMetrics.ascent() > r.height()) {
| 0 | ||||||||||||||||||
251 | QRectF tbr = fontMetrics.tightBoundingRect(text); | - | ||||||||||||||||||
252 | painter->drawText(r.x(), r.y() + (r.height() + tbr.height()) / 2.0, text); | - | ||||||||||||||||||
253 | } else { never executed: end of block | 0 | ||||||||||||||||||
254 | painter->drawText(r, Qt::AlignVCenter|Qt::AlignLeading|Qt::TextSingleLine, text); | - | ||||||||||||||||||
255 | } never executed: end of block | 0 | ||||||||||||||||||
256 | - | |||||||||||||||||||
257 | if (writingSystem != QFontDatabase::Any)
| 0 | ||||||||||||||||||
258 | system = writingSystem; never executed: system = writingSystem; | 0 | ||||||||||||||||||
259 | - | |||||||||||||||||||
260 | if (system != QFontDatabase::Any) {
| 0 | ||||||||||||||||||
261 | int w = painter->fontMetrics().width(text + QLatin1String(" ")); | - | ||||||||||||||||||
262 | painter->setFont(font2); | - | ||||||||||||||||||
263 | QString sample = QFontDatabase().writingSystemSample(system); | - | ||||||||||||||||||
264 | if (option.direction == Qt::RightToLeft)
| 0 | ||||||||||||||||||
265 | r.setRight(r.right() - w); never executed: r.setRight(r.right() - w); | 0 | ||||||||||||||||||
266 | else | - | ||||||||||||||||||
267 | r.setLeft(r.left() + w); never executed: r.setLeft(r.left() + w); | 0 | ||||||||||||||||||
268 | painter->drawText(r, Qt::AlignVCenter|Qt::AlignLeading|Qt::TextSingleLine, sample); | - | ||||||||||||||||||
269 | } never executed: end of block | 0 | ||||||||||||||||||
270 | painter->setFont(old); | - | ||||||||||||||||||
271 | - | |||||||||||||||||||
272 | if (option.state & QStyle::State_Selected)
| 0 | ||||||||||||||||||
273 | painter->restore(); never executed: painter->restore(); | 0 | ||||||||||||||||||
274 | - | |||||||||||||||||||
275 | } never executed: end of block | 0 | ||||||||||||||||||
276 | - | |||||||||||||||||||
277 | QSize QFontFamilyDelegate::sizeHint(const QStyleOptionViewItem &option, | - | ||||||||||||||||||
278 | const QModelIndex &index) const | - | ||||||||||||||||||
279 | { | - | ||||||||||||||||||
280 | QString text = index.data(Qt::DisplayRole).toString(); | - | ||||||||||||||||||
281 | QFont font(option.font); | - | ||||||||||||||||||
282 | // font.setFamily(text); | - | ||||||||||||||||||
283 | font.setPointSize(QFontInfo(font).pointSize() * 3/2); | - | ||||||||||||||||||
284 | QFontMetrics fontMetrics(font); | - | ||||||||||||||||||
285 | return QSize(fontMetrics.width(text), fontMetrics.height()); never executed: return QSize(fontMetrics.width(text), fontMetrics.height()); | 0 | ||||||||||||||||||
286 | } | - | ||||||||||||||||||
287 | - | |||||||||||||||||||
288 | - | |||||||||||||||||||
289 | class QFontComboBoxPrivate : public QComboBoxPrivate | - | ||||||||||||||||||
290 | { | - | ||||||||||||||||||
291 | public: | - | ||||||||||||||||||
292 | inline QFontComboBoxPrivate() { filters = QFontComboBox::AllFonts; } never executed: end of block | 0 | ||||||||||||||||||
293 | - | |||||||||||||||||||
294 | QFontComboBox::FontFilters filters; | - | ||||||||||||||||||
295 | QFont currentFont; | - | ||||||||||||||||||
296 | - | |||||||||||||||||||
297 | void _q_updateModel(); | - | ||||||||||||||||||
298 | void _q_currentChanged(const QString &); | - | ||||||||||||||||||
299 | - | |||||||||||||||||||
300 | Q_DECLARE_PUBLIC(QFontComboBox) | - | ||||||||||||||||||
301 | }; | - | ||||||||||||||||||
302 | - | |||||||||||||||||||
303 | - | |||||||||||||||||||
304 | void QFontComboBoxPrivate::_q_updateModel() | - | ||||||||||||||||||
305 | { | - | ||||||||||||||||||
306 | Q_Q(QFontComboBox); | - | ||||||||||||||||||
307 | const int scalableMask = (QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts); | - | ||||||||||||||||||
308 | const int spacingMask = (QFontComboBox::ProportionalFonts | QFontComboBox::MonospacedFonts); | - | ||||||||||||||||||
309 | - | |||||||||||||||||||
310 | QStringListModel *m = qobject_cast<QStringListModel *>(q->model()); | - | ||||||||||||||||||
311 | if (!m)
| 0 | ||||||||||||||||||
312 | return; never executed: return; | 0 | ||||||||||||||||||
313 | QFontFamilyDelegate *delegate = qobject_cast<QFontFamilyDelegate *>(q->view()->itemDelegate()); | - | ||||||||||||||||||
314 | QFontDatabase::WritingSystem system = delegate ? delegate->writingSystem : QFontDatabase::Any;
| 0 | ||||||||||||||||||
315 | - | |||||||||||||||||||
316 | QFontDatabase fdb; | - | ||||||||||||||||||
317 | QStringList list = fdb.families(system); | - | ||||||||||||||||||
318 | QStringList result; | - | ||||||||||||||||||
319 | - | |||||||||||||||||||
320 | int offset = 0; | - | ||||||||||||||||||
321 | QFontInfo fi(currentFont); | - | ||||||||||||||||||
322 | - | |||||||||||||||||||
323 | for (int i = 0; i < list.size(); ++i) {
| 0 | ||||||||||||||||||
324 | if (fdb.isPrivateFamily(list.at(i)))
| 0 | ||||||||||||||||||
325 | continue; never executed: continue; | 0 | ||||||||||||||||||
326 | - | |||||||||||||||||||
327 | if ((filters & scalableMask) && (filters & scalableMask) != scalableMask) {
| 0 | ||||||||||||||||||
328 | if (bool(filters & QFontComboBox::ScalableFonts) != fdb.isSmoothlyScalable(list.at(i)))
| 0 | ||||||||||||||||||
329 | continue; never executed: continue; | 0 | ||||||||||||||||||
330 | } never executed: end of block | 0 | ||||||||||||||||||
331 | if ((filters & spacingMask) && (filters & spacingMask) != spacingMask) {
| 0 | ||||||||||||||||||
332 | if (bool(filters & QFontComboBox::MonospacedFonts) != fdb.isFixedPitch(list.at(i)))
| 0 | ||||||||||||||||||
333 | continue; never executed: continue; | 0 | ||||||||||||||||||
334 | } never executed: end of block | 0 | ||||||||||||||||||
335 | result += list.at(i); | - | ||||||||||||||||||
336 | if (list.at(i) == fi.family() || list.at(i).startsWith(fi.family() + QLatin1String(" [")))
| 0 | ||||||||||||||||||
337 | offset = result.count() - 1; never executed: offset = result.count() - 1; | 0 | ||||||||||||||||||
338 | } never executed: end of block | 0 | ||||||||||||||||||
339 | list = result; | - | ||||||||||||||||||
340 | - | |||||||||||||||||||
341 | //we need to block the signals so that the model doesn't emit reset | - | ||||||||||||||||||
342 | //this prevents the current index from changing | - | ||||||||||||||||||
343 | //it will be updated just after this | - | ||||||||||||||||||
344 | ///TODO: we should finda way to avoid blocking signals and have a real update of the model | - | ||||||||||||||||||
345 | { | - | ||||||||||||||||||
346 | const QSignalBlocker blocker(m); | - | ||||||||||||||||||
347 | m->setStringList(list); | - | ||||||||||||||||||
348 | } | - | ||||||||||||||||||
349 | - | |||||||||||||||||||
350 | if (list.isEmpty()) {
| 0 | ||||||||||||||||||
351 | if (currentFont != QFont()) {
| 0 | ||||||||||||||||||
352 | currentFont = QFont(); | - | ||||||||||||||||||
353 | emit q->currentFontChanged(currentFont); | - | ||||||||||||||||||
354 | } never executed: end of block | 0 | ||||||||||||||||||
355 | } else { never executed: end of block | 0 | ||||||||||||||||||
356 | q->setCurrentIndex(offset); | - | ||||||||||||||||||
357 | } never executed: end of block | 0 | ||||||||||||||||||
358 | } | - | ||||||||||||||||||
359 | - | |||||||||||||||||||
360 | - | |||||||||||||||||||
361 | void QFontComboBoxPrivate::_q_currentChanged(const QString &text) | - | ||||||||||||||||||
362 | { | - | ||||||||||||||||||
363 | Q_Q(QFontComboBox); | - | ||||||||||||||||||
364 | if (currentFont.family() != text) {
| 0 | ||||||||||||||||||
365 | currentFont.setFamily(text); | - | ||||||||||||||||||
366 | emit q->currentFontChanged(currentFont); | - | ||||||||||||||||||
367 | } never executed: end of block | 0 | ||||||||||||||||||
368 | } never executed: end of block | 0 | ||||||||||||||||||
369 | - | |||||||||||||||||||
370 | /*! | - | ||||||||||||||||||
371 | \class QFontComboBox | - | ||||||||||||||||||
372 | \brief The QFontComboBox widget is a combobox that lets the user | - | ||||||||||||||||||
373 | select a font family. | - | ||||||||||||||||||
374 | - | |||||||||||||||||||
375 | \since 4.2 | - | ||||||||||||||||||
376 | \ingroup basicwidgets | - | ||||||||||||||||||
377 | \inmodule QtWidgets | - | ||||||||||||||||||
378 | - | |||||||||||||||||||
379 | The combobox is populated with an alphabetized list of font | - | ||||||||||||||||||
380 | family names, such as Arial, Helvetica, and Times New Roman. | - | ||||||||||||||||||
381 | Family names are displayed using the actual font when possible. | - | ||||||||||||||||||
382 | For fonts such as Symbol, where the name is not representable in | - | ||||||||||||||||||
383 | the font itself, a sample of the font is displayed next to the | - | ||||||||||||||||||
384 | family name. | - | ||||||||||||||||||
385 | - | |||||||||||||||||||
386 | QFontComboBox is often used in toolbars, in conjunction with a | - | ||||||||||||||||||
387 | QComboBox for controlling the font size and two \l{QToolButton}s | - | ||||||||||||||||||
388 | for bold and italic. | - | ||||||||||||||||||
389 | - | |||||||||||||||||||
390 | When the user selects a new font, the currentFontChanged() signal | - | ||||||||||||||||||
391 | is emitted in addition to currentIndexChanged(). | - | ||||||||||||||||||
392 | - | |||||||||||||||||||
393 | Call setWritingSystem() to tell QFontComboBox to show only fonts | - | ||||||||||||||||||
394 | that support a given writing system, and setFontFilters() to | - | ||||||||||||||||||
395 | filter out certain types of fonts as e.g. non scalable fonts or | - | ||||||||||||||||||
396 | monospaced fonts. | - | ||||||||||||||||||
397 | - | |||||||||||||||||||
398 | \image windowsvista-fontcombobox.png Screenshot of QFontComboBox on Windows Vista | - | ||||||||||||||||||
399 | - | |||||||||||||||||||
400 | \sa QComboBox, QFont, QFontInfo, QFontMetrics, QFontDatabase, {Character Map Example} | - | ||||||||||||||||||
401 | */ | - | ||||||||||||||||||
402 | - | |||||||||||||||||||
403 | /*! | - | ||||||||||||||||||
404 | Constructs a font combobox with the given \a parent. | - | ||||||||||||||||||
405 | */ | - | ||||||||||||||||||
406 | QFontComboBox::QFontComboBox(QWidget *parent) | - | ||||||||||||||||||
407 | : QComboBox(*new QFontComboBoxPrivate, parent) | - | ||||||||||||||||||
408 | { | - | ||||||||||||||||||
409 | Q_D(QFontComboBox); | - | ||||||||||||||||||
410 | d->currentFont = font(); | - | ||||||||||||||||||
411 | setEditable(true); | - | ||||||||||||||||||
412 | - | |||||||||||||||||||
413 | QStringListModel *m = new QStringListModel(this); | - | ||||||||||||||||||
414 | setModel(m); | - | ||||||||||||||||||
415 | setItemDelegate(new QFontFamilyDelegate(this)); | - | ||||||||||||||||||
416 | QListView *lview = qobject_cast<QListView*>(view()); | - | ||||||||||||||||||
417 | if (lview)
| 0 | ||||||||||||||||||
418 | lview->setUniformItemSizes(true); never executed: lview->setUniformItemSizes(true); | 0 | ||||||||||||||||||
419 | setWritingSystem(QFontDatabase::Any); | - | ||||||||||||||||||
420 | - | |||||||||||||||||||
421 | connect(this, SIGNAL(currentIndexChanged(QString)), | - | ||||||||||||||||||
422 | this, SLOT(_q_currentChanged(QString))); | - | ||||||||||||||||||
423 | - | |||||||||||||||||||
424 | connect(qApp, SIGNAL(fontDatabaseChanged()), | - | ||||||||||||||||||
425 | this, SLOT(_q_updateModel())); | - | ||||||||||||||||||
426 | } never executed: end of block | 0 | ||||||||||||||||||
427 | - | |||||||||||||||||||
428 | - | |||||||||||||||||||
429 | /*! | - | ||||||||||||||||||
430 | Destroys the combobox. | - | ||||||||||||||||||
431 | */ | - | ||||||||||||||||||
432 | QFontComboBox::~QFontComboBox() | - | ||||||||||||||||||
433 | { | - | ||||||||||||||||||
434 | } | - | ||||||||||||||||||
435 | - | |||||||||||||||||||
436 | /*! | - | ||||||||||||||||||
437 | \property QFontComboBox::writingSystem | - | ||||||||||||||||||
438 | \brief the writing system that serves as a filter for the combobox | - | ||||||||||||||||||
439 | - | |||||||||||||||||||
440 | If \a script is QFontDatabase::Any (the default), all fonts are | - | ||||||||||||||||||
441 | listed. | - | ||||||||||||||||||
442 | - | |||||||||||||||||||
443 | \sa fontFilters | - | ||||||||||||||||||
444 | */ | - | ||||||||||||||||||
445 | - | |||||||||||||||||||
446 | void QFontComboBox::setWritingSystem(QFontDatabase::WritingSystem script) | - | ||||||||||||||||||
447 | { | - | ||||||||||||||||||
448 | Q_D(QFontComboBox); | - | ||||||||||||||||||
449 | QFontFamilyDelegate *delegate = qobject_cast<QFontFamilyDelegate *>(view()->itemDelegate()); | - | ||||||||||||||||||
450 | if (delegate)
| 0 | ||||||||||||||||||
451 | delegate->writingSystem = script; never executed: delegate->writingSystem = script; | 0 | ||||||||||||||||||
452 | d->_q_updateModel(); | - | ||||||||||||||||||
453 | } never executed: end of block | 0 | ||||||||||||||||||
454 | - | |||||||||||||||||||
455 | QFontDatabase::WritingSystem QFontComboBox::writingSystem() const | - | ||||||||||||||||||
456 | { | - | ||||||||||||||||||
457 | QFontFamilyDelegate *delegate = qobject_cast<QFontFamilyDelegate *>(view()->itemDelegate()); | - | ||||||||||||||||||
458 | if (delegate)
| 0 | ||||||||||||||||||
459 | return delegate->writingSystem; never executed: return delegate->writingSystem; | 0 | ||||||||||||||||||
460 | return QFontDatabase::Any; never executed: return QFontDatabase::Any; | 0 | ||||||||||||||||||
461 | } | - | ||||||||||||||||||
462 | - | |||||||||||||||||||
463 | - | |||||||||||||||||||
464 | /*! | - | ||||||||||||||||||
465 | \enum QFontComboBox::FontFilter | - | ||||||||||||||||||
466 | - | |||||||||||||||||||
467 | This enum can be used to only show certain types of fonts in the font combo box. | - | ||||||||||||||||||
468 | - | |||||||||||||||||||
469 | \value AllFonts Show all fonts | - | ||||||||||||||||||
470 | \value ScalableFonts Show scalable fonts | - | ||||||||||||||||||
471 | \value NonScalableFonts Show non scalable fonts | - | ||||||||||||||||||
472 | \value MonospacedFonts Show monospaced fonts | - | ||||||||||||||||||
473 | \value ProportionalFonts Show proportional fonts | - | ||||||||||||||||||
474 | */ | - | ||||||||||||||||||
475 | - | |||||||||||||||||||
476 | /*! | - | ||||||||||||||||||
477 | \property QFontComboBox::fontFilters | - | ||||||||||||||||||
478 | \brief the filter for the combobox | - | ||||||||||||||||||
479 | - | |||||||||||||||||||
480 | By default, all fonts are listed. | - | ||||||||||||||||||
481 | - | |||||||||||||||||||
482 | \sa writingSystem | - | ||||||||||||||||||
483 | */ | - | ||||||||||||||||||
484 | void QFontComboBox::setFontFilters(FontFilters filters) | - | ||||||||||||||||||
485 | { | - | ||||||||||||||||||
486 | Q_D(QFontComboBox); | - | ||||||||||||||||||
487 | d->filters = filters; | - | ||||||||||||||||||
488 | d->_q_updateModel(); | - | ||||||||||||||||||
489 | } never executed: end of block | 0 | ||||||||||||||||||
490 | - | |||||||||||||||||||
491 | QFontComboBox::FontFilters QFontComboBox::fontFilters() const | - | ||||||||||||||||||
492 | { | - | ||||||||||||||||||
493 | Q_D(const QFontComboBox); | - | ||||||||||||||||||
494 | return d->filters; never executed: return d->filters; | 0 | ||||||||||||||||||
495 | } | - | ||||||||||||||||||
496 | - | |||||||||||||||||||
497 | /*! | - | ||||||||||||||||||
498 | \property QFontComboBox::currentFont | - | ||||||||||||||||||
499 | \brief the currently selected font | - | ||||||||||||||||||
500 | - | |||||||||||||||||||
501 | \sa currentIndex, currentText | - | ||||||||||||||||||
502 | */ | - | ||||||||||||||||||
503 | QFont QFontComboBox::currentFont() const | - | ||||||||||||||||||
504 | { | - | ||||||||||||||||||
505 | Q_D(const QFontComboBox); | - | ||||||||||||||||||
506 | return d->currentFont; never executed: return d->currentFont; | 0 | ||||||||||||||||||
507 | } | - | ||||||||||||||||||
508 | - | |||||||||||||||||||
509 | void QFontComboBox::setCurrentFont(const QFont &font) | - | ||||||||||||||||||
510 | { | - | ||||||||||||||||||
511 | Q_D(QFontComboBox); | - | ||||||||||||||||||
512 | if (font != d->currentFont) {
| 0 | ||||||||||||||||||
513 | d->currentFont = font; | - | ||||||||||||||||||
514 | d->_q_updateModel(); | - | ||||||||||||||||||
515 | if (d->currentFont == font) { //else the signal has already be emitted by _q_updateModel
| 0 | ||||||||||||||||||
516 | emit currentFontChanged(d->currentFont); | - | ||||||||||||||||||
517 | } never executed: end of block | 0 | ||||||||||||||||||
518 | } never executed: end of block | 0 | ||||||||||||||||||
519 | } never executed: end of block | 0 | ||||||||||||||||||
520 | - | |||||||||||||||||||
521 | /*! | - | ||||||||||||||||||
522 | \fn QFontComboBox::currentFontChanged(const QFont &font) | - | ||||||||||||||||||
523 | - | |||||||||||||||||||
524 | This signal is emitted whenever the current font changes, with | - | ||||||||||||||||||
525 | the new \a font. | - | ||||||||||||||||||
526 | - | |||||||||||||||||||
527 | \sa currentFont | - | ||||||||||||||||||
528 | */ | - | ||||||||||||||||||
529 | - | |||||||||||||||||||
530 | /*! | - | ||||||||||||||||||
531 | \reimp | - | ||||||||||||||||||
532 | */ | - | ||||||||||||||||||
533 | bool QFontComboBox::event(QEvent *e) | - | ||||||||||||||||||
534 | { | - | ||||||||||||||||||
535 | if (e->type() == QEvent::Resize) {
| 0 | ||||||||||||||||||
536 | QListView *lview = qobject_cast<QListView*>(view()); | - | ||||||||||||||||||
537 | if (lview) {
| 0 | ||||||||||||||||||
538 | lview->window()->setFixedWidth(qMin(width() * 5 / 3, | - | ||||||||||||||||||
539 | QApplication::desktop()->availableGeometry(lview).width())); | - | ||||||||||||||||||
540 | } never executed: end of block | 0 | ||||||||||||||||||
541 | } never executed: end of block | 0 | ||||||||||||||||||
542 | return QComboBox::event(e); never executed: return QComboBox::event(e); | 0 | ||||||||||||||||||
543 | } | - | ||||||||||||||||||
544 | - | |||||||||||||||||||
545 | /*! | - | ||||||||||||||||||
546 | \reimp | - | ||||||||||||||||||
547 | */ | - | ||||||||||||||||||
548 | QSize QFontComboBox::sizeHint() const | - | ||||||||||||||||||
549 | { | - | ||||||||||||||||||
550 | QSize sz = QComboBox::sizeHint(); | - | ||||||||||||||||||
551 | QFontMetrics fm(font()); | - | ||||||||||||||||||
552 | sz.setWidth(fm.width(QLatin1Char('m'))*14); | - | ||||||||||||||||||
553 | return sz; never executed: return sz; | 0 | ||||||||||||||||||
554 | } | - | ||||||||||||||||||
555 | - | |||||||||||||||||||
556 | QT_END_NAMESPACE | - | ||||||||||||||||||
557 | - | |||||||||||||||||||
558 | #include "qfontcombobox.moc" | - | ||||||||||||||||||
559 | #include "moc_qfontcombobox.cpp" | - | ||||||||||||||||||
560 | - | |||||||||||||||||||
561 | #endif // QT_NO_FONTCOMBOBOX | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |