• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • fonts
kfontrequester.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 Nadeem Hasan <nhasan@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kfontrequester.h"
21#include "fonthelpers_p.h"
22
23#include <QtGui/QLabel>
24#include <QtGui/QPushButton>
25#include <QtGui/QLayout>
26#include <QtGui/QFontDatabase>
27
28#include <kfontdialog.h>
29#include <klocale.h>
30
31#include <cmath>
32
33// Determine if the font with given properties is available on the system,
34// otherwise find and return the best fitting combination.
35static QFont nearestExistingFont (const QFont &font)
36{
37 QFontDatabase dbase;
38
39 // Initialize font data accoring to given font object.
40 QString family = font.family();
41 QString style = dbase.styleString(font);
42 qreal size = font.pointSizeF();
43
44 // Check if the family exists.
45 const QStringList families = dbase.families();
46 if (!families.contains(family)) {
47 // Chose another family.
48 family = families.count() ? families[0] : "fixed";
49 // TODO: Try to find nearest match?
50 }
51
52 // Check if the family has the requested style.
53 // Easiest by piping it through font selection in the database.
54 QString retStyle = dbase.styleString(dbase.font(family, style, 10));
55 style = retStyle;
56
57 // Check if the family has the requested size.
58 // Only for bitmap fonts.
59 if (!dbase.isSmoothlyScalable(family, style)) {
60 QList<int> sizes = dbase.smoothSizes(family, style);
61 if (!sizes.contains(size)) {
62 // Find nearest available size.
63 int mindiff = 1000;
64 int refsize = size;
65 foreach (int lsize, sizes) {
66 int diff = qAbs(refsize - lsize);
67 if (mindiff > diff) {
68 mindiff = diff;
69 size = lsize;
70 }
71 }
72 }
73 }
74
75 // Select the font with confirmed properties.
76 QFont result = dbase.font(family, style, int(size));
77 if (dbase.isSmoothlyScalable(family, style) && result.pointSize() == floor(size)) {
78 result.setPointSizeF(size);
79 }
80 return result;
81}
82
83class KFontRequester::KFontRequesterPrivate
84{
85public:
86 KFontRequesterPrivate(KFontRequester *q): q(q) {}
87
88 void displaySampleText();
89 void setToolTip();
90
91 void _k_buttonClicked();
92
93 KFontRequester *q;
94 bool m_onlyFixed;
95 QString m_sampleText, m_title;
96 QLabel *m_sampleLabel;
97 QPushButton *m_button;
98 QFont m_selFont;
99};
100
101KFontRequester::KFontRequester( QWidget *parent, bool onlyFixed )
102 : QWidget( parent ), d(new KFontRequesterPrivate(this))
103{
104 d->m_onlyFixed = onlyFixed;
105
106 QHBoxLayout *layout = new QHBoxLayout( this );
107 layout->setMargin( 0 );
108
109 d->m_sampleLabel = new QLabel( this );
110 d->m_button = new QPushButton( i18n( "Choose..." ), this );
111
112 d->m_sampleLabel->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
113 setFocusProxy( d->m_button );
114
115 layout->addWidget( d->m_sampleLabel, 1 );
116 layout->addWidget( d->m_button );
117
118 connect( d->m_button, SIGNAL(clicked()), SLOT(_k_buttonClicked()) );
119
120 d->displaySampleText();
121 d->setToolTip();
122}
123
124KFontRequester::~KFontRequester()
125{
126 delete d;
127}
128
129QFont KFontRequester::font() const
130{
131 return d->m_selFont;
132}
133
134bool KFontRequester::isFixedOnly() const
135{
136 return d->m_onlyFixed;
137}
138
139QString KFontRequester::sampleText() const
140{
141 return d->m_sampleText;
142}
143
144QString KFontRequester::title() const
145{
146 return d->m_title;
147}
148
149QLabel *KFontRequester::label() const
150{
151 return d->m_sampleLabel;
152}
153
154QPushButton *KFontRequester::button() const
155{
156 return d->m_button;
157}
158
159void KFontRequester::setFont( const QFont &font, bool onlyFixed )
160{
161 d->m_selFont = nearestExistingFont(font);
162 d->m_onlyFixed = onlyFixed;
163
164 d->displaySampleText();
165 emit fontSelected( d->m_selFont );
166}
167
168void KFontRequester::setSampleText( const QString &text )
169{
170 d->m_sampleText = text;
171 d->displaySampleText();
172}
173
174void KFontRequester::setTitle( const QString &title )
175{
176 d->m_title = title;
177 d->setToolTip();
178}
179
180void KFontRequester::KFontRequesterPrivate::_k_buttonClicked()
181{
182 KFontChooser::DisplayFlags flags = KFontChooser::NoDisplayFlags;
183 if ( m_onlyFixed ) {
184 flags |= KFontChooser::FixedFontsOnly;
185 }
186
187 int result = KFontDialog::getFont( m_selFont, flags, q->parentWidget() );
188
189 if ( result == KDialog::Accepted )
190 {
191 displaySampleText();
192 emit q->fontSelected( m_selFont );
193 }
194}
195
196void KFontRequester::KFontRequesterPrivate::displaySampleText()
197{
198 m_sampleLabel->setFont( m_selFont );
199
200 qreal size = m_selFont.pointSizeF();
201 if(size == -1)
202 size = m_selFont.pixelSize();
203
204 if ( m_sampleText.isEmpty() ) {
205 QString family = translateFontName(m_selFont.family());
206 m_sampleLabel->setText( QString( "%1 %2" ).arg( family ).arg( KGlobal::locale()->formatNumber( size, (size == floor(size)) ? 0 : 1 ) ) );
207 }
208 else {
209 m_sampleLabel->setText( m_sampleText );
210 }
211}
212
213void KFontRequester::KFontRequesterPrivate::setToolTip()
214{
215 m_button->setToolTip( i18n( "Click to select a font" ) );
216
217 m_sampleLabel->setToolTip( QString() );
218 m_sampleLabel->setWhatsThis(QString());
219
220 if ( m_title.isNull() )
221 {
222 m_sampleLabel->setToolTip( i18n( "Preview of the selected font" ) );
223 m_sampleLabel->setWhatsThis( i18n( "This is a preview of the selected font. You can change it"
224 " by clicking the \"Choose...\" button." ) );
225 }
226 else
227 {
228 m_sampleLabel->setToolTip( i18n( "Preview of the \"%1\" font" , m_title ) );
229 m_sampleLabel->setWhatsThis( i18n( "This is a preview of the \"%1\" font. You can change it"
230 " by clicking the \"Choose...\" button." , m_title ) );
231 }
232}
233
234#include "kfontrequester.moc"
235
236/* vim: et sw=2 ts=2
237*/
KFontChooser::FixedFontsOnly
@ FixedFontsOnly
Definition: kfontchooser.h:82
KFontChooser::NoDisplayFlags
@ NoDisplayFlags
Definition: kfontchooser.h:81
KFontDialog::getFont
static int getFont(QFont &theFont, const KFontChooser::DisplayFlags &flags=KFontChooser::NoDisplayFlags, QWidget *parent=0L, Qt::CheckState *sizeIsRelativeState=0L)
Creates a modal font dialog, lets the user choose a font, and returns when the dialog is closed.
Definition: kfontdialog.cpp:136
KFontRequester
This class provides a widget with a lineedit and a button, which invokes a font dialog (KFontDialog).
Definition: kfontrequester.h:45
KFontRequester::setFont
virtual void setFont(const QFont &font, bool onlyFixed=false)
Sets the currently selected font in the requester.
Definition: kfontrequester.cpp:159
KFontRequester::~KFontRequester
~KFontRequester()
Definition: kfontrequester.cpp:124
KFontRequester::sampleText
QString sampleText
Definition: kfontrequester.h:49
KFontRequester::isFixedOnly
bool isFixedOnly() const
Definition: kfontrequester.cpp:134
KFontRequester::label
QLabel * label() const
Definition: kfontrequester.cpp:149
KFontRequester::setTitle
virtual void setTitle(const QString &title)
Set the title for the widget that will be used in the tooltip and what's this text.
Definition: kfontrequester.cpp:174
KFontRequester::title
QString title
Definition: kfontrequester.h:48
KFontRequester::button
QPushButton * button() const
Definition: kfontrequester.cpp:154
KFontRequester::fontSelected
void fontSelected(const QFont &font)
Emitted when a new font has been selected in the underlying dialog.
KFontRequester::setSampleText
virtual void setSampleText(const QString &text)
Sets the sample text.
Definition: kfontrequester.cpp:168
KFontRequester::KFontRequester
KFontRequester(QWidget *parent=0L, bool onlyFixed=false)
Constructs a font requester widget.
Definition: kfontrequester.cpp:101
KFontRequester::font
QFont font
Definition: kfontrequester.h:50
QLabel
QList
QPushButton
QWidget
translateFontName
QString translateFontName(const QString &name)
Definition: fonthelpers.cpp:64
kfontdialog.h
nearestExistingFont
static QFont nearestExistingFont(const QFont &font)
Definition: kfontrequester.cpp:35
kfontrequester.h
klocale.h
i18n
QString i18n(const char *text)
KGlobal::locale
KLocale * locale()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal