libopie API Documentation

colordialog.cpp

00001 /****************************************************************************
00002 ** $Id: colordialog.cpp,v 1.4 2003/04/16 10:59:24 zecke Exp $
00003 **
00004 ** Implementation of OColorDialog class
00005 **
00006 ** Created : 990222
00007 **
00008 ** Copyright (C) 1999-2000 Trolltech AS.  All rights reserved.
00009 **
00010 ** This file is part of the dialogs module of the Qt GUI Toolkit.
00011 **
00012 ** This file may be distributed under the terms of the Q Public License
00013 ** as defined by Trolltech AS of Norway and appearing in the file
00014 ** LICENSE.QPL included in the packaging of this file.
00015 **
00016 ** This file may be distributed and/or modified under the terms of the
00017 ** GNU General Public License version 2 as published by the Free Software
00018 ** Foundation and appearing in the file LICENSE.GPL included in the
00019 ** packaging of this file.
00020 **
00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00022 ** licenses may use this file in accordance with the Qt Commercial License
00023 ** Agreement provided with the Software.
00024 **
00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00027 **
00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00029 **   information about Qt Commercial License Agreements.
00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00032 **
00033 ** Contact info@trolltech.com if any conditions of this licensing are
00034 ** not clear to you.
00035 **
00036 **********************************************************************/
00037 
00038 #include "colordialog.h"
00039 
00040 #include "qpainter.h"
00041 #include "qlayout.h"
00042 #include "qlabel.h"
00043 #include "qpushbutton.h"
00044 #include "qlineedit.h"
00045 #include "qimage.h"
00046 #include "qpixmap.h"
00047 #include "qdrawutil.h"
00048 #include "qvalidator.h"
00049 #include "qdragobject.h"
00050 #include "qapplication.h"
00051 #include "qdragobject.h"
00052 
00053 static inline void rgb2hsv( QRgb rgb, int&h, int&s, int&v )
00054 {
00055     QColor c;
00056     c.setRgb( rgb );
00057     c.getHsv(h,s,v);
00058 }
00059 
00060 /*
00061  * avoid clashes with the original Qt
00062  */
00063 namespace {
00064 
00065 class QColorPicker : public QFrame
00066 {
00067     Q_OBJECT
00068 public:
00069     QColorPicker(QWidget* parent=0, const char* name=0);
00070     ~QColorPicker();
00071 
00072 public slots:
00073     void setCol( int h, int s );
00074 
00075 signals:
00076     void newCol( int h, int s );
00077 
00078 protected:
00079     QSize sizeHint() const;
00080     QSizePolicy sizePolicy() const;
00081     void drawContents(QPainter* p);
00082     void mouseMoveEvent( QMouseEvent * );
00083     void mousePressEvent( QMouseEvent * );
00084 
00085 private:
00086     int hue;
00087     int sat;
00088 
00089     QPoint colPt();
00090     int huePt( const QPoint &pt );
00091     int satPt( const QPoint &pt );
00092     void setCol( const QPoint &pt );
00093 
00094     QPixmap *pix;
00095 };
00096 
00097 static int pWidth = 200;
00098 static int pHeight = 200;
00099 
00100 class QColorLuminancePicker : public QWidget
00101 {
00102     Q_OBJECT
00103 public:
00104     QColorLuminancePicker(QWidget* parent=0, const char* name=0);
00105     ~QColorLuminancePicker();
00106 
00107 public slots:
00108     void setCol( int h, int s, int v );
00109     void setCol( int h, int s );
00110 
00111 signals:
00112     void newHsv( int h, int s, int v );
00113 
00114 protected:
00115 //    QSize sizeHint() const;
00116 //    QSizePolicy sizePolicy() const;
00117     void paintEvent( QPaintEvent*);
00118     void mouseMoveEvent( QMouseEvent * );
00119     void mousePressEvent( QMouseEvent * );
00120 
00121 private:
00122     enum { foff = 3, coff = 4 }; //frame and contents offset
00123     int val;
00124     int hue;
00125     int sat;
00126 
00127     int y2val( int y );
00128     int val2y( int val );
00129     void setVal( int v );
00130 
00131     QPixmap *pix;
00132 };
00133 
00134 
00135 int QColorLuminancePicker::y2val( int y )
00136 {
00137     int d = height() - 2*coff - 1;
00138     return 255 - (y - coff)*255/d;
00139 }
00140 
00141 int QColorLuminancePicker::val2y( int v )
00142 {
00143     int d = height() - 2*coff - 1;
00144     return coff + (255-v)*d/255;
00145 }
00146 
00147 QColorLuminancePicker::QColorLuminancePicker(QWidget* parent,
00148                           const char* name)
00149     :QWidget( parent, name )
00150 {
00151     hue = 100; val = 100; sat = 100;
00152     pix = 0;
00153     //    setBackgroundMode( NoBackground );
00154 }
00155 
00156 QColorLuminancePicker::~QColorLuminancePicker()
00157 {
00158     delete pix;
00159 }
00160 
00161 void QColorLuminancePicker::mouseMoveEvent( QMouseEvent *m )
00162 {
00163     setVal( y2val(m->y()) );
00164 }
00165 void QColorLuminancePicker::mousePressEvent( QMouseEvent *m )
00166 {
00167     setVal( y2val(m->y()) );
00168 }
00169 
00170 void QColorLuminancePicker::setVal( int v )
00171 {
00172     if ( val == v )
00173     return;
00174     val = QMAX( 0, QMIN(v,255));
00175     delete pix; pix=0;
00176     repaint( FALSE ); //###
00177     emit newHsv( hue, sat, val );
00178 }
00179 
00180 //receives from a hue,sat chooser and relays.
00181 void QColorLuminancePicker::setCol( int h, int s )
00182 {
00183     setCol( h, s, val );
00184     emit newHsv( h, s, val );
00185 }
00186 
00187 void QColorLuminancePicker::paintEvent( QPaintEvent * )
00188 {
00189     int w = width() - 5;
00190 
00191     QRect r( 0, foff, w, height() - 2*foff );
00192     int wi = r.width() - 2;
00193     int hi = r.height() - 2;
00194     if ( !pix || pix->height() != hi || pix->width() != wi ) {
00195     delete pix;
00196     QImage img( wi, hi, 32 );
00197     int y;
00198     for ( y = 0; y < hi; y++ ) {
00199         QColor c( hue, sat, y2val(y+coff), QColor::Hsv );
00200         QRgb r = c.rgb();
00201         int x;
00202         for ( x = 0; x < wi; x++ )
00203         img.setPixel( x, y, r );
00204     }
00205     pix = new QPixmap;
00206     pix->convertFromImage(img);
00207     }
00208     QPainter p(this);
00209     p.drawPixmap( 1, coff, *pix );
00210     QColorGroup g = colorGroup();
00211     qDrawShadePanel( &p, r, g, TRUE );
00212     p.setPen( g.foreground() );
00213     p.setBrush( g.foreground() );
00214     QPointArray a;
00215     int y = val2y(val);
00216     a.setPoints( 3, w, y, w+5, y+5, w+5, y-5 );
00217     erase( w, 0, 5, height() );
00218     p.drawPolygon( a );
00219 }
00220 
00221 void QColorLuminancePicker::setCol( int h, int s , int v )
00222 {
00223     val = v;
00224     hue = h;
00225     sat = s;
00226     delete pix; pix=0;
00227     repaint( FALSE );//####
00228 }
00229 
00230 QPoint QColorPicker::colPt()
00231 { return QPoint( (360-hue)*(pWidth-1)/360, (255-sat)*(pHeight-1)/255 ); }
00232 int QColorPicker::huePt( const QPoint &pt )
00233 { return 360 - pt.x()*360/(pWidth-1); }
00234 int QColorPicker::satPt( const QPoint &pt )
00235 { return 255 - pt.y()*255/(pHeight-1) ; }
00236 void QColorPicker::setCol( const QPoint &pt )
00237 { setCol( huePt(pt), satPt(pt) ); }
00238 
00239 QColorPicker::QColorPicker(QWidget* parent, const char* name )
00240     : QFrame( parent, name )
00241 {
00242     hue = 0; sat = 0;
00243     setCol( 150, 255 );
00244 
00245     QImage img( pWidth, pHeight, 32 );
00246     int x,y;
00247     for ( y = 0; y < pHeight; y++ )
00248     for ( x = 0; x < pWidth; x++ ) {
00249         QPoint p( x, y );
00250         img.setPixel( x, y, QColor(huePt(p), satPt(p),
00251                        200, QColor::Hsv).rgb() );
00252     }
00253     pix = new QPixmap;
00254     pix->convertFromImage(img);
00255     setBackgroundMode( NoBackground );
00256 }
00257 
00258 QColorPicker::~QColorPicker()
00259 {
00260     delete pix;
00261 }
00262 
00263 QSize QColorPicker::sizeHint() const
00264 {
00265     return QSize( pWidth + 2*frameWidth(), pHeight + 2*frameWidth() );
00266 }
00267 
00268 QSizePolicy QColorPicker::sizePolicy() const
00269 {
00270     return QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00271 }
00272 
00273 void QColorPicker::setCol( int h, int s )
00274 {
00275     int nhue = QMIN( QMAX(0,h), 360 );
00276     int nsat = QMIN( QMAX(0,s), 255);
00277     if ( nhue == hue && nsat == sat )
00278     return;
00279     QRect r( colPt(), QSize(20,20) );
00280     hue = nhue; sat = nsat;
00281     r = r.unite( QRect( colPt(), QSize(20,20) ) );
00282     r.moveBy( contentsRect().x()-9, contentsRect().y()-9 );
00283     //    update( r );
00284     repaint( r, FALSE );
00285 }
00286 
00287 void QColorPicker::mouseMoveEvent( QMouseEvent *m )
00288 {
00289     QPoint p = m->pos() - contentsRect().topLeft();
00290     setCol( p );
00291     emit newCol( hue, sat );
00292 }
00293 
00294 void QColorPicker::mousePressEvent( QMouseEvent *m )
00295 {
00296     QPoint p = m->pos() - contentsRect().topLeft();
00297     setCol( p );
00298     emit newCol( hue, sat );
00299 }
00300 
00301 void QColorPicker::drawContents(QPainter* p)
00302 {
00303     QRect r = contentsRect();
00304 
00305     p->drawPixmap( r.topLeft(), *pix );
00306     QPoint pt = colPt() + r.topLeft();
00307     p->setPen( QPen(black) );
00308 
00309     p->fillRect( pt.x()-9, pt.y(), 20, 2, black );
00310     p->fillRect( pt.x(), pt.y()-9, 2, 20, black );
00311 
00312 }
00313 
00314 class QColorShowLabel;
00315 
00316 
00317 
00318 class QColIntValidator: public QIntValidator
00319 {
00320 public:
00321     QColIntValidator( int bottom, int top,
00322            QWidget * parent, const char *name = 0 )
00323     :QIntValidator( bottom, top, parent, name ) {}
00324 
00325     QValidator::State validate( QString &, int & ) const;
00326 };
00327 
00328 QValidator::State QColIntValidator::validate( QString &s, int &pos ) const
00329 {
00330     State state = QIntValidator::validate(s,pos);
00331     if ( state == Valid ) {
00332     long int val = s.toLong();
00333     // This is not a general solution, assumes that top() > 0 and
00334     // bottom >= 0
00335     if ( val < 0 ) {
00336         s = "0";
00337         pos = 1;
00338     } else if ( val > top() ) {
00339         s.setNum( top() );
00340         pos = s.length();
00341     }
00342     }
00343     return state;
00344 }
00345 
00346 
00347 
00348 class QColNumLineEdit : public QLineEdit
00349 {
00350 public:
00351     QColNumLineEdit( QWidget *parent, const char* name = 0 )
00352     : QLineEdit( parent, name ) { setMaxLength( 3 );}
00353     QSize sizeHint() const {
00354     return QSize( 30, //#####
00355              QLineEdit::sizeHint().height() ); }
00356     void setNum( int i ) {
00357     QString s;
00358     s.setNum(i);
00359     bool block = signalsBlocked();
00360     blockSignals(TRUE);
00361     setText( s );
00362     blockSignals(block);
00363     }
00364     int val() const { return text().toInt(); }
00365 };
00366 
00367 
00368 class QColorShower : public QWidget
00369 {
00370     Q_OBJECT
00371 public:
00372     QColorShower( QWidget *parent, const char *name = 0 );
00373 
00374     //things that don't emit signals
00375     void setHsv( int h, int s, int v );
00376 
00377     int currentAlpha() const { return alphaEd->val(); }
00378     void setCurrentAlpha( int a ) { alphaEd->setNum( a ); }
00379     void showAlpha( bool b );
00380 
00381 
00382     QRgb currentColor() const { return curCol; }
00383 
00384 public slots:
00385     void setRgb( QRgb rgb );
00386 
00387 signals:
00388     void newCol( QRgb rgb );
00389 private slots:
00390     void rgbEd();
00391     void hsvEd();
00392 private:
00393     void showCurrentColor();
00394     int hue, sat, val;
00395     QRgb curCol;
00396     QColNumLineEdit *hEd;
00397     QColNumLineEdit *sEd;
00398     QColNumLineEdit *vEd;
00399     QColNumLineEdit *rEd;
00400     QColNumLineEdit *gEd;
00401     QColNumLineEdit *bEd;
00402     QColNumLineEdit *alphaEd;
00403     QLabel *alphaLab;
00404     QColorShowLabel *lab;
00405     bool rgbOriginal;
00406 };
00407 
00408 class QColorShowLabel : public QFrame
00409 {
00410     Q_OBJECT
00411 
00412 public:
00413     QColorShowLabel( QWidget *parent ) :QFrame( parent ) {
00414     setFrameStyle( QFrame::Panel|QFrame::Sunken );
00415     setBackgroundMode( PaletteBackground );
00416     setAcceptDrops( TRUE );
00417     mousePressed = FALSE;
00418     }
00419     void setColor( QColor c ) { col = c; }
00420 
00421 signals:
00422     void colorDropped( QRgb );
00423 
00424 protected:
00425     void drawContents( QPainter *p );
00426     void mousePressEvent( QMouseEvent *e );
00427     void mouseReleaseEvent( QMouseEvent *e );
00428 
00429 private:
00430     QColor col;
00431     bool mousePressed;
00432     QPoint pressPos;
00433 
00434 };
00435 
00436 void QColorShowLabel::drawContents( QPainter *p )
00437 {
00438     p->fillRect( contentsRect(), col );
00439 }
00440 
00441 void QColorShower::showAlpha( bool b )
00442 {
00443     if ( b ) {
00444     alphaLab->show();
00445     alphaEd->show();
00446     } else {
00447     alphaLab->hide();
00448     alphaEd->hide();
00449     }
00450 }
00451 
00452 void QColorShowLabel::mousePressEvent( QMouseEvent *e )
00453 {
00454     mousePressed = TRUE;
00455     pressPos = e->pos();
00456 }
00457 
00458 void QColorShowLabel::mouseReleaseEvent( QMouseEvent * )
00459 {
00460     if ( !mousePressed )
00461     return;
00462     mousePressed = FALSE;
00463 }
00464 
00465 QColorShower::QColorShower( QWidget *parent, const char *name )
00466     :QWidget( parent, name)
00467 {
00468     curCol = qRgb( -1, -1, -1 );
00469     QColIntValidator *val256 = new QColIntValidator( 0, 255, this );
00470     QColIntValidator *val360 = new QColIntValidator( 0, 360, this );
00471 
00472     QGridLayout *gl = new QGridLayout( this, 1, 1, 2 );
00473     gl->setMargin( 0 );
00474     lab = new QColorShowLabel( this );
00475     lab->setMinimumWidth( 60 ); //###
00476     gl->addMultiCellWidget(lab, 0,-1,0,0);
00477     connect( lab, SIGNAL( colorDropped( QRgb ) ),
00478          this, SIGNAL( newCol( QRgb ) ) );
00479     connect( lab, SIGNAL( colorDropped( QRgb ) ),
00480          this, SLOT( setRgb( QRgb ) ) );
00481 
00482     hEd = new QColNumLineEdit( this );
00483     hEd->setValidator( val360 );
00484     QLabel *l = new QLabel( hEd, OColorDialog::tr("Hue:"), this );
00485     l->setAlignment( AlignRight|AlignVCenter );
00486     gl->addWidget( l, 0, 1 );
00487     gl->addWidget( hEd, 0, 2 );
00488 
00489     sEd = new QColNumLineEdit( this );
00490     sEd->setValidator( val256 );
00491     l = new QLabel( sEd, OColorDialog::tr("Sat:"), this );
00492     l->setAlignment( AlignRight|AlignVCenter );
00493     gl->addWidget( l, 1, 1 );
00494     gl->addWidget( sEd, 1, 2 );
00495 
00496     vEd = new QColNumLineEdit( this );
00497     vEd->setValidator( val256 );
00498     l = new QLabel( vEd, OColorDialog::tr("Val:"), this );
00499     l->setAlignment( AlignRight|AlignVCenter );
00500     gl->addWidget( l, 2, 1 );
00501     gl->addWidget( vEd, 2, 2 );
00502 
00503     rEd = new QColNumLineEdit( this );
00504     rEd->setValidator( val256 );
00505     l = new QLabel( rEd, OColorDialog::tr("Red:"), this );
00506     l->setAlignment( AlignRight|AlignVCenter );
00507     gl->addWidget( l, 0, 3 );
00508     gl->addWidget( rEd, 0, 4 );
00509 
00510     gEd = new QColNumLineEdit( this );
00511     gEd->setValidator( val256 );
00512     l = new QLabel( gEd, OColorDialog::tr("Green:"), this );
00513     l->setAlignment( AlignRight|AlignVCenter );
00514     gl->addWidget( l, 1, 3 );
00515     gl->addWidget( gEd, 1, 4 );
00516 
00517     bEd = new QColNumLineEdit( this );
00518     bEd->setValidator( val256 );
00519     l = new QLabel( bEd, OColorDialog::tr("Blue:"), this );
00520     l->setAlignment( AlignRight|AlignVCenter );
00521     gl->addWidget( l, 2, 3 );
00522     gl->addWidget( bEd, 2, 4 );
00523 
00524     alphaEd = new QColNumLineEdit( this );
00525     alphaEd->setValidator( val256 );
00526     alphaLab = new QLabel( alphaEd, OColorDialog::tr("Alpha channel:"), this );
00527     alphaLab->setAlignment( AlignRight|AlignVCenter );
00528     gl->addMultiCellWidget( alphaLab, 3, 3, 1, 3 );
00529     gl->addWidget( alphaEd, 3, 4 );
00530     alphaEd->hide();
00531     alphaLab->hide();
00532 
00533     connect( hEd, SIGNAL(textChanged(const QString&)), this, SLOT(hsvEd()) );
00534     connect( sEd, SIGNAL(textChanged(const QString&)), this, SLOT(hsvEd()) );
00535     connect( vEd, SIGNAL(textChanged(const QString&)), this, SLOT(hsvEd()) );
00536 
00537     connect( rEd, SIGNAL(textChanged(const QString&)), this, SLOT(rgbEd()) );
00538     connect( gEd, SIGNAL(textChanged(const QString&)), this, SLOT(rgbEd()) );
00539     connect( bEd, SIGNAL(textChanged(const QString&)), this, SLOT(rgbEd()) );
00540 }
00541 
00542 void QColorShower::showCurrentColor()
00543 {
00544     lab->setColor( currentColor() );
00545     lab->repaint(FALSE); //###
00546 }
00547 
00548 void QColorShower::rgbEd()
00549 {
00550     rgbOriginal = TRUE;
00551     curCol = qRgb( rEd->val(), gEd->val(), bEd->val() );
00552     rgb2hsv(currentColor(), hue, sat, val );
00553 
00554     hEd->setNum( hue );
00555     sEd->setNum( sat );
00556     vEd->setNum( val );
00557 
00558     showCurrentColor();
00559     emit newCol( currentColor() );
00560 }
00561 
00562 void QColorShower::hsvEd()
00563 {
00564     rgbOriginal = FALSE;
00565     hue = hEd->val();
00566     sat = sEd->val();
00567     val = vEd->val();
00568 
00569     curCol = QColor( hue, sat, val, QColor::Hsv ).rgb();
00570 
00571     rEd->setNum( qRed(currentColor()) );
00572     gEd->setNum( qGreen(currentColor()) );
00573     bEd->setNum( qBlue(currentColor()) );
00574 
00575     showCurrentColor();
00576     emit newCol( currentColor() );
00577 }
00578 
00579 void QColorShower::setRgb( QRgb rgb )
00580 {
00581     rgbOriginal = TRUE;
00582     curCol = rgb;
00583 
00584     rgb2hsv( currentColor(), hue, sat, val );
00585 
00586     hEd->setNum( hue );
00587     sEd->setNum( sat );
00588     vEd->setNum( val );
00589 
00590     rEd->setNum( qRed(currentColor()) );
00591     gEd->setNum( qGreen(currentColor()) );
00592     bEd->setNum( qBlue(currentColor()) );
00593 
00594     showCurrentColor();
00595 }
00596 
00597 void QColorShower::setHsv( int h, int s, int v )
00598 {
00599     rgbOriginal = FALSE;
00600     hue = h; val = v; sat = s; //Range check###
00601     curCol = QColor( hue, sat, val, QColor::Hsv ).rgb();
00602 
00603     hEd->setNum( hue );
00604     sEd->setNum( sat );
00605     vEd->setNum( val );
00606 
00607     rEd->setNum( qRed(currentColor()) );
00608     gEd->setNum( qGreen(currentColor()) );
00609     bEd->setNum( qBlue(currentColor()) );
00610 
00611 
00612     showCurrentColor();
00613 }
00614 
00615 }
00616 
00617 class OColorDialogPrivate : public QObject
00618 {
00619 Q_OBJECT
00620 public:
00621     OColorDialogPrivate( OColorDialog *p );
00622     QRgb currentColor() const { return cs->currentColor(); }
00623     void setCurrentColor( const QRgb& rgb );
00624 
00625     int currentAlpha() const { return cs->currentAlpha(); }
00626     void setCurrentAlpha( int a ) { cs->setCurrentAlpha( a ); }
00627     void showAlpha( bool b ) { cs->showAlpha( b ); }
00628 
00629 private slots:
00630     void newHsv( int h, int s, int v );
00631     void newColorTypedIn( QRgb rgb );
00632 private:
00633     QColorPicker *cp;
00634     QColorLuminancePicker *lp;
00635     QColorShower *cs;
00636 };
00637 
00638 //sets all widgets to display h,s,v
00639 void OColorDialogPrivate::newHsv( int h, int s, int v )
00640 {
00641     cs->setHsv( h, s, v );
00642     cp->setCol( h, s );
00643     lp->setCol( h, s, v );
00644 }
00645 
00646 //sets all widgets to display rgb
00647 void OColorDialogPrivate::setCurrentColor( const QRgb& rgb )
00648 {
00649     cs->setRgb( rgb );
00650     newColorTypedIn( rgb );
00651 }
00652 
00653 //sets all widgets exept cs to display rgb
00654 void OColorDialogPrivate::newColorTypedIn( QRgb rgb )
00655 {
00656     int h, s, v;
00657     rgb2hsv(rgb, h, s, v );
00658     cp->setCol( h, s );
00659     lp->setCol( h, s, v);
00660 }
00661 
00662 OColorDialogPrivate::OColorDialogPrivate( OColorDialog *dialog ) :
00663     QObject(dialog)
00664 {
00665     int border = 2;
00666     QVBoxLayout *topLay = new QVBoxLayout( dialog, border, 2 );
00667 
00668     QHBoxLayout *pickLay = new QHBoxLayout( topLay );
00669 
00670 
00671     cp = new QColorPicker( dialog );
00672     cp->setFrameStyle( QFrame::Panel + QFrame::Sunken );
00673     pickLay->addWidget( cp );
00674 
00675     pickLay->addStretch();
00676 
00677     lp = new QColorLuminancePicker( dialog );
00678     lp->setFixedWidth( 20 ); //###
00679     pickLay->addWidget( lp );
00680 
00681     connect( cp, SIGNAL(newCol(int,int)), lp, SLOT(setCol(int,int)) );
00682     connect( lp, SIGNAL(newHsv(int,int,int)), this, SLOT(newHsv(int,int,int)) );
00683 
00684     topLay->addStretch();
00685 
00686     cs = new QColorShower( dialog );
00687     connect( cs, SIGNAL(newCol(QRgb)), this, SLOT(newColorTypedIn(QRgb)));
00688     topLay->addWidget( cs );
00689 
00690 }
00691 
00692 
00693 // BEING REVISED: jo
00722 OColorDialog::OColorDialog(QWidget* parent, const char* name, bool modal) :
00723     QDialog(parent, name, modal )
00724 {
00725     d = new OColorDialogPrivate( this );
00726 }
00727 
00728 
00736 QColor OColorDialog::getColor( const QColor& initial, QWidget *parent,
00737                    const char *name )
00738 {
00739     int allocContext = QColor::enterAllocContext();
00740     OColorDialog *dlg = new OColorDialog( parent, name, TRUE );  //modal
00741     if ( parent && parent->icon() && !parent->icon()->isNull() )
00742     dlg->setIcon( *parent->icon() );
00743     else if ( qApp->mainWidget() && qApp->mainWidget()->icon() && !qApp->mainWidget()->icon()->isNull() )
00744     dlg->setIcon( *qApp->mainWidget()->icon() );
00745 
00746     dlg->setCaption( OColorDialog::tr( "Select color" ) );
00747     dlg->setColor( initial );
00748     dlg->showMaximized();
00749     int resultCode = dlg->exec();
00750     QColor::leaveAllocContext();
00751     QColor result;
00752     if ( resultCode == QDialog::Accepted ) {
00753     result = dlg->color();
00754     } else {
00755         result = initial;
00756     }
00757     QColor::destroyAllocContext(allocContext);
00758     delete dlg;
00759     return result;
00760 }
00761 
00762 
00773 QRgb OColorDialog::getRgba( const QRgb& initial, bool *ok,
00774                 QWidget *parent, const char* name )
00775 {
00776     int allocContext = QColor::enterAllocContext();
00777     OColorDialog *dlg = new OColorDialog( parent, name, TRUE );  //modal
00778     dlg->setColor( initial );
00779     dlg->setSelectedAlpha( qAlpha(initial) );
00780     dlg->showMaximized();
00781     int resultCode = dlg->exec();
00782     QColor::leaveAllocContext();
00783     QRgb result = initial;
00784     if ( resultCode == QDialog::Accepted ) {
00785     QRgb c = dlg->color().rgb();
00786     int alpha = dlg->selectedAlpha();
00787     result = qRgba( qRed(c), qGreen(c), qBlue(c), alpha );
00788     }
00789     if ( ok )
00790     *ok = resultCode == QDialog::Accepted;
00791 
00792     QColor::destroyAllocContext(allocContext);
00793     delete dlg;
00794     return result;
00795 }
00796 
00797 
00798 
00799 
00800 
00807 QColor OColorDialog::color() const
00808 {
00809     return QColor(d->currentColor());
00810 }
00811 
00812 
00817 OColorDialog::~OColorDialog()
00818 {
00819     //d inherits QObject, so it is deleted by Qt.
00820 }
00821 
00822 
00829 void OColorDialog::setColor( const QColor& c )
00830 {
00831     d->setCurrentColor( c.rgb() );
00832 }
00833 
00834 
00835 
00836 
00842 void OColorDialog::setSelectedAlpha( int a )
00843 {
00844     d->showAlpha( TRUE );
00845     d->setCurrentAlpha( a );
00846 }
00847 
00848 
00853 int OColorDialog::selectedAlpha() const
00854 {
00855     return d->currentAlpha();
00856 }
00857 
00858 #include "colordialog.moc"
KDE Logo
This file is part of the documentation for OPIE Version 1.1.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:42 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001