ko_hsv_widget.cpp
1/*
2 * Copyright (c) 1999 Matthias Elter (me@kde.org)
3 * Copyright (c) 2001-2002 Igor Jansen (rm@kde.org)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18*/
19
20#include "ko_hsv_widget.h"
21#include "ko_color_wheel.h"
22
23#include <tdeselect.h>
24#include <tqlayout.h>
25#include <tqhbox.h>
26#include <tqlabel.h>
27#include <tqspinbox.h>
28#include <tqtooltip.h>
29#include <koFrameButton.h>
30#include <koColorSlider.h>
31#include <kcolordialog.h>
32#include <kdualcolorbutton.h>
33#include <koColor.h>
34#include <kdebug.h>
35#include <tdelocale.h>
36
37KoHSVWidget::KoHSVWidget(TQWidget *parent, const char *name) : super(parent, name)
38{
39 m_ColorButton = new KDualColorButton(this);
40 m_ColorButton -> setFixedSize(m_ColorButton->sizeHint());
41
42 TQGridLayout *mGrid = new TQGridLayout(this, 5, 7, 5, 2);
43 m_colorwheel = new KoColorWheel(this);
44 m_colorwheel->setFixedSize( 120, 120);
45 m_VSelector = new KValueSelector(TQt::Vertical, this);
46 m_VSelector-> setFixedSize( 30, 120);
47
48 /* setup slider labels */
49 mHLabel = new TQLabel("H:", this);
50 mHLabel->setFixedSize(12, 20);
51 mSLabel = new TQLabel("S:", this);
52 mSLabel->setFixedSize(12, 20);
53 mVLabel = new TQLabel("V:", this);
54 mVLabel->setFixedSize(12, 20);
55
56 /* setup spin box */
57 mHIn = new TQSpinBox(0, 359, 1, this);
58 mHIn->setFixedSize(50, 20);
59 mHIn->setFocusPolicy( TQWidget::ClickFocus );
60 TQToolTip::add( mHIn, i18n( "Hue" ) );
61
62 mSIn = new TQSpinBox(0, 255, 1, this);
63 mSIn->setFixedSize(50, 20);
64 mSIn->setFocusPolicy( TQWidget::ClickFocus );
65 TQToolTip::add( mSIn, i18n( "Saturation" ) );
66
67 mVIn = new TQSpinBox(0, 255, 1, this);
68 mVIn->setFixedSize(50, 20);
69 mVIn->setFocusPolicy( TQWidget::ClickFocus );
70 TQToolTip::add( mVIn, i18n( "Value (brightness)" ) );
71
72 mGrid->addMultiCellWidget(m_ColorButton, 0, 0, 0, 1, TQt::AlignTop);
73
74 mGrid->addWidget(mHLabel, 1, 0);
75 mGrid->addWidget(mSLabel, 2, 0);
76 mGrid->addWidget(mVLabel, 3, 0);
77
78 mGrid->addMultiCellWidget(m_colorwheel, 0, 3, 2, 4);
79
80 mGrid->addWidget(mHIn, 1, 1);
81 mGrid->addWidget(mSIn, 2, 1);
82 mGrid->addWidget(mVIn, 3, 1);
83
84 mGrid->addMultiCellWidget(m_VSelector, 0, 3, 5, 5);
85
86
87 connect(m_ColorButton, TQ_SIGNAL(fgChanged(const TQColor &)), this, TQ_SLOT(slotFGColorSelected(const TQColor &)));
88 connect(m_ColorButton, TQ_SIGNAL(bgChanged(const TQColor &)), this, TQ_SLOT(slotBGColorSelected(const TQColor &)));
89 connect(m_ColorButton, TQ_SIGNAL(currentChanged(KDualColorButton::DualColor)), this, TQ_SLOT(currentChanged(KDualColorButton::DualColor)));
90
91 connect(m_VSelector, TQ_SIGNAL(valueChanged(int)), this, TQ_SLOT(slotVChanged(int)));
92 connect(m_colorwheel, TQ_SIGNAL(valueChanged(const KoColor&)), this, TQ_SLOT(slotWheelChanged(const KoColor&)));
93
94 /* connect spin box */
95 connect(mHIn, TQ_SIGNAL(valueChanged(int)), this, TQ_SLOT(slotHChanged(int)));
96 connect(mSIn, TQ_SIGNAL(valueChanged(int)), this, TQ_SLOT(slotSChanged(int)));
97 connect(mVIn, TQ_SIGNAL(valueChanged(int)), this, TQ_SLOT(slotVChanged(int)));
98
99 //setFixedSize(mGrid -> minimumSize());
100 m_autovalue = true; // So on the initial selection of h or v, s gets set to 255.
101
102 update(TQt::black, TQt::white);
103}
104
105void KoHSVWidget::slotHChanged(int h)
106{
107 //kdDebug() << "H changed: " << h << endl;
108 if (m_ColorButton->current() == KDualColorButton::Foreground){
109 m_fgColor.setHSV(h, m_fgColor.S(), m_fgColor.V());
110 changedFgColor();
111 }
112 else{
113 m_bgColor.setHSV(h, m_bgColor.S(), m_bgColor.V());
114 changedBgColor();
115 }
116}
117
118void KoHSVWidget::slotSChanged(int s)
119{
120 //kdDebug() << "S changed: " << s << endl;
121 if (m_ColorButton->current() == KDualColorButton::Foreground){
122 m_fgColor.setHSV(m_fgColor.H(), s, m_fgColor.V());
123 changedFgColor();
124 }
125 else{
126 m_bgColor.setHSV(m_bgColor.H(), s, m_bgColor.V());
127 changedBgColor();
128 }
129}
130
131void KoHSVWidget::slotVChanged(int v)
132{
133 //kdDebug() << "V changed: " << v << ", setting autovalue to false " << endl;
134 m_autovalue = false;
135 if (m_ColorButton->current() == KDualColorButton::Foreground){
136 m_fgColor.setHSV(m_fgColor.H(), m_fgColor.S(), v);
137 changedFgColor();
138 }
139 else{
140 m_bgColor.setHSV(m_bgColor.H(), m_bgColor.S(), v);
141 changedBgColor();
142 }
143}
144
145void KoHSVWidget::slotWheelChanged(const KoColor& c)
146{
147 //kdDebug() << "Wheel changed: " << c.color() << endl;
148 if (m_ColorButton->current() == KDualColorButton::Foreground){
149 if(m_autovalue)
150 m_fgColor.setHSV(c.H(), c.S(), 255);
151 else
152 m_fgColor.setHSV(c.H(), c.S(), m_fgColor.V());
153 changedFgColor();
154 }
155 else{
156 if(m_autovalue)
157 m_bgColor.setHSV(c.H(), c.S(), 255);
158 else
159 m_bgColor.setHSV(c.H(), c.S(), m_bgColor.V());
160 changedBgColor();
161 }
162}
163
164
165void KoHSVWidget::setFgColor(const TQColor & c)
166{
167 //kdDebug() << "setFGColor " << c << endl;
168 blockSignals(true);
169 slotFGColorSelected(c);
170 blockSignals(false);
171}
172
173void KoHSVWidget::setBgColor(const TQColor & c)
174{
175 //kdDebug() << "setBgColor " << c << endl;
176 blockSignals(true);
177 slotBGColorSelected(c);
178 blockSignals(false);
179}
180
181void KoHSVWidget::changedFgColor()
182{
183 //kdDebug() << "ChangedFgColor\n";
184 disconnect(m_ColorButton, TQ_SIGNAL(fgChanged(const TQColor &)), this, TQ_SLOT(slotFGColorSelected(const TQColor &)));
185 m_ColorButton->setForeground( m_fgColor.color() );
186 connect(m_ColorButton, TQ_SIGNAL(fgChanged(const TQColor &)), this, TQ_SLOT(slotFGColorSelected(const TQColor &)));
187
188 update( m_fgColor, m_bgColor);
189
190 emit sigFgColorChanged(m_fgColor.color());
191}
192
193void KoHSVWidget::changedBgColor()
194{
195 //kdDebug() << "changedBgColor()\n";
196 disconnect(m_ColorButton, TQ_SIGNAL(bgChanged(const TQColor &)), this, TQ_SLOT(slotBGColorSelected(const TQColor &)));
197 m_ColorButton->setBackground( m_bgColor.color() );
198 connect(m_ColorButton, TQ_SIGNAL(bgChanged(const TQColor &)), this, TQ_SLOT(slotBGColorSelected(const TQColor &)));
199
200 update(m_fgColor, m_bgColor );
201
202 emit sigBgColorChanged(m_bgColor.color());
203}
204
205void KoHSVWidget::update(const KoColor & fgColor, const KoColor & bgColor)
206{
207
208 mHIn->blockSignals(true);
209 mSIn->blockSignals(true);
210 mVIn->blockSignals(true);
211 m_VSelector->blockSignals(true);
212 m_colorwheel->blockSignals(true);
213
214 //kdDebug() << "update. FG: " << fgColor.color() << ", bg: " << bgColor.color() << endl;
215 m_fgColor = fgColor;
216 m_bgColor = bgColor;
217
218 KoColor color = (m_ColorButton->current() == KDualColorButton::Foreground)? m_fgColor : m_bgColor;
219
220 int h = color.H();
221 int s = color.S();
222 int v = color.V();
223
224 mHIn->setValue(h);
225 mSIn->setValue(s);
226 mVIn->setValue(v);
227
228 m_VSelector->setHue(h);
229 m_VSelector->setSaturation(s);
230 m_VSelector->setValue(v);
231 m_VSelector->updateContents();
232
233 m_colorwheel->slotSetValue(color);
234
235 mHIn->blockSignals(false);
236 mSIn->blockSignals(false);
237 mVIn->blockSignals(false);
238 m_VSelector->blockSignals(false);
239 m_VSelector->repaint(false);
240 m_colorwheel->blockSignals(false);
241}
242
243void KoHSVWidget::slotFGColorSelected(const TQColor& c)
244{
245 //kdDebug() << "slotFGColorSelected " << c << endl;
246 m_fgColor = KoColor(c);
247
248 changedFgColor();
249}
250
251void KoHSVWidget::slotBGColorSelected(const TQColor& c)
252{
253 //kdDebug() << "slotBGColorSelected()" << c << endl;
254 m_bgColor = KoColor(c);
255
256 changedBgColor();
257}
258
259void KoHSVWidget::currentChanged(KDualColorButton::DualColor s)
260{
261 //kdDebug() << "currentChanged\n";
262 if(s == KDualColorButton::Foreground)
263 slotFGColorSelected(m_ColorButton->currentColor());
264 else
265 slotBGColorSelected(m_ColorButton->currentColor());
266 emit sigModeChanged( s );
267}
268
269void KoHSVWidget::setMode(KDualColorButton::DualColor s)
270{
271 m_ColorButton->setCurrent( s );
272}
273
274#include "ko_hsv_widget.moc"
const char * name(StdAction id)