summaryrefslogtreecommitdiff
path: root/libopie2/opieui/oselector.cpp
authormickeyl <mickeyl>2003-03-28 15:11:52 (UTC)
committer mickeyl <mickeyl>2003-03-28 15:11:52 (UTC)
commit11304d02942e9fa493e4e80943a828f9c65f6772 (patch) (unidiff)
treea0223c10c067e1afc70d15c2b82be3f3c15e41ae /libopie2/opieui/oselector.cpp
parentb271d575fa05cf570a1a829136517761bd47e69b (diff)
downloadopie-11304d02942e9fa493e4e80943a828f9c65f6772.zip
opie-11304d02942e9fa493e4e80943a828f9c65f6772.tar.gz
opie-11304d02942e9fa493e4e80943a828f9c65f6772.tar.bz2
skeleton and the start of libopie2, please read README, ROADMAP and STATUS and comment...
Diffstat (limited to 'libopie2/opieui/oselector.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opieui/oselector.cpp716
1 files changed, 716 insertions, 0 deletions
diff --git a/libopie2/opieui/oselector.cpp b/libopie2/opieui/oselector.cpp
new file mode 100644
index 0000000..ec5af6b
--- a/dev/null
+++ b/libopie2/opieui/oselector.cpp
@@ -0,0 +1,716 @@
1/* This file is part of the KDE libraries
2 Copyright (C) 1997 Martin Jones (mjones@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., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18*/
19
20/* QT */
21
22#include <qimage.h>
23#include <qpainter.h>
24#include <qdrawutil.h>
25
26/* OPIE */
27
28#include <opie2/oimageeffect.h>
29#include <opie2/oselector.h>
30
31#define STORE_W 8
32#define STORE_W2 STORE_W * 2
33
34//-----------------------------------------------------------------------------
35/*
36 * 2D value selector.
37 * The contents of the selector are drawn by derived class.
38 */
39
40OXYSelector::OXYSelector( QWidget *parent, const char *name )
41 : QWidget( parent, name )
42{
43 xPos = 0;
44 yPos = 0;
45 minX = 0;
46 minY = 0;
47 maxX = 100;
48 maxY = 100;
49 store.setOptimization( QPixmap::BestOptim );
50 store.resize( STORE_W2, STORE_W2 );
51}
52
53
54OXYSelector::~OXYSelector()
55{}
56
57
58void OXYSelector::setRange( int _minX, int _minY, int _maxX, int _maxY )
59{
60 px = 2;
61 py = 2;
62 minX = _minX;
63 minY = _minY;
64 maxX = _maxX;
65 maxY = _maxY;
66}
67
68void OXYSelector::setValues( int _xPos, int _yPos )
69{
70 xPos = _xPos;
71 yPos = _yPos;
72
73 if ( xPos > maxX )
74 xPos = maxX;
75 else if ( xPos < minX )
76 xPos = minX;
77
78 if ( yPos > maxY )
79 yPos = maxY;
80 else if ( yPos < minY )
81 yPos = minY;
82
83 int xp = 2 + (width() - 4) * xPos / (maxX - minX);
84 int yp = height() - 2 - (height() - 4) * yPos / (maxY - minY);
85
86 setPosition( xp, yp );
87}
88
89QRect OXYSelector::contentsRect() const
90{
91 return QRect( 2, 2, width()-4, height()-4 );
92}
93
94void OXYSelector::paintEvent( QPaintEvent *ev )
95{
96 QRect cursorRect( px - STORE_W, py - STORE_W, STORE_W2, STORE_W2);
97 QRect paintRect = ev->rect();
98
99 QPainter painter;
100 painter.begin( this );
101
102 QBrush brush;
103 qDrawShadePanel( &painter, 0, 0, width(), height(), colorGroup(),
104 TRUE, 2, &brush );
105
106 drawContents( &painter );
107 if (paintRect.contains(cursorRect))
108 {
109 bitBlt( &store, 0, 0, this, px - STORE_W, py - STORE_W,
110 STORE_W2, STORE_W2, CopyROP );
111 drawCursor( &painter, px, py );
112 }
113 else if (paintRect.intersects(cursorRect))
114 {
115 repaint( cursorRect, false);
116 }
117
118 painter.end();
119}
120
121void OXYSelector::mousePressEvent( QMouseEvent *e )
122{
123 int xVal, yVal;
124 valuesFromPosition( e->pos().x() - 2, e->pos().y() - 2, xVal, yVal );
125 setValues( xVal, yVal );
126
127 emit valueChanged( xPos, yPos );
128}
129
130void OXYSelector::mouseMoveEvent( QMouseEvent *e )
131{
132 int xVal, yVal;
133 valuesFromPosition( e->pos().x() - 2, e->pos().y() - 2, xVal, yVal );
134 setValues( xVal, yVal );
135
136 emit valueChanged( xPos, yPos );
137}
138
139void OXYSelector::wheelEvent( QWheelEvent *e )
140{
141 #if QT_VERSION > 290
142 if ( e->orientation() == Qt::Horizontal )
143 setValues( xValue() + e->delta()/120, yValue() );
144 else
145 setValues( xValue(), yValue() + e->delta()/120 );
146
147 emit valueChanged( xPos, yPos );
148 #endif
149}
150
151void OXYSelector::valuesFromPosition( int x, int y, int &xVal, int &yVal ) const
152{
153 xVal = ( (maxX-minX) * (x-2) ) / ( width()-4 );
154 yVal = maxY - ( ( (maxY-minY) * (y-2) ) / ( height()-4 ) );
155
156 if ( xVal > maxX )
157 xVal = maxX;
158 else if ( xVal < minX )
159 xVal = minX;
160
161 if ( yVal > maxY )
162 yVal = maxY;
163 else if ( yVal < minY )
164 yVal = minY;
165}
166
167void OXYSelector::setPosition( int xp, int yp )
168{
169 if ( xp < 2 )
170 xp = 2;
171 else if ( xp > width() - 2 )
172 xp = width() - 2;
173
174 if ( yp < 2 )
175 yp = 2;
176 else if ( yp > height() - 2 )
177 yp = height() - 2;
178
179 QPainter painter;
180 painter.begin( this );
181
182 bitBlt( this, px - STORE_W, py - STORE_W, &store, 0, 0,
183 STORE_W2, STORE_W2, CopyROP );
184 bitBlt( &store, 0, 0, this, xp - STORE_W, yp - STORE_W,
185 STORE_W2, STORE_W2, CopyROP );
186 drawCursor( &painter, xp, yp );
187 px = xp;
188 py = yp;
189
190 painter.end();
191}
192
193void OXYSelector::drawContents( QPainter * )
194{}
195
196
197void OXYSelector::drawCursor( QPainter *p, int xp, int yp )
198{
199 p->setPen( QPen( white ) );
200
201 p->drawLine( xp - 6, yp - 6, xp - 2, yp - 2 );
202 p->drawLine( xp - 6, yp + 6, xp - 2, yp + 2 );
203 p->drawLine( xp + 6, yp - 6, xp + 2, yp - 2 );
204 p->drawLine( xp + 6, yp + 6, xp + 2, yp + 2 );
205}
206
207//-----------------------------------------------------------------------------
208/*
209 * 1D value selector with contents drawn by derived class.
210 * See OColorDialog for example.
211 */
212
213
214OSelector::OSelector( QWidget *parent, const char *name )
215 : QWidget( parent, name ), QRangeControl()
216{
217 _orientation = Horizontal;
218 _indent = TRUE;
219}
220
221OSelector::OSelector( Orientation o, QWidget *parent, const char *name )
222 : QWidget( parent, name ), QRangeControl()
223{
224 _orientation = o;
225 _indent = TRUE;
226}
227
228
229OSelector::~OSelector()
230{}
231
232
233QRect OSelector::contentsRect() const
234{
235 if ( orientation() == Vertical )
236 return QRect( 2, 5, width()-9, height()-10 );
237 else
238 return QRect( 5, 2, width()-10, height()-9 );
239}
240
241void OSelector::paintEvent( QPaintEvent * )
242{
243 QPainter painter;
244
245 painter.begin( this );
246
247 drawContents( &painter );
248
249 QBrush brush;
250
251 if ( indent() )
252 {
253 if ( orientation() == Vertical )
254 qDrawShadePanel( &painter, 0, 3, width()-5, height()-6,
255 colorGroup(), TRUE, 2, &brush );
256 else
257 qDrawShadePanel( &painter, 3, 0, width()-6, height()-5,
258 colorGroup(), TRUE, 2, &brush );
259 }
260
261 QPoint pos = calcArrowPos( value() );
262 drawArrow( &painter, TRUE, pos );
263
264 painter.end();
265}
266
267void OSelector::mousePressEvent( QMouseEvent *e )
268{
269 moveArrow( e->pos() );
270}
271
272void OSelector::mouseMoveEvent( QMouseEvent *e )
273{
274 moveArrow( e->pos() );
275}
276
277void OSelector::wheelEvent( QWheelEvent *e )
278{
279 int val = value() + e->delta()/120;
280 emit valueChanged( val );
281 setValue( val );
282}
283
284void OSelector::valueChange()
285{
286 QPainter painter;
287 QPoint pos;
288
289 painter.begin( this );
290
291 pos = calcArrowPos( prevValue() );
292 drawArrow( &painter, FALSE, pos );
293
294 pos = calcArrowPos( value() );
295 drawArrow( &painter, TRUE, pos );
296
297 painter.end();
298}
299
300void OSelector::moveArrow( const QPoint &pos )
301{
302 int val;
303
304 if ( orientation() == Vertical )
305 val = ( maxValue() - minValue() ) * (height()-pos.y()-3)
306 / (height()-10) + minValue();
307 else
308 val = ( maxValue() - minValue() ) * (width()-pos.x()-3)
309 / (width()-10) + minValue();
310
311 if ( val > maxValue() )
312 val = maxValue();
313 if ( val < minValue() )
314 val = minValue();
315
316 emit valueChanged( val );
317 setValue( val );
318}
319
320QPoint OSelector::calcArrowPos( int val )
321{
322 QPoint p;
323
324 if ( orientation() == Vertical )
325 {
326 p.setY( height() - ( (height()-10) * val
327 / ( maxValue() - minValue() ) + 5 ) );
328 p.setX( width() - 5 );
329 }
330 else
331 {
332 p.setX( width() - ( (width()-10) * val
333 / ( maxValue() - minValue() ) + 5 ) );
334 p.setY( height() - 5 );
335 }
336
337 return p;
338}
339
340void OSelector::drawContents( QPainter * )
341{}
342
343void OSelector::drawArrow( QPainter *painter, bool show, const QPoint &pos )
344{
345 if ( show )
346 {
347 QPointArray array(3);
348
349 painter->setPen( QPen() );
350 painter->setBrush( QBrush( colorGroup().buttonText() ) );
351 if ( orientation() == Vertical )
352 {
353 array.setPoint( 0, pos.x()+0, pos.y()+0 );
354 array.setPoint( 1, pos.x()+5, pos.y()+5 );
355 array.setPoint( 2, pos.x()+5, pos.y()-5 );
356 }
357 else
358 {
359 array.setPoint( 0, pos.x()+0, pos.y()+0 );
360 array.setPoint( 1, pos.x()+5, pos.y()+5 );
361 array.setPoint( 2, pos.x()-5, pos.y()+5 );
362 }
363
364 painter->drawPolygon( array );
365 }
366 else
367 {
368 if ( orientation() == Vertical )
369 {
370 repaint(pos.x(), pos.y()-5, 6, 11, true);
371 }
372 else
373 {
374 repaint(pos.x()-5, pos.y(), 11, 6, true);
375 }
376 }
377}
378
379//----------------------------------------------------------------------------
380
381OGradientSelector::OGradientSelector( QWidget *parent, const char *name )
382 : OSelector( parent, name )
383{
384 init();
385}
386
387
388OGradientSelector::OGradientSelector( Orientation o, QWidget *parent,
389 const char *name )
390 : OSelector( o, parent, name )
391{
392 init();
393}
394
395
396OGradientSelector::~OGradientSelector()
397{}
398
399
400void OGradientSelector::init()
401{
402 color1.setRgb( 0, 0, 0 );
403 color2.setRgb( 255, 255, 255 );
404
405 text1 = text2 = "";
406}
407
408
409void OGradientSelector::drawContents( QPainter *painter )
410{
411 QImage image( contentsRect().width(), contentsRect().height(), 32 );
412
413 QColor col;
414 float scale;
415
416 int redDiff = color2.red() - color1.red();
417 int greenDiff = color2.green() - color1.green();
418 int blueDiff = color2.blue() - color1.blue();
419
420 if ( orientation() == Vertical )
421 {
422 for ( int y = 0; y < image.height(); y++ )
423 {
424 scale = 1.0 * y / image.height();
425 col.setRgb( color1.red() + int(redDiff*scale),
426 color1.green() + int(greenDiff*scale),
427 color1.blue() + int(blueDiff*scale) );
428
429 unsigned int *p = (uint *) image.scanLine( y );
430 for ( int x = 0; x < image.width(); x++ )
431 *p++ = col.rgb();
432 }
433 }
434 else
435 {
436 unsigned int *p = (uint *) image.scanLine( 0 );
437
438 for ( int x = 0; x < image.width(); x++ )
439 {
440 scale = 1.0 * x / image.width();
441 col.setRgb( color1.red() + int(redDiff*scale),
442 color1.green() + int(greenDiff*scale),
443 color1.blue() + int(blueDiff*scale) );
444 *p++ = col.rgb();
445 }
446
447 for ( int y = 1; y < image.height(); y++ )
448 memcpy( image.scanLine( y ), image.scanLine( y - 1),
449 sizeof( unsigned int ) * image.width() );
450 }
451
452 QColor ditherPalette[8];
453
454 for ( int s = 0; s < 8; s++ )
455 ditherPalette[s].setRgb( color1.red() + redDiff * s / 8,
456 color1.green() + greenDiff * s / 8,
457 color1.blue() + blueDiff * s / 8 );
458
459 OImageEffect::dither( image, ditherPalette, 8 );
460
461 QPixmap p;
462 p.convertFromImage( image );
463
464 painter->drawPixmap( contentsRect().x(), contentsRect().y(), p );
465
466 if ( orientation() == Vertical )
467 {
468 int yPos = contentsRect().top() + painter->fontMetrics().ascent() + 2;
469 int xPos = contentsRect().left() + (contentsRect().width() -
470 painter->fontMetrics().width( text2 )) / 2;
471 QPen pen( color2 );
472 painter->setPen( pen );
473 painter->drawText( xPos, yPos, text2 );
474
475 yPos = contentsRect().bottom() - painter->fontMetrics().descent() - 2;
476 xPos = contentsRect().left() + (contentsRect().width() -
477 painter->fontMetrics().width( text1 )) / 2;
478 pen.setColor( color1 );
479 painter->setPen( pen );
480 painter->drawText( xPos, yPos, text1 );
481 }
482 else
483 {
484 int yPos = contentsRect().bottom()-painter->fontMetrics().descent()-2;
485
486 QPen pen( color2 );
487 painter->setPen( pen );
488 painter->drawText( contentsRect().left() + 2, yPos, text1 );
489
490 pen.setColor( color1 );
491 painter->setPen( pen );
492 painter->drawText( contentsRect().right() -
493 painter->fontMetrics().width( text2 ) - 2, yPos, text2 );
494 }
495}
496
497//-----------------------------------------------------------------------------
498
499static QColor *standardPalette = 0;
500
501#define STANDARD_PAL_SIZE 17
502
503OColor::OColor()
504: QColor()
505{
506 r = 0; g = 0; b = 0; h = 0; s = 0; v = 0;
507};
508
509OColor::OColor( const OColor &col)
510: QColor( col )
511{
512 h = col.h; s = col.s; v = col.v;
513 r = col.r; g = col.g; b = col.b;
514};
515
516OColor::OColor( const QColor &col)
517: QColor( col )
518{
519 QColor::rgb(&r, &g, &b);
520 QColor::hsv(&h, &s, &v);
521};
522
523bool OColor::operator==(const OColor& col) const
524{
525 return (h == col.h) && (s == col.s) && (v == col.v) &&
526 (r == col.r) && (g == col.g) && (b == col.b);
527}
528
529OColor& OColor::operator=(const OColor& col)
530{
531 *(QColor *)this = col;
532 h = col.h; s = col.s; v = col.v;
533 r = col.r; g = col.g; b = col.b;
534 return *this;
535}
536
537void
538OColor::setHsv(int _h, int _s, int _v)
539{
540 h = _h; s = _s; v = _v;
541 QColor::setHsv(h, s, v);
542 QColor::rgb(&r, &g, &b);
543};
544
545void
546OColor::setRgb(int _r, int _g, int _b)
547{
548 r = _r; g = _g; b = _b;
549 QColor::setRgb(r, g, b);
550 QColor::hsv(&h, &s, &v);
551}
552
553void
554OColor::rgb(int *_r, int *_g, int *_b) const
555{
556 *_r = r; *_g = g; *_b = b;
557}
558
559void
560OColor::hsv(int *_h, int *_s, int *_v) const
561{
562 *_h = h; *_s = s; *_v = v;
563}
564
565static void createStandardPalette()
566{
567 if ( standardPalette )
568 return;
569
570 standardPalette = new QColor[STANDARD_PAL_SIZE];
571
572 int i = 0;
573
574 standardPalette[i++] = Qt::red;
575 standardPalette[i++] = Qt::green;
576 standardPalette[i++] = Qt::blue;
577 standardPalette[i++] = Qt::cyan;
578 standardPalette[i++] = Qt::magenta;
579 standardPalette[i++] = Qt::yellow;
580 standardPalette[i++] = Qt::darkRed;
581 standardPalette[i++] = Qt::darkGreen;
582 standardPalette[i++] = Qt::darkBlue;
583 standardPalette[i++] = Qt::darkCyan;
584 standardPalette[i++] = Qt::darkMagenta;
585 standardPalette[i++] = Qt::darkYellow;
586 standardPalette[i++] = Qt::white;
587 standardPalette[i++] = Qt::lightGray;
588 standardPalette[i++] = Qt::gray;
589 standardPalette[i++] = Qt::darkGray;
590 standardPalette[i++] = Qt::black;
591}
592
593
594OHSSelector::OHSSelector( QWidget *parent, const char *name )
595 : OXYSelector( parent, name )
596{
597 setRange( 0, 0, 359, 255 );
598}
599
600void OHSSelector::updateContents()
601{
602 drawPalette(&pixmap);
603}
604
605void OHSSelector::resizeEvent( QResizeEvent * )
606{
607 updateContents();
608}
609
610void OHSSelector::drawContents( QPainter *painter )
611{
612 painter->drawPixmap( contentsRect().x(), contentsRect().y(), pixmap );
613}
614
615void OHSSelector::drawPalette( QPixmap *pixmap )
616{
617 int xSize = contentsRect().width(), ySize = contentsRect().height();
618 QImage image( xSize, ySize, 32 );
619 QColor col;
620 int h, s;
621 uint *p;
622
623 for ( s = ySize-1; s >= 0; s-- )
624 {
625 p = (uint *) image.scanLine( ySize - s - 1 );
626 for( h = 0; h < xSize; h++ )
627 {
628 col.setHsv( 359*h/(xSize-1), 255*s/(ySize-1), 192 );
629 *p = col.rgb();
630 p++;
631 }
632 }
633
634 if ( QColor::numBitPlanes() <= 8 )
635 {
636 createStandardPalette();
637 OImageEffect::dither( image, standardPalette, STANDARD_PAL_SIZE );
638 }
639 pixmap->convertFromImage( image );
640}
641
642
643//-----------------------------------------------------------------------------
644
645OValueSelector::OValueSelector( QWidget *parent, const char *name )
646 : OSelector( OSelector::Vertical, parent, name ), _hue(0), _sat(0)
647{
648 setRange( 0, 255 );
649 pixmap.setOptimization( QPixmap::BestOptim );
650}
651
652OValueSelector::OValueSelector(Orientation o, QWidget *parent, const char *name
653 )
654 : OSelector( o, parent, name), _hue(0), _sat(0)
655{
656 setRange( 0, 255 );
657 pixmap.setOptimization( QPixmap::BestOptim );
658}
659
660void OValueSelector::updateContents()
661{
662 drawPalette(&pixmap);
663}
664
665void OValueSelector::resizeEvent( QResizeEvent * )
666{
667 updateContents();
668}
669
670void OValueSelector::drawContents( QPainter *painter )
671{
672 painter->drawPixmap( contentsRect().x(), contentsRect().y(), pixmap );
673}
674
675void OValueSelector::drawPalette( QPixmap *pixmap )
676{
677 int xSize = contentsRect().width(), ySize = contentsRect().height();
678 QImage image( xSize, ySize, 32 );
679 QColor col;
680 uint *p;
681 QRgb rgb;
682
683 if ( orientation() == OSelector::Horizontal )
684 {
685 for ( int v = 0; v < ySize; v++ )
686 {
687 p = (uint *) image.scanLine( ySize - v - 1 );
688
689 for( int x = 0; x < xSize; x++ )
690 {
691 col.setHsv( _hue, _sat, 255*x/(xSize-1) );
692 rgb = col.rgb();
693 *p++ = rgb;
694 }
695 }
696 }
697
698 if( orientation() == OSelector::Vertical )
699 {
700 for ( int v = 0; v < ySize; v++ )
701 {
702 p = (uint *) image.scanLine( ySize - v - 1 );
703 col.setHsv( _hue, _sat, 255*v/(ySize-1) );
704 rgb = col.rgb();
705 for ( int i = 0; i < xSize; i++ )
706 *p++ = rgb;
707 }
708 }
709
710 if ( QColor::numBitPlanes() <= 8 )
711 {
712 createStandardPalette();
713 OImageEffect::dither( image, standardPalette, STANDARD_PAL_SIZE );
714 }
715 pixmap->convertFromImage( image );
716}