Back to index

salome-gui  6.5.0
QtxFontEdit.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File:      QtxFontEdit.cxx
00021 // Author:    Sergey TELKOV
00022 //
00023 #include "QtxFontEdit.h"
00024 
00025 #include "QtxComboBox.h"
00026 
00027 #include <QLayout>
00028 #include <QToolButton>
00029 #include <QFontDialog>
00030 #include <QFontDatabase>
00031 #include <QFontComboBox>
00032 
00059 QtxFontEdit::QtxFontEdit( const int feat, QWidget* parent )
00060 : QFrame( parent ),
00061   myFeatures( feat ),
00062   myMode( Native )
00063 {
00064   initialize();
00065 }
00066 
00073 QtxFontEdit::QtxFontEdit( QWidget* parent )
00074 : QFrame( parent ),
00075   myFeatures( All ),
00076   myMode( Native )
00077 {
00078   initialize();
00079 }
00080 
00084 QtxFontEdit::~QtxFontEdit()
00085 {
00086 }
00087 
00093 int QtxFontEdit::features() const
00094 {
00095   return myFeatures;
00096 }
00097 
00103 void QtxFontEdit::setFeatures( const int f )
00104 {
00105   if ( myFeatures == f )
00106     return;
00107 
00108   myFeatures = f;
00109   updateState();
00110 }
00111 
00117 QFont QtxFontEdit::currentFont() const
00118 {
00119   QFont fnt( fontFamily(), fontSize() );
00120 
00121   int script = fontScripting();
00122   fnt.setBold( script & Bold );
00123   fnt.setItalic( script & Italic );
00124   fnt.setUnderline( script & Underline );
00125   fnt.setOverline( script & Shadow ); //addVtkFontPref( tr( "LABELS" ), valLblFontGr, "values_labeling_font" );
00126 
00127   return fnt;
00128 }
00129 
00135 void QtxFontEdit::setCurrentFont( const QFont& fnt )
00136 {
00137   myFamily->blockSignals( true );
00138   myCustomFams->blockSignals( true );
00139   mySize->blockSignals( true );
00140   myB->blockSignals( true );
00141   myI->blockSignals( true );
00142   myU->blockSignals( true );
00143   
00144   setFontFamily( fnt.family() );
00145   setFontSize( fnt.pointSize() );
00146   setFontScripting( ( fnt.bold() ? Bold : 0 ) |
00147                     ( fnt.italic() ? Italic : 0 ) |
00148                     ( fnt.underline() ? Underline : 0 ) | 
00149                     ( fnt.overline() ? Shadow : 0 ) );
00150 
00151   myFamily->blockSignals( false );
00152   myCustomFams->blockSignals( false );
00153   mySize->blockSignals( false );
00154   myB->blockSignals( false );
00155   myI->blockSignals( false );
00156   myU->blockSignals( false );
00157 
00158   emit( changed( currentFont() ) );
00159 }
00160 
00166 QString QtxFontEdit::fontFamily() const
00167 {
00168   if ( myMode == Native )
00169     return myFamily->currentFont().family();
00170   else
00171     return myCustomFams->currentText();
00172 }
00173 
00179 int QtxFontEdit::fontSize() const
00180 {
00181   bool ok;
00182   int pSize = mySize->currentText().toInt( &ok );
00183   return ok ? pSize : 0;
00184 }
00185 
00191 int QtxFontEdit::fontScripting() const
00192 {
00193   return ( myB->isChecked() ? Bold : 0 ) |
00194          ( myI->isChecked() ? Italic : 0 ) |
00195          ( myU->isChecked() ? Underline : 0 ) |
00196          ( myS->isChecked() ? Shadow : 0 ) ;
00197 }
00198 
00204 void QtxFontEdit::setFontFamily( const QString& fam )
00205 {
00206   if ( myMode == Native )
00207   {
00208     myFamily->setCurrentFont( QFont( fam ) );
00209     onFontChanged( myFamily->currentFont() );  
00210   }
00211   else 
00212   {
00213     myCustomFams->setCurrentIndex( myCustomFams->findText( fam ) );
00214     if ( !myCustomFams->signalsBlocked() )
00215       emit( changed( currentFont() ) );
00216   }
00217 }
00218 
00224 void QtxFontEdit::setFontSize( const int s )
00225 {
00226   if ( s <= 0 )
00227     return;
00228 
00229   int idx = mySize->findText( QString::number( s ) );
00230   if ( idx != -1 )
00231     mySize->setCurrentIndex( idx );
00232   else if ( mySize->isEditable() )
00233     mySize->setEditText( QString::number( s ) );
00234 }
00235 
00241 void QtxFontEdit::setFontScripting( const int script )
00242 {
00243   myB->setChecked( script & Bold );
00244   myI->setChecked( script & Italic );
00245   myU->setChecked( script & Underline );
00246   myS->setChecked( script & Shadow );
00247 }
00248 
00252 void QtxFontEdit::updateState()
00253 {
00254   int feat = features();
00255 
00256   myFamily->setVisible( ( feat & Family ) && myMode == Native );
00257   myCustomFams->setVisible( ( feat & Family ) && myMode == Custom );
00258   mySize->setVisible( feat & Size );
00259   myB->setVisible( feat & Bold );
00260   myI->setVisible( feat & Italic );
00261   myU->setVisible( feat & Underline );
00262   myS->setVisible( feat & Shadow );
00263   myPreview->setVisible( feat & Preview );
00264 
00265   mySize->setEditable( feat & UserSize );
00266 }
00267 
00272 void QtxFontEdit::onFontChanged( const QFont& /*f*/ )
00273 {
00274   bool blocked = mySize->signalsBlocked();
00275   mySize->blockSignals( true );
00276 
00277   int s = fontSize();
00278   mySize->clear();
00279 
00280   QList<int> szList = QFontDatabase().pointSizes( fontFamily() );
00281   QStringList sizes;
00282   for ( QList<int>::const_iterator it = szList.begin(); it != szList.end(); ++it )
00283     sizes.append( QString::number( *it ) );
00284   mySize->addItems( sizes );
00285 
00286   setFontSize( s );
00287 
00288   mySize->blockSignals( blocked );
00289 
00290   if ( !myFamily->signalsBlocked() )
00291     emit( changed( currentFont() ) );
00292 }
00293 
00294 void QtxFontEdit::onPropertyChanged()
00295 {
00296   emit( changed( currentFont() ) );
00297 }
00298 
00303 void QtxFontEdit::onPreview( bool /*on*/ )
00304 {
00305   bool ok;
00306   QFont fnt = QFontDialog::getFont( &ok, currentFont() );
00307 
00308   if ( ok )
00309     setCurrentFont( fnt );
00310 }
00311 
00312 /*
00313   \brief Perform internal intialization.
00314 */
00315 void QtxFontEdit::initialize()
00316 {
00317   QHBoxLayout* base = new QHBoxLayout( this );
00318   base->setMargin( 0 );
00319   base->setSpacing( 5 );
00320 
00321   base->addWidget( myFamily = new QFontComboBox( this ) );
00322   base->addWidget( myCustomFams = new QComboBox( this ) );
00323   base->addWidget( mySize = new QtxComboBox( this ) );
00324   mySize->setInsertPolicy( QComboBox::NoInsert );
00325   mySize->setValidator( new QIntValidator( 1, 250, mySize ) );
00326 
00327   base->addWidget( myB = new QToolButton( this ) );
00328   myB->setText( tr( "B" ) );
00329   myB->setCheckable( true );
00330 
00331   base->addWidget( myI = new QToolButton( this ) );
00332   myI->setText( tr( "I" ) );
00333   myI->setCheckable( true );
00334 
00335   base->addWidget( myU = new QToolButton( this ) );
00336   myU->setText( tr( "U" ) );
00337   myU->setCheckable( true );
00338 
00339   base->addWidget( myS = new QToolButton( this ) );
00340   myS->setText( tr( "S" ) );
00341   myS->setCheckable( true );
00342 
00343   base->addWidget( myPreview = new QToolButton( this ) );
00344   myPreview->setText( "..." );
00345 
00346   myFamily->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
00347   myCustomFams->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
00348 
00349   connect( myPreview, SIGNAL( clicked( bool ) ),                    this, SLOT( onPreview( bool ) ) );
00350   connect( myFamily,  SIGNAL( currentFontChanged( const QFont& ) ), this, SLOT( onFontChanged( const QFont& ) ) );
00351   connect( mySize,    SIGNAL( currentIndexChanged( int ) ),         this, SLOT( onPropertyChanged() ) );
00352   connect( mySize,    SIGNAL( editTextChanged( QString ) ),         this, SLOT( onPropertyChanged() ) );
00353   connect( myB,       SIGNAL( toggled( bool ) ),                    this, SLOT( onPropertyChanged() ) );
00354   connect( myI,       SIGNAL( toggled( bool ) ),                    this, SLOT( onPropertyChanged() ) );
00355   connect( myU,       SIGNAL( toggled( bool ) ),                    this, SLOT( onPropertyChanged() ) );
00356 
00357   myCustomFams->hide();
00358   myS->hide();
00359 
00360   updateState();
00361   onFontChanged( currentFont() );
00362 }
00363 
00372 void QtxFontEdit::setMode( const int mode )
00373 {
00374   if ( myMode == mode )
00375     return;
00376 
00377   myMode = mode;
00378 
00379   myFamily->setShown( myMode == Native );
00380   myCustomFams->setShown( myMode == Custom );
00381 
00382   updateGeometry();
00383 }
00384 
00390 int QtxFontEdit::mode() const
00391 {
00392   return myMode;
00393 }
00394 
00401 void QtxFontEdit::setFonts( const QStringList& fams )
00402 {
00403   QString currFam = myCustomFams->currentText();
00404   
00405   myCustomFams->clear();
00406   myCustomFams->addItems( fams );
00407 
00408   int ind = myCustomFams->findText( currFam );
00409   if ( ind != -1 )
00410     myCustomFams->setCurrentIndex( ind );
00411 
00412   setSizes( QList<int>() );
00413 }
00414 
00422 void QtxFontEdit::setSizes( const QList<int>& sizes )
00423 {
00424   QString currSize = mySize->currentText();
00425 
00426   mySize->clear();
00427   if ( !sizes.isEmpty() )
00428   {
00429     QStringList szList;
00430     for ( QList<int>::const_iterator it = sizes.begin(); it != sizes.end(); ++it )
00431       szList.append( QString::number( *it ) );
00432     mySize->addItems( szList );
00433   }
00434   else
00435   {
00436     static QStringList defLst;
00437     if ( defLst.isEmpty() )
00438     {
00439       QString str( "8 9 10 11 12 14 16 18 20 22 24 26 28 36 48 72" );
00440       defLst = str.split( " " );
00441     }
00442     mySize->addItems( defLst );
00443   }
00444   
00445   int ind = mySize->findText( currSize );
00446   if ( ind != -1 )
00447     mySize->setCurrentIndex( ind );
00448 }
00449 
00455 QStringList QtxFontEdit::fonts() const
00456 {
00457   QStringList fams;
00458   for ( int i = 0, n = myCustomFams->count(); i < n; i++ )
00459     fams.append( myCustomFams->itemText( i ) );
00460   return fams;
00461 }
00462 
00468 QList<int> QtxFontEdit::sizes() const
00469 {
00470   QList<int> lst;
00471   for ( int i = 0, n = mySize->count(); i < n; i++ )
00472     lst.append( mySize->itemText( i ).toInt() );
00473   return lst;
00474 }
00475 
00476 
00477 
00478 
00479 
00480 
00481 
00482 
00483 
00484 
00485 
00486 
00487 
00488 
00489 
00490 
00491 
00492 
00493 
00494