Back to index

salome-gui  6.5.0
QtxBackgroundTool.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:      QtxBackgroundTool.cxx
00021 // Author:    Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00022 
00023 #include "QtxBackgroundTool.h"
00024 #include "QtxColorButton.h"
00025 
00026 #include <QCheckBox>
00027 #include <QComboBox>
00028 #include <QFileDialog>
00029 #include <QHBoxLayout>
00030 #include <QLabel>
00031 #include <QLineEdit>
00032 #include <QPushButton>
00033 #include <QStackedWidget>
00034 #include <QVBoxLayout>
00035 
00036 
00082 QtxBackgroundTool::QtxBackgroundTool( QWidget* parent )
00083   : QWidget( parent ), myTextureAllowed( true ), myLastGradient( -1 )
00084 {
00085   init( Qt::Horizontal );
00086 }
00087 
00096 QtxBackgroundTool::QtxBackgroundTool( Qt::Orientation o, QWidget* parent )
00097   : QWidget( parent ), myTextureAllowed( true ), myLastGradient( -1 )
00098 {
00099   init( o );
00100 }
00101 
00105 void QtxBackgroundTool::init( Qt::Orientation o )
00106 {
00107   // mode combo box
00108   myModeCombo = new QComboBox( this );
00109   // color / gradient sub-widgets container
00110   myCContainer = new QStackedWidget( this );
00111   // texture sub-widgets container
00112   myTContainer = new QWidget( this );
00113 
00114   QWidget* wrap;
00115   QHBoxLayout* wrapLayout;
00116 
00117   // add image controls
00118   wrapLayout = new QHBoxLayout( myTContainer );
00119   wrapLayout->setMargin( 0 );
00120   wrapLayout->setSpacing( 5 );
00121   myTextureCheck = new QCheckBox( tr( "Image" ), myTContainer );
00122   myFileName     = new QLineEdit( myTContainer );
00123   myFileName->setMinimumWidth(100);
00124   myBrowseBtn    = new QPushButton( tr( "Browse..." ), myTContainer );
00125   myTextureMode  = new QComboBox( myTContainer );
00126   wrapLayout->addWidget( myTextureCheck );
00127   wrapLayout->addWidget( myFileName );
00128   wrapLayout->addWidget( myBrowseBtn );
00129   wrapLayout->addWidget( myTextureMode );
00130   wrapLayout->setStretchFactor(myFileName, 10);
00131   // add color controls
00132   wrap = new QWidget( this );
00133   wrapLayout = new QHBoxLayout( wrap );
00134   wrapLayout->setMargin( 0 );
00135   wrapLayout->setSpacing( 5 );
00136   myFirstColor  = new QtxColorButton( wrap );
00137   mySecondColor = new QtxColorButton( wrap );
00138   wrapLayout->addWidget( myFirstColor );
00139   wrapLayout->addWidget( mySecondColor );
00140   wrapLayout->setStretchFactor(myFirstColor, 5);
00141   wrapLayout->setStretchFactor(mySecondColor, 5);
00142   myCContainer->addWidget( wrap ); // Color
00143   // add gradient controls ... NOT IMPLEMENTED YET
00144   wrap = new QWidget( this );
00145   wrapLayout = new QHBoxLayout( wrap );
00146   wrapLayout->setMargin( 0 );
00147   wrapLayout->setSpacing( 5 );
00148   QLabel* foo = new QLabel( tr( "Not implemented yet" ), wrap );
00149   foo->setAlignment( Qt::AlignCenter );
00150   wrapLayout->addWidget( foo );
00151   myCContainer->addWidget( wrap ); // Gradient
00152 
00153   // initialize widget
00154   myFirstColor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00155   mySecondColor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00156 
00157   connect( myModeCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updateState() ) );
00158   connect( myTextureCheck, SIGNAL( toggled( bool ) ), this, SLOT( updateState() ) );
00159   connect( myBrowseBtn, SIGNAL( clicked() ), this, SLOT( browse() ) );
00160 
00161   setModeAllowed( Qtx::ColorBackground );
00162   setModeAllowed( Qtx::SimpleGradientBackground );
00163   setModeAllowed( Qtx::CustomGradientBackground );
00164   setTextureModeAllowed( Qtx::CenterTexture );
00165   setTextureModeAllowed( Qtx::TileTexture );
00166   setTextureModeAllowed( Qtx::StretchTexture );
00167   setTextureAllowed();
00168   setImageFormats( QString() );
00169   setOrientation( o );
00170 }
00171 
00175 QtxBackgroundTool::~QtxBackgroundTool()
00176 {
00177 }
00178 
00184 Qtx::BackgroundData QtxBackgroundTool::data() const
00185 {
00186   Qtx::BackgroundData bgData;
00187   int idx = myModeCombo->currentIndex();
00188   if ( idx != -1 ) {
00189     // get currently selected mode
00190     int id = myModeCombo->itemData( idx, TypeRole ).toInt();
00191     // texture data
00192     if ( isTextureAllowed() && myTextureMode->currentIndex() != -1 ) {
00193       bgData.setTexture( myFileName->text().trimmed(),
00194                       Qtx::TextureMode( myTextureMode->itemData( myTextureMode->currentIndex() ).toInt() ) );
00195       bgData.setTextureShown( myTextureCheck->isChecked() );
00196     }
00197     // single-color data
00198     if ( isModeAllowed( Qtx::ColorBackground ) ) {
00199       bgData.setColor( myFirstColor->color() );
00200     }
00201     // simple two-color gradient data
00202     if ( isModeAllowed( Qtx::SimpleGradientBackground ) ) {
00203       bgData.setGradient( myLastGradient, myFirstColor->color(), mySecondColor->color() );
00204     }
00205     // custom gradient data
00206     if ( isModeAllowed( Qtx::CustomGradientBackground ) ) {
00207       // bgData.setGradient( ... );
00208       // NOT IMPLEMENTED YET
00209     }
00210 
00211     // set current mode
00212     bgData.setMode( Qtx::BackgroundMode( id ) );
00213   }
00214   return bgData;
00215 }
00216 
00222 void QtxBackgroundTool::setData( const Qtx::BackgroundData& bgData )
00223 {
00224   Qtx::BackgroundMode m = bgData.mode();
00225   QColor c1, c2;
00226   int     gtype = bgData.gradient( c1, c2 );
00227   QString fileName;
00228   int tmode = bgData.texture( fileName );
00229   bool tshown = bgData.isTextureShown();
00230 
00231   // texture data
00232   myFileName->setText( fileName );
00233   for ( int i = 0; i < myTextureMode->count(); i++ ) {
00234     if ( myTextureMode->itemData( i ).toInt() == tmode ) {
00235       myTextureMode->setCurrentIndex( i );
00236       break;
00237     }
00238   }
00239   myTextureCheck->setChecked( tshown );
00240 
00241   // color / simple gradient data
00242   myFirstColor->setColor( c1 );
00243   mySecondColor->setColor( c2 );
00244 
00245   // gradient data
00246   // ... NOT IMPLEMENTED YET
00247 
00248   // set current mode index
00249   int idx = -1;
00250   for ( int i = 0; i < myModeCombo->count() && idx == -1; i++ ) {
00251     int im = myModeCombo->itemData( i, TypeRole ).toInt();
00252     // for simple gradient mode we also check gradient type
00253     if ( m == Qtx::SimpleGradientBackground && im == Qtx::SimpleGradientBackground ) {
00254       int it = myModeCombo->itemData( i, IdRole ).toInt();
00255       if ( it == gtype ) idx = i;
00256     }
00257     // for other modes we just check mode itself
00258     else if ( im == m ) {
00259       idx = i;
00260     }
00261   }
00262   // if background data is invalid, we set-up widget to the first available type
00263   if ( idx == -1 && myModeCombo->count() > 0 ) idx = 0;
00264   myModeCombo->setCurrentIndex( idx );
00265 }
00266 
00272 void QtxBackgroundTool::gradients( QStringList& gradList, QIntList& idList ) const
00273 {
00274   gradList = myGradients;
00275   idList   = myGradientsIds;
00276 }
00277 
00283 void QtxBackgroundTool::setGradients( const QStringList& gradList, const QIntList& idList )
00284 {
00285   Qtx::BackgroundData d = data(); // get current state
00286   myGradients    = gradList;      // store new gradient data
00287   myGradientsIds = idList;        // ...
00288   myLastGradient = -1;            // ...
00289   internalUpdate();               // re-initialize
00290   setData( d );                   // restore current state (if possible)
00291 }
00292 
00299 bool QtxBackgroundTool::isModeAllowed( Qtx::BackgroundMode mode ) const
00300 {
00301   return myTypesAllowed.contains( mode ) ? myTypesAllowed[ mode ] : false;
00302 }
00303 
00310 void QtxBackgroundTool::setModeAllowed( Qtx::BackgroundMode mode, bool on )
00311 {
00312   if ( mode == Qtx::CustomGradientBackground )
00313     return; // NOT IMPLEMENTED YET //
00314 
00315   if ( isModeAllowed( mode ) != on ) {
00316     Qtx::BackgroundData d = data(); // get current state
00317     myTypesAllowed[ mode ] = on;    // store new background mode state
00318     internalUpdate();               // re-initialize
00319     setData( d );                   // restore current state (if possible)
00320   }
00321 }
00322 
00329 bool QtxBackgroundTool::isTextureModeAllowed( Qtx::TextureMode mode ) const
00330 {
00331   return myTextureTypesAllowed.contains( mode ) ? myTextureTypesAllowed[ mode ] : false;
00332 }
00333 
00340 void QtxBackgroundTool::setTextureModeAllowed( Qtx::TextureMode mode, bool on )
00341 {
00342   if ( isTextureModeAllowed( mode ) != on ) {
00343     Qtx::BackgroundData d = data();     // get current state
00344     myTextureTypesAllowed[ mode ] = on; // store new texture mode
00345     internalUpdate();                   // re-initialize
00346     setData( d );                       // restore current state (if possible)
00347   }
00348 }
00349 
00355 bool QtxBackgroundTool::isTextureAllowed() const
00356 {
00357   return myTextureAllowed;
00358 }
00359 
00365 void QtxBackgroundTool::setTextureAllowed( bool on )
00366 {
00367   if ( myTextureAllowed != on ) {
00368     myTextureAllowed = on;
00369     setOrientation( orientation() );
00370   }
00371 }
00372 
00378 QString QtxBackgroundTool::imageFormats() const
00379 {
00380   return myImageFormats;
00381 }
00382 
00388 void QtxBackgroundTool::setImageFormats( const QString& formats )
00389 {
00390   myImageFormats = formats.isEmpty() ? tr( "Images Files (*.bmp *.png *.jpg *.jpeg *.gif *.tiff)" ) : formats;
00391 }
00392 
00398 Qt::Orientation QtxBackgroundTool::orientation() const
00399 {
00400   return qobject_cast<QVBoxLayout*>( layout() ) ? Qt::Vertical : Qt::Horizontal;
00401 }
00402 
00408 void QtxBackgroundTool::setOrientation( Qt::Orientation orientation )
00409 {
00410   QBoxLayout* l = (orientation == Qt::Horizontal) ? (QBoxLayout*)(new QHBoxLayout) : (QBoxLayout*)(new QVBoxLayout);
00411   l->setMargin( 0 );
00412   l->setSpacing( 5 );
00413   l->addWidget( myModeCombo );
00414   l->addWidget( myCContainer );
00415   myTContainer->setVisible( isTextureAllowed() );
00416   if ( isTextureAllowed() )
00417     l->addWidget( myTContainer );
00418   delete layout();
00419   setLayout( l );
00420 }
00421 
00426 void QtxBackgroundTool::internalUpdate()
00427 {
00428   myModeCombo->clear();
00429   if ( isModeAllowed( Qtx::ColorBackground ) ) {
00430     myModeCombo->addItem( tr( "Single Color" ) );
00431     myModeCombo->setItemData( 0, (int)Qtx::ColorBackground, TypeRole );
00432   }
00433   if ( isModeAllowed( Qtx::SimpleGradientBackground ) ) {
00434 //     if ( myGradients.count() > 0 && myModeCombo->count() > 0 ) 
00435 //       myModeCombo->insertSeparator( myModeCombo->count() );
00436     for ( int i = 0; i < myGradients.count(); i++ ) {
00437       myModeCombo->addItem( myGradients[i] );
00438       int idx = myModeCombo->count()-1;
00439       myModeCombo->setItemData( idx, (int)Qtx::SimpleGradientBackground, TypeRole );
00440       myModeCombo->setItemData( idx, myGradientsIds.count() > i ? myGradientsIds[i] : i, IdRole );
00441     }
00442   }
00443   if ( isModeAllowed( Qtx::CustomGradientBackground ) ) {
00444 //     if ( myModeCombo->count() > 0 ) 
00445 //       myModeCombo->insertSeparator( myModeCombo->count() );
00446     myModeCombo->addItem( tr( "Custom" ) );
00447     int idx = myModeCombo->count()-1;
00448     myModeCombo->setItemData( idx, (int)Qtx::CustomGradientBackground, TypeRole );
00449   }
00450 
00451   myTextureMode->clear();
00452   if ( isTextureModeAllowed( Qtx::CenterTexture ) )
00453     myTextureMode->addItem( tr( "Center" ),  Qtx::CenterTexture );
00454   if ( isTextureModeAllowed( Qtx::TileTexture ) )
00455     myTextureMode->addItem( tr( "Tile" ),    Qtx::TileTexture );
00456   if ( isTextureModeAllowed( Qtx::StretchTexture ) )
00457     myTextureMode->addItem( tr( "Stretch" ), Qtx::StretchTexture );
00458 
00459   updateState();
00460 }
00461 
00465 void QtxBackgroundTool::updateState()
00466 {
00467   int idx = myModeCombo->currentIndex();
00468   int id = -1;
00469   if ( idx >= 0 ) {
00470     id = myModeCombo->itemData( idx, TypeRole ).toInt();
00471     switch( id ) {
00472     case Qtx::ColorBackground:
00473       myCContainer->setCurrentIndex( Color );
00474       break;
00475     case Qtx::SimpleGradientBackground:
00476       myCContainer->setCurrentIndex( Color );
00477       myLastGradient = myModeCombo->itemData( idx, IdRole ).toInt();
00478       break;
00479     case Qtx::CustomGradientBackground:
00480       myCContainer->setCurrentIndex( Gradient );
00481       break;
00482     }
00483   }
00484   myModeCombo->setEnabled( idx >= 0 );
00485   myCContainer->setEnabled( idx >= 0 );
00486   mySecondColor->setEnabled( id == Qtx::SimpleGradientBackground );
00487   myTContainer->setEnabled( idx >= 0 );
00488   myFileName->setEnabled( myTextureCheck->isChecked() );
00489   myBrowseBtn->setEnabled( myTextureCheck->isChecked() );
00490   myTextureMode->setEnabled( myTextureCheck->isChecked() );
00491 }
00492 
00496 void QtxBackgroundTool::browse()
00497 {
00498   QString fileName = QFileDialog::getOpenFileName( this, tr( "Select Picture" ), myFileName->text().trimmed(), myImageFormats );
00499   if ( !fileName.isEmpty() )
00500     myFileName->setText( fileName );
00501 }
00502 
00538 QtxBackgroundDialog::QtxBackgroundDialog( QWidget* parent )
00539   : QtxDialog( parent, true, true, OK | Cancel )
00540 {
00541   init();
00542 }
00543 
00549 QtxBackgroundDialog::QtxBackgroundDialog( const Qtx::BackgroundData& bgData, QWidget* parent )
00550   : QtxDialog( parent, true, true, OK | Cancel )
00551 {
00552   init();
00553   setData( bgData );
00554 }
00555 
00559 QtxBackgroundDialog::~QtxBackgroundDialog()
00560 {
00561 }
00562 
00566 void QtxBackgroundDialog::init()
00567 {
00568   // title
00569   setWindowTitle( tr( "Change background" ) );
00570   // flags
00571   setDialogFlags( SetFocus );
00572   // move "Cancel" button to the right
00573   setButtonPosition( Right, Cancel );
00574   // set OK button to be default (activated by Enter key)
00575   qobject_cast<QPushButton*>( button( OK ) )->setDefault( true );
00576 
00577   // main layout
00578   QVBoxLayout* main = new QVBoxLayout( mainFrame() );
00579   main->setMargin( 0 );
00580   main->setSpacing( 5 );
00581 
00582   // background widget
00583   myTool = new QtxBackgroundTool( Qt::Vertical, mainFrame() );
00584   main->addWidget( myTool );
00585 }
00586 
00591 void QtxBackgroundDialog::setData( const Qtx::BackgroundData& bgData )
00592 {
00593   myTool->setData( bgData );
00594 }
00595 
00600 Qtx::BackgroundData QtxBackgroundDialog::data() const
00601 {
00602   return myTool->data();
00603 }
00604 
00610 void QtxBackgroundDialog::setGradients( const QStringList& gradList, const QIntList& idList )
00611 {
00612   myTool->setGradients( gradList, idList );
00613 }
00614 
00620 void QtxBackgroundDialog::setModeAllowed( Qtx::BackgroundMode mode, bool on )
00621 {
00622   myTool->setModeAllowed( mode, on );
00623 }
00624 
00630 void QtxBackgroundDialog::setTextureAllowed( bool on )
00631 {
00632   myTool->setTextureAllowed( on );
00633 }
00634 
00641 void QtxBackgroundDialog::setTextureModeAllowed( Qtx::TextureMode mode, bool on )
00642 {
00643   myTool->setTextureModeAllowed( mode, on );
00644 }
00645 
00650 void QtxBackgroundDialog::setImageFormats( const QString& formats )
00651 {
00652   myTool->setImageFormats( formats );
00653 }
00654 
00679 Qtx::BackgroundData QtxBackgroundDialog::getBackground( const Qtx::BackgroundData& bgData,
00680                                                  QWidget* parent,
00681                                                  bool enableSolidColor,
00682                                                  bool enableGradient,
00683                                                  bool enableCustom,
00684                                                  bool enableTexture,
00685                                                  const QStringList& gradList,
00686                                                  const QIntList& idList,
00687                                                  const QString& formats )
00688 {
00689   QtxBackgroundDialog dlg( parent );
00690   dlg.setTextureAllowed( enableTexture );
00691   dlg.setModeAllowed( Qtx::ColorBackground,          enableSolidColor );
00692   dlg.setModeAllowed( Qtx::SimpleGradientBackground, enableGradient );
00693   dlg.setModeAllowed( Qtx::CustomGradientBackground, enableCustom );
00694   dlg.setGradients( gradList, idList );
00695   dlg.setImageFormats( formats );
00696   dlg.setData( bgData );
00697   Qtx::BackgroundData res;
00698   int rc = dlg.exec();
00699   if ( rc ) res = dlg.data();
00700   return res;
00701 }
00702 
00728 Qtx::BackgroundData QtxBackgroundDialog::getBackground( const Qtx::BackgroundData& bgData,
00729                                                  QWidget* parent,
00730                                                  const QIntList& tmList,
00731                                                  bool enableSolidColor,
00732                                                  bool enableGradient,
00733                                                  bool enableCustom,
00734                                                  bool enableTexture,
00735                                                  const QStringList& gradList,
00736                                                  const QIntList& idList,
00737                                                  const QString& formats )
00738 {
00739   QtxBackgroundDialog dlg( parent );
00740   dlg.setTextureAllowed( enableTexture );
00741   for ( int i = Qtx::CenterTexture; i <= Qtx::StretchTexture; i++ )
00742     dlg.setTextureModeAllowed( (Qtx::TextureMode)i, tmList.contains( i ) );
00743   dlg.setModeAllowed( Qtx::ColorBackground,          enableSolidColor );
00744   dlg.setModeAllowed( Qtx::SimpleGradientBackground, enableGradient );
00745   dlg.setModeAllowed( Qtx::CustomGradientBackground, enableCustom );
00746   dlg.setGradients( gradList, idList );
00747   dlg.setImageFormats( formats );
00748   dlg.setData( bgData );
00749   Qtx::BackgroundData res;
00750   int rc = dlg.exec();
00751   if ( rc ) res = dlg.data();
00752   return res;
00753 }