Salome HOME
Merge from V6_main 01/04/2013
[modules/gui.git] / src / Qtx / QtxFontEdit.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:      QtxFontEdit.cxx
21 // Author:    Sergey TELKOV
22 //
23 #include "QtxFontEdit.h"
24
25 #include "QtxComboBox.h"
26
27 #include <QLayout>
28 #include <QToolButton>
29 #include <QFontDialog>
30 #include <QFontDatabase>
31 #include <QFontComboBox>
32
33 /*!
34   \class QtxFontEdit
35   \brief The QtxFontEdit class represents a widget for font
36   preference items editing.
37
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
41   available for use.
42
43   Initial font value can be set with setCurrentFont() method. Chosen font
44   can be retrieved with the currentFont() method.
45
46   Font properties can be set with the setFontSize(), setFontFamily(), 
47   setFontScripting() methods and retrieved with fontSize(), fontFamily(), 
48   fontScripting() methods.
49
50   Additional widgets for direct modyfing font properties are available
51   with use of setFeatures() method.
52 */
53
54 /*!
55   \brief Constructor
56   \param feat font widget features (ORed QtxFontEdit::Features flags)
57   \param parent parent widget
58 */
59 QtxFontEdit::QtxFontEdit( const int feat, QWidget* parent )
60 : QFrame( parent ),
61   myFeatures( feat ),
62   myMode( Native )
63 {
64   initialize();
65 }
66
67 /*!
68   \brief Constructor
69   \param parent parent widget
70
71   All font widget features are enabled.
72 */
73 QtxFontEdit::QtxFontEdit( QWidget* parent )
74 : QFrame( parent ),
75   myFeatures( All ),
76   myMode( Native )
77 {
78   initialize();
79 }
80
81 /*!
82   \brief Destructor
83 */
84 QtxFontEdit::~QtxFontEdit()
85 {
86 }
87
88 /*!
89   \brief Get font widget features.
90   \return font widget features (ORed QtxFontEdit::Features flags)
91   \sa setFeatures()
92 */
93 int QtxFontEdit::features() const
94 {
95   return myFeatures;
96 }
97
98 /*!
99   \brief Set font widget features.
100   \param f font widget features (ORed QtxFontEdit::Features flags)
101   \sa features()
102 */
103 void QtxFontEdit::setFeatures( const int f )
104 {
105   if ( myFeatures == f )
106     return;
107
108   myFeatures = f;
109   updateState();
110 }
111
112 /*!
113   \brief Get currently selected font.
114   \return current font
115   \sa setCurrentFont()
116 */
117 QFont QtxFontEdit::currentFont() const
118 {
119   QFont fnt( fontFamily(), fontSize() );
120
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" );
126
127   return fnt;
128 }
129
130 /*!
131   \brief Set currently selected font.
132   \param fnt current font
133   \sa currentFont()
134 */
135 void QtxFontEdit::setCurrentFont( const QFont& fnt )
136 {
137   myFamily->blockSignals( true );
138   myCustomFams->blockSignals( true );
139   mySize->blockSignals( true );
140   myB->blockSignals( true );
141   myI->blockSignals( true );
142   myU->blockSignals( true );
143   
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 ) );
150
151   myFamily->blockSignals( false );
152   myCustomFams->blockSignals( false );
153   mySize->blockSignals( false );
154   myB->blockSignals( false );
155   myI->blockSignals( false );
156   myU->blockSignals( false );
157
158   emit( changed( currentFont() ) );
159 }
160
161 /*!
162   \brief Get selected font family name.
163   \return current font family name
164   \sa setFontFamily()
165 */
166 QString QtxFontEdit::fontFamily() const
167 {
168   if ( myMode == Native )
169     return myFamily->currentFont().family();
170   else
171     return myCustomFams->currentText();
172 }
173
174 /*!
175   \brief Get selected font size.
176   \return current font size
177   \sa setFontSize()
178 */
179 int QtxFontEdit::fontSize() const
180 {
181   bool ok;
182   int pSize = mySize->currentText().toInt( &ok );
183   return ok ? pSize : 0;
184 }
185
186 /*!
187   \brief Get selected font scripting.
188   \return current font scripting
189   \sa setFontScripting()
190 */
191 int QtxFontEdit::fontScripting() const
192 {
193   return ( myB->isChecked() ? Bold : 0 ) |
194          ( myI->isChecked() ? Italic : 0 ) |
195          ( myU->isChecked() ? Underline : 0 ) |
196          ( myS->isChecked() ? Shadow : 0 ) ;
197 }
198
199 /*!
200   \brief Set font family name.
201   \param fam new font family name
202   \sa fontFamily()
203 */
204 void QtxFontEdit::setFontFamily( const QString& fam )
205 {
206   if ( myMode == Native )
207   {
208     myFamily->setCurrentFont( QFont( fam ) );
209     onFontChanged( myFamily->currentFont() );  
210   }
211   else 
212   {
213     myCustomFams->setCurrentIndex( myCustomFams->findText( fam ) );
214     if ( !myCustomFams->signalsBlocked() )
215       emit( changed( currentFont() ) );
216   }
217 }
218
219 /*!
220   \brief Set font size.
221   \param fam new font size
222   \sa fontSize()
223 */
224 void QtxFontEdit::setFontSize( const int s )
225 {
226   if ( s <= 0 )
227     return;
228
229   int idx = mySize->findText( QString::number( s ) );
230   if ( idx != -1 )
231     mySize->setCurrentIndex( idx );
232   else if ( mySize->isEditable() )
233     mySize->setEditText( QString::number( s ) );
234 }
235
236 /*!
237   \brief Set font scripting.
238   \param fam new font scripting
239   \sa fontScripting()
240 */
241 void QtxFontEdit::setFontScripting( const int script )
242 {
243   myB->setChecked( script & Bold );
244   myI->setChecked( script & Italic );
245   myU->setChecked( script & Underline );
246   myS->setChecked( script & Shadow );
247 }
248
249 /*!
250   \brief Update widget state
251 */
252 void QtxFontEdit::updateState()
253 {
254   int feat = features();
255
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 );
264
265   mySize->setEditable( feat & UserSize );
266 }
267
268 /*!
269   \brief Called when current font is changed.
270   \param f (not used)
271 */
272 void QtxFontEdit::onFontChanged( const QFont& /*f*/ )
273 {
274   bool blocked = mySize->signalsBlocked();
275   mySize->blockSignals( true );
276
277   int s = fontSize();
278   mySize->clear();
279
280   QList<int> szList = QFontDatabase().pointSizes( fontFamily() );
281   QStringList sizes;
282   for ( QList<int>::const_iterator it = szList.begin(); it != szList.end(); ++it )
283     sizes.append( QString::number( *it ) );
284   mySize->addItems( sizes );
285
286   setFontSize( s );
287
288   mySize->blockSignals( blocked );
289
290   if ( !myFamily->signalsBlocked() )
291     emit( changed( currentFont() ) );
292 }
293
294 void QtxFontEdit::onPropertyChanged()
295 {
296   emit( changed( currentFont() ) );
297 }
298
299 /*!
300   \brief Called when "Preview" button is clicked.
301   \param on (not used)
302 */
303 void QtxFontEdit::onPreview( bool /*on*/ )
304 {
305   bool ok;
306   QFont fnt = QFontDialog::getFont( &ok, currentFont() );
307
308   if ( ok )
309     setCurrentFont( fnt );
310 }
311
312 /*
313   \brief Perform internal intialization.
314 */
315 void QtxFontEdit::initialize()
316 {
317   QHBoxLayout* base = new QHBoxLayout( this );
318   base->setMargin( 0 );
319   base->setSpacing( 5 );
320
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 ) );
326
327   base->addWidget( myB = new QToolButton( this ) );
328   myB->setText( tr( "B" ) );
329   myB->setCheckable( true );
330
331   base->addWidget( myI = new QToolButton( this ) );
332   myI->setText( tr( "I" ) );
333   myI->setCheckable( true );
334
335   base->addWidget( myU = new QToolButton( this ) );
336   myU->setText( tr( "U" ) );
337   myU->setCheckable( true );
338
339   base->addWidget( myS = new QToolButton( this ) );
340   myS->setText( tr( "S" ) );
341   myS->setCheckable( true );
342
343   base->addWidget( myPreview = new QToolButton( this ) );
344   myPreview->setText( "..." );
345
346   myFamily->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
347   myCustomFams->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
348
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() ) );
356
357   myCustomFams->hide();
358   myS->hide();
359
360   updateState();
361   onFontChanged( currentFont() );
362 }
363
364 /*!
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
370   \sa mode()
371 */
372 void QtxFontEdit::setMode( const int mode )
373 {
374   if ( myMode == mode )
375     return;
376
377   myMode = mode;
378
379   myFamily->setShown( myMode == Native );
380   myCustomFams->setShown( myMode == Custom );
381
382   updateGeometry();
383 }
384
385 /*!
386   \brief Verifies whether widget works in Native or Custom mode
387   \return Native or Custom mode
388   \sa setMode()
389 */
390 int QtxFontEdit::mode() const
391 {
392   return myMode;
393 }
394
395 /*!
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()
400 */
401 void QtxFontEdit::setFonts( const QStringList& fams )
402 {
403   QString currFam = myCustomFams->currentText();
404   
405   myCustomFams->clear();
406   myCustomFams->addItems( fams );
407
408   int ind = myCustomFams->findText( currFam );
409   if ( ind != -1 )
410     myCustomFams->setCurrentIndex( ind );
411
412   setSizes( QList<int>() );
413 }
414
415 /*!
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()
421 */
422 void QtxFontEdit::setSizes( const QList<int>& sizes )
423 {
424   QString currSize = mySize->currentText();
425
426   mySize->clear();
427   if ( !sizes.isEmpty() )
428   {
429     QStringList szList;
430     for ( QList<int>::const_iterator it = sizes.begin(); it != sizes.end(); ++it )
431       szList.append( QString::number( *it ) );
432     mySize->addItems( szList );
433   }
434   else
435   {
436     static QStringList defLst;
437     if ( defLst.isEmpty() )
438     {
439       QString str( "8 9 10 11 12 14 16 18 20 22 24 26 28 36 48 72" );
440       defLst = str.split( " " );
441     }
442     mySize->addItems( defLst );
443   }
444   
445   int ind = mySize->findText( currSize );
446   if ( ind != -1 )
447     mySize->setCurrentIndex( ind );
448 }
449
450 /*!
451   \brief Gets list of custom fonts 
452   \return list of families
453   \sa setFonts(), setMode()
454 */
455 QStringList QtxFontEdit::fonts() const
456 {
457   QStringList fams;
458   for ( int i = 0, n = myCustomFams->count(); i < n; i++ )
459     fams.append( myCustomFams->itemText( i ) );
460   return fams;
461 }
462
463 /*!
464   \brief Gets list of custom fonts 
465   \return list of families
466   \sa setCustomFonts(), setMode()
467 */
468 QList<int> QtxFontEdit::sizes() const
469 {
470   QList<int> lst;
471   for ( int i = 0, n = mySize->count(); i < n; i++ )
472     lst.append( mySize->itemText( i ).toInt() );
473   return lst;
474 }
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494