karbon

vcolorslider.cc

00001 /* This file is part of the KDE project
00002    Made by Tomislav Lukman (tomislav.lukman@ck.tel.hr)
00003    Copyright (C) 2002, The Karbon Developers
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 /* vcolorslider.cc */
00022 
00023 #include <qlayout.h>
00024 #include <qlabel.h>
00025 #include <knuminput.h>
00026 #include <kselect.h>
00027 
00028 #include "vcolorslider.h"
00029 
00030 VColorSlider::VColorSlider( QWidget* parent, const char* name )
00031     : QWidget( parent, name )
00032 {
00033     init();
00034 }
00035 
00036 // Label, left color, right color, min, max, value ...
00037 VColorSlider::VColorSlider( const QString& label, const QColor& col1,
00038     const QColor& col2, int min, int max, int value, QWidget* parent, const char* name )
00039         : QWidget( parent, name )
00040 {
00041     init();
00042     setLabel( label );
00043     setColors( col1, col2 );
00044     setMinValue( min );
00045     setMaxValue( max );
00046     setValue( value );
00047 }
00048 
00049 VColorSlider::~VColorSlider()
00050 {
00051 }
00052 
00053 void VColorSlider::init()
00054 {
00055     m_isDragging = false;
00056     QHBoxLayout *layout = new QHBoxLayout( this, 3 );
00057 
00058     m_label = new QLabel( this );
00059     m_gradientSelect = new KGradientSelector( KSelector::Horizontal, this );
00060     m_spinBox = new KIntSpinBox( this );
00061 
00062     layout->addWidget( m_label );
00063     layout->addWidget( m_gradientSelect, 2 );
00064     layout->addWidget( m_spinBox );
00065 
00066     setValue( 0 );
00067     setMinValue( 0 );
00068     setMaxValue( 255 );
00069 
00070     connect( m_spinBox, SIGNAL( valueChanged ( int ) ), this, SLOT( updateFrom_spinBox( int ) ) );
00071     connect( m_gradientSelect, SIGNAL( valueChanged ( int ) ), this, SLOT( updateFrom_gradientSelect( int ) ) );
00072 
00073     m_gradientSelect->installEventFilter( this );
00074 
00075     layout->activate();
00076 }
00077 
00078 void VColorSlider::setLabel( const QString& label )
00079 {
00080     m_label->setText( label );
00081 }
00082 
00083 void VColorSlider::setColors( const QColor& color1, const QColor& color2 )
00084 {
00085     m_gradientSelect->setColors( color1, color2 );
00086 }
00087 
00088 void VColorSlider::setValue( int value )
00089 {
00090     m_spinBox->setValue( value );
00091     m_gradientSelect->setValue( (m_maxValue - value) + m_minValue );
00092 }
00093 
00094 void VColorSlider::setMinValue( int value )
00095 {
00096     m_minValue = value;
00097     m_spinBox->setMinValue( value );
00098     m_gradientSelect->setMinValue( value );
00099 }
00100 
00101 void VColorSlider::setMaxValue( int value )
00102 {
00103     m_maxValue = value;
00104     m_spinBox->setMaxValue( value );
00105     m_gradientSelect->setMaxValue( value );
00106 }
00107 
00108 int VColorSlider::value()
00109 {
00110     return( m_spinBox->value() );
00111 }
00112 
00113 void VColorSlider::updateFrom_spinBox( int value )
00114 {
00115     if ( value != m_gradientSelect->value() )
00116     {
00117         disconnect( m_gradientSelect, SIGNAL( valueChanged ( int ) ), this, SLOT( updateFrom_gradientSelect( int ) ) );
00118         m_gradientSelect->setValue( (m_maxValue - value) + m_minValue );
00119         connect( m_gradientSelect, SIGNAL( valueChanged ( int ) ), this, SLOT( updateFrom_gradientSelect( int ) ) );
00120         emit valueChanged( value );
00121     }
00122 }
00123 
00124 void VColorSlider::updateFrom_gradientSelect( int value )
00125 {
00126     value = (m_maxValue - value) + m_minValue;
00127     if ( value != m_spinBox->value() )
00128     {
00129         disconnect( m_spinBox, SIGNAL( valueChanged ( int ) ), this, SLOT( updateFrom_spinBox( int ) ) );
00130         m_spinBox->setValue( value );
00131         connect( m_spinBox, SIGNAL( valueChanged ( int ) ), this, SLOT( updateFrom_spinBox( int ) ) );
00132         emit valueChanged( value );
00133     }
00134 }
00135 
00136 bool VColorSlider::eventFilter( QObject *obj, QEvent *ev )
00137 {
00138     if( obj == m_gradientSelect ) 
00139     {
00140         if ( ev->type() == QEvent::MouseButtonPress ) 
00141             m_isDragging = true;
00142         else if( ev->type() == QEvent::MouseButtonRelease )
00143             m_isDragging = false;
00144     } 
00145     return FALSE;
00146 }
00147 
00148 #include "vcolorslider.moc"
00149 
KDE Home | KDE Accessibility Home | Description of Access Keys