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 ? Help|Ok|Cancel : Help|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   setHelp( QString::fromLatin1("kcolordialog.html"), QString::null );
00928   connect( this, SIGNAL(okClicked(void)),this,SLOT(slotWriteSettings(void)));
00929   connect( this, SIGNAL(closeClicked(void)),this,SLOT(slotWriteSettings(void)));
00930 
00931   QLabel *label;
00932 
00933   //
00934   // Create the top level page and its layout
00935   //
00936   QWidget *page = new QWidget( this );
00937   setMainWidget( page );
00938 
00939   QGridLayout *tl_layout = new QGridLayout( page, 3, 3, 0, spacingHint() );
00940   d->tl_layout = tl_layout;
00941   tl_layout->addColSpacing( 1, spacingHint() * 2 );
00942 
00943   //
00944   // the more complicated part: the left side
00945   // add a V-box
00946   //
00947   QVBoxLayout *l_left = new QVBoxLayout();
00948   tl_layout->addLayout(l_left, 0, 0);
00949 
00950   //
00951   // add a H-Box for the XY-Selector and a grid for the
00952   // entry fields
00953   //
00954   QHBoxLayout *l_ltop = new QHBoxLayout();
00955   l_left->addLayout(l_ltop);
00956 
00957   // a little space between
00958   l_left->addSpacing(10);
00959 
00960   QGridLayout *l_lbot = new QGridLayout(3, 6);
00961   l_left->addLayout(l_lbot);
00962 
00963   //
00964   // the palette and value selector go into the H-box
00965   //
00966   d->hsSelector = new KHSSelector( page );
00967   d->hsSelector->setMinimumSize(140, 70);
00968   l_ltop->addWidget(d->hsSelector, 8);
00969   connect( d->hsSelector, SIGNAL( valueChanged( int, int ) ),
00970        SLOT( slotHSChanged( int, int ) ) );
00971 
00972   d->valuePal = new KValueSelector( page );
00973   d->valuePal->setMinimumSize(26, 70);
00974   l_ltop->addWidget(d->valuePal, 1);
00975   connect( d->valuePal, SIGNAL( valueChanged( int ) ),
00976        SLOT( slotVChanged( int ) ) );
00977 
00978 
00979   //
00980   // add the HSV fields
00981   //
00982   label = new QLabel( i18n("H:"), page );
00983   label->setAlignment(AlignRight | AlignVCenter);
00984   l_lbot->addWidget(label, 0, 2);
00985   d->hedit = new KColorSpinBox( 0, 359, 1, page );
00986   d->hedit->setValidator( new QIntValidator( d->hedit ) );
00987   l_lbot->addWidget(d->hedit, 0, 3);
00988   connect( d->hedit, SIGNAL( valueChanged(int) ),
00989     SLOT( slotHSVChanged() ) );
00990 
00991   label = new QLabel( i18n("S:"), page );
00992   label->setAlignment(AlignRight | AlignVCenter);
00993   l_lbot->addWidget(label, 1, 2);
00994   d->sedit = new KColorSpinBox( 0, 255, 1, page );
00995   d->sedit->setValidator( new QIntValidator( d->sedit ) );
00996   l_lbot->addWidget(d->sedit, 1, 3);
00997   connect( d->sedit, SIGNAL( valueChanged(int) ),
00998     SLOT( slotHSVChanged() ) );
00999 
01000   label = new QLabel( i18n("V:"), page );
01001   label->setAlignment(AlignRight | AlignVCenter);
01002   l_lbot->addWidget(label, 2, 2);
01003   d->vedit = new KColorSpinBox( 0, 255, 1, page );
01004   d->vedit->setValidator( new QIntValidator( d->vedit ) );
01005   l_lbot->addWidget(d->vedit, 2, 3);
01006   connect( d->vedit, SIGNAL( valueChanged(int) ),
01007     SLOT( slotHSVChanged() ) );
01008 
01009   //
01010   // add the RGB fields
01011   //
01012   label = new QLabel( i18n("R:"), page );
01013   label->setAlignment(AlignRight | AlignVCenter);
01014   l_lbot->addWidget(label, 0, 4);
01015   d->redit = new KColorSpinBox( 0, 255, 1, page );
01016   d->redit->setValidator( new QIntValidator( d->redit ) );
01017   l_lbot->addWidget(d->redit, 0, 5);
01018   connect( d->redit, SIGNAL( valueChanged(int) ),
01019     SLOT( slotRGBChanged() ) );
01020 
01021   label = new QLabel( i18n("G:"), page );
01022   label->setAlignment(AlignRight | AlignVCenter);
01023   l_lbot->addWidget( label, 1, 4);
01024   d->gedit = new KColorSpinBox( 0, 255,1, page );
01025   d->gedit->setValidator( new QIntValidator( d->gedit ) );
01026   l_lbot->addWidget(d->gedit, 1, 5);
01027   connect( d->gedit, SIGNAL( valueChanged(int) ),
01028     SLOT( slotRGBChanged() ) );
01029 
01030   label = new QLabel( i18n("B:"), page );
01031   label->setAlignment(AlignRight | AlignVCenter);
01032   l_lbot->addWidget(label, 2, 4);
01033   d->bedit = new KColorSpinBox( 0, 255, 1, page );
01034   d->bedit->setValidator( new QIntValidator( d->bedit ) );
01035   l_lbot->addWidget(d->bedit, 2, 5);
01036   connect( d->bedit, SIGNAL( valueChanged(int) ),
01037     SLOT( slotRGBChanged() ) );
01038 
01039   //
01040   // the entry fields should be wide enough to hold 8888888
01041   //
01042   int w = d->hedit->fontMetrics().width("8888888");
01043   d->hedit->setFixedWidth(w);
01044   d->sedit->setFixedWidth(w);
01045   d->vedit->setFixedWidth(w);
01046 
01047   d->redit->setFixedWidth(w);
01048   d->gedit->setFixedWidth(w);
01049   d->bedit->setFixedWidth(w);
01050 
01051   //
01052   // add a layout for the right side
01053   //
01054   d->l_right = new QVBoxLayout;
01055   tl_layout->addLayout(d->l_right, 0, 2);
01056 
01057   //
01058   // Add the palette table
01059   //
01060   d->table = new KPaletteTable( page );
01061   d->l_right->addWidget(d->table, 10);
01062 
01063   connect( d->table, SIGNAL( colorSelected( const QColor &, const QString & ) ),
01064        SLOT( slotColorSelected( const QColor &, const QString & ) ) );
01065 
01066   connect(
01067     d->table,
01068     SIGNAL( colorDoubleClicked( const QColor &, const QString & ) ),
01069     SLOT( slotColorDoubleClicked( const QColor &, const QString & ) )
01070   );
01071   // Store the default value for saving time.
01072   d->originalPalette = d->table->palette();
01073 
01074   //
01075   // a little space between
01076   //
01077   d->l_right->addSpacing(10);
01078 
01079   QHBoxLayout *l_hbox = new QHBoxLayout( d->l_right );
01080 
01081   //
01082   // The add to custom colors button
01083   //
01084   QPushButton *button = new QPushButton( page );
01085   button->setText(i18n("&Add to Custom Colors"));
01086   l_hbox->addWidget(button, 0, AlignLeft);
01087   connect( button, SIGNAL( clicked()), SLOT( slotAddToCustomColors()));
01088 
01089   //
01090   // The color picker button
01091   //
01092   button = new QPushButton( page );
01093   button->setPixmap( BarIcon("colorpicker"));
01094   l_hbox->addWidget(button, 0, AlignHCenter );
01095   connect( button, SIGNAL( clicked()), SLOT( slotColorPicker()));
01096 
01097   //
01098   // a little space between
01099   //
01100   d->l_right->addSpacing(10);
01101 
01102   //
01103   // and now the entry fields and the patch (=colored box)
01104   //
01105   QGridLayout *l_grid = new QGridLayout( d->l_right, 2, 3);
01106 
01107   l_grid->setColStretch(2, 1);
01108 
01109   label = new QLabel( page );
01110   label->setText(i18n("Name:"));
01111   l_grid->addWidget(label, 0, 1, AlignLeft);
01112 
01113   d->colorName = new QLabel( page );
01114   l_grid->addWidget(d->colorName, 0, 2, AlignLeft);
01115 
01116   label = new QLabel( page );
01117   label->setText(i18n("HTML:"));
01118   l_grid->addWidget(label, 1, 1, AlignLeft);
01119 
01120   d->htmlName = new QLineEdit( page );
01121   d->htmlName->setMaxLength( 13 ); // Qt's QColor allows 12 hexa-digits
01122   d->htmlName->setText("#FFFFFF"); // But HTML uses only 6, so do not worry about the size
01123   w = d->htmlName->fontMetrics().width(QString::fromLatin1("#DDDDDDD"));
01124   d->htmlName->setFixedWidth(w);
01125   l_grid->addWidget(d->htmlName, 1, 2, AlignLeft);
01126 
01127   connect( d->htmlName, SIGNAL( textChanged(const QString &) ),
01128       SLOT( slotHtmlChanged() ) );
01129 
01130   d->patch = new KColorPatch( page );
01131   d->patch->setFixedSize(48, 48);
01132   l_grid->addMultiCellWidget(d->patch, 0, 1, 0, 0, AlignHCenter | AlignVCenter);
01133   connect( d->patch, SIGNAL( colorChanged( const QColor&)),
01134        SLOT( setColor( const QColor&)));
01135 
01136   tl_layout->activate();
01137   page->setMinimumSize( page->sizeHint() );
01138 
01139   readSettings();
01140   d->bRecursion = false;
01141   d->bEditHsv = false;
01142   d->bEditRgb = false;
01143   d->bEditHtml = false;
01144 
01145   disableResize();
01146   KColor col;
01147   col.setHsv( 0, 0, 255 );
01148   _setColor( col );
01149 
01150   d->htmlName->installEventFilter(this);
01151   d->hsSelector->installEventFilter(this);
01152   d->hsSelector->setAcceptDrops(true);
01153 }
01154 
01155 KColorDialog::~KColorDialog()
01156 {
01157 #ifdef Q_WS_X11
01158     if (d->bColorPicking)
01159         qt_set_x11_event_filter(d->oldfilter);
01160 #endif
01161     delete d;
01162 }
01163 
01164 bool
01165 KColorDialog::eventFilter( QObject *obj, QEvent *ev )
01166 {
01167     if ((obj == d->htmlName) || (obj == d->hsSelector))
01168     switch(ev->type())
01169     {
01170       case QEvent::DragEnter:
01171       case QEvent::DragMove:
01172       case QEvent::DragLeave:
01173       case QEvent::Drop:
01174       case QEvent::DragResponse:
01175             qApp->sendEvent(d->patch, ev);
01176             return true;
01177       default:
01178             break;
01179     }
01180     return KDialogBase::eventFilter(obj, ev);
01181 }
01182 
01183 void
01184 KColorDialog::setDefaultColor( const QColor& col )
01185 {
01186     if ( !d->cbDefaultColor )
01187     {
01188         //
01189         // a little space between
01190         //
01191         d->l_right->addSpacing(10);
01192 
01193         //
01194         // and the "default color" checkbox, under all items on the right side
01195         //
01196         d->cbDefaultColor = new QCheckBox( i18n( "Default color" ), mainWidget() );
01197         d->cbDefaultColor->setChecked(true);
01198 
01199         d->l_right->addWidget( d->cbDefaultColor );
01200 
01201         mainWidget()->setMaximumSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX ); // cancel setFixedSize()
01202         d->tl_layout->activate();
01203         mainWidget()->setMinimumSize( mainWidget()->sizeHint() );
01204         disableResize();
01205 
01206         connect( d->cbDefaultColor, SIGNAL( clicked() ), SLOT( slotDefaultColorClicked() ) );
01207     }
01208 
01209     d->defaultColor = col;
01210 
01211     slotDefaultColorClicked();
01212 }
01213 
01214 QColor KColorDialog::defaultColor() const
01215 {
01216     return d->defaultColor;
01217 }
01218 
01219 void KColorDialog::slotDefaultColorClicked()
01220 {
01221     if ( d->cbDefaultColor->isChecked() )
01222     {
01223         d->selColor = d->defaultColor;
01224         showColor( d->selColor, i18n( "-default-" ) );
01225     } else
01226     {
01227         showColor( d->selColor, QString::null );
01228     }
01229 }
01230 
01231 void
01232 KColorDialog::readSettings()
01233 {
01234   KConfig* config = KGlobal::config();
01235 
01236   QString oldgroup = config->group();
01237 
01238   config->setGroup("Colors");
01239   QString palette = config->readEntry("CurrentPalette");
01240   d->table->setPalette(palette);
01241   config->setGroup( oldgroup );
01242 }
01243 
01244 void
01245 KColorDialog::slotWriteSettings()
01246 {
01247   KConfig* config = KGlobal::config();
01248   config->setGroup("Colors");
01249   QString palette = d->table->palette();
01250   if (!config->hasDefault("CurrentPalette") &&
01251       (d->table->palette() == d->originalPalette))
01252   {
01253      config->revertToDefault("CurrentPalette");
01254   }
01255   else
01256   {
01257      config->writeEntry("CurrentPalette", d->table->palette());
01258   }
01259 }
01260 
01261 QColor
01262 KColorDialog::color() const
01263 {
01264   if ( d->cbDefaultColor && d->cbDefaultColor->isChecked() )
01265      return QColor();
01266   if ( d->selColor.isValid() )
01267     d->table->addToRecentColors( d->selColor );
01268   return d->selColor;
01269 }
01270 
01271 void KColorDialog::setColor( const QColor &col )
01272 {
01273   _setColor( col );
01274 }
01275 
01276 //
01277 // static function to display dialog and return color
01278 //
01279 int KColorDialog::getColor( QColor &theColor, QWidget *parent )
01280 {
01281   KColorDialog dlg( parent, "Color Selector", true );
01282   if ( theColor.isValid() )
01283     dlg.setColor( theColor );
01284   int result = dlg.exec();
01285 
01286   if ( result == Accepted )
01287   {
01288     theColor = dlg.color();
01289   }
01290 
01291   return result;
01292 }
01293 
01294 //
01295 // static function to display dialog and return color
01296 //
01297 int KColorDialog::getColor( QColor &theColor, const QColor& defaultCol, QWidget *parent )
01298 {
01299   KColorDialog dlg( parent, "Color Selector", true );
01300   dlg.setDefaultColor( defaultCol );
01301   dlg.setColor( theColor );
01302   int result = dlg.exec();
01303 
01304   if ( result == Accepted )
01305     theColor = dlg.color();
01306 
01307   return result;
01308 }
01309 
01310 void KColorDialog::slotRGBChanged( void )
01311 {
01312   if (d->bRecursion) return;
01313   int red = d->redit->value();
01314   int grn = d->gedit->value();
01315   int blu = d->bedit->value();
01316 
01317   if ( red > 255 || red < 0 ) return;
01318   if ( grn > 255 || grn < 0 ) return;
01319   if ( blu > 255 || blu < 0 ) return;
01320 
01321   KColor col;
01322   col.setRgb( red, grn, blu );
01323   d->bEditRgb = true;
01324   _setColor( col );
01325   d->bEditRgb = false;
01326 }
01327 
01328 void KColorDialog::slotHtmlChanged( void )
01329 {
01330   if (d->bRecursion || d->htmlName->text().isEmpty()) return;
01331 
01332   QString strColor( d->htmlName->text() );
01333 
01334   // Assume that a user does not want to type the # all the time
01335   if ( strColor[0] != '#' )
01336   {
01337     bool signalsblocked = d->htmlName->signalsBlocked();
01338     d->htmlName->blockSignals(true);
01339     strColor.prepend("#");
01340     d->htmlName->setText(strColor);
01341     d->htmlName->blockSignals(signalsblocked);
01342   }
01343 
01344   const QColor color( strColor );
01345 
01346   if ( color.isValid() )
01347   {
01348     KColor col( color );
01349     d->bEditHtml = true;
01350     _setColor( col );
01351     d->bEditHtml = false;
01352   }
01353 }
01354 
01355 void KColorDialog::slotHSVChanged( void )
01356 {
01357   if (d->bRecursion) return;
01358   int hue = d->hedit->value();
01359   int sat = d->sedit->value();
01360   int val = d->vedit->value();
01361 
01362   if ( hue > 359 || hue < 0 ) return;
01363   if ( sat > 255 || sat < 0 ) return;
01364   if ( val > 255 || val < 0 ) return;
01365 
01366   KColor col;
01367   col.setHsv( hue, sat, val );
01368   d->bEditHsv = true;
01369   _setColor( col );
01370   d->bEditHsv = false;
01371 }
01372 
01373 void KColorDialog::slotHSChanged( int h, int s )
01374 {
01375   int _h, _s, v;
01376   d->selColor.hsv(&_h, &_s, &v);
01377   if (v < 0)
01378      v = 0;
01379   KColor col;
01380   col.setHsv( h, s, v );
01381   _setColor( col );
01382 }
01383 
01384 void KColorDialog::slotVChanged( int v )
01385 {
01386   int h, s, _v;
01387   d->selColor.hsv(&h, &s, &_v);
01388   KColor col;
01389   col.setHsv( h, s, v );
01390   _setColor( col );
01391 }
01392 
01393 void KColorDialog::slotColorSelected( const QColor &color )
01394 {
01395   _setColor( color );
01396 }
01397 
01398 void KColorDialog::slotAddToCustomColors( )
01399 {
01400   d->table->addToCustomColors( d->selColor );
01401 }
01402 
01403 void KColorDialog::slotColorSelected( const QColor &color, const QString &name )
01404 {
01405   _setColor( color, name);
01406 }
01407 
01408 void KColorDialog::slotColorDoubleClicked
01409 (
01410   const QColor  & color,
01411   const QString & name
01412 )
01413 {
01414   _setColor(color, name);
01415   accept();
01416 }
01417 
01418 void KColorDialog::_setColor(const KColor &color, const QString &name)
01419 {
01420   if (color.isValid())
01421   {
01422      if (d->cbDefaultColor && d->cbDefaultColor->isChecked())
01423         d->cbDefaultColor->setChecked(false);
01424      d->selColor = color;
01425   }
01426   else
01427   {
01428      if (d->cbDefaultColor && d->cbDefaultColor->isChecked())
01429         d->cbDefaultColor->setChecked(true);
01430      d->selColor = d->defaultColor;
01431   }
01432 
01433   showColor( d->selColor, name );
01434 
01435   emit colorSelected( d->selColor );
01436 }
01437 
01438 // show but don't set into selColor, nor emit colorSelected
01439 void KColorDialog::showColor( const KColor &color, const QString &name )
01440 {
01441   d->bRecursion = true;
01442 
01443   if (name.isEmpty())
01444      d->colorName->setText( i18n("-unnamed-"));
01445   else
01446      d->colorName->setText( name );
01447 
01448   d->patch->setColor( color );
01449 
01450   setRgbEdit( color );
01451   setHsvEdit( color );
01452   setHtmlEdit( color );
01453 
01454   int h, s, v;
01455   color.hsv( &h, &s, &v );
01456   d->hsSelector->setValues( h, s );
01457   d->valuePal->blockSignals(true);
01458   d->valuePal->setHue( h );
01459   d->valuePal->setSaturation( s );
01460   d->valuePal->setValue( v );
01461   d->valuePal->updateContents();
01462   d->valuePal->blockSignals(false);
01463   d->valuePal->repaint( false );
01464   d->bRecursion = false;
01465 }
01466 
01467 
01468 static QWidget *kde_color_dlg_widget = 0;
01469 
01470 #ifdef Q_WS_X11
01471 static int kde_color_dlg_handler(XEvent *event)
01472 {
01473     if (event->type == ButtonRelease)
01474     {
01475         QMouseEvent e( QEvent::MouseButtonRelease, QPoint(),
01476                        QPoint(event->xmotion.x_root, event->xmotion.y_root) , 0, 0 );
01477         QApplication::sendEvent( kde_color_dlg_widget, &e );
01478         return true;
01479     }
01480     return false;
01481 }
01482 #endif
01483 void
01484 KColorDialog::slotColorPicker()
01485 {
01486     d->bColorPicking = true;
01487 #ifdef Q_WS_X11
01488     d->oldfilter = qt_set_x11_event_filter(kde_color_dlg_handler);
01489 #endif
01490     kde_color_dlg_widget = this;
01491     grabMouse( crossCursor );
01492     grabKeyboard();
01493 }
01494 
01495 void
01496 KColorDialog::mouseReleaseEvent( QMouseEvent *e )
01497 {
01498   if (d->bColorPicking)
01499   {
01500      d->bColorPicking = false;
01501 #ifdef Q_WS_X11
01502      qt_set_x11_event_filter(d->oldfilter);
01503      d->oldfilter = 0;
01504 #endif
01505      releaseMouse();
01506      releaseKeyboard();
01507      _setColor( grabColor( e->globalPos() ) );
01508      return;
01509   }
01510   KDialogBase::mouseReleaseEvent( e );
01511 }
01512 
01513 QColor
01514 KColorDialog::grabColor(const QPoint &p)
01515 {
01516     QWidget *desktop = QApplication::desktop();
01517     QPixmap pm = QPixmap::grabWindow( desktop->winId(), p.x(), p.y(), 1, 1);
01518     QImage i = pm.convertToImage();
01519     return i.pixel(0,0);
01520 }
01521 
01522 void
01523 KColorDialog::keyPressEvent( QKeyEvent *e )
01524 {
01525   if (d->bColorPicking)
01526   {
01527      if (e->key() == Key_Escape)
01528      {
01529         d->bColorPicking = false;
01530 #ifdef Q_WS_X11
01531         qt_set_x11_event_filter(d->oldfilter);
01532         d->oldfilter = 0;
01533 #endif
01534         releaseMouse();
01535         releaseKeyboard();
01536      }
01537      e->accept();
01538      return;
01539   }
01540   KDialogBase::keyPressEvent( e );
01541 }
01542 
01543 void KColorDialog::setRgbEdit( const KColor &col )
01544 {
01545   if (d->bEditRgb) return;
01546   int r, g, b;
01547   col.rgb( &r, &g, &b );
01548 
01549   d->redit->setValue( r );
01550   d->gedit->setValue( g );
01551   d->bedit->setValue( b );
01552 }
01553 
01554 void KColorDialog::setHtmlEdit( const KColor &col )
01555 {
01556   if (d->bEditHtml) return;
01557   int r, g, b;
01558   col.rgb( &r, &g, &b );
01559   QString num;
01560 
01561   num.sprintf("#%02X%02X%02X", r,g,b);
01562   d->htmlName->setText( num );
01563 }
01564 
01565 
01566 void KColorDialog::setHsvEdit( const KColor &col )
01567 {
01568   if (d->bEditHsv) return;
01569   int h, s, v;
01570   col.hsv( &h, &s, &v );
01571 
01572   d->hedit->setValue( h );
01573   d->sedit->setValue( s );
01574   d->vedit->setValue( v );
01575 }
01576 
01577 void KHSSelector::virtual_hook( int id, void* data )
01578 { KXYSelector::virtual_hook( id, data ); }
01579 
01580 void KValueSelector::virtual_hook( int id, void* data )
01581 { KSelector::virtual_hook( id, data ); }
01582 
01583 void KPaletteTable::virtual_hook( int, void* )
01584 { /*BASE::virtual_hook( id, data );*/ }
01585 
01586 void KColorCells::virtual_hook( int, void* )
01587 { /*BASE::virtual_hook( id, data );*/ }
01588 
01589 void KColorPatch::virtual_hook( int, void* )
01590 { /*BASE::virtual_hook( id, data );*/ }
01591 
01592 void KColorDialog::virtual_hook( int id, void* data )
01593 { KDialogBase::virtual_hook( id, data ); }
01594 
01595 
01596 #include "kcolordialog.moc"
01597 //#endif
KDE Logo
This file is part of the documentation for kdeui Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 28 01:34:46 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003