Back to index

salome-gui  6.5.0
Style_PrefDlg.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   : Style_PrefDlg.cxx
00021 // Author : Vadim SANDLER, Open CASCADE S.A.S (vadim.sandler@opencascade.com)
00022 //
00023 #include "Style_PrefDlg.h"
00024 #include "Style_ResourceMgr.h"
00025 #include "Style_Model.h"
00026 #include "Style_Salome.h"
00027 
00028 #include <QtxColorButton.h>
00029 #include <QtxDoubleSpinBox.h>
00030 #include <QtxFontEdit.h>
00031 
00032 #include <QApplication>
00033 #include <QButtonGroup>
00034 #include <QCheckBox>
00035 #include <QColorDialog>
00036 #include <QComboBox>
00037 #include <QGridLayout>
00038 #include <QGroupBox>
00039 #include <QHBoxLayout>
00040 #include <QKeyEvent>
00041 #include <QLabel>
00042 #include <QListWidget>
00043 #include <QMap>
00044 #include <QMessageBox>
00045 #include <QPushButton>
00046 #include <QRadioButton>
00047 #include <QSlider>
00048 #include <QSpinBox>
00049 #include <QStackedWidget>
00050 #include <QTabWidget>
00051 #include <QVBoxLayout>
00052 
00053 static const int MARGIN  = 9;
00054 static const int SPACING = 6;
00055 
00067 Style_PrefDlg::PaletteEditor::PaletteEditor( QWidget* parent )
00068   : QFrame( parent ), myCurrentRow( 0 ), myCurrentColumn( -1 )
00069 {
00070   QGridLayout* l = new QGridLayout( this );
00071   l->setMargin( MARGIN ); l->setSpacing( SPACING );
00072   l->addWidget( myQuickButton = new QPushButton( tr( "Quick" ), this ), 0, 0 );
00073   l->addWidget( myAutoCheck   = new QCheckBox( tr( "Auto" ),    this ), 0, 1 );
00074   l->addWidget( myContainer   = new QFrame( this ),                     1, 0, 1, 2 );
00075 
00076   l = new QGridLayout( myContainer );
00077   l->setMargin( 0 ); l->setSpacing( SPACING );
00078 
00079   addColumn( tr( "Base colors" ) );
00080   for ( int i = (int)Style_Model::WindowText; i < (int)Style_Model::NColorRoles; i++ ) {
00081     if ( i == Style_Model::NoRole ) continue;
00082     addItem( i );
00083   }
00084 
00085   addColumn( tr( "Additional colors" ) );
00086   for ( int i = (int)Style_Model::BorderTop; i < (int)Style_Model::LastColor; i++ )
00087     addItem( i );
00088 
00089   connect( myQuickButton, SIGNAL( clicked() ),       this, SLOT( onQuick() ) );
00090   connect( myAutoCheck,   SIGNAL( toggled( bool ) ), this, SLOT( onAuto()  ) );
00091 }
00092 
00097 Style_PrefDlg::PaletteEditor::~PaletteEditor()
00098 {
00099 }
00100 
00107 void Style_PrefDlg::PaletteEditor::addColumn( const QString& title )
00108 {
00109   QGridLayout* l = qobject_cast<QGridLayout*>( myContainer->layout() );
00110   myCurrentRow = 0;
00111   myCurrentColumn++;
00112 
00113   if ( !title.isEmpty() ) {
00114     QHBoxLayout* hbox = new QHBoxLayout;
00115     hbox->setMargin( 0 ); hbox->setSpacing( SPACING );
00116     QWidget* ln = line();
00117     hbox->addWidget( ln );
00118     hbox->setStretchFactor( ln, 5 );
00119     QLabel* tlt = new QLabel( title, myContainer );
00120     tlt->setAlignment( Qt::AlignCenter );
00121     hbox->addWidget( tlt );
00122     ln = line();
00123     hbox->addWidget( ln );
00124     hbox->setStretchFactor( ln, 5 );
00125     l->addLayout( hbox, myCurrentRow++, myCurrentColumn*4, 1, 4 );
00126   }
00127 
00128   l->addWidget( myActiveLab   = new QLabel( tr( "Active" ),   myContainer ), myCurrentRow, myCurrentColumn*4+1 );
00129   l->addWidget( myInactiveLab = new QLabel( tr( "Inactive" ), myContainer ), myCurrentRow, myCurrentColumn*4+2 );
00130   l->addWidget( myDisabledLab = new QLabel( tr( "Disabled" ), myContainer ), myCurrentRow, myCurrentColumn*4+3 );
00131   int w = 0;
00132   w = qMax( w, myActiveLab->sizeHint().width() );
00133   w = qMax( w, myInactiveLab->sizeHint().width() );
00134   w = qMax( w, myDisabledLab->sizeHint().width() );
00135   myActiveLab->setMinimumWidth( w );
00136   myInactiveLab->setMinimumWidth( w );
00137   myDisabledLab->setMinimumWidth( w );
00138   
00139   for( int i = 1; i < 4; i++ ) l->setColumnStretch( myCurrentColumn*4+i, 5 );
00140 
00141   myCurrentRow++;
00142 }
00143 
00150 void Style_PrefDlg::PaletteEditor::addItem( int id )
00151 {
00152   if ( myButtons.contains( id ) )
00153     return;
00154 
00155   myButtons[ id ] = Btns();
00156   myButtons[ id ][ QPalette::Active ]   = new QtxColorButton( myContainer );
00157   myButtons[ id ][ QPalette::Inactive ] = new QtxColorButton( myContainer );
00158   myButtons[ id ][ QPalette::Disabled ] = new QtxColorButton( myContainer );
00159   myButtons[ id ][ QPalette::Active ]->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
00160   myButtons[ id ][ QPalette::Inactive ]->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
00161   myButtons[ id ][ QPalette::Disabled ]->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
00162 
00163   QGridLayout* l = qobject_cast<QGridLayout*>( myContainer->layout() );
00164   l->addWidget( new QLabel( idToName( id ), myContainer ), myCurrentRow, myCurrentColumn*4 );
00165   l->addWidget( myButtons[ id ][ QPalette::Active ],       myCurrentRow, myCurrentColumn*4+1 );
00166   l->addWidget( myButtons[ id ][ QPalette::Inactive ],     myCurrentRow, myCurrentColumn*4+2 );
00167   l->addWidget( myButtons[ id ][ QPalette::Disabled ],     myCurrentRow, myCurrentColumn*4+3 );
00168 
00169   connect( myButtons[ id ][ QPalette::Active   ], SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) );
00170   connect( myButtons[ id ][ QPalette::Inactive ], SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) );
00171   connect( myButtons[ id ][ QPalette::Disabled ], SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) );
00172 
00173   myCurrentRow++;
00174 }
00175 
00182 QList<int> Style_PrefDlg::PaletteEditor::items() const
00183 {
00184   return myButtons.keys();
00185 }
00186 
00195 void Style_PrefDlg::PaletteEditor::setColor( int id, QPalette::ColorGroup cg, const QColor& c )
00196 {
00197   if ( myButtons.contains( id ) ) {
00198     myButtons[ id ][ cg ]->setColor( c );
00199   }
00200 }
00201 
00211 void Style_PrefDlg::PaletteEditor::setColor( int id, const QColor& active,
00212                                              const QColor& inactive, const QColor& disabled )
00213 {
00214   setColor( id, QPalette::Active,   active );
00215   setColor( id, QPalette::Inactive, inactive.isValid() ? inactive : active );
00216   setColor( id, QPalette::Disabled, disabled.isValid() ? disabled : active );
00217 }
00218 
00227 QColor Style_PrefDlg::PaletteEditor::color( int id, QPalette::ColorGroup cg ) const
00228 {
00229   QColor c;
00230   if ( myButtons.contains( id ) ) {
00231     c = myButtons[ id ][ cg ]->color();
00232   }
00233   return c;
00234 }
00235 
00242 bool Style_PrefDlg::PaletteEditor::isAuto() const
00243 {
00244   return myAutoCheck->isChecked();
00245 }
00246 
00253 void Style_PrefDlg::PaletteEditor::setAuto( bool on )
00254 {
00255   myAutoCheck->setChecked( on );
00256 }
00257 
00263 void Style_PrefDlg::PaletteEditor::fromColor( const QColor& c )
00264 {
00265   const QPalette pal = QPalette( c );
00266   for ( int i = 0; i < (int)Style_Model::NColorRoles; i++ ) {
00267     setColor( i,
00268               pal.color( QPalette::Active,   (QPalette::ColorRole)i ), 
00269               pal.color( QPalette::Inactive, (QPalette::ColorRole)i ),
00270               pal.color( QPalette::Disabled, (QPalette::ColorRole)i ) );
00271   }
00272 
00273   // Header
00274   setColor( Style_Model::Header,
00275             pal.color( QPalette::Active,   QPalette::Button ), 
00276             pal.color( QPalette::Inactive, QPalette::Button ),
00277             pal.color( QPalette::Disabled, QPalette::Button ) );
00278   // Checked
00279   setColor( Style_Model::Checked,
00280             pal.color( QPalette::Active,   QPalette::Base ),
00281             pal.color( QPalette::Inactive, QPalette::Base ),
00282             pal.color( QPalette::Disabled, QPalette::Base ) );
00283   // Lines
00284   setColor( Style_Model::Lines,
00285             pal.color( QPalette::Active,   QPalette::Mid ),
00286             pal.color( QPalette::Inactive, QPalette::Mid ),
00287             pal.color( QPalette::Disabled, QPalette::Mid ) );
00288   // Slider
00289   setColor( Style_Model::Slider,
00290             pal.color( QPalette::Active,   QPalette::Button ),
00291             pal.color( QPalette::Inactive, QPalette::Button ),
00292             pal.color( QPalette::Disabled, QPalette::Button ) );
00293   // ProgressBar
00294   setColor( Style_Model::ProgressBar,
00295             pal.color( QPalette::Active,   QPalette::Highlight ),
00296             pal.color( QPalette::Inactive, QPalette::Highlight ),
00297             pal.color( QPalette::Disabled, QPalette::Highlight ) );
00298   // FieldLight
00299   setColor( Style_Model::FieldLight,
00300             pal.color( QPalette::Active,   QPalette::Light ),
00301             pal.color( QPalette::Inactive, QPalette::Light ),
00302             pal.color( QPalette::Disabled, QPalette::Light ) );
00303   // FieldDark
00304   setColor( Style_Model::FieldDark,
00305             pal.color( QPalette::Active,   QPalette::Mid ).light( 125 ),
00306             pal.color( QPalette::Inactive, QPalette::Mid ).light( 125 ),
00307             pal.color( QPalette::Disabled, QPalette::Mid ).light( 125 ) );
00308   // GridLine
00309   setColor( Style_Model::GridLine,
00310             pal.color( QPalette::Active,   QPalette::Mid ),
00311             pal.color( QPalette::Inactive, QPalette::Mid ),
00312             pal.color( QPalette::Disabled, QPalette::Mid ) );
00313   // HighlightWidget
00314   setColor( Style_Model::HighlightWidget,
00315             pal.color( QPalette::Active,   QPalette::Button ),
00316             pal.color( QPalette::Inactive, QPalette::Button ),
00317             pal.color( QPalette::Disabled, QPalette::Button ) );
00318   // HighlightWidget
00319   setColor( Style_Model::HighlightBorder,
00320             pal.color( QPalette::Active,   QPalette::Button ).dark( 100 ),
00321             pal.color( QPalette::Inactive, QPalette::Button ).dark( 100 ),
00322             pal.color( QPalette::Disabled, QPalette::Button ).dark( 100 ) );
00323   // Pointer
00324   setColor( Style_Model::Pointer, Qt::black, Qt::black, Qt::black );
00325 
00326   QColor aDarkActive   = pal.color( QPalette::Active,   QPalette::Dark );
00327   QColor aDarkInactive = pal.color( QPalette::Inactive, QPalette::Dark );
00328   QColor aDarkDisabled = pal.color( QPalette::Disabled, QPalette::Dark );
00329 
00330   // BorderTop
00331   setColor( Style_Model::BorderTop,
00332             aDarkActive.lighter(),
00333             aDarkInactive.lighter(),
00334             aDarkDisabled.lighter() );
00335   // BorderBottom
00336   setColor( Style_Model::BorderBottom,
00337             aDarkActive.darker(),
00338             aDarkInactive.darker(),
00339             aDarkDisabled.darker() );
00340   // TabBorderTop
00341   setColor( Style_Model::TabBorderTop,
00342             aDarkActive.light().light().light(),
00343             aDarkInactive.light().light().light(), 
00344             aDarkDisabled.light().light().light() );
00345   // TabBorderBottom
00346   setColor( Style_Model::TabBorderBottom,
00347             aDarkActive.dark().dark().dark(),
00348             aDarkInactive.dark().dark().dark(),
00349             aDarkDisabled.dark().dark().dark() );
00350 }
00351 
00358 QString Style_PrefDlg::PaletteEditor::idToName( int id )
00359 {
00360   QString name;
00361   switch ( id ) {
00362   case Style_Model::WindowText:        //  0
00363     name = tr( "Window text" ); break;
00364   case Style_Model::Button:            //  1
00365     name = tr( "Button" ); break;
00366   case Style_Model::Light:             //  2
00367     name = tr( "Light" ); break;
00368   case Style_Model::Midlight:          //  3
00369     name = tr( "Midlight" ); break;
00370   case Style_Model::Dark:              //  4
00371     name = tr( "Dark" ); break;
00372   case Style_Model::Mid:               //  5
00373     name = tr( "Mid" );             break;
00374   case Style_Model::Text:              //  6
00375     name = tr( "Text" );             break;
00376   case Style_Model::BrightText:        //  7
00377     name = tr( "Bright text" );      break;
00378   case Style_Model::ButtonText:        //  8
00379     name = tr( "Button text" );      break;
00380   case Style_Model::Base:              //  9
00381     name = tr( "Base" );             break;
00382   case Style_Model::Window:            // 10
00383     name = tr( "Window" );           break;
00384   case Style_Model::Shadow:            // 11
00385     name = tr( "Shadow" );           break;
00386   case Style_Model::Highlight:         // 12
00387     name = tr( "Highlight" );        break;
00388   case Style_Model::HighlightedText:   // 13
00389     name = tr( "Highlighted text" ); break;
00390   case Style_Model::Link:              // 14
00391     name = tr( "Link" );             break;
00392   case Style_Model::LinkVisited:       // 15
00393     name = tr( "Visited link" );     break;
00394   case Style_Model::AlternateBase:     // 16
00395     name = tr( "Alternate base" );   break;
00396   case Style_Model::ToolTipBase:       // 18
00397     name = tr( "Tooltip base" );    break;
00398   case Style_Model::ToolTipText:       // 19
00399     name = tr( "Tooltip text" );    break;
00400   case Style_Model::BorderTop:
00401     name = tr( "Border top" ); break;
00402   case Style_Model::BorderBottom:
00403     name = tr( "Border bottom" ); break;
00404   case Style_Model::TabBorderTop:
00405     name = tr( "Tab border top" ); break;
00406   case Style_Model::TabBorderBottom:
00407     name = tr( "Tab border bottom" ); break;
00408   case Style_Model::FieldLight:
00409     name = tr( "Field light" ); break;
00410   case Style_Model::FieldDark:
00411     name = tr( "Field dark" ); break;
00412   case Style_Model::Slider:
00413     name = tr( "Slider" ); break;
00414   case Style_Model::Lines:
00415     name = tr( "Lines" ); break;
00416   case Style_Model::HighlightWidget:
00417     name = tr( "Widget center" ); break;
00418   case Style_Model::HighlightBorder:
00419     name = tr( "Widget border" ); break;
00420   case Style_Model::Header:
00421     name = tr( "Header" ); break;
00422   case Style_Model::ProgressBar:
00423     name = tr( "Progress bar" ); break;
00424   case Style_Model::Pointer:
00425     name = tr( "Pointer" ); break;
00426   case Style_Model::Checked:
00427     name = tr( "Checked" ); break;
00428   case Style_Model::GridLine:
00429     name = tr( "Table grid" ); break;
00430   default:
00431     break;
00432   }
00433   return name;
00434 }
00435 
00441 QWidget* Style_PrefDlg::PaletteEditor::line()
00442 {
00443   QFrame* hline = new QFrame( myContainer );
00444   hline->setFrameStyle( QFrame::HLine | QFrame::Sunken );
00445   return hline;
00446 }
00447 
00452 void Style_PrefDlg::PaletteEditor::onQuick()
00453 {
00454   static QColor lastColor = Qt::white;
00455   QColor c = QColorDialog::getColor( lastColor, this );
00456   if ( c.isValid() ) {
00457     fromColor( lastColor = c );
00458     emit( changed() );
00459   }
00460 }
00461 
00466 void Style_PrefDlg::PaletteEditor::onAuto()
00467 {
00468   foreach( Btns btn, myButtons ) {
00469     btn[ QPalette::Inactive ]->setEnabled( !myAutoCheck->isChecked() );
00470     btn[ QPalette::Disabled ]->setEnabled( !myAutoCheck->isChecked() );
00471   }
00472   emit( changed() );
00473 }
00474 
00487 Style_PrefDlg::Style_PrefDlg( QWidget* parent )
00488   : QtxDialog( parent, true, true, OK | Close | Apply ),
00489     myResMgr( 0 )
00490 {
00491   // set title
00492   setWindowTitle( tr( "SALOME style preferences" ) );
00493 
00494   // create main layout
00495   QVBoxLayout* main = new QVBoxLayout( mainFrame() );
00496   main->setMargin( 0 ); main->setSpacing( SPACING );
00497 
00498   // create main widgets
00499   myStyleCheck = new QCheckBox( tr( "Enable SALOME Style" ), this );
00500   QFrame* fr = new QFrame( this );
00501   fr->setFrameStyle( QFrame::Box | QFrame::Sunken );
00502 
00503   main->addWidget( myStyleCheck );
00504   main->addWidget( fr );
00505 
00506   // create editor widgets
00507   myStylesList = new QListWidget( fr );
00508   myStylesTab  = new QTabWidget( fr );
00509 
00510   QHBoxLayout* frLayout = new QHBoxLayout( fr );
00511   frLayout->setMargin( MARGIN ); frLayout->setSpacing( SPACING );
00512   frLayout->addWidget( myStylesList );
00513   frLayout->addWidget( myStylesTab );
00514   frLayout->setStretchFactor( myStylesList, 1 );
00515   frLayout->setStretchFactor( myStylesTab, 2 );
00516 
00517   // ...
00518   QWidget* w1 = new QWidget( myStylesTab );
00519   QVBoxLayout* vLayout = new QVBoxLayout( w1 );
00520   vLayout->setMargin( 0 ); vLayout->setSpacing( SPACING );
00521 
00522   myPaletteEditor = new PaletteEditor( w1 );
00523   vLayout->addWidget( myPaletteEditor );
00524   vLayout->addStretch();
00525 
00526   // ...
00527   QWidget* w2 = new QWidget( myStylesTab );
00528   vLayout = new QVBoxLayout( w2 );
00529   vLayout->setMargin( MARGIN ); vLayout->setSpacing( SPACING );
00530 
00531   QGroupBox* fontGroup = new QGroupBox( tr( "Font" ), w2 );
00532   myFontEdit = new QtxFontEdit( fontGroup );
00533 
00534   QHBoxLayout* fontLayout = new QHBoxLayout( fontGroup );
00535   fontLayout->setMargin( MARGIN ); fontLayout->setSpacing( SPACING );
00536   fontLayout->addWidget( myFontEdit );
00537 
00538   QGroupBox* linesGroup = new QGroupBox( tr( "Lines" ), w2 );
00539   QLabel* linesTypeLab = new QLabel( tr( "Type" ), linesGroup );
00540   myLinesCombo  = new QComboBox( linesGroup );
00541   myLinesCombo->addItem( tr( "None" ),       Style_Model::NoLines );
00542   myLinesCombo->addItem( tr( "Horizontal" ), Style_Model::Horizontal );
00543   myLinesCombo->addItem( tr( "Inclined" ),   Style_Model::Inclined );
00544   QLabel* linesTranspLab = new QLabel( tr( "Transparency" ), linesGroup );
00545   myLinesTransparency = new QSlider( Qt::Horizontal, linesGroup );
00546   myLinesTransparency->setMinimum( 0 );
00547   myLinesTransparency->setMaximum( 100 );
00548   myLinesTransparency->setSingleStep( 1 );
00549   myLinesTransparency->setTracking( false );
00550 
00551   QHBoxLayout* linesLayout = new QHBoxLayout( linesGroup );
00552   linesLayout->setMargin( MARGIN ); linesLayout->setSpacing( SPACING );
00553   linesLayout->addWidget( linesTypeLab );
00554   linesLayout->addWidget( myLinesCombo );
00555   linesLayout->addWidget( linesTranspLab );
00556   linesLayout->addWidget( myLinesTransparency );
00557 
00558   QGroupBox* roundGroup = new QGroupBox( tr( "Widgets rounding" ), w2 );
00559   QLabel* roundButtonLab = new QLabel( tr( "Buttons" ), roundGroup );
00560   myButtonRound = new QtxDoubleSpinBox( roundGroup );
00561   QLabel* roundEditLab = new QLabel( tr( "Edit boxes" ), roundGroup );
00562   myEditRound = new QtxDoubleSpinBox( roundGroup );
00563   QLabel* roundFrameLab = new QLabel( tr( "Frames" ), roundGroup );
00564   myFrameRound = new QtxDoubleSpinBox( roundGroup );
00565   QLabel* roundSliderLab = new QLabel( tr( "Sliders" ), roundGroup );
00566   mySliderRound = new QtxDoubleSpinBox( roundGroup );
00567   myAntiAliasing = new QCheckBox( tr( "Anti-aliased borders" ), roundGroup );
00568 
00569   QGridLayout* roundLayout = new QGridLayout( roundGroup );
00570   roundLayout->setMargin( MARGIN ); roundLayout->setSpacing( SPACING );
00571   roundLayout->addWidget( roundButtonLab, 0, 0 );
00572   roundLayout->addWidget( myButtonRound,  0, 1 );
00573   roundLayout->addWidget( roundEditLab,   0, 2 );
00574   roundLayout->addWidget( myEditRound,    0, 3 );
00575   roundLayout->addWidget( roundFrameLab,  1, 0 );
00576   roundLayout->addWidget( myFrameRound,   1, 1 );
00577   roundLayout->addWidget( roundSliderLab, 1, 2 );
00578   roundLayout->addWidget( mySliderRound,  1, 3 );
00579   roundLayout->addWidget( myAntiAliasing, 2, 0, 1, 4 );
00580   
00581   QGroupBox* handleGroup       = new QGroupBox( tr( "Handle" ), w2 );
00582   QLabel*    horHandleLab      = new QLabel( tr( "Horizontal spacing" ), handleGroup );
00583   myHorHandleDelta  = new QSpinBox( handleGroup );
00584   QLabel*    verHandleLab      = new QLabel( tr( "Vertical spacing" ), handleGroup );
00585   myVerHandleDelta  = new QSpinBox( handleGroup );
00586   QLabel*    splitterLengthLab = new QLabel( tr( "Splitter handle size" ), handleGroup );
00587   mySplitterLength  = new QSpinBox( handleGroup );
00588   QLabel*    sliderLengthLab   = new QLabel( tr( "Slider handle size" ), handleGroup );
00589   mySliderSize      = new QSpinBox( handleGroup );
00590   
00591   QGridLayout* handleLayout = new QGridLayout( handleGroup );
00592   handleLayout->setMargin( MARGIN ); handleLayout->setSpacing( SPACING );
00593   handleLayout->addWidget( horHandleLab,      0, 0 );
00594   handleLayout->addWidget( myHorHandleDelta,  0, 1 );
00595   handleLayout->addWidget( verHandleLab,      0, 2);
00596   handleLayout->addWidget( myVerHandleDelta,  0, 3 );
00597   handleLayout->addWidget( splitterLengthLab, 1, 0 );
00598   handleLayout->addWidget( mySplitterLength,  1, 1 );
00599   handleLayout->addWidget( sliderLengthLab,   1, 2 );
00600   handleLayout->addWidget( mySliderSize,      1, 3 );
00601 
00602   QGroupBox* effectGroup = new QGroupBox( tr( "Widget effect" ), w2 );
00603   myEffectNone      = new QRadioButton( tr( "None" ),               effectGroup );
00604   myEffectHighlight = new QRadioButton( tr( "Highlight widgets" ),  effectGroup );
00605   myEffectAutoRaise = new QRadioButton( tr( "Auto raise widgets" ), effectGroup );
00606   QButtonGroup* aGroup = new QButtonGroup( w2 );
00607   aGroup->addButton( myEffectNone );
00608   aGroup->addButton( myEffectHighlight );
00609   aGroup->addButton( myEffectAutoRaise );
00610   myEffectNone->setChecked( true );
00611   myCurrentEffect = myEffectNone;
00612 
00613   QHBoxLayout* effectLayout = new QHBoxLayout( effectGroup );
00614   effectLayout->setMargin( MARGIN ); effectLayout->setSpacing( SPACING );
00615   effectLayout->addWidget( myEffectNone );
00616   effectLayout->addWidget( myEffectHighlight );
00617   effectLayout->addWidget( myEffectAutoRaise );
00618 
00619   vLayout->addWidget( fontGroup );
00620   vLayout->addWidget( linesGroup );
00621   vLayout->addWidget( roundGroup );
00622   vLayout->addWidget( handleGroup );
00623   vLayout->addWidget( effectGroup );
00624   vLayout->addStretch();
00625 
00626   // ...
00627   myStylesTab->addTab( w1, tr( "Colors" )  );
00628   myStylesTab->addTab( w2, tr( "Properties" )  );
00629 
00630   // initialize dialog box
00631   setFocusProxy( fr );
00632   setButtonPosition( Right, Close );
00633   setDialogFlags( AlignOnce );
00634   myStylesList->setEditTriggers( QAbstractItemView::EditKeyPressed );
00635   myStylesList->installEventFilter( this );
00636 
00637   QStringList globalStyles = resourceMgr()->styles( Style_ResourceMgr::Global );
00638   QStringList userStyles   = resourceMgr()->styles( Style_ResourceMgr::User );
00639 
00640   QListWidgetItem* item;
00641 
00642   // current style
00643   item = new QListWidgetItem( tr( "[ Current ]" ) );
00644   item->setForeground( QColor( Qt::red ) );
00645   item->setData( TypeRole, QVariant( Current ) );
00646   myStylesList->addItem( item );
00647   // default style
00648   item = new QListWidgetItem( tr( "[ Default ]" ) );
00649   item->setForeground( QColor( Qt::green ) );
00650   item->setData( TypeRole, QVariant( Default ) );
00651   myStylesList->addItem( item );
00652   // global styles
00653   foreach ( QString sname, globalStyles ) {
00654     item = new QListWidgetItem( sname );
00655     item->setForeground( QColor( Qt::blue ) );
00656     item->setData( TypeRole, QVariant( Global ) );
00657     item->setData( NameRole, QVariant( sname ) );
00658     myStylesList->addItem( item );
00659   }
00660   // user styles
00661   foreach ( QString sname, userStyles ) {
00662     item = new QListWidgetItem( sname );
00663     item->setData( TypeRole, QVariant( User ) );
00664     item->setData( NameRole, QVariant( sname ) );
00665     item->setFlags( item->flags() | Qt::ItemIsEditable );
00666     myStylesList->addItem( item );
00667   }
00668   
00669   // connect widgets
00670   connect( myStyleCheck,        SIGNAL( toggled( bool ) ),        fr,   SLOT( setEnabled( bool ) ) );
00671   connect( myStylesList,        SIGNAL( itemSelectionChanged() ), this, SLOT( onStyleChanged() ) );
00672   connect( myStylesList,        SIGNAL( itemChanged( QListWidgetItem* ) ),       
00673            this, SLOT( onItemChanged( QListWidgetItem* ) ) );
00674   connect( myStylesList,        SIGNAL( itemDoubleClicked( QListWidgetItem* ) ), 
00675            this, SLOT( onApply() ) );
00676   connect( myLinesCombo,        SIGNAL( activated( int ) ),       this, SLOT( onLinesType() ) );
00677   connect( myPaletteEditor,     SIGNAL( changed() ),              this, SIGNAL( styleChanged() ) );
00678   connect( myFontEdit,          SIGNAL( changed( QFont ) ),       this, SIGNAL( styleChanged() ) );
00679   connect( myLinesTransparency, SIGNAL( valueChanged( int ) ),    this, SIGNAL( styleChanged() ) );
00680   connect( myButtonRound,       SIGNAL( valueChanged( double ) ), this, SIGNAL( styleChanged() ) );
00681   connect( myEditRound,         SIGNAL( valueChanged( double ) ), this, SIGNAL( styleChanged() ) );
00682   connect( myFrameRound,        SIGNAL( valueChanged( double ) ), this, SIGNAL( styleChanged() ) );
00683   connect( mySliderRound,       SIGNAL( valueChanged( double ) ), this, SIGNAL( styleChanged() ) );
00684   connect( myAntiAliasing,      SIGNAL( toggled( bool ) ),        this, SIGNAL( styleChanged() ) );
00685   connect( myHorHandleDelta,    SIGNAL( valueChanged( int ) ),    this, SIGNAL( styleChanged() ) );
00686   connect( myVerHandleDelta,    SIGNAL( valueChanged( int ) ),    this, SIGNAL( styleChanged() ) );
00687   connect( mySplitterLength,    SIGNAL( valueChanged( int ) ),    this, SIGNAL( styleChanged() ) );
00688   connect( mySliderSize,        SIGNAL( valueChanged( int ) ),    this, SIGNAL( styleChanged() ) );
00689   connect( aGroup,              SIGNAL( buttonClicked( QAbstractButton* ) ),
00690            this, SLOT( onEffectChanged( QAbstractButton* ) ) );
00691   connect( this,                SIGNAL( styleChanged() ),         this, SLOT( onChanged() ) );
00692   
00693   connect( this, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
00694   connect( this, SIGNAL( dlgHelp() ),  this, SLOT( onHelp() ) );
00695 
00696   myStylesList->setCurrentRow( 0 );
00697   
00698   myStyleCheck->setChecked( Style_Salome::isActive() );
00699   fr->setEnabled( Style_Salome::isActive() );
00700 }
00701 
00705 Style_PrefDlg::~Style_PrefDlg()
00706 {
00707 }
00708 
00712 void Style_PrefDlg::accept()
00713 {
00714   onApply();
00715   QtxDialog::accept();
00716 }
00717 
00722 bool Style_PrefDlg::eventFilter( QObject* o, QEvent* e )
00723 {
00724   if ( o == myStylesList && e->type() == QEvent::KeyPress ) {
00725     QKeyEvent* ke = (QKeyEvent*)e;
00726     if ( ke->key() == Qt::Key_Delete ) {
00727       QListWidgetItem* item = myStylesList->currentItem();
00728       if ( item && item->data( TypeRole ).toInt() == User ) {
00729        if ( QMessageBox::question( this,
00730                                 tr( "Delete user theme" ),
00731                                 tr( "Remove theme %1?" ).arg( item->text() ),
00732                                 QMessageBox::Yes | QMessageBox::No,
00733                                 QMessageBox::Yes ) == QMessageBox::Yes ) {
00734          resourceMgr()->remove( item->data( NameRole ).toString() );
00735          resourceMgr()->save();
00736          delete item;
00737        }
00738       }
00739     }
00740     if ( ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return ) {
00741 //       onApply();
00742 //       return true;
00743     }
00744   }
00745   return QtxDialog::eventFilter( o, e );
00746 }
00747 
00752 Style_ResourceMgr* Style_PrefDlg::resourceMgr()
00753 {
00754   if ( !myResMgr )
00755     myResMgr = new Style_ResourceMgr();
00756   return myResMgr;
00757 }
00758 
00763 void Style_PrefDlg::fromModel( Style_Model* model )
00764 {
00765   if ( !model ) return;
00766   
00767   // colors
00768   for ( int i = (int)Style_Model::WindowText; i < (int)Style_Model::LastColor; i++ ) {
00769     if ( i == Style_Model::NoRole ) continue;
00770     myPaletteEditor->setColor( i, QPalette::Active,   model->color( (Style_Model::ColorRole)i, QPalette::Active ) );
00771     myPaletteEditor->setColor( i, QPalette::Inactive, model->color( (Style_Model::ColorRole)i, QPalette::Inactive ) );
00772     myPaletteEditor->setColor( i, QPalette::Disabled, model->color( (Style_Model::ColorRole)i, QPalette::Disabled ) );
00773   }
00774   myPaletteEditor->setAuto( model->isAutoPalette() );
00775   // font
00776   myFontEdit->setCurrentFont( model->applicationFont() );
00777   // lines type
00778   int idx = myLinesCombo->findData( QVariant( model->linesType() ) );
00779   if ( idx >= 0 ) myLinesCombo->setCurrentIndex( idx );
00780   // lines transparency
00781   myLinesTransparency->setValue( model->linesTransparency() );
00782   // widgets rounding
00783   myButtonRound->setValue( model->widgetRounding( Style_Model::ButtonRadius ) );
00784   myEditRound->setValue( model->widgetRounding( Style_Model::EditRadius ) );
00785   myFrameRound->setValue( model->widgetRounding( Style_Model::FrameRadius ) );
00786   mySliderRound->setValue( model->widgetRounding( Style_Model::SliderRadius ) );
00787   // widgets antialiasing
00788   myAntiAliasing->setChecked( model->antialiasing() );
00789   // handle delta
00790   myHorHandleDelta->setValue( model->handleDelta( Qt::Horizontal ) );
00791   myVerHandleDelta->setValue( model->handleDelta( Qt::Vertical ) );
00792   // splitter handle lentgh
00793   mySplitterLength->setValue( model->splitHandleLength() );
00794   // slider size
00795   mySliderSize->setValue( model->sliderSize() );
00796   // widgets effect
00797   Style_Model::WidgetEffect we = model->widgetEffect();
00798   if ( we == Style_Model::HighlightEffect ) myEffectHighlight->setChecked( true );
00799   else if ( we == Style_Model::AutoRaiseEffect ) myEffectAutoRaise->setChecked( true );
00800   else myEffectNone->setChecked( true );
00801 }
00802  
00807 void Style_PrefDlg::toModel( Style_Model* model ) const
00808 {
00809   if ( !model ) return;
00810   
00811   // colors
00812   // ... first set Button color 'cause it is used to calculate other ones
00813   if ( myPaletteEditor->isAuto() )
00814     model->setColor( Style_Model::Button,
00815                      myPaletteEditor->color( Style_Model::Button, QPalette::Active ) );
00816   else 
00817     model->setColor( Style_Model::Button,
00818                      myPaletteEditor->color( Style_Model::Button, QPalette::Active ),
00819                      myPaletteEditor->color( Style_Model::Button, QPalette::Inactive ),
00820                      myPaletteEditor->color( Style_Model::Button, QPalette::Disabled ));
00821   // ... then set all other colors
00822   for ( int i = (int)Style_Model::WindowText; i < (int)Style_Model::LastColor; i++ ) {
00823     if ( i == Style_Model::NoRole ) continue; // not supported
00824     if ( i == Style_Model::Button ) continue; // already set
00825     if ( myPaletteEditor->isAuto() )
00826       model->setColor( (Style_Model::ColorRole)i,
00827                        myPaletteEditor->color( i, QPalette::Active ) );
00828     else 
00829       model->setColor( (Style_Model::ColorRole)i,
00830                        myPaletteEditor->color( i, QPalette::Active ),
00831                        myPaletteEditor->color( i, QPalette::Inactive ),
00832                        myPaletteEditor->color( i, QPalette::Disabled ));
00833   }
00834   model->setAutoPalette( myPaletteEditor->isAuto() ); // internal
00835   // font
00836   model->setApplicationFont( myFontEdit->currentFont() );
00837   // lines type
00838   model->setLinesType( (Style_Model::LineType)myLinesCombo->itemData( myLinesCombo->currentIndex() ).toInt() );
00839   // lines transparency
00840   model->setLinesTransparency( myLinesTransparency->value() );
00841   // widgets rounding
00842   model->setWidgetRounding( Style_Model::ButtonRadius, myButtonRound->value() );
00843   model->setWidgetRounding( Style_Model::EditRadius,   myEditRound->value() );
00844   model->setWidgetRounding( Style_Model::FrameRadius,  myFrameRound->value() );
00845   model->setWidgetRounding( Style_Model::SliderRadius, mySliderRound->value() );
00846   // widgets antialiasing
00847   model->setAntialiasing( myAntiAliasing->isChecked() );
00848   // handle delta
00849   model->setHandleDelta( Qt::Horizontal, myHorHandleDelta->value() );
00850   model->setHandleDelta( Qt::Vertical,   myVerHandleDelta->value() );
00851   // splitter handle lentgh
00852   model->setSplitHandleLength( mySplitterLength->value() );
00853   // slider size
00854   model->setSliderSize( mySliderSize->value() );
00855   // widgets effect
00856   if      ( myEffectHighlight->isChecked() ) model->setWidgetEffect( Style_Model::HighlightEffect );
00857   else if ( myEffectAutoRaise->isChecked() ) model->setWidgetEffect( Style_Model::AutoRaiseEffect );
00858   else                                       model->setWidgetEffect( Style_Model::NoEffect );
00859 }
00860 
00869 QString Style_PrefDlg::findUniqueName( const QString& name, QListWidgetItem* item, bool addSuffix )
00870 {
00871   bool found = false;
00872   int idx = 0;
00873   for( int i = 2; i < myStylesList->count(); i++ ) {
00874     if ( item == myStylesList->item( i ) ) continue;
00875     QString iname = myStylesList->item( i )->text();
00876     if ( iname == name ) {
00877       found = true;
00878     }
00879     else {
00880       iname = iname.mid( name.length() ).trimmed();
00881       bool ok = false;
00882       int nx = iname.toInt( &ok );
00883       if ( ok ) idx = qMax( idx, nx );
00884     }
00885   }
00886   return found || addSuffix ? QString( "%1 %2" ).arg( name ).arg( idx+1 ) : name;
00887 }
00888 
00892 void Style_PrefDlg::onApply()
00893 {
00894   // save user schemas
00895   resourceMgr()->save();
00896   // set selected schema as current
00897   if ( myStylesList->currentRow() >= 0 ) {
00898     Style_Model* model = Style_Salome::model();
00899     toModel( model );
00900     model->save();
00901     Style_Salome::update();
00902     if ( myStyleCheck->isChecked() )
00903       Style_Salome::apply();
00904     else
00905       Style_Salome::restore();
00906   }
00907 }
00908 
00912 void Style_PrefDlg::onHelp()
00913 {
00914 }
00915 
00919 void Style_PrefDlg::onStyleChanged()
00920 {
00921   blockSignals( true );
00922 
00923   QListWidgetItem* item = myStylesList->currentItem();
00924   int type = item->data( TypeRole ).toInt();
00925 
00926   Style_Model* model = 0;
00927 
00928   switch ( type ) {
00929   case Current:
00930     // current style
00931     model = Style_Salome::model();
00932     break;
00933   case Default:
00934     // default style
00935     model = new Style_Model();
00936     break;
00937   case Global:
00938   case User:
00939     // global style, user style
00940     model = new Style_Model();
00941     model->fromResources( resourceMgr(), item->data( NameRole ).toString() );
00942     break;
00943   default:
00944     break;
00945   }
00946 
00947   fromModel( model );
00948   if ( type != Current )
00949     delete model;
00950 
00951   onLinesType();
00952 
00953   blockSignals( false );
00954 }
00955 
00959 void Style_PrefDlg::onLinesType()
00960 {
00961   myLinesTransparency->setEnabled( myLinesCombo->itemData( myLinesCombo->currentIndex() ) != Style_Model::NoLines );
00962   emit( styleChanged() );
00963 }
00964 
00968 void Style_PrefDlg::onChanged()
00969 {
00970   QListWidgetItem* item = myStylesList->currentItem();
00971   int type = item->data( TypeRole ).toInt();
00972 
00973   // for the current and user schemas do not perform any actions
00974   if ( type == Current ) {
00975     Style_Model model = *( Style_Salome::model() );
00976     toModel( &model );
00977     model.save();
00978     blockSignals( true );
00979     fromModel( &model );
00980     blockSignals( false );
00981   }
00982   else if ( type == User ) {
00983     Style_Model model;
00984     toModel( &model );
00985     QString oldName = item->data( NameRole ).toString(), newName = item->text();
00986     if ( oldName == newName ) {
00987       model.save( resourceMgr(), oldName );
00988     }
00989     else {
00990       resourceMgr()->remove( oldName );
00991       model.save( resourceMgr(), newName );
00992       item->setData( NameRole, newName );
00993     }
00994     blockSignals( true );
00995     fromModel( &model );
00996     blockSignals( false );
00997   }
00998   else {
00999     // if user tries to change global (or default) schema, we create new user schema basing on selected one
01000     QString newName = findUniqueName( tr( "Custom schema" ), 0, true );
01001     item = new QListWidgetItem( newName );
01002     item->setData( TypeRole, QVariant( User ) );
01003     item->setData( NameRole, QVariant( newName ) );
01004     item->setFlags( item->flags() | Qt::ItemIsEditable );
01005     myStylesList->addItem( item );
01006     
01007     Style_Model model;
01008     toModel( &model );
01009     model.save( resourceMgr(), newName );
01010     
01011     myStylesList->setCurrentItem( item );
01012   }
01013 }
01014 
01018 void Style_PrefDlg::onItemChanged( QListWidgetItem* item )
01019 {
01020   QString newName = item->text();
01021   QString uniqueName = findUniqueName( newName, item );
01022   if ( uniqueName != newName ) {
01023     myStylesList->blockSignals( true );
01024     item->setText( uniqueName );
01025     myStylesList->blockSignals( false );
01026   }
01027   onChanged();
01028 }                               
01029 
01033 void Style_PrefDlg::onEffectChanged( QAbstractButton* rb )
01034 {
01035   if ( rb != myCurrentEffect )
01036     emit( styleChanged() );
01037   myCurrentEffect = qobject_cast<QRadioButton*>( rb );
01038 }