[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project

src/klftools/klfcolorchooser.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfcolorchooser.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2008 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id: klfcolorchooser.cpp 510 2010-09-22 23:33:10Z philippe $ */
00023 
00024 #include <stdio.h>
00025 
00026 #include <QAction>
00027 #include <QMenu>
00028 #include <QStylePainter>
00029 #include <QColorDialog>
00030 #include <QPaintEvent>
00031 #include <QStyle>
00032 #include <QPlastiqueStyle>
00033 #include <QStyleOptionButton>
00034 #include <QRegExp>
00035 
00036 #include "klfcolorchooser.h"
00037 #include "klfcolorchooser_p.h"
00038 #include "klfguiutil.h"
00039 
00040 #include <ui_klfcolorchoosewidget.h>
00041 #include <ui_klfcolordialog.h>
00042 
00043 
00044 
00045 // -------------------------------------------------------------------
00046 
00047 
00048 KLFColorDialog::KLFColorDialog(QWidget *parent) : QDialog(parent)
00049 {
00050   u = new Ui::KLFColorDialog;
00051   u->setupUi(this);
00052   setObjectName("KLFColorDialog");
00053 }
00054 KLFColorDialog::~KLFColorDialog()
00055 {
00056   delete u;
00057 }
00058 
00059 KLFColorChooseWidget *KLFColorDialog::colorChooseWidget()
00060 {
00061   return u->mColorChooseWidget;
00062 }
00063 
00064 QColor KLFColorDialog::getColor(QColor startwith, bool alphaenabled, QWidget *parent)
00065 {
00066   KLFColorDialog dlg(parent);
00067   dlg.u->mColorChooseWidget->setAlphaEnabled(alphaenabled);
00068   dlg.u->mColorChooseWidget->setColor(startwith);
00069   int r = dlg.exec();
00070   if ( r != QDialog::Accepted )
00071     return QColor();
00072   QColor color = dlg.u->mColorChooseWidget->color();
00073   KLFColorChooseWidget::addRecentColor(color);
00074   return color;
00075 }
00076 
00077 // -------------------------------------------------------------------
00078 
00079 KLFColorClickSquare::KLFColorClickSquare(QColor color, int size, bool removable, QWidget *parent)
00080   : QWidget(parent), _color(color), _size(size), _removable(removable)
00081 {
00082   setFocusPolicy(Qt::StrongFocus);
00083   setFixedSize(_size, _size);
00084   setContextMenuPolicy(Qt::DefaultContextMenu);
00085 }
00086 void KLFColorClickSquare::paintEvent(QPaintEvent */*event*/)
00087 {
00088   QStylePainter p(this);
00089   p.fillRect(0, 0, width(), height(), QBrush(_color));
00090   if (hasFocus()) {
00091     QStyleOptionFocusRect option;
00092     option.initFrom(this);
00093     option.backgroundColor = QColor(0,0,0,0);
00094     p.drawPrimitive(QStyle::PE_FrameFocusRect, option);
00095   }
00096 }
00097 void KLFColorClickSquare::mousePressEvent(QMouseEvent */*event*/)
00098 {
00099   activate();
00100 }
00101 void KLFColorClickSquare::keyPressEvent(QKeyEvent *kev)
00102 {
00103   if (kev->key() == Qt::Key_Space) {
00104     activate();
00105   }
00106   return QWidget::keyPressEvent(kev);
00107 }
00108 void KLFColorClickSquare::contextMenuEvent(QContextMenuEvent *event)
00109 {
00110   if (_removable) {
00111     QMenu *menu = new QMenu(this);
00112     menu->addAction("Remove", this, SLOT(internalWantRemove()));
00113     menu->popup(event->globalPos());
00114   }
00115 }
00116 void KLFColorClickSquare::internalWantRemove()
00117 {
00118   emit wantRemove();
00119   emit wantRemoveColor(_color);
00120 }
00121 
00122 // -------------------------------------------------------------------
00123 
00124 KLFColorChooseWidgetPane::KLFColorChooseWidgetPane(QWidget *parent)
00125   : QWidget(parent), _img()
00126 {
00127 }
00128 
00129 void KLFColorChooseWidgetPane::setColor(const QColor& newcolor)
00130 {
00131   _color = newcolor;
00132   update();
00133   emit colorChanged(_color);
00134 }
00135 void KLFColorChooseWidgetPane::setPaneType(const QString& panetype)
00136 {
00137   QStringList strlist = panetype.split("+");
00138   _colorcomponent = strlist[0].toLower();
00139   _colorcomponent_b = strlist[1].toLower();
00140 }
00141 void KLFColorChooseWidgetPane::paintEvent(QPaintEvent */*e*/)
00142 {
00143   QPainter p(this);
00144   // background: a checker grid to distinguish transparency
00145   p.fillRect(0,0,width(),height(), QBrush(QPixmap(":/pics/checker.png")));
00146   // then prepare an image for our gradients
00147   int x;
00148   int y;
00149   _img = QImage(width(), height(), QImage::Format_ARGB32);
00150   double xfac = (double)valueAMax() / (_img.width()-1);
00151   double yfac = (double)valueBMax() / (_img.height()-1);
00152   for (x = 0; x < _img.width(); ++x) {
00153     for (y = 0; y < _img.height(); ++y) {
00154       _img.setPixel(x, y, colorFromValues(_color, (int)(xfac*x), (int)(yfac*y)).rgba());
00155     }
00156   }
00157   p.drawImage(0, 0, _img);
00158   // draw crosshairs
00159   QColor hairscol = qGray(_color.rgb()) > 80 ? Qt::black : Qt::white;
00160   if ( ! _colorcomponent.isEmpty() && _colorcomponent != "fix" ) {
00161     p.setPen(QPen(hairscol, 1.f, Qt::DotLine));
00162     x = (int)(valueA()/xfac);
00163     if (x < 0) x = 0; if (x >= width()) x = width()-1;
00164     p.drawLine(x, 0, x, height());
00165   }
00166   if ( ! _colorcomponent_b.isEmpty() && _colorcomponent_b != "fix" ) {
00167     p.setPen(QPen(hairscol, 1.f, Qt::DotLine));
00168     y = (int)(valueB()/yfac);
00169     if (y < 0) y = 0; if (y >= height()) y = height()-1;
00170     p.drawLine(0, y, width(), y);
00171   }
00172 }
00173 void KLFColorChooseWidgetPane::mousePressEvent(QMouseEvent *e)
00174 {
00175   double xfac = (double)valueAMax() / (_img.width()-1);
00176   double yfac = (double)valueBMax() / (_img.height()-1);
00177   int x = e->pos().x();
00178   int y = e->pos().y();
00179 
00180   setColor(colorFromValues(_color, (int)(x*xfac), (int)(y*yfac)));
00181 }
00182 void KLFColorChooseWidgetPane::mouseMoveEvent(QMouseEvent *e)
00183 {
00184   double xfac = (double)valueAMax() / (_img.width()-1);
00185   double yfac = (double)valueBMax() / (_img.height()-1);
00186   int x = e->pos().x();
00187   int y = e->pos().y();
00188   if (x < 0) x = 0; if (x >= width()) x = width()-1;
00189   if (y < 0) y = 0; if (y >= height()) y = height()-1;
00190 
00191   setColor(colorFromValues(_color, (int)(x*xfac), (int)(y*yfac)));
00192 }
00193 void KLFColorChooseWidgetPane::wheelEvent(QWheelEvent *e)
00194 {
00195   int step = - 10 * e->delta() / 120;
00196   // isA: TRUE if we are modifying component A, if FALSE then modifying component B
00197 
00198   bool isA =  (e->orientation() == Qt::Horizontal);
00199   if (isA && _colorcomponent=="fix")
00200     isA = false;
00201   if (!isA && _colorcomponent_b=="fix")
00202     isA = true;
00203   if (isA) {
00204     // the first component
00205     setColor(colorFromValues(_color, valueA()+step, valueB()));
00206   } else {
00207     setColor(colorFromValues(_color, valueA(), valueB()+step));
00208   }
00209   e->accept();
00210 }
00211 
00212 
00213 // -------------------------------------------------------------------
00214 
00215 
00216 KLFGridFlowLayout::KLFGridFlowLayout(int columns, QWidget *parent)
00217   : QGridLayout(parent), _ncols(columns),
00218     _currow(0), _curcol(0)
00219 {
00220   addItem(new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed), 0, _ncols);
00221 }
00222 void KLFGridFlowLayout::insertGridFlowWidget(QWidget *w, Qt::Alignment align)
00223 {
00224   mGridFlowWidgets.append(w);
00225   QGridLayout::addWidget(w, _currow, _curcol, align);
00226   _curcol++;
00227   if (_curcol >= _ncols) {
00228     _curcol = 0;
00229     _currow++;
00230   }
00231 }
00232 void KLFGridFlowLayout::clearAll()
00233 {
00234   int k;
00235   for (k = 0; k < mGridFlowWidgets.size(); ++k) {
00236     // because KLFColorClickSquare::wantRemoveColor() can call this by a chain of
00237     // signal/slots; and we shouldn't delete an object inside one of its handlers
00238     //delete mGridFlowWidgets[k];
00239     mGridFlowWidgets[k]->deleteLater();
00240   }
00241   mGridFlowWidgets.clear();
00242   _currow = _curcol = 0;
00243 }
00244 
00245 
00246 // -------------------------------------------------------------------
00247 
00248 
00249 int KLFColorComponentsEditorBase::valueAFromNewColor(const QColor& color) const
00250 {
00251   return valueFromNewColor(color, _colorcomponent);
00252 }
00253 int KLFColorComponentsEditorBase::valueBFromNewColor(const QColor& color) const
00254 {
00255   return valueFromNewColor(color, _colorcomponent_b);
00256 }
00257 int KLFColorComponentsEditorBase::valueFromNewColor(const QColor& color, const QString& component)
00258 {
00259   int value = -1;
00260   if (component == "hue") {
00261     value = color.hue();
00262   } else if (component == "sat") {
00263     value = color.saturation();
00264   } else if (component == "val") {
00265     value = color.value();
00266   } else if (component == "red") {
00267     value = color.red();
00268   } else if (component == "green") {
00269     value = color.green();
00270   } else if (component == "blue") {
00271     value = color.blue();
00272   } else if (component == "alpha") {
00273     value = color.alpha();
00274   } else if (component == "fix" || component.isEmpty()) {
00275     value = -1;
00276   } else {
00277     qWarning("Unknown color component property : %s", component.toLocal8Bit().constData());
00278   }
00279   return value;
00280 }
00281 
00282 int KLFColorComponentsEditorBase::valueMax(const QString& component)
00283 {
00284   if (component == "hue")
00285     return 359;
00286   else if (component == "sat" || component == "val" ||
00287            component == "red" || component == "green" ||
00288            component == "blue" || component == "alpha")
00289     return 255;
00290   else if (component == "fix" || component.isEmpty())
00291     return -1;
00292 
00293   qWarning("Unknown color component property : %s", component.toLocal8Bit().constData());
00294   return -1;
00295 }
00296 
00297 QColor KLFColorComponentsEditorBase::colorFromValues(QColor base, int a, int b)
00298 {
00299   QColor col = base;
00300   /*  printf("colorFromValues(%s/alpha=%d, %d, %d): My components:(%s+%s);\n", qPrintable(col.name()),
00301       col.alpha(), a, b, qPrintable(_colorcomponent), qPrintable(_colorcomponent_b)); */
00302   if (_colorcomponent == "hue") {
00303     col.setHsv(a, col.saturation(), col.value());
00304     col.setAlpha(base.alpha());
00305   } else if (_colorcomponent == "sat") {
00306     col.setHsv(col.hue(), a, col.value());
00307     col.setAlpha(base.alpha());
00308   } else if (_colorcomponent == "val") {
00309     col.setHsv(col.hue(), col.saturation(), a);
00310     col.setAlpha(base.alpha());
00311   } else if (_colorcomponent == "red") {
00312     col.setRgb(a, col.green(), col.blue());
00313     col.setAlpha(base.alpha());
00314   } else if (_colorcomponent == "green") {
00315     col.setRgb(col.red(), a, col.blue());
00316     col.setAlpha(base.alpha());
00317   } else if (_colorcomponent == "blue") {
00318     col.setRgb(col.red(), col.green(), a);
00319     col.setAlpha(base.alpha());
00320   } else if (_colorcomponent == "alpha") {
00321     col.setAlpha(a);
00322   } else if (_colorcomponent == "fix") {
00323     // no change to col
00324   } else {
00325     qWarning("Unknown color component property : %s", _colorcomponent.toLocal8Bit().constData());
00326   }
00327   QColor base2 = col;
00328   //  printf("\tnew color is (%s/alpha=%d);\n", qPrintable(col.name()), col.alpha());
00329   if ( ! _colorcomponent_b.isEmpty() && _colorcomponent_b != "fix" ) {
00330     //    printf("\twe have a second component\n");
00331     if (_colorcomponent_b == "hue") {
00332       col.setHsv(b, col.saturation(), col.value());
00333       col.setAlpha(base2.alpha());
00334     } else if (_colorcomponent_b == "sat") {
00335       col.setHsv(col.hue(), b, col.value());
00336       col.setAlpha(base2.alpha());
00337     } else if (_colorcomponent_b == "val") {
00338       col.setHsv(col.hue(), col.saturation(), b);
00339       col.setAlpha(base2.alpha());
00340     } else if (_colorcomponent_b == "red") {
00341       col.setRgb(b, col.green(), col.blue());
00342       col.setAlpha(base2.alpha());
00343     } else if (_colorcomponent_b == "green") {
00344       col.setRgb(col.red(), b, col.blue());
00345       col.setAlpha(base2.alpha());
00346     } else if (_colorcomponent_b == "blue") {
00347       col.setRgb(col.red(), col.blue(), b);
00348       col.setAlpha(base2.alpha());
00349     } else if (_colorcomponent_b == "alpha") {
00350       col.setAlpha(b);
00351     } else {
00352       qWarning("Unknown color component property : %s", _colorcomponent_b.toLocal8Bit().constData());
00353     }
00354   }
00355   //  printf("\tand color is finally %s/alpha=%d\n", qPrintable(col.name()), col.alpha());
00356   return col;
00357 }
00358 bool KLFColorComponentsEditorBase::refreshColorFromInternalValues(int a, int b)
00359 {
00360   QColor oldcolor = _color;
00361   _color = colorFromValues(_color, a, b);
00362   /*  printf("My components:(%s+%s); New color is %s/alpha=%d\n", _colorcomponent.toLocal8Bit().constData(),
00363       _colorcomponent_b.toLocal8Bit().constData(),  _color.name().toLocal8Bit().constData(), _color.alpha()); */
00364   if ( oldcolor != _color )
00365     return true;
00366   return false;
00367 }
00368 
00369 
00370 // -------------------------------------------------------------------
00371 
00372 
00373 KLFColorComponentSpinBox::KLFColorComponentSpinBox(QWidget *parent)
00374   : QSpinBox(parent)
00375 {
00376   _color = Qt::black;
00377 
00378   setColorComponent("hue");
00379   setColor(_color);
00380 
00381   connect(this, SIGNAL(valueChanged(int)), this, SLOT(internalChanged(int)));
00382 
00383   setValue(valueAFromNewColor(_color));
00384 }
00385 
00386 void KLFColorComponentSpinBox::setColorComponent(const QString& comp)
00387 {
00388   _colorcomponent = comp.toLower();
00389   setMinimum(0);
00390   setMaximum(valueAMax());
00391 }
00392 
00393 void KLFColorComponentSpinBox::internalChanged(int newvalue)
00394 {
00395   if ( refreshColorFromInternalValues(newvalue) )
00396     emit colorChanged(_color);
00397 }
00398 
00399 void KLFColorComponentSpinBox::setColor(const QColor& color)
00400 {
00401   int value = valueAFromNewColor(color);
00402   /*  printf("My components:(%s+%s); setColor(%s/alpha=%d); new value = %d\n",
00403       _colorcomponent.toLocal8Bit().constData(), _colorcomponent_b.toLocal8Bit().constData(),
00404       color.name().toLocal8Bit().constData(), color.alpha(), value); */
00405   _color = color;
00406   setValue(value); // will emit QSpinBox::valueChanged() --> internalChanged() --> colorChanged()
00407 }
00408 
00409 
00410 // -------------------------------------------------------------------
00411 
00412 
00413 KLFColorList * KLFColorChooseWidget::_recentcolors = 0;
00414 KLFColorList * KLFColorChooseWidget::_standardcolors = 0;
00415 KLFColorList * KLFColorChooseWidget::_customcolors = 0;
00416 
00417 // static
00418 void KLFColorChooseWidget::setRecentCustomColors(QList<QColor> recentcolors, QList<QColor> customcolors)
00419 {
00420   ensureColorListsInstance();
00421   _recentcolors->list = recentcolors;
00422   _recentcolors->notifyListChanged();
00423   _customcolors->list = customcolors;
00424   _customcolors->notifyListChanged();
00425 }
00426 // static
00427 QList<QColor> KLFColorChooseWidget::recentColors()
00428 {
00429   ensureColorListsInstance(); return _recentcolors->list;
00430 }
00431 // static
00432 QList<QColor> KLFColorChooseWidget::customColors() {
00433   ensureColorListsInstance(); return _customcolors->list;
00434 }
00435 
00436 
00437 KLFColorChooseWidget::KLFColorChooseWidget(QWidget *parent)
00438   : QWidget(parent)
00439 {
00440   u = new Ui::KLFColorChooseWidget;
00441   u->setupUi(this);
00442   setObjectName("KLFColorChooseWidget");
00443 
00444   _alphaenabled = true;
00445 
00446   ensureColorListsInstance();
00447 
00448   if (_standardcolors->list.size() == 0) {
00449     // add a few standard colors.
00450     QList<QRgb> rgbs;
00451     // inspired from the "Forty Colors" Palette in KDE3 color dialog
00452     rgbs << 0x000000 << 0x303030 << 0x585858 << 0x808080 << 0xa0a0a0 << 0xc3c3c3
00453          << 0xdcdcdc << 0xffffff << 0x400000 << 0x800000 << 0xc00000 << 0xff0000
00454          << 0xffc0c0 << 0x004000 << 0x008000 << 0x00c000 << 0x00ff00 << 0xc0ffc0
00455          << 0x000040 << 0x000080 << 0x0000c0 << 0x0000ff << 0xc0c0ff << 0x404000
00456          << 0x808000 << 0xc0c000 << 0xffff00 << 0xffffc0 << 0x004040 << 0x008080
00457          << 0x00c0c0 << 0x00ffff << 0xc0ffff << 0x400040 << 0x800080 << 0xc000c0
00458          << 0xff00ff << 0xffc0ff << 0xc05800 << 0xff8000 << 0xffa858 << 0xffdca8 ;
00459     for (int k = 0; k < rgbs.size(); ++k)
00460       _standardcolors->list.append(QColor(QRgb(rgbs[k])));
00461   }
00462 
00463   _connectedColorChoosers.append(u->mDisplayColor);
00464   _connectedColorChoosers.append(u->mHueSatPane);
00465   _connectedColorChoosers.append(u->mValPane);
00466   _connectedColorChoosers.append(u->mAlphaPane);
00467   _connectedColorChoosers.append(u->mColorTriangle);
00468   _connectedColorChoosers.append(u->mHueSlider);
00469   _connectedColorChoosers.append(u->mSatSlider);
00470   _connectedColorChoosers.append(u->mValSlider);
00471   _connectedColorChoosers.append(u->mRedSlider);
00472   _connectedColorChoosers.append(u->mGreenSlider);
00473   _connectedColorChoosers.append(u->mBlueSlider);
00474   _connectedColorChoosers.append(u->mAlphaSlider);
00475   _connectedColorChoosers.append(u->spnHue);
00476   _connectedColorChoosers.append(u->spnSat);
00477   _connectedColorChoosers.append(u->spnVal);
00478   _connectedColorChoosers.append(u->spnRed);
00479   _connectedColorChoosers.append(u->spnGreen);
00480   _connectedColorChoosers.append(u->spnBlue);
00481   _connectedColorChoosers.append(u->spnAlpha);
00482 
00483   KLFGridFlowLayout *lytRecent = new KLFGridFlowLayout(12, u->mRecentColorsPalette);
00484   lytRecent->setSpacing(2);
00485   KLFGridFlowLayout *lytStandard = new KLFGridFlowLayout(12, u->mStandardColorsPalette);
00486   lytStandard->setSpacing(2);
00487   KLFGridFlowLayout *lytCustom = new KLFGridFlowLayout(12, u->mCustomColorsPalette);
00488   lytCustom->setSpacing(2);
00489 
00490   connect(_recentcolors, SIGNAL(listChanged()), this, SLOT(updatePaletteRecent()));
00491   connect(_standardcolors, SIGNAL(listChanged()), this, SLOT(updatePaletteStandard()));
00492   connect(_customcolors, SIGNAL(listChanged()), this, SLOT(updatePaletteCustom()));
00493 
00494   updatePalettes();
00495 
00496   int k;
00497   for (k = 0; k < _connectedColorChoosers.size(); ++k) {
00498     connect(_connectedColorChoosers[k], SIGNAL(colorChanged(const QColor&)),
00499             this, SLOT(internalColorChanged(const QColor&)));
00500   }
00501 
00502   connect(u->lstNames, SIGNAL(itemClicked(QListWidgetItem*)),
00503           this, SLOT(internalColorNameSelected(QListWidgetItem*)));
00504   connect(u->txtHex, SIGNAL(textChanged(const QString&)),
00505           this, SLOT(internalColorNameSet(const QString&)));
00506 
00507   connect(u->btnAddCustomColor, SIGNAL(clicked()),
00508           this, SLOT(setCurrentToCustomColor()));
00509 
00510   QStringList colornames = QColor::colorNames();
00511   for (k = 0; k < colornames.size(); ++k) {
00512     QPixmap colsample(16, 16);
00513     colsample.fill(QColor(colornames[k]));
00514     new QListWidgetItem(QIcon(colsample), colornames[k], u->lstNames);
00515   }
00516 
00517   internalColorChanged(_color);
00518 }
00519 
00520 void KLFColorChooseWidget::internalColorChanged(const QColor& wanted_newcolor)
00521 {
00522   QColor newcolor = wanted_newcolor;
00523   if (!_alphaenabled)
00524     newcolor.setAlpha(255);
00525 
00526   int k;
00527   for (k = 0; k < _connectedColorChoosers.size(); ++k) {
00528     _connectedColorChoosers[k]->blockSignals(true);
00529     _connectedColorChoosers[k]->setProperty("color", QVariant(newcolor));
00530     _connectedColorChoosers[k]->blockSignals(false);
00531   }
00532   QString newcolorname = newcolor.name();
00533   if (u->txtHex->text() != newcolorname) {
00534     u->txtHex->blockSignals(true);
00535     u->txtHex->setText(newcolorname);
00536     u->txtHex->blockSignals(false);
00537   }
00538 
00539   _color = newcolor;
00540 
00541   emit colorChanged(newcolor);
00542 }
00543 
00544 void KLFColorChooseWidget::internalColorNameSelected(QListWidgetItem *item)
00545 {
00546   if (!item)
00547     return;
00548   QColor color(item->text());
00549   internalColorChanged(color);
00550 }
00551 
00552 void KLFColorChooseWidget::internalColorNameSet(const QString& n)
00553 {
00554   QString name = n;
00555   static QRegExp rx("\\#?[0-9A-Za-z]{6}");
00556   if (!rx.exactMatch(name)) {
00557     u->txtHex->setProperty("invalidInput", true);
00558     u->txtHex->setStyleSheet("background-color: rgb(255,128,128)");
00559     return;
00560   }
00561   u->txtHex->setProperty("invalidInput", QVariant());
00562   u->txtHex->setStyleSheet("");
00563   if (name[0] != QLatin1Char('#'))
00564     name = "#"+name;
00565   QColor color(name);
00566   internalColorChanged(color);
00567 }
00568 
00569 void KLFColorChooseWidget::setColor(const QColor& color)
00570 {
00571   if (color == _color)
00572     return;
00573   if (!_alphaenabled && color.rgb() == _color.rgb())
00574     return;
00575 
00576   internalColorChanged(color);
00577 }
00578 
00579 void KLFColorChooseWidget::setAlphaEnabled(bool enabled)
00580 {
00581   _alphaenabled = enabled;
00582   u->spnAlpha->setShown(enabled);
00583   u->lblAlpha->setShown(enabled);
00584   u->mAlphaPane->setShown(enabled);
00585   u->lblsAlpha->setShown(enabled);
00586   u->mAlphaSlider->setShown(enabled);
00587   _color.setAlpha(255);
00588   setColor(_color);
00589 }
00590 
00591 void KLFColorChooseWidget::fillPalette(KLFColorList *colorlist, QWidget *w)
00592 {
00593   int k;
00594   KLFGridFlowLayout *lyt = dynamic_cast<KLFGridFlowLayout*>( w->layout() );
00595   lyt->clearAll();
00596   for (k = 0; k < colorlist->list.size(); ++k) {
00597     KLFColorClickSquare *sq = new KLFColorClickSquare(colorlist->list[k], 12,
00598                                                       (colorlist == _customcolors ||
00599                                                        colorlist == _recentcolors),
00600                                                       w);
00601     connect(sq, SIGNAL(colorActivated(const QColor&)),
00602             this, SLOT(internalColorChanged(const QColor&)));
00603     connect(sq, SIGNAL(wantRemoveColor(const QColor&)),
00604               colorlist, SLOT(removeColor(const QColor&)));
00605     lyt->insertGridFlowWidget(sq);
00606     sq->show();
00607   }
00608   w->adjustSize();
00609 }
00610 
00611 void KLFColorChooseWidget::setCurrentToCustomColor()
00612 {
00613   _customcolors->addColor(_color);
00614   updatePaletteCustom();
00615 }
00616 
00617 void KLFColorChooseWidget::updatePalettes()
00618 {
00619   updatePaletteRecent();
00620   updatePaletteStandard();
00621   updatePaletteCustom();
00622 }
00623 
00624 void KLFColorChooseWidget::updatePaletteRecent()
00625 {
00626   fillPalette(_recentcolors, u->mRecentColorsPalette);
00627 }
00628 void KLFColorChooseWidget::updatePaletteStandard()
00629 {
00630   fillPalette(_standardcolors, u->mStandardColorsPalette);
00631 }
00632 void KLFColorChooseWidget::updatePaletteCustom()
00633 {
00634   fillPalette(_customcolors, u->mCustomColorsPalette);
00635 }
00636 
00637 
00638 
00639 // static
00640 void KLFColorChooseWidget::ensureColorListsInstance()
00641 {
00642   if ( _recentcolors == 0 )
00643     _recentcolors = new KLFColorList(128);
00644   if ( _standardcolors == 0 )
00645     _standardcolors = new KLFColorList(256);
00646   if ( _customcolors == 0 )
00647     _customcolors = new KLFColorList(128);
00648 }
00649 
00650 // static
00651 void KLFColorChooseWidget::addRecentColor(const QColor& col)
00652 {
00653   ensureColorListsInstance();
00654   QList<QColor>::iterator it = _recentcolors->list.begin();
00655   while (it != _recentcolors->list.end()) {
00656     if ( (*it) == col )
00657       it = _recentcolors->list.erase(it);
00658     else
00659       ++it;
00660   }
00661   _recentcolors->list.append(col);
00662 
00663   if (_recentcolors->list.size() > MAX_RECENT_COLORS) {
00664     _recentcolors->list.removeAt(0);
00665   }
00666   _recentcolors->notifyListChanged();
00667 }
00668 
00669 
00670 
00671 // -------------------------------------------------------------------
00672 
00673 
00674 
00675 void KLFColorList::addColor(const QColor& color)
00676 {
00677   int i;
00678   if ( (i = list.indexOf(color)) >= 0 )
00679     list.removeAt(i);
00680 
00681   list.append(color);
00682   while (list.size() >= _maxsize)
00683     list.pop_front();
00684 
00685   emit listChanged();
00686 }
00687 
00688 void KLFColorList::removeColor(const QColor& color)
00689 {
00690   bool changed = false;
00691   int i;
00692   if ( (i = list.indexOf(color)) >= 0 ) {
00693     list.removeAt(i);
00694     changed = true;
00695   }
00696   if (changed)
00697     emit listChanged();
00698 }
00699 
00700 // static
00701 KLFColorList *KLFColorChooser::_colorlist = NULL;
00702 
00703 QStyle *KLFColorChooser::mReplaceButtonStyle = NULL;
00704 
00705 KLFColorChooser::KLFColorChooser(QWidget *parent)
00706   : QPushButton(parent), _color(0,0,0,255), _pix(), _allowdefaultstate(false),
00707     _defaultstatestring(tr("[ Default ]")), _autoadd(true), _size(120, 20),
00708     _xalignfactor(0.5f), _yalignfactor(0.5f), _alphaenabled(true), mMenu(0)
00709 {
00710   ensureColorListInstance();
00711   connect(_colorlist, SIGNAL(listChanged()), this, SLOT(_makemenu()));
00712   
00713   _makemenu();
00714   _setpix();
00715 
00716 #ifdef Q_WS_MAC
00717   if ( mReplaceButtonStyle == NULL )
00718     mReplaceButtonStyle = new QPlastiqueStyle;
00719   setStyle(mReplaceButtonStyle);
00720 #endif
00721 }
00722 
00723 
00724 KLFColorChooser::~KLFColorChooser()
00725 {
00726 }
00727 
00728 
00729 QColor KLFColorChooser::color() const
00730 {
00731   return _color;
00732 }
00733 
00734 QSize KLFColorChooser::sizeHint() const
00735 {
00736   // inspired by QPushButton::sizeHint() in qpushbutton.cpp
00737 
00738   ensurePolished();
00739 
00740   int w = 0, h = 0;
00741   QStyleOptionButton opt;
00742   initStyleOption(&opt);
00743 
00744   // calculate contents size...
00745   w = _pix.width()+4;
00746   h = _pix.height()+2;
00747 
00748   if (menu())
00749     w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
00750 
00751   return (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this).
00752           expandedTo(QApplication::globalStrut()));
00753 }
00754 
00755 void KLFColorChooser::setColor(const QColor& col)
00756 {
00757   if ( ! _allowdefaultstate && ! col.isValid() )
00758     return;
00759 
00760   if (_color == col)
00761     return;
00762 
00763   _color = col;
00764   _setpix();
00765 
00766   if (_autoadd && _color.isValid()) {
00767     _colorlist->addColor(_color);
00768   }
00769   emit colorChanged(_color);
00770 }
00771 
00772 void KLFColorChooser::setDefaultColor()
00773 {
00774   setColor(QColor());
00775 }
00776 
00777 void KLFColorChooser::setAllowDefaultState(bool allow)
00778 {
00779   _allowdefaultstate = allow;
00780   _makemenu();
00781 }
00782 void KLFColorChooser::setDefaultStateString(const QString& str)
00783 {
00784   _defaultstatestring = str;
00785   _makemenu();
00786 }
00787 
00788 void KLFColorChooser::setAlphaEnabled(bool on)
00789 {
00790   _alphaenabled = on;
00791   _makemenu();
00792 }
00793 
00794 void KLFColorChooser::requestColor()
00795 {
00796   // prefer our own color selection dialog
00797   QColor col = KLFColorDialog::getColor(_color, _alphaenabled, this);
00798   // QColor col = QColorDialog::getColor(_color, this);
00799   if ( ! col.isValid() )
00800     return;
00801 
00802   setColor(col);
00803 }
00804 
00805 void KLFColorChooser::setSenderPropertyColor()
00806 {
00807   QColor c = sender()->property("setColor").value<QColor>();
00808   setColor(c);
00809 }
00810 
00811 void KLFColorChooser::_makemenu()
00812 {
00813   if (mMenu) {
00814     setMenu(0);
00815     mMenu->deleteLater();
00816   }
00817 
00818   QSize menuIconSize = QSize(16,16);
00819 
00820   mMenu = new QMenu(this);
00821 
00822   if (_allowdefaultstate) {
00823     mMenu->addAction(QIcon(colorPixmap(QColor(), menuIconSize)), _defaultstatestring,
00824                      this, SLOT(setDefaultColor()));
00825     mMenu->addSeparator();
00826   }
00827 
00828   int n, k, nk;
00829   ensureColorListInstance();
00830   n = _colorlist->list.size();
00831   for (k = 0; k < n; ++k) {
00832     nk = n - k - 1;
00833     QColor col = _colorlist->list[nk];
00834     if (!_alphaenabled)
00835       col.setAlpha(255);
00836     QString collabel;
00837     if (col.alpha() == 255)
00838       collabel = QString("%1").arg(col.name());
00839     else
00840       collabel = QString("%1 (%2%)").arg(col.name()).arg((int)(100.0*col.alpha()/255.0+0.5));
00841 
00842     QAction *a = mMenu->addAction(QIcon(colorPixmap(col, menuIconSize)), collabel,
00843                                   this, SLOT(setSenderPropertyColor()));
00844     a->setProperty("setColor", QVariant::fromValue<QColor>(col));
00845   }
00846   if (k > 0)
00847     mMenu->addSeparator();
00848 
00849   mMenu->addAction(tr("Custom ..."), this, SLOT(requestColor()));
00850 
00851   setMenu(mMenu);
00852 }
00853 
00854 void KLFColorChooser::paintEvent(QPaintEvent *e)
00855 {
00856   QPushButton::paintEvent(e);
00857   QPainter p(this);
00858   p.setClipRect(e->rect());
00859   p.drawPixmap(QPointF(_xalignfactor*(width()-_pix.width()), _yalignfactor*(height()-_pix.height())), _pix);
00860 }
00861 
00862 void KLFColorChooser::_setpix()
00863 {
00864   //  if (_color.isValid()) {
00865   _pix = colorPixmap(_color, _size);
00866   // DON'T setIcon() because we draw ourselves ! see paintEvent() !
00867   //  setIconSize(_pix.size());
00868   //  setIcon(_pix);
00869   setText("");
00870   //  } else {
00871   //    _pix = QPixmap();
00872   //    setIcon(QIcon());
00873   //    setIconSize(QSize(0,0));
00874   //    setText("");
00875   //  }
00876 }
00877 
00878 
00879 QPixmap KLFColorChooser::colorPixmap(const QColor& color, const QSize& size)
00880 {
00881   QPixmap pix = QPixmap(size);
00882   if (color.isValid()) {
00883     pix.fill(Qt::black);
00884     QPainter p(&pix);
00885     // background: a checker grid to distinguish transparency
00886     p.fillRect(0,0,pix.width(),pix.height(), QBrush(QPixmap(":/pics/checker.png")));
00887     // and fill with color
00888     p.fillRect(0,0,pix.width(),pix.height(), QBrush(color));
00889     //    pix.fill(color);
00890   } else {
00891     // draw "transparent"-representing pixmap
00892     pix.fill(QColor(127,127,127,80));
00893     QPainter p(&pix);
00894     p.setPen(QPen(QColor(255,0,0), 2));
00895     p.drawLine(0,0,size.width(),size.height());
00896   }
00897   return pix;
00898 }
00899 
00900 
00901 
00902 // static
00903 int KLFColorChooser::staticUserMaxColors = 10;   // default of 10 colors
00904 
00905 
00906 // static
00907 void KLFColorChooser::setUserMaxColors(int maxColors)
00908 {
00909   staticUserMaxColors = maxColors;
00910 }
00911 
00912 // static
00913 void KLFColorChooser::ensureColorListInstance()
00914 {
00915   if ( _colorlist == 0 )
00916     _colorlist = new KLFColorList(staticUserMaxColors);
00917 }
00918 // static
00919 void KLFColorChooser::setColorList(const QList<QColor>& colors)
00920 {
00921   ensureColorListInstance();
00922   _colorlist->list = colors;
00923   _colorlist->notifyListChanged();
00924 }
00925 
00926 // static
00927 QList<QColor> KLFColorChooser::colorList()
00928 {
00929   ensureColorListInstance();
00930   QList<QColor> l = _colorlist->list;
00931   return l;
00932 }
00933 
00934 
00935 
00936 

Generated by doxygen 1.7.3