• 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 #ifdef HAVE_CONFIG_H
68 #include "config.h"
69 #endif
70 
71 #ifdef Q_WS_X11
72 #include <X11/Xlib.h>
73 
74 // defined in qapplication_x11.cpp
75 typedef int (*QX11EventFilter) (XEvent*);
76 extern QX11EventFilter tqt_set_x11_event_filter (QX11EventFilter filter);
77 #endif
78 
79 struct ColorPaletteNameType
80 {
81  const char* m_fileName;
82  const char* m_displayName;
83 };
84 
85 const ColorPaletteNameType colorPaletteName[]=
86 {
87  { "Recent_Colors", I18N_NOOP2( "palette name", "* Recent Colors *" ) },
88  { "Custom_Colors", I18N_NOOP2( "palette name", "* Custom Colors *" ) },
89  { "40.colors", I18N_NOOP2( "palette name", "Forty Colors" ) },
90  { "Rainbow.colors",I18N_NOOP2( "palette name", "Rainbow Colors" ) },
91  { "Royal.colors", I18N_NOOP2( "palette name", "Royal Colors" ) },
92  { "Web.colors", I18N_NOOP2( "palette name", "Web Colors" ) },
93  { 0, 0 } // end of data
94 };
95 
96 const int recentColorIndex = 0;
97 const int customColorIndex = 1;
98 
99 class KColorSpinBox : public TQSpinBox
100 {
101 public:
102  KColorSpinBox(int minValue, int maxValue, int step, TQWidget* parent)
103  : TQSpinBox(minValue, maxValue, step, parent, "kcolorspinbox")
104  { }
105 
106  // Override Qt's braindead auto-selection.
107  virtual void valueChange()
108  {
109  updateDisplay();
110  emit valueChanged( value() );
111  emit valueChanged( currentValueText() );
112  }
113 
114 };
115 
116 
117 #define STANDARD_PAL_SIZE 17
118 
119 KColor::KColor()
120 : TQColor()
121 {
122  r = 0; g = 0; b = 0; h = 0; s = 0; v = 0;
123 }
124 
125 KColor::KColor( const KColor &col)
126 : TQColor( col )
127 {
128  h = col.h; s = col.s; v = col.v;
129  r = col.r; g = col.g; b = col.b;
130 }
131 
132 KColor::KColor( const TQColor &col)
133 : TQColor( col )
134 {
135  TQColor::getRgb(&r, &g, &b);
136  TQColor::getHsv(&h, &s, &v);
137 }
138 
139 bool KColor::operator==(const KColor& col) const
140 {
141  return (h == col.h) && (s == col.s) && (v == col.v) &&
142  (r == col.r) && (g == col.g) && (b == col.b);
143 }
144 
145 KColor& KColor::operator=(const KColor& col)
146 {
147  *(TQColor *)this = col;
148  h = col.h; s = col.s; v = col.v;
149  r = col.r; g = col.g; b = col.b;
150  return *this;
151 }
152 
153 void
154 KColor::setHsv(int _h, int _s, int _v)
155 {
156  h = _h; s = _s; v = _v;
157  TQColor::setHsv(h, s, v);
158  TQColor::rgb(&r, &g, &b);
159 }
160 
161 void
162 KColor::setRgb(int _r, int _g, int _b)
163 {
164  r = _r; g = _g; b = _b;
165  TQColor::setRgb(r, g, b);
166  TQColor::hsv(&h, &s, &v);
167 }
168 
169 void
170 KColor::rgb(int *_r, int *_g, int *_b) const
171 {
172  *_r = r; *_g = g; *_b = b;
173 }
174 
175 void
176 KColor::hsv(int *_h, int *_s, int *_v) const
177 {
178  *_h = h; *_s = s; *_v = v;
179 }
180 
181 
182 static TQColor *standardPalette = 0;
183 static KStaticDeleter<TQColor> spd;
184 
185 static void createStandardPalette()
186 {
187  if ( standardPalette )
188  return;
189 
190  spd.setObject(standardPalette, new TQColor [STANDARD_PAL_SIZE], true/*array*/);
191 
192  int i = 0;
193 
194  standardPalette[i++] = Qt::red;
195  standardPalette[i++] = Qt::green;
196  standardPalette[i++] = Qt::blue;
197  standardPalette[i++] = Qt::cyan;
198  standardPalette[i++] = Qt::magenta;
199  standardPalette[i++] = Qt::yellow;
200  standardPalette[i++] = Qt::darkRed;
201  standardPalette[i++] = Qt::darkGreen;
202  standardPalette[i++] = Qt::darkBlue;
203  standardPalette[i++] = Qt::darkCyan;
204  standardPalette[i++] = Qt::darkMagenta;
205  standardPalette[i++] = Qt::darkYellow;
206  standardPalette[i++] = Qt::white;
207  standardPalette[i++] = Qt::lightGray;
208  standardPalette[i++] = Qt::gray;
209  standardPalette[i++] = Qt::darkGray;
210  standardPalette[i++] = Qt::black;
211 }
212 
213 
214 KHSSelector::KHSSelector( TQWidget *parent, const char *name )
215  : KXYSelector( parent, name )
216 {
217  setRange( 0, 0, 359, 255 );
218 }
219 
220 void KHSSelector::updateContents()
221 {
222  drawPalette(&pixmap);
223 }
224 
225 void KHSSelector::resizeEvent( TQResizeEvent * )
226 {
227  updateContents();
228 }
229 
230 void KHSSelector::drawContents( TQPainter *painter )
231 {
232  painter->drawPixmap( contentsRect().x(), contentsRect().y(), pixmap );
233 }
234 
235 void KHSSelector::drawPalette( TQPixmap *pixmap )
236 {
237  int xSize = contentsRect().width(), ySize = contentsRect().height();
238  TQImage image( xSize, ySize, 32 );
239  TQColor col;
240  int h, s;
241  uint *p;
242 
243  for ( s = ySize-1; s >= 0; s-- )
244  {
245  p = (uint *) image.scanLine( ySize - s - 1 );
246  for( h = 0; h < xSize; h++ )
247  {
248  col.setHsv( 359*h/(xSize-1), 255*s/((ySize == 1) ? 1 : ySize-1), 192 );
249  *p = col.rgb();
250  p++;
251  }
252  }
253 
254  if ( TQColor::numBitPlanes() <= 8 )
255  {
256  createStandardPalette();
257  KImageEffect::dither( image, standardPalette, STANDARD_PAL_SIZE );
258  }
259  pixmap->convertFromImage( image );
260 }
261 
262 
263 //-----------------------------------------------------------------------------
264 
265 KValueSelector::KValueSelector( TQWidget *parent, const char *name )
266  : TDESelector( Qt::Vertical, parent, name ), _hue(0), _sat(0)
267 {
268  setRange( 0, 255 );
269  pixmap.setOptimization( TQPixmap::BestOptim );
270 }
271 
272 KValueSelector::KValueSelector(Orientation o, TQWidget *parent, const char *name
273  )
274  : TDESelector( o, parent, name), _hue(0), _sat(0)
275 {
276  setRange( 0, 255 );
277  pixmap.setOptimization( TQPixmap::BestOptim );
278 }
279 
280 void KValueSelector::updateContents()
281 {
282  drawPalette(&pixmap);
283 }
284 
285 void KValueSelector::resizeEvent( TQResizeEvent * )
286 {
287  updateContents();
288 }
289 
290 void KValueSelector::drawContents( TQPainter *painter )
291 {
292  painter->drawPixmap( contentsRect().x(), contentsRect().y(), pixmap );
293 }
294 
295 void KValueSelector::drawPalette( TQPixmap *pixmap )
296 {
297  int xSize = contentsRect().width(), ySize = contentsRect().height();
298  TQImage image( xSize, ySize, 32 );
299  TQColor col;
300  uint *p;
301  QRgb rgb;
302 
303  if ( orientation() == Qt::Horizontal )
304  {
305  for ( int v = 0; v < ySize; v++ )
306  {
307  p = (uint *) image.scanLine( ySize - v - 1 );
308 
309  for( int x = 0; x < xSize; x++ )
310  {
311  col.setHsv( _hue, _sat, 255*x/((xSize == 1) ? 1 : xSize-1) );
312  rgb = col.rgb();
313  *p++ = rgb;
314  }
315  }
316  }
317 
318  if( orientation() == Qt::Vertical )
319  {
320  for ( int v = 0; v < ySize; v++ )
321  {
322  p = (uint *) image.scanLine( ySize - v - 1 );
323  col.setHsv( _hue, _sat, 255*v/((ySize == 1) ? 1 : ySize-1) );
324  rgb = col.rgb();
325  for ( int i = 0; i < xSize; i++ )
326  *p++ = rgb;
327  }
328  }
329 
330  if ( TQColor::numBitPlanes() <= 8 )
331  {
332  createStandardPalette();
333  KImageEffect::dither( image, standardPalette, STANDARD_PAL_SIZE );
334  }
335  pixmap->convertFromImage( image );
336 }
337 
338 //-----------------------------------------------------------------------------
339 
340 KColorCells::KColorCells( TQWidget *parent, int rows, int cols )
341  : TQGridView( parent )
342 {
343  shade = true;
344  setNumRows( rows );
345  setNumCols( cols );
346  colors = new TQColor [ rows * cols ];
347 
348  for ( int i = 0; i < rows * cols; i++ )
349  colors[i] = TQColor();
350 
351  selected = 0;
352  inMouse = false;
353 
354  // Drag'n'Drop
355  setAcceptDrops( true);
356 
357  setHScrollBarMode( AlwaysOff );
358  setVScrollBarMode( AlwaysOff );
359  viewport()->setBackgroundMode( PaletteBackground );
360  setBackgroundMode( PaletteBackground );
361 }
362 
363 KColorCells::~KColorCells()
364 {
365  delete [] colors;
366 }
367 
368 void KColorCells::setColor( int colNum, const TQColor &col )
369 {
370  colors[colNum] = col;
371  updateCell( colNum/numCols(), colNum%numCols() );
372 }
373 
374 void KColorCells::paintCell( TQPainter *painter, int row, int col )
375 {
376  TQBrush brush;
377  int w = 1;
378 
379  if (shade)
380  {
381  qDrawShadePanel( painter, 1, 1, cellWidth()-2,
382  cellHeight()-2, colorGroup(), true, 1, &brush );
383  w = 2;
384  }
385  TQColor color = colors[ row * numCols() + col ];
386  if (!color.isValid())
387  {
388  if (!shade) return;
389  color = backgroundColor();
390  }
391 
392  painter->setPen( color );
393  painter->setBrush( TQBrush( color ) );
394  painter->drawRect( w, w, cellWidth()-w*2, cellHeight()-w*2 );
395 
396  if ( row * numCols() + col == selected )
397  painter->drawWinFocusRect( w, w, cellWidth()-w*2, cellHeight()-w*2 );
398 }
399 
400 void KColorCells::resizeEvent( TQResizeEvent * )
401 {
402  setCellWidth( width() / numCols() );
403  setCellHeight( height() / numRows() );
404 }
405 
406 void KColorCells::mousePressEvent( TQMouseEvent *e )
407 {
408  inMouse = true;
409  mPos = e->pos();
410 }
411 
412 int KColorCells::posToCell(const TQPoint &pos, bool ignoreBorders)
413 {
414  int row = pos.y() / cellHeight();
415  int col = pos.x() / cellWidth();
416  int cell = row * numCols() + col;
417 
418  if (!ignoreBorders)
419  {
420  int border = 2;
421  int x = pos.x() - col * cellWidth();
422  int y = pos.y() - row * cellHeight();
423  if ( (x < border) || (x > cellWidth()-border) ||
424  (y < border) || (y > cellHeight()-border))
425  return -1;
426  }
427  return cell;
428 }
429 
430 void KColorCells::mouseMoveEvent( TQMouseEvent *e )
431 {
432  if( !(e->state() & Qt::LeftButton)) return;
433 
434  if(inMouse) {
435  int delay = TDEGlobalSettings::dndEventDelay();
436  if(e->x() > mPos.x()+delay || e->x() < mPos.x()-delay ||
437  e->y() > mPos.y()+delay || e->y() < mPos.y()-delay){
438  // Drag color object
439  int cell = posToCell(mPos);
440  if ((cell != -1) && colors[cell].isValid())
441  {
442  KColorDrag *d = new KColorDrag( colors[cell], this);
443  d->dragCopy();
444  }
445  }
446  }
447 }
448 
449 void KColorCells::dragEnterEvent( TQDragEnterEvent *event)
450 {
451  event->accept( acceptDrags && KColorDrag::canDecode( event));
452 }
453 
454 void KColorCells::dropEvent( TQDropEvent *event)
455 {
456  TQColor c;
457  if( KColorDrag::decode( event, c)) {
458  int cell = posToCell(event->pos(), true);
459  setColor(cell,c);
460  }
461 }
462 
463 void KColorCells::mouseReleaseEvent( TQMouseEvent *e )
464 {
465  int cell = posToCell(mPos);
466  int currentCell = posToCell(e->pos());
467 
468  // If we release the mouse in another cell and we don't have
469  // a drag we should ignore this event.
470  if (currentCell != cell)
471  cell = -1;
472 
473  if ( (cell != -1) && (selected != cell) )
474  {
475  int prevSel = selected;
476  selected = cell;
477  updateCell( prevSel/numCols(), prevSel%numCols() );
478  updateCell( cell/numCols(), cell%numCols() );
479  }
480 
481  inMouse = false;
482  if (cell != -1)
483  emit colorSelected( cell );
484 }
485 
486 void KColorCells::mouseDoubleClickEvent( TQMouseEvent * /*e*/ )
487 {
488  int cell = posToCell(mPos);
489 
490  if (cell != -1)
491  emit colorDoubleClicked( cell );
492 }
493 
494 
495 //-----------------------------------------------------------------------------
496 
497 KColorPatch::KColorPatch( TQWidget *parent ) : TQFrame( parent )
498 {
499  setFrameStyle( TQFrame::Panel | TQFrame::Sunken );
500  colContext = 0;
501  setAcceptDrops( true);
502 }
503 
504 KColorPatch::~KColorPatch()
505 {
506  if ( colContext )
507  TQColor::destroyAllocContext( colContext );
508 }
509 
510 void KColorPatch::setColor( const TQColor &col )
511 {
512  if ( colContext )
513  TQColor::destroyAllocContext( colContext );
514  colContext = TQColor::enterAllocContext();
515  color.setRgb( col.rgb() );
516  color.alloc();
517  TQColor::leaveAllocContext();
518 
519  TQPainter painter;
520 
521  painter.begin( this );
522  drawContents( &painter );
523  painter.end();
524 }
525 
526 void KColorPatch::drawContents( TQPainter *painter )
527 {
528  painter->setPen( color );
529  painter->setBrush( TQBrush( color ) );
530  painter->drawRect( contentsRect() );
531 }
532 
533 void KColorPatch::mouseMoveEvent( TQMouseEvent *e )
534 {
535  // Drag color object
536  if( !(e->state() & Qt::LeftButton)) return;
537  KColorDrag *d = new KColorDrag( color, this);
538  d->dragCopy();
539 }
540 
541 void KColorPatch::dragEnterEvent( TQDragEnterEvent *event)
542 {
543  event->accept( KColorDrag::canDecode( event));
544 }
545 
546 void KColorPatch::dropEvent( TQDropEvent *event)
547 {
548  TQColor c;
549  if( KColorDrag::decode( event, c)) {
550  setColor( c);
551  emit colorChanged( c);
552  }
553 }
554 
555 class KPaletteTable::KPaletteTablePrivate
556 {
557 public:
558  TQMap<TQString,TQColor> m_namedColorMap;
559 };
560 
561 KPaletteTable::KPaletteTable( TQWidget *parent, int minWidth, int cols)
562  : TQWidget( parent ), cells(0), mPalette(0), mMinWidth(minWidth), mCols(cols)
563 {
564  d = new KPaletteTablePrivate;
565 
566  i18n_namedColors = i18n("Named Colors");
567 
568  TQStringList diskPaletteList = KPalette::getPaletteList();
569  TQStringList paletteList;
570 
571  // We must replace the untranslated file names by translate names (of course only for KDE's standard palettes)
572  for ( int i = 0; colorPaletteName[i].m_fileName; ++i )
573  {
574  diskPaletteList.remove( colorPaletteName[i].m_fileName );
575  paletteList.append( i18n( "palette name", colorPaletteName[i].m_displayName ) );
576  }
577  paletteList += diskPaletteList;
578  paletteList.append( i18n_namedColors );
579 
580  TQVBoxLayout *layout = new TQVBoxLayout( this );
581 
582  combo = new TQComboBox( false, this );
583  combo->insertStringList( paletteList );
584  layout->addWidget(combo);
585 
586  sv = new TQScrollView( this );
587  TQSize cellSize = TQSize( mMinWidth, 120);
588  sv->setHScrollBarMode( TQScrollView::AlwaysOff);
589  sv->setVScrollBarMode( TQScrollView::AlwaysOn);
590  TQSize minSize = TQSize(sv->verticalScrollBar()->width(), 0);
591  minSize += TQSize(sv->frameWidth(), 0);
592  minSize += TQSize(cellSize);
593  sv->setFixedSize(minSize);
594  layout->addWidget(sv);
595 
596  mNamedColorList = new TDEListBox( this, "namedColorList", 0 );
597  mNamedColorList->setFixedSize(minSize);
598  mNamedColorList->hide();
599  layout->addWidget(mNamedColorList);
600  connect( mNamedColorList, TQT_SIGNAL(highlighted( const TQString & )),
601  this, TQT_SLOT( slotColorTextSelected( const TQString & )) );
602 
603  setFixedSize( sizeHint());
604  connect( combo, TQT_SIGNAL(activated(const TQString &)),
605  this, TQT_SLOT(slotSetPalette( const TQString &)));
606 }
607 
608 KPaletteTable::~KPaletteTable()
609 {
610  delete mPalette;
611  delete d;
612 }
613 
614 TQString
615 KPaletteTable::palette() const
616 {
617  return combo->currentText();
618 }
619 
620 
621 static const char * const *namedColorFilePath( void )
622 {
623  //
624  // 2000-02-05 Espen Sand.
625  // Add missing filepaths here. Make sure the last entry is 0!
626  //
627  static const char * const path[] =
628  {
629 #ifdef X11_RGBFILE
630  X11_RGBFILE,
631 #endif
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:295
KColorDrag::setColor
void setColor(const TQColor &col)
Sets the color of the drag to col.
Definition: kcolordrag.cpp:40
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:235
KPalette::nrColors
int nrColors() const
KStaticDeleter
Vertical
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)
KPalette::name
TQString name() const
KPaletteTable::i18n_recentColors
TQString i18n_recentColors
KDE4: remove
Definition: kcolordialog.h:227
TDEConfigGroup
KPalette::color
TQColor color(int index)
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
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:214
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:290
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
KPaletteTable::i18n_namedColors
TQString i18n_namedColors
KDE4: remove
Definition: kcolordialog.h:228
KPalette::colorName
TQString colorName(int index)
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:230
Horizontal
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:265
TDEGlobal::config
static TDEConfig * config()
TDEListBox
A variant of TQListBox that honors KDE's system-wide settings.
Definition: tdelistbox.h:40
KColorDialog::~KColorDialog
~KColorDialog()
Destroys the color selection dialog.
Definition: kcolordialog.cpp:1178
TDEStdAccel::name
TQString name(StdAccel id)
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
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.8.8
This website is maintained by Timothy Pearson.