00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "qcolordialog.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 "qapplication.h"
00050
00052
00053 #include "qobjectdict.h"
00054
00055 #ifndef QT_NO_DRAGANDDROP
00056 #include "qdragobject.h"
00057 #endif
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #include "qtableview.h"
00072
00073
00074 struct QWellArrayData;
00075
00076 class QWellArray : public QTableView
00077 {
00078 Q_OBJECT
00079 Q_PROPERTY( int numCols READ numCols )
00080 Q_PROPERTY( int numRows READ numRows )
00081 Q_PROPERTY( int selectedColumn READ selectedColumn )
00082 Q_PROPERTY( int selectedRow READ selectedRow )
00083
00084 public:
00085 QWellArray( QWidget *parent=0, const char *name=0, bool popup = FALSE );
00086
00087 ~QWellArray() {}
00088 QString cellContent( int row, int col ) const;
00089
00090
00091
00092 int numCols() { return nCols; }
00093 int numRows() { return nRows; }
00094
00095 int numCols() const { return nCols; }
00096 int numRows() const { return nRows; }
00097
00098
00099 int selectedColumn() { return selCol; }
00100 int selectedRow() { return selRow; }
00101
00102 int selectedColumn() const { return selCol; }
00103 int selectedRow() const { return selRow; }
00104
00105 virtual void setSelected( int row, int col );
00106
00107 void setCellSize( int w, int h ) { setCellWidth(w);setCellHeight( h ); }
00108
00109 QSize sizeHint() const;
00110
00111 virtual void setDimension( int rows, int cols );
00112 virtual void setCellBrush( int row, int col, const QBrush & );
00113 QBrush cellBrush( int row, int col );
00114
00115 signals:
00116 void selected( int row, int col );
00117
00118 protected:
00119 virtual void setCurrent( int row, int col );
00120
00121 virtual void drawContents( QPainter *, int row, int col, const QRect& );
00122 void drawContents( QPainter * );
00123
00124 void paintCell( QPainter*, int row, int col );
00125 void mousePressEvent( QMouseEvent* );
00126 void mouseReleaseEvent( QMouseEvent* );
00127 void mouseMoveEvent( QMouseEvent* );
00128 void keyPressEvent( QKeyEvent* );
00129 void focusInEvent( QFocusEvent* );
00130 void focusOutEvent( QFocusEvent* );
00131
00132 private:
00133 int curRow;
00134 int curCol;
00135 int selRow;
00136 int selCol;
00137 int nCols;
00138 int nRows;
00139 bool smallStyle;
00140 QWellArrayData *d;
00141
00142 private:
00143 #if defined(Q_DISABLE_COPY)
00144 QWellArray( const QWellArray & );
00145 QWellArray& operator=( const QWellArray & );
00146 #endif
00147 };
00148
00149
00150
00151
00152
00153
00154
00155 struct QWellArrayData {
00156 QBrush *brush;
00157 };
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 QWellArray::QWellArray( QWidget *parent, const char * name, bool popup )
00170 : QTableView( parent, name,
00171 popup ? (WStyle_Customize|WStyle_Tool|WStyle_NoBorder) : 0 )
00172 {
00173 d = 0;
00174 setFocusPolicy( StrongFocus );
00175 setBackgroundMode( PaletteButton );
00176 nCols = 7;
00177 nRows = 7;
00178 int w = 24;
00179 int h = 21;
00180 smallStyle = popup;
00181
00182 if ( popup ) {
00183 w = h = 18;
00184 if ( style() == WindowsStyle )
00185 setFrameStyle( QFrame::WinPanel | QFrame::Raised );
00186 else
00187 setFrameStyle( QFrame::Panel | QFrame::Raised );
00188 setMargin( 1 );
00189 setLineWidth( 2 );
00190 }
00191 setNumCols( nCols );
00192 setNumRows( nRows );
00193 setCellWidth( w );
00194 setCellHeight( h );
00195 curCol = 0;
00196 curRow = 0;
00197 selCol = -1;
00198 selRow = -1;
00199
00200 if ( smallStyle )
00201 setMouseTracking( TRUE );
00202 setOffset( 5 , 10 );
00203
00204 resize( sizeHint() );
00205
00206 }
00207
00208
00209 QSize QWellArray::sizeHint() const
00210 {
00211 constPolish();
00212 int f = frameWidth() * 2;
00213 int w = nCols * cellWidth() + f;
00214 int h = nRows * cellHeight() + f;
00215 return QSize( w, h );
00216 }
00217
00218
00219 void QWellArray::paintCell( QPainter* p, int row, int col )
00220 {
00221 int w = cellWidth( col );
00222 int h = cellHeight( row );
00223 int b = 1;
00224
00225 if ( !smallStyle )
00226 b = 3;
00227
00228 const QColorGroup & g = colorGroup();
00229 p->setPen( QPen( black, 0, SolidLine ) );
00230 if ( !smallStyle && row ==selRow && col == selCol &&
00231 style() != MotifStyle ) {
00232 int n = 2;
00233 p->drawRect( n, n, w-2*n, h-2*n );
00234 }
00235
00236
00237 if ( style() == WindowsStyle ) {
00238 qDrawWinPanel( p, b, b , w - 2*b, h - 2*b,
00239 g, TRUE );
00240 b += 2;
00241 } else {
00242 if ( smallStyle ) {
00243 qDrawShadePanel( p, b, b , w - 2*b, h - 2*b,
00244 g, TRUE, 2 );
00245 b += 2;
00246 } else {
00247 int t = ( row == selRow && col == selCol ) ? 2 : 0;
00248 b -= t;
00249 qDrawShadePanel( p, b, b , w - 2*b, h - 2*b,
00250 g, TRUE, 2 );
00251 b += 2 + t;
00252 }
00253 }
00254
00255
00256 if ( (row == curRow) && (col == curCol) ) {
00257 if ( smallStyle ) {
00258 p->setPen ( white );
00259 p->drawRect( 1, 1, w-2, h-2 );
00260 p->setPen ( black );
00261 p->drawRect( 0, 0, w, h );
00262 p->drawRect( 2, 2, w-4, h-4 );
00263 b = 3;
00264 } else if ( hasFocus() ) {
00265 style().drawFocusRect(p, QRect(0,0,w,h), g );
00266 }
00267 }
00268 drawContents( p, row, col, QRect(b, b, w - 2*b, h - 2*b) );
00269 }
00270
00274 void QWellArray::drawContents( QPainter *p )
00275 {
00276 QTableView::drawContents(p);
00277 }
00278
00282 void QWellArray::drawContents( QPainter *p, int row, int col, const QRect &r )
00283 {
00284
00285 if ( d ) {
00286 p->fillRect( r, d->brush[row*nCols+col] );
00287 } else {
00288 p->fillRect( r, white );
00289 p->setPen( black );
00290 p->drawLine( r.topLeft(), r.bottomRight() );
00291 p->drawLine( r.topRight(), r.bottomLeft() );
00292 }
00293 }
00294
00295
00296
00297
00298 void QWellArray::mousePressEvent( QMouseEvent* e )
00299 {
00300
00301
00302 QPoint pos = e->pos();
00303 setCurrent( findRow( pos.y() ), findCol( pos.x() ) );
00304 }
00305
00306
00307
00308 void QWellArray::mouseReleaseEvent( QMouseEvent* )
00309 {
00310
00311
00312 setSelected( curRow, curCol );
00313 }
00314
00315
00316
00317
00318 void QWellArray::mouseMoveEvent( QMouseEvent* e )
00319 {
00320
00321
00322 if ( smallStyle ) {
00323 QPoint pos = e->pos();
00324 setCurrent( findRow( pos.y() ), findCol( pos.x() ) );
00325 }
00326 }
00327
00328
00329
00330
00331
00332
00333 void QWellArray::setCurrent( int row, int col )
00334 {
00335
00336 if ( (curRow == row) && (curCol == col) )
00337 return;
00338
00339 if ( row < 0 || col < 0 )
00340 row = col = -1;
00341
00342 int oldRow = curRow;
00343 int oldCol = curCol;
00344
00345 curRow = row;
00346 curCol = col;
00347
00348 updateCell( oldRow, oldCol );
00349 updateCell( curRow, curCol );
00350 }
00351
00352
00360 void QWellArray::setSelected( int row, int col )
00361 {
00362 if ( (selRow == row) && (selCol == col) )
00363 return;
00364
00365 int oldRow = selRow;
00366 int oldCol = selCol;
00367
00368 if ( row < 0 || col < 0 )
00369 row = col = -1;
00370
00371 selCol = col;
00372 selRow = row;
00373
00374 updateCell( oldRow, oldCol );
00375 updateCell( selRow, selCol );
00376 if ( row >= 0 )
00377 emit selected( row, col );
00378
00379 if ( isVisible() && parentWidget() && parentWidget()->inherits("QPopupMenu") )
00380 parentWidget()->close();
00381
00382 }
00383
00384
00385
00388 void QWellArray::focusInEvent( QFocusEvent* )
00389 {
00390 updateCell( curRow, curCol );
00391 }
00392
00393
00400 void QWellArray::setDimension( int rows, int cols )
00401 {
00402 nRows = rows;
00403 nCols = cols;
00404 if ( d ) {
00405 if ( d->brush )
00406 delete[] d->brush;
00407 delete d;
00408 d = 0;
00409 }
00410 setNumCols( nCols );
00411 setNumRows( nRows );
00412 }
00413
00414 void QWellArray::setCellBrush( int row, int col, const QBrush &b )
00415 {
00416 if ( !d ) {
00417 d = new QWellArrayData;
00418 d->brush = new QBrush[nRows*nCols];
00419 }
00420 if ( row >= 0 && row < nRows && col >= 0 && col < nCols )
00421 d->brush[row*nCols+col] = b;
00422 #ifdef CHECK_RANGE
00423 else
00424 qWarning( "QWellArray::setCellBrush( %d, %d ) out of range", row, col );
00425 #endif
00426 }
00427
00428
00429
00435 QBrush QWellArray::cellBrush( int row, int col )
00436 {
00437 if ( d && row >= 0 && row < nRows && col >= 0 && col < nCols )
00438 return d->brush[row*nCols+col];
00439 return NoBrush;
00440 }
00441
00442
00443
00447 void QWellArray::focusOutEvent( QFocusEvent* )
00448 {
00449 updateCell( curRow, curCol );
00450 }
00451
00452
00453
00454 void QWellArray::keyPressEvent( QKeyEvent* e )
00455 {
00456 switch( e->key() ) {
00457 case Key_Left:
00458 if( curCol > 0 ) {
00459 setCurrent( curRow, curCol - 1);
00460 int edge = leftCell();
00461 if ( curCol < edge )
00462 setLeftCell( edge - 1 );
00463 }
00464 break;
00465 case Key_Right:
00466 if( curCol < numCols()-1 ) {
00467 setCurrent( curRow, curCol + 1);
00468 int edge = lastColVisible();
00469 if ( curCol >= edge )
00470 setLeftCell( leftCell() + 1 );
00471 }
00472 break;
00473 case Key_Up:
00474 if( curRow > 0 ) {
00475 setCurrent( curRow - 1, curCol);
00476 int edge = topCell();
00477 if ( curRow < edge )
00478 setTopCell( edge - 1 );
00479 } else if ( smallStyle )
00480 focusNextPrevChild( FALSE );
00481 break;
00482 case Key_Down:
00483 if( curRow < numRows()-1 ) {
00484 setCurrent( curRow + 1, curCol);
00485 int edge = lastRowVisible();
00486 if ( curRow >= edge )
00487 setTopCell( topCell() + 1 );
00488 } else if ( smallStyle )
00489 focusNextPrevChild( TRUE );
00490 break;
00491 case Key_Space:
00492 case Key_Return:
00493 case Key_Enter:
00494 setSelected( curRow, curCol );
00495 break;
00496 default:
00497 e->ignore();
00498 return;
00499 }
00500
00501 }
00502
00504
00505 static bool initrgb = FALSE;
00506 static QRgb stdrgb[6*8];
00507 static QRgb cusrgb[2*8];
00508
00509
00510 static void initRGB()
00511 {
00512 if ( initrgb )
00513 return;
00514 initrgb = TRUE;
00515 int i = 0;
00516 for ( int g = 0; g < 4; g++ )
00517 for ( int r = 0; r < 4; r++ )
00518 for ( int b = 0; b < 3; b++ )
00519 stdrgb[i++] = qRgb( r*255/3, g*255/3, b*255/2 );
00520
00521 for ( i = 0; i < 2*8; i++ )
00522 cusrgb[i] = qRgb(0xff,0xff,0xff);
00523 }
00524
00529 int QColorDialog::customCount()
00530 {
00531 return 2*8;
00532 }
00533
00537 QRgb QColorDialog::customColor( int i )
00538 {
00539 initRGB();
00540 if ( i < 0 || i >= customCount() ) {
00541 #ifdef CHECK_RANGE
00542 qWarning( "QColorDialog::customColor() index %d out of range", i );
00543 #endif
00544 i = 0;
00545 }
00546 return cusrgb[i];
00547 }
00548
00552 void QColorDialog::setCustomColor( int i, QRgb c )
00553 {
00554 initRGB();
00555 if ( i < 0 || i >= customCount() ) {
00556 #ifdef CHECK_RANGE
00557 qWarning( "QColorDialog::customColor() index %d out of range", i );
00558 #endif
00559 return;
00560 }
00561 cusrgb[i] = c;
00562 }
00563
00564 static inline void rgb2hsv( QRgb rgb, int&h, int&s, int&v )
00565 {
00566 QColor c;
00567 c.setRgb( rgb );
00568 c.getHsv(h,s,v);
00569 }
00570
00571 class QColorWell : public QWellArray
00572 {
00573 public:
00574 QColorWell( QWidget *parent, int r, int c, QRgb *vals )
00575 :QWellArray( parent, "" ), values( vals ), mousePressed( FALSE ), oldCurrent( -1, -1 )
00576 { setDimension(r,c); setWFlags( WResizeNoErase ); }
00577 QSizePolicy sizePolicy() const;
00578
00579 protected:
00580 void drawContents( QPainter *, int row, int col, const QRect& );
00581 void drawContents( QPainter *p ) { QWellArray::drawContents(p); }
00582 void mousePressEvent( QMouseEvent *e );
00583 void mouseMoveEvent( QMouseEvent *e );
00584 void mouseReleaseEvent( QMouseEvent *e );
00585 #ifndef QT_NO_DRAGANDDROP
00586 void dragEnterEvent( QDragEnterEvent *e );
00587 void dragLeaveEvent( QDragLeaveEvent *e );
00588 void dragMoveEvent( QDragMoveEvent *e );
00589 void dropEvent( QDropEvent *e );
00590 #endif
00591
00592 private:
00593 QRgb *values;
00594 bool mousePressed;
00595 QPoint pressPos;
00596 QPoint oldCurrent;
00597
00598 };
00599
00600 QSizePolicy QColorWell::sizePolicy() const
00601 {
00602 return QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
00603 }
00604
00605 void QColorWell::drawContents( QPainter *p, int row, int col, const QRect &r )
00606 {
00607 int i = row + col*numRows();
00608 p->fillRect( r, QColor( values[i] ) );
00609 }
00610
00611 void QColorWell::mousePressEvent( QMouseEvent *e )
00612 {
00613 oldCurrent = QPoint( selectedRow(), selectedColumn() );
00614 QWellArray::mousePressEvent( e );
00615 mousePressed = TRUE;
00616 pressPos = e->pos();
00617 }
00618
00619 void QColorWell::mouseMoveEvent( QMouseEvent *e )
00620 {
00621 QWellArray::mouseMoveEvent( e );
00622 #ifndef QT_NO_DRAGANDDROP
00623 if ( !mousePressed )
00624 return;
00625 if ( ( pressPos - e->pos() ).manhattanLength() > QApplication::startDragDistance() ) {
00626 setCurrent( oldCurrent.x(), oldCurrent.y() );
00627 int i = findRow( e->y() ) + findCol( e->x() ) * numRows();
00628 QColor col( values[ i ] );
00629 QColorDrag *drg = new QColorDrag( col, this );
00630 QPixmap pix( cellWidth(), cellHeight() );
00631 pix.fill( col );
00632 QPainter p( &pix );
00633 p.drawRect( 0, 0, pix.width(), pix.height() );
00634 p.end();
00635 drg->setPixmap( pix );
00636 mousePressed = FALSE;
00637 drg->dragCopy();
00638 }
00639 #endif
00640 }
00641
00642 #ifndef QT_NO_DRAGANDDROP
00643 void QColorWell::dragEnterEvent( QDragEnterEvent *e )
00644 {
00645 setFocus();
00646 if ( QColorDrag::canDecode( e ) )
00647 e->accept();
00648 else
00649 e->ignore();
00650 }
00651
00652 void QColorWell::dragLeaveEvent( QDragLeaveEvent * )
00653 {
00654 if ( hasFocus() )
00655 parentWidget()->setFocus();
00656 }
00657
00658 void QColorWell::dragMoveEvent( QDragMoveEvent *e )
00659 {
00660 if ( QColorDrag::canDecode( e ) ) {
00661 setCurrent( findRow( e->pos().y() ), findCol( e->pos().x() ) );
00662 e->accept();
00663 } else
00664 e->ignore();
00665 }
00666
00667 void QColorWell::dropEvent( QDropEvent *e )
00668 {
00669 if ( QColorDrag::canDecode( e ) ) {
00670 int i = findRow( e->pos().y() ) + findCol( e->pos().x() ) * numRows();
00671 QColor col;
00672 QColorDrag::decode( e, col );
00673 values[ i ] = col.rgb();
00674 repaint( FALSE );
00675 e->accept();
00676 } else {
00677 e->ignore();
00678 }
00679 }
00680
00681 #endif // QT_NO_DRAGANDDROP
00682
00683 void QColorWell::mouseReleaseEvent( QMouseEvent *e )
00684 {
00685 if ( !mousePressed )
00686 return;
00687 QWellArray::mouseReleaseEvent( e );
00688 mousePressed = FALSE;
00689 }
00690
00691 class QColorPicker : public QFrame
00692 {
00693 Q_OBJECT
00694 public:
00695 QColorPicker(QWidget* parent=0, const char* name=0);
00696 ~QColorPicker();
00697
00698 public slots:
00699 void setCol( int h, int s );
00700
00701 signals:
00702 void newCol( int h, int s );
00703
00704 protected:
00705 QSize sizeHint() const;
00706 QSizePolicy sizePolicy() const;
00707 void drawContents(QPainter* p);
00708 void mouseMoveEvent( QMouseEvent * );
00709 void mousePressEvent( QMouseEvent * );
00710
00711 private:
00712 int hue;
00713 int sat;
00714
00715 QPoint colPt();
00716 int huePt( const QPoint &pt );
00717 int satPt( const QPoint &pt );
00718 void setCol( const QPoint &pt );
00719
00720 QPixmap *pix;
00721 };
00722
00723 static int pWidth = 200;
00724 static int pHeight = 200;
00725
00726 class QColorLuminancePicker : public QWidget
00727 {
00728 Q_OBJECT
00729 public:
00730 QColorLuminancePicker(QWidget* parent=0, const char* name=0);
00731 ~QColorLuminancePicker();
00732
00733 public slots:
00734 void setCol( int h, int s, int v );
00735 void setCol( int h, int s );
00736
00737 signals:
00738 void newHsv( int h, int s, int v );
00739
00740 protected:
00741
00742
00743 void paintEvent( QPaintEvent*);
00744 void mouseMoveEvent( QMouseEvent * );
00745 void mousePressEvent( QMouseEvent * );
00746
00747 private:
00748 enum { foff = 3, coff = 4 };
00749 int val;
00750 int hue;
00751 int sat;
00752
00753 int y2val( int y );
00754 int val2y( int val );
00755 void setVal( int v );
00756
00757 QPixmap *pix;
00758 };
00759
00760
00761 int QColorLuminancePicker::y2val( int y )
00762 {
00763 int d = height() - 2*coff - 1;
00764 return 255 - (y - coff)*255/d;
00765 }
00766
00767 int QColorLuminancePicker::val2y( int v )
00768 {
00769 int d = height() - 2*coff - 1;
00770 return coff + (255-v)*d/255;
00771 }
00772
00773 QColorLuminancePicker::QColorLuminancePicker(QWidget* parent,
00774 const char* name)
00775 :QWidget( parent, name )
00776 {
00777 hue = 100; val = 100; sat = 100;
00778 pix = 0;
00779
00780 }
00781
00782 QColorLuminancePicker::~QColorLuminancePicker()
00783 {
00784 delete pix;
00785 }
00786
00787 void QColorLuminancePicker::mouseMoveEvent( QMouseEvent *m )
00788 {
00789 setVal( y2val(m->y()) );
00790 }
00791 void QColorLuminancePicker::mousePressEvent( QMouseEvent *m )
00792 {
00793 setVal( y2val(m->y()) );
00794 }
00795
00796 void QColorLuminancePicker::setVal( int v )
00797 {
00798 if ( val == v )
00799 return;
00800 val = QMAX( 0, QMIN(v,255));
00801 delete pix; pix=0;
00802 repaint( FALSE );
00803 emit newHsv( hue, sat, val );
00804 }
00805
00806
00807 void QColorLuminancePicker::setCol( int h, int s )
00808 {
00809 setCol( h, s, val );
00810 emit newHsv( h, s, val );
00811 }
00812
00813 void QColorLuminancePicker::paintEvent( QPaintEvent * )
00814 {
00815 int w = width() - 5;
00816
00817 QRect r( 0, foff, w, height() - 2*foff );
00818 int wi = r.width() - 2;
00819 int hi = r.height() - 2;
00820 if ( !pix || pix->height() != hi || pix->width() != wi ) {
00821 delete pix;
00822 QImage img( wi, hi, 32 );
00823 int y;
00824 for ( y = 0; y < hi; y++ ) {
00825 QColor c( hue, sat, y2val(y+coff), QColor::Hsv );
00826 QRgb r = c.rgb();
00827 int x;
00828 for ( x = 0; x < wi; x++ )
00829 img.setPixel( x, y, r );
00830 }
00831 pix = new QPixmap;
00832 pix->convertFromImage(img);
00833 }
00834 QPainter p(this);
00835 p.drawPixmap( 1, coff, *pix );
00836 QColorGroup g = colorGroup();
00837 qDrawShadePanel( &p, r, g, TRUE );
00838 p.setPen( g.foreground() );
00839 p.setBrush( g.foreground() );
00840 QPointArray a;
00841 int y = val2y(val);
00842 a.setPoints( 3, w, y, w+5, y+5, w+5, y-5 );
00843 erase( w, 0, 5, height() );
00844 p.drawPolygon( a );
00845 }
00846
00847 void QColorLuminancePicker::setCol( int h, int s , int v )
00848 {
00849 val = v;
00850 hue = h;
00851 sat = s;
00852 delete pix; pix=0;
00853 repaint( FALSE );
00854 }
00855
00856 QPoint QColorPicker::colPt()
00857 { return QPoint( (360-hue)*(pWidth-1)/360, (255-sat)*(pHeight-1)/255 ); }
00858 int QColorPicker::huePt( const QPoint &pt )
00859 { return 360 - pt.x()*360/(pWidth-1); }
00860 int QColorPicker::satPt( const QPoint &pt )
00861 { return 255 - pt.y()*255/(pHeight-1) ; }
00862 void QColorPicker::setCol( const QPoint &pt )
00863 { setCol( huePt(pt), satPt(pt) ); }
00864
00865 QColorPicker::QColorPicker(QWidget* parent, const char* name )
00866 : QFrame( parent, name )
00867 {
00868 hue = 0; sat = 0;
00869 setCol( 150, 255 );
00870
00871 QImage img( pWidth, pHeight, 32 );
00872 int x,y;
00873 for ( y = 0; y < pHeight; y++ )
00874 for ( x = 0; x < pWidth; x++ ) {
00875 QPoint p( x, y );
00876 img.setPixel( x, y, QColor(huePt(p), satPt(p),
00877 200, QColor::Hsv).rgb() );
00878 }
00879 pix = new QPixmap;
00880 pix->convertFromImage(img);
00881 setBackgroundMode( NoBackground );
00882 }
00883
00884 QColorPicker::~QColorPicker()
00885 {
00886 delete pix;
00887 }
00888
00889 QSize QColorPicker::sizeHint() const
00890 {
00891 return QSize( pWidth + 2*frameWidth(), pHeight + 2*frameWidth() );
00892 }
00893
00894 QSizePolicy QColorPicker::sizePolicy() const
00895 {
00896 return QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00897 }
00898
00899 void QColorPicker::setCol( int h, int s )
00900 {
00901 int nhue = QMIN( QMAX(0,h), 360 );
00902 int nsat = QMIN( QMAX(0,s), 255);
00903 if ( nhue == hue && nsat == sat )
00904 return;
00905 QRect r( colPt(), QSize(20,20) );
00906 hue = nhue; sat = nsat;
00907 r = r.unite( QRect( colPt(), QSize(20,20) ) );
00908 r.moveBy( contentsRect().x()-9, contentsRect().y()-9 );
00909
00910 repaint( r, FALSE );
00911 }
00912
00913 void QColorPicker::mouseMoveEvent( QMouseEvent *m )
00914 {
00915 QPoint p = m->pos() - contentsRect().topLeft();
00916 setCol( p );
00917 emit newCol( hue, sat );
00918 }
00919
00920 void QColorPicker::mousePressEvent( QMouseEvent *m )
00921 {
00922 QPoint p = m->pos() - contentsRect().topLeft();
00923 setCol( p );
00924 emit newCol( hue, sat );
00925 }
00926
00927 void QColorPicker::drawContents(QPainter* p)
00928 {
00929 QRect r = contentsRect();
00930
00931 p->drawPixmap( r.topLeft(), *pix );
00932 QPoint pt = colPt() + r.topLeft();
00933 p->setPen( QPen(black) );
00934
00935 p->fillRect( pt.x()-9, pt.y(), 20, 2, black );
00936 p->fillRect( pt.x(), pt.y()-9, 2, 20, black );
00937
00938 }
00939
00940 class QColorShowLabel;
00941
00942
00943
00944 class QColIntValidator: public QIntValidator
00945 {
00946 public:
00947 QColIntValidator( int bottom, int top,
00948 QWidget * parent, const char *name = 0 )
00949 :QIntValidator( bottom, top, parent, name ) {}
00950
00951 QValidator::State validate( QString &, int & ) const;
00952 };
00953
00954 QValidator::State QColIntValidator::validate( QString &s, int &pos ) const
00955 {
00956 State state = QIntValidator::validate(s,pos);
00957 if ( state == Valid ) {
00958 long int val = s.toLong();
00959
00960
00961 if ( val < 0 ) {
00962 s = "0";
00963 pos = 1;
00964 } else if ( val > top() ) {
00965 s.setNum( top() );
00966 pos = s.length();
00967 }
00968 }
00969 return state;
00970 }
00971
00972
00973
00974 class QColNumLineEdit : public QLineEdit
00975 {
00976 public:
00977 QColNumLineEdit( QWidget *parent, const char* name = 0 )
00978 : QLineEdit( parent, name ) { setMaxLength( 3 );}
00979 QSize sizeHint() const {
00980 return QSize( 30,
00981 QLineEdit::sizeHint().height() ); }
00982 void setNum( int i ) {
00983 QString s;
00984 s.setNum(i);
00985 bool block = signalsBlocked();
00986 blockSignals(TRUE);
00987 setText( s );
00988 blockSignals(block);
00989 }
00990 int val() const { return text().toInt(); }
00991 };
00992
00993
00994 class QColorShower : public QWidget
00995 {
00996 Q_OBJECT
00997 public:
00998 QColorShower( QWidget *parent, const char *name = 0 );
00999
01000
01001 void setHsv( int h, int s, int v );
01002
01003 int currentAlpha() const { return alphaEd->val(); }
01004 void setCurrentAlpha( int a ) { alphaEd->setNum( a ); }
01005 void showAlpha( bool b );
01006
01007
01008 QRgb currentColor() const { return curCol; }
01009
01010 public slots:
01011 void setRgb( QRgb rgb );
01012
01013 signals:
01014 void newCol( QRgb rgb );
01015 private slots:
01016 void rgbEd();
01017 void hsvEd();
01018 private:
01019 void showCurrentColor();
01020 int hue, sat, val;
01021 QRgb curCol;
01022 QColNumLineEdit *hEd;
01023 QColNumLineEdit *sEd;
01024 QColNumLineEdit *vEd;
01025 QColNumLineEdit *rEd;
01026 QColNumLineEdit *gEd;
01027 QColNumLineEdit *bEd;
01028 QColNumLineEdit *alphaEd;
01029 QLabel *alphaLab;
01030 QColorShowLabel *lab;
01031 bool rgbOriginal;
01032 };
01033
01034 class QColorShowLabel : public QFrame
01035 {
01036 Q_OBJECT
01037
01038 public:
01039 QColorShowLabel( QWidget *parent ) :QFrame( parent ) {
01040 setFrameStyle( QFrame::Panel|QFrame::Sunken );
01041 setBackgroundMode( PaletteBackground );
01042 setAcceptDrops( TRUE );
01043 mousePressed = FALSE;
01044 }
01045 void setColor( QColor c ) { col = c; }
01046
01047 signals:
01048 void colorDropped( QRgb );
01049
01050 protected:
01051 void drawContents( QPainter *p );
01052 void mousePressEvent( QMouseEvent *e );
01053 void mouseMoveEvent( QMouseEvent *e );
01054 void mouseReleaseEvent( QMouseEvent *e );
01055 #ifndef QT_NO_DRAGANDDROP
01056 void dragEnterEvent( QDragEnterEvent *e );
01057 void dragLeaveEvent( QDragLeaveEvent *e );
01058 void dropEvent( QDropEvent *e );
01059 #endif
01060
01061 private:
01062 QColor col;
01063 bool mousePressed;
01064 QPoint pressPos;
01065
01066 };
01067
01068 void QColorShowLabel::drawContents( QPainter *p )
01069 {
01070 p->fillRect( contentsRect(), col );
01071 }
01072
01073 void QColorShower::showAlpha( bool b )
01074 {
01075 if ( b ) {
01076 alphaLab->show();
01077 alphaEd->show();
01078 } else {
01079 alphaLab->hide();
01080 alphaEd->hide();
01081 }
01082 }
01083
01084 void QColorShowLabel::mousePressEvent( QMouseEvent *e )
01085 {
01086 mousePressed = TRUE;
01087 pressPos = e->pos();
01088 }
01089
01090 void QColorShowLabel::mouseMoveEvent( QMouseEvent *e )
01091 {
01092 #ifndef QT_NO_DRAGANDDROP
01093 if ( !mousePressed )
01094 return;
01095 if ( ( pressPos - e->pos() ).manhattanLength() > QApplication::startDragDistance() ) {
01096 QColorDrag *drg = new QColorDrag( col, this );
01097 QPixmap pix( 30, 20 );
01098 pix.fill( col );
01099 QPainter p( &pix );
01100 p.drawRect( 0, 0, pix.width(), pix.height() );
01101 p.end();
01102 drg->setPixmap( pix );
01103 mousePressed = FALSE;
01104 drg->dragCopy();
01105 }
01106 #else
01107 Q_UNUSED( e )
01108 #endif
01109 }
01110
01111 #ifndef QT_NO_DRAGANDDROP
01112 void QColorShowLabel::dragEnterEvent( QDragEnterEvent *e )
01113 {
01114 if ( QColorDrag::canDecode( e ) )
01115 e->accept();
01116 else
01117 e->ignore();
01118 }
01119
01120 void QColorShowLabel::dragLeaveEvent( QDragLeaveEvent * )
01121 {
01122 }
01123
01124 void QColorShowLabel::dropEvent( QDropEvent *e )
01125 {
01126 if ( QColorDrag::canDecode( e ) ) {
01127 QColorDrag::decode( e, col );
01128 repaint( FALSE );
01129 emit colorDropped( col.rgb() );
01130 e->accept();
01131 } else {
01132 e->ignore();
01133 }
01134 }
01135 #endif // QT_NO_DRAGANDDROP
01136
01137 void QColorShowLabel::mouseReleaseEvent( QMouseEvent * )
01138 {
01139 if ( !mousePressed )
01140 return;
01141 mousePressed = FALSE;
01142 }
01143
01144 QColorShower::QColorShower( QWidget *parent, const char *name )
01145 :QWidget( parent, name)
01146 {
01147 curCol = qRgb( -1, -1, -1 );
01148 QColIntValidator *val256 = new QColIntValidator( 0, 255, this );
01149 QColIntValidator *val360 = new QColIntValidator( 0, 360, this );
01150
01151 QGridLayout *gl = new QGridLayout( this, 1, 1, 6 );
01152 lab = new QColorShowLabel( this );
01153 lab->setMinimumWidth( 60 );
01154 gl->addMultiCellWidget(lab, 0,-1,0,0);
01155 connect( lab, SIGNAL( colorDropped(QRgb) ),
01156 this, SIGNAL( newCol(QRgb) ) );
01157 connect( lab, SIGNAL( colorDropped(QRgb) ),
01158 this, SLOT( setRgb(QRgb) ) );
01159
01160 hEd = new QColNumLineEdit( this );
01161 hEd->setValidator( val360 );
01162 QLabel *l = new QLabel( hEd, QColorDialog::tr("Hu&e:"), this );
01163 l->setAlignment( AlignRight|AlignVCenter );
01164 gl->addWidget( l, 0, 1 );
01165 gl->addWidget( hEd, 0, 2 );
01166
01167 sEd = new QColNumLineEdit( this );
01168 sEd->setValidator( val256 );
01169 l = new QLabel( sEd, QColorDialog::tr("&Sat:"), this );
01170 l->setAlignment( AlignRight|AlignVCenter );
01171 gl->addWidget( l, 1, 1 );
01172 gl->addWidget( sEd, 1, 2 );
01173
01174 vEd = new QColNumLineEdit( this );
01175 vEd->setValidator( val256 );
01176 l = new QLabel( vEd, QColorDialog::tr("&Val:"), this );
01177 l->setAlignment( AlignRight|AlignVCenter );
01178 gl->addWidget( l, 2, 1 );
01179 gl->addWidget( vEd, 2, 2 );
01180
01181 rEd = new QColNumLineEdit( this );
01182 rEd->setValidator( val256 );
01183 l = new QLabel( rEd, QColorDialog::tr("&Red:"), this );
01184 l->setAlignment( AlignRight|AlignVCenter );
01185 gl->addWidget( l, 0, 3 );
01186 gl->addWidget( rEd, 0, 4 );
01187
01188 gEd = new QColNumLineEdit( this );
01189 gEd->setValidator( val256 );
01190 l = new QLabel( gEd, QColorDialog::tr("&Green:"), this );
01191 l->setAlignment( AlignRight|AlignVCenter );
01192 gl->addWidget( l, 1, 3 );
01193 gl->addWidget( gEd, 1, 4 );
01194
01195 bEd = new QColNumLineEdit( this );
01196 bEd->setValidator( val256 );
01197 l = new QLabel( bEd, QColorDialog::tr("Bl&ue:"), this );
01198 l->setAlignment( AlignRight|AlignVCenter );
01199 gl->addWidget( l, 2, 3 );
01200 gl->addWidget( bEd, 2, 4 );
01201
01202 alphaEd = new QColNumLineEdit( this );
01203 alphaEd->setValidator( val256 );
01204 alphaLab = new QLabel( alphaEd, QColorDialog::tr("A&lpha channel:"), this );
01205 alphaLab->setAlignment( AlignRight|AlignVCenter );
01206 gl->addMultiCellWidget( alphaLab, 3, 3, 1, 3 );
01207 gl->addWidget( alphaEd, 3, 4 );
01208 alphaEd->hide();
01209 alphaLab->hide();
01210
01211 connect( hEd, SIGNAL(textChanged(const QString&)), this, SLOT(hsvEd()) );
01212 connect( sEd, SIGNAL(textChanged(const QString&)), this, SLOT(hsvEd()) );
01213 connect( vEd, SIGNAL(textChanged(const QString&)), this, SLOT(hsvEd()) );
01214
01215 connect( rEd, SIGNAL(textChanged(const QString&)), this, SLOT(rgbEd()) );
01216 connect( gEd, SIGNAL(textChanged(const QString&)), this, SLOT(rgbEd()) );
01217 connect( bEd, SIGNAL(textChanged(const QString&)), this, SLOT(rgbEd()) );
01218 }
01219
01220 void QColorShower::showCurrentColor()
01221 {
01222 lab->setColor( currentColor() );
01223 lab->repaint(FALSE);
01224 }
01225
01226 void QColorShower::rgbEd()
01227 {
01228 rgbOriginal = TRUE;
01229 curCol = qRgb( rEd->val(), gEd->val(), bEd->val() );
01230 rgb2hsv(currentColor(), hue, sat, val );
01231
01232 hEd->setNum( hue );
01233 sEd->setNum( sat );
01234 vEd->setNum( val );
01235
01236 showCurrentColor();
01237 emit newCol( currentColor() );
01238 }
01239
01240 void QColorShower::hsvEd()
01241 {
01242 rgbOriginal = FALSE;
01243 hue = hEd->val();
01244 sat = sEd->val();
01245 val = vEd->val();
01246
01247 curCol = QColor( hue, sat, val, QColor::Hsv ).rgb();
01248
01249 rEd->setNum( qRed(currentColor()) );
01250 gEd->setNum( qGreen(currentColor()) );
01251 bEd->setNum( qBlue(currentColor()) );
01252
01253 showCurrentColor();
01254 emit newCol( currentColor() );
01255 }
01256
01257 void QColorShower::setRgb( QRgb rgb )
01258 {
01259 rgbOriginal = TRUE;
01260 curCol = rgb;
01261
01262 rgb2hsv( currentColor(), hue, sat, val );
01263
01264 hEd->setNum( hue );
01265 sEd->setNum( sat );
01266 vEd->setNum( val );
01267
01268 rEd->setNum( qRed(currentColor()) );
01269 gEd->setNum( qGreen(currentColor()) );
01270 bEd->setNum( qBlue(currentColor()) );
01271
01272 showCurrentColor();
01273 }
01274
01275 void QColorShower::setHsv( int h, int s, int v )
01276 {
01277 rgbOriginal = FALSE;
01278 hue = h; val = v; sat = s;
01279 curCol = QColor( hue, sat, val, QColor::Hsv ).rgb();
01280
01281 hEd->setNum( hue );
01282 sEd->setNum( sat );
01283 vEd->setNum( val );
01284
01285 rEd->setNum( qRed(currentColor()) );
01286 gEd->setNum( qGreen(currentColor()) );
01287 bEd->setNum( qBlue(currentColor()) );
01288
01289
01290 showCurrentColor();
01291 }
01292
01293 class QColorDialogPrivate : public QObject
01294 {
01295 Q_OBJECT
01296 public:
01297 QColorDialogPrivate( QColorDialog *p );
01298 QRgb currentColor() const { return cs->currentColor(); }
01299 void setCurrentColor( QRgb rgb );
01300
01301 int currentAlpha() const { return cs->currentAlpha(); }
01302 void setCurrentAlpha( int a ) { cs->setCurrentAlpha( a ); }
01303 void showAlpha( bool b ) { cs->showAlpha( b ); }
01304
01305 private slots:
01306 void addCustom();
01307
01308 void newHsv( int h, int s, int v );
01309 void newColorTypedIn( QRgb rgb );
01310 void newCustom( int, int );
01311 void newStandard( int, int );
01312 private:
01313 QColorPicker *cp;
01314 QColorLuminancePicker *lp;
01315 QWellArray *custom;
01316 QWellArray *standard;
01317 QColorShower *cs;
01318 int nextCust;
01319 bool compact;
01320 };
01321
01322
01323 void QColorDialogPrivate::newHsv( int h, int s, int v )
01324 {
01325 cs->setHsv( h, s, v );
01326 cp->setCol( h, s );
01327 lp->setCol( h, s, v );
01328 }
01329
01330
01331 void QColorDialogPrivate::setCurrentColor( QRgb rgb )
01332 {
01333 cs->setRgb( rgb );
01334 newColorTypedIn( rgb );
01335 }
01336
01337
01338 void QColorDialogPrivate::newColorTypedIn( QRgb rgb )
01339 {
01340 int h, s, v;
01341 rgb2hsv(rgb, h, s, v );
01342 cp->setCol( h, s );
01343 lp->setCol( h, s, v);
01344 }
01345
01346 void QColorDialogPrivate::newCustom( int r, int c )
01347 {
01348 int i = r+2*c;
01349 setCurrentColor( cusrgb[i] );
01350 nextCust = i;
01351 standard->setSelected(-1,-1);
01352 }
01353
01354 void QColorDialogPrivate::newStandard( int r, int c )
01355 {
01356 setCurrentColor( stdrgb[r+c*6] );
01357 custom->setSelected(-1,-1);
01358 }
01359
01360 QColorDialogPrivate::QColorDialogPrivate( QColorDialog *dialog ) :
01361 QObject(dialog)
01362 {
01363 compact = FALSE;
01364
01365
01366 if ( qApp->desktop()->width() < 480 || qApp->desktop()->height() < 350 )
01367 compact = TRUE;
01368
01369 nextCust = 0;
01370 const int lumSpace = 3;
01371 int border = 12;
01372 if ( compact )
01373 border = 6;
01374 QHBoxLayout *topLay = new QHBoxLayout( dialog, border, 6 );
01375 QVBoxLayout *leftLay = 0;
01376
01377 if ( !compact )
01378 leftLay = new QVBoxLayout( topLay );
01379
01380 initRGB();
01381
01382 if ( !compact ) {
01383 standard = new QColorWell( dialog, 6, 8, stdrgb );
01384 standard->setCellSize( 28, 24 );
01385 QLabel * lab = new QLabel( standard,
01386 QColorDialog::tr( "&Basic colors"), dialog );
01387 connect( standard, SIGNAL(selected(int,int)), SLOT(newStandard(int,int)));
01388 leftLay->addWidget( lab );
01389 leftLay->addWidget( standard );
01390
01391
01392 leftLay->addStretch();
01393
01394 custom = new QColorWell( dialog, 2, 8, cusrgb );
01395 custom->setCellSize( 28, 24 );
01396 custom->setAcceptDrops( TRUE );
01397
01398 connect( custom, SIGNAL(selected(int,int)), SLOT(newCustom(int,int)));
01399 lab = new QLabel( custom, QColorDialog::tr( "&Custom colors") , dialog );
01400 leftLay->addWidget( lab );
01401 leftLay->addWidget( custom );
01402
01403 QPushButton *custbut =
01404 new QPushButton( QColorDialog::tr("&Define Custom Colors >>"),
01405 dialog );
01406 custbut->setEnabled( FALSE );
01407 leftLay->addWidget( custbut );
01408 } else {
01409
01410 pWidth = 150;
01411 pHeight = 100;
01412 }
01413
01414 QVBoxLayout *rightLay = new QVBoxLayout( topLay );
01415
01416 QHBoxLayout *pickLay = new QHBoxLayout( rightLay );
01417
01418
01419 QVBoxLayout *cLay = new QVBoxLayout( pickLay );
01420 cp = new QColorPicker( dialog );
01421 cp->setFrameStyle( QFrame::Panel + QFrame::Sunken );
01422 cLay->addSpacing( lumSpace );
01423 cLay->addWidget( cp );
01424 cLay->addSpacing( lumSpace );
01425
01426 lp = new QColorLuminancePicker( dialog );
01427 lp->setFixedWidth( 20 );
01428 pickLay->addWidget( lp );
01429
01430 connect( cp, SIGNAL(newCol(int,int)), lp, SLOT(setCol(int,int)) );
01431 connect( lp, SIGNAL(newHsv(int,int,int)), this, SLOT(newHsv(int,int,int)) );
01432
01433 rightLay->addStretch();
01434
01435 cs = new QColorShower( dialog );
01436 connect( cs, SIGNAL(newCol(QRgb)), this, SLOT(newColorTypedIn(QRgb)));
01437 rightLay->addWidget( cs );
01438
01439 QHBoxLayout *buttons;
01440 if ( compact )
01441 buttons = new QHBoxLayout( rightLay );
01442 else
01443 buttons = new QHBoxLayout( leftLay );
01444
01445 QPushButton *ok, *cancel;
01446 ok = new QPushButton( QColorDialog::tr("OK"), dialog );
01447 connect( ok, SIGNAL(clicked()), dialog, SLOT(accept()) );
01448 ok->setDefault(TRUE);
01449 cancel = new QPushButton( QColorDialog::tr("Cancel"), dialog );
01450 connect( cancel, SIGNAL(clicked()), dialog, SLOT(reject()) );
01451 buttons->addWidget( ok );
01452 buttons->addWidget( cancel );
01453 buttons->addStretch();
01454
01455 if ( !compact ) {
01456 QPushButton *addCusBt = new QPushButton(
01457 QColorDialog::tr("&Add To Custom Colors"),
01458 dialog );
01459 rightLay->addWidget( addCusBt );
01460 connect( addCusBt, SIGNAL(clicked()), this, SLOT(addCustom()) );
01461 }
01462 }
01463
01464 void QColorDialogPrivate::addCustom()
01465 {
01466 cusrgb[nextCust] = cs->currentColor();
01467 custom->repaint( FALSE );
01468 nextCust = (nextCust+1) % 16;
01469 }
01470
01471
01472
01501 QColorDialog::QColorDialog(QWidget* parent, const char* name, bool modal) :
01502 QDialog(parent, name, modal )
01503 {
01504 setSizeGripEnabled( TRUE );
01505 d = new QColorDialogPrivate( this );
01506 }
01507
01508
01516 QColor QColorDialog::getColor( QColor initial, QWidget *parent,
01517 const char *name )
01518 {
01519 int allocContext = QColor::enterAllocContext();
01520 QColorDialog *dlg = new QColorDialog( parent, name, TRUE );
01521 if ( parent && parent->icon() && !parent->icon()->isNull() )
01522 dlg->setIcon( *parent->icon() );
01523 else if ( qApp->mainWidget() && qApp->mainWidget()->icon() && !qApp->mainWidget()->icon()->isNull() )
01524 dlg->setIcon( *qApp->mainWidget()->icon() );
01525
01526 dlg->setCaption( QColorDialog::tr( "Select color" ) );
01527 dlg->setColor( initial );
01528 int resultCode = dlg->exec();
01529 QColor::leaveAllocContext();
01530 QColor result;
01531 if ( resultCode == QDialog::Accepted )
01532 result = dlg->color();
01533 QColor::destroyAllocContext(allocContext);
01534 delete dlg;
01535 return result;
01536 }
01537
01538
01549 QRgb QColorDialog::getRgba( QRgb initial, bool *ok,
01550 QWidget *parent, const char* name )
01551 {
01552 int allocContext = QColor::enterAllocContext();
01553 QColorDialog *dlg = new QColorDialog( parent, name, TRUE );
01554 dlg->setColor( initial );
01555 dlg->setSelectedAlpha( qAlpha(initial) );
01556 int resultCode = dlg->exec();
01557 QColor::leaveAllocContext();
01558 QRgb result = initial;
01559 if ( resultCode == QDialog::Accepted ) {
01560 QRgb c = dlg->color().rgb();
01561 int alpha = dlg->selectedAlpha();
01562 result = qRgba( qRed(c), qGreen(c), qBlue(c), alpha );
01563 }
01564 if ( ok )
01565 *ok = resultCode == QDialog::Accepted;
01566
01567 QColor::destroyAllocContext(allocContext);
01568 delete dlg;
01569 return result;
01570 }
01571
01572
01573
01574
01575
01582 QColor QColorDialog::color() const
01583 {
01584 return QColor(d->currentColor());
01585 }
01586
01587
01592 QColorDialog::~QColorDialog()
01593 {
01594
01595 }
01596
01597
01604 void QColorDialog::setColor( QColor c )
01605 {
01606 d->setCurrentColor( c.rgb() );
01607 }
01608
01609
01610
01611
01617 void QColorDialog::setSelectedAlpha( int a )
01618 {
01619 d->showAlpha( TRUE );
01620 d->setCurrentAlpha( a );
01621 }
01622
01623
01628 int QColorDialog::selectedAlpha() const
01629 {
01630 return d->currentAlpha();
01631 }
01632
01633
01634 #include "qcolordialog.moc"