Back to index

salome-gui  6.5.0
QtxBiColorTool.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:      QtxBiColorTool.cxx
00021 // Author:    Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00022 
00023 #include "QtxBiColorTool.h"
00024 #include "QtxColorButton.h"
00025 
00026 #include <QHBoxLayout>
00027 #include <QLabel>
00028 #include <QPainter>
00029 #include <QSlider>
00030 #include <QStyle>
00031 
00032 const int BICOLOR_MAX_DELTA = 100;
00033 
00039 class QtxBiColorTool::ColorLabel: public QFrame
00040 {
00041 public:
00042   ColorLabel( QWidget* parent) : QFrame( parent )
00043   {
00044     setFrameStyle( QFrame::Box | QFrame::Plain );
00045     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00046   }
00047   ~ColorLabel() {}
00048   QSize sizeHint() const
00049   {
00050     return minimumSizeHint();
00051   }
00052   QSize minimumSizeHint() const
00053   {
00054     int pm = style()->pixelMetric(QStyle::PM_ButtonMargin);
00055     QFontMetrics fm ( font() );
00056     return QSize( fm.height() + pm, fm.height() + pm );
00057   }
00058   void paintEvent( QPaintEvent* e )
00059   {
00060     QPainter p( this );
00061     drawFrame( &p );
00062     QRect r = contentsRect();
00063     if ( myColor.isValid() ) {
00064       p.fillRect( r, QBrush( myColor ) );
00065     }
00066     else {
00067       p.fillRect( r, QBrush( palette().color( foregroundRole() ), Qt::BDiagPattern ) );
00068     }
00069     p.end();
00070   }
00071   void setColor( const QColor& c )
00072   {
00073     myColor = c;
00074     update();
00075   }
00076   
00077 private:
00078   QColor myColor;
00079 };
00080 
00093 QtxBiColorTool::QtxBiColorTool( QWidget* parent )
00094   : QWidget( parent )
00095 {
00096   QHBoxLayout* l = new QHBoxLayout( this );
00097   l->setMargin( 0 );
00098   l->setSpacing( 5 );
00099 
00100   myMainColor = new QtxColorButton( this );
00101   myMainColor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00102   myExtraText = new QLabel( this );
00103   myRuler = new QSlider( Qt::Horizontal, this );
00104   myRuler->setMinimum( -BICOLOR_MAX_DELTA );
00105   myRuler->setMaximum( +BICOLOR_MAX_DELTA );
00106   myRuler->setSingleStep( 1 );
00107   myRuler->setPageStep( 10 );
00108   myRuler->setValue( 0 );
00109   myRuler->setTickPosition( QSlider::NoTicks );
00110   myDelta = new ColorLabel( this );
00111 
00112   l->addWidget( myMainColor );
00113   l->addWidget( myExtraText );
00114   l->addWidget( myRuler );
00115   l->addWidget( myDelta );
00116 
00117   connect( myMainColor, SIGNAL( changed( QColor ) ),   this, SLOT( updateState() ) );
00118   connect( myRuler,     SIGNAL( valueChanged( int ) ), this, SLOT( updateState() ) );
00119 
00120   updateState();
00121 }
00122 
00126 QtxBiColorTool::~QtxBiColorTool()
00127 {
00128 }
00129 
00138 QColor QtxBiColorTool::mainColor() const
00139 {
00140   return myMainColor->color();
00141 }
00142 
00148 void QtxBiColorTool::setMainColor( const QColor& c )
00149 {
00150   myMainColor->setColor( c );
00151   updateState();
00152 }
00153 
00159 int QtxBiColorTool::delta() const
00160 {
00161   return myRuler->value();
00162 }
00163 
00169 void QtxBiColorTool::setDelta( int d )
00170 {
00171   myRuler->setValue( d );
00172   updateState();
00173 }
00174 
00185 QColor QtxBiColorTool::secondaryColor() const
00186 {
00187   return Qtx::mainColorToSecondary( mainColor(), delta() );
00188 }
00189 
00195 QString QtxBiColorTool::text() const
00196 {
00197   return myExtraText->text();
00198 }
00199 
00205 void QtxBiColorTool::setText( const QString& txt )
00206 {
00207   myExtraText->setText( txt );
00208 }
00209 
00213 void QtxBiColorTool::updateState()
00214 {
00215   myDelta->setColor( secondaryColor() );
00216 }