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