[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
klfcolorchooser.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfcolorchooser.cpp
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfcolorchooser.cpp 603 2011-02-26 23:14:55Z phfaist $ */
23 
24 #include <stdio.h>
25 
26 #include <QAction>
27 #include <QMenu>
28 #include <QStylePainter>
29 #include <QColorDialog>
30 #include <QPaintEvent>
31 #include <QStyle>
32 #include <QPlastiqueStyle>
33 #include <QStyleOptionButton>
34 #include <QRegExp>
35 
36 #include "klfcolorchooser.h"
37 #include "klfcolorchooser_p.h"
38 #include "klfguiutil.h"
39 
40 #include <ui_klfcolorchoosewidget.h>
41 #include <ui_klfcolordialog.h>
42 
43 
44 
45 // -------------------------------------------------------------------
46 
47 
49 {
50  u = new Ui::KLFColorDialog;
51  u->setupUi(this);
52  setObjectName("KLFColorDialog");
53 }
55 {
56  delete u;
57 }
58 
60 {
61  return u->mColorChooseWidget;
62 }
63 
64 QColor KLFColorDialog::getColor(QColor startwith, bool alphaenabled, QWidget *parent)
65 {
66  KLFColorDialog dlg(parent);
67  dlg.u->mColorChooseWidget->setAlphaEnabled(alphaenabled);
68  dlg.u->mColorChooseWidget->setColor(startwith);
69  int r = dlg.exec();
70  if ( r != QDialog::Accepted )
71  return QColor();
72  QColor color = dlg.u->mColorChooseWidget->color();
74  return color;
75 }
76 
77 // -------------------------------------------------------------------
78 
79 KLFColorClickSquare::KLFColorClickSquare(QColor color, int size, bool removable, QWidget *parent)
80  : QWidget(parent), _color(color), _size(size), _removable(removable)
81 {
82  setFocusPolicy(Qt::StrongFocus);
83  setFixedSize(_size, _size);
84  setContextMenuPolicy(Qt::DefaultContextMenu);
85 }
87 {
88  QStylePainter p(this);
89  p.fillRect(0, 0, width(), height(), QBrush(_color));
90  if (hasFocus()) {
91  QStyleOptionFocusRect option;
92  option.initFrom(this);
93  option.backgroundColor = QColor(0,0,0,0);
94  p.drawPrimitive(QStyle::PE_FrameFocusRect, option);
95  }
96 }
98 {
99  activate();
100 }
102 {
103  if (kev->key() == Qt::Key_Space) {
104  activate();
105  }
106  return QWidget::keyPressEvent(kev);
107 }
109 {
110  if (_removable) {
111  QMenu *menu = new QMenu(this);
112  menu->addAction("Remove", this, SLOT(internalWantRemove()));
113  menu->popup(event->globalPos());
114  }
115 }
116 void KLFColorClickSquare::internalWantRemove()
117 {
118  emit wantRemove();
119  emit wantRemoveColor(_color);
120 }
121 
122 // -------------------------------------------------------------------
123 
125  : QWidget(parent), _img()
126 {
127 }
128 
130 {
131  _color = newcolor;
132  update();
133  emit colorChanged(_color);
134 }
136 {
137  QStringList strlist = panetype.split("+");
138  _colorcomponent = strlist[0].toLower();
139  _colorcomponent_b = strlist[1].toLower();
140 }
142 {
143  QPainter p(this);
144  // background: a checker grid to distinguish transparency
145  p.fillRect(0,0,width(),height(), QBrush(QPixmap(":/pics/checker.png")));
146  // then prepare an image for our gradients
147  int x;
148  int y;
149  _img = QImage(width(), height(), QImage::Format_ARGB32);
150  double xfac = (double)valueAMax() / (_img.width()-1);
151  double yfac = (double)valueBMax() / (_img.height()-1);
152  for (x = 0; x < _img.width(); ++x) {
153  for (y = 0; y < _img.height(); ++y) {
154  _img.setPixel(x, y, colorFromValues(_color, (int)(xfac*x), (int)(yfac*y)).rgba());
155  }
156  }
157  p.drawImage(0, 0, _img);
158  // draw crosshairs
159  QColor hairscol = qGray(_color.rgb()) > 80 ? Qt::black : Qt::white;
160  if ( ! _colorcomponent.isEmpty() && _colorcomponent != "fix" ) {
161  p.setPen(QPen(hairscol, 1.f, Qt::DotLine));
162  x = (int)(valueA()/xfac);
163  if (x < 0) x = 0; if (x >= width()) x = width()-1;
164  p.drawLine(x, 0, x, height());
165  }
166  if ( ! _colorcomponent_b.isEmpty() && _colorcomponent_b != "fix" ) {
167  p.setPen(QPen(hairscol, 1.f, Qt::DotLine));
168  y = (int)(valueB()/yfac);
169  if (y < 0) y = 0; if (y >= height()) y = height()-1;
170  p.drawLine(0, y, width(), y);
171  }
172 }
174 {
175  double xfac = (double)valueAMax() / (_img.width()-1);
176  double yfac = (double)valueBMax() / (_img.height()-1);
177  int x = e->pos().x();
178  int y = e->pos().y();
179 
180  setColor(colorFromValues(_color, (int)(x*xfac), (int)(y*yfac)));
181 }
183 {
184  double xfac = (double)valueAMax() / (_img.width()-1);
185  double yfac = (double)valueBMax() / (_img.height()-1);
186  int x = e->pos().x();
187  int y = e->pos().y();
188  if (x < 0) x = 0; if (x >= width()) x = width()-1;
189  if (y < 0) y = 0; if (y >= height()) y = height()-1;
190 
191  setColor(colorFromValues(_color, (int)(x*xfac), (int)(y*yfac)));
192 }
194 {
195  int step = - 10 * e->delta() / 120;
196  // isA: TRUE if we are modifying component A, if FALSE then modifying component B
197 
198  bool isA = (e->orientation() == Qt::Horizontal);
199  if (isA && _colorcomponent=="fix")
200  isA = false;
201  if (!isA && _colorcomponent_b=="fix")
202  isA = true;
203  if (isA) {
204  // the first component
206  } else {
208  }
209  e->accept();
210 }
211 
212 
213 // -------------------------------------------------------------------
214 
215 
217  : QGridLayout(parent), _ncols(columns),
218  _currow(0), _curcol(0)
219 {
220  addItem(new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed), 0, _ncols);
221 }
223 {
226  _curcol++;
227  if (_curcol >= _ncols) {
228  _curcol = 0;
229  _currow++;
230  }
231 }
233 {
234  int k;
235  for (k = 0; k < mGridFlowWidgets.size(); ++k) {
236  // because KLFColorClickSquare::wantRemoveColor() can call this by a chain of
237  // signal/slots; and we shouldn't delete an object inside one of its handlers
238  //delete mGridFlowWidgets[k];
239  mGridFlowWidgets[k]->deleteLater();
240  }
242  _currow = _curcol = 0;
243 }
244 
245 
246 // -------------------------------------------------------------------
247 
248 
250 {
251  return valueFromNewColor(color, _colorcomponent);
252 }
254 {
255  return valueFromNewColor(color, _colorcomponent_b);
256 }
258 {
259  int value = -1;
260  if (component == "hue") {
261  value = color.hue();
262  } else if (component == "sat") {
263  value = color.saturation();
264  } else if (component == "val") {
265  value = color.value();
266  } else if (component == "red") {
267  value = color.red();
268  } else if (component == "green") {
269  value = color.green();
270  } else if (component == "blue") {
271  value = color.blue();
272  } else if (component == "alpha") {
273  value = color.alpha();
274  } else if (component == "fix" || component.isEmpty()) {
275  value = -1;
276  } else {
277  qWarning("Unknown color component property : %s", component.toLocal8Bit().constData());
278  }
279  return value;
280 }
281 
283 {
284  if (component == "hue")
285  return 359;
286  else if (component == "sat" || component == "val" ||
287  component == "red" || component == "green" ||
288  component == "blue" || component == "alpha")
289  return 255;
290  else if (component == "fix" || component.isEmpty())
291  return -1;
292 
293  qWarning("Unknown color component property : %s", component.toLocal8Bit().constData());
294  return -1;
295 }
296 
298 {
299  QColor col = base;
300  /* printf("colorFromValues(%s/alpha=%d, %d, %d): My components:(%s+%s);\n", qPrintable(col.name()),
301  col.alpha(), a, b, qPrintable(_colorcomponent), qPrintable(_colorcomponent_b)); */
302  if (_colorcomponent == "hue") {
303  col.setHsv(a, col.saturation(), col.value());
304  col.setAlpha(base.alpha());
305  } else if (_colorcomponent == "sat") {
306  col.setHsv(col.hue(), a, col.value());
307  col.setAlpha(base.alpha());
308  } else if (_colorcomponent == "val") {
309  col.setHsv(col.hue(), col.saturation(), a);
310  col.setAlpha(base.alpha());
311  } else if (_colorcomponent == "red") {
312  col.setRgb(a, col.green(), col.blue());
313  col.setAlpha(base.alpha());
314  } else if (_colorcomponent == "green") {
315  col.setRgb(col.red(), a, col.blue());
316  col.setAlpha(base.alpha());
317  } else if (_colorcomponent == "blue") {
318  col.setRgb(col.red(), col.green(), a);
319  col.setAlpha(base.alpha());
320  } else if (_colorcomponent == "alpha") {
321  col.setAlpha(a);
322  } else if (_colorcomponent == "fix") {
323  // no change to col
324  } else {
325  qWarning("Unknown color component property : %s", _colorcomponent.toLocal8Bit().constData());
326  }
327  QColor base2 = col;
328  // printf("\tnew color is (%s/alpha=%d);\n", qPrintable(col.name()), col.alpha());
329  if ( ! _colorcomponent_b.isEmpty() && _colorcomponent_b != "fix" ) {
330  // printf("\twe have a second component\n");
331  if (_colorcomponent_b == "hue") {
332  col.setHsv(b, col.saturation(), col.value());
333  col.setAlpha(base2.alpha());
334  } else if (_colorcomponent_b == "sat") {
335  col.setHsv(col.hue(), b, col.value());
336  col.setAlpha(base2.alpha());
337  } else if (_colorcomponent_b == "val") {
338  col.setHsv(col.hue(), col.saturation(), b);
339  col.setAlpha(base2.alpha());
340  } else if (_colorcomponent_b == "red") {
341  col.setRgb(b, col.green(), col.blue());
342  col.setAlpha(base2.alpha());
343  } else if (_colorcomponent_b == "green") {
344  col.setRgb(col.red(), b, col.blue());
345  col.setAlpha(base2.alpha());
346  } else if (_colorcomponent_b == "blue") {
347  col.setRgb(col.red(), col.blue(), b);
348  col.setAlpha(base2.alpha());
349  } else if (_colorcomponent_b == "alpha") {
350  col.setAlpha(b);
351  } else {
352  qWarning("Unknown color component property : %s", _colorcomponent_b.toLocal8Bit().constData());
353  }
354  }
355  // printf("\tand color is finally %s/alpha=%d\n", qPrintable(col.name()), col.alpha());
356  return col;
357 }
359 {
360  QColor oldcolor = _color;
361  _color = colorFromValues(_color, a, b);
362  /* printf("My components:(%s+%s); New color is %s/alpha=%d\n", _colorcomponent.toLocal8Bit().constData(),
363  _colorcomponent_b.toLocal8Bit().constData(), _color.name().toLocal8Bit().constData(), _color.alpha()); */
364  if ( oldcolor != _color )
365  return true;
366  return false;
367 }
368 
369 
370 // -------------------------------------------------------------------
371 
372 
374  : QSpinBox(parent)
375 {
376  _color = Qt::black;
377 
378  setColorComponent("hue");
379  setColor(_color);
380 
381  connect(this, SIGNAL(valueChanged(int)), this, SLOT(internalChanged(int)));
382 
383  setValue(valueAFromNewColor(_color));
384 }
385 
387 {
388  _colorcomponent = comp.toLower();
389  setMinimum(0);
390  setMaximum(valueAMax());
391 }
392 
393 void KLFColorComponentSpinBox::internalChanged(int newvalue)
394 {
395  if ( refreshColorFromInternalValues(newvalue) )
396  emit colorChanged(_color);
397 }
398 
400 {
401  int value = valueAFromNewColor(color);
402  /* printf("My components:(%s+%s); setColor(%s/alpha=%d); new value = %d\n",
403  _colorcomponent.toLocal8Bit().constData(), _colorcomponent_b.toLocal8Bit().constData(),
404  color.name().toLocal8Bit().constData(), color.alpha(), value); */
405  _color = color;
406  setValue(value); // will emit QSpinBox::valueChanged() --> internalChanged() --> colorChanged()
407 }
408 
409 
410 // -------------------------------------------------------------------
411 
412 
413 KLFColorList * KLFColorChooseWidget::_recentcolors = 0;
414 KLFColorList * KLFColorChooseWidget::_standardcolors = 0;
415 KLFColorList * KLFColorChooseWidget::_customcolors = 0;
416 
417 // static
419 {
421  _recentcolors->list = recentcolors;
422  _recentcolors->notifyListChanged();
423  _customcolors->list = customcolors;
424  _customcolors->notifyListChanged();
425 }
426 // static
428 {
429  ensureColorListsInstance(); return _recentcolors->list;
430 }
431 // static
433  ensureColorListsInstance(); return _customcolors->list;
434 }
435 
436 
438  : QWidget(parent)
439 {
440  u = new Ui::KLFColorChooseWidget;
441  u->setupUi(this);
442  setObjectName("KLFColorChooseWidget");
443 
444  _alphaenabled = true;
445 
447 
448  if (_standardcolors->list.size() == 0) {
449  // add a few standard colors.
450  QList<QRgb> rgbs;
451  // inspired from the "Forty Colors" Palette in KDE3 color dialog
452  rgbs << 0x000000 << 0x303030 << 0x585858 << 0x808080 << 0xa0a0a0 << 0xc3c3c3
453  << 0xdcdcdc << 0xffffff << 0x400000 << 0x800000 << 0xc00000 << 0xff0000
454  << 0xffc0c0 << 0x004000 << 0x008000 << 0x00c000 << 0x00ff00 << 0xc0ffc0
455  << 0x000040 << 0x000080 << 0x0000c0 << 0x0000ff << 0xc0c0ff << 0x404000
456  << 0x808000 << 0xc0c000 << 0xffff00 << 0xffffc0 << 0x004040 << 0x008080
457  << 0x00c0c0 << 0x00ffff << 0xc0ffff << 0x400040 << 0x800080 << 0xc000c0
458  << 0xff00ff << 0xffc0ff << 0xc05800 << 0xff8000 << 0xffa858 << 0xffdca8 ;
459  for (int k = 0; k < rgbs.size(); ++k)
460  _standardcolors->list.append(QColor(QRgb(rgbs[k])));
461  }
462 
463  _connectedColorChoosers.append(u->mDisplayColor);
464  _connectedColorChoosers.append(u->mHueSatPane);
465  _connectedColorChoosers.append(u->mValPane);
466  _connectedColorChoosers.append(u->mAlphaPane);
467  _connectedColorChoosers.append(u->mColorTriangle);
468  _connectedColorChoosers.append(u->mHueSlider);
469  _connectedColorChoosers.append(u->mSatSlider);
470  _connectedColorChoosers.append(u->mValSlider);
471  _connectedColorChoosers.append(u->mRedSlider);
472  _connectedColorChoosers.append(u->mGreenSlider);
473  _connectedColorChoosers.append(u->mBlueSlider);
474  _connectedColorChoosers.append(u->mAlphaSlider);
475  _connectedColorChoosers.append(u->spnHue);
476  _connectedColorChoosers.append(u->spnSat);
477  _connectedColorChoosers.append(u->spnVal);
478  _connectedColorChoosers.append(u->spnRed);
479  _connectedColorChoosers.append(u->spnGreen);
480  _connectedColorChoosers.append(u->spnBlue);
481  _connectedColorChoosers.append(u->spnAlpha);
482 
483  KLFGridFlowLayout *lytRecent = new KLFGridFlowLayout(12, u->mRecentColorsPalette);
484  lytRecent->setSpacing(2);
485  KLFGridFlowLayout *lytStandard = new KLFGridFlowLayout(12, u->mStandardColorsPalette);
486  lytStandard->setSpacing(2);
487  KLFGridFlowLayout *lytCustom = new KLFGridFlowLayout(12, u->mCustomColorsPalette);
488  lytCustom->setSpacing(2);
489 
490  connect(_recentcolors, SIGNAL(listChanged()), this, SLOT(updatePaletteRecent()));
491  connect(_standardcolors, SIGNAL(listChanged()), this, SLOT(updatePaletteStandard()));
492  connect(_customcolors, SIGNAL(listChanged()), this, SLOT(updatePaletteCustom()));
493 
494  updatePalettes();
495 
496  int k;
497  for (k = 0; k < _connectedColorChoosers.size(); ++k) {
498  connect(_connectedColorChoosers[k], SIGNAL(colorChanged(const QColor&)),
499  this, SLOT(internalColorChanged(const QColor&)));
500  }
501 
502  connect(u->lstNames, SIGNAL(itemClicked(QListWidgetItem*)),
504  connect(u->txtHex, SIGNAL(textChanged(const QString&)),
505  this, SLOT(internalColorNameSet(const QString&)));
506 
507  connect(u->btnAddCustomColor, SIGNAL(clicked()),
508  this, SLOT(setCurrentToCustomColor()));
509 
510  QStringList colornames = QColor::colorNames();
511  for (k = 0; k < colornames.size(); ++k) {
512  QPixmap colsample(16, 16);
513  colsample.fill(QColor(colornames[k]));
514  new QListWidgetItem(QIcon(colsample), colornames[k], u->lstNames);
515  }
516 
517  internalColorChanged(_color);
518 }
519 
521 {
522  QColor newcolor = wanted_newcolor;
523  if (!_alphaenabled)
524  newcolor.setAlpha(255);
525 
526  int k;
527  for (k = 0; k < _connectedColorChoosers.size(); ++k) {
528  _connectedColorChoosers[k]->blockSignals(true);
529  _connectedColorChoosers[k]->setProperty("color", QVariant(newcolor));
530  _connectedColorChoosers[k]->blockSignals(false);
531  }
532  QString newcolorname = newcolor.name();
533  if (u->txtHex->text() != newcolorname) {
534  u->txtHex->blockSignals(true);
535  u->txtHex->setText(newcolorname);
536  u->txtHex->blockSignals(false);
537  }
538 
539  _color = newcolor;
540 
541  emit colorChanged(newcolor);
542 }
543 
545 {
546  if (!item)
547  return;
548  QColor color(item->text());
550 }
551 
553 {
554  QString name = n;
555  static QRegExp rx("\\#?[0-9A-Za-z]{6}");
556  if (!rx.exactMatch(name)) {
557  u->txtHex->setProperty("invalidInput", true);
558  u->txtHex->setStyleSheet("background-color: rgb(255,128,128)");
559  return;
560  }
561  u->txtHex->setProperty("invalidInput", QVariant());
562  u->txtHex->setStyleSheet("");
563  if (name[0] != QLatin1Char('#'))
564  name = "#"+name;
565  QColor color(name);
566  internalColorChanged(color);
567 }
568 
570 {
571  if (color == _color)
572  return;
573  if (!_alphaenabled && color.rgb() == _color.rgb())
574  return;
575 
576  internalColorChanged(color);
577 }
578 
580 {
581  _alphaenabled = enabled;
582  u->spnAlpha->setShown(enabled);
583  u->lblAlpha->setShown(enabled);
584  u->mAlphaPane->setShown(enabled);
585  u->lblsAlpha->setShown(enabled);
586  u->mAlphaSlider->setShown(enabled);
587  _color.setAlpha(255);
588  setColor(_color);
589 }
590 
591 void KLFColorChooseWidget::fillPalette(KLFColorList *colorlist, QWidget *w)
592 {
593  int k;
594  KLFGridFlowLayout *lyt = dynamic_cast<KLFGridFlowLayout*>( w->layout() );
595  lyt->clearAll();
596  for (k = 0; k < colorlist->list.size(); ++k) {
597  KLFColorClickSquare *sq = new KLFColorClickSquare(colorlist->list[k], 12,
598  (colorlist == _customcolors ||
599  colorlist == _recentcolors),
600  w);
601  connect(sq, SIGNAL(colorActivated(const QColor&)),
602  this, SLOT(internalColorChanged(const QColor&)));
603  connect(sq, SIGNAL(wantRemoveColor(const QColor&)),
604  colorlist, SLOT(removeColor(const QColor&)));
605  lyt->insertGridFlowWidget(sq);
606  sq->show();
607  }
608  w->adjustSize();
609 }
610 
612 {
613  _customcolors->addColor(_color);
615 }
616 
618 {
622 }
623 
625 {
626  fillPalette(_recentcolors, u->mRecentColorsPalette);
627 }
629 {
630  fillPalette(_standardcolors, u->mStandardColorsPalette);
631 }
633 {
634  fillPalette(_customcolors, u->mCustomColorsPalette);
635 }
636 
637 
638 
639 // static
641 {
642  if ( _recentcolors == 0 )
643  _recentcolors = new KLFColorList(128);
644  if ( _standardcolors == 0 )
645  _standardcolors = new KLFColorList(256);
646  if ( _customcolors == 0 )
647  _customcolors = new KLFColorList(128);
648 }
649 
650 // static
652 {
654  QList<QColor>::iterator it = _recentcolors->list.begin();
655  while (it != _recentcolors->list.end()) {
656  if ( (*it) == col )
657  it = _recentcolors->list.erase(it);
658  else
659  ++it;
660  }
661  _recentcolors->list.append(col);
662 
663  if (_recentcolors->list.size() > MAX_RECENT_COLORS) {
664  _recentcolors->list.removeAt(0);
665  }
666  _recentcolors->notifyListChanged();
667 }
668 
669 
670 
671 // -------------------------------------------------------------------
672 
673 
674 
675 void KLFColorList::addColor(const QColor& color)
676 {
677  int i;
678  if ( (i = list.indexOf(color)) >= 0 )
679  list.removeAt(i);
680 
681  list.append(color);
682  while (list.size() >= _maxsize)
683  list.pop_front();
684 
685  emit listChanged();
686 }
687 
689 {
690  bool changed = false;
691  int i;
692  if ( (i = list.indexOf(color)) >= 0 ) {
693  list.removeAt(i);
694  changed = true;
695  }
696  if (changed)
697  emit listChanged();
698 }
699 
700 // static
701 KLFColorList *KLFColorChooser::_colorlist = NULL;
702 
703 QStyle *KLFColorChooser::mReplaceButtonStyle = NULL;
704 
706  : QPushButton(parent), _color(0,0,0,255), _pix(), _allowdefaultstate(false),
707  _defaultstatestring(tr("[ Default ]")), _autoadd(true), _size(120, 20),
708  _xalignfactor(0.5f), _yalignfactor(0.5f), _alphaenabled(true), mMenu(0)
709 {
710  ensureColorListInstance();
711  connect(_colorlist, SIGNAL(listChanged()), this, SLOT(_makemenu()));
712 
713  _makemenu();
714  _setpix();
715 
716 #ifdef Q_WS_MAC
717  if ( mReplaceButtonStyle == NULL )
718  mReplaceButtonStyle = new QPlastiqueStyle;
719  setStyle(mReplaceButtonStyle);
720 #endif
721 }
722 
723 
725 {
726 }
727 
728 
730 {
731  return _color;
732 }
733 
735 {
736  // inspired by QPushButton::sizeHint() in qpushbutton.cpp
737 
738  ensurePolished();
739 
740  int w = 0, h = 0;
741  QStyleOptionButton opt;
742  initStyleOption(&opt);
743 
744  // calculate contents size...
745  w = _pix.width()+4;
746  h = _pix.height()+2;
747 
748  if (menu())
749  w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
750 
751  return (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this).
752  expandedTo(QApplication::globalStrut()));
753 }
754 
756 {
757  if ( ! _allowdefaultstate && ! col.isValid() )
758  return;
759 
760  if (_color == col)
761  return;
762 
763  _color = col;
764  _setpix();
765 
766  if (_autoadd && _color.isValid()) {
767  _colorlist->addColor(_color);
768  }
769  emit colorChanged(_color);
770 }
771 
773 {
774  setColor(QColor());
775 }
776 
778 {
779  _allowdefaultstate = allow;
780  _makemenu();
781 }
783 {
784  _defaultstatestring = str;
785  _makemenu();
786 }
787 
789 {
790  _alphaenabled = on;
791  _makemenu();
792 }
793 
795 {
796  // prefer our own color selection dialog
797  QColor col = KLFColorDialog::getColor(_color, _alphaenabled, this);
798  // QColor col = QColorDialog::getColor(_color, this);
799  if ( ! col.isValid() )
800  return;
801 
802  setColor(col);
803 }
804 
806 {
807  QColor c = sender()->property("setColor").value<QColor>();
808  setColor(c);
809 }
810 
812 {
813  if (mMenu) {
814  setMenu(0);
815  mMenu->deleteLater();
816  }
817 
818  QSize menuIconSize = QSize(16,16);
819 
820  mMenu = new QMenu(this);
821 
822  if (_allowdefaultstate) {
823  mMenu->addAction(QIcon(colorPixmap(QColor(), menuIconSize)), _defaultstatestring,
824  this, SLOT(setDefaultColor()));
825  mMenu->addSeparator();
826  }
827 
828  int n, k, nk;
829  ensureColorListInstance();
830  n = _colorlist->list.size();
831  for (k = 0; k < n; ++k) {
832  nk = n - k - 1;
833  QColor col = _colorlist->list[nk];
834  if (!_alphaenabled)
835  col.setAlpha(255);
836  QString collabel;
837  if (col.alpha() == 255)
838  collabel = QString("%1").arg(col.name());
839  else
840  collabel = QString("%1 (%2%)").arg(col.name()).arg((int)(100.0*col.alpha()/255.0+0.5));
841 
842  QAction *a = mMenu->addAction(QIcon(colorPixmap(col, menuIconSize)), collabel,
843  this, SLOT(setSenderPropertyColor()));
844  a->setProperty("setColor", QVariant::fromValue<QColor>(col));
845  }
846  if (k > 0)
847  mMenu->addSeparator();
848 
849  mMenu->addAction(tr("Custom ..."), this, SLOT(requestColor()));
850 
851  setMenu(mMenu);
852 }
853 
855 {
856  QPushButton::paintEvent(e);
857  QPainter p(this);
858  p.setClipRect(e->rect());
859  p.drawPixmap(QPointF(_xalignfactor*(width()-_pix.width()), _yalignfactor*(height()-_pix.height())), _pix);
860 }
861 
862 void KLFColorChooser::_setpix()
863 {
864  // if (_color.isValid()) {
865  _pix = colorPixmap(_color, _size);
866  // DON'T setIcon() because we draw ourselves ! see paintEvent() !
867  // setIconSize(_pix.size());
868  // setIcon(_pix);
869  setText("");
870  // } else {
871  // _pix = QPixmap();
872  // setIcon(QIcon());
873  // setIconSize(QSize(0,0));
874  // setText("");
875  // }
876 }
877 
878 
879 QPixmap KLFColorChooser::colorPixmap(const QColor& color, const QSize& size)
880 {
881  QPixmap pix = QPixmap(size);
882  if (color.isValid()) {
883  pix.fill(Qt::black);
884  QPainter p(&pix);
885  // background: a checker grid to distinguish transparency
886  p.fillRect(0,0,pix.width(),pix.height(), QBrush(QPixmap(":/pics/checker.png")));
887  // and fill with color
888  p.fillRect(0,0,pix.width(),pix.height(), QBrush(color));
889  // pix.fill(color);
890  } else {
891  // draw "transparent"-representing pixmap
892  pix.fill(QColor(127,127,127,80));
893  QPainter p(&pix);
894  p.setPen(QPen(QColor(255,0,0), 2));
895  p.drawLine(0,0,size.width(),size.height());
896  }
897  return pix;
898 }
899 
900 
901 
902 // static
903 int KLFColorChooser::staticUserMaxColors = 10; // default of 10 colors
904 
905 
906 // static
908 {
909  staticUserMaxColors = maxColors;
910 }
911 
912 // static
913 void KLFColorChooser::ensureColorListInstance()
914 {
915  if ( _colorlist == 0 )
916  _colorlist = new KLFColorList(staticUserMaxColors);
917 }
918 // static
920 {
921  ensureColorListInstance();
922  _colorlist->list = colors;
923  _colorlist->notifyListChanged();
924 }
925 
926 // static
928 {
929  ensureColorListInstance();
930  QList<QColor> l = _colorlist->list;
931  return l;
932 }
933 
934 
935 
936 
void setAlphaEnabled(bool alpha_enabled)
static QList< QColor > colorList()
setMenu(QMenu *menu)
static void addRecentColor(const QColor &col)
void mousePressEvent(QMouseEvent *event)
KLFColorChooseWidget * colorChooseWidget()
void removeColor(const QColor &color)
void setAllowDefaultState(bool allow)
QList< QColor > list
fillRect(const QRectF &rectangle, const QBrush &brush)
static int valueMax(const QString &component)
KLFColorChooseWidget(QWidget *parent=0)
static void ensureColorListsInstance()
fill(const QColor &fillColor=Qt::white)
addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment=0)
A Layout that lays out its children in a grid, flowing left to right, top to bottom.
Definition: klfguiutil.h:381
QColor colorFromValues(QColor color_base, int value_a, int value_b=-1)
void setPaneType(const QString &panetype)
split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive)
#define MAX_RECENT_COLORS
void colorChanged(const QColor &color)
arg(const QString &a, int fieldWidth=0, const QChar &fillChar=QLatin1Char( ' ')
KLFGridFlowLayout(int columns, QWidget *parent)
const char * style
Definition: klfutil.cpp:211
removeAt(int i)
setPen(const QPen &pen)
QColor color() const
static QList< QColor > customColors()
virtual void paintEvent(QPaintEvent *e)
setAlpha(int alpha)
erase(iterator pos)
void wantRemoveColor(const QColor &color)
drawLine(const QLineF &line)
KLFColorChooser(QWidget *parent)
setRgb(int r, int g, int b, int a=255)
addAction(const QString &text)
drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption &option)
virtual void wheelEvent(QWheelEvent *e)
setSpacing(int spacing)
initStyleOption(QStyleOptionButton *option)
int valueBFromNewColor(const QColor &color) const
indexOf(const T &value, int from=0)
valueChanged(int i)
KLFColorComponentSpinBox(QWidget *parent)
void paintEvent(QPaintEvent *event)
bool refreshColorFromInternalValues(int value_a, int value_b=-1)
void paintEvent(QPaintEvent *event)
static QColor getColor(QColor startwith=Qt::black, bool alphaenabled=true, QWidget *parent=0)
append(const T &value)
virtual void internalColorNameSelected(QListWidgetItem *item)
popup(const QPoint &p, QAction *atAction=0)
pop_front()
static void setColorList(const QList< QColor > &colorlist)
KLFColorChooseWidgetPane(QWidget *parent=0)
drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
static int valueFromNewColor(const QColor &color, const QString &component)
static void setRecentCustomColors(QList< QColor > recentcolors, QList< QColor > customcolors)
KLFColorDialog(QWidget *parent=0)
void setDefaultStateString(const QString &str)
void addColor(const QColor &color)
virtual QSize sizeHint() const
addSeparator()
void setColor(const QColor &color)
colorNames()
void setAlphaEnabled(bool alpha_enabled)
QList< QWidget * > mGridFlowWidgets
Definition: klfguiutil.h:395
virtual ~KLFColorDialog()
KLFColorClickSquare(QColor color=Qt::white, int size=16, bool removable=true, QWidget *parent=0)
setFixedSize(const QSize &s)
void setColor(const QColor &color)
void setColorComponent(const QString &component)
setPixel(const QPoint &position, uint index_or_rgb)
virtual void mouseMoveEvent(QMouseEvent *e)
int valueAFromNewColor(const QColor &color) const
static QList< QColor > recentColors()
virtual void mousePressEvent(QMouseEvent *e)
drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags=Qt::AutoColor)
setClipRect(const QRectF &rectangle, Qt::ClipOperation operation=Qt::ReplaceClip)
void contextMenuEvent(QContextMenuEvent *event)
void listChanged()
void colorChanged(const QColor &color)
void colorChanged(const QColor &color)
addItem(QLayoutItem *item, int row, int column, int rowSpan=1, int columnSpan=1, Qt::Alignment alignment=0)
void setColor(const QColor &color)
keyPressEvent(QKeyEvent *event)
void setColor(const QColor &newcolor)
setHsv(int h, int s, int v, int a=255)
void colorChanged(const QColor &newcolor)
saturation()
A dialog to let the user select a color.
void keyPressEvent(QKeyEvent *event)
exactMatch(const QString &str)
virtual void insertGridFlowWidget(QWidget *w, Qt::Alignment align=0)
virtual void internalColorNameSet(const QString &colorname)
void notifyListChanged()
static void setUserMaxColors(int maxcolors)
virtual void internalColorChanged(const QColor &newcolor)
QColor color() const

Generated by doxygen 1.8.5