00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdio.h>
00025
00026 #include <QAction>
00027 #include <QMenu>
00028 #include <QDebug>
00029 #include <QColorDialog>
00030 #include <QPaintEvent>
00031 #include <QStyle>
00032 #include <QStylePainter>
00033 #include <QStyleFactory>
00034 #include <QStyleOptionButton>
00035 #include <QRegExp>
00036
00037 #include <klfdefs.h>
00038 #include "klfflowlayout.h"
00039 #include "klfguiutil.h"
00040 #include "klfrelativefont.h"
00041 #include "klfcolorchooser.h"
00042 #include "klfcolorchooser_p.h"
00043
00044 #include <ui_klfcolorchoosewidget.h>
00045 #include <ui_klfcolordialog.h>
00046
00047
00048
00049
00050
00051
00052 KLFColorDialog::KLFColorDialog(QWidget *parent) : QDialog(parent)
00053 {
00054 u = new Ui::KLFColorDialog;
00055 u->setupUi(this);
00056 setObjectName("KLFColorDialog");
00057 }
00058 KLFColorDialog::~KLFColorDialog()
00059 {
00060 delete u;
00061 }
00062
00063 KLFColorChooseWidget *KLFColorDialog::colorChooseWidget()
00064 {
00065 return u->mColorChooseWidget;
00066 }
00067
00068 QColor KLFColorDialog::getColor(QColor startwith, bool alphaenabled, QWidget *parent)
00069 {
00070 KLFColorDialog dlg(parent);
00071 dlg.u->mColorChooseWidget->setAlphaEnabled(alphaenabled);
00072 dlg.u->mColorChooseWidget->setColor(startwith);
00073 int r = dlg.exec();
00074 if ( r != QDialog::Accepted )
00075 return QColor();
00076 QColor color = dlg.u->mColorChooseWidget->color();
00077 return color;
00078 }
00079
00080 QColor KLFColorDialog::color() const
00081 {
00082 return u->mColorChooseWidget->color();
00083 }
00084 void KLFColorDialog::setColor(const QColor& color)
00085 {
00086 u->mColorChooseWidget->setColor(color);
00087 }
00088 void KLFColorDialog::slotAccepted()
00089 {
00090 KLFColorChooseWidget::addRecentColor(color());
00091 }
00092
00093
00094
00095 KLFColorClickSquare::KLFColorClickSquare(QColor color, int size, bool removable, QWidget *parent)
00096 : QWidget(parent), _color(color), _removable(removable)
00097 {
00098 initwidget();
00099 setSqSize(size);
00100 }
00101 KLFColorClickSquare::KLFColorClickSquare(QWidget *parent)
00102 : QWidget(parent), _color(Qt::white), _removable(false)
00103 {
00104 initwidget();
00105 setSqSize(16);
00106 }
00107 void KLFColorClickSquare::initwidget()
00108 {
00109 setFocusPolicy(Qt::StrongFocus);
00110 setContextMenuPolicy(Qt::DefaultContextMenu);
00111
00112
00113 }
00114
00115 void KLFColorClickSquare::setSqSize(int sz)
00116 {
00117 if (_size == sz)
00118 return;
00119
00120 _size = sz;
00121 setFixedSize(_size, _size);
00122 }
00123
00124 void KLFColorClickSquare::setRemovable(bool removable)
00125 {
00126 _removable = removable;
00127 }
00128
00129 void KLFColorClickSquare::paintEvent(QPaintEvent *event)
00130 {
00131 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00132 Q_UNUSED(event) ;
00133 klfDbg("event->rect="<<event->rect()) ;
00134 {
00135 QPainter p(this);
00136 p.fillRect(0, 0, width(), height(), QBrush(_color));
00137 }
00138 if (hasFocus()) {
00139 QStylePainter p(this);
00140 QStyleOptionFocusRect option;
00141 option.initFrom(this);
00142 option.backgroundColor = QColor(0,0,0,0);
00143 p.drawPrimitive(QStyle::PE_FrameFocusRect, option);
00144 }
00145 }
00146 void KLFColorClickSquare::resizeEvent(QResizeEvent *event)
00147 {
00148 Q_UNUSED(event) ;
00149 }
00150
00151 void KLFColorClickSquare::mousePressEvent(QMouseEvent *)
00152 {
00153 activate();
00154 }
00155 void KLFColorClickSquare::keyPressEvent(QKeyEvent *kev)
00156 {
00157 if (kev->key() == Qt::Key_Space) {
00158 activate();
00159 }
00160 return QWidget::keyPressEvent(kev);
00161 }
00162 void KLFColorClickSquare::contextMenuEvent(QContextMenuEvent *event)
00163 {
00164 if (_removable) {
00165 QMenu *menu = new QMenu(this);
00166 menu->addAction("Remove", this, SLOT(internalWantRemove()));
00167 menu->popup(event->globalPos());
00168 }
00169 }
00170 void KLFColorClickSquare::internalWantRemove()
00171 {
00172 emit wantRemove();
00173 emit wantRemoveColor(_color);
00174 }
00175
00176
00177
00178 KLFColorChooseWidgetPane::KLFColorChooseWidgetPane(QWidget *parent)
00179 : QWidget(parent), _img()
00180 {
00181 setPaneType("red+fix");
00182 _color = Qt::black;
00183 }
00184
00185 QSize KLFColorChooseWidgetPane::sizeHint() const
00186 {
00187 return QSize((_colorcomponent == "fix") ? 16 : 50, (_colorcomponent_b == "fix") ? 16 : 50);
00188 }
00189 QSize KLFColorChooseWidgetPane::minimumSizeHint() const
00190 {
00191 return QSize(16, 16);
00192 }
00193
00194 void KLFColorChooseWidgetPane::setColor(const QColor& newcolor)
00195 {
00196 if (_color == newcolor)
00197 return;
00198
00199 _color = newcolor;
00200 update();
00201 emit colorChanged(_color);
00202 }
00203 void KLFColorChooseWidgetPane::setPaneType(const QString& panetype)
00204 {
00205 static QStringList okvals =
00206 QStringList() << "hue"<<"sat"<<"val"<<"red"<<"green"<<"blue"<<"alpha"<<"fix";
00207
00208 QStringList strlist = panetype.split("+");
00209 if (strlist.size() != 2) {
00210 qWarning()<<KLF_FUNC_NAME<<": expected a pane-type string \"<pane1type>+<pane2type>\"!";
00211 return;
00212 }
00213 _colorcomponent = strlist[0].toLower();
00214 _colorcomponent_b = strlist[1].toLower();
00215 if (!okvals.contains(_colorcomponent))
00216 _colorcomponent = "fix";
00217 if (!okvals.contains(_colorcomponent_b))
00218 _colorcomponent_b = "fix";
00219
00220 if (_colorcomponent == "fix" && _colorcomponent_b == "fix")
00221 setFocusPolicy(Qt::NoFocus);
00222 else
00223 setFocusPolicy(Qt::WheelFocus);
00224 }
00225 void KLFColorChooseWidgetPane::paintEvent(QPaintEvent *)
00226 {
00227 QStylePainter p(this);
00228
00229 p.fillRect(0,0,width(),height(), QBrush(QPixmap(":/pics/checker.png")));
00230
00231 int x;
00232 int y;
00233 _img = QImage(width(), height(), QImage::Format_ARGB32);
00234 double xfac = (double)valueAMax() / (_img.width()-1);
00235 double yfac = (double)valueBMax() / (_img.height()-1);
00236 for (x = 0; x < _img.width(); ++x) {
00237 for (y = 0; y < _img.height(); ++y) {
00238 _img.setPixel(x, _img.height()-y-1, colorFromValues(_color, (int)(xfac*x), (int)(yfac*y)).rgba());
00239 }
00240 }
00241 p.drawImage(0, 0, _img);
00242
00243 QColor hairscol = qGray(_color.rgb()) > 80 ? QColor(0,0,0,180) : QColor(255,255,255,180);
00244 if ( _colorcomponent != "fix" ) {
00245 p.setPen(QPen(hairscol, 1.f, Qt::DotLine));
00246 x = (int)(valueA()/xfac);
00247 if (x < 0) { x = 0; }
00248 if (x >= width()) { x = width()-1; }
00249 p.drawLine(x, 0, x, height());
00250 }
00251 if ( _colorcomponent_b != "fix" ) {
00252 p.setPen(QPen(hairscol, 1.f, Qt::DotLine));
00253 y = (int)(valueB()/yfac);
00254 if (y < 0) { y = 0; }
00255 if (y >= height()) { y = height()-1; }
00256 p.drawLine(0, height()-y-1, width(), height()-y-1);
00257 }
00258
00259 if (hasFocus()) {
00260 QStyleOptionFocusRect option;
00261 option.initFrom(this);
00262 option.backgroundColor = QColor(0,0,0,0);
00263 p.drawPrimitive(QStyle::PE_FrameFocusRect, option);
00264 }
00265 }
00266 void KLFColorChooseWidgetPane::mousePressEvent(QMouseEvent *e)
00267 {
00268 double xfac = (double)valueAMax() / (_img.width()-1);
00269 double yfac = (double)valueBMax() / (_img.height()-1);
00270 int x = e->pos().x();
00271 int y = height() - e->pos().y() - 1;
00272
00273 setColor(colorFromValues(_color, (int)(x*xfac), (int)(y*yfac)));
00274 }
00275 void KLFColorChooseWidgetPane::mouseMoveEvent(QMouseEvent *e)
00276 {
00277 double xfac = (double)valueAMax() / (_img.width()-1);
00278 double yfac = (double)valueBMax() / (_img.height()-1);
00279 int x = e->pos().x();
00280 int y = height() - e->pos().y() - 1;
00281 if (x < 0) { x = 0; }
00282 if (x >= width()) { x = width()-1; }
00283 if (y < 0) { y = 0; } if (y >= height()) { y = height()-1; }
00284
00285 setColor(colorFromValues(_color, (int)(x*xfac), (int)(y*yfac)));
00286 }
00287 void KLFColorChooseWidgetPane::wheelEvent(QWheelEvent *e)
00288 {
00289 double step = - 7.5 * e->delta() / 120;
00290
00291 if (e->modifiers() == Qt::ShiftModifier)
00292 step = step / 5.0;
00293 if (e->modifiers() == Qt::ControlModifier)
00294 step = step * 2.5;
00295
00296
00297 bool isA = (e->orientation() == Qt::Horizontal);
00298 if (isA && _colorcomponent=="fix")
00299 isA = false;
00300 if (!isA && _colorcomponent_b=="fix")
00301 isA = true;
00302 if (isA) {
00303
00304 int x = (int)(valueA()+step);
00305 if (x < 0) x = 0;
00306 if (x > valueAMax()) x = valueAMax();
00307 setColor(colorFromValues(_color, x, valueB()));
00308 } else {
00309 int x = (int)(valueB() - step);
00310 if (x < 0) x = 0;
00311 if (x > valueBMax()) x = valueBMax();
00312 setColor(colorFromValues(_color, valueA(), x));
00313 }
00314 e->accept();
00315 }
00316 void KLFColorChooseWidgetPane::keyPressEvent(QKeyEvent *e)
00317 {
00318 const int dir_step = 5;
00319 double xstep = 0;
00320 double ystep = 0;
00321
00322 if (e->key() == Qt::Key_Left)
00323 xstep -= dir_step;
00324 if (e->key() == Qt::Key_Right)
00325 xstep += dir_step;
00326 if (e->key() == Qt::Key_Up)
00327 ystep += dir_step;
00328 if (e->key() == Qt::Key_Down)
00329 ystep -= dir_step;
00330 if (e->key() == Qt::Key_Home)
00331 xstep = -10000;
00332 if (e->key() == Qt::Key_End)
00333 xstep = 10000;
00334 if (e->key() == Qt::Key_PageUp)
00335 ystep = 10000;
00336 if (e->key() == Qt::Key_PageDown)
00337 ystep = -10000;
00338
00339
00340 if (_colorcomponent == "fix") {
00341 ystep += xstep;
00342 xstep = 0;
00343 } else if (_colorcomponent_b == "fix") {
00344 xstep += ystep;
00345 ystep = 0;
00346 }
00347
00348 if (e->modifiers() == Qt::ShiftModifier) {
00349 xstep = xstep / 5; ystep = ystep / 5;
00350 }
00351 if (e->modifiers() == Qt::ControlModifier) {
00352 xstep = xstep * 2.5; ystep = ystep * 2.5;
00353 }
00354
00355 int x = (int)(valueA() + xstep);
00356 int y = (int)(valueB() + ystep);
00357 if (x < 0) x = 0;
00358 if (x > valueAMax()) x = valueAMax();
00359 if (y < 0) y = 0;
00360 if (y > valueBMax()) y = valueBMax();
00361
00362 setColor(colorFromValues(_color, x, y));
00363 }
00364
00365
00366
00367
00368
00369 KLFGridFlowLayout::KLFGridFlowLayout(int columns, QWidget *parent)
00370 : QGridLayout(parent), _ncols(columns),
00371 _currow(0), _curcol(0)
00372 {
00373 addItem(new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed), 0, _ncols);
00374 }
00375 void KLFGridFlowLayout::insertGridFlowWidget(QWidget *w, Qt::Alignment align)
00376 {
00377 mGridFlowWidgets.append(w);
00378 QGridLayout::addWidget(w, _currow, _curcol, align);
00379 _curcol++;
00380 if (_curcol >= _ncols) {
00381 _curcol = 0;
00382 _currow++;
00383 }
00384 }
00385 void KLFGridFlowLayout::clearAll()
00386 {
00387 int k;
00388 for (k = 0; k < mGridFlowWidgets.size(); ++k) {
00389
00390
00391
00392 mGridFlowWidgets[k]->deleteLater();
00393 }
00394 mGridFlowWidgets.clear();
00395 _currow = _curcol = 0;
00396 }
00397
00398
00399
00400
00401
00402 int KLFColorComponentsEditorBase::valueAFromNewColor(const QColor& color) const
00403 {
00404 return valueFromNewColor(color, _colorcomponent);
00405 }
00406 int KLFColorComponentsEditorBase::valueBFromNewColor(const QColor& color) const
00407 {
00408 return valueFromNewColor(color, _colorcomponent_b);
00409 }
00410 int KLFColorComponentsEditorBase::valueFromNewColor(const QColor& color, const QString& component)
00411 {
00412 int value = -1;
00413 if (component == "hue") {
00414 value = color.hue();
00415 } else if (component == "sat") {
00416 value = color.saturation();
00417 } else if (component == "val") {
00418 value = color.value();
00419 } else if (component == "red") {
00420 value = color.red();
00421 } else if (component == "green") {
00422 value = color.green();
00423 } else if (component == "blue") {
00424 value = color.blue();
00425 } else if (component == "alpha") {
00426 value = color.alpha();
00427 } else if (component == "fix" || component.isEmpty()) {
00428 value = -1;
00429 } else {
00430 qWarning("Unknown color component property : %s", component.toLocal8Bit().constData());
00431 }
00432 return value;
00433 }
00434
00435 int KLFColorComponentsEditorBase::valueMax(const QString& component)
00436 {
00437 if (component == "hue")
00438 return 359;
00439 else if (component == "sat" || component == "val" ||
00440 component == "red" || component == "green" ||
00441 component == "blue" || component == "alpha")
00442 return 255;
00443 else if (component == "fix" || component.isEmpty())
00444 return -1;
00445
00446 qWarning("Unknown color component property : %s", component.toLocal8Bit().constData());
00447 return -1;
00448 }
00449
00450 QColor KLFColorComponentsEditorBase::colorFromValues(QColor base, int a, int b)
00451 {
00452 QColor col = base;
00453
00454
00455 if (_colorcomponent == "hue") {
00456 col.setHsv(a, col.saturation(), col.value());
00457 col.setAlpha(base.alpha());
00458 } else if (_colorcomponent == "sat") {
00459 col.setHsv(col.hue(), a, col.value());
00460 col.setAlpha(base.alpha());
00461 } else if (_colorcomponent == "val") {
00462 col.setHsv(col.hue(), col.saturation(), a);
00463 col.setAlpha(base.alpha());
00464 } else if (_colorcomponent == "red") {
00465 col.setRgb(a, col.green(), col.blue());
00466 col.setAlpha(base.alpha());
00467 } else if (_colorcomponent == "green") {
00468 col.setRgb(col.red(), a, col.blue());
00469 col.setAlpha(base.alpha());
00470 } else if (_colorcomponent == "blue") {
00471 col.setRgb(col.red(), col.green(), a);
00472 col.setAlpha(base.alpha());
00473 } else if (_colorcomponent == "alpha") {
00474 col.setAlpha(a);
00475 } else if (_colorcomponent == "fix") {
00476
00477 } else {
00478 qWarning("Unknown color component property : %s", _colorcomponent.toLocal8Bit().constData());
00479 }
00480 QColor base2 = col;
00481
00482 if ( ! _colorcomponent_b.isEmpty() && _colorcomponent_b != "fix" ) {
00483
00484 if (_colorcomponent_b == "hue") {
00485 col.setHsv(b, col.saturation(), col.value());
00486 col.setAlpha(base2.alpha());
00487 } else if (_colorcomponent_b == "sat") {
00488 col.setHsv(col.hue(), b, col.value());
00489 col.setAlpha(base2.alpha());
00490 } else if (_colorcomponent_b == "val") {
00491 col.setHsv(col.hue(), col.saturation(), b);
00492 col.setAlpha(base2.alpha());
00493 } else if (_colorcomponent_b == "red") {
00494 col.setRgb(b, col.green(), col.blue());
00495 col.setAlpha(base2.alpha());
00496 } else if (_colorcomponent_b == "green") {
00497 col.setRgb(col.red(), b, col.blue());
00498 col.setAlpha(base2.alpha());
00499 } else if (_colorcomponent_b == "blue") {
00500 col.setRgb(col.red(), col.blue(), b);
00501 col.setAlpha(base2.alpha());
00502 } else if (_colorcomponent_b == "alpha") {
00503 col.setAlpha(b);
00504 } else {
00505 qWarning("Unknown color component property : %s", _colorcomponent_b.toLocal8Bit().constData());
00506 }
00507 }
00508
00509 return col;
00510 }
00511 bool KLFColorComponentsEditorBase::refreshColorFromInternalValues(int a, int b)
00512 {
00513 QColor oldcolor = _color;
00514 _color = colorFromValues(_color, a, b);
00515
00516
00517 if ( oldcolor != _color )
00518 return true;
00519 return false;
00520 }
00521
00522
00523
00524
00525
00526 KLFColorComponentSpinBox::KLFColorComponentSpinBox(QWidget *parent)
00527 : QSpinBox(parent)
00528 {
00529 _color = Qt::black;
00530
00531 setColorComponent("hue");
00532 setColor(_color);
00533
00534 connect(this, SIGNAL(valueChanged(int)), this, SLOT(internalChanged(int)));
00535
00536 setValue(valueAFromNewColor(_color));
00537 }
00538
00539 void KLFColorComponentSpinBox::setColorComponent(const QString& comp)
00540 {
00541 _colorcomponent = comp.toLower();
00542 setMinimum(0);
00543 setMaximum(valueAMax());
00544 }
00545
00546 void KLFColorComponentSpinBox::internalChanged(int newvalue)
00547 {
00548 if ( refreshColorFromInternalValues(newvalue) )
00549 emit colorChanged(_color);
00550 }
00551
00552 void KLFColorComponentSpinBox::setColor(const QColor& color)
00553 {
00554 if (_color == color)
00555 return;
00556 int value = valueAFromNewColor(color);
00557
00558
00559
00560 _color = color;
00561 setValue(value);
00562 }
00563
00564
00565
00566
00567
00568 KLFColorList * KLFColorChooseWidget::_recentcolors = 0;
00569 KLFColorList * KLFColorChooseWidget::_standardcolors = 0;
00570 KLFColorList * KLFColorChooseWidget::_customcolors = 0;
00571
00572
00573 void KLFColorChooseWidget::setRecentCustomColors(QList<QColor> recentcolors, QList<QColor> customcolors)
00574 {
00575 ensureColorListsInstance();
00576 _recentcolors->list = recentcolors;
00577 _recentcolors->notifyListChanged();
00578 _customcolors->list = customcolors;
00579 _customcolors->notifyListChanged();
00580 }
00581
00582 QList<QColor> KLFColorChooseWidget::recentColors()
00583 {
00584 ensureColorListsInstance();
00585 return _recentcolors->list;
00586 }
00587
00588 QList<QColor> KLFColorChooseWidget::customColors()
00589 {
00590 ensureColorListsInstance();
00591 return _customcolors->list;
00592 }
00593
00594
00595 KLFColorChooseWidget::KLFColorChooseWidget(QWidget *parent)
00596 : QWidget(parent)
00597 {
00598 u = new Ui::KLFColorChooseWidget;
00599 u->setupUi(this);
00600 setObjectName("KLFColorChooseWidget");
00601
00602 _alphaenabled = true;
00603
00604 ensureColorListsInstance();
00605
00606 if (_standardcolors->list.size() == 0) {
00607
00608 QList<QRgb> rgbs;
00609
00610 rgbs << 0x000000 << 0x303030 << 0x585858 << 0x808080 << 0xa0a0a0 << 0xc3c3c3
00611 << 0xdcdcdc << 0xffffff << 0x400000 << 0x800000 << 0xc00000 << 0xff0000
00612 << 0xffc0c0 << 0x004000 << 0x008000 << 0x00c000 << 0x00ff00 << 0xc0ffc0
00613 << 0x000040 << 0x000080 << 0x0000c0 << 0x0000ff << 0xc0c0ff << 0x404000
00614 << 0x808000 << 0xc0c000 << 0xffff00 << 0xffffc0 << 0x004040 << 0x008080
00615 << 0x00c0c0 << 0x00ffff << 0xc0ffff << 0x400040 << 0x800080 << 0xc000c0
00616 << 0xff00ff << 0xffc0ff << 0xc05800 << 0xff8000 << 0xffa858 << 0xffdca8 ;
00617 for (int k = 0; k < rgbs.size(); ++k)
00618 _standardcolors->list.append(QColor(QRgb(rgbs[k])));
00619 }
00620
00621 _connectedColorChoosers.append(u->mDisplayColor);
00622 _connectedColorChoosers.append(u->mHueSatPane);
00623 _connectedColorChoosers.append(u->mValPane);
00624 _connectedColorChoosers.append(u->mAlphaPane);
00625 _connectedColorChoosers.append(u->mColorTriangle);
00626 _connectedColorChoosers.append(u->mHueSlider);
00627 _connectedColorChoosers.append(u->mSatSlider);
00628 _connectedColorChoosers.append(u->mValSlider);
00629 _connectedColorChoosers.append(u->mRedSlider);
00630 _connectedColorChoosers.append(u->mGreenSlider);
00631 _connectedColorChoosers.append(u->mBlueSlider);
00632 _connectedColorChoosers.append(u->mAlphaSlider);
00633 _connectedColorChoosers.append(u->spnHue);
00634 _connectedColorChoosers.append(u->spnSat);
00635 _connectedColorChoosers.append(u->spnVal);
00636 _connectedColorChoosers.append(u->spnRed);
00637 _connectedColorChoosers.append(u->spnGreen);
00638 _connectedColorChoosers.append(u->spnBlue);
00639 _connectedColorChoosers.append(u->spnAlpha);
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651 KLFFlowLayout *lytRecent = new KLFFlowLayout(u->mRecentColorsPalette, 11, 2, 2);
00652 lytRecent->setFlush(KLFFlowLayout::FlushBegin);
00653 KLFFlowLayout *lytStandard = new KLFFlowLayout(u->mStandardColorsPalette, 11, 2, 2);
00654 lytStandard->setFlush(KLFFlowLayout::FlushBegin);
00655 KLFFlowLayout *lytCustom = new KLFFlowLayout(u->mCustomColorsPalette, 11, 2, 2);
00656 lytCustom->setFlush(KLFFlowLayout::FlushBegin);
00657
00658 connect(_recentcolors, SIGNAL(listChanged()), this, SLOT(updatePaletteRecent()));
00659 connect(_standardcolors, SIGNAL(listChanged()), this, SLOT(updatePaletteStandard()));
00660 connect(_customcolors, SIGNAL(listChanged()), this, SLOT(updatePaletteCustom()));
00661
00662 updatePalettes();
00663
00664 int k;
00665 for (k = 0; k < _connectedColorChoosers.size(); ++k) {
00666 connect(_connectedColorChoosers[k], SIGNAL(colorChanged(const QColor&)),
00667 this, SLOT(internalColorChanged(const QColor&)));
00668 }
00669
00670 connect(u->lstNames, SIGNAL(itemClicked(QListWidgetItem*)),
00671 this, SLOT(internalColorNameSelected(QListWidgetItem*)));
00672 connect(u->txtHex, SIGNAL(textChanged(const QString&)),
00673 this, SLOT(internalColorNameSet(const QString&)));
00674
00675 QPalette p = u->txtHex->palette();
00676 u->txtHex->setProperty("paletteDefault", QVariant::fromValue<QPalette>(p));
00677 p.setColor(QPalette::Base, QColor(255,169, 184,128));
00678 u->txtHex->setProperty("paletteInvalidInput", QVariant::fromValue<QPalette>(p));
00679
00680
00681 connect(u->btnAddCustomColor, SIGNAL(clicked()),
00682 this, SLOT(setCurrentToCustomColor()));
00683
00684 QStringList colornames = QColor::colorNames();
00685 for (k = 0; k < colornames.size(); ++k) {
00686 QPixmap colsample(16, 16);
00687 colsample.fill(QColor(colornames[k]));
00688 new QListWidgetItem(QIcon(colsample), colornames[k], u->lstNames);
00689 }
00690
00691 internalColorChanged(_color);
00692 }
00693
00694 void KLFColorChooseWidget::internalColorChanged(const QColor& wanted_newcolor)
00695 {
00696 QColor newcolor = wanted_newcolor;
00697 if (!_alphaenabled)
00698 newcolor.setAlpha(255);
00699
00700 int k;
00701 for (k = 0; k < _connectedColorChoosers.size(); ++k) {
00702 _connectedColorChoosers[k]->blockSignals(true);
00703 _connectedColorChoosers[k]->setProperty("color", QVariant(newcolor));
00704 _connectedColorChoosers[k]->blockSignals(false);
00705 }
00706 QString newcolorname = newcolor.name();
00707 if (u->txtHex->text() != newcolorname) {
00708 u->txtHex->blockSignals(true);
00709 u->txtHex->setText(newcolorname);
00710 u->txtHex->blockSignals(false);
00711 }
00712
00713 _color = newcolor;
00714
00715 emit colorChanged(newcolor);
00716 }
00717
00718 void KLFColorChooseWidget::internalColorNameSelected(QListWidgetItem *item)
00719 {
00720 if (!item)
00721 return;
00722 QColor color(item->text());
00723 internalColorChanged(color);
00724 }
00725
00726 void KLFColorChooseWidget::internalColorNameSet(const QString& n)
00727 {
00728 klfDbg("name set: "<<n) ;
00729 QString name = n;
00730 static QRegExp rx("\\#?[0-9A-Fa-f]{6}");
00731 bool validinput = false;
00732 bool setcolor = false;
00733 int listselect = -1;
00734 QColor color;
00735 if (rx.exactMatch(name)) {
00736 if (name[0] != QLatin1Char('#'))
00737 name = "#"+name;
00738 validinput = setcolor = true;
00739 color = QColor(name);
00740 } else {
00741
00742 int k;
00743 for (k = 0; k < u->lstNames->count(); ++k) {
00744 QString s = u->lstNames->item(k)->text();
00745 if (s == name) {
00746
00747 validinput = true;
00748 listselect = k;
00749 setcolor = true;
00750 color = QColor(name);
00751 break;
00752 }
00753 if (s.startsWith(n)) {
00754
00755 validinput = true;
00756 listselect = k;
00757 setcolor = false;
00758 break;
00759 }
00760 }
00761 }
00762
00763 if (!validinput) {
00764 u->txtHex->setProperty("invalidInput", true);
00765 u->txtHex->setStyleSheet(u->txtHex->styleSheet());
00766 u->txtHex->setPalette(u->txtHex->property("paletteInvalidInput").value<QPalette>());
00767 } else {
00768 u->txtHex->setProperty("invalidInput", QVariant());
00769 u->txtHex->setStyleSheet(u->txtHex->styleSheet());
00770 u->txtHex->setPalette(u->txtHex->property("paletteDefault").value<QPalette>());
00771 }
00772
00773 if (listselect >= 0) {
00774 u->lstNames->blockSignals(true);
00775 u->lstNames->setCurrentRow(listselect, QItemSelectionModel::ClearAndSelect);
00776 u->lstNames->blockSignals(false);
00777 }
00778 if (setcolor)
00779 internalColorChanged(color);
00780 }
00781
00782 void KLFColorChooseWidget::setColor(const QColor& color)
00783 {
00784 if (color == _color)
00785 return;
00786 if (!_alphaenabled && color.rgb() == _color.rgb())
00787 return;
00788
00789 internalColorChanged(color);
00790 }
00791
00792 void KLFColorChooseWidget::setAlphaEnabled(bool enabled)
00793 {
00794 _alphaenabled = enabled;
00795 u->spnAlpha->setVisible(enabled);
00796 u->lblAlpha->setVisible(enabled);
00797 u->mAlphaPane->setVisible(enabled);
00798 u->lblsAlpha->setVisible(enabled);
00799 u->mAlphaSlider->setVisible(enabled);
00800 if (!enabled) {
00801 _color.setAlpha(255);
00802 setColor(_color);
00803 }
00804 }
00805
00806 void KLFColorChooseWidget::fillPalette(KLFColorList *colorlist, QWidget *w)
00807 {
00808 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00809 klfDbg("colorlist is "<<colorlist<<", _customcolors is "<<_customcolors<<", _recentcolors is "<<_recentcolors) ;
00810 int k;
00811
00812
00813 KLFFlowLayout *lyt = dynamic_cast<KLFFlowLayout*>( w->layout() );
00814 KLF_ASSERT_NOT_NULL(lyt, "Layout is not a KLFFlowLayout !", return; ) ;
00815
00816 lyt->clearAll();
00817 for (k = 0; k < colorlist->list.size(); ++k) {
00818 klfDbg("Adding a KLFColorClickSquare for color: "<<colorlist->list[k]) ;
00819
00820 KLFColorClickSquare *sq = new KLFColorClickSquare(colorlist->list[k], 12,
00821 (colorlist == _customcolors ||
00822 colorlist == _recentcolors),
00823 w);
00824 connect(sq, SIGNAL(colorActivated(const QColor&)),
00825 this, SLOT(internalColorChanged(const QColor&)));
00826 connect(sq, SIGNAL(wantRemoveColor(const QColor&)),
00827 colorlist, SLOT(removeColor(const QColor&)));
00828
00829 lyt->addWidget(sq);
00830 sq->show();
00831 }
00832 w->adjustSize();
00833 }
00834
00835 void KLFColorChooseWidget::setCurrentToCustomColor()
00836 {
00837 _customcolors->addColor(_color);
00838 updatePaletteCustom();
00839 }
00840
00841 void KLFColorChooseWidget::updatePalettes()
00842 {
00843 updatePaletteRecent();
00844 updatePaletteStandard();
00845 updatePaletteCustom();
00846 }
00847
00848 void KLFColorChooseWidget::updatePaletteRecent()
00849 {
00850 fillPalette(_recentcolors, u->mRecentColorsPalette);
00851 }
00852 void KLFColorChooseWidget::updatePaletteStandard()
00853 {
00854 fillPalette(_standardcolors, u->mStandardColorsPalette);
00855 }
00856 void KLFColorChooseWidget::updatePaletteCustom()
00857 {
00858 fillPalette(_customcolors, u->mCustomColorsPalette);
00859 }
00860
00861
00862
00863
00864 void KLFColorChooseWidget::ensureColorListsInstance()
00865 {
00866 if ( _recentcolors == 0 )
00867 _recentcolors = new KLFColorList(128);
00868 if ( _standardcolors == 0 )
00869 _standardcolors = new KLFColorList(256);
00870 if ( _customcolors == 0 )
00871 _customcolors = new KLFColorList(128);
00872 }
00873
00874
00875 void KLFColorChooseWidget::addRecentColor(const QColor& col)
00876 {
00877 ensureColorListsInstance();
00878 QList<QColor>::iterator it = _recentcolors->list.begin();
00879 while (it != _recentcolors->list.end()) {
00880 if ( (*it) == col )
00881 it = _recentcolors->list.erase(it);
00882 else
00883 ++it;
00884 }
00885 _recentcolors->list.append(col);
00886
00887 if (_recentcolors->list.size() > MAX_RECENT_COLORS) {
00888 _recentcolors->list.removeAt(0);
00889 }
00890 _recentcolors->notifyListChanged();
00891 }
00892
00893
00894
00895
00896
00897
00898
00899 void KLFColorList::addColor(const QColor& color)
00900 {
00901 int i;
00902 if ( (i = list.indexOf(color)) >= 0 )
00903 list.removeAt(i);
00904
00905 list.append(color);
00906 while (list.size() >= _maxsize)
00907 list.pop_front();
00908
00909 emit listChanged();
00910 }
00911
00912 void KLFColorList::removeColor(const QColor& color)
00913 {
00914 bool changed = false;
00915 int i;
00916 if ( (i = list.indexOf(color)) >= 0 ) {
00917 list.removeAt(i);
00918 changed = true;
00919 }
00920 if (changed)
00921 emit listChanged();
00922 }
00923
00924
00925 KLFColorList *KLFColorChooser::_colorlist = NULL;
00926
00927 QStyle *KLFColorChooser::mReplaceButtonStyle = NULL;
00928
00929 KLFColorChooser::KLFColorChooser(QWidget *parent)
00930 : QPushButton(parent), _color(0,0,0,255), _pix(), _allowdefaultstate(false),
00931 _defaultstatestring(tr("[ Default ]")), _autoadd(true), _size(120, 20),
00932 _xalignfactor(0.5f), _yalignfactor(0.5f), _alphaenabled(true), mMenu(NULL), menuRelFont(NULL)
00933 {
00934 ensureColorListInstance();
00935 connect(_colorlist, SIGNAL(listChanged()), this, SLOT(_makemenu()));
00936
00937 _makemenu();
00938 _setpix();
00939
00940 #ifdef KLF_WS_MAC
00941 if ( mReplaceButtonStyle == NULL ) {
00942 mReplaceButtonStyle = QStyleFactory::create("fusion");
00943 }
00944 setStyle(mReplaceButtonStyle);
00945 #endif
00946 }
00947
00948
00949 KLFColorChooser::~KLFColorChooser()
00950 {
00951 }
00952
00953
00954 QColor KLFColorChooser::color() const
00955 {
00956 return _color;
00957 }
00958
00959 QSize KLFColorChooser::sizeHint() const
00960 {
00961
00962
00963
00964
00965 ensurePolished();
00966
00967 int w = 0, h = 0;
00968 QStyleOptionButton opt;
00969 initStyleOption(&opt);
00970
00971
00972 w = _pix.width()+4;
00973 h = _pix.height()+2;
00974
00975 opt.rect.setSize(QSize(w,h));
00976
00977 if (menu())
00978 w += KLF_DEBUG_TEE( style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this) );
00979
00980
00981
00982 QSize hint = style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this);
00983
00984 hint = hint.expandedTo(QApplication::globalStrut());
00985
00986 return hint;
00987 }
00988
00989 void KLFColorChooser::setColor(const QColor& col)
00990 {
00991 if ( ! _allowdefaultstate && ! col.isValid() )
00992 return;
00993
00994 if (_color == col)
00995 return;
00996
00997 _color = col;
00998 _setpix();
00999
01000 if (_autoadd && _color.isValid()) {
01001 _colorlist->addColor(_color);
01002 }
01003 emit colorChanged(_color);
01004 }
01005
01006 void KLFColorChooser::setDefaultColor()
01007 {
01008 setColor(QColor());
01009 }
01010
01011 void KLFColorChooser::setAllowDefaultState(bool allow)
01012 {
01013 _allowdefaultstate = allow;
01014 _makemenu();
01015 }
01016 void KLFColorChooser::setDefaultStateString(const QString& str)
01017 {
01018 _defaultstatestring = str;
01019 _makemenu();
01020 }
01021
01022 void KLFColorChooser::setAutoAddToList(bool autoadd)
01023 {
01024 _autoadd = autoadd;
01025 }
01026 void KLFColorChooser::setShowSize(const QSize& size)
01027 {
01028 _size = size;
01029 _setpix();
01030 if (size.isValid())
01031 setMinimumSize(sizeHint());
01032 else
01033 setMinimumSize(QSize());
01034 }
01035 void KLFColorChooser::setPixXAlignFactor(float xalignfactor)
01036 {
01037 _xalignfactor = xalignfactor;
01038 }
01039 void KLFColorChooser::setPixYAlignFactor(float yalignfactor) {
01040 _yalignfactor = yalignfactor;
01041 }
01042
01043 void KLFColorChooser::setAlphaEnabled(bool on)
01044 {
01045 _alphaenabled = on;
01046 _makemenu();
01047 }
01048
01049 void KLFColorChooser::requestColor()
01050 {
01051
01052 QColor col = KLFColorDialog::getColor(_color, _alphaenabled, this);
01053
01054 if ( ! col.isValid() )
01055 return;
01056
01057 setColor(col);
01058 }
01059
01060 void KLFColorChooser::setSenderPropertyColor()
01061 {
01062 QColor c = sender()->property("setColor").value<QColor>();
01063 setColor(c);
01064 }
01065
01066 void KLFColorChooser::_makemenu()
01067 {
01068 if (mMenu) {
01069 setMenu(0);
01070 mMenu->deleteLater();
01071 }
01072
01073 QSize menuIconSize = QSize(16,16);
01074
01075 mMenu = new QMenu(this);
01076
01077 if (_allowdefaultstate) {
01078 mMenu->addAction(QIcon(colorPixmap(QColor(), menuIconSize)), _defaultstatestring,
01079 this, SLOT(setDefaultColor()));
01080 mMenu->addSeparator();
01081 }
01082
01083 int n, k, nk;
01084 ensureColorListInstance();
01085 n = _colorlist->list.size();
01086 for (k = 0; k < n; ++k) {
01087 nk = n - k - 1;
01088 QColor col = _colorlist->list[nk];
01089 if (!_alphaenabled)
01090 col.setAlpha(255);
01091 QString collabel;
01092 if (col.alpha() == 255)
01093 collabel = QString("%1").arg(col.name());
01094 else
01095 collabel = QString("%1 (%2%)").arg(col.name()).arg((int)(100.0*col.alpha()/255.0+0.5));
01096
01097 QAction *a = mMenu->addAction(QIcon(colorPixmap(col, menuIconSize)), collabel,
01098 this, SLOT(setSenderPropertyColor()));
01099 a->setIconVisibleInMenu(true);
01100 a->setProperty("setColor", QVariant::fromValue<QColor>(col));
01101 }
01102 if (k > 0)
01103 mMenu->addSeparator();
01104
01105 mMenu->addAction(tr("Custom ..."), this, SLOT(requestColor()));
01106
01107 if (menuRelFont != NULL)
01108 delete menuRelFont;
01109 menuRelFont = new KLFRelativeFont(this, mMenu);
01110 menuRelFont->setRelPointSize(-1);
01111 setMenu(mMenu);
01112 }
01113
01114 void KLFColorChooser::paintEvent(QPaintEvent *e)
01115 {
01116 QPushButton::paintEvent(e);
01117 QPainter p(this);
01118 p.setClipRect(e->rect());
01119 p.drawPixmap(QPointF(_xalignfactor*(width()-_pix.width()), _yalignfactor*(height()-_pix.height())), _pix);
01120 }
01121
01122 void KLFColorChooser::_setpix()
01123 {
01124
01125 _pix = colorPixmap(_color, _size);
01126
01127
01128
01129 setText("");
01130
01131
01132
01133
01134
01135
01136 }
01137
01138
01139 QPixmap KLFColorChooser::colorPixmap(const QColor& color, const QSize& size)
01140 {
01141 QPixmap pix = QPixmap(size);
01142 pix.fill(Qt::transparent);
01143 if (color.isValid()) {
01144 pix.fill(Qt::black);
01145 QPainter p(&pix);
01146
01147 p.fillRect(0,0,pix.width(),pix.height(), QBrush(QPixmap(":/pics/checker.png")));
01148
01149 p.fillRect(0,0,pix.width(),pix.height(), QBrush(color));
01150
01151 } else {
01152
01153
01154
01155
01156
01157
01158
01159
01160 QPainter p(&pix);
01161 p.setRenderHint(QPainter::Antialiasing);
01162
01163
01164
01165
01166
01167
01168 QPen pen(QColor(127,0,0), 2.f, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
01169 p.setPen(pen);
01170
01171 p.drawLine(QPointF(0,pix.height()), QPointF(pix.width(), 0));
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220 }
01221 return pix;
01222 }
01223
01224
01225
01226
01227 int KLFColorChooser::staticUserMaxColors = 10;
01228
01229
01230
01231 void KLFColorChooser::setUserMaxColors(int maxColors)
01232 {
01233 staticUserMaxColors = maxColors;
01234 }
01235
01236
01237 void KLFColorChooser::ensureColorListInstance()
01238 {
01239 if ( _colorlist == 0 )
01240 _colorlist = new KLFColorList(staticUserMaxColors);
01241 }
01242
01243 void KLFColorChooser::setColorList(const QList<QColor>& colors)
01244 {
01245 ensureColorListInstance();
01246 _colorlist->list = colors;
01247 _colorlist->notifyListChanged();
01248 }
01249
01250
01251 QList<QColor> KLFColorChooser::colorList()
01252 {
01253 ensureColorListInstance();
01254 QList<QColor> l = _colorlist->list;
01255 return l;
01256 }
01257
01258
01259
01260