1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
148
149
150
151
152
153
154
155
156
157
158
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
/* ============================================================
* Copyright 2004-2005 by Gilles Caulier
* Copyright 2005 by Casper Boemann (reworked to be generic)
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation;
* either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* ============================================================ */
// C++ includes.
#include <cmath>
#include <cstdlib>
// TQt includes.
#include <tqpixmap.h>
#include <tqpainter.h>
#include <tqpoint.h>
#include <tqpen.h>
#include <tqevent.h>
#include <tqtimer.h>
#include <tqrect.h>
#include <tqfont.h>
#include <tqfontmetrics.h>
// KDE includes.
#include <kdebug.h>
#include <kcursor.h>
#include <tdelocale.h>
// Local includes.
#include "kcurve.h"
KCurve::KCurve(TQWidget *parent, const char *name, WFlags f)
: TQWidget(parent, name, f)
{
m_grab_point = NULL;
m_readOnlyMode = false;
m_guideVisible = false;
m_dragging = false;
m_pix = NULL;
setMouseTracking(true);
setPaletteBackgroundColor(TQt::NoBackground);
setMinimumSize(150, 50);
TQPair<double,double> *p = new TQPair<double,double>;
p->first = 0.0; p->second=0.0;
m_points.append(p);
p = new TQPair<double,double>;
p->first = 1.0; p->second=1.0;
m_points.append(p);
m_points.setAutoDelete(true);
setFocusPolicy(TQWidget::StrongFocus);
}
KCurve::~KCurve()
{
if (m_pix) delete m_pix;
}
void KCurve::reset(void)
{
m_grab_point = NULL;
m_guideVisible = false;
repaint(false);
}
void KCurve::setCurveGuide(TQColor color)
{
m_guideVisible = true;
m_colorGuide = color;
repaint(false);
}
void KCurve::setPixmap(TQPixmap pix)
{
if (m_pix) delete m_pix;
m_pix = new TQPixmap(pix);
repaint(false);
}
void KCurve::keyPressEvent(TQKeyEvent *e)
{
if(e->key() == TQt::Key_Delete || e->key() == TQt::Key_Backspace)
{
TQPair<double,double> *closest_point=NULL;
if(m_grab_point)
{
//first find closest point to get focus afterwards
TQPair<double,double> *p = m_points.first();
double distance = 1000; // just a big number
while(p)
{
if(p!=m_grab_point)
if (fabs (m_grab_point->first - p->first) < distance)
{
distance = fabs(m_grab_point->first - p->first);
closest_point = p;
}
p = m_points.next();
}
m_points.remove(m_grab_point);
}
m_grab_point = closest_point;
repaint(false);
}
else
TQWidget::keyPressEvent(e);
}
void KCurve::paintEvent(TQPaintEvent *)
{
int x, y;
int wWidth = width();
int wHeight = height();
x = 0;
y = 0;
// Drawing selection or all histogram values.
// A TQPixmap is used for enable the double buffering.
TQPixmap pm(size());
TQPainter p1;
p1.begin(TQT_TQPAINTDEVICE(&pm), this);
// draw background
if(m_pix)
{
p1.scale(1.0*wWidth/m_pix->width(), 1.0*wHeight/m_pix->height());
p1.drawPixmap(0, 0, *m_pix);
p1.resetXForm();
}
else
pm.fill();
// Draw grid separators.
p1.setPen(TQPen(TQt::gray, 1, TQt::SolidLine));
p1.drawLine(wWidth/3, 0, wWidth/3, wHeight);
p1.drawLine(2*wWidth/3, 0, 2*wWidth/3, wHeight);
p1.drawLine(0, wHeight/3, wWidth, wHeight/3);
p1.drawLine(0, 2*wHeight/3, wWidth, 2*wHeight/3);
// Draw curve.
double curvePrevVal = getCurveValue(0.0);
p1.setPen(TQPen(TQt::black, 1, TQt::SolidLine));
for (x = 0 ; x < wWidth ; x++)
{
double curveX;
double curveVal;
curveX = (x + 0.5) / wWidth;
curveVal = getCurveValue(curveX);
p1.drawLine(x - 1, wHeight - int(curvePrevVal * wHeight),
x, wHeight - int(curveVal * wHeight));
curvePrevVal = curveVal;
}
p1.drawLine(x - 1, wHeight - int(curvePrevVal * wHeight),
x, wHeight - int(getCurveValue(1.0) * wHeight));
// Drawing curve handles.
if ( !m_readOnlyMode )
{
TQPair<double,double> *p = m_points.first();
while(p)
{
double curveX = p->first;
double curveY = p->second;
if(p == m_grab_point)
{
p1.setPen(TQPen(TQt::red, 3, TQt::SolidLine));
p1.drawEllipse( int(curveX * wWidth) - 2,
wHeight - 2 - int(curveY * wHeight), 4, 4 );
}
else
{
p1.setPen(TQPen(TQt::red, 1, TQt::SolidLine));
p1.drawEllipse( int(curveX * wWidth) - 3,
wHeight - 3 - int(curveY * wHeight), 6, 6 );
}
p = m_points.next();
}
}
p1.end();
bitBlt(this, 0, 0, &pm);
}
void KCurve::mousePressEvent ( TQMouseEvent * e )
{
if (m_readOnlyMode) return;
TQPair<double,double> *closest_point=NULL;
double distance;
if (e->button() != TQt::LeftButton)
return;
double x = e->pos().x() / (float)width();
double y = 1.0 - e->pos().y() / (float)height();
distance = 1000; // just a big number
TQPair<double,double> *p = m_points.first();
int insert_pos,pos=0;
while(p)
{
if (fabs (x - p->first) < distance)
{
distance = fabs(x - p->first);
closest_point = p;
if(x < p->first)
insert_pos = pos;
else
insert_pos = pos + 1;
}
p = m_points.next();
pos++;
}
if(closest_point == NULL)
{
closest_point = new TQPair<double,double>;
closest_point->first = x;
closest_point->second = y;
m_points.append(closest_point);
}
else if(distance * width() > 5)
{
closest_point = new TQPair<double,double>;
closest_point->first = x;
closest_point->second = y;
m_points.insert(insert_pos, closest_point);
}
else
if(fabs(y - closest_point->second) * width() > 5)
return;
m_grab_point = closest_point;
m_grabOffsetX = m_grab_point->first - x;
m_grabOffsetY = m_grab_point->second - y;
m_grab_point->first = x + m_grabOffsetX;
m_grab_point->second = y + m_grabOffsetY;
m_dragging = true;
setCursor( KCursor::crossCursor() );
// Determine the leftmost and rightmost points.
m_leftmost = 0;
m_rightmost = 1;
p = m_points.first();
while(p)
{
if (p != m_grab_point)
{
if(p->first> m_leftmost && p->first < x)
m_leftmost = p->first;
if(p->first < m_rightmost && p->first > x)
m_rightmost = p->first;
}
p = m_points.next();
}
repaint(false);
}
void KCurve::mouseReleaseEvent ( TQMouseEvent * e )
{
if (m_readOnlyMode) return;
if (e->button() != TQt::LeftButton)
return;
setCursor( KCursor::arrowCursor() );
m_dragging = false;
repaint(false);
emit modified();
}
void KCurve::mouseMoveEvent ( TQMouseEvent * e )
{
if (m_readOnlyMode) return;
double x = e->pos().x() / (float)width();
double y = 1.0 - e->pos().y() / (float)height();
if (m_dragging == false) // If no point is selected set the the cursor shape if on top
{
double distance = 1000;
double ydistance = 1000;
TQPair<double,double> *p = m_points.first();
while(p)
{
if (fabs (x - p->first) < distance)
{
distance = fabs(x - p->first);
ydistance = fabs(y - p->second);
}
p = m_points.next();
}
if (distance * width() > 5 || ydistance * height() > 5)
setCursor( KCursor::arrowCursor() );
else
setCursor( KCursor::crossCursor() );
}
else // Else, drag the selected point
{
setCursor( KCursor::crossCursor() );
x += m_grabOffsetX;
y += m_grabOffsetY;
if (x <= m_leftmost)
x = m_leftmost + 1E-4; // the addition so we can grab the dot later.
if(x >= m_rightmost)
x = m_rightmost - 1E-4;
if(y > 1.0)
y = 1.0;
if(y < 0.0)
y = 0.0;
m_grab_point->first = x;
m_grab_point->second = y;
emit modified();
}
repaint(false);
}
double KCurve::getCurveValue(double x)
{
return getCurveValue(m_points, x);
}
double KCurve::getCurveValue(TQPtrList<TQPair<double,double> > &curve, double x)
{
double t;
TQPair<double,double> *p;
TQPair<double,double> *p0,*p1,*p2,*p3;
double c0,c1,c2,c3;
double val;
if(curve.count() == 0)
return 0.5;
// First find curve segment
p = curve.first();
if(x < p->first)
return p->second;
p = curve.last();
if(x >= p->first)
return p->second;
// Find the four control points (two on each side of x)
p = curve.first();
while(x >= p->first)
{
p = curve.next();
}
curve.prev();
if((p0 = curve.prev()) == NULL)
p1 = p0 = curve.first();
else
p1 = curve.next();
p2 = curve.next();
if( (p = curve.next()) )
p3 = p;
else
p3 = p2;
// Calculate the value
t = (x - p1->first) / (p2->first - p1->first);
c2 = (p2->second - p0->second) * (p2->first-p1->first) / (p2->first-p0->first);
c3 = p1->second;
c0 = -2*p2->second + 2*c3 + c2 + (p3->second - p1->second) * (p2->first - p1->first) / (p3->first - p1->first);
c1 = p2->second - c3 - c2 - c0;
val = ((c0*t + c1)*t + c2)*t + c3;
if(val < 0.0)
val = 0.0;
if(val > 1.0)
val = 1.0;
return val;
}
TQPtrList<TQPair<double,double> > KCurve::getCurve()
{
TQPtrList<TQPair<double,double> > outlist;
TQPair<double,double> *p;
TQPair<double,double> *outpoint;
p = m_points.first();
while(p)
{
outpoint = new TQPair<double,double>(p->first, p->second);
outlist.append(outpoint);
p = m_points.next();
}
return outlist;
}
void KCurve::setCurve(TQPtrList<TQPair<double,double> >inlist)
{
TQPair<double,double> *p;
TQPair<double,double> *inpoint;
m_points.clear();
inpoint = inlist.first();
while(inpoint)
{
p = new TQPair<double,double>(inpoint->first, inpoint->second);
m_points.append(p);
inpoint = inlist.next();
}
}
void KCurve::leaveEvent( TQEvent * )
{
}
#include "kcurve.moc"
|