lib

ko_rgb_widget.cc

00001 /*
00002  * This file is part of Krita
00003  *
00004  * Copyright (c) 1999 Matthias Elter (me@kde.org)
00005  * Copyright (c) 2001-2002 Igor Jansen (rm@kde.org)
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "ko_rgb_widget.h"
00023 
00024 #include <qlayout.h>
00025 #include <qhbox.h>
00026 #include <qlabel.h>
00027 #include <qspinbox.h>
00028 #include <qtooltip.h>
00029 #include <qcolor.h>
00030 
00031 #include <kdebug.h>
00032 #include <klocale.h>
00033 
00034 #include <koFrameButton.h>
00035 #include <koColorSlider.h>
00036 #include <kcolordialog.h>
00037 
00038 KoRGBWidget::KoRGBWidget(QWidget *parent, const char *name) : super(parent, name)
00039 {
00040     m_ColorButton = new KDualColorButton(this);
00041     m_ColorButton ->  setFixedSize(m_ColorButton->sizeHint());
00042     QGridLayout *mGrid = new QGridLayout(this, 3, 5, 5, 2);
00043 
00044     /* setup color sliders */
00045     mRSlider = new KoColorSlider(this);
00046     mRSlider->setMaximumHeight(20);
00047     mRSlider->slotSetRange(0, 255);
00048     mRSlider->setFocusPolicy( QWidget::ClickFocus );
00049 
00050     mGSlider = new KoColorSlider(this);
00051     mGSlider->setMaximumHeight(20);
00052     mGSlider->slotSetRange(0, 255);
00053     mGSlider->setFocusPolicy( QWidget::ClickFocus );
00054 
00055     mBSlider = new KoColorSlider(this);
00056     mBSlider->setMaximumHeight(20);
00057     mBSlider->slotSetRange(0, 255);
00058     mBSlider->setFocusPolicy( QWidget::ClickFocus );
00059 
00060     /* setup slider labels */
00061     mRLabel = new QLabel("R:", this);
00062     mRLabel->setFixedWidth(12);
00063     mRLabel->setFixedHeight(20);
00064     mGLabel = new QLabel("G:", this);
00065     mGLabel->setFixedWidth(12);
00066     mGLabel->setFixedHeight(20);
00067     mBLabel = new QLabel("B:", this);
00068     mBLabel->setFixedWidth(12);
00069     mBLabel->setFixedHeight(20);
00070 
00071     /* setup spin box */
00072     mRIn = new QSpinBox(0, 255, 1, this);
00073     mRIn->setFixedWidth(50);
00074     mRIn->setFixedHeight(20);
00075     mRIn->setFocusPolicy( QWidget::ClickFocus );
00076     QToolTip::add( mRIn, i18n( "Red" ) );
00077 
00078     mGIn = new QSpinBox(0, 255, 1, this);
00079     mGIn->setFixedWidth(50);
00080     mGIn->setFixedHeight(20);
00081     mGIn->setFocusPolicy( QWidget::ClickFocus );
00082     QToolTip::add( mGIn, i18n( "Green" ) );
00083 
00084     mBIn = new QSpinBox(0, 255, 1, this);
00085     mBIn->setFixedWidth(50);
00086     mBIn->setFixedHeight(20);
00087     mBIn->setFocusPolicy( QWidget::ClickFocus );
00088     QToolTip::add( mBIn, i18n( "Blue" ) );
00089 
00090     mGrid->addMultiCellWidget(m_ColorButton, 0, 3, 0, 0, Qt::AlignTop);
00091     mGrid->addWidget(mRLabel, 0, 1);
00092     mGrid->addWidget(mGLabel, 1, 1);
00093     mGrid->addWidget(mBLabel, 2, 1);
00094     mGrid->addMultiCellWidget(mRSlider, 0, 0, 2, 3);
00095     mGrid->addMultiCellWidget(mGSlider, 1, 1, 2, 3);
00096     mGrid->addMultiCellWidget(mBSlider, 2, 2, 2, 3);
00097     mGrid->addWidget(mRIn, 0, 4);
00098     mGrid->addWidget(mGIn, 1, 4);
00099     mGrid->addWidget(mBIn, 2, 4);
00100 
00101     connect(m_ColorButton, SIGNAL(fgChanged(const QColor &)), this, SLOT(slotFGColorSelected(const QColor &)));
00102     connect(m_ColorButton, SIGNAL(bgChanged(const QColor &)), this, SLOT(slotBGColorSelected(const QColor &)));
00103     connect(m_ColorButton, SIGNAL(currentChanged(KDualColorButton::DualColor)), this, SLOT(currentChanged(KDualColorButton::DualColor)));
00104 
00105     /* connect color sliders */
00106     connect(mRSlider, SIGNAL(valueChanged(int)), this, SLOT(slotRChanged(int)));
00107     connect(mGSlider, SIGNAL(valueChanged(int)), this, SLOT(slotGChanged(int)));
00108     connect(mBSlider, SIGNAL(valueChanged(int)), this, SLOT(slotBChanged(int)));
00109 
00110     /* connect spin box */
00111     connect(mRIn, SIGNAL(valueChanged(int)), this, SLOT(slotRChanged(int)));
00112     connect(mGIn, SIGNAL(valueChanged(int)), this, SLOT(slotGChanged(int)));
00113     connect(mBIn, SIGNAL(valueChanged(int)), this, SLOT(slotBChanged(int)));
00114 
00115     update(Qt::black, Qt::white);
00116 }
00117 
00118 void KoRGBWidget::slotRChanged(int r)
00119 {
00120     if (m_ColorButton->current() == KDualColorButton::Foreground)
00121         slotFGColorSelected( QColor(r, m_fgColor.green(), m_fgColor.blue()));
00122     else
00123         slotBGColorSelected( QColor(r, m_bgColor.green(), m_bgColor.blue()));
00124 }
00125 
00126 void KoRGBWidget::slotGChanged(int g)
00127 {
00128     if (m_ColorButton->current() == KDualColorButton::Foreground)
00129         slotFGColorSelected( QColor( m_fgColor.red(), g, m_fgColor.blue()));
00130     else
00131         slotBGColorSelected( QColor( m_bgColor.red(), g, m_bgColor.blue()));;
00132 }
00133 
00134 void KoRGBWidget::slotBChanged(int b)
00135 {
00136     if (m_ColorButton->current() == KDualColorButton::Foreground)
00137         slotFGColorSelected( QColor( m_fgColor.red(), m_fgColor.green(), b));
00138     else
00139         slotBGColorSelected( QColor( m_bgColor.red(), m_bgColor.green(), b));
00140 }
00141 
00142 void KoRGBWidget::setFgColor(const QColor & c)
00143 {
00144     blockSignals(true);
00145     slotFGColorSelected(c);
00146     blockSignals(false);
00147 }
00148 
00149 void KoRGBWidget::setBgColor(const QColor & c)
00150 {
00151     blockSignals(true);
00152     slotBGColorSelected(c);
00153     blockSignals(false);
00154 }
00155 
00156 void KoRGBWidget::update(const QColor fgColor, const QColor bgColor)
00157 {
00158 
00159     m_fgColor = fgColor;
00160     m_bgColor = bgColor;
00161 
00162     QColor color = (m_ColorButton->current() == KDualColorButton::Foreground)? m_fgColor : m_bgColor;
00163 
00164     int r = color.red();
00165     int g = color.green();
00166     int b = color.blue();
00167 
00168     mRSlider->blockSignals(true);
00169     mRIn->blockSignals(true);
00170     mGSlider->blockSignals(true);
00171     mGIn->blockSignals(true);
00172     mBSlider->blockSignals(true);
00173     mBIn->blockSignals(true);
00174 
00175     mRSlider->slotSetColor1(QColor(0, g, b));
00176     mRSlider->slotSetColor2(QColor(255, g, b));
00177     mRSlider->slotSetValue(r);
00178     mRIn->setValue(r);
00179 
00180     mGSlider->slotSetColor1(QColor(r, 0, b));
00181     mGSlider->slotSetColor2(QColor(r, 255, b));
00182     mGSlider->slotSetValue(g);
00183     mGIn->setValue(g);
00184 
00185     mBSlider->slotSetColor1(QColor(r, g, 0));
00186     mBSlider->slotSetColor2(QColor(r, g, 255));
00187     mBSlider->slotSetValue(b);
00188     mBIn->setValue(b);
00189 
00190     mRSlider->blockSignals(false);
00191     mRIn->blockSignals(false);
00192     mGSlider->blockSignals(false);
00193     mGIn->blockSignals(false);
00194     mBSlider->blockSignals(false);
00195     mBIn->blockSignals(false);
00196 }
00197 
00198 void KoRGBWidget::slotFGColorSelected(const QColor& c)
00199 {
00200     m_fgColor = c;
00201     disconnect(m_ColorButton, SIGNAL(fgChanged(const QColor &)), this, SLOT(slotFGColorSelected(const QColor &)));
00202     m_ColorButton->setForeground( m_fgColor );
00203     connect(m_ColorButton, SIGNAL(fgChanged(const QColor &)), this, SLOT(slotFGColorSelected(const QColor &)));
00204 
00205     update( m_fgColor, m_bgColor);
00206     emit sigFgColorChanged(m_fgColor);
00207 }
00208 
00209 void KoRGBWidget::slotBGColorSelected(const QColor& c)
00210 {
00211     m_bgColor = c;
00212 
00213     disconnect(m_ColorButton, SIGNAL(bgChanged(const QColor &)), this, SLOT(slotBGColorSelected(const QColor &)));
00214     m_ColorButton->setBackground( m_bgColor );
00215     connect(m_ColorButton, SIGNAL(bgChanged(const QColor &)), this, SLOT(slotBGColorSelected(const QColor &)));
00216 
00217     update(m_fgColor, m_bgColor);
00218     emit sigBgColorChanged(m_bgColor);
00219 }
00220 
00221 void KoRGBWidget::currentChanged(KDualColorButton::DualColor s)
00222 {
00223    if(s == KDualColorButton::Foreground)
00224      slotFGColorSelected(m_ColorButton->currentColor());
00225    else
00226      slotBGColorSelected(m_ColorButton->currentColor());
00227 }
00228 
00229 #include "ko_rgb_widget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys