source: trunk/src/gui/dialogs/qfontdialog.cpp@ 138

Last change on this file since 138 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 30.8 KB
Line 
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 "qwindowdefs.h"
43
44#ifndef QT_NO_FONTDIALOG
45
46#include "qfontdialog.h"
47#include "qfontdialog_p.h"
48
49#include <qapplication.h>
50#include <qcheckbox.h>
51#include <qcombobox.h>
52#include <qevent.h>
53#include <qfontdatabase.h>
54#include <qgroupbox.h>
55#include <qlabel.h>
56#include <qlayout.h>
57#include <qlineedit.h>
58#include <qpushbutton.h>
59#include <qstyle.h>
60#include <qdialogbuttonbox.h>
61#include <qheaderview.h>
62#include <qlistview.h>
63#include <qstringlistmodel.h>
64#include <qvalidator.h>
65#include <private/qdialog_p.h>
66#include <private/qfont_p.h>
67
68QT_BEGIN_NAMESPACE
69
70class QFontListView : public QListView
71{
72 Q_OBJECT
73public:
74 QFontListView(QWidget *parent);
75 inline QStringListModel *model() const {
76 return static_cast<QStringListModel *>(QListView::model());
77 }
78 inline void setCurrentItem(int item) {
79 QListView::setCurrentIndex(static_cast<QAbstractListModel*>(model())->index(item));
80 }
81 inline int currentItem() const {
82 return QListView::currentIndex().row();
83 }
84 inline int count() const {
85 return model()->rowCount();
86 }
87 inline QString currentText() const {
88 int row = QListView::currentIndex().row();
89 return row < 0 ? QString() : model()->stringList().at(row);
90 }
91 void currentChanged(const QModelIndex &current, const QModelIndex &previous) {
92 QListView::currentChanged(current, previous);
93 if (current.isValid())
94 emit highlighted(current.row());
95 }
96 QString text(int i) const {
97 return model()->stringList().at(i);
98 }
99signals:
100 void highlighted(int);
101};
102
103QFontListView::QFontListView(QWidget *parent)
104 : QListView(parent)
105{
106 setModel(new QStringListModel(parent));
107 setEditTriggers(NoEditTriggers);
108}
109
110static const Qt::WindowFlags DefaultWindowFlags =
111 Qt::Dialog | Qt::WindowSystemMenuHint;
112
113/*!
114 \class QFontDialog
115 \ingroup dialogs
116 \ingroup text
117 \mainclass
118 \brief The QFontDialog class provides a dialog widget for selecting a font.
119
120 A font dialog is created through one of the static getFont()
121 functions.
122
123 Examples:
124
125 \snippet doc/src/snippets/code/src_gui_dialogs_qfontdialog.cpp 0
126
127 The dialog can also be used to set a widget's font directly:
128 \snippet doc/src/snippets/code/src_gui_dialogs_qfontdialog.cpp 1
129 If the user clicks OK the font they chose will be used for myWidget,
130 and if they click Cancel the original font is used.
131
132 \image plastique-fontdialog.png A font dialog in the Plastique widget style.
133
134 \sa QFont, QFontInfo, QFontMetrics, QColorDialog, QFileDialog, QPrintDialog,
135 {Standard Dialogs Example}
136*/
137
138/*!
139 \since 4.5
140
141 Constructs a standard font dialog.
142
143 Use setCurrentFont() to set the initial font attributes.
144
145 The \a parent parameter is passed to the QDialog constructor.
146
147 \sa getFont()
148*/
149QFontDialog::QFontDialog(QWidget *parent)
150 : QDialog(*new QFontDialogPrivate, parent, DefaultWindowFlags)
151{
152 Q_D(QFontDialog);
153 d->init();
154}
155
156/*!
157 \since 4.5
158
159 Constructs a standard font dialog with the given \a parent and specified
160 \a initial color.
161*/
162QFontDialog::QFontDialog(const QFont &initial, QWidget *parent)
163 : QDialog(*new QFontDialogPrivate, parent, DefaultWindowFlags)
164{
165 Q_D(QFontDialog);
166 d->init();
167 setCurrentFont(initial);
168}
169
170void QFontDialogPrivate::init()
171{
172 Q_Q(QFontDialog);
173
174 q->setSizeGripEnabled(true);
175 q->setWindowTitle(QFontDialog::tr("Select Font"));
176
177 // grid
178 familyEdit = new QLineEdit(q);
179 familyEdit->setReadOnly(true);
180 familyList = new QFontListView(q);
181 familyEdit->setFocusProxy(familyList);
182
183 familyAccel = new QLabel(q);
184#ifndef QT_NO_SHORTCUT
185 familyAccel->setBuddy(familyList);
186#endif
187 familyAccel->setIndent(2);
188
189 styleEdit = new QLineEdit(q);
190 styleEdit->setReadOnly(true);
191 styleList = new QFontListView(q);
192 styleEdit->setFocusProxy(styleList);
193
194 styleAccel = new QLabel(q);
195#ifndef QT_NO_SHORTCUT
196 styleAccel->setBuddy(styleList);
197#endif
198 styleAccel->setIndent(2);
199
200 sizeEdit = new QLineEdit(q);
201 sizeEdit->setFocusPolicy(Qt::ClickFocus);
202 QIntValidator *validator = new QIntValidator(1, 512, q);
203 sizeEdit->setValidator(validator);
204 sizeList = new QFontListView(q);
205
206 sizeAccel = new QLabel(q);
207#ifndef QT_NO_SHORTCUT
208 sizeAccel->setBuddy(sizeEdit);
209#endif
210 sizeAccel->setIndent(2);
211
212 // effects box
213 effects = new QGroupBox(q);
214 QVBoxLayout *vbox = new QVBoxLayout(effects);
215 strikeout = new QCheckBox(effects);
216 vbox->addWidget(strikeout);
217 underline = new QCheckBox(effects);
218 vbox->addWidget(underline);
219
220 sample = new QGroupBox(q);
221 QHBoxLayout *hbox = new QHBoxLayout(sample);
222 sampleEdit = new QLineEdit(sample);
223 sampleEdit->setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored));
224 sampleEdit->setAlignment(Qt::AlignCenter);
225 // Note that the sample text is *not* translated with tr(), as the
226 // characters used depend on the charset encoding.
227 sampleEdit->setText(QLatin1String("AaBbYyZz"));
228 hbox->addWidget(sampleEdit);
229
230 writingSystemCombo = new QComboBox(q);
231
232 writingSystemAccel = new QLabel(q);
233#ifndef QT_NO_SHORTCUT
234 writingSystemAccel->setBuddy(writingSystemCombo);
235#endif
236 writingSystemAccel->setIndent(2);
237
238 size = 0;
239 smoothScalable = false;
240
241 QObject::connect(writingSystemCombo, SIGNAL(activated(int)), q, SLOT(_q_writingSystemHighlighted(int)));
242 QObject::connect(familyList, SIGNAL(highlighted(int)), q, SLOT(_q_familyHighlighted(int)));
243 QObject::connect(styleList, SIGNAL(highlighted(int)), q, SLOT(_q_styleHighlighted(int)));
244 QObject::connect(sizeList, SIGNAL(highlighted(int)), q, SLOT(_q_sizeHighlighted(int)));
245 QObject::connect(sizeEdit, SIGNAL(textChanged(QString)), q, SLOT(_q_sizeChanged(QString)));
246
247 QObject::connect(strikeout, SIGNAL(clicked()), q, SLOT(_q_updateSample()));
248 QObject::connect(underline, SIGNAL(clicked()), q, SLOT(_q_updateSample()));
249
250 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
251 QFontDatabase::WritingSystem ws = QFontDatabase::WritingSystem(i);
252 QString writingSystemName = QFontDatabase::writingSystemName(ws);
253 if (writingSystemName.isEmpty())
254 break;
255 writingSystemCombo->addItem(writingSystemName);
256 }
257
258 updateFamilies();
259 if (familyList->count() != 0)
260 familyList->setCurrentItem(0);
261
262 // grid layout
263 QGridLayout *mainGrid = new QGridLayout(q);
264
265 int spacing = mainGrid->spacing();
266 if (spacing >= 0) { // uniform spacing
267 mainGrid->setSpacing(0);
268
269 mainGrid->setColumnMinimumWidth(1, spacing);
270 mainGrid->setColumnMinimumWidth(3, spacing);
271
272 int margin = 0;
273 mainGrid->getContentsMargins(0, 0, 0, &margin);
274
275 mainGrid->setRowMinimumHeight(3, margin);
276 mainGrid->setRowMinimumHeight(6, 2);
277 mainGrid->setRowMinimumHeight(8, margin);
278 }
279
280 mainGrid->addWidget(familyAccel, 0, 0);
281 mainGrid->addWidget(familyEdit, 1, 0);
282 mainGrid->addWidget(familyList, 2, 0);
283
284 mainGrid->addWidget(styleAccel, 0, 2);
285 mainGrid->addWidget(styleEdit, 1, 2);
286 mainGrid->addWidget(styleList, 2, 2);
287
288 mainGrid->addWidget(sizeAccel, 0, 4);
289 mainGrid->addWidget(sizeEdit, 1, 4);
290 mainGrid->addWidget(sizeList, 2, 4);
291
292 mainGrid->setColumnStretch(0, 38);
293 mainGrid->setColumnStretch(2, 24);
294 mainGrid->setColumnStretch(4, 10);
295
296 mainGrid->addWidget(effects, 4, 0);
297
298 mainGrid->addWidget(sample, 4, 2, 4, 3);
299
300 mainGrid->addWidget(writingSystemAccel, 5, 0);
301 mainGrid->addWidget(writingSystemCombo, 7, 0);