KoUniColorChooser.cpp 9.44 KB
Newer Older
1 2 3
/*
 * Copyright (c) 2006 Casper Boemann (cbr@boemann.dk)
 *
4 5 6 7
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
8
 *
9 10 11 12
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
13
 *
14 15 16 17
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
*/

#include <kselector.h>
#include <QLayout>
#include <QLabel>
#include <QSpinBox>
#include <QRadioButton>
#include <QToolTip>
#include <QGridLayout>
#include <kcolordialog.h>

#include "koFrameButton.h"
#include "koColor.h"
#include <kdebug.h>
#include <klocale.h>

#include "ko_color_wheel.h"

#include "KoUniColorChooser.h"

KoUniColorChooser::KoUniColorChooser(QWidget *parent, const char *name) : super(parent)
{
    setObjectName(name);

    QGridLayout *mGrid = new QGridLayout;

    m_colorwheel = new KoOldColorWheel(this);
    m_colorwheel->setFixedSize(120, 120);

    m_VSelector = new KValueSelector(Qt::Vertical, this);
    m_VSelector->setFixedSize(25, 100);

    m_colorpatch = new QFrame(this);
    m_colorpatch->setFixedSize(18, 18);
    m_colorpatch->setFrameStyle(QFrame::Panel|QFrame::Sunken);

    /* setup channel labels */
    m_HLabel = new QLabel("H:", this);
    m_HLabel->setFixedSize(10, 18);
    m_SLabel = new QLabel("S:", this);
    m_SLabel->setFixedSize(10, 18);
    m_VLabel = new QLabel("V:", this);
    m_VLabel->setFixedSize(10, 18);
    m_RLabel = new QLabel("R:", this);
    m_RLabel->setFixedSize(10, 18);
    m_GLabel = new QLabel("G:", this);
    m_GLabel->setFixedSize(10, 18);
    m_BLabel = new QLabel("B:", this);
    m_BLabel->setFixedSize(10, 18);
    m_LLabel = new QLabel("L:", this);
    m_LLabel->setFixedSize(10, 18);
    m_aLabel = new QLabel("a:", this);
    m_aLabel->setFixedSize(10, 18);
    m_bLabel = new QLabel("b:", this);
    m_bLabel->setFixedSize(10, 18);

    /* setup sradio buttons */
    m_HRB = new QRadioButton(this);
    m_SRB = new QRadioButton(this);
    m_VRB = new QRadioButton(this);
    m_RRB = new QRadioButton(this);
    m_GRB = new QRadioButton(this);
    m_BRB = new QRadioButton(this);
    m_LRB = new QRadioButton(this);
    m_aRB = new QRadioButton(this);
    m_bRB = new QRadioButton(this);
    
    /* setup spin box */
    m_HIn = new QSpinBox(this);
    m_HIn->setMinimum(0);
    m_HIn->setMaximum(359);   // Is this correct? 359?
    m_HIn->setSingleStep(1);
    m_HIn->setFixedSize(40, 18);
    m_HIn->setFocusPolicy( Qt::ClickFocus );
    m_HIn->setToolTip( i18n( "Hue" ) );

    m_SIn = new QSpinBox(this);
    m_SIn->setMinimum(0);
    m_SIn->setMaximum(255);
    m_SIn->setSingleStep(1);
    m_SIn->setFixedSize(40, 18);
    m_SIn->setFocusPolicy( Qt::ClickFocus );
    m_SIn->setToolTip( i18n( "Saturation" ) );

    m_VIn = new QSpinBox(this);
    m_VIn->setMinimum(0);
    m_VIn->setMaximum(255);
    m_VIn->setSingleStep(1);
    m_VIn->setFixedSize(40, 18);
    m_VIn->setFocusPolicy( Qt::ClickFocus );
    m_VIn->setToolTip( i18n( "Value (brightness)" ) );

    m_RIn = new QSpinBox(this);
    m_RIn->setMinimum(0);
    m_RIn->setMaximum(255);
    m_RIn->setSingleStep(1);
    m_RIn->setFixedSize(40, 18);
    m_RIn->setFocusPolicy( Qt::ClickFocus );
    m_RIn->setToolTip( i18n( "Red" ) );

    m_GIn = new QSpinBox(this);
    m_GIn->setMinimum(0);
    m_GIn->setMaximum(255);
    m_GIn->setSingleStep(1);
    m_GIn->setFixedSize(40, 18);
    m_GIn->setFocusPolicy( Qt::ClickFocus );
    m_GIn->setToolTip( i18n( "Green" ) );

    m_BIn = new QSpinBox(this);
    m_BIn->setMinimum(0);
    m_BIn->setMaximum(255);
    m_BIn->setSingleStep(1);
    m_BIn->setFixedSize(40, 18);
    m_BIn->setFocusPolicy( Qt::ClickFocus );
    m_BIn->setToolTip( i18n( "Blue" ) );

    m_LIn = new QSpinBox(this);
    m_LIn->setMinimum(0);
    m_LIn->setMaximum(100);
    m_LIn->setSingleStep(1);
    m_LIn->setFixedSize(40, 18);
    m_LIn->setFocusPolicy( Qt::ClickFocus );
    m_LIn->setToolTip( i18n( "Lightness" ) );

    m_aIn = new QSpinBox(this);
    m_aIn->setMinimum(0);
    m_aIn->setMaximum(255);
    m_aIn->setSingleStep(1);
    m_aIn->setFixedSize(40, 18);
    m_aIn->setFocusPolicy( Qt::ClickFocus );
148
    m_aIn->setToolTip( i18n( "Green to magenta*" ) );
149 150 151 152 153 154 155

    m_bIn = new QSpinBox(this);
    m_bIn->setMinimum(0);
    m_bIn->setMaximum(255);
    m_bIn->setSingleStep(1);
    m_bIn->setFixedSize(40, 18);
    m_bIn->setFocusPolicy( Qt::ClickFocus );
156
    m_bIn->setToolTip( i18n( "Blue to yellow" ) );
157

158
    mGrid->setSpacing(0);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    mGrid->setMargin(0);

    mGrid->addWidget(m_colorwheel, 0, 0, -1, 1, Qt::AlignTop);

    mGrid->addWidget(m_colorpatch, 0, 1, Qt::AlignCenter);

    mGrid->addWidget(m_VSelector, 1, 1, -1, 1, Qt::AlignTop);

    mGrid->addWidget(m_HRB, 0, 2, Qt::AlignCenter);
    mGrid->addWidget(m_SRB, 1, 2, Qt::AlignCenter);
    mGrid->addWidget(m_VRB, 2, 2, Qt::AlignCenter);
    mGrid->addWidget(m_RRB, 4, 2, Qt::AlignCenter);
    mGrid->addWidget(m_GRB, 5, 2, Qt::AlignCenter);
    mGrid->addWidget(m_BRB, 6, 2, Qt::AlignCenter);
    mGrid->addWidget(m_LRB, 0, 6, Qt::AlignCenter);
    mGrid->addWidget(m_aRB, 1, 6, Qt::AlignCenter);
    mGrid->addWidget(m_bRB, 2, 6, Qt::AlignCenter);

    mGrid->addWidget(m_HLabel, 0, 3, Qt::AlignTop);
    mGrid->addWidget(m_SLabel, 1, 3, Qt::AlignTop);
    mGrid->addWidget(m_VLabel, 2, 3, Qt::AlignTop);
    mGrid->addWidget(m_RLabel, 4, 3, Qt::AlignTop);
    mGrid->addWidget(m_GLabel, 5, 3, Qt::AlignTop);
    mGrid->addWidget(m_BLabel, 6, 3, Qt::AlignTop);
    mGrid->addWidget(m_LLabel, 0, 7, Qt::AlignTop);
    mGrid->addWidget(m_aLabel, 1, 7, Qt::AlignTop);
    mGrid->addWidget(m_bLabel, 2, 7, Qt::AlignTop);

    mGrid->addWidget(m_HIn, 0, 4, Qt::AlignTop);
    mGrid->addWidget(m_SIn, 1, 4, Qt::AlignTop);
    mGrid->addWidget(m_VIn, 2, 4, Qt::AlignTop);
    mGrid->addWidget(m_RIn, 4, 4, Qt::AlignTop);
    mGrid->addWidget(m_GIn, 5, 4, Qt::AlignTop);
    mGrid->addWidget(m_BIn, 6, 4, Qt::AlignTop);
    mGrid->addWidget(m_LIn, 0, 8, Qt::AlignTop);
    mGrid->addWidget(m_aIn, 1, 8, Qt::AlignTop);
    mGrid->addWidget(m_bIn, 2, 8, Qt::AlignTop);

    mGrid->addItem( new QSpacerItem( 4, 4, QSizePolicy::Fixed, QSizePolicy::Fixed), 3, 5 );
    mGrid->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding ), 7, 9 );

    connect(m_VSelector, SIGNAL(valueChanged(int)), this, SLOT(slotVChanged(int)));
    connect(m_colorwheel, SIGNAL(valueChanged(const KoOldColor&)), this, SLOT(slotWheelChanged(const KoOldColor&)));

    /* connect spin box */
    connect(m_HIn, SIGNAL(valueChanged(int)), this, SLOT(slotHChanged(int)));
    connect(m_SIn, SIGNAL(valueChanged(int)), this, SLOT(slotSChanged(int)));
    connect(m_VIn, SIGNAL(valueChanged(int)), this, SLOT(slotVChanged(int)));

    //setFixedSize(mGrid -> minimumSize());
    m_autovalue = true; // So on the initial selection of h or v, s gets set to 255.

    update(QColor(Qt::black));

    setLayout(mGrid);
}

void KoUniColorChooser::slotHChanged(int h)
{
    //kDebug() << "H changed: " << h << endl;
    m_fgColor.setHSV(h, m_fgColor.S(), m_fgColor.V());
    changedFgColor();
}

void KoUniColorChooser::slotSChanged(int s)
{
    //kDebug() << "S changed: " << s << endl;
    m_fgColor.setHSV(m_fgColor.H(), s, m_fgColor.V());
    changedFgColor();
}

void KoUniColorChooser::slotVChanged(int v)
{
    //kDebug() << "V changed: " << v << ", setting autovalue to false " << endl;
    m_autovalue = false;
    m_fgColor.setHSV(m_fgColor.H(), m_fgColor.S(), v);
    changedFgColor();
}

void KoUniColorChooser::slotWheelChanged(const KoOldColor& c)
{
    //kDebug() << "Wheel changed: " << c.color() <<  endl;
    if(m_autovalue)
        m_fgColor.setHSV(c.H(), c.S(), 255);
    else
        m_fgColor.setHSV(c.H(), c.S(), m_fgColor.V());
    changedFgColor();
}


void KoUniColorChooser::setColor(const QColor & c)
{
    //kDebug() << "setFGColor " << c << endl;
    blockSignals(true);
    slotSetColor(c);
    blockSignals(false);
}

void KoUniColorChooser::changedFgColor()
{
    //kDebug() << "ChangedFgColor\n";
    update( m_fgColor);

    emit sigColorChanged(m_fgColor.color());
}


void KoUniColorChooser::update(const KoOldColor & fgColor)
{
    
    m_HIn->blockSignals(true);
    m_SIn->blockSignals(true);
    m_VIn->blockSignals(true);
    m_VSelector->blockSignals(true);
    m_colorwheel->blockSignals(true);
            
    //kDebug() << "update. FG: " << fgColor.color() << ", bg: " << bgColor.color() << endl;
    m_fgColor = fgColor;
    KoOldColor color = m_fgColor;

    int h = color.H();
    int s = color.S();
    int v = color.V();

    m_HIn->setValue(h);
    m_SIn->setValue(s);
    m_VIn->setValue(v);
    
    m_VSelector->setHue(h);
    m_VSelector->setSaturation(s);
    m_VSelector->setValue(v);
    m_VSelector->updateContents();
    
    m_colorwheel->slotSetValue(color);

    m_HIn->blockSignals(false);
    m_SIn->blockSignals(false);
    m_VIn->blockSignals(false);
    m_VSelector->blockSignals(false);
    m_colorwheel->blockSignals(false);
}

void KoUniColorChooser::slotSetColor(const QColor& c)
{
    //kDebug() << "slotFGColorSelected " << c << endl;
    m_fgColor = KoOldColor(c);

    changedFgColor();
}

#include "KoUniColorChooser.moc"