• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
kcolordialog.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1997 Martin Jones (mjones@kde.org)
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 //-----------------------------------------------------------------------------
20 // KDE color selection dialog.
21 //
22 // 1999-09-27 Espen Sand <espensa@online.no>
23 // KColorDialog is now subclassed from KDialogBase. I have also extended
24 // KColorDialog::getColor() so that it contains a parent argument. This
25 // improves centering capability.
26 //
27 // layout management added Oct 1997 by Mario Weilguni
28 // <mweilguni@sime.com>
29 //
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 
34 #include <tqcheckbox.h>
35 #include <tqcombobox.h>
36 #include <tqdrawutil.h>
37 #include <tqevent.h>
38 #include <tqfile.h>
39 #include <tqimage.h>
40 #include <tqlabel.h>
41 #include <tqlayout.h>
42 #include <tqvalidator.h>
43 #include <tqpainter.h>
44 #include <tqpushbutton.h>
45 #include <tqspinbox.h>
46 #include <tqtimer.h>
47 
48 #include <tdeapplication.h>
49 #include <tdeconfig.h>
50 #include <tdeglobal.h>
51 #include <tdeglobalsettings.h>
52 #include <kiconloader.h>
53 #include <klineedit.h>
54 #include <tdelistbox.h>
55 #include <tdelocale.h>
56 #include <tdemessagebox.h>
57 #include <kseparator.h>
58 #include <kpalette.h>
59 #include <kimageeffect.h>
60 
61 #include "kcolordialog.h"
62 #include "kcolordrag.h"
63 #include "kstaticdeleter.h"
64 #include <config.h>
65 #include <kdebug.h>
66 
67 #include "config.h"
68 #ifdef Q_WS_X11
69 #include <X11/Xlib.h>
70 
71 // defined in qapplication_x11.cpp
72 typedef int (*QX11EventFilter) (XEvent*);
73 extern QX11EventFilter tqt_set_x11_event_filter (QX11EventFilter filter);
74 #endif
75 
76 struct ColorPaletteNameType
77 {
78  const char* m_fileName;
79  const char* m_displayName;
80 };
81 
82 const ColorPaletteNameType colorPaletteName[]=
83 {
84  { "Recent_Colors", I18N_NOOP2( "palette name", "* Recent Colors *" ) },
85  { "Custom_Colors", I18N_NOOP2( "palette name", "* Custom Colors *" ) },
86  { "40.colors", I18N_NOOP2( "palette name", "Forty Colors" ) },
87  { "Rainbow.colors",I18N_NOOP2( "palette name", "Rainbow Colors" ) },
88  { "Royal.colors", I18N_NOOP2( "palette name", "Royal Colors" ) },
89  { "Web.colors", I18N_NOOP2( "palette name", "Web Colors" ) },
90  { 0, 0 } // end of data
91 };
92 
93 const int recentColorIndex = 0;
94 const int customColorIndex = 1;
95 
96 class KColorSpinBox : public TQSpinBox
97 {
98 public:
99  KColorSpinBox(int minValue, int maxValue, int step, TQWidget* parent)
100  : TQSpinBox(minValue, maxValue, step, parent, "kcolorspinbox")
101  { }
102 
103  // Override Qt's braindead auto-selection.
104  virtual void valueChange()
105  {
106  updateDisplay();
107  emit valueChanged( value() );
108  emit valueChanged( currentValueText() );
109  }
110 
111 };
112 
113 
114 #define STANDARD_PAL_SIZE 17
115 
116 KColor::KColor()
117 : TQColor()
118 {
119  r = 0; g = 0; b = 0; h = 0; s = 0; v = 0;
120 }
121 
122 KColor::KColor( const KColor &col)
123 : TQColor( col )
124 {
125  h = col.h; s = col.s; v = col.v;
126  r = col.r; g = col.g; b = col.b;
127 }
128 
129 KColor::KColor( const TQColor &col)
130 : TQColor( col )
131 {
132  TQColor::getRgb(&r, &g, &b);
133  TQColor::getHsv(&h, &s, &v);
134 }
135 
136 bool KColor::operator==(const KColor& col) const
137 {
138  return (h == col.h) && (s == col.s) && (v == col.v) &&
139  (r == col.r) && (g == col.g) && (b == col.b);
140 }
141 
142 KColor& KColor::operator=(const KColor& col)
143 {
144  *(TQColor *)this = col;
145  h = col.h; s = col.s; v = col.v;
146  r = col.r; g = col.g; b = col.b;
147  return *this;
148 }
149 
150 void
151 KColor::setHsv(int _h, int _s, int _v)
152 {
153  h = _h; s = _s; v = _v;
154  TQColor::setHsv(h, s, v);
155  TQColor::rgb(&r, &g, &b);
156 }
157 
158 void
159 KColor::setRgb(int _r, int _g, int _b)
160 {
161  r = _r; g = _g; b = _b;
162  TQColor::setRgb(r, g, b);
163  TQColor::hsv(&h, &s, &v);
164 }
165 
166 void
167 KColor::rgb(int *_r, int *_g, int *_b) const
168 {
169  *_r = r; *_g = g; *_b = b;
170 }
171 
172 void
173 KColor::hsv(int *_h, int *_s, int *_v) const
174 {
175  *_h = h; *_s = s; *_v = v;
176 }
177 
178 
179 static TQColor *standardPalette = 0;
180 static KStaticDeleter<TQColor> spd;
181 
182 static void createStandardPalette()
183 {
184  if ( standardPalette )
185  return;
186 
187  spd.setObject(standardPalette, new TQColor [STANDARD_PAL_SIZE], true/*array*/);
188 
189  int i = 0;
190 
191  standardPalette[i++] = Qt::red;
192  standardPalette[i++] = Qt::green;
193  standardPalette[i++] = Qt::blue;
194  standardPalette[i++] = Qt::cyan;
195  standardPalette[i++] = Qt::magenta;
196  standardPalette[i++] = Qt::yellow;
197  standardPalette[i++] = Qt::darkRed;
198  standardPalette[i++] = Qt::darkGreen;
199  standardPalette[i++] = Qt::darkBlue;
200  standardPalette[i++] = Qt::darkCyan;
201  standardPalette[i++] = Qt::darkMagenta;
202  standardPalette[i++] = Qt::darkYellow;
203  standardPalette[i++] = Qt::white;
204  standardPalette[i++] = Qt::lightGray;
205  standardPalette[i++] = Qt::gray;
206  standardPalette[i++] = Qt::darkGray;
207  standardPalette[i++] = Qt::black;
208 }
209 
210 
211 KHSSelector::KHSSelector( TQWidget *parent, const char *name )
212  : KXYSelector( parent, name )
213 {
214  setRange( 0, 0, 359, 255 );
215 }
216 
217 void KHSSelector::updateContents()
218 {
219  drawPalette(&pixmap);
220 }
221 
222 void KHSSelector::resizeEvent( TQResizeEvent * )
223 {
224  updateContents();
225 }
226 
227 void KHSSelector::drawContents( TQPainter *painter )
228 {
229  painter->drawPixmap( contentsRect().x(), contentsRect().y(), pixmap );
230 }
231 
232 void KHSSelector::drawPalette( TQPixmap *pixmap )
233 {
234  int xSize = contentsRect().width(), ySize = contentsRect().height();
235  TQImage image( xSize, ySize, 32 );
236  TQColor col;
237  int h, s;
238  uint *p;
239 
240  for ( s = ySize-1; s >= 0; s-- )
241  {
242  p = (uint *) image.scanLine( ySize - s - 1 );
243  for( h = 0; h < xSize; h++ )
244  {
245  col.setHsv( 359*h/(xSize-1), 255*s/((ySize == 1) ? 1 : ySize-1), 192 );
246  *p = col.rgb();
247  p++;
248  }
249  }
250 
251  if ( TQColor::numBitPlanes() <= 8 )
252  {
253  createStandardPalette();
254  KImageEffect::dither( image, standardPalette, STANDARD_PAL_SIZE );
255  }
256  pixmap->convertFromImage( image );
257 }
258 
259 
260 //-----------------------------------------------------------------------------
261 
262 KValueSelector::KValueSelector( TQWidget *parent, const char *name )
263  : TDESelector( Qt::Vertical, parent, name ), _hue(0), _sat(0)
264 {
265  setRange( 0, 255 );
266  pixmap.setOptimization( TQPixmap::BestOptim );
267 }
268 
269 KValueSelector::KValueSelector(Orientation o, TQWidget *parent, const char *name
270  )
271  : TDESelector( o, parent, name), _hue(0), _sat(0)
272 {
273  setRange( 0, 255 );
274  pixmap.setOptimization( TQPixmap::BestOptim );
275 }
276 
277 void KValueSelector::updateContents()
278 {
279  drawPalette(&pixmap);
280 }
281 
282 void KValueSelector::resizeEvent( TQResizeEvent * )
283 {
284  updateContents();
285 }
286 
287 void KValueSelector::drawContents( TQPainter *painter )
288 {
289  painter->drawPixmap( contentsRect().x(), contentsRect().y(), pixmap );
290 }
291 
292 void KValueSelector::drawPalette( TQPixmap *pixmap )
293 {
294  int xSize = contentsRect().width(), ySize = contentsRect().height();
295  TQImage image( xSize, ySize, 32 );
296  TQColor col;
297  uint *p;
298  QRgb rgb;
299 
300  if ( orientation() == Qt::Horizontal )
301  {
302  for ( int v = 0; v < ySize; v++ )
303  {
304  p = (uint *) image.scanLine( ySize - v - 1 );
305 
306  for( int x = 0; x < xSize; x++ )
307  {
308  col.setHsv( _hue, _sat, 255*x/((xSize == 1) ? 1 : xSize-1) );
309  rgb = col.rgb();
310  *p++ = rgb;
311  }
312  }
313  }
314 
315  if( orientation() == Qt::Vertical )
316  {
317  for ( int v = 0; v < ySize; v++ )
318  {
319  p = (uint *) image.scanLine( ySize - v - 1 );
320  col.setHsv( _hue, _sat, 255*v/((ySize == 1) ? 1 : ySize-1) );
321  rgb = col.rgb();
322  for ( int i = 0; i < xSize; i++ )
323  *p++ = rgb;
324  }
325  }
326 
327  if ( TQColor::numBitPlanes() <= 8 )
328  {
329  createStandardPalette();
330  KImageEffect::dither( image, standardPalette, STANDARD_PAL_SIZE );
331  }
332  pixmap->convertFromImage( image );
333 }
334 
335 //-----------------------------------------------------------------------------
336 
337 KColorCells::KColorCells( TQWidget *parent, int rows, int cols )
338  : TQGridView( parent )
339 {
340  shade = true;
341  setNumRows( rows );
342  setNumCols( cols );
343  colors = new TQColor [ rows * cols ];
344 
345  for ( int i = 0; i < rows * cols; i++ )
346  colors[i] = TQColor();
347 
348  selected = 0;
349  inMouse = false;
350 
351  // Drag'n'Drop
352  setAcceptDrops( true);
353 
354  setHScrollBarMode( AlwaysOff );
355  setVScrollBarMode( AlwaysOff );
356  viewport()->setBackgroundMode( PaletteBackground );
357  setBackgroundMode( PaletteBackground );
358 }
359 
360 KColorCells::~KColorCells()
361 {
362  delete [] colors;
363 }
364 
365 void KColorCells::setColor( int colNum, const TQColor &col )
366 {
367  colors[colNum] = col;
368  updateCell( colNum/numCols(), colNum%numCols() );
369 }
370 
371 void KColorCells::paintCell( TQPainter *painter, int row, int col )
372 {
373  TQBrush brush;
374  int w = 1;
375 
376  if (shade)
377  {
378  qDrawShadePanel( painter, 1, 1, cellWidth()-2,
379  cellHeight()-2, colorGroup(), true, 1, &brush );
380  w = 2;
381  }
382  TQColor color = colors[ row * numCols() + col ];
383  if (!color.isValid())
384  {
385  if (!shade) return;
386  color = backgroundColor();
387  }
388 
389  painter->setPen( color );
390  painter->setBrush( TQBrush( color ) );
391  painter->drawRect( w, w, cellWidth()-w*2, cellHeight()-w*2 );
392 
393  if ( row * numCols() + col == selected )
394  painter->drawWinFocusRect( w, w, cellWidth()-w*2, cellHeight()-w*2 );
395 }
396 
397 void KColorCells::resizeEvent( TQResizeEvent * )
398 {
399  setCellWidth( width() / numCols() );
400  setCellHeight( height() / numRows() );
401 }
402 
403 void KColorCells::mousePressEvent( TQMouseEvent *e )
404 {
405  inMouse = true;
406  mPos = e->pos();
407 }
408 
409 int KColorCells::posToCell(const TQPoint &pos, bool ignoreBorders)
410 {
411  int row = pos.y() / cellHeight();
412  int col = pos.x() / cellWidth();
413  int cell = row * numCols() + col;
414 
415  if (!ignoreBorders)
416  {
417  int border = 2;
418  int x = pos.x() - col * cellWidth();
419  int y = pos.y() - row * cellHeight();
420  if ( (x < border) || (x > cellWidth()-border) ||
421  (y < border) || (y > cellHeight()-border))
422  return -1;
423  }
424  return cell;
425 }
426 
427 void KColorCells::mouseMoveEvent( TQMouseEvent *e )
428 {
429  if( !(e->state() & Qt::LeftButton)) return;
430 
431  if(inMouse) {
432  int delay = TDEGlobalSettings::dndEventDelay();
433  if(e->x() > mPos.x()+delay || e->x() < mPos.x()-delay ||
434  e->y() > mPos.y()+delay || e->y() < mPos.y()-delay){
435  // Drag color object
436  int cell = posToCell(mPos);
437  if ((cell != -1) && colors[cell].isValid())
438  {
439  KColorDrag *d = new KColorDrag( colors[cell], this);
440  d->dragCopy();
441  }
442  }
443  }
444 }
445 
446 void KColorCells::dragEnterEvent( TQDragEnterEvent *event)
447 {
448  event->accept( acceptDrags && KColorDrag::canDecode( event));
449 }
450 
451 void KColorCells::dropEvent( TQDropEvent *event)
452 {
453  TQColor c;
454  if( KColorDrag::decode( event, c)) {
455  int cell = posToCell(event->pos(), true);
456  setColor(cell,c);
457  }
458 }
459 
460 void KColorCells::mouseReleaseEvent( TQMouseEvent *e )
461 {
462  int cell = posToCell(mPos);
463  int currentCell = posToCell(e->pos());
464 
465  // If we release the mouse in another cell and we don't have
466  // a drag we should ignore this event.
467  if (currentCell != cell)
468  cell = -1;
469 
470  if ( (cell != -1) && (selected != cell) )
471  {
472  int prevSel = selected;
473  selected = cell;
474  updateCell( prevSel/numCols(), prevSel%numCols() );
475  updateCell( cell/numCols(), cell%numCols() );
476  }
477 
478  inMouse = false;
479  if (cell != -1)
480  emit colorSelected( cell );
481 }
482 
483 void KColorCells::mouseDoubleClickEvent( TQMouseEvent * /*e*/ )
484 {
485  int cell = posToCell(mPos);
486 
487  if (cell != -1)
488  emit colorDoubleClicked( cell );
489 }
490 
491 
492 //-----------------------------------------------------------------------------
493 
494 KColorPatch::KColorPatch( TQWidget *parent ) : TQFrame( parent )
495 {
496  setFrameStyle( TQFrame::Panel | TQFrame::Sunken );
497  colContext = 0;
498  setAcceptDrops( true);
499 }
500 
501 KColorPatch::~KColorPatch()
502 {
503  if ( colContext )
504  TQColor::destroyAllocContext( colContext );
505 }
506 
507 void KColorPatch::setColor( const TQColor &col )
508 {
509  if ( colContext )
510  TQColor::destroyAllocContext( colContext );
511  colContext = TQColor::enterAllocContext();
512  color.setRgb( col.rgb() );
513  color.alloc();
514  TQColor::leaveAllocContext();
515 
516  TQPainter painter;
517 
518  painter.begin( this );
519  drawContents( &painter );
520  painter.end();
521 }
522 
523 void KColorPatch::drawContents( TQPainter *painter )
524 {
525  painter->setPen( color );
526  painter->setBrush( TQBrush( color ) );
527  painter->drawRect( contentsRect() );
528 }
529 
530 void KColorPatch::mouseMoveEvent( TQMouseEvent *e )
531 {
532  // Drag color object
533  if( !(e->state() & Qt::LeftButton)) return;
534  KColorDrag *d = new KColorDrag( color, this);
535  d->dragCopy();
536 }
537 
538 void KColorPatch::dragEnterEvent( TQDragEnterEvent *event)
539 {
540  event->accept( KColorDrag::canDecode( event));
541 }
542 
543 void KColorPatch::dropEvent( TQDropEvent *event)
544 {
545  TQColor c;
546  if( KColorDrag::decode( event, c)) {
547  setColor( c);
548  emit colorChanged( c);
549  }
550 }
551 
552 class KPaletteTable::KPaletteTablePrivate
553 {
554 public:
555  TQMap<TQString,TQColor> m_namedColorMap;
556 };
557 
558 KPaletteTable::KPaletteTable( TQWidget *parent, int minWidth, int cols)
559  : TQWidget( parent ), cells(0), mPalette(0), mMinWidth(minWidth), mCols(cols)
560 {
561  d = new KPaletteTablePrivate;
562 
563  i18n_namedColors = i18n("Named Colors");
564 
565  TQStringList diskPaletteList = KPalette::getPaletteList();
566  TQStringList paletteList;
567 
568  // We must replace the untranslated file names by translate names (of course only for KDE's standard palettes)
569  for ( int i = 0; colorPaletteName[i].m_fileName; ++i )
570  {
571  diskPaletteList.remove( colorPaletteName[i].m_fileName );
572  paletteList.append( i18n( "palette name", colorPaletteName[i].m_displayName ) );
573  }
574  paletteList += diskPaletteList;
575  paletteList.append( i18n_namedColors );
576 
577  TQVBoxLayout *layout = new TQVBoxLayout( this );
578 
579  combo = new TQComboBox( false, this );
580  combo->insertStringList( paletteList );
581  layout->addWidget(combo);
582 
583  sv = new TQScrollView( this );
584  TQSize cellSize = TQSize( mMinWidth, 120);
585  sv->setHScrollBarMode( TQScrollView::AlwaysOff);
586  sv->setVScrollBarMode( TQScrollView::AlwaysOn);
587  TQSize minSize = TQSize(sv->verticalScrollBar()->width(), 0);
588  minSize += TQSize(sv->frameWidth(), 0);
589  minSize += TQSize(cellSize);
590  sv->setFixedSize(minSize);
591  layout->addWidget(sv);
592 
593  mNamedColorList = new TDEListBox( this, "namedColorList", 0 );
594  mNamedColorList->setFixedSize(minSize);
595  mNamedColorList->hide();
596  layout->addWidget(mNamedColorList);
597  connect( mNamedColorList, TQT_SIGNAL(highlighted( const TQString & )),
598  this, TQT_SLOT( slotColorTextSelected( const TQString & )) );
599 
600  setFixedSize( sizeHint());
601  connect( combo, TQT_SIGNAL(activated(const TQString &)),
602  this, TQT_SLOT(slotSetPalette( const TQString &)));
603 }
604 
605 KPaletteTable::~KPaletteTable()
606 {
607  delete mPalette;
608  delete d;
609 }
610 
611 TQString
612 KPaletteTable::palette() const
613 {
614  return combo->currentText();
615 }
616 
617 
618 static const char * const *namedColorFilePath( void )
619 {
620  //
621  // 2000-02-05 Espen Sand.
622  // Add missing filepaths here. Make sure the last entry is 0!
623  //
624  static const char * const path[] =
625  {
626 #ifdef X11_RGBFILE
627  X11_RGBFILE,
628 #endif
629  "/usr/share/X11/rgb.txt",
630  "/usr/X11R6/lib/X11/rgb.txt",
631  "/usr/openwin/lib/X11/rgb.txt", // for Solaris.
632  0
633  };
634  return path;
635 }
636 
637 
638 
639 
640 void
641 KPaletteTable::readNamedColor( void )
642 {
643  if( mNamedColorList->count() != 0 )
644  {
645  return; // Strings already present
646  }
647 
648  TDEGlobal::locale()->insertCatalogue("tdelibs_colors");
649 
650  //
651  // Code somewhat inspired by KPalette.
652  //
653 
654  const char * const *path = namedColorFilePath();
655  for( int i=0; path[i]; ++i )
656  {
657  TQFile paletteFile( path[i] );
658  if( !paletteFile.open( IO_ReadOnly ) )
659  {
660  continue;
661  }
662 
663  TQString line;
664  TQStringList list;
665  while( paletteFile.readLine( line, 100 ) != -1 )
666  {
667  int red, green, blue;
668  int pos = 0;
669 
670  if( sscanf(line.ascii(), "%d %d %d%n", &red, &green, &blue, &pos ) == 3 )
671  {
672  //
673  // Remove duplicates. Every name with a space and every name
674  // that start with "gray".
675  //
676  TQString name = line.mid(pos).stripWhiteSpace();
677  if( name.isNull() || name.find(' ') != -1 ||
678  name.find( "gray" ) != -1 || name.find( "grey" ) != -1 )
679  {
680  continue;
681  }
682 
683  const TQColor color ( red, green, blue );
684  if ( color.isValid() )
685  {
686  const TQString colorName( i18n("color", name.latin1() ) );
687  list.append( colorName );
688  d->m_namedColorMap[ colorName ] = color;
689  }
690  }
691  }
692 
693  list.sort();
694  mNamedColorList->insertStringList( list );
695  break;
696  }
697 
698  if( mNamedColorList->count() == 0 )
699  {
700  //
701  // Give the error dialog box a chance to center above the
702  // widget (or dialog). If we had displayed it now we could get a
703  // situation where the (modal) error dialog box pops up first
704  // preventing the real dialog to become visible until the
705  // error dialog box is removed (== bad UI).
706  //
707  TQTimer::singleShot( 10, this, TQT_SLOT(slotShowNamedColorReadError()) );
708  }
709 }
710 
711 
712 void
713 KPaletteTable::slotShowNamedColorReadError( void )
714 {
715  if( mNamedColorList->count() == 0 )
716  {
717  TQString msg = i18n(""
718  "Unable to read X11 RGB color strings. The following "
719  "file location(s) were examined:\n");
720 
721  const char * const *path = namedColorFilePath();
722  for( int i=0; path[i]; ++i )
723  {
724  msg += path[i];
725  msg += "\n";
726  }
727  KMessageBox::sorry( this, msg );
728  }
729 }
730 
731 
732 //
733 // 2000-02-12 Espen Sand
734 // Set the color in two steps. The setPalette() slot will not emit a signal
735 // with the current color setting. The reason is that setPalette() is used
736 // by the color selector dialog on startup. In the color selector dialog
737 // we normally want to display a startup color which we specify
738 // when the dialog is started. The slotSetPalette() slot below will
739 // set the palette and then use the information to emit a signal with the
740 // new color setting. It is only used by the combobox widget.
741 //
742 void
743 KPaletteTable::slotSetPalette( const TQString &_paletteName )
744 {
745  setPalette( _paletteName );
746  if( mNamedColorList->isVisible() )
747  {
748  int item = mNamedColorList->currentItem();
749  mNamedColorList->setCurrentItem( item < 0 ? 0 : item );
750  slotColorTextSelected( mNamedColorList->currentText() );
751  }
752  else
753  {
754  slotColorCellSelected(0); // FIXME: We need to save the current value!!
755  }
756 }
757 
758 
759 void
760 KPaletteTable::setPalette( const TQString &_paletteName )
761 {
762  TQString paletteName( _paletteName);
763  if (paletteName.isEmpty())
764  paletteName = i18n_recentColors;
765 
766  if (combo->currentText() != paletteName)
767  {
768  bool found = false;
769  for(int i = 0; i < combo->count(); i++)
770  {
771  if (combo->text(i) == paletteName)
772  {
773  combo->setCurrentItem(i);
774  found = true;
775  break;
776  }
777  }
778  if (!found)
779  {
780  combo->insertItem(paletteName);
781  combo->setCurrentItem(combo->count()-1);
782  }
783  }
784 
785  // We must again find the file name of the palette from the eventual translation
786  for ( int i = 0; colorPaletteName[i].m_fileName; ++i )
787  {
788  if ( paletteName == i18n( "palette name", colorPaletteName[i].m_displayName ) )
789  {
790  paletteName = colorPaletteName[i].m_fileName;
791  break;
792  }
793  }
794 
795 
796  //
797  // 2000-02-12 Espen Sand
798  // The palette mode "i18n_namedColors" does not use the KPalette class.
799  // In fact, 'mPalette' and 'cells' are 0 when in this mode. The reason
800  // for this is maninly that KPalette reads from and writes to files using
801  // "locate()". The colors used in "i18n_namedColors" mode comes from the
802  // X11 diretory and is not writable. I don't think this fit in KPalette.
803  //
804  if( !mPalette || mPalette->name() != paletteName )
805  {
806  if( paletteName == i18n_namedColors )
807  {
808  sv->hide();
809  mNamedColorList->show();
810  readNamedColor();
811 
812  delete cells; cells = 0;
813  delete mPalette; mPalette = 0;
814  }
815  else
816  {
817  mNamedColorList->hide();
818  sv->show();
819 
820  delete cells;
821  delete mPalette;
822  mPalette = new KPalette(paletteName);
823  int rows = (mPalette->nrColors()+mCols-1) / mCols;
824  if (rows < 1) rows = 1;
825  cells = new KColorCells( sv->viewport(), rows, mCols);
826  cells->setShading(false);
827  cells->setAcceptDrags(false);
828  TQSize cellSize = TQSize( mMinWidth, mMinWidth * rows / mCols);
829  cells->setFixedSize( cellSize );
830  for( int i = 0; i < mPalette->nrColors(); i++)
831  {
832  cells->setColor( i, mPalette->color(i) );
833  }
834  connect( cells, TQT_SIGNAL( colorSelected( int ) ),
835  TQT_SLOT( slotColorCellSelected( int ) ) );
836  connect( cells, TQT_SIGNAL( colorDoubleClicked( int ) ),
837  TQT_SLOT( slotColorCellDoubleClicked( int ) ) );
838  sv->addChild( cells );
839  cells->show();
840  sv->updateScrollBars();
841  }
842  }
843 }
844 
845 
846 
847 void
848 KPaletteTable::slotColorCellSelected( int col )
849 {
850  if (!mPalette || (col >= mPalette->nrColors()))
851  return;
852  emit colorSelected( mPalette->color(col), mPalette->colorName(col) );
853 }
854 
855 void
856 KPaletteTable::slotColorCellDoubleClicked( int col )
857 {
858  if (!mPalette || (col >= mPalette->nrColors()))
859  return;
860  emit colorDoubleClicked( mPalette->color(col), mPalette->colorName(col) );
861 }
862 
863 
864 void
865 KPaletteTable::slotColorTextSelected( const TQString &colorText )
866 {
867  emit colorSelected( d->m_namedColorMap[ colorText ], colorText );
868 }
869 
870 
871 void
872 KPaletteTable::addToCustomColors( const TQColor &color)
873 {
874  setPalette(i18n( "palette name", colorPaletteName[ customColorIndex ].m_displayName ));
875  mPalette->addColor( color );
876  mPalette->save();
877  delete mPalette;
878  mPalette = 0;
879  setPalette(i18n( "palette name", colorPaletteName[ customColorIndex ].m_displayName ));
880 }
881 
882 void
883 KPaletteTable::addToRecentColors( const TQColor &color)
884 {
885  //
886  // 2000-02-12 Espen Sand.
887  // The 'mPalette' is always 0 when current mode is i18n_namedColors
888  //
889  bool recentIsSelected = false;
890  if ( mPalette && mPalette->name() == colorPaletteName[ recentColorIndex ].m_fileName )
891  {
892  delete mPalette;
893  mPalette = 0;
894  recentIsSelected = true;
895  }
896  KPalette *recentPal = new KPalette( colorPaletteName[ recentColorIndex ].m_fileName );
897  if (recentPal->findColor(color) == -1)
898  {
899  recentPal->addColor( color );
900  recentPal->save();
901  }
902  delete recentPal;
903  if (recentIsSelected)
904  setPalette( i18n( "palette name", colorPaletteName[ recentColorIndex ].m_displayName ) );
905 }
906 
907 class KColorDialog::KColorDialogPrivate {
908 public:
909  KPaletteTable *table;
910  TQString originalPalette;
911  bool bRecursion;
912  bool bEditRgb;
913  bool bEditHsv;
914  bool bEditHtml;
915  bool bColorPicking;
916  TQLabel *colorName;
917  KLineEdit *htmlName;
918  KColorSpinBox *hedit;
919  KColorSpinBox *sedit;
920  KColorSpinBox *vedit;
921  KColorSpinBox *redit;
922  KColorSpinBox *gedit;
923  KColorSpinBox *bedit;
924  KColorPatch *patch;
925  KHSSelector *hsSelector;
926  KPalette *palette;
927  KValueSelector *valuePal;
928  TQVBoxLayout* l_right;
929  TQGridLayout* tl_layout;
930  TQCheckBox *cbDefaultColor;
931  KColor defaultColor;
932  KColor selColor;
933 #ifdef Q_WS_X11
934  QX11EventFilter oldfilter;
935 #endif
936 };
937 
938 
939 KColorDialog::KColorDialog( TQWidget *parent, const char *name, bool modal )
940  :KDialogBase( parent, name, modal, i18n("Select Color"),
941  modal ? Ok|Cancel : Close,
942  Ok, true )
943 {
944  d = new KColorDialogPrivate;
945  d->bRecursion = true;
946  d->bColorPicking = false;
947 #ifdef Q_WS_X11
948  d->oldfilter = 0;
949 #endif
950  d->cbDefaultColor = 0L;
951  connect( this, TQT_SIGNAL(okClicked(void)),this,TQT_SLOT(slotWriteSettings(void)));
952  connect( this, TQT_SIGNAL(closeClicked(void)),this,TQT_SLOT(slotWriteSettings(void)));
953 
954  TQLabel *label;
955 
956  //
957  // Create the top level page and its layout
958  //
959  TQWidget *page = new TQWidget( this );
960  setMainWidget( page );
961 
962  TQGridLayout *tl_layout = new TQGridLayout( page, 3, 3, 0, spacingHint() );
963  d->tl_layout = tl_layout;
964  tl_layout->addColSpacing( 1, spacingHint() * 2 );
965 
966  //
967  // the more complicated part: the left side
968  // add a V-box
969  //
970  TQVBoxLayout *l_left = new TQVBoxLayout();
971  tl_layout->addLayout(l_left, 0, 0);
972 
973  //
974  // add a H-Box for the XY-Selector and a grid for the
975  // entry fields
976  //
977  TQHBoxLayout *l_ltop = new TQHBoxLayout();
978  l_left->addLayout(l_ltop);
979 
980  // a little space between
981  l_left->addSpacing(10);
982 
983  TQGridLayout *l_lbot = new TQGridLayout(3, 6);
984  l_left->addLayout(TQT_TQLAYOUT(l_lbot));
985 
986  //
987  // the palette and value selector go into the H-box
988  //
989  d->hsSelector = new KHSSelector( page );
990  d->hsSelector->setMinimumSize(140, 70);
991  l_ltop->addWidget(d->hsSelector, 8);
992  connect( d->hsSelector, TQT_SIGNAL( valueChanged( int, int ) ),
993  TQT_SLOT( slotHSChanged( int, int ) ) );
994 
995  d->valuePal = new KValueSelector( page );
996  d->valuePal->setMinimumSize(26, 70);
997  l_ltop->addWidget(d->valuePal, 1);
998  connect( d->valuePal, TQT_SIGNAL( valueChanged( int ) ),
999  TQT_SLOT( slotVChanged( int ) ) );
1000 
1001 
1002  //
1003  // add the HSV fields
1004  //
1005  label = new TQLabel( i18n("H:"), page );
1006  label->setAlignment(AlignRight | AlignVCenter);
1007  l_lbot->addWidget(label, 0, 2);
1008  d->hedit = new KColorSpinBox( 0, 359, 1, page );
1009  d->hedit->setValidator( new TQIntValidator( TQT_TQOBJECT(d->hedit) ) );
1010  l_lbot->addWidget(d->hedit, 0, 3);
1011  connect( d->hedit, TQT_SIGNAL( valueChanged(int) ),
1012  TQT_SLOT( slotHSVChanged() ) );
1013 
1014  label = new TQLabel( i18n("S:"), page );
1015  label->setAlignment(AlignRight | AlignVCenter);
1016  l_lbot->addWidget(label, 1, 2);
1017  d->sedit = new KColorSpinBox( 0, 255, 1, page );
1018  d->sedit->setValidator( new TQIntValidator( TQT_TQOBJECT(d->sedit) ) );
1019  l_lbot->addWidget(d->sedit, 1, 3);
1020  connect( d->sedit, TQT_SIGNAL( valueChanged(int) ),
1021  TQT_SLOT( slotHSVChanged() ) );
1022 
1023  label = new TQLabel( i18n("V:"), page );
1024  label->setAlignment(AlignRight | AlignVCenter);
1025  l_lbot->addWidget(label, 2, 2);
1026  d->vedit = new KColorSpinBox( 0, 255, 1, page );
1027  d->vedit->setValidator( new TQIntValidator( TQT_TQOBJECT(d->vedit) ) );
1028  l_lbot->addWidget(d->vedit, 2, 3);
1029  connect( d->vedit, TQT_SIGNAL( valueChanged(int) ),
1030  TQT_SLOT( slotHSVChanged() ) );
1031 
1032  //
1033  // add the RGB fields
1034  //
1035  label = new TQLabel( i18n("R:"), page );
1036  label->setAlignment(AlignRight | AlignVCenter);
1037  l_lbot->addWidget(label, 0, 4);
1038  d->redit = new KColorSpinBox( 0, 255, 1, page );
1039  d->redit->setValidator( new TQIntValidator( TQT_TQOBJECT(d->redit) ) );
1040  l_lbot->addWidget(d->redit, 0, 5);
1041  connect( d->redit, TQT_SIGNAL( valueChanged(int) ),
1042  TQT_SLOT( slotRGBChanged() ) );
1043 
1044  label = new TQLabel( i18n("G:"), page );
1045  label->setAlignment(AlignRight | AlignVCenter);
1046  l_lbot->addWidget( label, 1, 4);
1047  d->gedit = new KColorSpinBox( 0, 255,1, page );
1048  d->gedit->setValidator( new TQIntValidator( TQT_TQOBJECT(d->gedit) ) );
1049  l_lbot->addWidget(d->gedit, 1, 5);
1050  connect( d->gedit, TQT_SIGNAL( valueChanged(int) ),
1051  TQT_SLOT( slotRGBChanged() ) );
1052 
1053  label = new TQLabel( i18n("B:"), page );
1054  label->setAlignment(AlignRight | AlignVCenter);
1055  l_lbot->addWidget(label, 2, 4);
1056  d->bedit = new KColorSpinBox( 0, 255, 1, page );
1057  d->bedit->setValidator( new TQIntValidator( TQT_TQOBJECT(d->bedit) ) );
1058  l_lbot->addWidget(d->bedit, 2, 5);
1059  connect( d->bedit, TQT_SIGNAL( valueChanged(int) ),
1060  TQT_SLOT( slotRGBChanged() ) );
1061 
1062  //
1063  // the entry fields should be wide enough to hold 8888888
1064  //
1065  int w = d->hedit->fontMetrics().width("8888888");
1066  d->hedit->setFixedWidth(w);
1067  d->sedit->setFixedWidth(w);
1068  d->vedit->setFixedWidth(w);
1069 
1070  d->redit->setFixedWidth(w);
1071  d->gedit->setFixedWidth(w);
1072  d->bedit->setFixedWidth(w);
1073 
1074  //
1075  // add a layout for the right side
1076  //
1077  d->l_right = new TQVBoxLayout;
1078  tl_layout->addLayout(d->l_right, 0, 2);
1079 
1080  //
1081  // Add the palette table
1082  //
1083  d->table = new KPaletteTable( page );
1084  d->l_right->addWidget(d->table, 10);
1085 
1086  connect( d->table, TQT_SIGNAL( colorSelected( const TQColor &, const TQString & ) ),
1087  TQT_SLOT( slotColorSelected( const TQColor &, const TQString & ) ) );
1088 
1089  connect(
1090  d->table,
1091  TQT_SIGNAL( colorDoubleClicked( const TQColor &, const TQString & ) ),
1092  TQT_SLOT( slotColorDoubleClicked( const TQColor &, const TQString & ) )
1093  );
1094  // Store the default value for saving time.
1095  d->originalPalette = d->table->palette();
1096 
1097  //
1098  // a little space between
1099  //
1100  d->l_right->addSpacing(10);
1101 
1102  TQHBoxLayout *l_hbox = new TQHBoxLayout( d->l_right );
1103 
1104  //
1105  // The add to custom colors button
1106  //
1107  TQPushButton *button = new TQPushButton( page );
1108  button->setText(i18n("&Add to Custom Colors"));
1109  l_hbox->addWidget(button, 0, AlignLeft);
1110  connect( button, TQT_SIGNAL( clicked()), TQT_SLOT( slotAddToCustomColors()));
1111 
1112  //
1113  // The color picker button
1114  //
1115  button = new TQPushButton( page );
1116  button->setPixmap( BarIcon("colorpicker"));
1117  l_hbox->addWidget(button, 0, AlignHCenter );
1118  connect( button, TQT_SIGNAL( clicked()), TQT_SLOT( slotColorPicker()));
1119 
1120  //
1121  // a little space between
1122  //
1123  d->l_right->addSpacing(10);
1124 
1125  //
1126  // and now the entry fields and the patch (=colored box)
1127  //
1128  TQGridLayout *l_grid = new TQGridLayout( d->l_right, 2, 3);
1129 
1130  l_grid->setColStretch(2, 1);
1131 
1132  label = new TQLabel( page );
1133  label->setText(i18n("Name:"));
1134  l_grid->addWidget(TQT_TQWIDGET(label), 0, 1, Qt::AlignLeft);
1135 
1136  d->colorName = new TQLabel( page );
1137  l_grid->addWidget(TQT_TQWIDGET(d->colorName), 0, 2, Qt::AlignLeft);
1138 
1139  label = new TQLabel( page );
1140  label->setText(i18n("HTML:"));
1141  l_grid->addWidget(TQT_TQWIDGET(label), 1, 1, Qt::AlignLeft);
1142 
1143  d->htmlName = new KLineEdit( page );
1144  d->htmlName->setMaxLength( 13 ); // Qt's TQColor allows 12 hexa-digits
1145  d->htmlName->setText("#FFFFFF"); // But HTML uses only 6, so do not worry about the size
1146  w = d->htmlName->fontMetrics().width(TQString::fromLatin1("#DDDDDDD"));
1147  d->htmlName->setFixedWidth(w);
1148  l_grid->addWidget(TQT_TQWIDGET(d->htmlName), 1, 2, Qt::AlignLeft);
1149 
1150  connect( d->htmlName, TQT_SIGNAL( textChanged(const TQString &) ),
1151  TQT_SLOT( slotHtmlChanged() ) );
1152 
1153  d->patch = new KColorPatch( page );
1154  d->patch->setFixedSize(48, 48);
1155  l_grid->addMultiCellWidget(TQT_TQWIDGET(d->patch), 0, 1, 0, 0, Qt::AlignHCenter | Qt::AlignVCenter);
1156  connect( d->patch, TQT_SIGNAL( colorChanged( const TQColor&)),
1157  TQT_SLOT( setColor( const TQColor&)));
1158 
1159  tl_layout->activate();
1160  page->setMinimumSize( page->sizeHint() );
1161 
1162  readSettings();
1163  d->bRecursion = false;
1164  d->bEditHsv = false;
1165  d->bEditRgb = false;
1166  d->bEditHtml = false;
1167 
1168  disableResize();
1169  KColor col;
1170  col.setHsv( 0, 0, 255 );
1171  _setColor( col );
1172 
1173  d->htmlName->installEventFilter(this);
1174  d->hsSelector->installEventFilter(this);
1175  d->hsSelector->setAcceptDrops(true);
1176 }
1177 
1178 KColorDialog::~KColorDialog()
1179 {
1180 #ifdef Q_WS_X11
1181  if (d->bColorPicking)
1182  tqt_set_x11_event_filter(d->oldfilter);
1183 #endif
1184  delete d;
1185 }
1186 
1187 bool
1188 KColorDialog::eventFilter( TQObject *obj, TQEvent *ev )
1189 {
1190  if ((TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(d->htmlName)) || (TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(d->hsSelector)))
1191  switch(ev->type())
1192  {
1193  case TQEvent::DragEnter:
1194  case TQEvent::DragMove:
1195  case TQEvent::DragLeave:
1196  case TQEvent::Drop:
1197  case TQEvent::DragResponse:
1198  tqApp->sendEvent(d->patch, ev);
1199  return true;
1200  default:
1201  break;
1202  }
1203  return KDialogBase::eventFilter(obj, ev);
1204 }
1205 
1206 void
1207 KColorDialog::setDefaultColor( const TQColor& col )
1208 {
1209  if ( !d->cbDefaultColor )
1210  {
1211  //
1212  // a little space between
1213  //
1214  d->l_right->addSpacing(10);
1215 
1216  //
1217  // and the "default color" checkbox, under all items on the right side
1218  //
1219  d->cbDefaultColor = new TQCheckBox( i18n( "Default color" ), mainWidget() );
1220  d->cbDefaultColor->setChecked(true);
1221 
1222  d->l_right->addWidget( d->cbDefaultColor );
1223 
1224  mainWidget()->setMaximumSize( TQWIDGETSIZE_MAX, TQWIDGETSIZE_MAX ); // cancel setFixedSize()
1225  d->tl_layout->activate();
1226  mainWidget()->setMinimumSize( mainWidget()->sizeHint() );
1227  disableResize();
1228 
1229  connect( d->cbDefaultColor, TQT_SIGNAL( clicked() ), TQT_SLOT( slotDefaultColorClicked() ) );
1230  }
1231 
1232  d->defaultColor = col;
1233 
1234  slotDefaultColorClicked();
1235 }
1236 
1237 TQColor KColorDialog::defaultColor() const
1238 {
1239  return d->defaultColor;
1240 }
1241 
1242 void KColorDialog::slotDefaultColorClicked()
1243 {
1244  if ( d->cbDefaultColor->isChecked() )
1245  {
1246  d->selColor = d->defaultColor;
1247  showColor( d->selColor, i18n( "-default-" ) );
1248  } else
1249  {
1250  showColor( d->selColor, TQString::null );
1251  }
1252 }
1253 
1254 void
1255 KColorDialog::readSettings()
1256 {
1257  TDEConfigGroup group( TDEGlobal::config(), "Colors" );
1258 
1259  TQString palette = group.readEntry("CurrentPalette");
1260  d->table->setPalette(palette);
1261 }
1262 
1263 void
1264 KColorDialog::slotWriteSettings()
1265 {
1266  TDEConfigGroup group( TDEGlobal::config(), "Colors" );
1267 
1268  TQString palette = d->table->palette();
1269  if (!group.hasDefault("CurrentPalette") &&
1270  (d->table->palette() == d->originalPalette))
1271  {
1272  group.revertToDefault("CurrentPalette");
1273  }
1274  else
1275  {
1276  group.writeEntry("CurrentPalette", d->table->palette());
1277  }
1278 }
1279 
1280 TQColor
1281 KColorDialog::color() const
1282 {
1283  if ( d->cbDefaultColor && d->cbDefaultColor->isChecked() )
1284  return TQColor();
1285  if ( d->selColor.isValid() )
1286  d->table->addToRecentColors( d->selColor );
1287  return d->selColor;
1288 }
1289 
1290 void KColorDialog::setColor( const TQColor &col )
1291 {
1292  _setColor( col );
1293 }
1294 
1295 //
1296 // static function to display dialog and return color
1297 //
1298 int KColorDialog::getColor( TQColor &theColor, TQWidget *parent )
1299 {
1300  KColorDialog dlg( parent, "Color Selector", true );
1301  if ( theColor.isValid() )
1302  dlg.setColor( theColor );
1303  int result = dlg.exec();
1304 
1305  if ( result == Accepted )
1306  {
1307  theColor = dlg.color();
1308  }
1309 
1310  return result;
1311 }
1312 
1313 //
1314 // static function to display dialog and return color
1315 //
1316 int KColorDialog::getColor( TQColor &theColor, const TQColor& defaultCol, TQWidget *parent )
1317 {
1318  KColorDialog dlg( parent, "Color Selector", true );
1319  dlg.setDefaultColor( defaultCol );
1320  dlg.setColor( theColor );
1321  int result = dlg.exec();
1322 
1323  if ( result == Accepted )
1324  theColor = dlg.color();
1325 
1326  return result;
1327 }
1328 
1329 void KColorDialog::slotRGBChanged( void )
1330 {
1331  if (d->bRecursion) return;
1332  int red = d->redit->value();
1333  int grn = d->gedit->value();
1334  int blu = d->bedit->value();
1335 
1336  if ( red > 255 || red < 0 ) return;
1337  if ( grn > 255 || grn < 0 ) return;
1338  if ( blu > 255 || blu < 0 ) return;
1339 
1340  KColor col;
1341  col.setRgb( red, grn, blu );
1342  d->bEditRgb = true;
1343  _setColor( col );
1344  d->bEditRgb = false;
1345 }
1346 
1347 void KColorDialog::slotHtmlChanged( void )
1348 {
1349  if (d->bRecursion || d->htmlName->text().isEmpty()) return;
1350 
1351  TQString strColor( d->htmlName->text() );
1352 
1353  // Assume that a user does not want to type the # all the time
1354  if ( strColor[0] != '#' )
1355  {
1356  bool signalsblocked = d->htmlName->signalsBlocked();
1357  d->htmlName->blockSignals(true);
1358  strColor.prepend("#");
1359  d->htmlName->setText(strColor);
1360  d->htmlName->blockSignals(signalsblocked);
1361  }
1362 
1363  const TQColor color( strColor );
1364 
1365  if ( color.isValid() )
1366  {
1367  KColor col( color );
1368  d->bEditHtml = true;
1369  _setColor( col );
1370  d->bEditHtml = false;
1371  }
1372 }
1373 
1374 void KColorDialog::slotHSVChanged( void )
1375 {
1376  if (d->bRecursion) return;
1377  int hue = d->hedit->value();
1378  int sat = d->sedit->value();
1379  int val = d->vedit->value();
1380 
1381  if ( hue > 359 || hue < 0 ) return;
1382  if ( sat > 255 || sat < 0 ) return;
1383  if ( val > 255 || val < 0 ) return;
1384 
1385  KColor col;
1386  col.setHsv( hue, sat, val );
1387  d->bEditHsv = true;
1388  _setColor( col );
1389  d->bEditHsv = false;
1390 }
1391 
1392 void KColorDialog::slotHSChanged( int h, int s )
1393 {
1394  int _h, _s, v;
1395  d->selColor.hsv(&_h, &_s, &v);
1396  if (v < 0)
1397  v = 0;
1398  KColor col;
1399  col.setHsv( h, s, v );
1400  _setColor( col );
1401 }
1402 
1403 void KColorDialog::slotVChanged( int v )
1404 {
1405  int h, s, _v;
1406  d->selColor.hsv(&h, &s, &_v);
1407  KColor col;
1408  col.setHsv( h, s, v );
1409  _setColor( col );
1410 }
1411 
1412 void KColorDialog::slotColorSelected( const TQColor &color )
1413 {
1414  _setColor( color );
1415 }
1416 
1417 void KColorDialog::slotAddToCustomColors( )
1418 {
1419  d->table->addToCustomColors( d->selColor );
1420 }
1421 
1422 void KColorDialog::slotColorSelected( const TQColor &color, const TQString &name )
1423 {
1424  _setColor( color, name);
1425 }
1426 
1427 void KColorDialog::slotColorDoubleClicked
1428 (
1429  const TQColor & color,
1430  const TQString & name
1431 )
1432 {
1433  _setColor(color, name);
1434  accept();
1435 }
1436 
1437 void KColorDialog::_setColor(const KColor &color, const TQString &name)
1438 {
1439  if (color.isValid())
1440  {
1441  if (d->cbDefaultColor && d->cbDefaultColor->isChecked())
1442  d->cbDefaultColor->setChecked(false);
1443  d->selColor = color;
1444  }
1445  else
1446  {
1447  if (d->cbDefaultColor && d->cbDefaultColor->isChecked())
1448  d->cbDefaultColor->setChecked(true);
1449  d->selColor = d->defaultColor;
1450  }
1451 
1452  showColor( d->selColor, name );
1453 
1454  emit colorSelected( d->selColor );
1455 }
1456 
1457 // show but don't set into selColor, nor emit colorSelected
1458 void KColorDialog::showColor( const KColor &color, const TQString &name )
1459 {
1460  d->bRecursion = true;
1461 
1462  if (name.isEmpty())
1463  d->colorName->setText( i18n("-unnamed-"));
1464  else
1465  d->colorName->setText( name );
1466 
1467  d->patch->setColor( color );
1468 
1469  setRgbEdit( color );
1470  setHsvEdit( color );
1471  setHtmlEdit( color );
1472 
1473  int h, s, v;
1474  color.hsv( &h, &s, &v );
1475  d->hsSelector->setValues( h, s );
1476  d->valuePal->blockSignals(true);
1477  d->valuePal->setHue( h );
1478  d->valuePal->setSaturation( s );
1479  d->valuePal->setValue( v );
1480  d->valuePal->updateContents();
1481  d->valuePal->blockSignals(false);
1482  d->valuePal->repaint( false );
1483  d->bRecursion = false;
1484 }
1485 
1486 
1487 static TQWidget *kde_color_dlg_widget = 0;
1488 
1489 #ifdef Q_WS_X11
1490 static int kde_color_dlg_handler(XEvent *event)
1491 {
1492  if (event->type == ButtonRelease)
1493  {
1494  TQMouseEvent e( TQEvent::MouseButtonRelease, TQPoint(),
1495  TQPoint(event->xmotion.x_root, event->xmotion.y_root) , 0, 0 );
1496  TQApplication::sendEvent( kde_color_dlg_widget, &e );
1497  return true;
1498  }
1499  return false;
1500 }
1501 #endif
1502 void
1503 KColorDialog::slotColorPicker()
1504 {
1505  d->bColorPicking = true;
1506 #ifdef Q_WS_X11
1507  d->oldfilter = tqt_set_x11_event_filter(kde_color_dlg_handler);
1508 #endif
1509  kde_color_dlg_widget = this;
1510  grabMouse( tqcrossCursor );
1511  grabKeyboard();
1512 }
1513 
1514 void
1515 KColorDialog::mouseReleaseEvent( TQMouseEvent *e )
1516 {
1517  if (d->bColorPicking)
1518  {
1519  d->bColorPicking = false;
1520 #ifdef Q_WS_X11
1521  tqt_set_x11_event_filter(d->oldfilter);
1522  d->oldfilter = 0;
1523 #endif
1524  releaseMouse();
1525  releaseKeyboard();
1526  _setColor( grabColor( e->globalPos() ) );
1527  return;
1528  }
1529  KDialogBase::mouseReleaseEvent( e );
1530 }
1531 
1532 TQColor
1533 KColorDialog::grabColor(const TQPoint &p)
1534 {
1535  TQWidget *desktop = TQT_TQWIDGET(TQApplication::desktop());
1536  TQPixmap pm = TQPixmap::grabWindow( desktop->winId(), p.x(), p.y(), 1, 1);
1537  TQImage i = pm.convertToImage();
1538  return i.pixel(0,0);
1539 }
1540 
1541 void
1542 KColorDialog::keyPressEvent( TQKeyEvent *e )
1543 {
1544  if (d->bColorPicking)
1545  {
1546  if (e->key() == Key_Escape)
1547  {
1548  d->bColorPicking = false;
1549 #ifdef Q_WS_X11
1550  tqt_set_x11_event_filter(d->oldfilter);
1551  d->oldfilter = 0;
1552 #endif
1553  releaseMouse();
1554  releaseKeyboard();
1555  }
1556  e->accept();
1557  return;
1558  }
1559  KDialogBase::keyPressEvent( e );
1560 }
1561 
1562 void KColorDialog::setRgbEdit( const KColor &col )
1563 {
1564  if (d->bEditRgb) return;
1565  int r, g, b;
1566  col.rgb( &r, &g, &b );
1567 
1568  d->redit->setValue( r );
1569  d->gedit->setValue( g );
1570  d->bedit->setValue( b );
1571 }
1572 
1573 void KColorDialog::setHtmlEdit( const KColor &col )
1574 {
1575  if (d->bEditHtml) return;
1576  int r, g, b;
1577  col.rgb( &r, &g, &b );
1578  TQString num;
1579 
1580  num.sprintf("#%02X%02X%02X", r,g,b);
1581  d->htmlName->setText( num );
1582 }
1583 
1584 
1585 void KColorDialog::setHsvEdit( const KColor &col )
1586 {
1587  if (d->bEditHsv) return;
1588  int h, s, v;
1589  col.hsv( &h, &s, &v );
1590 
1591  d->hedit->setValue( h );
1592  d->sedit->setValue( s );
1593  d->vedit->setValue( v );
1594 }
1595 
1596 void KHSSelector::virtual_hook( int id, void* data )
1597 { KXYSelector::virtual_hook( id, data ); }
1598 
1599 void KValueSelector::virtual_hook( int id, void* data )
1600 { TDESelector::virtual_hook( id, data ); }
1601 
1602 void KPaletteTable::virtual_hook( int, void* )
1603 { /*BASE::virtual_hook( id, data );*/ }
1604 
1605 void KColorCells::virtual_hook( int, void* )
1606 { /*BASE::virtual_hook( id, data );*/ }
1607 
1608 void KColorPatch::virtual_hook( int, void* )
1609 { /*BASE::virtual_hook( id, data );*/ }
1610 
1611 void KColorDialog::virtual_hook( int id, void* data )
1612 { KDialogBase::virtual_hook( id, data ); }
1613 
1614 
1615 #include "kcolordialog.moc"
1616 //#endif
KColorDrag::decode
static bool decode(TQMimeSource *e, TQColor &col)
Decodes the MIME source e and puts the resulting color into col.
Definition: kcolordrag.cpp:89
KColorDialog::defaultColor
TQColor defaultColor() const
Definition: kcolordialog.cpp:1237
KImageEffect::dither
static TQImage & dither(TQImage &image, const TQColor *palette, int size)
KHSSelector
Widget for Hue/Saturation colour selection.
Definition: kcolordialog.h:58
KColorDialog::colorSelected
void colorSelected(const TQColor &col)
Emitted when a color is selected.
KPalette
KValueSelector::drawPalette
virtual void drawPalette(TQPixmap *pixmap)
Draws the contents of the widget on a pixmap, which is used for buffering.
Definition: kcolordialog.cpp:292
KPaletteTable
A color palette in table form.
Definition: kcolordialog.h:198
KHSSelector::drawPalette
virtual void drawPalette(TQPixmap *pixmap)
Draws the contents of the widget on a pixmap, which is used for buffering.
Definition: kcolordialog.cpp:232
KStaticDeleter
TDEGlobalSettings::dndEventDelay
static int dndEventDelay()
KDialogBase::disableResize
void disableResize()
Convenience method.
Definition: kdialogbase.cpp:531
KXYSelector::setRange
void setRange(int minX, int minY, int maxX, int maxY)
Sets the range of possible values.
Definition: tdeselect.cpp:54
KColorDialog::getColor
static int getColor(TQColor &theColor, TQWidget *parent=0L)
Creates a modal color dialog, let the user choose a color, and returns when the dialog is closed...
Definition: kcolordialog.cpp:1298
KMessageBox::sorry
static void sorry(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
Display an "Sorry" dialog.
Definition: tdemessagebox.cpp:825
KXYSelector::contentsRect
TQRect contentsRect() const
Definition: tdeselect.cpp:99
KDialogBase
A dialog base class with standard buttons and predefined layouts.
Definition: kdialogbase.h:191
KStaticDeleter::setObject
KDE_DEPRECATED type * setObject(type *obj, bool isArray=false)
TDEConfigGroup
KColorDrag
A drag-and-drop object for colors.
Definition: kcolordrag.h:36
KColorDialog::keyPressEvent
virtual void keyPressEvent(TQKeyEvent *)
Maps some keys to the actions buttons.
Definition: kcolordialog.cpp:1542
tdelocale.h
KDialog::spacingHint
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
Definition: kdialog.cpp:110
KPalette::save
bool save()
KColorDrag::canDecode
static bool canDecode(TQMimeSource *e)
Returns true if the MIME source e contains a color object.
Definition: kcolordrag.cpp:76
TDEConfigBase::writeEntry
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
KDialogBase::setMainWidget
void setMainWidget(TQWidget *widget)
Sets the main user definable widget.
Definition: kdialogbase.cpp:1431
TDELocale::insertCatalogue
void insertCatalogue(const TQString &catalog)
KColorDialog::setColor
void setColor(const TQColor &col)
Preselects a color.
Definition: kcolordialog.cpp:1290
KPalette::findColor
int findColor(const TQColor &color) const
I18N_NOOP2
#define I18N_NOOP2(comment, x)
KHSSelector::KHSSelector
KHSSelector(TQWidget *parent=0, const char *name=0)
Constructs a hue/saturation selection widget.
Definition: kcolordialog.cpp:211
KColorCells
A table of editable color cells.
Definition: kcolordialog.h:253
KValueSelector
Widget for color value selection.
Definition: kcolordialog.h:102
KColor
A color class that preserves both RGB and HSV values.
Definition: kcolordialog.h:164
KValueSelector::drawContents
virtual void drawContents(TQPainter *painter)
Reimplemented from TDESelector.
Definition: kcolordialog.cpp:287
TDESelector::contentsRect
TQRect contentsRect() const
Definition: tdeselect.cpp:257
KColorDialog::color
TQColor color() const
Returns the currently selected color.
Definition: kcolordialog.cpp:1281
KColorPatch
A color displayer.
Definition: kcolordialog.h:311
TDEConfigBase::hasDefault
bool hasDefault(const TQString &key) const
KColorDialog::setDefaultColor
void setDefaultColor(const TQColor &defaultCol)
Call this to make the dialog show a "Default Color" checkbox.
Definition: kcolordialog.cpp:1207
KDialogBase::keyPressEvent
virtual void keyPressEvent(TQKeyEvent *e)
Maps some keys to the actions buttons.
Definition: kdialogbase.cpp:1553
KLineEdit
An enhanced TQLineEdit widget for inputting text.
Definition: klineedit.h:145
TDEGlobal::locale
static TDELocale * locale()
KHSSelector::drawContents
virtual void drawContents(TQPainter *painter)
Reimplemented from KXYSelector.
Definition: kcolordialog.cpp:227
KColorDialog
A color selection dialog.
Definition: kcolordialog.h:377
KDialogBase::mainWidget
TQWidget * mainWidget()
Returns the main widget if any.
Definition: kdialogbase.cpp:1464
KPalette::getPaletteList
static TQStringList getPaletteList()
KDialogBase::okClicked
void okClicked()
The OK button was pressed.
TDESelector::orientation
Orientation orientation() const
Definition: tdeselect.h:184
KColorDialog::KColorDialog
KColorDialog(TQWidget *parent=0L, const char *name=0L, bool modal=false)
Constructs a color selection dialog.
Definition: kcolordialog.cpp:939
KXYSelector
KXYSelector is the base class for other widgets which provides the ability to choose from a two-dimen...
Definition: tdeselect.h:43
KValueSelector::KValueSelector
KValueSelector(TQWidget *parent=0, const char *name=0)
Constructs a widget for color selection.
Definition: kcolordialog.cpp:262
TDEGlobal::config
static TDEConfig * config()
TDEListBox
A variant of TQListBox that honors KDE&#39;s system-wide settings.
Definition: tdelistbox.h:40
TDEConfigBase::revertToDefault
void revertToDefault(const TQString &key)
TDEConfigBase::readEntry
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
KColorDialog::~KColorDialog
~KColorDialog()
Destroys the color selection dialog.
Definition: kcolordialog.cpp:1178
KPalette::addColor
int addColor(const TQColor &newColor, const TQString &newColorName=TQString::null)
TDESelector
TDESelector is the base class for other widgets which provides the ability to choose from a one-dimen...
Definition: tdeselect.h:159
KDialogBase::closeClicked
void closeClicked()
The Close button was pressed.
KColorDialog::grabColor
static TQColor grabColor(const TQPoint &p)
Gets the color from the pixel at point p on the screen.
Definition: kcolordialog.cpp:1533

tdeui

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

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.8.11
This website is maintained by Timothy Pearson.