1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: QtxFontEdit.cxx
21 // Author: Sergey TELKOV
23 #include "QtxFontEdit.h"
25 #include "QtxComboBox.h"
28 #include <QToolButton>
29 #include <QFontDialog>
30 #include <QFontDatabase>
31 #include <QFontComboBox>
35 \brief The QtxFontEdit class represents a widget for font
36 preference items editing.
38 The font preference item is represented as the drop-down combo box
39 filled with the list of available fonts. Additional controls for
40 modifying font properties ('bold', 'italic', font size, etc) are also
43 Initial font value can be set with setCurrentFont() method. Chosen font
44 can be retrieved with the currentFont() method.
46 Font properties can be set with the setFontSize(), setFontFamily(),
47 setFontScripting() methods and retrieved with fontSize(), fontFamily(),
48 fontScripting() methods.
50 Additional widgets for direct modyfing font properties are available
51 with use of setFeatures() method.
56 \param feat font widget features (ORed QtxFontEdit::Features flags)
57 \param parent parent widget
59 QtxFontEdit::QtxFontEdit( const int feat, QWidget* parent )
69 \param parent parent widget
71 All font widget features are enabled.
73 QtxFontEdit::QtxFontEdit( QWidget* parent )
84 QtxFontEdit::~QtxFontEdit()
89 \brief Get font widget features.
90 \return font widget features (ORed QtxFontEdit::Features flags)
93 int QtxFontEdit::features() const
99 \brief Set font widget features.
100 \param f font widget features (ORed QtxFontEdit::Features flags)
103 void QtxFontEdit::setFeatures( const int f )
105 if ( myFeatures == f )
113 \brief Get currently selected font.
117 QFont QtxFontEdit::currentFont() const
119 QFont fnt( fontFamily(), fontSize() );
121 int script = fontScripting();
122 fnt.setBold( script & Bold );
123 fnt.setItalic( script & Italic );
124 fnt.setUnderline( script & Underline );
125 fnt.setOverline( script & Shadow ); //addVtkFontPref( tr( "LABELS" ), valLblFontGr, "values_labeling_font" );
131 \brief Set currently selected font.
132 \param fnt current font
135 void QtxFontEdit::setCurrentFont( const QFont& fnt )
137 myFamily->blockSignals( true );
138 myCustomFams->blockSignals( true );
139 mySize->blockSignals( true );
140 myB->blockSignals( true );
141 myI->blockSignals( true );
142 myU->blockSignals( true );
144 setFontFamily( fnt.family() );
145 setFontSize( fnt.pointSize() );
146 setFontScripting( ( fnt.bold() ? Bold : 0 ) |
147 ( fnt.italic() ? Italic : 0 ) |
148 ( fnt.underline() ? Underline : 0 ) |
149 ( fnt.overline() ? Shadow : 0 ) );
151 myFamily->blockSignals( false );
152 myCustomFams->blockSignals( false );
153 mySize->blockSignals( false );
154 myB->blockSignals( false );
155 myI->blockSignals( false );
156 myU->blockSignals( false );
158 emit( changed( currentFont() ) );
162 \brief Get selected font family name.
163 \return current font family name
166 QString QtxFontEdit::fontFamily() const
168 if ( myMode == Native )
169 return myFamily->currentFont().family();
171 return myCustomFams->currentText();
175 \brief Get selected font size.
176 \return current font size
179 int QtxFontEdit::fontSize() const
182 int pSize = mySize->currentText().toInt( &ok );
183 return ok ? pSize : 0;
187 \brief Get selected font scripting.
188 \return current font scripting
189 \sa setFontScripting()
191 int QtxFontEdit::fontScripting() const
193 return ( myB->isChecked() ? Bold : 0 ) |
194 ( myI->isChecked() ? Italic : 0 ) |
195 ( myU->isChecked() ? Underline : 0 ) |
196 ( myS->isChecked() ? Shadow : 0 ) ;
200 \brief Set font family name.
201 \param fam new font family name
204 void QtxFontEdit::setFontFamily( const QString& fam )
206 if ( myMode == Native )
208 myFamily->setCurrentFont( QFont( fam ) );
209 onFontChanged( myFamily->currentFont() );
213 myCustomFams->setCurrentIndex( myCustomFams->findText( fam ) );
214 if ( !myCustomFams->signalsBlocked() )
215 emit( changed( currentFont() ) );
220 \brief Set font size.
221 \param fam new font size
224 void QtxFontEdit::setFontSize( const int s )
229 int idx = mySize->findText( QString::number( s ) );
231 mySize->setCurrentIndex( idx );
232 else if ( mySize->isEditable() )
233 mySize->setEditText( QString::number( s ) );
237 \brief Set font scripting.
238 \param fam new font scripting
241 void QtxFontEdit::setFontScripting( const int script )
243 myB->setChecked( script & Bold );
244 myI->setChecked( script & Italic );
245 myU->setChecked( script & Underline );
246 myS->setChecked( script & Shadow );
250 \brief Update widget state
252 void QtxFontEdit::updateState()
254 int feat = features();
256 myFamily->setVisible( ( feat & Family ) && myMode == Native );
257 myCustomFams->setVisible( ( feat & Family ) && myMode == Custom );
258 mySize->setVisible( feat & Size );
259 myB->setVisible( feat & Bold );
260 myI->setVisible( feat & Italic );
261 myU->setVisible( feat & Underline );
262 myS->setVisible( feat & Shadow );
263 myPreview->setVisible( feat & Preview );
265 mySize->setEditable( feat & UserSize );
269 \brief Called when current font is changed.
272 void QtxFontEdit::onFontChanged( const QFont& /*f*/ )
274 bool blocked = mySize->signalsBlocked();
275 mySize->blockSignals( true );
280 QList<int> szList = QFontDatabase().pointSizes( fontFamily() );
282 for ( QList<int>::const_iterator it = szList.begin(); it != szList.end(); ++it )
283 sizes.append( QString::number( *it ) );
284 mySize->addItems( sizes );
288 mySize->blockSignals( blocked );
290 if ( !myFamily->signalsBlocked() )
291 emit( changed( currentFont() ) );
294 void QtxFontEdit::onPropertyChanged()
296 emit( changed( currentFont() ) );
300 \brief Called when "Preview" button is clicked.
303 void QtxFontEdit::onPreview( bool /*on*/ )
306 QFont fnt = QFontDialog::getFont( &ok, currentFont() );
309 setCurrentFont( fnt );
313 \brief Perform internal intialization.
315 void QtxFontEdit::initialize()
317 QHBoxLayout* base = new QHBoxLayout( this );
318 base->setMargin( 0 );
319 base->setSpacing( 5 );
321 base->addWidget( myFamily = new QFontComboBox( this ) );
322 base->addWidget( myCustomFams = new QComboBox( this ) );
323 base->addWidget( mySize = new QtxComboBox( this ) );
324 mySize->setInsertPolicy( QComboBox::NoInsert );
325 mySize->setValidator( new QIntValidator( 1, 250, mySize ) );
327 base->addWidget( myB = new QToolButton( this ) );
328 myB->setText( tr( "B" ) );
329 myB->setCheckable( true );
331 base->addWidget( myI = new QToolButton( this ) );
332 myI->setText( tr( "I" ) );
333 myI->setCheckable( true );
335 base->addWidget( myU = new QToolButton( this ) );
336 myU->setText( tr( "U" ) );
337 myU->setCheckable( true );
339 base->addWidget( myS = new QToolButton( this ) );
340 myS->setText( tr( "S" ) );
341 myS->setCheckable( true );
343 base->addWidget( myPreview = new QToolButton( this ) );
344 myPreview->setText( "..." );
346 myFamily->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
347 myCustomFams->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
349 connect( myPreview, SIGNAL( clicked( bool ) ), this, SLOT( onPreview( bool ) ) );
350 connect( myFamily, SIGNAL( currentFontChanged( const QFont& ) ), this, SLOT( onFontChanged( const QFont& ) ) );
351 connect( mySize, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onPropertyChanged() ) );
352 connect( mySize, SIGNAL( editTextChanged( QString ) ), this, SLOT( onPropertyChanged() ) );
353 connect( myB, SIGNAL( toggled( bool ) ), this, SLOT( onPropertyChanged() ) );
354 connect( myI, SIGNAL( toggled( bool ) ), this, SLOT( onPropertyChanged() ) );
355 connect( myU, SIGNAL( toggled( bool ) ), this, SLOT( onPropertyChanged() ) );
357 myCustomFams->hide();
361 onFontChanged( currentFont() );
365 \brief Specifies whether widget works in Native or Custom mode. Native mode
366 is intended for working with system fonts. Custom mode is intended for
367 working with manually defined set of fonts. Set of custom fonts can be
368 specified with setCustomFonts() method
369 \param mode mode from QtxFontEdit::Mode enumeration
372 void QtxFontEdit::setMode( const int mode )
374 if ( myMode == mode )
379 myFamily->setShown( myMode == Native );
380 myCustomFams->setShown( myMode == Custom );
386 \brief Verifies whether widget works in Native or Custom mode
387 \return Native or Custom mode
390 int QtxFontEdit::mode() const
396 \brief Sets list of custom fonts.
397 <b>This method is intended for working in Custom mode.</b>
398 \param fams list of families
399 \sa fonts(), setMode()
401 void QtxFontEdit::setFonts( const QStringList& fams )
403 QString currFam = myCustomFams->currentText();
405 myCustomFams->clear();
406 myCustomFams->addItems( fams );
408 int ind = myCustomFams->findText( currFam );
410 myCustomFams->setCurrentIndex( ind );
412 setSizes( QList<int>() );
416 \brief Sets list of available font sizes.
417 <b>This method is intended for working in Custom mode.</b> The list of sizes can
418 be empty. In this case system generate listof size automatically from 8 till 72.
419 \param sizes list of sizes
420 \sa sizes(), setMode()
422 void QtxFontEdit::setSizes( const QList<int>& sizes )
424 QString currSize = mySize->currentText();
427 if ( !sizes.isEmpty() )
430 for ( QList<int>::const_iterator it = sizes.begin(); it != sizes.end(); ++it )
431 szList.append( QString::number( *it ) );
432 mySize->addItems( szList );
436 static QStringList defLst;
437 if ( defLst.isEmpty() )
439 QString str( "8 9 10 11 12 14 16 18 20 22 24 26 28 36 48 72" );
440 defLst = str.split( " " );
442 mySize->addItems( defLst );
445 int ind = mySize->findText( currSize );
447 mySize->setCurrentIndex( ind );
451 \brief Gets list of custom fonts
452 \return list of families
453 \sa setFonts(), setMode()
455 QStringList QtxFontEdit::fonts() const
458 for ( int i = 0, n = myCustomFams->count(); i < n; i++ )
459 fams.append( myCustomFams->itemText( i ) );
464 \brief Gets list of custom fonts
465 \return list of families
466 \sa setCustomFonts(), setMode()
468 QList<int> QtxFontEdit::sizes() const
471 for ( int i = 0, n = mySize->count(); i < n; i++ )
472 lst.append( mySize->itemText( i ).toInt() );