1 // Copyright (C) 2007-2019 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 // VSR 25/08/2016: IPAL53224
138 // blocking signals of QFontComboBox breaks its internal business logic
139 // that prevents correct retrieving of available sizes for the font in case
140 // when some non-existent font is replaced by its closest analogue
141 //myFamily->blockSignals( true );
142 myCustomFams->blockSignals( true );
143 mySize->blockSignals( true );
144 myB->blockSignals( true );
145 myI->blockSignals( true );
146 myU->blockSignals( true );
148 setFontFamily( fnt.family() );
149 setFontSize( fnt.pointSize() );
150 setFontScripting( ( fnt.bold() ? Bold : 0 ) |
151 ( fnt.italic() ? Italic : 0 ) |
152 ( fnt.underline() ? Underline : 0 ) |
153 ( fnt.overline() ? Shadow : 0 ) );
155 //myFamily->blockSignals( false );
156 myCustomFams->blockSignals( false );
157 mySize->blockSignals( false );
158 myB->blockSignals( false );
159 myI->blockSignals( false );
160 myU->blockSignals( false );
162 emit( changed( currentFont() ) );
166 \brief Get selected font family name.
167 \return current font family name
170 QString QtxFontEdit::fontFamily() const
172 if ( myMode == Native )
173 return myFamily->currentFont().family();
175 return myCustomFams->currentText();
179 \brief Get selected font size.
180 \return current font size
183 int QtxFontEdit::fontSize() const
186 int pSize = mySize->currentText().toInt( &ok );
187 return ok ? pSize : 0;
191 \brief Get selected font scripting.
192 \return current font scripting
193 \sa setFontScripting()
195 int QtxFontEdit::fontScripting() const
197 return ( myB->isChecked() ? Bold : 0 ) |
198 ( myI->isChecked() ? Italic : 0 ) |
199 ( myU->isChecked() ? Underline : 0 ) |
200 ( myS->isChecked() ? Shadow : 0 ) ;
204 \brief Set font family name.
205 \param fam new font family name
208 void QtxFontEdit::setFontFamily( const QString& fam )
210 if ( myMode == Native )
212 myFamily->setCurrentFont( QFont( fam ) );
213 onFontChanged( myFamily->currentFont() );
217 myCustomFams->setCurrentIndex( myCustomFams->findText( fam ) );
218 if ( !myCustomFams->signalsBlocked() )
219 emit( changed( currentFont() ) );
224 \brief Set font size.
225 \param fam new font size
228 void QtxFontEdit::setFontSize( const int s )
233 int idx = mySize->findText( QString::number( s ) );
235 mySize->setCurrentIndex( idx );
236 else if ( mySize->isEditable() )
237 mySize->setEditText( QString::number( s ) );
241 \brief Set font scripting.
242 \param fam new font scripting
245 void QtxFontEdit::setFontScripting( const int script )
247 myB->setChecked( script & Bold );
248 myI->setChecked( script & Italic );
249 myU->setChecked( script & Underline );
250 myS->setChecked( script & Shadow );
254 \brief Update widget state
256 void QtxFontEdit::updateState()
258 int feat = features();
260 myFamily->setVisible( ( feat & Family ) && myMode == Native );
261 myCustomFams->setVisible( ( feat & Family ) && myMode == Custom );
262 mySize->setVisible( feat & Size );
263 myB->setVisible( feat & Bold );
264 myI->setVisible( feat & Italic );
265 myU->setVisible( feat & Underline );
266 myS->setVisible( feat & Shadow );
267 myPreview->setVisible( feat & Preview );
269 mySize->setEditable( feat & UserSize );
273 \brief Called when current font is changed.
276 void QtxFontEdit::onFontChanged( const QFont& /*f*/ )
278 bool blocked = mySize->signalsBlocked();
279 mySize->blockSignals( true );
284 QList<int> szList = QFontDatabase().pointSizes( fontFamily() );
286 for ( QList<int>::const_iterator it = szList.begin(); it != szList.end(); ++it )
287 sizes.append( QString::number( *it ) );
288 mySize->addItems( sizes );
292 mySize->blockSignals( blocked );
294 if ( !myFamily->signalsBlocked() )
295 emit( changed( currentFont() ) );
298 void QtxFontEdit::onPropertyChanged()
300 emit( changed( currentFont() ) );
304 \brief Called when "Preview" button is clicked.
307 void QtxFontEdit::onPreview( bool /*on*/ )
310 QFont fnt = QFontDialog::getFont( &ok, currentFont() );
313 setCurrentFont( fnt );
317 \brief Perform internal intialization.
319 void QtxFontEdit::initialize()
321 QHBoxLayout* base = new QHBoxLayout( this );
322 base->setMargin( 0 );
323 base->setSpacing( 5 );
325 base->addWidget( myFamily = new QFontComboBox( this ) );
326 base->addWidget( myCustomFams = new QComboBox( this ) );
327 base->addWidget( mySize = new QtxComboBox( this ) );
328 mySize->setInsertPolicy( QComboBox::NoInsert );
329 mySize->setValidator( new QIntValidator( 1, 250, mySize ) );
331 base->addWidget( myB = new QToolButton( this ) );
332 myB->setText( tr( "B" ) );
333 myB->setCheckable( true );
335 base->addWidget( myI = new QToolButton( this ) );
336 myI->setText( tr( "I" ) );
337 myI->setCheckable( true );
339 base->addWidget( myU = new QToolButton( this ) );
340 myU->setText( tr( "U" ) );
341 myU->setCheckable( true );
343 base->addWidget( myS = new QToolButton( this ) );
344 myS->setText( tr( "S" ) );
345 myS->setCheckable( true );
347 base->addWidget( myPreview = new QToolButton( this ) );
348 myPreview->setText( "..." );
350 myFamily->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
351 myCustomFams->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
353 connect( myPreview, SIGNAL( clicked( bool ) ), this, SLOT( onPreview( bool ) ) );
354 connect( myFamily, SIGNAL( currentFontChanged( const QFont& ) ), this, SLOT( onFontChanged( const QFont& ) ) );
355 connect( mySize, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onPropertyChanged() ) );
356 connect( mySize, SIGNAL( editTextChanged( QString ) ), this, SLOT( onPropertyChanged() ) );
357 connect( myB, SIGNAL( toggled( bool ) ), this, SLOT( onPropertyChanged() ) );
358 connect( myI, SIGNAL( toggled( bool ) ), this, SLOT( onPropertyChanged() ) );
359 connect( myU, SIGNAL( toggled( bool ) ), this, SLOT( onPropertyChanged() ) );
361 myCustomFams->hide();
365 onFontChanged( currentFont() );
369 \brief Specifies whether widget works in Native or Custom mode. Native mode
370 is intended for working with system fonts. Custom mode is intended for
371 working with manually defined set of fonts. Set of custom fonts can be
372 specified with setCustomFonts() method
373 \param mode mode from QtxFontEdit::Mode enumeration
376 void QtxFontEdit::setMode( const int mode )
378 if ( myMode == mode )
383 myFamily->setVisible( myMode == Native );
384 myCustomFams->setVisible( myMode == Custom );
390 \brief Verifies whether widget works in Native or Custom mode
391 \return Native or Custom mode
394 int QtxFontEdit::mode() const
400 \brief Sets list of custom fonts.
401 <b>This method is intended for working in Custom mode.</b>
402 \param fams list of families
403 \sa fonts(), setMode()
405 void QtxFontEdit::setFonts( const QStringList& fams )
407 QString currFam = myCustomFams->currentText();
409 myCustomFams->clear();
410 myCustomFams->addItems( fams );
412 int ind = myCustomFams->findText( currFam );
414 myCustomFams->setCurrentIndex( ind );
416 setSizes( QList<int>() );
420 \brief Sets list of available font sizes.
421 <b>This method is intended for working in Custom mode.</b> The list of sizes can
422 be empty. In this case system generate listof size automatically from 8 till 72.
423 \param sizes list of sizes
424 \sa sizes(), setMode()
426 void QtxFontEdit::setSizes( const QList<int>& sizes )
428 QString currSize = mySize->currentText();
431 if ( !sizes.isEmpty() )
434 for ( QList<int>::const_iterator it = sizes.begin(); it != sizes.end(); ++it )
435 szList.append( QString::number( *it ) );
436 mySize->addItems( szList );
440 static QStringList defLst;
441 if ( defLst.isEmpty() )
443 QString str( "8 9 10 11 12 14 16 18 20 22 24 26 28 36 48 72" );
444 defLst = str.split( " " );
446 mySize->addItems( defLst );
449 int ind = mySize->findText( currSize );
451 mySize->setCurrentIndex( ind );
455 \brief Gets list of custom fonts
456 \return list of families
457 \sa setFonts(), setMode()
459 QStringList QtxFontEdit::fonts() const
462 for ( int i = 0, n = myCustomFams->count(); i < n; i++ )
463 fams.append( myCustomFams->itemText( i ) );
468 \brief Gets list of custom fonts
469 \return list of families
470 \sa setCustomFonts(), setMode()
472 QList<int> QtxFontEdit::sizes() const
475 for ( int i = 0, n = mySize->count(); i < n; i++ )
476 lst.append( mySize->itemText( i ).toInt() );