lib

koColorSlider.cpp
1/* This file is part of the KDE project
2 Copyright (c) 1999 Matthias Elter (me@kde.org)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "koColorSlider.h"
21
22#include <tqpainter.h>
23#include <tqcursor.h>
24#include <tqpen.h>
25
26#include <kdebug.h>
27#include <kpixmapeffect.h>
28
29KoColorFrame::KoColorFrame(TQWidget *parent):
30TQFrame(parent)
31{
32 setFrameStyle(Panel | Sunken);
33 setBackgroundMode(NoBackground);
34
35 /* default values */
36 mC1 = TQColor(0, 0, 0);
37 mC2 = TQColor(255, 255, 255);
38
39 mColorChanged = false;
40 mPixChanged = false;
41 mDragging = false;
42}
43
44const TQColor KoColorFrame::colorAt(const TQPoint &p)
45{
46 if(mPixChanged)
47 {
48 mImage = mPixmap.convertToImage();
49 mPixChanged = false;
50 }
51
52 if(p.x() >= mPixmap.width() || p.y() >= mPixmap.height())
53 return TQColor(255,255,255);
54
55 return TQColor(mImage.pixel(p.x(), p.y()));
56}
57
58void KoColorFrame::slotSetColor1(const TQColor &c)
59{
60 mC1 = c;
61 mColorChanged = true;
62 mPixChanged = true;
63 repaint();
64}
65
66void KoColorFrame::slotSetColor2(const TQColor &c)
67{
68 mC2 = c;
69 mColorChanged = true;
70 repaint();
71}
72
73void KoColorFrame::drawContents(TQPainter *p)
74{
75 TQRect r = contentsRect();
76
77 if((mPixmap.size() != r.size()) || mColorChanged)
78 {
79 mPixmap.resize(r.width() + 1, r.height() + 1);
80 KPixmapEffect::gradient(mPixmap, mC1, mC2, KPixmapEffect::HorizontalGradient);
81 mColorChanged = false;
82 mPixChanged = true;
83 }
84
85 p->drawPixmap(r.left(), r.top(), mPixmap);
86}
87
88void KoColorFrame::mousePressEvent(TQMouseEvent *e)
89{
90 if(e->button() & TQt::LeftButton)
91 {
92 emit clicked(e->pos());
93
94 mDragging = true;
95 TQPoint pos = TQPoint(e->pos().x() - contentsRect().left(), e->pos().y() - contentsRect().top());
96
97 if(pos.x() < 0)
98 pos.setX(0);
99 else if(pos.x() >= contentsRect().width())
100 pos.setX(contentsRect().width()-1);
101
102 if(pos.y() < 0)
103 pos.setY(0);
104 else if(pos.y() >= contentsRect().height())
105 pos.setY(contentsRect().height()-1);
106
107 TQColor c = colorAt(pos);
108 emit colorSelected(c);
109 }
110 else
111 TQFrame::mousePressEvent(e);
112}
113
114void KoColorFrame::mouseReleaseEvent(TQMouseEvent *e)
115{
116 if(e->button() & TQt::LeftButton)
117 mDragging = false;
118 else
119 TQFrame::mouseReleaseEvent(e);
120}
121
122void KoColorFrame::mouseMoveEvent(TQMouseEvent *e)
123{
124 if(mDragging)
125 {
126 bool set = false;
127 int x = e->pos().x();
128 int y = e->pos().y();
129
130 int left = contentsRect().left();
131 int right = contentsRect().left() + contentsRect().width();
132 int top = contentsRect().top();
133 int bottom = contentsRect().top() + contentsRect().height();
134
135 if(x < left)
136 {
137 x = left;
138 set = true;
139 }
140 else if(x > right)
141 {
142 x = right;
143 set = true;
144 }
145 if(y < top)
146 {
147 y = top;
148 set = true;
149 }
150 else if(y > bottom)
151 {
152 y = bottom;
153 set = true;
154 }
155
156// if(set)
157// TQCursor::setPos(mapToGlobal(TQPoint(x,y)));
158
159 TQPoint pos = TQPoint(x - contentsRect().left(), y - contentsRect().top());
160
161 TQColor c = colorAt(pos);
162 emit colorSelected(c);
163 }
164 else
165 TQFrame::mouseMoveEvent(e);
166}
167
168/***********************************************************************************/
169
170KoSliderWidget::KoSliderWidget(TQWidget *parent):
171TQWidget(parent)
172{
173 mDragging = false;
174 setFixedHeight(6);
175 setFixedWidth(11);
176}
177
178void KoSliderWidget::paintEvent(TQPaintEvent *)
179{
180 TQPainter p;
181 TQPen pen(black, 1);
182 p.begin(this);
183
184 p.setPen(pen);
185 p.drawLine(0, 5, 5, 0);
186 p.drawLine(10, 5, 5, 0);
187 p.drawLine(0, 5, 10, 5);
188 p.end();
189}
190
191void KoSliderWidget::mousePressEvent(TQMouseEvent *e)
192{
193 if(e->button() & TQt::LeftButton)
194 {
195 mPos = e->pos();
196 mDragging = true;
197 }
198 else
199 TQWidget::mousePressEvent(e);
200}
201
202void KoSliderWidget::mouseReleaseEvent(TQMouseEvent *e)
203{
204 if(e->button() & TQt::LeftButton)
205 mDragging = false;
206 else
207 TQWidget::mouseReleaseEvent(e);
208}
209
210void KoSliderWidget::mouseMoveEvent(TQMouseEvent *e)
211{
212 if(mDragging)
213 {
214 TQWidget *p = parentWidget();
215
216 if(!p)
217 return;
218
219 TQPoint newPos = p->mapFromGlobal(TQCursor::pos()) - mPos;
220
221 /* don't drag vertically */
222 newPos.setY(pos().y());
223
224 if(newPos.x() < 0)
225 newPos.setX(0);
226 if(newPos.x() > p->width() - width())
227 newPos.setX(p->width() - width());
228
229 move(newPos);
230 emit positionChanged(newPos.x());
231 }
232 else
233 TQWidget::mouseMoveEvent(e);
234}
235
236/***********************************************************************************/
237
238KoColorSlider::KoColorSlider(TQWidget *parent):
239TQWidget(parent)
240{
241 mColorFrame = new KoColorFrame(this);
242 mSlider = new KoSliderWidget(this);
243
244 mMin = 0;
245 mMax = 255;
246 mValue = 0;
247
248 connect(mSlider, TQ_SIGNAL(positionChanged(int)), this, TQ_SLOT(slotSliderMoved(int)));
249 connect(mColorFrame, TQ_SIGNAL(clicked(const TQPoint &)), this, TQ_SLOT(slotFrameClicked(const TQPoint &)));
250}
251
252KoColorSlider::~KoColorSlider()
253{
254 delete mColorFrame;
255 delete mSlider;
256}
257
258int KoColorSlider::minValue()
259{
260 return mMin;
261}
262
263int KoColorSlider::maxValue()
264{
265 return mMax;
266}
267
268void KoColorSlider::slotSetRange(int min, int max)
269{
270 if(min >= max)
271 return;
272
273 mMin = min;
274 mMax = max;
275}
276
277void KoColorSlider::resizeEvent(TQResizeEvent *e)
278{
279 TQWidget::resizeEvent(e);
280 // m_pSlider->width()/2 * 2 seems stupid but is not because for example
281 // m_pSlider->width() == 11 I get 10.
282 mColorFrame->setGeometry(mSlider->width() / 2, 0, width() - mSlider->width() / 2 * 2, height() - mSlider->height());
283 slotSetValue(mValue);
284}
285
286void KoColorSlider::slotSetColor1(const TQColor &c)
287{
288 mColorFrame->slotSetColor1(c);
289}
290
291void KoColorSlider::slotSetColor2(const TQColor &c)
292{
293 mColorFrame->slotSetColor2(c);
294}
295
296void KoColorSlider::slotSetValue(int value)
297{
298 if(value < mMin)
299 value = mMin;
300 if(value > mMax)
301 value = mMax;
302
303 mValue = value;
304
305 int range = mMax - mMin;
306 float v = value;
307 if(mMin < 0)
308 v += -mMin;
309
310 float factor = v / range;
311 int x = static_cast<int>(factor * mColorFrame->contentsRect().width());
312
313 mSlider->move(TQPoint(x, height() - mSlider->height()));
314}
315
316void KoColorSlider::slotSliderMoved(int x)
317{
318 if(x < 0)
319 x = 0;
320 if(x > mColorFrame->contentsRect().width())
321 x = mColorFrame->contentsRect().width();
322
323 float factor = x;
324 factor /= mColorFrame->contentsRect().width();
325 int range = mMax - mMin;
326
327 mValue = static_cast<int>(factor * range);
328
329 emit valueChanged(mValue);
330 emit colorSelected(mColorFrame->colorAt(TQPoint(x, mColorFrame->contentsRect().height()/2)));
331}
332
333void KoColorSlider::slotFrameClicked(const TQPoint &p)
334{
335 TQPoint local = mColorFrame->mapToParent(p);
336 TQPoint pos = TQPoint(local.x() - mSlider->width() / 2, height() - mSlider->height());
337
338 if(pos.x() < 0)
339 pos.setX(0);
340 else if(pos.x() > width() - mSlider->width())
341 pos.setX(width() - mSlider->width());
342
343 mSlider->move(pos);
344 slotSliderMoved(pos.x());
345}
346
347void KoColorSlider::mousePressEvent(TQMouseEvent *e)
348{
349 if(e->button() & TQt::LeftButton)
350 {
351 TQPoint pos = TQPoint(e->pos().x() - mSlider->width() / 2, height() - mSlider->height());
352
353 if(pos.x() < 0)
354 pos.setX(0);
355 else if(pos.x() > width() - mSlider->width())
356 pos.setX(width() - mSlider->width());
357
358 mSlider->move(pos);
359 slotSliderMoved(pos.x());
360 }
361 else
362 TQWidget::mousePressEvent(e);
363}
364
365#include "koColorSlider.moc"