1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: QtxBiColorTool.cxx
21 // Author: Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
23 #include "QtxBiColorTool.h"
24 #include "QtxColorButton.h"
26 #include <QHBoxLayout>
31 #include <QStylePainter>
32 #include <QStyleOptionButton>
34 const int BICOLOR_MAX_DELTA = 100;
37 \class QtxBiColorTool::ColorLabel
38 \brief Draw colored label (for secondary color)
41 class QtxBiColorTool::ColorLabel: public QFrame
44 ColorLabel( QWidget* parent) : QFrame( parent )
46 setFrameStyle( QFrame::Panel | QFrame::Raised );
47 //setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
50 QSize sizeHint() const
52 return minimumSizeHint();
54 QSize minimumSizeHint() const
56 if ( !mySizeHint.isValid() ) {
57 int is = style()->pixelMetric( QStyle::PM_ButtonIconSize, 0, this );
58 int pm = style()->pixelMetric( QStyle::PM_ButtonMargin );
59 ColorLabel* that = const_cast<ColorLabel*>( this );
60 that->mySizeHint = QSize( is + pm, is + pm );
64 void paintEvent( QPaintEvent* e )
66 QStylePainter sp(this);
67 QStyleOptionButton option;
68 option.initFrom(this);
69 option.features = QStyleOptionButton::None;
70 option.state |= QStyle::State_Raised;
71 sp.drawControl( QStyle::CE_PushButton, option );
74 r.setTopLeft( r.topLeft() + QPoint( 2, 2 ) );
75 r.setBottomRight( r.bottomRight() - QPoint( 2, 2 ) );
77 QPixmap pix( r.size() );
78 pix.fill( palette().color( backgroundRole() ) );
80 if ( myColor.isValid() ) {
81 QPainter pixp( &pix );
82 pixp.setPen( isEnabled() ? Qt::black : palette().mid().color() );
83 pixp.fillRect( 1, 1, pix.width()-3, pix.height()- 3, QBrush( isEnabled() ? myColor : palette().mid().color() ) );
84 pixp.drawRect( 1, 1, pix.width()-3, pix.height()- 3 );
88 QPainter pixp( &pix );
89 pixp.setPen( palette().color( isEnabled() ? QPalette::WindowText : QPalette::Mid ) );
90 pixp.drawRect( 2, 2, pix.width() - 4, pix.height() - 4 );
91 pixp.fillRect( 3, 3, pix.width() - 6, pix.height() - 6,
92 QBrush( palette().color( isEnabled() ? QPalette::WindowText : QPalette::Mid ), Qt::BDiagPattern ) );
97 p.drawPixmap( r, pix );
100 void setColor( const QColor& c )
112 \class QtxBiColorTool
113 \brief Implementation of the widget managing a couple of colors.
115 The main color is specified explicitly. The secondary color is calculated
116 by changing "value" of the main color in HSV notation to the specified delta.
121 \param parent parent widget
123 QtxBiColorTool::QtxBiColorTool( QWidget* parent )
126 QHBoxLayout* l = new QHBoxLayout( this );
130 myMainColor = new QtxColorButton( this );
131 //myMainColor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
132 myExtraText = new QLabel( this );
133 myRuler = new QSlider( Qt::Horizontal, this );
134 myRuler->setMinimum( -BICOLOR_MAX_DELTA );
135 myRuler->setMaximum( +BICOLOR_MAX_DELTA );
136 myRuler->setSingleStep( 1 );
137 myRuler->setPageStep( 10 );
138 myRuler->setValue( 0 );
139 myRuler->setTickPosition( QSlider::NoTicks );
140 myDelta = new ColorLabel( this );
142 l->addWidget( myMainColor );
143 l->addWidget( myExtraText );
144 l->addWidget( myRuler );
145 l->addWidget( myDelta );
146 myRuler->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
148 connect( myMainColor, SIGNAL( changed( QColor ) ), this, SLOT( updateState() ) );
149 connect( myRuler, SIGNAL( valueChanged( int ) ), this, SLOT( updateState() ) );
157 QtxBiColorTool::~QtxBiColorTool()
162 \brief Get currently selected main color
164 Returns invalid QColor if no color is selected.
166 \return selected main color
169 QColor QtxBiColorTool::mainColor() const
171 return myMainColor->color();
175 \brief Set main color.
176 \param c color to be set as current main color
179 void QtxBiColorTool::setMainColor( const QColor& c )
181 myMainColor->setColor( c );
186 \brief Get current value delta for the secondary color
187 \return curent color value delta
188 \sa setDelta(), secondaryColor()
190 int QtxBiColorTool::delta() const
192 return myRuler->value();
196 \brief Set value delta for the secondary color
197 \param d new color value delta
198 \sa delta(), secondaryColor()
200 void QtxBiColorTool::setDelta( int d )
202 myRuler->setValue( d );
207 \brief Get secondary color.
209 Returns invalid QColor if no main color is selected.
210 Secondary color is calculated by changing "value" of the main color
211 in HSV notation to the specified delta.
213 \return secondary color
214 \sa mainColor(), setMainColor(), delta(), setDelta()
216 QColor QtxBiColorTool::secondaryColor() const
218 return Qtx::mainColorToSecondary( mainColor(), delta() );
222 \brief Returns auxiliary text assigned to the widget
223 \return current widget text
226 QString QtxBiColorTool::text() const
228 return myExtraText->text();
232 \brief Assign auxiliary text to the widet
233 \param txt new widget text
236 void QtxBiColorTool::setText( const QString& txt )
238 myExtraText->setText( txt );
242 \brief Get access to the internal label use for drawing
243 auxiliary test assigned to the widget
245 QLabel* QtxBiColorTool::label()
251 \brief Update widget state
253 void QtxBiColorTool::updateState()
255 myDelta->setColor( secondaryColor() );