Diffstat (limited to 'noncore/unsupported/libopie/colordialog.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/unsupported/libopie/colordialog.cpp | 856 |
1 files changed, 856 insertions, 0 deletions
diff --git a/noncore/unsupported/libopie/colordialog.cpp b/noncore/unsupported/libopie/colordialog.cpp new file mode 100644 index 0000000..b2854a6 --- a/dev/null +++ b/noncore/unsupported/libopie/colordialog.cpp | |||
@@ -0,0 +1,856 @@ | |||
1 | /**************************************************************************** | ||
2 | ** $Id$ | ||
3 | ** | ||
4 | ** Implementation of OColorDialog class | ||
5 | ** | ||
6 | ** Created : 990222 | ||
7 | ** | ||
8 | ** Copyright (C) 1999-2000 Trolltech AS. All rights reserved. | ||
9 | ** | ||
10 | ** This file is part of the dialogs module of the Qt GUI Toolkit. | ||
11 | ** | ||
12 | ** This file may be distributed under the terms of the Q Public License | ||
13 | ** as defined by Trolltech AS of Norway and appearing in the file | ||
14 | ** LICENSE.QPL included in the packaging of this file. | ||
15 | ** | ||
16 | ** This file may be distributed and/or modified under the terms of the | ||
17 | ** GNU General Public License version 2 as published by the Free Software | ||
18 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
19 | ** packaging of this file. | ||
20 | ** | ||
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition | ||
22 | ** licenses may use this file in accordance with the Qt Commercial License | ||
23 | ** Agreement provided with the Software. | ||
24 | ** | ||
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
27 | ** | ||
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for | ||
29 | ** information about Qt Commercial License Agreements. | ||
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information. | ||
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
32 | ** | ||
33 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
34 | ** not clear to you. | ||
35 | ** | ||
36 | **********************************************************************/ | ||
37 | |||
38 | #include "colordialog.h" | ||
39 | |||
40 | #include "qpainter.h" | ||
41 | #include "qlayout.h" | ||
42 | #include "qlabel.h" | ||
43 | #include "qpushbutton.h" | ||
44 | #include "qlineedit.h" | ||
45 | #include "qimage.h" | ||
46 | #include "qpixmap.h" | ||
47 | #include "qdrawutil.h" | ||
48 | #include "qvalidator.h" | ||
49 | #include "qapplication.h" | ||
50 | |||
51 | static inline void rgb2hsv( QRgb rgb, int&h, int&s, int&v ) | ||
52 | { | ||
53 | QColor c; | ||
54 | c.setRgb( rgb ); | ||
55 | c.getHsv(h,s,v); | ||
56 | } | ||
57 | |||
58 | /* | ||
59 | * avoid clashes with the original Qt | ||
60 | */ | ||
61 | namespace { | ||
62 | |||
63 | class QColorPicker : public QFrame | ||
64 | { | ||
65 | Q_OBJECT | ||
66 | public: | ||
67 | QColorPicker(QWidget* parent=0, const char* name=0); | ||
68 | ~QColorPicker(); | ||
69 | |||
70 | public slots: | ||
71 | void setCol( int h, int s ); | ||
72 | |||
73 | signals: | ||
74 | void newCol( int h, int s ); | ||
75 | |||
76 | protected: | ||
77 | QSize sizeHint() const; | ||
78 | QSizePolicy sizePolicy() const; | ||
79 | void drawContents(QPainter* p); | ||
80 | void mouseMoveEvent( QMouseEvent * ); | ||
81 | void mousePressEvent( QMouseEvent * ); | ||
82 | |||
83 | private: | ||
84 | int hue; | ||
85 | int sat; | ||
86 | |||
87 | QPoint colPt(); | ||
88 | int huePt( const QPoint &pt ); | ||
89 | int satPt( const QPoint &pt ); | ||
90 | void setCol( const QPoint &pt ); | ||
91 | |||
92 | QPixmap *pix; | ||
93 | }; | ||
94 | |||
95 | static int pWidth = 200; | ||
96 | static int pHeight = 200; | ||
97 | |||
98 | class QColorLuminancePicker : public QWidget | ||
99 | { | ||
100 | Q_OBJECT | ||
101 | public: | ||
102 | QColorLuminancePicker(QWidget* parent=0, const char* name=0); | ||
103 | ~QColorLuminancePicker(); | ||
104 | |||
105 | public slots: | ||
106 | void setCol( int h, int s, int v ); | ||
107 | void setCol( int h, int s ); | ||
108 | |||
109 | signals: | ||
110 | void newHsv( int h, int s, int v ); | ||
111 | |||
112 | protected: | ||
113 | // QSize sizeHint() const; | ||
114 | // QSizePolicy sizePolicy() const; | ||
115 | void paintEvent( QPaintEvent*); | ||
116 | void mouseMoveEvent( QMouseEvent * ); | ||
117 | void mousePressEvent( QMouseEvent * ); | ||
118 | |||
119 | private: | ||
120 | enum { foff = 3, coff = 4 }; //frame and contents offset | ||
121 | int val; | ||
122 | int hue; | ||
123 | int sat; | ||
124 | |||
125 | int y2val( int y ); | ||
126 | int val2y( int val ); | ||
127 | void setVal( int v ); | ||
128 | |||
129 | QPixmap *pix; | ||
130 | }; | ||
131 | |||
132 | |||
133 | int QColorLuminancePicker::y2val( int y ) | ||
134 | { | ||
135 | int d = height() - 2*coff - 1; | ||
136 | return 255 - (y - coff)*255/d; | ||
137 | } | ||
138 | |||
139 | int QColorLuminancePicker::val2y( int v ) | ||
140 | { | ||
141 | int d = height() - 2*coff - 1; | ||
142 | return coff + (255-v)*d/255; | ||
143 | } | ||
144 | |||
145 | QColorLuminancePicker::QColorLuminancePicker(QWidget* parent, | ||
146 | const char* name) | ||
147 | :QWidget( parent, name ) | ||
148 | { | ||
149 | hue = 100; val = 100; sat = 100; | ||
150 | pix = 0; | ||
151 | // setBackgroundMode( NoBackground ); | ||
152 | } | ||
153 | |||
154 | QColorLuminancePicker::~QColorLuminancePicker() | ||
155 | { | ||
156 | delete pix; | ||
157 | } | ||
158 | |||
159 | void QColorLuminancePicker::mouseMoveEvent( QMouseEvent *m ) | ||
160 | { | ||
161 | setVal( y2val(m->y()) ); | ||
162 | } | ||
163 | void QColorLuminancePicker::mousePressEvent( QMouseEvent *m ) | ||
164 | { | ||
165 | setVal( y2val(m->y()) ); | ||
166 | } | ||
167 | |||
168 | void QColorLuminancePicker::setVal( int v ) | ||
169 | { | ||
170 | if ( val == v ) | ||
171 | return; | ||
172 | val = QMAX( 0, QMIN(v,255)); | ||
173 | delete pix; pix=0; | ||
174 | repaint( FALSE ); //### | ||
175 | emit newHsv( hue, sat, val ); | ||
176 | } | ||
177 | |||
178 | //receives from a hue,sat chooser and relays. | ||
179 | void QColorLuminancePicker::setCol( int h, int s ) | ||
180 | { | ||
181 | setCol( h, s, val ); | ||
182 | emit newHsv( h, s, val ); | ||
183 | } | ||
184 | |||
185 | void QColorLuminancePicker::paintEvent( QPaintEvent * ) | ||
186 | { | ||
187 | int w = width() - 5; | ||
188 | |||
189 | QRect r( 0, foff, w, height() - 2*foff ); | ||
190 | int wi = r.width() - 2; | ||
191 | int hi = r.height() - 2; | ||
192 | if ( !pix || pix->height() != hi || pix->width() != wi ) { | ||
193 | delete pix; | ||
194 | QImage img( wi, hi, 32 ); | ||
195 | int y; | ||
196 | for ( y = 0; y < hi; y++ ) { | ||
197 | QColor c( hue, sat, y2val(y+coff), QColor::Hsv ); | ||
198 | QRgb r = c.rgb(); | ||
199 | int x; | ||
200 | for ( x = 0; x < wi; x++ ) | ||
201 | img.setPixel( x, y, r ); | ||
202 | } | ||
203 | pix = new QPixmap; | ||
204 | pix->convertFromImage(img); | ||
205 | } | ||
206 | QPainter p(this); | ||
207 | p.drawPixmap( 1, coff, *pix ); | ||
208 | QColorGroup g = colorGroup(); | ||
209 | qDrawShadePanel( &p, r, g, TRUE ); | ||
210 | p.setPen( g.foreground() ); | ||
211 | p.setBrush( g.foreground() ); | ||
212 | QPointArray a; | ||
213 | int y = val2y(val); | ||
214 | a.setPoints( 3, w, y, w+5, y+5, w+5, y-5 ); | ||
215 | erase( w, 0, 5, height() ); | ||
216 | p.drawPolygon( a ); | ||
217 | } | ||
218 | |||
219 | void QColorLuminancePicker::setCol( int h, int s , int v ) | ||
220 | { | ||
221 | val = v; | ||
222 | hue = h; | ||
223 | sat = s; | ||
224 | delete pix; pix=0; | ||
225 | repaint( FALSE );//#### | ||
226 | } | ||
227 | |||
228 | QPoint QColorPicker::colPt() | ||
229 | { return QPoint( (360-hue)*(pWidth-1)/360, (255-sat)*(pHeight-1)/255 ); } | ||
230 | int QColorPicker::huePt( const QPoint &pt ) | ||
231 | { return 360 - pt.x()*360/(pWidth-1); } | ||
232 | int QColorPicker::satPt( const QPoint &pt ) | ||
233 | { return 255 - pt.y()*255/(pHeight-1) ; } | ||
234 | void QColorPicker::setCol( const QPoint &pt ) | ||
235 | { setCol( huePt(pt), satPt(pt) ); } | ||
236 | |||
237 | QColorPicker::QColorPicker(QWidget* parent, const char* name ) | ||
238 | : QFrame( parent, name ) | ||
239 | { | ||
240 | hue = 0; sat = 0; | ||
241 | setCol( 150, 255 ); | ||
242 | |||
243 | QImage img( pWidth, pHeight, 32 ); | ||
244 | int x,y; | ||
245 | for ( y = 0; y < pHeight; y++ ) | ||
246 | for ( x = 0; x < pWidth; x++ ) { | ||
247 | QPoint p( x, y ); | ||
248 | img.setPixel( x, y, QColor(huePt(p), satPt(p), | ||
249 | 200, QColor::Hsv).rgb() ); | ||
250 | } | ||
251 | pix = new QPixmap; | ||
252 | pix->convertFromImage(img); | ||
253 | setBackgroundMode( NoBackground ); | ||
254 | } | ||
255 | |||
256 | QColorPicker::~QColorPicker() | ||
257 | { | ||
258 | delete pix; | ||
259 | } | ||
260 | |||
261 | QSize QColorPicker::sizeHint() const | ||
262 | { | ||
263 | return QSize( pWidth + 2*frameWidth(), pHeight + 2*frameWidth() ); | ||
264 | } | ||
265 | |||
266 | QSizePolicy QColorPicker::sizePolicy() const | ||
267 | { | ||
268 | return QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ); | ||
269 | } | ||
270 | |||
271 | void QColorPicker::setCol( int h, int s ) | ||
272 | { | ||
273 | int nhue = QMIN( QMAX(0,h), 360 ); | ||
274 | int nsat = QMIN( QMAX(0,s), 255); | ||
275 | if ( nhue == hue && nsat == sat ) | ||
276 | return; | ||
277 | QRect r( colPt(), QSize(20,20) ); | ||
278 | hue = nhue; sat = nsat; | ||
279 | r = r.unite( QRect( colPt(), QSize(20,20) ) ); | ||
280 | r.moveBy( contentsRect().x()-9, contentsRect().y()-9 ); | ||
281 | // update( r ); | ||
282 | repaint( r, FALSE ); | ||
283 | } | ||
284 | |||
285 | void QColorPicker::mouseMoveEvent( QMouseEvent *m ) | ||
286 | { | ||
287 | QPoint p = m->pos() - contentsRect().topLeft(); | ||
288 | setCol( p ); | ||
289 | emit newCol( hue, sat ); | ||
290 | } | ||
291 | |||
292 | void QColorPicker::mousePressEvent( QMouseEvent *m ) | ||
293 | { | ||
294 | QPoint p = m->pos() - contentsRect().topLeft(); | ||
295 | setCol( p ); | ||
296 | emit newCol( hue, sat ); | ||
297 | } | ||
298 | |||
299 | void QColorPicker::drawContents(QPainter* p) | ||
300 | { | ||
301 | QRect r = contentsRect(); | ||
302 | |||
303 | p->drawPixmap( r.topLeft(), *pix ); | ||
304 | QPoint pt = colPt() + r.topLeft(); | ||
305 | p->setPen( QPen(black) ); | ||
306 | |||
307 | p->fillRect( pt.x()-9, pt.y(), 20, 2, black ); | ||
308 | p->fillRect( pt.x(), pt.y()-9, 2, 20, black ); | ||
309 | |||
310 | } | ||
311 | |||
312 | class QColorShowLabel; | ||
313 | |||
314 | |||
315 | |||
316 | class QColIntValidator: public QIntValidator | ||
317 | { | ||
318 | public: | ||
319 | QColIntValidator( int bottom, int top, | ||
320 | QWidget * parent, const char *name = 0 ) | ||
321 | :QIntValidator( bottom, top, parent, name ) {} | ||
322 | |||
323 | QValidator::State validate( QString &, int & ) const; | ||
324 | }; | ||
325 | |||
326 | QValidator::State QColIntValidator::validate( QString &s, int &pos ) const | ||
327 | { | ||
328 | State state = QIntValidator::validate(s,pos); | ||
329 | if ( state == Valid ) { | ||
330 | long int val = s.toLong(); | ||
331 | // This is not a general solution, assumes that top() > 0 and | ||
332 | // bottom >= 0 | ||
333 | if ( val < 0 ) { | ||
334 | s = "0"; | ||
335 | pos = 1; | ||
336 | } else if ( val > top() ) { | ||
337 | s.setNum( top() ); | ||
338 | pos = s.length(); | ||
339 | } | ||
340 | } | ||
341 | return state; | ||
342 | } | ||
343 | |||
344 | |||
345 | |||
346 | class QColNumLineEdit : public QLineEdit | ||
347 | { | ||
348 | public: | ||
349 | QColNumLineEdit( QWidget *parent, const char* name = 0 ) | ||
350 | : QLineEdit( parent, name ) { setMaxLength( 3 );} | ||
351 | QSize sizeHint() const { | ||
352 | return QSize( 30, //##### | ||
353 | QLineEdit::sizeHint().height() ); } | ||
354 | void setNum( int i ) { | ||
355 | QString s; | ||
356 | s.setNum(i); | ||
357 | bool block = signalsBlocked(); | ||
358 | blockSignals(TRUE); | ||
359 | setText( s ); | ||
360 | blockSignals(block); | ||
361 | } | ||
362 | int val() const { return text().toInt(); } | ||
363 | }; | ||
364 | |||
365 | |||
366 | class QColorShower : public QWidget | ||
367 | { | ||
368 | Q_OBJECT | ||
369 | public: | ||
370 | QColorShower( QWidget *parent, const char *name = 0 ); | ||
371 | |||
372 | //things that don't emit signals | ||
373 | void setHsv( int h, int s, int v ); | ||
374 | |||
375 | int currentAlpha() const { return alphaEd->val(); } | ||
376 | void setCurrentAlpha( int a ) { alphaEd->setNum( a ); } | ||
377 | void showAlpha( bool b ); | ||
378 | |||
379 | |||
380 | QRgb currentColor() const { return curCol; } | ||
381 | |||
382 | public slots: | ||
383 | void setRgb( QRgb rgb ); | ||
384 | |||
385 | signals: | ||
386 | void newCol( QRgb rgb ); | ||
387 | private slots: | ||
388 | void rgbEd(); | ||
389 | void hsvEd(); | ||
390 | private: | ||
391 | void showCurrentColor(); | ||
392 | int hue, sat, val; | ||
393 | QRgb curCol; | ||
394 | QColNumLineEdit *hEd; | ||
395 | QColNumLineEdit *sEd; | ||
396 | QColNumLineEdit *vEd; | ||
397 | QColNumLineEdit *rEd; | ||
398 | QColNumLineEdit *gEd; | ||
399 | QColNumLineEdit *bEd; | ||
400 | QColNumLineEdit *alphaEd; | ||
401 | QLabel *alphaLab; | ||
402 | QColorShowLabel *lab; | ||
403 | bool rgbOriginal; | ||
404 | }; | ||
405 | |||
406 | class QColorShowLabel : public QFrame | ||
407 | { | ||
408 | Q_OBJECT | ||
409 | |||
410 | public: | ||
411 | QColorShowLabel( QWidget *parent ) :QFrame( parent ) { | ||
412 | setFrameStyle( QFrame::Panel|QFrame::Sunken ); | ||
413 | setBackgroundMode( PaletteBackground ); | ||
414 | setAcceptDrops( TRUE ); | ||
415 | mousePressed = FALSE; | ||
416 | } | ||
417 | void setColor( QColor c ) { col = c; } | ||
418 | |||
419 | signals: | ||
420 | void colorDropped( QRgb ); | ||
421 | |||
422 | protected: | ||
423 | void drawContents( QPainter *p ); | ||
424 | void mousePressEvent( QMouseEvent *e ); | ||
425 | void mouseReleaseEvent( QMouseEvent *e ); | ||
426 | |||
427 | private: | ||
428 | QColor col; | ||
429 | bool mousePressed; | ||
430 | QPoint pressPos; | ||
431 | |||
432 | }; | ||
433 | |||
434 | void QColorShowLabel::drawContents( QPainter *p ) | ||
435 | { | ||
436 | p->fillRect( contentsRect(), col ); | ||
437 | } | ||
438 | |||
439 | void QColorShower::showAlpha( bool b ) | ||
440 | { | ||
441 | if ( b ) { | ||
442 | alphaLab->show(); | ||
443 | alphaEd->show(); | ||
444 | } else { | ||
445 | alphaLab->hide(); | ||
446 | alphaEd->hide(); | ||
447 | } | ||
448 | } | ||
449 | |||
450 | void QColorShowLabel::mousePressEvent( QMouseEvent *e ) | ||
451 | { | ||
452 | mousePressed = TRUE; | ||
453 | pressPos = e->pos(); | ||
454 | } | ||
455 | |||
456 | void QColorShowLabel::mouseReleaseEvent( QMouseEvent * ) | ||
457 | { | ||
458 | if ( !mousePressed ) | ||
459 | return; | ||
460 | mousePressed = FALSE; | ||
461 | } | ||
462 | |||
463 | QColorShower::QColorShower( QWidget *parent, const char *name ) | ||
464 | :QWidget( parent, name) | ||
465 | { | ||
466 | curCol = qRgb( -1, -1, -1 ); | ||
467 | QColIntValidator *val256 = new QColIntValidator( 0, 255, this ); | ||
468 | QColIntValidator *val360 = new QColIntValidator( 0, 360, this ); | ||
469 | |||
470 | QGridLayout *gl = new QGridLayout( this, 1, 1, 2 ); | ||
471 | gl->setMargin( 0 ); | ||
472 | lab = new QColorShowLabel( this ); | ||
473 | lab->setMinimumWidth( 60 ); //### | ||
474 | gl->addMultiCellWidget(lab, 0,-1,0,0); | ||
475 | connect( lab, SIGNAL( colorDropped(QRgb) ), | ||
476 | this, SIGNAL( newCol(QRgb) ) ); | ||
477 | connect( lab, SIGNAL( colorDropped(QRgb) ), | ||
478 | this, SLOT( setRgb(QRgb) ) ); | ||
479 | |||
480 | hEd = new QColNumLineEdit( this ); | ||
481 | hEd->setValidator( val360 ); | ||
482 | QLabel *l = new QLabel( hEd, OColorDialog::tr("Hue:"), this ); | ||
483 | l->setAlignment( AlignRight|AlignVCenter ); | ||
484 | gl->addWidget( l, 0, 1 ); | ||
485 | gl->addWidget( hEd, 0, 2 ); | ||
486 | |||
487 | sEd = new QColNumLineEdit( this ); | ||
488 | sEd->setValidator( val256 ); | ||
489 | l = new QLabel( sEd, OColorDialog::tr("Sat:"), this ); | ||
490 | l->setAlignment( AlignRight|AlignVCenter ); | ||
491 | gl->addWidget( l, 1, 1 ); | ||
492 | gl->addWidget( sEd, 1, 2 ); | ||
493 | |||
494 | vEd = new QColNumLineEdit( this ); | ||
495 | vEd->setValidator( val256 ); | ||
496 | l = new QLabel( vEd, OColorDialog::tr("Val:"), this ); | ||
497 | l->setAlignment( AlignRight|AlignVCenter ); | ||
498 | gl->addWidget( l, 2, 1 ); | ||
499 | gl->addWidget( vEd, 2, 2 ); | ||
500 | |||
501 | rEd = new QColNumLineEdit( this ); | ||
502 | rEd->setValidator( val256 ); | ||
503 | l = new QLabel( rEd, OColorDialog::tr("Red:"), this ); | ||
504 | l->setAlignment( AlignRight|AlignVCenter ); | ||
505 | gl->addWidget( l, 0, 3 ); | ||
506 | gl->addWidget( rEd, 0, 4 ); | ||
507 | |||
508 | gEd = new QColNumLineEdit( this ); | ||
509 | gEd->setValidator( val256 ); | ||
510 | l = new QLabel( gEd, OColorDialog::tr("Green:"), this ); | ||
511 | l->setAlignment( AlignRight|AlignVCenter ); | ||
512 | gl->addWidget( l, 1, 3 ); | ||
513 | gl->addWidget( gEd, 1, 4 ); | ||
514 | |||
515 | bEd = new QColNumLineEdit( this ); | ||
516 | bEd->setValidator( val256 ); | ||
517 | l = new QLabel( bEd, OColorDialog::tr("Blue:"), this ); | ||
518 | l->setAlignment( AlignRight|AlignVCenter ); | ||
519 | gl->addWidget( l, 2, 3 ); | ||
520 | gl->addWidget( bEd, 2, 4 ); | ||
521 | |||
522 | alphaEd = new QColNumLineEdit( this ); | ||
523 | alphaEd->setValidator( val256 ); | ||
524 | alphaLab = new QLabel( alphaEd, OColorDialog::tr("Alpha channel:"), this ); | ||
525 | alphaLab->setAlignment( AlignRight|AlignVCenter ); | ||
526 | gl->addMultiCellWidget( alphaLab, 3, 3, 1, 3 ); | ||
527 | gl->addWidget( alphaEd, 3, 4 ); | ||
528 | alphaEd->hide(); | ||
529 | alphaLab->hide(); | ||
530 | |||
531 | connect( hEd, SIGNAL(textChanged(const QString&)), this, SLOT(hsvEd()) ); | ||
532 | connect( sEd, SIGNAL(textChanged(const QString&)), this, SLOT(hsvEd()) ); | ||
533 | connect( vEd, SIGNAL(textChanged(const QString&)), this, SLOT(hsvEd()) ); | ||
534 | |||
535 | connect( rEd, SIGNAL(textChanged(const QString&)), this, SLOT(rgbEd()) ); | ||
536 | connect( gEd, SIGNAL(textChanged(const QString&)), this, SLOT(rgbEd()) ); | ||
537 | connect( bEd, SIGNAL(textChanged(const QString&)), this, SLOT(rgbEd()) ); | ||
538 | } | ||
539 | |||
540 | void QColorShower::showCurrentColor() | ||
541 | { | ||
542 | lab->setColor( currentColor() ); | ||
543 | lab->repaint(FALSE); //### | ||
544 | } | ||
545 | |||
546 | void QColorShower::rgbEd() | ||
547 | { | ||
548 | rgbOriginal = TRUE; | ||
549 | curCol = qRgb( rEd->val(), gEd->val(), bEd->val() ); | ||
550 | rgb2hsv(currentColor(), hue, sat, val ); | ||
551 | |||
552 | hEd->setNum( hue ); | ||
553 | sEd->setNum( sat ); | ||
554 | vEd->setNum( val ); | ||
555 | |||
556 | showCurrentColor(); | ||
557 | emit newCol( currentColor() ); | ||
558 | } | ||
559 | |||
560 | void QColorShower::hsvEd() | ||
561 | { | ||
562 | rgbOriginal = FALSE; | ||
563 | hue = hEd->val(); | ||
564 | sat = sEd->val(); | ||
565 | val = vEd->val(); | ||
566 | |||
567 | curCol = QColor( hue, sat, val, QColor::Hsv ).rgb(); | ||
568 | |||
569 | rEd->setNum( qRed(currentColor()) ); | ||
570 | gEd->setNum( qGreen(currentColor()) ); | ||
571 | bEd->setNum( qBlue(currentColor()) ); | ||
572 | |||
573 | showCurrentColor(); | ||
574 | emit newCol( currentColor() ); | ||
575 | } | ||
576 | |||
577 | void QColorShower::setRgb( QRgb rgb ) | ||
578 | { | ||
579 | rgbOriginal = TRUE; | ||
580 | curCol = rgb; | ||
581 | |||
582 | rgb2hsv( currentColor(), hue, sat, val ); | ||
583 | |||
584 | hEd->setNum( hue ); | ||
585 | sEd->setNum( sat ); | ||
586 | vEd->setNum( val ); | ||
587 | |||
588 | rEd->setNum( qRed(currentColor()) ); | ||
589 | gEd->setNum( qGreen(currentColor()) ); | ||
590 | bEd->setNum( qBlue(currentColor()) ); | ||
591 | |||
592 | showCurrentColor(); | ||
593 | } | ||
594 | |||
595 | void QColorShower::setHsv( int h, int s, int v ) | ||
596 | { | ||
597 | rgbOriginal = FALSE; | ||
598 | hue = h; val = v; sat = s; //Range check### | ||
599 | curCol = QColor( hue, sat, val, QColor::Hsv ).rgb(); | ||
600 | |||
601 | hEd->setNum( hue ); | ||
602 | sEd->setNum( sat ); | ||
603 | vEd->setNum( val ); | ||
604 | |||
605 | rEd->setNum( qRed(currentColor()) ); | ||
606 | gEd->setNum( qGreen(currentColor()) ); | ||
607 | bEd->setNum( qBlue(currentColor()) ); | ||
608 | |||
609 | |||
610 | showCurrentColor(); | ||
611 | } | ||
612 | |||
613 | } | ||
614 | |||
615 | class OColorDialogPrivate : public QObject | ||
616 | { | ||
617 | Q_OBJECT | ||
618 | public: | ||
619 | OColorDialogPrivate( OColorDialog *p ); | ||
620 | QRgb currentColor() const { return cs->currentColor(); } | ||
621 | void setCurrentColor( const QRgb& rgb ); | ||
622 | |||
623 | int currentAlpha() const { return cs->currentAlpha(); } | ||
624 | void setCurrentAlpha( int a ) { cs->setCurrentAlpha( a ); } | ||
625 | void showAlpha( bool b ) { cs->showAlpha( b ); } | ||
626 | |||
627 | private slots: | ||
628 | void newHsv( int h, int s, int v ); | ||
629 | void newColorTypedIn( QRgb rgb ); | ||
630 | private: | ||
631 | QColorPicker *cp; | ||
632 | QColorLuminancePicker *lp; | ||
633 | QColorShower *cs; | ||
634 | }; | ||
635 | |||
636 | //sets all widgets to display h,s,v | ||
637 | void OColorDialogPrivate::newHsv( int h, int s, int v ) | ||
638 | { | ||
639 | cs->setHsv( h, s, v ); | ||
640 | cp->setCol( h, s ); | ||
641 | lp->setCol( h, s, v ); | ||
642 | } | ||
643 | |||
644 | //sets all widgets to display rgb | ||
645 | void OColorDialogPrivate::setCurrentColor( const QRgb& rgb ) | ||
646 | { | ||
647 | cs->setRgb( rgb ); | ||
648 | newColorTypedIn( rgb ); | ||
649 | } | ||
650 | |||
651 | //sets all widgets exept cs to display rgb | ||
652 | void OColorDialogPrivate::newColorTypedIn( QRgb rgb ) | ||
653 | { | ||
654 | int h, s, v; | ||
655 | rgb2hsv(rgb, h, s, v ); | ||
656 | cp->setCol( h, s ); | ||
657 | lp->setCol( h, s, v); | ||
658 | } | ||
659 | |||
660 | OColorDialogPrivate::OColorDialogPrivate( OColorDialog *dialog ) : | ||
661 | QObject(dialog) | ||
662 | { | ||
663 | int border = 2; | ||
664 | QVBoxLayout *topLay = new QVBoxLayout( dialog, border, 2 ); | ||
665 | |||
666 | QHBoxLayout *pickLay = new QHBoxLayout( topLay ); | ||
667 | |||
668 | |||
669 | cp = new QColorPicker( dialog ); | ||
670 | cp->setFrameStyle( QFrame::Panel + QFrame::Sunken ); | ||
671 | pickLay->addWidget( cp ); | ||
672 | |||
673 | pickLay->addStretch(); | ||
674 | |||
675 | lp = new QColorLuminancePicker( dialog ); | ||
676 | lp->setFixedWidth( 20 ); //### | ||
677 | pickLay->addWidget( lp ); | ||
678 | |||
679 | connect( cp, SIGNAL(newCol(int,int)), lp, SLOT(setCol(int,int)) ); | ||
680 | connect( lp, SIGNAL(newHsv(int,int,int)), this, SLOT(newHsv(int,int,int)) ); | ||
681 | |||
682 | topLay->addStretch(); | ||
683 | |||
684 | cs = new QColorShower( dialog ); | ||
685 | connect( cs, SIGNAL(newCol(QRgb)), this, SLOT(newColorTypedIn(QRgb))); | ||
686 | topLay->addWidget( cs ); | ||
687 | |||
688 | } | ||
689 | |||
690 | |||
691 | // BEING REVISED: jo | ||
692 | /*! | ||
693 | \class ColorDialog ColorDialog.h | ||
694 | \brief The OColorDialog class provides a dialog widget for specifying colors. | ||
695 | \ingroup dialogs | ||
696 | |||
697 | The color dialog's function is to allow users to choose colors - | ||
698 | for instance, you might use this in a drawing program to allow the | ||
699 | user to set the brush color. | ||
700 | |||
701 | This version of Qt only provides modal color dialogs. The static | ||
702 | getColor() function shows the dialog and allows the user to specify a color, | ||
703 | while getRgba() does the same but allows the user to specify a color with an | ||
704 | alpha channel (transparency) value. | ||
705 | |||
706 | The user can store customCount() different custom colors. The custom | ||
707 | colors are shared by all color dialogs, and remembered during the | ||
708 | execution of the program. Use setCustomColor() to set the | ||
709 | custom colors, and customColor() to get them. | ||
710 | |||
711 | <img src=qcolordlg-m.png> <img src=qcolordlg-w.png> | ||
712 | */ | ||
713 | |||
714 | /*! | ||
715 | Constructs a default color dialog. Use setColor() for setting an initial value. | ||
716 | |||
717 | \sa getColor() | ||
718 | */ | ||
719 | |||
720 | OColorDialog::OColorDialog(QWidget* parent, const char* name, bool modal) : | ||
721 | QDialog(parent, name, modal ) | ||
722 | { | ||
723 | d = new OColorDialogPrivate( this ); | ||
724 | } | ||
725 | |||
726 | |||
727 | /*! | ||
728 | Pops up a modal color dialog letting the user choose a color and returns | ||
729 | that color. The color is initially set to \a initial. Returns an \link QColor::isValid() invalid\endlink color if the user cancels | ||
730 | the dialog. All colors allocated by the dialog will be deallocated | ||
731 | before this function returns. | ||
732 | */ | ||
733 | |||
734 | QColor OColorDialog::getColor( const QColor& initial, QWidget *parent, | ||
735 | const char *name ) | ||
736 | { | ||
737 | int allocContext = QColor::enterAllocContext(); | ||
738 | OColorDialog *dlg = new OColorDialog( parent, name, TRUE ); //modal | ||
739 | if ( parent && parent->icon() && !parent->icon()->isNull() ) | ||
740 | dlg->setIcon( *parent->icon() ); | ||
741 | else if ( qApp->mainWidget() && qApp->mainWidget()->icon() && !qApp->mainWidget()->icon()->isNull() ) | ||
742 | dlg->setIcon( *qApp->mainWidget()->icon() ); | ||
743 | |||
744 | dlg->setCaption( OColorDialog::tr( "Select color" ) ); | ||
745 | dlg->setColor( initial ); | ||
746 | dlg->showMaximized(); | ||
747 | int resultCode = dlg->exec(); | ||
748 | QColor::leaveAllocContext(); | ||
749 | QColor result; | ||
750 | if ( resultCode == QDialog::Accepted ) { | ||
751 | result = dlg->color(); | ||
752 | } else { | ||
753 | result = initial; | ||
754 | } | ||
755 | QColor::destroyAllocContext(allocContext); | ||
756 | delete dlg; | ||
757 | return result; | ||
758 | } | ||
759 | |||
760 | |||
761 | /*! | ||
762 | Pops up a modal color dialog, letting the user choose a color and an | ||
763 | alpha channel value. The color+alpha is initially set to \a initial. | ||
764 | |||
765 | If \a ok is non-null, \c *ok is set to TRUE if the user clicked OK, | ||
766 | and FALSE if the user clicked Cancel. | ||
767 | |||
768 | If the user clicks Cancel the \a initial value is returned. | ||
769 | */ | ||
770 | |||
771 | QRgb OColorDialog::getRgba( const QRgb& initial, bool *ok, | ||
772 | QWidget *parent, const char* name ) | ||
773 | { | ||
774 | int allocContext = QColor::enterAllocContext(); | ||
775 | OColorDialog *dlg = new OColorDialog( parent, name, TRUE ); //modal | ||
776 | dlg->setColor( initial ); | ||
777 | dlg->setSelectedAlpha( qAlpha(initial) ); | ||
778 | dlg->showMaximized(); | ||
779 | int resultCode = dlg->exec(); | ||
780 | QColor::leaveAllocContext(); | ||
781 | QRgb result = initial; | ||
782 | if ( resultCode == QDialog::Accepted ) { | ||
783 | QRgb c = dlg->color().rgb(); | ||
784 | int alpha = dlg->selectedAlpha(); | ||
785 | result = qRgba( qRed(c), qGreen(c), qBlue(c), alpha ); | ||
786 | } | ||
787 | if ( ok ) | ||
788 | *ok = resultCode == QDialog::Accepted; | ||
789 | |||
790 | QColor::destroyAllocContext(allocContext); | ||
791 | delete dlg; | ||
792 | return result; | ||
793 | } | ||
794 | |||
795 | |||
796 | |||
797 | |||
798 | |||
799 | /*! | ||
800 | Returns the color currently selected in the dialog. | ||
801 | |||
802 | \sa setColor() | ||
803 | */ | ||
804 | |||
805 | QColor OColorDialog::color() const | ||
806 | { | ||
807 | return QColor(d->currentColor()); | ||
808 | } | ||
809 | |||
810 | |||
811 | /*! Destructs the dialog and frees any memory it allocated. | ||
812 | |||
813 | */ | ||
814 | |||
815 | OColorDialog::~OColorDialog() | ||
816 | { | ||
817 | //d inherits QObject, so it is deleted by Qt. | ||
818 | } | ||
819 | |||
820 | |||
821 | /*! | ||
822 | Sets the color shown in the dialog to \a c. | ||
823 | |||
824 | \sa color() | ||
825 | */ | ||
826 | |||
827 | void OColorDialog::setColor( const QColor& c ) | ||
828 | { | ||
829 | d->setCurrentColor( c.rgb() ); | ||
830 | } | ||
831 | |||
832 | |||
833 | |||
834 | |||
835 | /*! | ||
836 | Sets the initial alpha channel value to \a a, and show the alpha channel | ||
837 | entry box. | ||
838 | */ | ||
839 | |||
840 | void OColorDialog::setSelectedAlpha( int a ) | ||
841 | { | ||
842 | d->showAlpha( TRUE ); | ||
843 | d->setCurrentAlpha( a ); | ||
844 | } | ||
845 | |||
846 | |||
847 | /*! | ||
848 | Returns the value selected for the alpha channel. | ||
849 | */ | ||
850 | |||
851 | int OColorDialog::selectedAlpha() const | ||
852 | { | ||
853 | return d->currentAlpha(); | ||
854 | } | ||
855 | |||
856 | #include "colordialog.moc" | ||