kdeui Library API Documentation

kcolordialog.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1997 Martin Jones (mjones@kde.org)
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 //-----------------------------------------------------------------------------
00020 // KDE color selection dialog.
00021 //
00022 // 1999-09-27 Espen Sand <espensa@online.no>
00023 // KColorDialog is now subclassed from KDialogBase. I have also extended
00024 // KColorDialog::getColor() so that it contains a parent argument. This
00025 // improves centering capability.
00026 //
00027 // layout management added Oct 1997 by Mario Weilguni
00028 // <mweilguni@sime.com>
00029 //
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 
00034 #include <qcheckbox.h>
00035 #include <qcombobox.h>
00036 #include <qdrawutil.h>
00037 #include <qevent.h>
00038 #include <qfile.h>
00039 #include <qimage.h>
00040 #include <qlabel.h>
00041 #include <qlayout.h>
00042 #include <qlineedit.h>
00043 #include <qvalidator.h>
00044 #include <qpainter.h>
00045 #include <qpushbutton.h>
00046 #include <qspinbox.h>
00047 #include <qtimer.h>
00048 
00049 #include <kapplication.h>
00050 #include <kconfig.h>
00051 #include <kglobal.h>
00052 #include <kglobalsettings.h>
00053 #include <kiconloader.h>
00054 #include <klistbox.h>
00055 #include <klocale.h>
00056 #include <kmessagebox.h>
00057 #include <kseparator.h>
00058 #include <kpalette.h>
00059 #include <kimageeffect.h>
00060 
00061 #include "kcolordialog.h"
00062 #include "kcolordrag.h"
00063 #include "kstaticdeleter.h"
00064 #include <config.h>
00065 #include <kdebug.h>
00066 
00067 #include "config.h"
00068 #ifdef Q_WS_X11
00069 #include <X11/Xlib.h> 
00070 
00071 // defined in qapplication_x11.cpp
00072 typedef int (*QX11EventFilter) (XEvent*);
00073 extern QX11EventFilter qt_set_x11_event_filter (QX11EventFilter filter);
00074 #endif
00075 
00076 static const char * const recentColors = "Recent_Colors";
00077 static const char * const customColors = "Custom_Colors";
00078 
00079 class KColorSpinBox : public QSpinBox
00080 {
00081 public:
00082   KColorSpinBox(int minValue, int maxValue, int step, QWidget* parent)
00083    : QSpinBox(minValue, maxValue, step, parent, "kcolorspinbox")
00084   { }
00085 
00086   // Override Qt's braindead auto-selection.
00087   virtual void valueChange()
00088   {
00089       updateDisplay();
00090       emit valueChanged( value() );
00091       emit valueChanged( currentValueText() );
00092   }
00093 
00094 };
00095 
00096 
00097 #define STANDARD_PAL_SIZE 17
00098 
00099 KColor::KColor()
00100 : QColor()
00101 {
00102   r = 0; g = 0; b = 0; h = 0; s = 0; v = 0;
00103 }
00104 
00105 KColor::KColor( const KColor &col)
00106 : QColor( col )
00107 {
00108   h = col.h; s = col.s; v = col.v;
00109   r = col.r; g = col.g; b = col.b;
00110 }
00111 
00112 KColor::KColor( const QColor &col)
00113 : QColor( col )
00114 {
00115   QColor::getRgb(&r, &g, &b);
00116   QColor::getHsv(&h, &s, &v);
00117 }
00118 
00119 bool KColor::operator==(const KColor& col) const
00120 {
00121   return (h == col.h) && (s == col.s) && (v == col.v) &&
00122          (r == col.r) && (g == col.g) && (b == col.b);
00123 }
00124 
00125 KColor& KColor::operator=(const KColor& col)
00126 {
00127   *(QColor *)this = col;
00128   h = col.h; s = col.s; v = col.v;
00129   r = col.r; g = col.g; b = col.b;
00130   return *this;
00131 }
00132 
00133 void
00134 KColor::setHsv(int _h, int _s, int _v)
00135 {
00136   h = _h; s = _s; v = _v;
00137   QColor::setHsv(h, s, v);
00138   QColor::rgb(&r, &g, &b);
00139 }
00140 
00141 void
00142 KColor::setRgb(int _r, int _g, int _b)
00143 {
00144   r = _r; g = _g; b = _b;
00145   QColor::setRgb(r, g, b);
00146   QColor::hsv(&h, &s, &v);
00147 }
00148 
00149 void
00150 KColor::rgb(int *_r, int *_g, int *_b) const
00151 {
00152   *_r = r; *_g = g; *_b = b;
00153 }
00154 
00155 void
00156 KColor::hsv(int *_h, int *_s, int *_v) const
00157 {
00158   *_h = h; *_s = s; *_v = v;
00159 }
00160 
00161 
00162 static QColor *standardPalette = 0;
00163 static KStaticDeleter<QColor> spd;
00164 
00165 static void createStandardPalette()
00166 {
00167     if ( standardPalette )
00168     return;
00169 
00170     spd.setObject(standardPalette, new QColor [STANDARD_PAL_SIZE], true/*array*/);
00171 
00172     int i = 0;
00173 
00174     standardPalette[i++] = Qt::red;
00175     standardPalette[i++] = Qt::green;
00176     standardPalette[i++] = Qt::blue;
00177     standardPalette[i++] = Qt::cyan;
00178     standardPalette[i++] = Qt::magenta;
00179     standardPalette[i++] = Qt::yellow;
00180     standardPalette[i++] = Qt::darkRed;
00181     standardPalette[i++] = Qt::darkGreen;
00182     standardPalette[i++] = Qt::darkBlue;
00183     standardPalette[i++] = Qt::darkCyan;
00184     standardPalette[i++] = Qt::darkMagenta;
00185     standardPalette[i++] = Qt::darkYellow;
00186     standardPalette[i++] = Qt::white;
00187     standardPalette[i++] = Qt::lightGray;
00188     standardPalette[i++] = Qt::gray;
00189     standardPalette[i++] = Qt::darkGray;
00190     standardPalette[i++] = Qt::black;
00191 }
00192 
00193 
00194 KHSSelector::KHSSelector( QWidget *parent, const char *name )
00195     : KXYSelector( parent, name )
00196 {
00197     setRange( 0, 0, 359, 255 );
00198 }
00199 
00200 void KHSSelector::updateContents()
00201 {
00202     drawPalette(&pixmap);
00203 }
00204 
00205 void KHSSelector::resizeEvent( QResizeEvent * )
00206 {
00207     updateContents();
00208 }
00209 
00210 void KHSSelector::drawContents( QPainter *painter )
00211 {
00212     painter->drawPixmap( contentsRect().x(), contentsRect().y(), pixmap );
00213 }
00214 
00215 void KHSSelector::drawPalette( QPixmap *pixmap )
00216 {
00217     int xSize = contentsRect().width(), ySize = contentsRect().height();
00218     QImage image( xSize, ySize, 32 );
00219     QColor col;
00220     int h, s;
00221     uint *p;
00222 
00223     for ( s = ySize-1; s >= 0; s-- )
00224     {
00225         p = (uint *) image.scanLine( ySize - s - 1 );
00226         for( h = 0; h < xSize; h++ )
00227         {
00228             col.setHsv( 359*h/(xSize-1), 255*s/(ySize-1), 192 );
00229             *p = col.rgb();
00230             p++;
00231         }
00232     }
00233 
00234     if ( QColor::numBitPlanes() <= 8 )
00235     {
00236         createStandardPalette();
00237         KImageEffect::dither( image, standardPalette, STANDARD_PAL_SIZE );
00238     }
00239     pixmap->convertFromImage( image );
00240 }
00241 
00242 
00243 //-----------------------------------------------------------------------------
00244 
00245 KValueSelector::KValueSelector( QWidget *parent, const char *name )
00246     : KSelector( KSelector::Vertical, parent, name ), _hue(0), _sat(0)
00247 {
00248     setRange( 0, 255 );
00249     pixmap.setOptimization( QPixmap::BestOptim );
00250 }
00251 
00252 KValueSelector::KValueSelector(Orientation o, QWidget *parent, const char *name
00253  )
00254     : KSelector( o, parent, name), _hue(0), _sat(0)
00255 {
00256     setRange( 0, 255 );
00257     pixmap.setOptimization( QPixmap::BestOptim );
00258 }
00259 
00260 void KValueSelector::updateContents()
00261 {
00262     drawPalette(&pixmap);
00263 }
00264 
00265 void KValueSelector::resizeEvent( QResizeEvent * )
00266 {
00267     updateContents();
00268 }
00269 
00270 void KValueSelector::drawContents( QPainter *painter )
00271 {
00272     painter->drawPixmap( contentsRect().x(), contentsRect().y(), pixmap );
00273 }
00274 
00275 void KValueSelector::drawPalette( QPixmap *pixmap )
00276 {
00277     int xSize = contentsRect().width(), ySize = contentsRect().height();
00278     QImage image( xSize, ySize, 32 );
00279     QColor col;
00280     uint *p;
00281     QRgb rgb;
00282 
00283     if ( orientation() == KSelector::Horizontal )
00284     {
00285         for ( int v = 0; v < ySize; v++ )
00286         {
00287             p = (uint *) image.scanLine( ySize - v - 1 );
00288 
00289             for( int x = 0; x < xSize; x++ )
00290             {
00291                 col.setHsv( _hue, _sat, 255*x/(xSize-1) );
00292                 rgb = col.rgb();
00293                 *p++ = rgb;
00294             }
00295         }
00296     }
00297 
00298     if( orientation() == KSelector::Vertical )
00299     {
00300         for ( int v = 0; v < ySize; v++ )
00301         {
00302             p = (uint *) image.scanLine( ySize - v - 1 );
00303             col.setHsv( _hue, _sat, 255*v/(ySize-1) );
00304             rgb = col.rgb();
00305             for ( int i = 0; i < xSize; i++ )
00306                 *p++ = rgb;
00307         }
00308     }
00309 
00310     if ( QColor::numBitPlanes() <= 8 )
00311     {
00312         createStandardPalette();
00313         KImageEffect::dither( image, standardPalette, STANDARD_PAL_SIZE );
00314     }
00315     pixmap->convertFromImage( image );
00316 }
00317 
00318 //-----------------------------------------------------------------------------
00319 
00320 KColorCells::KColorCells( QWidget *parent, int rows, int cols )
00321     : QGridView( parent )
00322 {
00323     shade = true;
00324     setNumRows( rows );
00325     setNumCols( cols );
00326     colors = new QColor [ rows * cols ];
00327 
00328     for ( int i = 0; i < rows * cols; i++ )
00329         colors[i] = QColor();
00330 
00331     selected = 0;
00332         inMouse = false;
00333 
00334     // Drag'n'Drop
00335     setAcceptDrops( true);
00336 
00337     setHScrollBarMode( AlwaysOff );
00338     setVScrollBarMode( AlwaysOff );
00339     viewport()->setBackgroundMode( PaletteBackground );
00340     setBackgroundMode( PaletteBackground );
00341 }
00342 
00343 KColorCells::~KColorCells()
00344 {
00345     delete [] colors;
00346 }
00347 
00348 void KColorCells::setColor( int colNum, const QColor &col )
00349 {
00350     colors[colNum] = col;
00351     updateCell( colNum/numCols(), colNum%numCols() );
00352 }
00353 
00354 void KColorCells::paintCell( QPainter *painter, int row, int col )
00355 {
00356     QBrush brush;
00357         int w = 1;
00358 
00359     if (shade)
00360         {
00361         qDrawShadePanel( painter, 1, 1, cellWidth()-2,
00362             cellHeight()-2, colorGroup(), true, 1, &brush );
00363         w = 2;
00364         }
00365         QColor color = colors[ row * numCols() + col ];
00366         if (!color.isValid())
00367     {
00368         if (!shade) return;
00369         color = backgroundColor();
00370     }
00371 
00372     painter->setPen( color );
00373     painter->setBrush( QBrush( color ) );
00374     painter->drawRect( w, w, cellWidth()-w*2, cellHeight()-w*2 );
00375 
00376     if ( row * numCols() + col == selected )
00377         painter->drawWinFocusRect( w, w, cellWidth()-w*2, cellHeight()-w*2 );
00378 }
00379 
00380 void KColorCells::resizeEvent( QResizeEvent * )
00381 {
00382     setCellWidth( width() / numCols() );
00383     setCellHeight( height() / numRows() );
00384 }
00385 
00386 void KColorCells::mousePressEvent( QMouseEvent *e )
00387 {
00388     inMouse = true;
00389     mPos = e->pos();
00390 }
00391 
00392 int KColorCells::posToCell(const QPoint &pos, bool ignoreBorders)
00393 {
00394    int row = pos.y() / cellHeight();
00395    int col = pos.x() / cellWidth();
00396    int cell = row * numCols() + col;
00397 
00398    if (!ignoreBorders)
00399    {
00400       int border = 2;
00401       int x = pos.x() - col * cellWidth();
00402       int y = pos.y() - row * cellHeight();
00403       if ( (x < border) || (x > cellWidth()-border) ||
00404            (y < border) || (y > cellHeight()-border))
00405          return -1;
00406    }
00407    return cell;
00408 }
00409 
00410 void KColorCells::mouseMoveEvent( QMouseEvent *e )
00411 {
00412     if( !(e->state() && LeftButton)) return;
00413 
00414     if(inMouse) {
00415         int delay = KGlobalSettings::dndEventDelay();
00416         if(e->x() > mPos.x()+delay || e->x() < mPos.x()-delay ||
00417            e->y() > mPos.y()+delay || e->y() < mPos.y()-delay){
00418             // Drag color object
00419             int cell = posToCell(mPos);
00420             if ((cell != -1) && colors[cell].isValid())
00421             {
00422                KColorDrag *d = new KColorDrag( colors[cell], this);
00423                d->dragCopy();
00424             }
00425         }
00426     }
00427 }
00428 
00429 void KColorCells::dragEnterEvent( QDragEnterEvent *event)
00430 {
00431      event->accept( acceptDrags && KColorDrag::canDecode( event));
00432 }
00433 
00434 void KColorCells::dropEvent( QDropEvent *event)
00435 {
00436      QColor c;
00437      if( KColorDrag::decode( event, c)) {
00438           int cell = posToCell(event->pos(), true);
00439       setColor(cell,c);
00440      }
00441 }
00442 
00443 void KColorCells::mouseReleaseEvent( QMouseEvent *e )
00444 {
00445     int cell = posToCell(mPos);
00446         int currentCell = posToCell(e->pos());
00447 
00448         // If we release the mouse in another cell and we don't have
00449         // a drag we should ignore this event.
00450         if (currentCell != cell)
00451            cell = -1;
00452 
00453     if ( (cell != -1) && (selected != cell) )
00454     {
00455         int prevSel = selected;
00456         selected = cell;
00457         updateCell( prevSel/numCols(), prevSel%numCols() );
00458         updateCell( cell/numCols(), cell%numCols() );
00459         }
00460 
00461         inMouse = false;
00462         if (cell != -1)
00463         emit colorSelected( cell );
00464 }
00465 
00466 void KColorCells::mouseDoubleClickEvent( QMouseEvent * /*e*/ )
00467 {
00468   int cell = posToCell(mPos);
00469 
00470   if (cell != -1)
00471     emit colorDoubleClicked( cell );
00472 }
00473 
00474 
00475 //-----------------------------------------------------------------------------
00476 
00477 KColorPatch::KColorPatch( QWidget *parent ) : QFrame( parent )
00478 {
00479     setFrameStyle( QFrame::Panel | QFrame::Sunken );
00480     colContext = 0;
00481     setAcceptDrops( true);
00482 }
00483 
00484 KColorPatch::~KColorPatch()
00485 {
00486   if ( colContext )
00487     QColor::destroyAllocContext( colContext );
00488 }
00489 
00490 void KColorPatch::setColor( const QColor &col )
00491 {
00492     if ( colContext )
00493         QColor::destroyAllocContext( colContext );
00494     colContext = QColor::enterAllocContext();
00495     color.setRgb( col.rgb() );
00496     color.alloc();
00497     QColor::leaveAllocContext();
00498 
00499     QPainter painter;
00500 
00501     painter.begin( this );
00502     drawContents( &painter );
00503     painter.end();
00504 }
00505 
00506 void KColorPatch::drawContents( QPainter *painter )
00507 {
00508     painter->setPen( color );
00509     painter->setBrush( QBrush( color ) );
00510     painter->drawRect( contentsRect() );
00511 }
00512 
00513 void KColorPatch::mouseMoveEvent( QMouseEvent *e )
00514 {
00515         // Drag color object
00516         if( !(e->state() && LeftButton)) return;
00517     KColorDrag *d = new KColorDrag( color, this);
00518     d->dragCopy();
00519 }
00520 
00521 void KColorPatch::dragEnterEvent( QDragEnterEvent *event)
00522 {
00523      event->accept( KColorDrag::canDecode( event));
00524 }
00525 
00526 void KColorPatch::dropEvent( QDropEvent *event)
00527 {
00528      QColor c;
00529      if( KColorDrag::decode( event, c)) {
00530       setColor( c);
00531       emit colorChanged( c);
00532      }
00533 }
00534 
00535 class KPaletteTable::KPaletteTablePrivate
00536 {
00537 public:
00538     QMap<QString,QColor> m_namedColorMap;
00539 };
00540 
00541 KPaletteTable::KPaletteTable( QWidget *parent, int minWidth, int cols)
00542     : QWidget( parent ), mMinWidth(minWidth), mCols(cols)
00543 {
00544   d = new KPaletteTablePrivate;
00545   
00546   cells = 0;
00547   mPalette = 0;
00548   i18n_customColors = i18n("* Custom Colors *");
00549   i18n_recentColors = i18n("* Recent Colors *");
00550   i18n_namedColors  = i18n("Named Colors");
00551 
00552   QStringList paletteList = KPalette::getPaletteList();
00553   paletteList.remove(customColors);
00554   paletteList.remove(recentColors);
00555   paletteList.prepend(i18n_customColors);
00556   paletteList.prepend(i18n_recentColors);
00557   paletteList.append( i18n_namedColors );
00558 
00559   QVBoxLayout *layout = new QVBoxLayout( this );
00560 
00561   combo = new QComboBox( false, this );
00562   combo->insertStringList( paletteList );
00563   layout->addWidget(combo);
00564 
00565   sv = new QScrollView( this );
00566   QSize cellSize = QSize( mMinWidth, 120);
00567   sv->setHScrollBarMode( QScrollView::AlwaysOff);
00568   sv->setVScrollBarMode( QScrollView::AlwaysOn);
00569   QSize minSize = QSize(sv->verticalScrollBar()->width(), 0);
00570   minSize += QSize(sv->frameWidth(), 0);
00571   minSize += QSize(cellSize);
00572   sv->setFixedSize(minSize);
00573   layout->addWidget(sv);
00574 
00575   mNamedColorList = new KListBox( this, "namedColorList", 0 );
00576   mNamedColorList->setFixedSize(minSize);
00577   mNamedColorList->hide();
00578   layout->addWidget(mNamedColorList);
00579   connect( mNamedColorList, SIGNAL(highlighted( const QString & )),
00580        this, SLOT( slotColorTextSelected( const QString & )) );
00581 
00582   setFixedSize( sizeHint());
00583   connect( combo, SIGNAL(activated(const QString &)),
00584     this, SLOT(slotSetPalette( const QString &)));
00585 }
00586 
00587 KPaletteTable::~KPaletteTable()
00588 {
00589    delete mPalette;
00590    delete d;
00591 }
00592 
00593 QString
00594 KPaletteTable::palette() const
00595 {
00596   return combo->currentText();
00597 }
00598 
00599 
00600 static const char * const *namedColorFilePath( void )
00601 {
00602   //
00603   // 2000-02-05 Espen Sand.
00604   // Add missing filepaths here. Make sure the last entry is 0!
00605   //
00606   static const char * const path[] =
00607   {
00608 #ifdef X11_RGBFILE
00609     X11_RGBFILE,
00610 #endif
00611     "/usr/X11R6/lib/X11/rgb.txt",
00612     "/usr/openwin/lib/X11/rgb.txt", // for Solaris.
00613     0
00614   };
00615   return path;
00616 }
00617 
00618 
00619 
00620 
00621 void
00622 KPaletteTable::readNamedColor( void )
00623 {
00624   if( mNamedColorList->count() != 0 )
00625   {
00626     return; // Strings already present
00627   }
00628 
00629   KGlobal::locale()->insertCatalogue("kdelibs_colors");
00630 
00631   //
00632   // Code somewhat inspired by KPalette.
00633   //
00634 
00635   const char * const *path = namedColorFilePath();
00636   for( int i=0; path[i]; ++i )
00637   {
00638     QFile paletteFile( path[i] );
00639     if( !paletteFile.open( IO_ReadOnly ) )
00640     {
00641       continue;
00642     }
00643 
00644     QString line;
00645     QStringList list;
00646     while( paletteFile.readLine( line, 100 ) != -1 )
00647     {
00648       int red, green, blue;
00649       int pos = 0;
00650 
00651       if( sscanf(line.ascii(), "%d %d %d%n", &red, &green, &blue, &pos ) == 3 )
00652       {
00653     //
00654     // Remove duplicates. Every name with a space and every name
00655     // that start with "gray".
00656     //
00657     QString name = line.mid(pos).stripWhiteSpace();
00658     if( name.isNull() || name.find(' ') != -1 ||
00659         name.find( "gray" ) != -1 ||  name.find( "grey" ) != -1 )
00660     {
00661       continue;
00662     }
00663 
00664         const QColor color ( red, green, blue );
00665         if ( color.isValid() )
00666         {
00667             const QString colorName( i18n("color", name.latin1() ) );
00668             list.append( colorName );
00669             d->m_namedColorMap[ colorName ] = color;
00670         }
00671       }
00672     }
00673 
00674     list.sort();
00675     mNamedColorList->insertStringList( list );
00676     break;
00677   }
00678 
00679   if( mNamedColorList->count() == 0 )
00680   {
00681     //
00682     // Give the error dialog box a chance to center above the
00683     // widget (or dialog). If we had displayed it now we could get a
00684     // situation where the (modal) error dialog box pops up first
00685     // preventing the real dialog to become visible until the
00686     // error dialog box is removed (== bad UI).
00687     //
00688     QTimer::singleShot( 10, this, SLOT(slotShowNamedColorReadError()) );
00689   }
00690 }
00691 
00692 
00693 void
00694 KPaletteTable::slotShowNamedColorReadError( void )
00695 {
00696   if( mNamedColorList->count() == 0 )
00697   {
00698     QString msg = i18n(""
00699       "Unable to read X11 RGB color strings. The following "
00700       "file location(s) were examined:\n");
00701 
00702     const char * const *path = namedColorFilePath();
00703     for( int i=0; path[i]; ++i )
00704     {
00705       msg += path[i];
00706       msg += "\n";
00707     }
00708     KMessageBox::sorry( this, msg );
00709   }
00710 }
00711 
00712 
00713 //
00714 // 2000-02-12 Espen Sand
00715 // Set the color in two steps. The setPalette() slot will not emit a signal
00716 // with the current color setting. The reason is that setPalette() is used
00717 // by the color selector dialog on startup. In the color selector dialog
00718 // we normally want to display a startup color which we specify
00719 // when the dialog is started. The slotSetPalette() slot below will
00720 // set the palette and then use the information to emit a signal with the
00721 // new color setting. It is only used by the combobox widget.
00722 //
00723 void
00724 KPaletteTable::slotSetPalette( const QString &_paletteName )
00725 {
00726   setPalette( _paletteName );
00727   if( mNamedColorList->isVisible() )
00728   {
00729     int item = mNamedColorList->currentItem();
00730     mNamedColorList->setCurrentItem( item < 0 ? 0 : item );
00731     slotColorTextSelected( mNamedColorList->currentText() );
00732   }
00733   else
00734   {
00735     slotColorCellSelected(0); // FIXME: We need to save the current value!!
00736   }
00737 }
00738 
00739 
00740 void
00741 KPaletteTable::setPalette( const QString &_paletteName )
00742 {
00743   QString paletteName( _paletteName);
00744   if (paletteName.isEmpty())
00745      paletteName = i18n_recentColors;
00746 
00747   if (combo->currentText() != paletteName)
00748   {
00749      bool found = false;
00750      for(int i = 0; i < combo->count(); i++)
00751      {
00752         if (combo->text(i) == paletteName)
00753         {
00754            combo->setCurrentItem(i);
00755            found = true;
00756            break;
00757         }
00758      }
00759      if (!found)
00760      {
00761         combo->insertItem(paletteName);
00762         combo->setCurrentItem(combo->count()-1);
00763      }
00764   }
00765 
00766   if (paletteName == i18n_customColors)
00767      paletteName = customColors;
00768   else if (paletteName == i18n_recentColors)
00769      paletteName = recentColors;
00770 
00771 
00772   //
00773   // 2000-02-12 Espen Sand
00774   // The palette mode "i18n_namedColors" does not use the KPalette class.
00775   // In fact, 'mPalette' and 'cells' are 0 when in this mode. The reason
00776   // for this is maninly that KPalette reads from and writes to files using
00777   // "locate()". The colors used in "i18n_namedColors" mode comes from the
00778   // X11 diretory and is not writable. I don't think this fit in KPalette.
00779   //
00780   if( !mPalette || mPalette->name() != paletteName )
00781   {
00782     if( paletteName == i18n_namedColors )
00783     {
00784       sv->hide();
00785       mNamedColorList->show();
00786       readNamedColor();
00787 
00788       delete cells; cells = 0;
00789       delete mPalette; mPalette = 0;
00790     }
00791     else
00792     {
00793       mNamedColorList->hide();
00794       sv->show();
00795 
00796       delete cells;
00797       delete mPalette;
00798       mPalette = new KPalette(paletteName);
00799       int rows = (mPalette->nrColors()+mCols-1) / mCols;
00800       if (rows < 1) rows = 1;
00801       cells = new KColorCells( sv->viewport(), rows, mCols);
00802       cells->setShading(false);
00803       cells->setAcceptDrags(false);
00804       QSize cellSize = QSize( mMinWidth, mMinWidth * rows / mCols);
00805       cells->setFixedSize( cellSize );
00806       for( int i = 0; i < mPalette->nrColors(); i++)
00807       {
00808         cells->setColor( i, mPalette->color(i) );
00809       }
00810       connect( cells, SIGNAL( colorSelected( int ) ),
00811            SLOT( slotColorCellSelected( int ) ) );
00812       connect( cells, SIGNAL( colorDoubleClicked( int ) ),
00813            SLOT( slotColorCellDoubleClicked( int ) ) );
00814       sv->addChild( cells );
00815       cells->show();
00816       sv->updateScrollBars();
00817     }
00818   }
00819 }
00820 
00821 
00822 
00823 void
00824 KPaletteTable::slotColorCellSelected( int col )
00825 {
00826   if (!mPalette || (col >= mPalette->nrColors()))
00827      return;
00828   emit colorSelected( mPalette->color(col), mPalette->colorName(col) );
00829 }
00830 
00831 void
00832 KPaletteTable::slotColorCellDoubleClicked( int col )
00833 {
00834   if (!mPalette || (col >= mPalette->nrColors()))
00835      return;
00836   emit colorDoubleClicked( mPalette->color(col), mPalette->colorName(col) );
00837 }
00838 
00839 
00840 void
00841 KPaletteTable::slotColorTextSelected( const QString &colorText )
00842 {
00843   emit colorSelected( d->m_namedColorMap[ colorText ], colorText );
00844 }
00845 
00846 
00847 void
00848 KPaletteTable::addToCustomColors( const QColor &color)
00849 {
00850   setPalette(i18n_customColors);
00851   mPalette->addColor( color );
00852   mPalette->save();
00853   delete mPalette;
00854   mPalette = 0;
00855   setPalette(i18n_customColors);
00856 }
00857 
00858 void
00859 KPaletteTable::addToRecentColors( const QColor &color)
00860 {
00861   //
00862   // 2000-02-12 Espen Sand.
00863   // The 'mPalette' is always 0 when current mode is i18n_namedColors
00864   //
00865   bool recentIsSelected = false;
00866   if ( mPalette && mPalette->name() == recentColors)
00867   {
00868      delete mPalette;
00869      mPalette = 0;
00870      recentIsSelected = true;
00871   }
00872   KPalette *recentPal = new KPalette(recentColors);
00873   if (recentPal->findColor(color) == -1)
00874   {
00875      recentPal->addColor( color );
00876      recentPal->save();
00877   }
00878   delete recentPal;
00879   if (recentIsSelected)
00880      setPalette(i18n_recentColors);
00881 }
00882 
00883 class KColorDialog::KColorDialogPrivate {
00884 public:
00885     KPaletteTable *table;
00886     QString originalPalette;
00887     bool bRecursion;
00888     bool bEditRgb;
00889     bool bEditHsv;
00890     bool bEditHtml;
00891     bool bColorPicking;
00892     QLabel *colorName;
00893     QLineEdit *htmlName;
00894     KColorSpinBox *hedit;
00895     KColorSpinBox *sedit;
00896     KColorSpinBox *vedit;
00897     KColorSpinBox *redit;
00898     KColorSpinBox *gedit;
00899     KColorSpinBox *bedit;
00900     KColorPatch *patch;
00901     KHSSelector *hsSelector;
00902     KPalette *palette;
00903     KValueSelector *valuePal;
00904     QVBoxLayout* l_right;
00905     QGridLayout* tl_layout;
00906     QCheckBox *cbDefaultColor;
00907     KColor defaultColor;
00908     KColor selColor;
00909 #ifdef Q_WS_X11
00910     QX11EventFilter oldfilter;
00911 #endif
00912 };
00913 
00914 
00915 KColorDialog::KColorDialog( QWidget *parent, const char *name, bool modal )
00916   :KDialogBase( parent, name, modal, i18n("Select Color"),
00917         modal ? Ok|Cancel : Close,
00918         Ok, true )
00919 {
00920   d = new KColorDialogPrivate;
00921   d->bRecursion = true;
00922   d->bColorPicking = false;
00923 #ifdef Q_WS_X11
00924   d->oldfilter = 0;
00925 #endif
00926   d->cbDefaultColor = 0L;
00927   connect( this, SIGNAL(okClicked(void)),this,SLOT(slotWriteSettings(void)));
00928   connect( this, SIGNAL(closeClicked(void)),this,SLOT(slotWriteSettings(void)));
00929 
00930   QLabel *label;
00931 
00932   //
00933   // Create the top level page and its layout
00934   //
00935   QWidget *page = new QWidget( this );
00936   setMainWidget( page );
00937 
00938   QGridLayout *tl_layout = new QGridLayout( page, 3, 3, 0, spacingHint() );
00939   d->tl_layout = tl_layout;
00940   tl_layout->addColSpacing( 1, spacingHint() * 2 );
00941 
00942   //
00943   // the more complicated part: the left side
00944   // add a V-box
00945   //
00946   QVBoxLayout *l_left = new QVBoxLayout();
00947   tl_layout->addLayout(l_left, 0, 0);
00948 
00949   //
00950   // add a H-Box for the XY-Selector and a grid for the
00951   // entry fields
00952   //
00953   QHBoxLayout *l_ltop = new QHBoxLayout();
00954   l_left->addLayout(l_ltop);
00955 
00956   // a little space between
00957   l_left->addSpacing(10);
00958 
00959   QGridLayout *l_lbot = new QGridLayout(3, 6);
00960   l_left->addLayout(l_lbot);
00961 
00962   //
00963   // the palette and value selector go into the H-box
00964   //
00965   d->hsSelector = new KHSSelector( page );
00966   d->hsSelector->setMinimumSize(140, 70);
00967   l_ltop->addWidget(d->hsSelector, 8);
00968   connect( d->hsSelector, SIGNAL( valueChanged( int, int ) ),
00969        SLOT( slotHSChanged( int, int ) ) );
00970 
00971   d->valuePal = new KValueSelector( page );
00972   d->valuePal->setMinimumSize(26, 70);
00973   l_ltop->addWidget(d->valuePal, 1);
00974   connect( d->valuePal, SIGNAL( valueChanged( int ) ),
00975        SLOT( slotVChanged( int ) ) );
00976 
00977 
00978   //
00979   // add the HSV fields
00980   //
00981   label = new QLabel( i18n("H:"), page );
00982   label->setAlignment(AlignRight | AlignVCenter);
00983   l_lbot->addWidget(label, 0, 2);
00984   d->hedit = new KColorSpinBox( 0, 359, 1, page );
00985   d->hedit->setValidator( new QIntValidator( d->hedit ) );
00986   l_lbot->addWidget(d->hedit, 0, 3);
00987   connect( d->hedit, SIGNAL( valueChanged(int) ),
00988     SLOT( slotHSVChanged() ) );
00989 
00990   label = new QLabel( i18n("S:"), page );
00991   label->setAlignment(AlignRight | AlignVCenter);
00992   l_lbot->addWidget(label, 1, 2);
00993   d->sedit = new KColorSpinBox( 0, 255, 1, page );
00994   d->sedit->setValidator( new QIntValidator( d->sedit ) );
00995   l_lbot->addWidget(d->sedit, 1, 3);
00996   connect( d->sedit, SIGNAL( valueChanged(int) ),
00997     SLOT( slotHSVChanged() ) );
00998 
00999   label = new QLabel( i18n("V:"), page );
01000   label->setAlignment(AlignRight | AlignVCenter);
01001   l_lbot->addWidget(label, 2, 2);
01002   d->vedit = new KColorSpinBox( 0, 255, 1, page );
01003   d->vedit->setValidator( new QIntValidator( d->vedit ) );
01004   l_lbot->addWidget(d->vedit, 2, 3);
01005   connect( d->vedit, SIGNAL( valueChanged(int) ),
01006     SLOT( slotHSVChanged() ) );
01007 
01008   //
01009   // add the RGB fields
01010   //
01011   label = new QLabel( i18n("R:"), page );
01012   label->setAlignment(AlignRight | AlignVCenter);
01013   l_lbot->addWidget(label, 0, 4);
01014   d->redit = new KColorSpinBox( 0, 255, 1, page );
01015   d->redit->setValidator( new QIntValidator( d->redit ) );
01016   l_lbot->addWidget(d->redit, 0, 5);
01017   connect( d->redit, SIGNAL( valueChanged(int) ),
01018     SLOT( slotRGBChanged() ) );
01019 
01020   label = new QLabel( i18n("G:"), page );
01021   label->setAlignment(AlignRight | AlignVCenter);
01022   l_lbot->addWidget( label, 1, 4);
01023   d->gedit = new KColorSpinBox( 0, 255,1, page );
01024   d->gedit->setValidator( new QIntValidator( d->gedit ) );
01025   l_lbot->addWidget(d->gedit, 1, 5);
01026   connect( d->gedit, SIGNAL( valueChanged(int) ),
01027     SLOT( slotRGBChanged() ) );
01028 
01029   label = new QLabel( i18n("B:"), page );
01030   label->setAlignment(AlignRight | AlignVCenter);
01031   l_lbot->addWidget(label, 2, 4);
01032   d->bedit = new KColorSpinBox( 0, 255, 1, page );
01033   d->bedit->setValidator( new QIntValidator( d->bedit ) );
01034   l_lbot->addWidget(d->bedit, 2, 5);
01035   connect( d->bedit, SIGNAL( valueChanged(int) ),
01036     SLOT( slotRGBChanged() ) );
01037 
01038   //
01039   // the entry fields should be wide enough to hold 8888888
01040   //
01041   int w = d->hedit->fontMetrics().width("8888888");
01042   d->hedit->setFixedWidth(w);
01043   d->sedit->setFixedWidth(w);
01044   d->vedit->setFixedWidth(w);
01045 
01046   d->redit->setFixedWidth(w);
01047   d->gedit->setFixedWidth(w);
01048   d->bedit->setFixedWidth(w);
01049 
01050   //
01051   // add a layout for the right side
01052   //
01053   d->l_right = new QVBoxLayout;
01054   tl_layout->addLayout(d->l_right, 0, 2);
01055 
01056   //
01057   // Add the palette table
01058   //
01059   d->table = new KPaletteTable( page );
01060   d->l_right->addWidget(d->table, 10);
01061 
01062   connect( d->table, SIGNAL( colorSelected( const QColor &, const QString & ) ),
01063        SLOT( slotColorSelected( const QColor &, const QString & ) ) );
01064 
01065   connect(
01066     d->table,
01067     SIGNAL( colorDoubleClicked( const QColor &, const QString & ) ),
01068     SLOT( slotColorDoubleClicked( const QColor &, const QString & ) )
01069   );
01070   // Store the default value for saving time.
01071   d->originalPalette = d->table->palette();
01072 
01073   //
01074   // a little space between
01075   //
01076   d->l_right->addSpacing(10);
01077 
01078   QHBoxLayout *l_hbox = new QHBoxLayout( d->l_right );
01079 
01080   //
01081   // The add to custom colors button
01082   //
01083   QPushButton *button = new QPushButton( page );
01084   button->setText(i18n("&Add to Custom Colors"));
01085   l_hbox->addWidget(button, 0, AlignLeft);
01086   connect( button, SIGNAL( clicked()), SLOT( slotAddToCustomColors()));
01087 
01088   //
01089   // The color picker button
01090   //
01091   button = new QPushButton( page );
01092   button->setPixmap( BarIcon("colorpicker"));
01093   l_hbox->addWidget(button, 0, AlignHCenter );
01094   connect( button, SIGNAL( clicked()), SLOT( slotColorPicker()));
01095 
01096   //
01097   // a little space between
01098   //
01099   d->l_right->addSpacing(10);
01100 
01101   //
01102   // and now the entry fields and the patch (=colored box)
01103   //
01104   QGridLayout *l_grid = new QGridLayout( d->l_right, 2, 3);
01105 
01106   l_grid->setColStretch(2, 1);
01107 
01108   label = new QLabel( page );
01109   label->setText(i18n("Name:"));
01110   l_grid->addWidget(label, 0, 1, AlignLeft);
01111 
01112   d->colorName = new QLabel( page );
01113   l_grid->addWidget(d->colorName, 0, 2, AlignLeft);
01114 
01115   label = new QLabel( page );
01116   label->setText(i18n("HTML:"));
01117   l_grid->addWidget(label, 1, 1, AlignLeft);
01118 
01119   d->htmlName = new QLineEdit( page );
01120   d->htmlName->setMaxLength( 13 ); // Qt's QColor allows 12 hexa-digits
01121   d->htmlName->setText("#FFFFFF"); // But HTML uses only 6, so do not worry about the size
01122   w = d->htmlName->fontMetrics().width(QString::fromLatin1("#DDDDDDD"));
01123   d->htmlName->setFixedWidth(w);
01124   l_grid->addWidget(d->htmlName, 1, 2, AlignLeft);
01125 
01126   connect( d->htmlName, SIGNAL( textChanged(const QString &) ),
01127       SLOT( slotHtmlChanged() ) );
01128 
01129   d->patch = new KColorPatch( page );
01130   d->patch->setFixedSize(48, 48);
01131   l_grid->addMultiCellWidget(d->patch, 0, 1, 0, 0, AlignHCenter | AlignVCenter);
01132   connect( d->patch, SIGNAL( colorChanged( const QColor&)),
01133        SLOT( setColor( const QColor&)));
01134 
01135   tl_layout->activate();
01136   page->setMinimumSize( page->sizeHint() );
01137 
01138   readSettings();
01139   d->bRecursion = false;
01140   d->bEditHsv = false;
01141   d->bEditRgb = false;
01142   d->bEditHtml = false;
01143 
01144   disableResize();
01145   KColor col;
01146   col.setHsv( 0, 0, 255 );
01147   _setColor( col );
01148 
01149   d->htmlName->installEventFilter(this);
01150   d->hsSelector->installEventFilter(this);
01151   d->hsSelector->setAcceptDrops(true);
01152 }
01153 
01154 KColorDialog::~KColorDialog()
01155 {
01156 #ifdef Q_WS_X11
01157     if (d->bColorPicking)
01158         qt_set_x11_event_filter(d->oldfilter);
01159 #endif
01160     delete d;
01161 }
01162 
01163 bool
01164 KColorDialog::eventFilter( QObject *obj, QEvent *ev )
01165 {
01166     if ((obj == d->htmlName) || (obj == d->hsSelector))
01167     switch(ev->type())
01168     {
01169       case QEvent::DragEnter:
01170       case QEvent::DragMove:
01171       case QEvent::DragLeave:
01172       case QEvent::Drop:
01173       case QEvent::DragResponse:
01174             qApp->sendEvent(d->patch, ev);
01175             return true;
01176       default:
01177             break;
01178     }
01179     return KDialogBase::eventFilter(obj, ev);
01180 }
01181 
01182 void
01183 KColorDialog::setDefaultColor( const QColor& col )
01184 {
01185     if ( !d->cbDefaultColor )
01186     {
01187         //
01188         // a little space between
01189         //
01190         d->l_right->addSpacing(10);
01191 
01192         //
01193         // and the "default color" checkbox, under all items on the right side
01194         //
01195         d->cbDefaultColor = new QCheckBox( i18n( "Default color" ), mainWidget() );
01196         d->cbDefaultColor->setChecked(true);
01197 
01198         d->l_right->addWidget( d->cbDefaultColor );
01199 
01200         mainWidget()->setMaximumSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX ); // cancel setFixedSize()
01201         d->tl_layout->activate();
01202         mainWidget()->setMinimumSize( mainWidget()->sizeHint() );
01203         disableResize();
01204 
01205         connect( d->cbDefaultColor, SIGNAL( clicked() ), SLOT( slotDefaultColorClicked() ) );
01206     }
01207 
01208     d->defaultColor = col;
01209 
01210     slotDefaultColorClicked();
01211 }
01212 
01213 QColor KColorDialog::defaultColor() const
01214 {
01215     return d->defaultColor;
01216 }
01217 
01218 void KColorDialog::slotDefaultColorClicked()
01219 {
01220     if ( d->cbDefaultColor->isChecked() )
01221     {
01222         d->selColor = d->defaultColor;
01223         showColor( d->selColor, i18n( "-default-" ) );
01224     } else
01225     {
01226         showColor( d->selColor, QString::null );
01227     }
01228 }
01229 
01230 void
01231 KColorDialog::readSettings()
01232 {
01233   KConfig* config = KGlobal::config();
01234 
01235   QString oldgroup = config->group();
01236 
01237   config->setGroup("Colors");
01238   QString palette = config->readEntry("CurrentPalette");
01239   d->table->setPalette(palette);
01240   config->setGroup( oldgroup );
01241 }
01242 
01243 void
01244 KColorDialog::slotWriteSettings()
01245 {
01246   KConfig* config = KGlobal::config();
01247   config->setGroup("Colors");
01248   QString palette = d->table->palette();
01249   if (!config->hasDefault("CurrentPalette") &&
01250       (d->table->palette() == d->originalPalette))
01251   {
01252      config->revertToDefault("CurrentPalette");
01253   }
01254   else
01255   {
01256      config->writeEntry("CurrentPalette", d->table->palette());
01257   }
01258 }
01259 
01260 QColor
01261 KColorDialog::color() const
01262 {
01263   if ( d->cbDefaultColor && d->cbDefaultColor->isChecked() )
01264      return QColor();
01265   if ( d->selColor.isValid() )
01266     d->table->addToRecentColors( d->selColor );
01267   return d->selColor;
01268 }
01269 
01270 void KColorDialog::setColor( const QColor &col )
01271 {
01272   _setColor( col );
01273 }
01274 
01275 //
01276 // static function to display dialog and return color
01277 //
01278 int KColorDialog::getColor( QColor &theColor, QWidget *parent )
01279 {
01280   KColorDialog dlg( parent, "Color Selector", true );
01281   if ( theColor.isValid() )
01282     dlg.setColor( theColor );
01283   int result = dlg.exec();
01284 
01285   if ( result == Accepted )
01286   {
01287     theColor = dlg.color();
01288   }
01289 
01290   return result;
01291 }
01292 
01293 //
01294 // static function to display dialog and return color
01295 //
01296 int KColorDialog::getColor( QColor &theColor, const QColor& defaultCol, QWidget *parent )
01297 {
01298   KColorDialog dlg( parent, "Color Selector", true );
01299   dlg.setDefaultColor( defaultCol );
01300   dlg.setColor( theColor );
01301   int result = dlg.exec();
01302 
01303   if ( result == Accepted )
01304     theColor = dlg.color();
01305 
01306   return result;
01307 }
01308 
01309 void KColorDialog::slotRGBChanged( void )
01310 {
01311   if (d->bRecursion) return;
01312   int red = d->redit->value();
01313   int grn = d->gedit->value();
01314   int blu = d->bedit->value();
01315 
01316   if ( red > 255 || red < 0 ) return;
01317   if ( grn > 255 || grn < 0 ) return;
01318   if ( blu > 255 || blu < 0 ) return;
01319 
01320   KColor col;
01321   col.setRgb( red, grn, blu );
01322   d->bEditRgb = true;
01323   _setColor( col );
01324   d->bEditRgb = false;
01325 }
01326 
01327 void KColorDialog::slotHtmlChanged( void )
01328 {
01329   if (d->bRecursion || d->htmlName->text().isEmpty()) return;
01330 
01331   QString strColor( d->htmlName->text() );
01332 
01333   // Assume that a user does not want to type the # all the time
01334   if ( strColor[0] != '#' )
01335   {
01336     bool signalsblocked = d->htmlName->signalsBlocked();
01337     d->htmlName->blockSignals(true);
01338     strColor.prepend("#");
01339     d->htmlName->setText(strColor);
01340     d->htmlName->blockSignals(signalsblocked);
01341   }
01342 
01343   const QColor color( strColor );
01344 
01345   if ( color.isValid() )
01346   {
01347     KColor col( color );
01348     d->bEditHtml = true;
01349     _setColor( col );
01350     d->bEditHtml = false;
01351   }
01352 }
01353 
01354 void KColorDialog::slotHSVChanged( void )
01355 {
01356   if (d->bRecursion) return;
01357   int hue = d->hedit->value();
01358   int sat = d->sedit->value();
01359   int val = d->vedit->value();
01360 
01361   if ( hue > 359 || hue < 0 ) return;
01362   if ( sat > 255 || sat < 0 ) return;
01363   if ( val > 255 || val < 0 ) return;
01364 
01365   KColor col;
01366   col.setHsv( hue, sat, val );
01367   d->bEditHsv = true;
01368   _setColor( col );
01369   d->bEditHsv = false;
01370 }
01371 
01372 void KColorDialog::slotHSChanged( int h, int s )
01373 {
01374   int _h, _s, v;
01375   d->selColor.hsv(&_h, &_s, &v);
01376   if (v < 0)
01377      v = 0;
01378   KColor col;
01379   col.setHsv( h, s, v );
01380   _setColor( col );
01381 }
01382 
01383 void KColorDialog::slotVChanged( int v )
01384 {
01385   int h, s, _v;
01386   d->selColor.hsv(&h, &s, &_v);
01387   KColor col;
01388   col.setHsv( h, s, v );
01389   _setColor( col );
01390 }
01391 
01392 void KColorDialog::slotColorSelected( const QColor &color )
01393 {
01394   _setColor( color );
01395 }
01396 
01397 void KColorDialog::slotAddToCustomColors( )
01398 {
01399   d->table->addToCustomColors( d->selColor );
01400 }
01401 
01402 void KColorDialog::slotColorSelected( const QColor &color, const QString &name )
01403 {
01404   _setColor( color, name);
01405 }
01406 
01407 void KColorDialog::slotColorDoubleClicked
01408 (
01409   const QColor  & color,
01410   const QString & name
01411 )
01412 {
01413   _setColor(color, name);
01414   accept();
01415 }
01416 
01417 void KColorDialog::_setColor(const KColor &color, const QString &name)
01418 {
01419   if (color.isValid())
01420   {
01421      if (d->cbDefaultColor && d->cbDefaultColor->isChecked())
01422         d->cbDefaultColor->setChecked(false);
01423      d->selColor = color;
01424   }
01425   else
01426   {
01427      if (d->cbDefaultColor && d->cbDefaultColor->isChecked())
01428         d->cbDefaultColor->setChecked(true);
01429      d->selColor = d->defaultColor;
01430   }
01431 
01432   showColor( d->selColor, name );
01433 
01434   emit colorSelected( d->selColor );
01435 }
01436 
01437 // show but don't set into selColor, nor emit colorSelected
01438 void KColorDialog::showColor( const KColor &color, const QString &name )
01439 {
01440   d->bRecursion = true;
01441 
01442   if (name.isEmpty())
01443      d->colorName->setText( i18n("-unnamed-"));
01444   else
01445      d->colorName->setText( name );
01446 
01447   d->patch->setColor( color );
01448 
01449   setRgbEdit( color );
01450   setHsvEdit( color );
01451   setHtmlEdit( color );
01452 
01453   int h, s, v;
01454   color.hsv( &h, &s, &v );
01455   d->hsSelector->setValues( h, s );
01456   d->valuePal->blockSignals(true);
01457   d->valuePal->setHue( h );
01458   d->valuePal->setSaturation( s );
01459   d->valuePal->setValue( v );
01460   d->valuePal->updateContents();
01461   d->valuePal->blockSignals(false);
01462   d->valuePal->repaint( false );
01463   d->bRecursion = false;
01464 }
01465 
01466 
01467 static QWidget *kde_color_dlg_widget = 0;
01468 
01469 #ifdef Q_WS_X11
01470 static int kde_color_dlg_handler(XEvent *event)
01471 {
01472     if (event->type == ButtonRelease)
01473     {
01474         QMouseEvent e( QEvent::MouseButtonRelease, QPoint(),
01475                        QPoint(event->xmotion.x_root, event->xmotion.y_root) , 0, 0 );
01476         QApplication::sendEvent( kde_color_dlg_widget, &e );
01477         return true;
01478     }
01479     return false;
01480 }
01481 #endif
01482 void
01483 KColorDialog::slotColorPicker()
01484 {
01485     d->bColorPicking = true;
01486 #ifdef Q_WS_X11
01487     d->oldfilter = qt_set_x11_event_filter(kde_color_dlg_handler);
01488 #endif
01489     kde_color_dlg_widget = this;
01490     grabMouse( crossCursor );
01491     grabKeyboard();
01492 }
01493 
01494 void
01495 KColorDialog::mouseReleaseEvent( QMouseEvent *e )
01496 {
01497   if (d->bColorPicking)
01498   {
01499      d->bColorPicking = false;
01500 #ifdef Q_WS_X11
01501      qt_set_x11_event_filter(d->oldfilter);
01502      d->oldfilter = 0;
01503 #endif
01504      releaseMouse();
01505      releaseKeyboard();
01506      _setColor( grabColor( e->globalPos() ) );
01507      return;
01508   }
01509   KDialogBase::mouseReleaseEvent( e );
01510 }
01511 
01512 QColor
01513 KColorDialog::grabColor(const QPoint &p)
01514 {
01515     QWidget *desktop = QApplication::desktop();
01516     QPixmap pm = QPixmap::grabWindow( desktop->winId(), p.x(), p.y(), 1, 1);
01517     QImage i = pm.convertToImage();
01518     return i.pixel(0,0);
01519 }
01520 
01521 void
01522 KColorDialog::keyPressEvent( QKeyEvent *e )
01523 {
01524   if (d->bColorPicking)
01525   {
01526      if (e->key() == Key_Escape)
01527      {
01528         d->bColorPicking = false;
01529 #ifdef Q_WS_X11
01530         qt_set_x11_event_filter(d->oldfilter);
01531         d->oldfilter = 0;
01532 #endif
01533         releaseMouse();
01534         releaseKeyboard();
01535      }
01536      e->accept();
01537      return;
01538   }
01539   KDialogBase::keyPressEvent( e );
01540 }
01541 
01542 void KColorDialog::setRgbEdit( const KColor &col )
01543 {
01544   if (d->bEditRgb) return;
01545   int r, g, b;
01546   col.rgb( &r, &g, &b );
01547 
01548   d->redit->setValue( r );
01549   d->gedit->setValue( g );
01550   d->bedit->setValue( b );
01551 }
01552 
01553 void KColorDialog::setHtmlEdit( const KColor &col )
01554 {
01555   if (d->bEditHtml) return;
01556   int r, g, b;
01557   col.rgb( &r, &g, &b );
01558   QString num;
01559 
01560   num.sprintf("#%02X%02X%02X", r,g,b);
01561   d->htmlName->setText( num );
01562 }
01563 
01564 
01565 void KColorDialog::setHsvEdit( const KColor &col )
01566 {
01567   if (d->bEditHsv) return;
01568   int h, s, v;
01569   col.hsv( &h, &s, &v );
01570 
01571   d->hedit->setValue( h );
01572   d->sedit->setValue( s );
01573   d->vedit->setValue( v );
01574 }
01575 
01576 void KHSSelector::virtual_hook( int id, void* data )
01577 { KXYSelector::virtual_hook( id, data ); }
01578 
01579 void KValueSelector::virtual_hook( int id, void* data )
01580 { KSelector::virtual_hook( id, data ); }
01581 
01582 void KPaletteTable::virtual_hook( int, void* )
01583 { /*BASE::virtual_hook( id, data );*/ }
01584 
01585 void KColorCells::virtual_hook( int, void* )
01586 { /*BASE::virtual_hook( id, data );*/ }
01587 
01588 void KColorPatch::virtual_hook( int, void* )
01589 { /*BASE::virtual_hook( id, data );*/ }
01590 
01591 void KColorDialog::virtual_hook( int id, void* data )
01592 { KDialogBase::virtual_hook( id, data ); }
01593 
01594 
01595 #include "kcolordialog.moc"
01596 //#endif
KDE Logo
This file is part of the documentation for kdeui Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Jul 20 13:48:31 2006 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003