Salome HOME
30cacf8dbbc810cc21a24bc76aa7762830d5ccd2
[modules/gui.git] / src / Qtx / QtxFontEdit.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File:      QtxFontEdit.cxx
23 // Author:    Sergey TELKOV
24 //
25 #include "QtxFontEdit.h"
26
27 #include "QtxComboBox.h"
28
29 #include <QLayout>
30 #include <QToolButton>
31 #include <QFontDialog>
32 #include <QFontDatabase>
33 #include <QFontComboBox>
34
35 /*!
36   \class QtxFontEdit
37   \brief The QtxFontEdit class represents a widget for font
38   preference items editing.
39
40   The font preference item is represented as the drop-down combo box 
41   filled with the list of available fonts. Additional controls for
42   modifying font properties ('bold', 'italic', font size, etc) are also
43   available for use.
44
45   Initial font value can be set with setCurrentFont() method. Chosen font
46   can be retrieved with the currentFont() method.
47
48   Font properties can be set with the setFontSize(), setFontFamily(), 
49   setFontScripting() methods and retrieved with fontSize(), fontFamily(), 
50   fontScripting() methods.
51
52   Additional widgets for direct modyfing font properties are available
53   with use of setFeatures() method.
54 */
55
56 /*!
57   \brief Constructor
58   \param feat font widget features (ORed QtxFontEdit::Features flags)
59   \param parent parent widget
60 */
61 QtxFontEdit::QtxFontEdit( const int feat, QWidget* parent )
62 : QFrame( parent ),
63   myFeatures( feat ),
64   myMode( Native )
65 {
66   initialize();
67 }
68
69 /*!
70   \brief Constructor
71   \param parent parent widget
72
73   All font widget features are enabled.
74 */
75 QtxFontEdit::QtxFontEdit( QWidget* parent )
76 : QFrame( parent ),
77   myFeatures( All )
78 {
79   initialize();
80 }
81
82 /*!
83   \brief Destructor
84 */
85 QtxFontEdit::~QtxFontEdit()
86 {
87 }
88
89 /*!
90   \brief Get font widget features.
91   \return font widget features (ORed QtxFontEdit::Features flags)
92   \sa setFeatures()
93 */
94 int QtxFontEdit::features() const
95 {
96   return myFeatures;
97 }
98
99 /*!
100   \brief Set font widget features.
101   \param f font widget features (ORed QtxFontEdit::Features flags)
102   \sa features()
103 */
104 void QtxFontEdit::setFeatures( const int f )
105 {
106   if ( myFeatures == f )
107     return;
108
109   myFeatures = f;
110   updateState();
111 }
112
113 /*!
114   \brief Get currently selected font.
115   \return current font
116   \sa setCurrentFont()
117 */
118 QFont QtxFontEdit::currentFont() const
119 {
120   QFont fnt( fontFamily(), fontSize() );
121
122   int script = fontScripting();
123   fnt.setBold( script & Bold );
124   fnt.setItalic( script & Italic );
125   fnt.setUnderline( script & Underline );
126   fnt.setOverline( script & Shadow ); //addVtkFontPref( tr( "LABELS" ), valLblFontGr, "values_labeling_font" );
127
128   return fnt;
129 }
130
131 /*!
132   \brief Set currently selected font.
133   \param fnt current font
134   \sa currentFont()
135 */
136 void QtxFontEdit::setCurrentFont( const QFont& fnt )
137 {
138   myFamily->blockSignals( true );
139   myCustomFams->blockSignals( true );
140   mySize->blockSignals( true );
141   myB->blockSignals( true );
142   myI->blockSignals( true );
143   myU->blockSignals( true );
144   
145   setFontFamily( fnt.family() );
146   setFontSize( fnt.pointSize() );
147   setFontScripting( ( fnt.bold() ? Bold : 0 ) |
148                     ( fnt.italic() ? Italic : 0 ) |
149                     ( fnt.underline() ? Underline : 0 ) | 
150                     ( fnt.overline() ? Shadow : 0 ) );
151
152   myFamily->blockSignals( false );
153   myCustomFams->blockSignals( false );
154   mySize->blockSignals( false );
155   myB->blockSignals( false );
156   myI->blockSignals( false );
157   myU->blockSignals( false );
158
159   emit( changed( currentFont() ) );
160 }
161
162 /*!
163   \brief Get selected font family name.
164   \return current font family name
165   \sa setFontFamily()
166 */
167 QString QtxFontEdit::fontFamily() const
168 {
169   if ( myMode == Native )
170     return myFamily->currentFont().family();
171   else
172     return myCustomFams->currentText();
173 }
174
175 /*!
176   \brief Get selected font size.
177   \return current font size
178   \sa setFontSize()
179 */
180 int QtxFontEdit::fontSize() const
181 {
182   bool ok;
183   int pSize = mySize->currentText().toInt( &ok );
184   return ok ? pSize : 0;
185 }
186
187 /*!
188   \brief Get selected font scripting.
189   \return current font scripting
190   \sa setFontScripting()
191 */
192 int QtxFontEdit::fontScripting() const
193 {
194   return ( myB->isChecked() ? Bold : 0 ) |
195          ( myI->isChecked() ? Italic : 0 ) |
196          ( myU->isChecked() ? Underline : 0 ) |
197          ( myS->isChecked() ? Shadow : 0 ) ;
198 }
199
200 /*!
201   \brief Set font family name.
202   \param fam new font family name
203   \sa fontFamily()
204 */
205 void QtxFontEdit::setFontFamily( const QString& fam )
206 {
207   if ( myMode == Native )
208   {
209     myFamily->setCurrentFont( QFont( fam ) );
210     onFontChanged( myFamily->currentFont() );  
211   }
212   else 
213   {
214     myCustomFams->setCurrentIndex( myCustomFams->findText( fam ) );
215     if ( !myCustomFams->signalsBlocked() )
216       emit( changed( currentFont() ) );
217   }
218 }
219
220 /*!
221   \brief Set font size.
222   \param fam new font size
223   \sa fontSize()
224 */
225 void QtxFontEdit::setFontSize( const int s )
226 {
227   if ( s <= 0 )
228     return;
229
230   int idx = mySize->findText( QString::number( s ) );
231   if ( idx != -1 )
232     mySize->setCurrentIndex( idx );
233   else if ( mySize->isEditable() )
234     mySize->setEditText( QString::number( s ) );
235 }
236
237 /*!
238   \brief Set font scripting.
239   \param fam new font scripting
240   \sa fontScripting()
241 */
242 void QtxFontEdit::setFontScripting( const int script )
243 {
244   myB->setChecked( script & Bold );
245   myI->setChecked( script & Italic );
246   myU->setChecked( script & Underline );
247   myS->setChecked( script & Shadow );
248 }
249
250 /*!
251   \brief Update widget state
252 */
253 void QtxFontEdit::updateState()
254 {
255   int feat = features();
256
257   myFamily->setVisible( ( feat & Family ) && myMode == Native );
258   myCustomFams->setVisible( ( feat & Family ) && myMode == Custom );
259   mySize->setVisible( feat & Size );
260   myB->setVisible( feat & Bold );
261   myI->setVisible( feat & Italic );
262   myU->setVisible( feat & Underline );
263   myS->setVisible( feat & Shadow );
264   myPreview->setVisible( feat & Preview );
265
266   mySize->setEditable( feat & UserSize );
267 }
268
269 /*!
270   \brief Called when current font is changed.
271   \param f (not used)
272 */
273 void QtxFontEdit::onFontChanged( const QFont& /*f*/ )
274 {
275   bool blocked = mySize->signalsBlocked();
276   mySize->blockSignals( true );
277
278   int s = fontSize();
279   mySize->clear();
280
281   QList<int> szList = QFontDatabase().pointSizes( fontFamily() );
282   QStringList sizes;
283   for ( QList<int>::const_iterator it = szList.begin(); it != szList.end(); ++it )
284     sizes.append( QString::number( *it ) );
285   mySize->addItems( sizes );
286
287   setFontSize( s );
288
289   mySize->blockSignals( blocked );
290
291   if ( !myFamily->signalsBlocked() )
292     emit( changed( currentFont() ) );
293 }
294
295 void QtxFontEdit::onPropertyChanged()
296 {
297   emit( changed( currentFont() ) );
298 }
299
300 /*!
301   \brief Called when "Preview" button is clicked.
302   \param on (not used)
303 */
304 void QtxFontEdit::onPreview( bool /*on*/ )
305 {
306   bool ok;
307   QFont fnt = QFontDialog::getFont( &ok, currentFont() );
308
309   if ( ok )
310     setCurrentFont( fnt );
311 }
312
313 /*
314   \brief Perform internal intialization.
315 */
316 void QtxFontEdit::initialize()
317 {
318   QHBoxLayout* base = new QHBoxLayout( this );
319   base->setMargin( 0 );
320   base->setSpacing( 5 );
321
322   base->addWidget( myFamily = new QFontComboBox( this ) );
323   base->addWidget( myCustomFams = new QComboBox( this ) );
324   base->addWidget( mySize = new QtxComboBox( this ) );
325   mySize->setInsertPolicy( QComboBox::NoInsert );
326   mySize->setValidator( new QIntValidator( 1, 250, mySize ) );
327
328   base->addWidget( myB = new QToolButton( this ) );
329   myB->setText( tr( "B" ) );
330   myB->setCheckable( true );
331
332   base->addWidget( myI = new QToolButton( this ) );
333   myI->setText( tr( "I" ) );
334   myI->setCheckable( true );
335
336   base->addWidget( myU = new QToolButton( this ) );
337   myU->setText( tr( "U" ) );
338   myU->setCheckable( true );
339
340   base->addWidget( myS = new QToolButton( this ) );
341   myS->setText( tr( "S" ) );
342   myS->setCheckable( true );
343
344   base->addWidget( myPreview = new QToolButton( this ) );
345   myPreview->setText( "..." );
346
347   myFamily->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
348   myCustomFams->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
349
350   connect( myPreview, SIGNAL( clicked( bool ) ),                    this, SLOT( onPreview( bool ) ) );
351   connect( myFamily,  SIGNAL( currentFontChanged( const QFont& ) ), this, SLOT( onFontChanged( const QFont& ) ) );
352   connect( mySize,    SIGNAL( currentIndexChanged( int ) ),         this, SLOT( onPropertyChanged() ) );
353   connect( mySize,    SIGNAL( editTextChanged( QString ) ),         this, SLOT( onPropertyChanged() ) );
354   connect( myB,       SIGNAL( toggled( bool ) ),                    this, SLOT( onPropertyChanged() ) );
355   connect( myI,       SIGNAL( toggled( bool ) ),                    this, SLOT( onPropertyChanged() ) );
356   connect( myU,       SIGNAL( toggled( bool ) ),                    this, SLOT( onPropertyChanged() ) );
357
358   myCustomFams->hide();
359   myS->hide();
360
361   updateState();
362   onFontChanged( currentFont() );
363 }
364
365 /*!
366   \brief Specifies whether widget works in Native or Custom mode. Native mode 
367   is intended for working with system fonts. Custom mode is intended for 
368   working with manually defined set of fonts. Set of custom fonts can be 
369   specified with setCustomFonts() method 
370   \param mode mode from QtxFontEdit::Mode enumeration
371   \sa mode()
372 */
373 void QtxFontEdit::setMode( const int mode )
374 {
375   if ( myMode == mode )
376     return;
377
378   myMode = mode;
379
380   myFamily->setShown( myMode == Native );
381   myCustomFams->setShown( myMode == Custom );
382
383   updateGeometry();
384 }
385
386 /*!
387   \brief Verifies whether widget works in Native or Custom mode
388   \return Native or Custom mode
389   \sa setMode()
390 */
391 int QtxFontEdit::mode() const
392 {
393   return myMode;
394 }
395
396 /*!
397   \brief Sets list of custom fonts. 
398   <b>This method is intended for working in Custom mode.</b>
399   \param fams list of families
400   \sa fonts(), setMode()
401 */
402 void QtxFontEdit::setFonts( const QStringList& fams )
403 {
404   QString currFam = myCustomFams->currentText();
405   
406   myCustomFams->clear();
407   myCustomFams->addItems( fams );
408
409   int ind = myCustomFams->findText( currFam );
410   if ( ind != -1 )
411     myCustomFams->setCurrentIndex( ind );
412
413   setSizes( QList<int>() );
414 }
415
416 /*!
417   \brief Sets list of available font sizes. 
418   <b>This method is intended for working in Custom mode.</b> The list of sizes can 
419   be empty. In this case system generate listof size automatically from 8 till 72.
420   \param sizes list of sizes
421   \sa sizes(), setMode()
422 */
423 void QtxFontEdit::setSizes( const QList<int>& sizes )
424 {
425   QString currSize = mySize->currentText();
426
427   mySize->clear();
428   if ( !sizes.isEmpty() )
429   {
430     QStringList szList;
431     for ( QList<int>::const_iterator it = sizes.begin(); it != sizes.end(); ++it )
432       szList.append( QString::number( *it ) );
433     mySize->addItems( szList );
434   }
435   else
436   {
437     static QStringList defLst;
438     if ( defLst.isEmpty() )
439     {
440       QString str( "8 9 10 11 12 14 16 18 20 22 24 26 28 36 48 72" );
441       defLst = str.split( " " );
442     }
443     mySize->addItems( defLst );
444   }
445   
446   int ind = mySize->findText( currSize );
447   if ( ind != -1 )
448     mySize->setCurrentIndex( ind );
449 }
450
451 /*!
452   \brief Gets list of custom fonts 
453   \return list of families
454   \sa setFonts(), setMode()
455 */
456 QStringList QtxFontEdit::fonts() const
457 {
458   QStringList fams;
459   for ( int i = 0, n = myCustomFams->count(); i < n; i++ )
460     fams.append( myCustomFams->itemText( i ) );
461   return fams;
462 }
463
464 /*!
465   \brief Gets list of custom fonts 
466   \return list of families
467   \sa setCustomFonts(), setMode()
468 */
469 QList<int> QtxFontEdit::sizes() const
470 {
471   QList<int> lst;
472   for ( int i = 0, n = mySize->count(); i < n; i++ )
473     lst.append( mySize->itemText( i ).toInt() );
474   return lst;
475 }
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495