• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kdeui

kcolordialog.cpp

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

kdeui

Skip menu "kdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal