lib

ko_gray_widget.cc

00001 /* 
00002  * Copyright (c) 1999 Matthias Elter (me@kde.org)
00003  * Copyright (c) 2001-2002 Igor Jansen (rm@kde.org)
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program 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
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "ko_gray_widget.h"
00021 
00022 #include <qlayout.h>
00023 #include <qhbox.h>
00024 #include <qlabel.h>
00025 #include <qspinbox.h>
00026 #include <qcolor.h>
00027 
00028 #include <kdebug.h>
00029 
00030 #include <koFrameButton.h>
00031 #include <koColorSlider.h>
00032 #include <kcolordialog.h>
00033 
00034 KoGrayWidget::KoGrayWidget(QWidget *parent, const char *name) : super(parent, name)
00035 {
00036     m_ColorButton = new KDualColorButton(this);
00037     Q_CHECK_PTR(m_ColorButton);
00038 
00039     m_ColorButton ->  setFixedSize(m_ColorButton->sizeHint());
00040     QGridLayout *mGrid = new QGridLayout(this, 3, 5, 5, 2);
00041 
00042     /* setup color sliders */
00043     mSlider = new KoColorSlider(this);
00044     mSlider->setFocusPolicy( QWidget::ClickFocus );
00045     mSlider->setMaximumHeight(20);
00046     mSlider->slotSetRange(0, 255);
00047     mSlider->slotSetColor1(QColor(255, 255, 255));
00048     mSlider->slotSetColor2(QColor(0, 0, 0));
00049 
00050     /* setup slider labels */
00051     mLabel = new QLabel("K:", this);
00052     mLabel->setFixedWidth(12);
00053     mLabel->setFixedHeight(20);
00054 
00055     /* setup spin box */
00056     mIn = new QSpinBox(0, 255, 1, this);
00057     mIn->setFixedWidth(50);
00058     mIn->setFixedHeight(20);
00059     mIn->setFocusPolicy( QWidget::ClickFocus );
00060 
00061     mGrid->addMultiCellWidget(m_ColorButton, 0, 3, 0, 0, Qt::AlignTop);
00062     mGrid->addWidget(mLabel, 0, 1);
00063     mGrid->addMultiCellWidget(mSlider, 0, 0, 2, 3);
00064     mGrid->addWidget(mIn, 0, 4);
00065 
00066     connect(m_ColorButton, SIGNAL(fgChanged(const QColor &)), this, SLOT(slotFGColorSelected(const QColor &)));
00067     connect(m_ColorButton, SIGNAL(bgChanged(const QColor &)), this, SLOT(slotBGColorSelected(const QColor &)));
00068 
00069     /* connect color slider */
00070     connect(mSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChanged(int)));
00071 
00072     /* connect spin box */
00073     connect(mIn, SIGNAL(valueChanged(int)), this, SLOT(slotChanged(int)));
00074 }
00075 
00076 void KoGrayWidget::slotChanged(int v)
00077 {
00078     v = 255 - v;
00079 
00080     if (m_ColorButton->current() == KDualColorButton::Foreground){
00081         slotFGColorSelected( QColor( v, v, v));
00082     }
00083     else{
00084         slotBGColorSelected( QColor( v, v, v));
00085     }
00086 }
00087 
00088 void KoGrayWidget::setFgColor(const QColor & c)
00089 {
00090     blockSignals(true);
00091     slotFGColorSelected(c);
00092     blockSignals(false);
00093 }
00094 
00095 void KoGrayWidget::setBgColor(const QColor & c)
00096 {
00097     blockSignals(true);
00098     slotBGColorSelected(c);
00099     blockSignals(false);
00100 }
00101 
00102 void KoGrayWidget::update(const QColor & fgColor, const QColor & bgColor)
00103 {
00104     
00105     m_fgColor = fgColor;
00106     m_bgColor = bgColor;
00107 
00108     QColor color = (m_ColorButton->current() == KDualColorButton::Foreground)? m_fgColor : m_bgColor;
00109 
00110     disconnect(mSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChanged(int)));
00111     disconnect(mIn, SIGNAL(valueChanged(int)), this, SLOT(slotChanged(int)));
00112 
00113     mIn->blockSignals(true);
00114     mSlider->blockSignals(true);
00115     double v = color.red() + color.green() + color.blue();
00116     v /= 3.0;
00117     v = 255.0 - v;
00118     mIn->setValue(static_cast<int>(v));
00119     mSlider->slotSetValue(static_cast<int>(v));
00120     mIn->blockSignals(false);
00121     mSlider->blockSignals(false);
00122 
00123     connect(mSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChanged(int)));
00124     connect(mIn, SIGNAL(valueChanged(int)), this, SLOT(slotChanged(int)));
00125 }
00126 
00127 void KoGrayWidget::slotFGColorSelected(const QColor& c)
00128 {
00129     m_fgColor = c;
00130 
00131     disconnect(m_ColorButton, SIGNAL(fgChanged(const QColor &)), this, SLOT(slotFGColorSelected(const QColor &)));
00132     m_ColorButton->setForeground( m_fgColor );
00133     connect(m_ColorButton, SIGNAL(fgChanged(const QColor &)), this, SLOT(slotFGColorSelected(const QColor &)));
00134 
00135     emit sigFgColorChanged(m_fgColor);
00136 }
00137 
00138 void KoGrayWidget::slotBGColorSelected(const QColor& c)
00139 {
00140     m_bgColor = c;
00141 
00142     disconnect(m_ColorButton, SIGNAL(bgChanged(const QColor &)), this, SLOT(slotBGColorSelected(const QColor &)));
00143     m_ColorButton->setBackground( m_bgColor );
00144     connect(m_ColorButton, SIGNAL(bgChanged(const QColor &)), this, SLOT(slotBGColorSelected(const QColor &)));
00145 
00146     emit sigBgColorChanged(m_bgColor);
00147 }
00148 
00149 void KoGrayWidget::currentChanged(KDualColorButton::DualColor s)
00150 {
00151    if(s == KDualColorButton::Foreground)
00152      slotFGColorSelected(m_ColorButton->currentColor());
00153    else
00154      slotBGColorSelected(m_ColorButton->currentColor());
00155 }
00156 
00157 #include "ko_gray_widget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys