dialogs/qfontdialog.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/****************************************************************************-
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/****************************************************************************
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtGui module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
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 Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/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 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
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 standard-dialogs -
116 \inmodule QtWidgets -
117 -
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 code/src_gui_dialogs_qfontdialog.cpp 0 -
126 -
127 The dialog can also be used to set a widget's font directly: -
128 \snippet 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 fusion-fontdialog.png A font dialog in the Fusion widget style. -
133 -
134 \sa QFont, QFontInfo, QFontMetrics, QColorDialog, QFileDialog, -
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); -
302 -
303 buttonBox = new QDialogButtonBox(q); -
304 mainGrid->addWidget(buttonBox, 9, 0, 1, 5); -
305 -
306 QPushButton *button -
307 = static_cast<QPushButton *>(buttonBox->addButton(QDialogButtonBox::Ok)); -
308 QObject::connect(buttonBox, SIGNAL(accepted()), q, SLOT(accept())); -
309 button->setDefault(true); -
310 -
311 buttonBox->addButton(QDialogButtonBox::Cancel); -
312 QObject::connect(buttonBox, SIGNAL(rejected()), q, SLOT(reject())); -
313 -
314#if defined(Q_OS_WINCE) -
315 q->resize(180, 120); -
316#else -
317 q->resize(500, 360); -
318#endif // Q_OS_WINCE -
319 -
320 sizeEdit->installEventFilter(q); -
321 familyList->installEventFilter(q); -
322 styleList->installEventFilter(q); -
323 sizeList->installEventFilter(q); -
324 -
325 familyList->setFocus(); -
326 retranslateStrings(); -
327} -
328 -
329/*! -
330 \internal -
331 Destroys the font dialog and frees up its storage. -
332*/ -
333 -
334QFontDialog::~QFontDialog() -
335{ -
336} -
337 -
338/*! -
339 Executes a modal font dialog and returns a font. -
340 -
341 If the user clicks \uicontrol OK, the selected font is returned. If the user -
342 clicks \uicontrol Cancel, the \a initial font is returned. -
343 -
344 The dialog is constructed with the given \a parent and the options specified -
345 in \a options. \a title is shown as the window title of the dialog and \a -
346 initial is the initially selected font. If the \a ok parameter is not-null, -
347 the value it refers to is set to true if the user clicks \uicontrol OK, and set to -
348 false if the user clicks \uicontrol Cancel. -
349 -
350 Examples: -
351 \snippet code/src_gui_dialogs_qfontdialog.cpp 2 -
352 -
353 The dialog can also be used to set a widget's font directly: -
354 \snippet code/src_gui_dialogs_qfontdialog.cpp 3 -
355 In this example, if the user clicks OK the font they chose will be -
356 used, and if they click Cancel the original font is used. -
357 -
358 \warning Do not delete \a parent during the execution of the dialog. -
359 If you want to do this, you should create the dialog -
360 yourself using one of the QFontDialog constructors. -
361*/ -
362QFont QFontDialog::getFont(bool *ok, const QFont &initial, QWidget *parent, const QString &title, -
363 FontDialogOptions options) -
364{ -
365 return QFontDialogPrivate::getFont(ok, initial, parent, title, options); -
366} -
367 -
368/*! -
369 \overload -
370 -
371 Executes a modal font dialog and returns a font. -
372 -
373 If the user clicks \uicontrol OK, the selected font is returned. If the user -
374 clicks \uicontrol Cancel, the Qt default font is returned. -
375 -
376 The dialog is constructed with the given \a parent. -
377 If the \a ok parameter is not-null, the value it refers to is set -
378 to true if the user clicks \uicontrol OK, and false if the user clicks -
379 \uicontrol Cancel. -
380 -
381 Example: -
382 \snippet code/src_gui_dialogs_qfontdialog.cpp 4 -
383 -
384 \warning Do not delete \a parent during the execution of the dialog. -
385 If you want to do this, you should create the dialog -
386 yourself using one of the QFontDialog constructors. -
387*/ -
388QFont QFontDialog::getFont(bool *ok, QWidget *parent) -
389{ -
390 QFont initial; -
391 return QFontDialogPrivate::getFont(ok, initial, parent, QString(), 0); -
392} -
393 -
394QFont QFontDialogPrivate::getFont(bool *ok, const QFont &initial, QWidget *parent, -
395 const QString &title, QFontDialog::FontDialogOptions options) -
396{ -
397 QFontDialog dlg(parent); -
398 dlg.setOptions(options); -
399 dlg.setCurrentFont(initial); -
400 if (!title.isEmpty()) -
401 dlg.setWindowTitle(title); -
402 -
403 int ret = (dlg.exec() || (options & QFontDialog::NoButtons)); -
404 if (ok) -
405 *ok = !!ret; -
406 if (ret) { -
407 return dlg.selectedFont(); -
408 } else { -
409 return initial; -
410 } -
411} -
412 -
413/*! -
414 \internal -
415 An event filter to make the Up, Down, PageUp and PageDown keys work -
416 correctly in the line edits. The source of the event is the object -
417 \a o and the event is \a e. -
418*/ -
419 -
420bool QFontDialog::eventFilter(QObject *o , QEvent *e) -
421{ -
422 Q_D(QFontDialog); -
423 if (e->type() == QEvent::KeyPress) { -
424 QKeyEvent *k = (QKeyEvent *)e; -
425 if (o == d->sizeEdit && -
426 (k->key() == Qt::Key_Up || -
427 k->key() == Qt::Key_Down || -
428 k->key() == Qt::Key_PageUp || -
429 k->key() == Qt::Key_PageDown)) { -
430 -
431 int ci = d->sizeList->currentItem(); -
432 (void)QApplication::sendEvent(d->sizeList, k); -
433 -
434 if (ci != d->sizeList->currentItem() -
435 && style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, this)) -
436 d->sizeEdit->selectAll(); -
437 return true; -
438 } else if ((o == d->familyList || o == d->styleList) && -
439 (k->key() == Qt::Key_Return || k->key() == Qt::Key_Enter)) { -
440 k->accept(); -
441 accept(); -
442 return true; -
443 } -
444 } else if (e->type() == QEvent::FocusIn -
445 && style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, this)) { -
446 if (o == d->familyList) -
447 d->familyEdit->selectAll(); -
448 else if (o == d->styleList) -
449 d->styleEdit->selectAll(); -
450 else if (o == d->sizeList) -
451 d->sizeEdit->selectAll(); -
452 } else if (e->type() == QEvent::MouseButtonPress && o == d->sizeList) { -
453 d->sizeEdit->setFocus(); -
454 } -
455 return QDialog::eventFilter(o, e); -
456} -
457 -
458void QFontDialogPrivate::initHelper(QPlatformDialogHelper *h) -
459{ -
460 QFontDialog *d = q_func(); -
461 QObject::connect(h, SIGNAL(currentFontChanged(QFont)), d, SIGNAL(currentFontChanged(QFont))); -
462 QObject::connect(h, SIGNAL(fontSelected(QFont)), d, SIGNAL(fontSelected(QFont))); -
463 static_cast<QPlatformFontDialogHelper *>(h)->setOptions(options); -
464} -
465 -
466void QFontDialogPrivate::helperPrepareShow(QPlatformDialogHelper *) -
467{ -
468 options->setWindowTitle(q_func()->windowTitle()); -
469} -
470 -
471/* -
472 Updates the contents of the "font family" list box. This -
473 function can be reimplemented if you have special requirements. -
474*/ -
475 -
476void QFontDialogPrivate::updateFamilies() -
477{ -
478 Q_Q(QFontDialog); -
479 -
480 enum match_t { MATCH_NONE = 0, MATCH_LAST_RESORT = 1, MATCH_APP = 2, MATCH_FAMILY = 3 }; -
481 -
482 QStringList familyNames = fdb.families(writingSystem); -
483 -
484 familyList->model()->setStringList(familyNames); -
485 -
486 QString foundryName1, familyName1, foundryName2, familyName2; -
487 int bestFamilyMatch = -1; -
488 match_t bestFamilyType = MATCH_NONE; -
489 -
490 QFont f; -
491 -
492 // ##### do the right thing for a list of family names in the font. -
493 QFontDatabase::parseFontName(family, foundryName1, familyName1); -
494 -
495 QStringList::const_iterator it = familyNames.constBegin(); -
496 int i = 0; -
497 for(; it != familyNames.constEnd(); ++it, ++i) { -
498 QFontDatabase::parseFontName(*it, foundryName2, familyName2); -
499 -
500 //try to match... -
501 if (familyName1 == familyName2) { -
502 bestFamilyType = MATCH_FAMILY; -
503 if (foundryName1 == foundryName2) { -
504 bestFamilyMatch = i; -
505 break; -
506 } -
507 if (bestFamilyMatch < MATCH_FAMILY) -
508 bestFamilyMatch = i; -
509 } -
510 -
511 //and try some fall backs -
512 match_t type = MATCH_NONE; -
513 if (bestFamilyType <= MATCH_NONE && familyName2 == f.lastResortFamily()) -
514 type = MATCH_LAST_RESORT; -
515 if (bestFamilyType <= MATCH_LAST_RESORT && familyName2 == f.family()) -
516 type = MATCH_APP; -
517 // ### add fallback for writingSystem -
518 if (type != MATCH_NONE) { -
519 bestFamilyType = type; -
520 bestFamilyMatch = i; -
521 } -
522 } -
523 -
524 if (i != -1 && bestFamilyType != MATCH_NONE) -
525 familyList->setCurrentItem(bestFamilyMatch); -
526 else -
527 familyList->setCurrentItem(0); -
528 familyEdit->setText(familyList->currentText()); -
529 if (q->style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, q) -
530 && familyList->hasFocus()) -
531 familyEdit->selectAll(); -
532 -
533 updateStyles(); -
534} -
535 -
536/* -
537 Updates the contents of the "font style" list box. This -
538 function can be reimplemented if you have special requirements. -
539*/ -
540void QFontDialogPrivate::updateStyles() -
541{ -
542 Q_Q(QFontDialog); -
543 QStringList styles = fdb.styles(familyList->currentText()); -
544 styleList->model()->setStringList(styles); -
545 -
546 if (styles.isEmpty()) { -
547 styleEdit->clear(); -
548 smoothScalable = false; -
549 } else { -
550 if (!style.isEmpty()) { -
551 bool found = false; -
552 bool first = true; -
553 QString cstyle = style; -
554 -
555 redo: -
556 for (int i = 0; i < (int)styleList->count(); i++) { -
557 if (cstyle == styleList->text(i)) { -
558 styleList->setCurrentItem(i); -
559 found = true; -
560 break; -
561 } -
562 } -
563 if (!found && first) { -
564 if (cstyle.contains(QLatin1String("Italic"))) { -
565 cstyle.replace(QLatin1String("Italic"), QLatin1String("Oblique")); -
566 first = false; -
567 goto redo; -
568 } else if (cstyle.contains(QLatin1String("Oblique"))) { -
569 cstyle.replace(QLatin1String("Oblique"), QLatin1String("Italic")); -
570 first = false; -
571 goto redo; -
572 } -
573 } -
574 if (!found) -
575 styleList->setCurrentItem(0); -
576 } else { -
577 styleList->setCurrentItem(0); -
578 } -
579 -
580 styleEdit->setText(styleList->currentText()); -
581 if (q->style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, q) -
582 && styleList->hasFocus()) -
583 styleEdit->selectAll(); -
584 -
585 smoothScalable = fdb.isSmoothlyScalable(familyList->currentText(), styleList->currentText()); -
586 } -
587 -
588 updateSizes(); -
589} -
590 -
591/*! -
592 \internal -
593 Updates the contents of the "font size" list box. This -
594 function can be reimplemented if you have special requirements. -
595*/ -
596 -
597void QFontDialogPrivate::updateSizes() -
598{ -
599 Q_Q(QFontDialog); -
600 -
601 if (!familyList->currentText().isEmpty()) { -
602 QList<int> sizes = fdb.pointSizes(familyList->currentText(), styleList->currentText()); -
603 -
604 int i = 0; -
605 int current = -1; -
606 QStringList str_sizes; -
607 for(QList<int>::const_iterator it = sizes.constBegin(); it != sizes.constEnd(); ++it) { -
608 str_sizes.append(QString::number(*it)); -
609 if (current == -1 && *it >= size) -
610 current = i; -
611 ++i; -
612 } -
613 sizeList->model()->setStringList(str_sizes); -
614 if (current == -1) { -
615 // we request a size bigger than the ones in the list, select the biggest one -
616 current = sizeList->count() - 1; -
617 } -
618 sizeList->setCurrentItem(current); -
619 -
620 sizeEdit->blockSignals(true); -
621 sizeEdit->setText((smoothScalable ? QString::number(size) : sizeList->currentText())); -
622 if (q->style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, q) -
623 && sizeList->hasFocus()) -
624 sizeEdit->selectAll(); -
625 sizeEdit->blockSignals(false); -
626 } else { -
627 sizeEdit->clear(); -
628 } -
629 -
630 _q_updateSample(); -
631} -
632 -
633void QFontDialogPrivate::_q_updateSample() -
634{ -
635 // compute new font -
636 int pSize = sizeEdit->text().toInt(); -
637 QFont newFont(fdb.font(familyList->currentText(), style, pSize)); -
638 newFont.setStrikeOut(strikeout->isChecked()); -
639 newFont.setUnderline(underline->isChecked()); -
640 -
641 if (familyList->currentText().isEmpty()) -
642 sampleEdit->clear(); -
643 -
644 updateSampleFont(newFont); -
645} -
646 -
647void QFontDialogPrivate::updateSampleFont(const QFont &newFont) -
648{ -
649 Q_Q(QFontDialog); -
650 if (newFont != sampleEdit->font()) { -
651 sampleEdit->setFont(newFont); -
652 emit q->currentFontChanged(newFont); -
653 } -
654} -
655 -
656/*! -
657 \internal -
658*/ -
659void QFontDialogPrivate::_q_writingSystemHighlighted(int index) -
660{ -
661 writingSystem = QFontDatabase::WritingSystem(index); -
662 sampleEdit->setText(fdb.writingSystemSample(writingSystem)); -
663 updateFamilies(); -
664} -
665 -
666/*! -
667 \internal -
668*/ -
669void QFontDialogPrivate::_q_familyHighlighted(int i) -
670{ -
671 Q_Q(QFontDialog); -
672 family = familyList->text(i); -
673 familyEdit->setText(family); -
674 if (q->style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, q) -
675 && familyList->hasFocus()) -
676 familyEdit->selectAll(); -
677 -
678 updateStyles(); -
679} -
680 -
681 -
682/*! -
683 \internal -
684*/ -
685 -
686void QFontDialogPrivate::_q_styleHighlighted(int index) -
687{ -
688 Q_Q(QFontDialog); -
689 QString s = styleList->text(index); -
690 styleEdit->setText(s); -
691 if (q->style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, q) -
692 && styleList->hasFocus()) -
693 styleEdit->selectAll(); -
694 -
695 style = s; -
696 -
697 updateSizes(); -
698} -
699 -
700 -
701/*! -
702 \internal -
703*/ -
704 -
705void QFontDialogPrivate::_q_sizeHighlighted(int index) -
706{ -
707 Q_Q(QFontDialog); -
708 QString s = sizeList->text(index); -
709 sizeEdit->setText(s); -
710 if (q->style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, q) -
711 && sizeEdit->hasFocus()) -
712 sizeEdit->selectAll(); -
713 -
714 size = s.toInt(); -
715 _q_updateSample(); -
716} -
717 -
718/*! -
719 \internal -
720 This slot is called if the user changes the font size. -
721 The size is passed in the \a s argument as a \e string. -
722*/ -
723 -
724void QFontDialogPrivate::_q_sizeChanged(const QString &s) -
725{ -
726 // no need to check if the conversion is valid, since we have an QIntValidator in the size edit -
727 int size = s.toInt(); -
728 if (this->size == size) -
729 return; -
730 -
731 this->size = size; -
732 if (sizeList->count() != 0) { -
733 int i; -
734 for (i = 0; i < sizeList->count() - 1; i++) { -
735 if (sizeList->text(i).toInt() >= this->size) -
736 break; -
737 } -
738 sizeList->blockSignals(true); -
739 sizeList->setCurrentItem(i); -
740 sizeList->blockSignals(false); -
741 } -
742 _q_updateSample(); -
743} -
744 -
745void QFontDialogPrivate::retranslateStrings() -
746{ -
747 familyAccel->setText(QFontDialog::tr("&Font")); -
748 styleAccel->setText(QFontDialog::tr("Font st&yle")); -
749 sizeAccel->setText(QFontDialog::tr("&Size")); -
750 effects->setTitle(QFontDialog::tr("Effects")); -
751 strikeout->setText(QFontDialog::tr("Stri&keout")); -
752 underline->setText(QFontDialog::tr("&Underline")); -
753 sample->setTitle(QFontDialog::tr("Sample")); -
754 writingSystemAccel->setText(QFontDialog::tr("Wr&iting System")); -
755} -
756 -
757/*! -
758 \reimp -
759*/ -
760void QFontDialog::changeEvent(QEvent *e) -
761{ -
762 Q_D(QFontDialog); -
763 if (e->type() == QEvent::LanguageChange) { -
764 d->retranslateStrings(); -
765 } -
766 QDialog::changeEvent(e); -
767} -
768 -
769/*! -
770 \since 4.5 -
771 -
772 \property QFontDialog::currentFont -
773 \brief the current font of the dialog. -
774*/ -
775 -
776/*! -
777 \since 4.5 -
778 -
779 Sets the font highlighted in the QFontDialog to the given \a font. -
780 -
781 \sa selectedFont() -
782*/ -
783void QFontDialog::setCurrentFont(const QFont &font) -
784{ -
785 Q_D(QFontDialog); -
786 d->family = font.family(); -
787 d->style = d->fdb.styleString(font); -
788 d->size = font.pointSize(); -
789 if (d->size == -1) { -
790 QFontInfo fi(font); -
791 d->size = fi.pointSize(); -
792 } -
793 d->strikeout->setChecked(font.strikeOut()); -
794 d->underline->setChecked(font.underline()); -
795 d->updateFamilies(); -
796 if (d->canBeNativeDialog()) { -
797 if (QPlatformFontDialogHelper *helper = d->platformFontDialogHelper()) -
798 helper->setCurrentFont(font); -
799 } -
800} -
801 -
802/*! -
803 \since 4.5 -
804 -
805 Returns the current font. -
806 -
807 \sa selectedFont() -
808*/ -
809QFont QFontDialog::currentFont() const -
810{ -
811 Q_D(const QFontDialog); -
812 if (d->canBeNativeDialog()) { -
813 if (const QPlatformFontDialogHelper *helper = d->platformFontDialogHelper()) -
814 return helper->currentFont(); -
815 } -
816 return d->sampleEdit->font(); -
817} -
818 -
819/*! -
820 Returns the font that the user selected by clicking the \uicontrol{OK} -
821 or equivalent button. -
822 -
823 \note This font is not always the same as the font held by the -
824 \l currentFont property since the user can choose different fonts -
825 before finally selecting the one to use. -
826*/ -
827QFont QFontDialog::selectedFont() const -
828{ -
829 Q_D(const QFontDialog); -
830 return d->selectedFont; -
831} -
832 -
833/*! -
834 \enum QFontDialog::FontDialogOption -
835 \since 4.5 -
836 -
837 This enum specifies various options that affect the look and feel -
838 of a font dialog. -
839 -
840 \value NoButtons Don't display \uicontrol{OK} and \uicontrol{Cancel} buttons. (Useful for "live dialogs".) -
841 \value DontUseNativeDialog Use Qt's standard font dialog on the Mac instead of Apple's -
842 native font panel. (Currently, the native dialog is never used, -
843 but this is likely to change in future Qt releases.) -
844 -
845 \sa options, setOption(), testOption() -
846*/ -
847 -
848/*! -
849 Sets the given \a option to be enabled if \a on is true; -
850 otherwise, clears the given \a option. -
851 -
852 \sa options, testOption() -
853*/ -
854void QFontDialog::setOption(FontDialogOption option, bool on) -
855{ -
856 Q_D(const QFontDialog);-
d->options->setOption(static_cast<QFontDialogOptions::FontDialogOption>(FontDialogOptions previousOptions = options();
executed (the execution status of this line is deduced): const QFontDialog::FontDialogOptions previousOptions = options();
857 if (!(previousOptions & option),) != !on)
partially evaluated: !(previousOptions & option) != !on
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
858 setOptions(previousOptions ^ option);
executed: setOptions(previousOptions ^ option);
Execution Count:1
1
859}
executed: }
Execution Count:1
1
860 -
861/*! -
862 Returns true if the given \a option is enabled; otherwise, returns -
863 false. -
864 -
865 \sa options, setOption() -
866*/ -
867bool QFontDialog::testOption(FontDialogOption option) const -
868{ -
869 Q_D(const QFontDialog); -
870 return d->options->testOption(static_cast<QFontDialogOptions::FontDialogOption>(option)); -
871} -
872 -
873/*! -
874 \property QFontDialog::options -
875 \brief the various options that affect the look and feel of the dialog -
876 \since 4.5 -
877 -
878 By default, all options are disabled. -
879 -
880 Options should be set before showing the dialog. Setting them while the -
881 dialog is visible is not guaranteed to have an immediate effect on the -
882 dialog (depending on the option and on the platform). -
883 -
884 \sa setOption(), testOption() -
885*/ -
886void QFontDialog::setOptions(FontDialogOptions options) -
887{ -
888 Q_D(QFontDialog); -
889 -
890 if (QFontDialog::options() == options) -
891 return; -
892 -
893 d->options->setOptions(QFontDialogOptions::FontDialogOptions(int(options))); -
894 d->buttonBox->setVisible(!(options & NoButtons)); -
895} -
896 -
897QFontDialog::FontDialogOptions QFontDialog::options() const -
898{ -
899 Q_D(const QFontDialog); -
900 return QFontDialog::FontDialogOptions(int(d->options->options())); -
901} -
902 -
903/*! -
904 \since 4.5 -
905 \overload -
906 -
907 Opens the dialog and connects its fontSelected() signal to the slot specified -
908 by \a receiver and \a member. -
909 -
910 The signal will be disconnected from the slot when the dialog is closed. -
911*/ -
912void QFontDialog::open(QObject *receiver, const char *member) -
913{ -
914 Q_D(QFontDialog); -
915 connect(this, SIGNAL(fontSelected(QFont)), receiver, member); -
916 d->receiverToDisconnectOnClose = receiver; -
917 d->memberToDisconnectOnClose = member; -
918 QDialog::open(); -
919} -
920 -
921/*! -
922 \since 4.5 -
923 -
924 \fn void QFontDialog::currentFontChanged(const QFont &font) -
925 -
926 This signal is emitted when the current font is changed. The new font is -
927 specified in \a font. -
928 -
929 The signal is emitted while a user is selecting a font. Ultimately, the -
930 chosen font may differ from the font currently selected. -
931 -
932 \sa currentFont, fontSelected(), selectedFont() -
933*/ -
934 -
935/*! -
936 \since 4.5 -
937 -
938 \fn void QFontDialog::fontSelected(const QFont &font) -
939 -
940 This signal is emitted when a font has been selected. The selected font is -
941 specified in \a font. -
942 -
943 The signal is only emitted when a user has chosen the final font to be -
944 used. It is not emitted while the user is changing the current font in the -
945 font dialog. -
946 -
947 \sa selectedFont(), currentFontChanged(), currentFont -
948*/ -
949 -
950/*! -
951 \reimp -
952*/ -
953void QFontDialog::setVisible(bool visible) -
954{ -
955 if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden) != visible) -
956 return; -
957 Q_D(QFontDialog); -
958 if (d->canBeNativeDialog()) -
959 d->setNativeDialogVisible(visible); -
960 if (d->nativeDialogInUse) { -
961 // Set WA_DontShowOnScreen so that QDialog::setVisible(visible) below -
962 // updates the state correctly, but skips showing the non-native version: -
963 setAttribute(Qt::WA_DontShowOnScreen, true); -
964 } else { -
965 d->nativeDialogInUse = false; -
966 setAttribute(Qt::WA_DontShowOnScreen, false); -
967 } -
968 QDialog::setVisible(visible); -
969} -
970 -
971/*! -
972 Closes the dialog and sets its result code to \a result. If this dialog -
973 is shown with exec(), done() causes the local event loop to finish, -
974 and exec() to return \a result. -
975 -
976 \sa QDialog::done() -
977*/ -
978void QFontDialog::done(int result) -
979{ -
980 Q_D(QFontDialog); -
981 QDialog::done(result); -
982 if (result == Accepted) { -
983 // We check if this is the same font we had before, if so we emit currentFontChanged -
984 QFont selectedFont = currentFont(); -
985 if(selectedFont != d->selectedFont) -
986 emit(currentFontChanged(selectedFont)); -
987 d->selectedFont = selectedFont; -
988 emit fontSelected(d->selectedFont); -
989 } else -
990 d->selectedFont = QFont(); -
991 if (d->receiverToDisconnectOnClose) { -
992 disconnect(this, SIGNAL(fontSelected(QFont)), -
993 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose); -
994 d->receiverToDisconnectOnClose = 0; -
995 } -
996 d->memberToDisconnectOnClose.clear(); -
997} -
998 -
999bool QFontDialogPrivate::canBeNativeDialog() const -
1000{ -
1001 Q_Q(const QFontDialog); -
1002 if (nativeDialogInUse) -
1003 return true; -
1004 if (q->testAttribute(Qt::WA_DontShowOnScreen)) -
1005 return false; -
1006 if (options->options() & QFontDialog::DontUseNativeDialog) -
1007 return false; -
1008 -
1009 QLatin1String staticName(QFontDialog::staticMetaObject.className()); -
1010 QLatin1String dynamicName(q->metaObject()->className()); -
1011 return (staticName == dynamicName); -
1012} -
1013 -
1014QT_END_NAMESPACE -
1015 -
1016#include "qfontdialog.moc" -
1017#include "moc_qfontdialog.cpp" -
1018 -
1019#endif // QT_NO_FONTDIALOG -
1020 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial