Salome HOME
updated copyright message
[modules/gui.git] / src / Qtx / QtxBiColorTool.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:      QtxBiColorTool.cxx
21 // Author:    Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
22
23 #include "QtxBiColorTool.h"
24 #include "QtxColorButton.h"
25
26 #include <QHBoxLayout>
27 #include <QLabel>
28 #include <QPainter>
29 #include <QSlider>
30 #include <QStyle>
31 #include <QStylePainter>
32 #include <QStyleOptionButton>
33
34 const int BICOLOR_MAX_DELTA = 100;
35
36 /*!
37   \class QtxBiColorTool::ColorLabel
38   \brief Draw colored label (for secondary color)
39   \internal
40 */
41 class QtxBiColorTool::ColorLabel: public QFrame
42 {
43 public:
44   ColorLabel( QWidget* parent) : QFrame( parent )
45   {
46     setFrameStyle( QFrame::Panel | QFrame::Raised );
47     //setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
48   }
49   ~ColorLabel() {}
50   QSize sizeHint() const
51   {
52     return minimumSizeHint();
53   }
54   QSize minimumSizeHint() const
55   {
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 );
61     }
62     return mySizeHint; 
63   }
64   void paintEvent( QPaintEvent* /*e*/ )
65   {
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 );
72
73     QRect r = rect();
74     r.setTopLeft( r.topLeft() + QPoint( 2, 2 ) );
75     r.setBottomRight( r.bottomRight() - QPoint( 2, 2 ) );
76
77     QPixmap pix( r.size() );
78     pix.fill( palette().color( backgroundRole() ) );
79
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 );
85       pixp.end();
86     }
87     else {
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 ) );
93       pixp.end();
94     }
95
96     QPainter p( this );
97     p.drawPixmap( r, pix );
98     p.end();
99   }
100   void setColor( const QColor& c )
101   {
102     myColor = c;
103     update();
104   }
105   
106 private:
107   QSize  mySizeHint;
108   QColor myColor;
109 };
110
111 /*!
112   \class QtxBiColorTool
113   \brief Implementation of the widget managing a couple of colors.
114
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.
117 */
118
119 /*!
120   \brief Constructor.
121   \param parent parent widget
122 */
123 QtxBiColorTool::QtxBiColorTool( QWidget* parent )
124   : QWidget( parent )
125 {
126   QHBoxLayout* l = new QHBoxLayout( this );
127   l->setMargin( 0 );
128   l->setSpacing( 5 );
129
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 );
141
142   l->addWidget( myMainColor );
143   l->addWidget( myExtraText );
144   l->addWidget( myRuler );
145   l->addWidget( myDelta );
146   myRuler->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
147
148   connect( myMainColor, SIGNAL( changed( QColor ) ),   this, SLOT( updateState() ) );
149   connect( myRuler,     SIGNAL( valueChanged( int ) ), this, SLOT( updateState() ) );
150
151   updateState();
152 }
153
154 /*!
155   \brief Destructor.
156 */
157 QtxBiColorTool::~QtxBiColorTool()
158 {
159 }
160
161 /*!
162   \brief Get currently selected main color
163
164   Returns invalid QColor if no color is selected.
165
166   \return selected main color
167   \sa setMainColor()
168 */
169 QColor QtxBiColorTool::mainColor() const
170 {
171   return myMainColor->color();
172 }
173
174 /*!
175   \brief Set main color.
176   \param c color to be set as current main color
177   \sa mainColor()
178 */
179 void QtxBiColorTool::setMainColor( const QColor& c )
180 {
181   myMainColor->setColor( c );
182   updateState();
183 }
184
185 /*!
186   \brief Get current value delta for the secondary color
187   \return curent color value delta
188   \sa setDelta(), secondaryColor()
189 */
190 int QtxBiColorTool::delta() const
191 {
192   return myRuler->value();
193 }
194
195 /*!
196   \brief Set value delta for the secondary color
197   \param d new color value delta
198   \sa delta(), secondaryColor()
199 */
200 void QtxBiColorTool::setDelta( int d )
201 {
202   myRuler->setValue( d );
203   updateState();
204 }
205
206 /*!
207   \brief Get secondary color.
208
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.
212
213   \return secondary color
214   \sa mainColor(), setMainColor(), delta(), setDelta()
215 */
216 QColor QtxBiColorTool::secondaryColor() const
217 {
218   return Qtx::mainColorToSecondary( mainColor(), delta() );
219 }
220
221 /*!
222   \brief Returns auxiliary text assigned to the widget
223   \return current widget text
224   \sa setText()
225 */
226 QString QtxBiColorTool::text() const
227 {
228   return myExtraText->text();
229 }
230
231 /*!
232   \brief Assign auxiliary text to the widet
233   \param txt new widget text
234   \sa text()
235 */
236 void QtxBiColorTool::setText( const QString& txt )
237 {
238   myExtraText->setText( txt );
239 }
240
241 /*!
242   \brief Get access to the internal label use for drawing 
243   auxiliary test assigned to the widget
244 */
245 QLabel* QtxBiColorTool::label()
246 {
247   return myExtraText;
248 }
249
250 /*!
251   \brief Update widget state
252 */
253 void QtxBiColorTool::updateState()
254 {
255   myDelta->setColor( secondaryColor() );
256 }