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 QtGui module 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 "qfontcombobox.h"
|
---|
43 |
|
---|
44 | #ifndef QT_NO_FONTCOMBOBOX
|
---|
45 |
|
---|
46 | #include <qstringlistmodel.h>
|
---|
47 | #include <qitemdelegate.h>
|
---|
48 | #include <qlistview.h>
|
---|
49 | #include <qpainter.h>
|
---|
50 | #include <qevent.h>
|
---|
51 | #include <qapplication.h>
|
---|
52 | #include <private/qcombobox_p.h>
|
---|
53 | #include <qdebug.h>
|
---|
54 |
|
---|
55 | QT_BEGIN_NAMESPACE
|
---|
56 |
|
---|
57 | static QFontDatabase::WritingSystem writingSystemForFont(const QFont &font, bool *hasLatin)
|
---|
58 | {
|
---|
59 | *hasLatin = true;
|
---|
60 |
|
---|
61 | QList<QFontDatabase::WritingSystem> writingSystems = QFontDatabase().writingSystems(font.family());
|
---|
62 | // qDebug() << font.family() << writingSystems;
|
---|
63 |
|
---|
64 | // this just confuses the algorithm below. Vietnamese is Latin with lots of special chars
|
---|
65 | writingSystems.removeAll(QFontDatabase::Vietnamese);
|
---|
66 |
|
---|
67 | QFontDatabase::WritingSystem system = QFontDatabase::Any;
|
---|
68 |
|
---|
69 | if (!writingSystems.contains(QFontDatabase::Latin)) {
|
---|
70 | *hasLatin = false;
|
---|
71 | // we need to show something
|
---|
72 | if (writingSystems.count())
|
---|
73 | system = writingSystems.last();
|
---|
74 | } else {
|
---|
75 | writingSystems.removeAll(QFontDatabase::Latin);
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (writingSystems.isEmpty())
|
---|
79 | return system;
|
---|
80 |
|
---|
81 | if (writingSystems.count() == 1 && writingSystems.at(0) > QFontDatabase::Cyrillic) {
|
---|
82 | system = writingSystems.at(0);
|
---|
83 | return system;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (writingSystems.count() <= 2
|
---|
87 | && writingSystems.last() > QFontDatabase::Armenian
|
---|
88 | && writingSystems.last() < QFontDatabase::Vietnamese) {
|
---|
89 | system = writingSystems.last();
|
---|
90 | return system;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (writingSystems.count() <= 5
|
---|
94 | && writingSystems.last() >= QFontDatabase::SimplifiedChinese
|
---|
95 | && writingSystems.last() <= QFontDatabase::Korean)
|
---|
96 | system = writingSystems.last();
|
---|
97 |
|
---|
98 | return system;
|
---|
99 | }
|
---|
100 |
|
---|
101 | class QFontFamilyDelegate : public QAbstractItemDelegate
|
---|
102 | {
|
---|
103 | Q_OBJECT
|
---|
104 | public:
|
---|
105 | explicit QFontFamilyDelegate(QObject *parent);
|
---|
106 |
|
---|
107 | // painting
|
---|
108 | void paint(QPainter *painter,
|
---|
109 | const QStyleOptionViewItem &option,
|
---|
110 | const QModelIndex &index) const;
|
---|
111 |
|
---|
112 | QSize sizeHint(const QStyleOptionViewItem &option,
|
---|
113 | const QModelIndex &index) const;
|
---|
114 |
|
---|
115 | QIcon truetype;
|
---|
116 | QIcon bitmap;
|
---|
117 | QFontDatabase::WritingSystem writingSystem;
|
---|
118 | };
|
---|
119 |
|
---|
120 | QFontFamilyDelegate::QFontFamilyDelegate(QObject *parent)
|
---|
121 | : QAbstractItemDelegate(parent)
|
---|
122 | {
|
---|
123 | truetype = QIcon(QLatin1String(":/trolltech/styles/commonstyle/images/fonttruetype-16.png"));
|
---|
124 | bitmap = QIcon(QLatin1String(":/trolltech/styles/commonstyle/images/fontbitmap-16.png"));
|
---|
125 | writingSystem = QFontDatabase::Any;
|
---|
126 | }
|
---|
127 |
|
---|
128 | void QFontFamilyDelegate::paint(QPainter *painter,
|
---|
129 | const QStyleOptionViewItem &option,
|
---|
130 | const QModelIndex &index) const
|
---|
131 | {
|
---|
132 | QString text = index.data(Qt::DisplayRole).toString();
|
---|
133 | QFont font(option.font);
|
---|
134 | font.setPointSize(QFontInfo(font).pointSize() * 3 / 2);
|
---|
135 | QFont font2 = font;
|
---|
136 | font2.setFamily(text);
|
---|
137 |
|
---|
138 | bool hasLatin;
|
---|
139 | QFontDatabase::WritingSystem system = writingSystemForFont(font2, &hasLatin);
|
---|
140 | if (hasLatin)
|
---|
141 | font = font2;
|
---|
142 |
|
---|
143 | QRect r = option.rect;
|
---|
144 |
|
---|
145 | if (option.state & QStyle::State_Selected) {
|
---|
146 | painter->save();
|
---|
147 | painter->setBrush(option.palette.highlight());
|
---|
148 | painter->setPen(Qt::NoPen);
|
---|
149 | painter->drawRect(option.rect);
|
---|
150 | painter->setPen(QPen(option.palette.highlightedText(), 0));
|
---|
151 | }
|
---|
152 |
|
---|
153 | const QIcon *icon = &bitmap;
|
---|
154 | if (QFontDatabase().isSmoothlyScalable(text)) {
|
---|
155 | icon = &truetype;
|
---|
156 | }
|
---|
157 | QSize actualSize = icon->actualSize(r.size());
|
---|
158 |
|
---|
159 | icon->paint(painter, r, Qt::AlignLeft|Qt::AlignVCenter);
|
---|
160 | if (option.direction == Qt::RightToLeft)
|
---|
161 | r.setRight(r.right() - actualSize.width() - 4);
|
---|
162 | else
|
---|
163 | r.setLeft(r.left() + actualSize.width() + 4);
|
---|
164 |
|
---|
165 | QFont old = painter->font();
|
---|
166 | painter->setFont(font);
|
---|
167 | painter->drawText(r, Qt::AlignVCenter|Qt::AlignLeading|Qt::TextSingleLine, text);
|
---|
168 |
|
---|
169 | if (writingSystem != QFontDatabase::Any)
|
---|
170 | system = writingSystem;
|
---|
171 |
|
---|
172 | if (system != QFontDatabase::Any) {
|
---|
173 | int w = painter->fontMetrics().width(text + QLatin1String(" "));
|
---|
174 | painter->setFont(font2);
|
---|
175 | QString sample = QFontDatabase().writingSystemSample(system);
|
---|
176 | if (option.direction == Qt::RightToLeft)
|
---|
177 | r.setRight(r.right() - w);
|
---|
178 | else
|
---|
179 | r.setLeft(r.left() + w);
|
---|
180 | painter->drawText(r, Qt::AlignVCenter|Qt::AlignLeading|Qt::TextSingleLine, sample);
|
---|
181 | }
|
---|
182 | painter->setFont(old);
|
---|
183 |
|
---|
184 | if (option.state & QStyle::State_Selected)
|
---|
185 | painter->restore();
|
---|
186 |
|
---|
187 | }
|
---|
188 |
|
---|
189 | QSize QFontFamilyDelegate::sizeHint(const QStyleOptionViewItem &option,
|
---|
190 | const QModelIndex &index) const
|
---|
191 | {
|
---|
192 | QString text = index.data(Qt::DisplayRole).toString();
|
---|
193 | QFont font(option.font);
|
---|
194 | // font.setFamily(text);
|
---|
195 | font.setPointSize(QFontInfo(font).pointSize() * 3/2);
|
---|
196 | QFontMetrics fontMetrics(font);
|
---|
197 | return QSize(fontMetrics.width(text), fontMetrics.lineSpacing());
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | class QFontComboBoxPrivate : public QComboBoxPrivate
|
---|
202 | {
|
---|
203 | public:
|
---|
204 | inline QFontComboBoxPrivate() { filters = QFontComboBox::AllFonts; }
|
---|
205 |
|
---|
206 | QFontComboBox::FontFilters filters;
|
---|
207 | QFont currentFont;
|
---|
208 |
|
---|
209 | void _q_updateModel();
|
---|
210 | void _q_currentChanged(const QString &);
|
---|
211 |
|
---|
212 | Q_DECLARE_PUBLIC(QFontComboBox)
|
---|
213 | };
|
---|
214 |
|
---|
215 |
|
---|
216 | void QFontComboBoxPrivate::_q_updateModel()
|
---|
217 | {
|
---|
218 | Q_Q(QFontComboBox);
|
---|
219 | const int scalableMask = (QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts);
|
---|
220 | const int spacingMask = (QFontComboBox::ProportionalFonts | QFontComboBox::MonospacedFonts);
|
---|
221 |
|
---|
222 | QStringListModel *m = qobject_cast<QStringListModel *>(q->model());
|
---|
223 | if (!m)
|
---|
224 | return;
|
---|
225 | QFontFamilyDelegate *delegate = qobject_cast<QFontFamilyDelegate *>(q->view()->itemDelegate());
|
---|
226 | QFontDatabase::WritingSystem system = delegate ? delegate->writingSystem : QFontDatabase::Any;
|
---|
227 |
|
---|
228 | QFontDatabase fdb;
|
---|
|
---|