1 | /****************************************************************************
|
---|
2 | ** $Id: qfontdialog.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QFontDialog
|
---|
5 | **
|
---|
6 | ** Created : 970605
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the dialogs module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #include "qwindowdefs.h"
|
---|
39 |
|
---|
40 | #ifndef QT_NO_FONTDIALOG
|
---|
41 |
|
---|
42 | #include "qfontdialog.h"
|
---|
43 |
|
---|
44 | #include "qlineedit.h"
|
---|
45 | #include "qlistbox.h"
|
---|
46 | #include "qpushbutton.h"
|
---|
47 | #include "qcheckbox.h"
|
---|
48 | #include "qcombobox.h"
|
---|
49 | #include "qlayout.h"
|
---|
50 | #include "qvgroupbox.h"
|
---|
51 | #include "qhgroupbox.h"
|
---|
52 | #include "qlabel.h"
|
---|
53 | #include "qapplication.h"
|
---|
54 | #include "qfontdatabase.h"
|
---|
55 | #include "qstyle.h"
|
---|
56 | #include <private/qfontdata_p.h>
|
---|
57 | #include <qvalidator.h>
|
---|
58 |
|
---|
59 | /*!
|
---|
60 | \class QFontDialog qfontdialog.h
|
---|
61 | \ingroup dialogs
|
---|
62 | \mainclass
|
---|
63 | \brief The QFontDialog class provides a dialog widget for selecting a font.
|
---|
64 |
|
---|
65 | The usual way to use this class is to call one of the static convenience
|
---|
66 | functions, e.g. getFont().
|
---|
67 |
|
---|
68 | Examples:
|
---|
69 |
|
---|
70 | \code
|
---|
71 | bool ok;
|
---|
72 | QFont font = QFontDialog::getFont(
|
---|
73 | &ok, QFont( "Helvetica [Cronyx]", 10 ), this );
|
---|
74 | if ( ok ) {
|
---|
75 | // font is set to the font the user selected
|
---|
76 | } else {
|
---|
77 | // the user canceled the dialog; font is set to the initial
|
---|
78 | // value, in this case Helvetica [Cronyx], 10
|
---|
79 | }
|
---|
80 | \endcode
|
---|
81 |
|
---|
82 | The dialog can also be used to set a widget's font directly:
|
---|
83 | \code
|
---|
84 | myWidget.setFont( QFontDialog::getFont( 0, myWidget.font() ) );
|
---|
85 | \endcode
|
---|
86 | If the user clicks OK the font they chose will be used for myWidget,
|
---|
87 | and if they click Cancel the original font is used.
|
---|
88 |
|
---|
89 | \sa QFont QFontInfo QFontMetrics
|
---|
90 |
|
---|
91 | <img src=qfontdlg-w.png>
|
---|
92 | */
|
---|
93 |
|
---|
94 | class QFontDialogPrivate
|
---|
95 | {
|
---|
96 | public:
|
---|
97 | QFontDialogPrivate() : script( QFontPrivate::defaultScript ) {};
|
---|
98 | QLabel * familyAccel;
|
---|
99 | QLineEdit * familyEdit;
|
---|
100 | QListBox * familyList;
|
---|
101 |
|
---|
102 | QLabel * styleAccel;
|
---|
103 | QLineEdit * styleEdit;
|
---|
104 | QListBox * styleList;
|
---|
105 |
|
---|
106 | QLabel * sizeAccel;
|
---|
107 | QLineEdit * sizeEdit;
|
---|
108 | QListBox * sizeList;
|
---|
109 |
|
---|
110 | QVGroupBox * effects;
|
---|
111 | QCheckBox * strikeout;
|
---|
112 | QCheckBox * underline;
|
---|
113 | QComboBox * color;
|
---|
114 |
|
---|
115 | QHGroupBox * sample;
|
---|
116 | QLineEdit * sampleEdit;
|
---|
117 |
|
---|
118 | QLabel * scriptAccel;
|
---|
119 | QComboBox * scriptCombo;
|
---|
120 |
|
---|
121 | QPushButton * ok;
|
---|
122 | QPushButton * cancel;
|
---|
123 |
|
---|
124 | QBoxLayout * buttonLayout;
|
---|
125 | QBoxLayout * effectsLayout;
|
---|
126 | QBoxLayout * sampleLayout;
|
---|
127 | QBoxLayout * sampleEditLayout;
|
---|
128 |
|
---|
129 | QFontDatabase fdb;
|
---|
130 |
|
---|
131 | QString family;
|
---|
132 | QFont::Script script;
|
---|
133 | QString style;
|
---|
134 | int size;
|
---|
135 |
|
---|
136 | bool smoothScalable;
|
---|
137 | };
|
---|
138 |
|
---|
139 |
|
---|
140 | /*!
|
---|
141 | \internal
|
---|
142 | Constructs a standard font dialog.
|
---|
143 |
|
---|
144 | Use setFont() to set the initial font attributes.
|
---|
145 |
|
---|
146 | The \a parent, \a name, \a modal and \a f parameters are passed to
|
---|
147 | the QDialog constructor.
|
---|
148 |
|
---|
149 | \sa getFont()
|
---|
150 | */
|
---|
151 |
|
---|
152 | QFontDialog::QFontDialog( QWidget *parent, const char *name,
|
---|
153 | bool modal, WFlags f )
|
---|
154 | : QDialog( parent, name, modal, f )
|
---|
155 | {
|
---|
156 | setSizeGripEnabled( TRUE );
|
---|
157 | d = new QFontDialogPrivate;
|
---|
158 | // grid
|
---|
159 | d->familyEdit = new QLineEdit( this, "font family I" );
|
---|
160 | d->familyEdit->setReadOnly( TRUE );
|
---|
161 | d->familyList = new QListBox( this, "font family II" );
|
---|
162 | d->familyEdit->setFocusProxy( d->familyList );
|
---|
163 |
|
---|
164 | d->familyAccel
|
---|
165 | = new QLabel( d->familyList, tr("&Font"), this, "family accelerator" );
|
---|
166 | d->familyAccel->setIndent( 2 );
|
---|
167 |
|
---|
168 | d->styleEdit = new QLineEdit( this, "font style I" );
|
---|
169 | d->styleEdit->setReadOnly( TRUE );
|
---|
170 | d->styleList = new QListBox( this, "font style II" );
|
---|
171 | d->styleEdit->setFocusProxy( d->styleList );
|
---|
172 |
|
---|
173 | d->styleAccel
|
---|
174 | = new QLabel( d->styleList, tr("Font st&yle"), this, "style accelerator" );
|
---|
175 | d->styleAccel->setIndent( 2 );
|
---|
176 |
|
---|
177 | d->sizeEdit = new QLineEdit( this, "font size I" );
|
---|
178 | d->sizeEdit->setFocusPolicy( ClickFocus );
|
---|
179 | QIntValidator *validator = new QIntValidator( 1, 512, this );
|
---|
180 | d->sizeEdit->setValidator( validator );
|
---|
181 | d->sizeList = new QListBox( this, "font size II" );
|
---|
182 |
|
---|
183 | d->sizeAccel
|
---|
184 | = new QLabel ( d->sizeEdit, tr("&Size"), this, "size accelerator" );
|
---|
185 | d->sizeAccel->setIndent( 2 );
|
---|
186 |
|
---|
187 | // effects box
|
---|
188 | d->effects = new QVGroupBox( tr("Effects"), this, "font effects" );
|
---|
189 | d->strikeout = new QCheckBox( d->effects, "strikeout on/off" );
|
---|
190 | d->strikeout->setText( tr("Stri&keout") );
|
---|
191 | d->underline = new QCheckBox( d->effects, "underline on/off" );
|
---|
192 | d->underline->setText( tr("&Underline") );
|
---|
193 |
|
---|
194 | d->sample = new QHGroupBox( tr("Sample"), this, "sample text" );
|
---|
195 | d->sampleEdit = new QLineEdit( d->sample, "r/w sample text" );
|
---|
196 | d->sampleEdit->setSizePolicy( QSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored) );
|
---|
197 | d->sampleEdit->setAlignment( AlignCenter );
|
---|
198 | // Note that the sample text is *not* translated with tr(), as the
|
---|
199 | // characters used depend on the charset encoding.
|
---|
200 | d->sampleEdit->setText( "AaBbYyZz" );
|
---|
201 |
|
---|
202 | d->scriptCombo = new QComboBox( FALSE, this, "font encoding" );
|
---|
203 |
|
---|
204 | d->scriptAccel
|
---|
205 | = new QLabel( d->scriptCombo, tr("Scr&ipt"), this,"encoding label");
|
---|
206 | d->scriptAccel->setIndent( 2 );
|
---|
207 |
|
---|
208 | d->size = 0;
|
---|
209 | d->smoothScalable = FALSE;
|
---|
210 |
|
---|
211 | connect( d->scriptCombo, SIGNAL(activated(int)),
|
---|
212 | SLOT(scriptHighlighted(int)) );
|
---|
213 | connect( d->familyList, SIGNAL(highlighted(int)),
|
---|
214 | SLOT(familyHighlighted(int)) );
|
---|
215 | connect( d->styleList, SIGNAL(highlighted(int)),
|
---|
216 | SLOT(styleHighlighted(int)) );
|
---|
217 | connect( d->sizeList, SIGNAL(highlighted(const QString&)),
|
---|
218 | SLOT(sizeHighlighted(const QString&)) );
|
---|
219 | connect( d->sizeEdit, SIGNAL(textChanged(const QString&)),
|
---|
220 | SLOT(sizeChanged(const QString&)) );
|
---|
221 |
|
---|
222 | connect( d->strikeout, SIGNAL(clicked()),
|
---|
223 | SLOT(updateSample()) );
|
---|
224 | connect( d->underline, SIGNAL(clicked()),
|
---|
225 | SLOT(updateSample()) );
|
---|
226 |
|
---|
227 | (void)d->familyList->sizeHint();
|
---|
228 | (void)d->styleList->sizeHint();
|
---|
229 | (void)d->sizeList->sizeHint();
|
---|
230 |
|
---|
231 | for (int i = 0; i < QFont::NScripts; i++) {
|
---|
232 | QString scriptname = QFontDatabase::scriptName((QFont::Script) i);
|
---|
233 | if ( !scriptname.isEmpty() )
|
---|
234 | d->scriptCombo->insertItem( scriptname );
|
---|
235 | }
|
---|
236 |
|
---|
237 | updateFamilies();
|
---|
238 | if ( d->familyList->count() != 0 )
|
---|
239 | d->familyList->setCurrentItem( 0 );
|
---|
240 |
|
---|
241 | // grid layout
|
---|
242 | QGridLayout * mainGrid = new QGridLayout( this, 9, 6, 12, 0 );
|
---|
243 |
|
---|
244 | mainGrid->addWidget( d->familyAccel, 0, 0 );
|
---|
245 | mainGrid->addWidget( d->familyEdit, 1, 0 );
|
---|
246 | mainGrid->addWidget( d->familyList, 2, 0 );
|
---|
247 |
|
---|
248 | mainGrid->addWidget( d->styleAccel, 0, 2 );
|
---|
249 | mainGrid->addWidget( d->styleEdit, 1, 2 );
|
---|
250 | mainGrid->addWidget( d->styleList, 2, 2 );
|
---|
251 |
|
---|
252 | mainGrid->addWidget( d->sizeAccel, 0, 4 );
|
---|
253 | mainGrid->addWidget( d->sizeEdit, 1, 4 );
|
---|
254 | mainGrid->addWidget( d->sizeList, 2, 4 );
|
---|
255 |
|
---|
256 | mainGrid->setColStretch( 0, 38 );
|
---|
257 | mainGrid->setColStretch( 2, 24 );
|
---|
258 | mainGrid->setColStretch( 4, 10 );
|
---|
259 |
|
---|
260 | mainGrid->addColSpacing( 1, 6 );
|
---|
261 | mainGrid->addColSpacing( 3, 6 );
|
---|
262 | mainGrid->addColSpacing( 5, 6 );
|
---|
263 |
|
---|
264 | mainGrid->addRowSpacing( 3, 12 );
|
---|
265 |
|
---|
266 | mainGrid->addWidget( d->effects, 4, 0 );
|
---|
267 |
|
---|
268 | mainGrid->addMultiCellWidget( d->sample, 4, 7, 2, 4 );
|
---|
269 |
|
---|
270 | mainGrid->addWidget( d->scriptAccel, 5, 0 );
|
---|
271 | mainGrid->addRowSpacing( 6, 2 );
|
---|
272 | mainGrid->addWidget( d->scriptCombo, 7, 0 );
|
---|
273 |
|
---|
274 | mainGrid->addRowSpacing( 8, 12 );
|
---|
275 |
|
---|
276 | QHBoxLayout *buttonBox = new QHBoxLayout;
|
---|
277 | mainGrid->addMultiCell( buttonBox, 9, 9, 0, 4 );
|
---|
278 |
|
---|
279 | buttonBox->addStretch( 1 );
|
---|
280 | QString okt = modal ? tr("OK") : tr("Apply");
|
---|
281 | d->ok = new QPushButton( okt, this, "accept font selection" );
|
---|
282 | buttonBox->addWidget( d->ok );
|
---|
283 | if ( modal )
|
---|
284 | connect( d->ok, SIGNAL(clicked()), SLOT(accept()) );
|
---|
285 | d->ok->setDefault( TRUE );
|
---|
286 | d->ok->setFixedWidth( 80 );
|
---|
287 |
|
---|
288 | buttonBox->addSpacing( 12 );
|
---|
289 |
|
---|
290 | QString cancelt = modal ? tr("Cancel") : tr("Close");
|
---|
291 | d->cancel = new QPushButton( cancelt, this, "cancel/close" );
|
---|
292 | buttonBox->addWidget( d->cancel );
|
---|
293 | connect( d->cancel, SIGNAL(clicked()), SLOT(reject()) );
|
---|
294 | d->cancel->setFixedWidth( 80 );
|
---|
295 |
|
---|
296 |
|
---|
297 | resize( 500, 360 );
|
---|
298 |
|
---|
299 | d->sizeEdit->installEventFilter( this );
|
---|
300 | d->familyList->installEventFilter( this );
|
---|
301 | d->styleList->installEventFilter( this );
|
---|
302 | d->sizeList->installEventFilter( this );
|
---|
303 |
|
---|
304 | d->familyList->setFocus();
|
---|
305 | }
|
---|
306 |
|
---|
307 | /*!
|
---|
308 | \internal
|
---|
309 | Destroys the font dialog and frees up its storage.
|
---|
310 | */
|
---|
311 |
|
---|
312 | QFontDialog::~QFontDialog()
|
---|
313 | {
|
---|
314 | delete d;
|
---|
315 | d = 0;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /*!
|
---|
319 | Executes a modal font dialog and returns a font.
|
---|
320 |
|
---|
321 | If the user clicks OK, the selected font is returned. If the user
|
---|
322 | clicks Cancel, the \a initial font is returned.
|
---|
323 |
|
---|
324 | The dialog is called \a name, with the parent \a parent.
|
---|
325 | \a initial is the initially selected font.
|
---|
326 | If the \a ok parameter is not-null, \e *\a ok is set to TRUE if the
|
---|
327 | user clicked OK, and set to FALSE if the user clicked Cancel.
|
---|
328 |
|
---|
329 | This static function is less flexible than the full QFontDialog
|
---|
330 | object, but is convenient and easy to use.
|
---|
331 |
|
---|
332 | Examples:
|
---|
333 | \code
|
---|
334 | bool ok;
|
---|
335 | QFont font = QFontDialog::getFont( &ok, QFont( "Times", 12 ), this );
|
---|
336 | if ( ok ) {
|
---|
337 | // font is set to the font the user selected
|
---|
338 | } else {
|
---|
339 | // the user canceled the dialog; font is set to the initial
|
---|
340 | // value, in this case Times, 12.
|
---|
341 | }
|
---|
342 | \endcode
|
---|
343 |
|
---|
344 | The dialog can also be used to set a widget's font directly:
|
---|
345 | \code
|
---|
346 | myWidget.setFont( QFontDialog::getFont( 0, myWidget.font() ) );
|
---|
347 | \endcode
|
---|
348 | In this example, if the user clicks OK the font they chose will be
|
---|
349 | used, and if they click Cancel the original font is used.
|
---|
350 | */
|
---|
351 | QFont QFontDialog::getFont( bool *ok, const QFont &initial,
|
---|
352 | QWidget *parent, const char* name)
|
---|
353 | {
|
---|
354 | return getFont( ok, &initial, parent, name );
|
---|
355 | }
|
---|
356 |
|
---|
357 | /*!
|
---|
358 | \overload
|
---|
359 |
|
---|
360 | Executes a modal font dialog and returns a font.
|
---|
361 |
|
---|
362 | If the user clicks OK, the selected font is returned. If the user
|
---|
363 | clicks Cancel, the Qt default font is returned.
|
---|
364 |
|
---|
365 | The dialog is called \a name, with parent \a parent.
|
---|
366 | If the \a ok parameter is not-null, \e *\a ok is set to TRUE if the
|
---|
367 | user clicked OK, and FALSE if the user clicked Cancel.
|
---|
368 |
|
---|
369 | This static function is less functional than the full QFontDialog
|
---|
370 | object, but is convenient and easy to use.
|
---|
371 |
|
---|
372 | Example:
|
---|
373 | \code
|
---|
374 | bool ok;
|
---|
375 | QFont font = QFontDialog::getFont( &ok, this );
|
---|
376 | if ( ok ) {
|
---|
377 | // font is set to the font the user selected
|
---|
378 | } else {
|
---|
379 | // the user canceled the dialog; font is set to the default
|
---|
380 | // application font, QApplication::font()
|
---|
381 | }
|
---|
382 | \endcode
|
---|
383 |
|
---|
384 | */
|
---|
385 | QFont QFontDialog::getFont( bool *ok, QWidget *parent,const char* name)
|
---|
386 | {
|
---|
387 | return getFont( ok, 0, parent, name );
|
---|
388 | }
|
---|
389 |
|
---|
390 | QFont QFontDialog::getFont( bool *ok, const QFont *def,
|
---|
391 | QWidget *parent, const char* name)
|
---|
392 | {
|
---|
393 | QFont result;
|
---|
394 | if ( def )
|
---|
395 | result = *def;
|
---|
396 |
|
---|
397 | QFontDialog *dlg = new QFontDialog( parent, name, TRUE );
|
---|
398 |
|
---|
399 | dlg->setFont( ( def ? *def : QFont() ) );
|
---|
400 | #ifndef QT_NO_WIDGET_TOPEXTRA
|
---|
401 | dlg->setCaption( tr("Select Font") );
|
---|
402 | #endif
|
---|
403 |
|
---|
404 | bool res = (dlg->exec() == QDialog::Accepted);
|
---|
405 | if ( res )
|
---|
406 | result = dlg->font();
|
---|
407 | if ( ok )
|
---|
408 | *ok = res;
|
---|
409 | delete dlg;
|
---|
410 | return result;
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 | /*!
|
---|
415 | \internal
|
---|
416 | An event filter to make the Up, Down, PageUp and PageDown keys work
|
---|
417 | correctly in the line edits. The source of the event is the object
|
---|
418 | \a o and the event is \a e.
|
---|
419 | */
|
---|
420 |
|
---|
421 | bool QFontDialog::eventFilter( QObject * o , QEvent * e )
|
---|
422 | {
|
---|
423 | if ( e->type() == QEvent::KeyPress) {
|
---|
424 | QKeyEvent * k = (QKeyEvent *)e;
|
---|
425 | if ( o == d->sizeEdit &&
|
---|
426 | (k->key() == Key_Up ||
|
---|
427 | k->key() == Key_Down ||
|
---|
428 | k->key() == Key_Prior ||
|
---|
429 | k->key() == Key_Next) ) {
|
---|
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, this))
|
---|
436 | d->sizeEdit->selectAll();
|
---|
437 | return TRUE;
|
---|
438 | } else if ( ( o == d->familyList || o == d->styleList ) &&
|
---|
439 | ( k->key() == Key_Return || k->key() == 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, 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 |
|
---|
458 | #ifdef Q_WS_MAC
|
---|
459 | // #define SHOW_FONTS_IN_FAMILIES
|
---|
460 | #endif
|
---|
461 |
|
---|
462 | #ifdef SHOW_FONTS_IN_FAMILIES
|
---|
463 | #include "qpainter.h"
|
---|
464 | #include <sizeedit.h>
|
---|
465 |
|
---|
466 | class QListBoxFontText : public QListBoxText
|
---|
467 | {
|
---|
468 | QFont cfont;
|
---|
469 | public:
|
---|
470 | QListBoxFontText( const QString & text );
|
---|
471 | ~QListBoxFontText() { }
|
---|
472 |
|
---|
473 | int height( const QListBox * ) const;
|
---|
474 | int width( const QListBox * ) const;
|
---|
475 |
|
---|
476 | protected:
|
---|
477 | void paint( QPainter * );
|
---|
478 | };
|
---|
479 |
|
---|
480 | QListBoxFontText::QListBoxFontText( const QString & text )
|
---|
481 | : QListBoxText(text), cfont(text)
|
---|
482 | {
|
---|
483 | }
|
---|
484 |
|
---|
485 | int QListBoxFontText::height( const QListBox * ) const
|
---|
486 | {
|
---|
487 | QFontMetrics fm(cfont);
|
---|
488 | return QMAX( fm.lineSpacing() + 2, QApplication::globalStrut().height() );
|
---|
489 | }
|
---|
490 |
|
---|
491 | int QListBoxFontText::width( const QListBox * ) const
|
---|
492 | {
|
---|
493 | QFontMetrics fm(cfont);
|
---|
494 | return QMAX( fm.width( text() ) + 6, QApplication::globalStrut().width() );
|
---|
495 | }
|
---|
496 |
|
---|
497 | void QListBoxFontText::paint( QPainter *painter )
|
---|
498 | {
|
---|
499 | painter->save();
|
---|
500 | painter->setFont(cfont);
|
---|
501 | QListBoxText::paint(painter);
|
---|
502 | painter->restore();
|
---|
503 | }
|
---|
504 |
|
---|
505 | #endif
|
---|
506 |
|
---|
507 | /*!
|
---|
508 | \internal
|
---|
509 | Updates the contents of the "font family" list box. This
|
---|
510 | function can be reimplemented if you have special requirements.
|
---|
511 | */
|
---|
512 |
|
---|
513 | void QFontDialog::updateFamilies()
|
---|
514 | {
|
---|
515 | d->familyList->blockSignals( TRUE );
|
---|
516 |
|
---|
517 | enum match_t { MATCH_NONE=0, MATCH_LAST_RESORT=1, MATCH_APP=2, MATCH_FALLBACK, MATCH_FAMILY=3 };
|
---|
518 |
|
---|
519 | QStringList familyNames = d->fdb.families(d->script);
|
---|
520 | {
|
---|
521 | // merge the unicode/unknown family list with the above list.
|
---|
522 | QStringList l = d->fdb.families(QFont::Unicode) +
|
---|
523 | d->fdb.families(QFont::UnknownScript);
|
---|
524 | QStringList::ConstIterator it = l.begin(), end = l.end();
|
---|
525 | for (; it != end; ++it) {
|
---|
526 | if (! familyNames.contains(*it))
|
---|
527 | familyNames << *it;
|
---|
528 | }
|
---|
529 | }
|
---|
530 |
|
---|
531 | familyNames.sort();
|
---|
532 |
|
---|
533 | d->familyList->clear();
|
---|
534 | #ifdef SHOW_FONTS_IN_FAMILIES
|
---|
535 | QStringList::Iterator it = familyNames.begin();
|
---|
536 | int idx = 0;
|
---|
537 | for( ; it != familyNames.end() ; ++it )
|
---|
538 | d->familyList->insertItem(new QListBoxFontText(*it), idx++);
|
---|
539 | #else
|
---|
540 | d->familyList->insertStringList( familyNames );
|
---|
541 | #endif
|
---|
542 |
|
---|
543 | QString foundryName1, familyName1, foundryName2, familyName2;
|
---|
544 | int bestFamilyMatch = -1;
|
---|
545 | match_t bestFamilyType = MATCH_NONE;
|
---|
546 |
|
---|
547 | QFont f;
|
---|
548 |
|
---|
549 | // ##### do the right thing for a list of family names in the font.
|
---|
550 | QFontDatabase::parseFontName(d->family, foundryName1, familyName1);
|
---|
551 |
|
---|
552 | QStringList::Iterator it = familyNames.begin();
|
---|
553 | int i = 0;
|
---|
554 | for( ; it != familyNames.end(); ++it, ++i ) {
|
---|
555 |
|
---|
556 | QFontDatabase::parseFontName(*it, foundryName2, familyName2);
|
---|
557 |
|
---|
558 | //try to match..
|
---|
559 | if ( familyName1 == familyName2 ) {
|
---|
560 | bestFamilyType = MATCH_FAMILY;
|
---|
561 | if ( foundryName1 == foundryName2 ) {
|
---|
562 | bestFamilyMatch = i;
|
---|
563 | break;
|
---|
564 | }
|
---|
565 | if ( bestFamilyMatch < MATCH_FAMILY )
|
---|
566 | bestFamilyMatch = i;
|
---|
567 | }
|
---|
568 |
|
---|
569 | //and try some fall backs
|
---|
570 | match_t type = MATCH_NONE;
|
---|
571 | if ( bestFamilyType <= MATCH_NONE && familyName2 == f.lastResortFamily() )
|
---|
572 | type = MATCH_LAST_RESORT;
|
---|
573 | if ( bestFamilyType <= MATCH_LAST_RESORT && familyName2 == f.family() )
|
---|
574 | type = MATCH_APP;
|
---|
575 | // ### add fallback for script
|
---|
576 | if ( type != MATCH_NONE ) {
|
---|
577 | bestFamilyType = type;
|
---|
578 | bestFamilyMatch = i;
|
---|
579 | }
|
---|
580 | }
|
---|
581 |
|
---|
582 | if (i != -1 && bestFamilyType != MATCH_NONE)
|
---|
583 | d->familyList->setCurrentItem(bestFamilyMatch);
|
---|
584 | else
|
---|
585 | d->familyList->setCurrentItem( 0 );
|
---|
586 | d->familyEdit->setText( d->familyList->currentText() );
|
---|
587 | if ( style().styleHint(QStyle::SH_FontDialog_SelectAssociatedText, this) &&
|
---|
588 | d->familyList->hasFocus() )
|
---|
589 | d->familyEdit->selectAll();
|
---|
590 |
|
---|
591 | d->familyList->blockSignals( FALSE );
|
---|
592 | updateStyles();
|
---|
593 | }
|
---|
594 |
|
---|
595 | /*!
|
---|
596 | \internal
|
---|
597 | Updates the contents of the "font style" list box. This
|
---|
598 | function can be reimplemented if you have special requirements.
|
---|
599 | */
|
---|
600 |
|
---|
601 | void QFontDialog::updateStyles()
|
---|
602 | {
|
---|
603 | d->styleList->blockSignals( TRUE );
|
---|
604 |
|
---|
605 | d->styleList->clear();
|
---|
606 |
|
---|
607 | QStringList styles = d->fdb.styles( d->familyList->currentText() );
|
---|
608 |
|
---|
609 | if ( styles.isEmpty() ) {
|
---|
610 | d->styleEdit->clear();
|
---|
611 | d->smoothScalable = FALSE;
|
---|
612 | } else {
|
---|
613 | d->styleList->insertStringList( styles );
|
---|
614 |
|
---|
615 | if ( !d->style.isEmpty() ) {
|
---|
616 | bool found = FALSE;
|
---|
617 | for ( int i = 0 ; i < (int)d->styleList->count() ; i++ ) {
|
---|
618 | if ( d->style == d->styleList->text(i) ) {
|
---|
619 | d->styleList->setCurrentItem( i );
|
---|
620 | found = TRUE;
|
---|
621 | break;
|
---|
622 | }
|
---|
623 | }
|
---|
624 | if ( !found )
|
---|
625 | d->styleList->setCurrentItem( 0 );
|
---|
626 | }
|
---|
627 |
|
---|
628 | d->styleEdit->setText( d->styleList->currentText() );
|
---|
629 | if ( style().styleHint(QStyle::SH_FontDialog_SelectAssociatedText, this) &&
|
---|
630 | d->styleList->hasFocus() )
|
---|
631 | d->styleEdit->selectAll();
|
---|
632 |
|
---|
633 | d->smoothScalable = d->fdb.isSmoothlyScalable( d->familyList->currentText(), d->styleList->currentText() );
|
---|
634 | }
|
---|
635 |
|
---|
636 | d->styleList->blockSignals( FALSE );
|
---|
637 |
|
---|
638 | updateSizes();
|
---|
639 | }
|
---|
640 |
|
---|
641 | /*!
|
---|
642 | \internal
|
---|
643 | Updates the contents of the "font size" list box. This
|
---|
644 | function can be reimplemented if you have special requirements.
|
---|
645 | */
|
---|
646 |
|
---|
647 | void QFontDialog::updateSizes()
|
---|
648 | {
|
---|
649 | d->sizeList->blockSignals( TRUE );
|
---|
650 |
|
---|
651 | d->sizeList->clear();
|
---|
652 |
|
---|
653 | if ( !d->familyList->currentText().isEmpty() ) {
|
---|
654 | QValueList<int> sizes = d->fdb.pointSizes( d->familyList->currentText(), d->styleList->currentText() );
|
---|
655 |
|
---|
656 | int i = 0;
|
---|
657 | bool found = FALSE;
|
---|
658 | for( QValueList<int>::iterator it = sizes.begin() ; it != sizes.end(); ++it ) {
|
---|
659 | d->sizeList->insertItem( QString::number( *it ) );
|
---|
660 | if ( !found && *it >= d->size ) {
|
---|
661 | d->sizeList->setCurrentItem( i );
|
---|
662 | found = TRUE;
|
---|
663 | }
|
---|
664 | ++i;
|
---|
665 | }
|
---|
666 | if ( !found ) {
|
---|
667 | // we request a size bigger than the ones in the list, select the biggest one
|
---|
668 | d->sizeList->setCurrentItem( d->sizeList->count() - 1 );
|
---|
669 | }
|
---|
670 |
|
---|
671 | d->sizeEdit->blockSignals( TRUE );
|
---|
672 | d->sizeEdit->setText( ( d->smoothScalable ? QString::number( d->size ) : d->sizeList->currentText() ) );
|
---|
673 | if ( style().styleHint(QStyle::SH_FontDialog_SelectAssociatedText, this) &&
|
---|
674 | d->sizeList->hasFocus() )
|
---|
675 | d->sizeEdit->selectAll();
|
---|
676 | d->sizeEdit->blockSignals( FALSE );
|
---|
677 | } else {
|
---|
678 | d->sizeEdit->clear();
|
---|
679 | }
|
---|
680 |
|
---|
681 | d->sizeList->blockSignals( FALSE );
|
---|
682 | updateSample();
|
---|
683 | }
|
---|
684 |
|
---|
685 | void QFontDialog::updateSample()
|
---|
686 | {
|
---|
687 | if ( d->familyList->currentText().isEmpty() )
|
---|
688 | d->sampleEdit->clear();
|
---|
689 | else
|
---|
690 | d->sampleEdit->setFont( font() );
|
---|
691 | }
|
---|
692 |
|
---|
693 | /*!
|
---|
694 | \internal
|
---|
695 | */
|
---|
696 | void QFontDialog::scriptHighlighted( int index )
|
---|
697 | {
|
---|
698 | d->script = (QFont::Script)index;
|
---|
699 | d->sampleEdit->setText( d->fdb.scriptSample( d->script ) );
|
---|
700 | updateFamilies();
|
---|
701 | }
|
---|
702 |
|
---|
703 | /*!
|
---|
704 | \internal
|
---|
705 | */
|
---|
706 | void QFontDialog::familyHighlighted( int i )
|
---|
707 | {
|
---|
708 | d->family = d->familyList->text( i );
|
---|
709 | d->familyEdit->setText( d->family );
|
---|
710 | if ( style().styleHint(QStyle::SH_FontDialog_SelectAssociatedText, this) &&
|
---|
711 | d->familyList->hasFocus() )
|
---|
712 | d->familyEdit->selectAll();
|
---|
713 |
|
---|
714 | updateStyles();
|
---|
715 | }
|
---|
716 |
|
---|
717 |
|
---|
718 | /*!
|
---|
719 | \internal
|
---|
720 | */
|
---|
721 |
|
---|
722 | void QFontDialog::styleHighlighted( int index )
|
---|
723 | {
|
---|
724 | QString s = d->styleList->text( index );
|
---|
725 | d->styleEdit->setText( s );
|
---|
726 | if ( style().styleHint(QStyle::SH_FontDialog_SelectAssociatedText, this) &&
|
---|
727 | d->styleList->hasFocus() )
|
---|
728 | d->styleEdit->selectAll();
|
---|
729 |
|
---|
730 | d->style = s;
|
---|
731 |
|
---|
732 | updateSizes();
|
---|
733 | }
|
---|
734 |
|
---|
735 |
|
---|
736 | /*!
|
---|
737 | \internal
|
---|
738 | */
|
---|
739 |
|
---|
740 | void QFontDialog::sizeHighlighted( const QString &s )
|
---|
741 | {
|
---|
742 | d->sizeEdit->setText( s );
|
---|
743 | if ( style().styleHint(QStyle::SH_FontDialog_SelectAssociatedText, this) &&
|
---|
744 | d->sizeEdit->hasFocus() )
|
---|
745 | d->sizeEdit->selectAll();
|
---|
746 |
|
---|
747 | d->size = s.toInt();
|
---|
748 | updateSample();
|
---|
749 | }
|
---|
750 |
|
---|
751 | /*!
|
---|
752 | \internal
|
---|
753 | This slot is called if the user changes the font size.
|
---|
754 | The size is passed in the \a s argument as a \e string.
|
---|
755 | */
|
---|
756 |
|
---|
757 | void QFontDialog::sizeChanged( const QString &s )
|
---|
758 | {
|
---|
759 | // no need to check if the conversion is valid, since we have an QIntValidator in the size edit
|
---|
760 | int size = s.toInt();
|
---|
761 | if ( d->size == size )
|
---|
762 | return;
|
---|
763 |
|
---|
764 | d->size = size;
|
---|
765 | if ( d->sizeList->count() != 0 ) {
|
---|
766 | int i;
|
---|
767 | for ( i = 0 ; i < (int)d->sizeList->count() - 1 ; i++ ) {
|
---|
768 | if ( d->sizeList->text(i).toInt() >= d->size )
|
---|
769 | break;
|
---|
770 | }
|
---|
771 | d->sizeList->blockSignals( TRUE );
|
---|
772 | d->sizeList->setCurrentItem( i );
|
---|
773 | d->sizeList->blockSignals( FALSE );
|
---|
774 | }
|
---|
775 | updateSample();
|
---|
776 | }
|
---|
777 |
|
---|
778 | /*!
|
---|
779 | \internal
|
---|
780 | Sets the font highlighted in the QFontDialog to font \a f.
|
---|
781 |
|
---|
782 | \sa font()
|
---|
783 | */
|
---|
784 |
|
---|
785 | void QFontDialog::setFont( const QFont &f )
|
---|
786 | {
|
---|
787 | d->family = f.family();
|
---|
788 | d->style = d->fdb.styleString( f );
|
---|
789 | d->size = f.pointSize();
|
---|
790 | if ( d->size == -1 ) {
|
---|
791 | QFontInfo fi( f );
|
---|
792 | d->size = fi.pointSize();
|
---|
793 | }
|
---|
794 | d->strikeout->setChecked( f.strikeOut() );
|
---|
795 | d->underline->setChecked( f.underline() );
|
---|
796 |
|
---|
797 | updateFamilies();
|
---|
798 | }
|
---|
799 |
|
---|
800 | /*!
|
---|
801 | \internal
|
---|
802 | Returns the font which the user has chosen.
|
---|
803 |
|
---|
804 | \sa setFont()
|
---|
805 | */
|
---|
806 |
|
---|
807 | QFont QFontDialog::font() const
|
---|
808 | {
|
---|
809 | int pSize = d->sizeEdit->text().toInt();
|
---|
810 |
|
---|
811 | QFont f = d->fdb.font( d->familyList->currentText(), d->style, pSize );
|
---|
812 | f.setStrikeOut( d->strikeout->isChecked() );
|
---|
813 | f.setUnderline( d->underline->isChecked() );
|
---|
814 | return f;
|
---|
815 | }
|
---|
816 |
|
---|
817 | #endif
|
---|