29#include <QColorDialog>
32#include <QStylePainter>
33#include <QStyleFactory>
34#include <QStyleOptionButton>
42#include "klfcolorchooser_p.h"
44#include <ui_klfcolorchoosewidget.h>
45#include <ui_klfcolordialog.h>
54 u =
new Ui::KLFColorDialog;
56 setObjectName(
"KLFColorDialog");
65 return u->mColorChooseWidget;
71 dlg.u->mColorChooseWidget->setAlphaEnabled(alphaenabled);
72 dlg.u->mColorChooseWidget->setColor(startwith);
74 if ( r != QDialog::Accepted )
82 return u->mColorChooseWidget->color();
86 u->mColorChooseWidget->setColor(
color);
88void KLFColorDialog::slotAccepted()
96 : QWidget(parent), _color(color), _removable(removable)
102 : QWidget(parent), _color(
Qt::white), _removable(false)
107void KLFColorClickSquare::initwidget()
109 setFocusPolicy(Qt::StrongFocus);
110 setContextMenuPolicy(Qt::DefaultContextMenu);
121 setFixedSize(_size, _size);
139 QStylePainter p(
this);
140 QStyleOptionFocusRect option;
141 option.initFrom(
this);
142 option.backgroundColor =
QColor(0,0,0,0);
143 p.drawPrimitive(QStyle::PE_FrameFocusRect, option);
157 if (kev->
key() == Qt::Key_Space) {
160 return QWidget::keyPressEvent(kev);
165 QMenu *menu =
new QMenu(
this);
166 menu->addAction(
"Remove",
this, SLOT(internalWantRemove()));
170void KLFColorClickSquare::internalWantRemove()
179 : QWidget(parent), _img()
191 return QSize(16, 16);
206 QStringList() <<
"hue"<<
"sat"<<
"val"<<
"red"<<
"green"<<
"blue"<<
"alpha"<<
"fix";
209 if (strlist.
size() != 2) {
210 qWarning()<<
KLF_FUNC_NAME<<
": expected a pane-type string \"<pane1type>+<pane2type>\"!";
221 setFocusPolicy(Qt::NoFocus);
223 setFocusPolicy(Qt::WheelFocus);
227 QStylePainter p(
this);
229 p.fillRect(0,0,width(),height(),
QBrush(
QPixmap(
":/pics/checker.png")));
233 _img =
QImage(width(), height(), QImage::Format_ARGB32);
236 for (x = 0; x < _img.
width(); ++x) {
237 for (y = 0; y < _img.
height(); ++y) {
241 p.drawImage(0, 0, _img);
245 p.setPen(
QPen(hairscol, 1.f, Qt::DotLine));
247 if (x < 0) { x = 0; }
248 if (x >= width()) { x = width()-1; }
249 p.drawLine(x, 0, x, height());
252 p.setPen(
QPen(hairscol, 1.f, Qt::DotLine));
254 if (y < 0) { y = 0; }
255 if (y >= height()) { y = height()-1; }
256 p.drawLine(0, height()-y-1, width(), height()-y-1);
260 QStyleOptionFocusRect option;
261 option.initFrom(
this);
262 option.backgroundColor =
QColor(0,0,0,0);
263 p.drawPrimitive(QStyle::PE_FrameFocusRect, option);
270 int x = e->
pos().
x();
271 int y = height() - e->
pos().
y() - 1;
279 int x = e->
pos().
x();
280 int y = height() - e->
pos().
y() - 1;
281 if (x < 0) { x = 0; }
282 if (x >= width()) { x = width()-1; }
283 if (y < 0) { y = 0; }
if (y >= height()) { y = height()-1; }
289 double step = - 7.5 * e->
delta() / 120;
293 if (e->
modifiers() == Qt::ControlModifier)
304 int x = (int)(
valueA()+step);
309 int x = (int)(
valueB() - step);
318 const int dir_step = 5;
322 if (e->
key() == Qt::Key_Left)
324 if (e->
key() == Qt::Key_Right)
326 if (e->
key() == Qt::Key_Up)
328 if (e->
key() == Qt::Key_Down)
330 if (e->
key() == Qt::Key_Home)
332 if (e->
key() == Qt::Key_End)
334 if (e->
key() == Qt::Key_PageUp)
336 if (e->
key() == Qt::Key_PageDown)
348 if (e->
modifiers() == Qt::ShiftModifier) {
349 xstep = xstep / 5; ystep = ystep / 5;
351 if (e->
modifiers() == Qt::ControlModifier) {
352 xstep = xstep * 2.5; ystep = ystep * 2.5;
355 int x = (int)(
valueA() + xstep);
356 int y = (int)(
valueB() + ystep);
370 : QGridLayout(parent), _ncols(columns),
371 _currow(0), _curcol(0)
373 addItem(
new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed), 0,
_ncols);
413 if (component ==
"hue") {
415 }
else if (component ==
"sat") {
417 }
else if (component ==
"val") {
418 value = color.
value();
419 }
else if (component ==
"red") {
421 }
else if (component ==
"green") {
422 value = color.
green();
423 }
else if (component ==
"blue") {
424 value = color.
blue();
425 }
else if (component ==
"alpha") {
426 value = color.
alpha();
427 }
else if (component ==
"fix" || component.
isEmpty()) {
437 if (component ==
"hue")
439 else if (component ==
"sat" || component ==
"val" ||
440 component ==
"red" || component ==
"green" ||
441 component ==
"blue" || component ==
"alpha")
443 else if (component ==
"fix" || component.
isEmpty())
534 connect(
this, SIGNAL(valueChanged(
int)),
this, SLOT(internalChanged(
int)));
546void KLFColorComponentSpinBox::internalChanged(
int newvalue)
568KLFColorList * KLFColorChooseWidget::_recentcolors = 0;
569KLFColorList * KLFColorChooseWidget::_standardcolors = 0;
570KLFColorList * KLFColorChooseWidget::_customcolors = 0;
576 _recentcolors->list = recentcolors;
577 _recentcolors->notifyListChanged();
578 _customcolors->list = customcolors;
579 _customcolors->notifyListChanged();
585 return _recentcolors->list;
591 return _customcolors->list;
598 u =
new Ui::KLFColorChooseWidget;
600 setObjectName(
"KLFColorChooseWidget");
602 _alphaenabled =
true;
606 if (_standardcolors->list.size() == 0) {
610 rgbs << 0x000000 << 0x303030 << 0x585858 << 0x808080 << 0xa0a0a0 << 0xc3c3c3
611 << 0xdcdcdc << 0xffffff << 0x400000 << 0x800000 << 0xc00000 << 0xff0000
612 << 0xffc0c0 << 0x004000 << 0x008000 << 0x00c000 << 0x00ff00 << 0xc0ffc0
613 << 0x000040 << 0x000080 << 0x0000c0 << 0x0000ff << 0xc0c0ff << 0x404000
614 << 0x808000 << 0xc0c000 << 0xffff00 << 0xffffc0 << 0x004040 << 0x008080
615 << 0x00c0c0 << 0x00ffff << 0xc0ffff << 0x400040 << 0x800080 << 0xc000c0
616 << 0xff00ff << 0xffc0ff << 0xc05800 << 0xff8000 << 0xffa858 << 0xffdca8 ;
617 for (
int k = 0; k < rgbs.
size(); ++k)
618 _standardcolors->list.append(
QColor(QRgb(rgbs[k])));
621 _connectedColorChoosers.
append(u->mDisplayColor);
622 _connectedColorChoosers.
append(u->mHueSatPane);
623 _connectedColorChoosers.
append(u->mValPane);
624 _connectedColorChoosers.
append(u->mAlphaPane);
625 _connectedColorChoosers.
append(u->mColorTriangle);
626 _connectedColorChoosers.
append(u->mHueSlider);
627 _connectedColorChoosers.
append(u->mSatSlider);
628 _connectedColorChoosers.
append(u->mValSlider);
629 _connectedColorChoosers.
append(u->mRedSlider);
630 _connectedColorChoosers.
append(u->mGreenSlider);
631 _connectedColorChoosers.
append(u->mBlueSlider);
632 _connectedColorChoosers.
append(u->mAlphaSlider);
633 _connectedColorChoosers.
append(u->spnHue);
634 _connectedColorChoosers.
append(u->spnSat);
635 _connectedColorChoosers.
append(u->spnVal);
636 _connectedColorChoosers.
append(u->spnRed);
637 _connectedColorChoosers.
append(u->spnGreen);
638 _connectedColorChoosers.
append(u->spnBlue);
639 _connectedColorChoosers.
append(u->spnAlpha);
665 for (k = 0; k < _connectedColorChoosers.
size(); ++k) {
670 connect(u->lstNames, SIGNAL(itemClicked(QListWidgetItem*)),
672 connect(u->txtHex, SIGNAL(textChanged(
const QString&)),
676 u->txtHex->setProperty(
"paletteDefault", QVariant::fromValue<QPalette>(p));
678 u->txtHex->setProperty(
"paletteInvalidInput", QVariant::fromValue<QPalette>(p));
681 connect(u->btnAddCustomColor, SIGNAL(clicked()),
685 for (k = 0; k < colornames.
size(); ++k) {
688 new QListWidgetItem(
QIcon(colsample), colornames[k], u->lstNames);
696 QColor newcolor = wanted_newcolor;
701 for (k = 0; k < _connectedColorChoosers.
size(); ++k) {
702 _connectedColorChoosers[k]->blockSignals(
true);
703 _connectedColorChoosers[k]->setProperty(
"color",
QVariant(newcolor));
704 _connectedColorChoosers[k]->blockSignals(
false);
707 if (u->txtHex->text() != newcolorname) {
708 u->txtHex->blockSignals(
true);
709 u->txtHex->setText(newcolorname);
710 u->txtHex->blockSignals(
false);
730 static QRegExp rx(
"\\#?[0-9A-Fa-f]{6}");
731 bool validinput =
false;
732 bool setcolor =
false;
738 validinput = setcolor =
true;
743 for (k = 0; k < u->lstNames->count(); ++k) {
744 QString s = u->lstNames->item(k)->text();
764 u->txtHex->setProperty(
"invalidInput",
true);
765 u->txtHex->setStyleSheet(u->txtHex->styleSheet());
766 u->txtHex->setPalette(u->txtHex->property(
"paletteInvalidInput").value<
QPalette>());
768 u->txtHex->setProperty(
"invalidInput",
QVariant());
769 u->txtHex->setStyleSheet(u->txtHex->styleSheet());
770 u->txtHex->setPalette(u->txtHex->property(
"paletteDefault").value<
QPalette>());
773 if (listselect >= 0) {
774 u->lstNames->blockSignals(
true);
775 u->lstNames->setCurrentRow(listselect, QItemSelectionModel::ClearAndSelect);
776 u->lstNames->blockSignals(
false);
794 _alphaenabled = enabled;
795 u->spnAlpha->setVisible(enabled);
796 u->lblAlpha->setVisible(enabled);
797 u->mAlphaPane->setVisible(enabled);
798 u->lblsAlpha->setVisible(enabled);
799 u->mAlphaSlider->setVisible(enabled);
806void KLFColorChooseWidget::fillPalette(KLFColorList *colorlist, QWidget *w)
809 klfDbg(
"colorlist is "<<colorlist<<
", _customcolors is "<<_customcolors<<
", _recentcolors is "<<_recentcolors) ;
817 for (k = 0; k < colorlist->list.size(); ++k) {
818 klfDbg(
"Adding a KLFColorClickSquare for color: "<<colorlist->list[k]) ;
821 (colorlist == _customcolors ||
822 colorlist == _recentcolors),
824 connect(sq, SIGNAL(colorActivated(
const QColor&)),
826 connect(sq, SIGNAL(wantRemoveColor(
const QColor&)),
827 colorlist, SLOT(removeColor(
const QColor&)));
837 _customcolors->addColor(_color);
850 fillPalette(_recentcolors, u->mRecentColorsPalette);
854 fillPalette(_standardcolors, u->mStandardColorsPalette);
858 fillPalette(_customcolors, u->mCustomColorsPalette);
866 if ( _recentcolors == 0 )
867 _recentcolors =
new KLFColorList(128);
868 if ( _standardcolors == 0 )
869 _standardcolors =
new KLFColorList(256);
870 if ( _customcolors == 0 )
871 _customcolors =
new KLFColorList(128);
879 while (it != _recentcolors->list.end()) {
881 it = _recentcolors->list.erase(it);
885 _recentcolors->list.append(col);
887 if (_recentcolors->list.size() > MAX_RECENT_COLORS) {
888 _recentcolors->list.removeAt(0);
890 _recentcolors->notifyListChanged();
899void KLFColorList::addColor(
const QColor& color)
902 if ( (i = list.indexOf(color)) >= 0 )
906 while (list.size() >= _maxsize)
912void KLFColorList::removeColor(
const QColor& color)
914 bool changed =
false;
916 if ( (i = list.indexOf(color)) >= 0 ) {
925KLFColorList *KLFColorChooser::_colorlist = NULL;
927QStyle *KLFColorChooser::mReplaceButtonStyle = NULL;
930 : QPushButton(parent), _color(0,0,0,255), _pix(), _allowdefaultstate(false),
931 _defaultstatestring(tr(
"[ Default ]")), _autoadd(true), _size(120, 20),
932 _xalignfactor(0.5f), _yalignfactor(0.5f), _alphaenabled(true), mMenu(NULL), menuRelFont(NULL)
934 ensureColorListInstance();
935 connect(_colorlist, SIGNAL(listChanged()),
this, SLOT(
_makemenu()));
941 if ( mReplaceButtonStyle == NULL ) {
942 mReplaceButtonStyle = QStyleFactory::create(
"fusion");
944 setStyle(mReplaceButtonStyle);
968 QStyleOptionButton opt;
969 initStyleOption(&opt);
975 opt.rect.setSize(
QSize(w,h));
978 w +=
KLF_DEBUG_TEE(
style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt,
this) );
982 QSize hint =
style()->sizeFromContents(QStyle::CT_PushButton, &opt,
QSize(w, h),
this);
984 hint = hint.
expandedTo(QApplication::globalStrut());
991 if ( ! _allowdefaultstate && ! col.
isValid() )
1000 if (_autoadd && _color.
isValid()) {
1001 _colorlist->addColor(_color);
1013 _allowdefaultstate = allow;
1018 _defaultstatestring = str;
1033 setMinimumSize(
QSize());
1037 _xalignfactor = xalignfactor;
1040 _yalignfactor = yalignfactor;
1070 mMenu->deleteLater();
1075 mMenu =
new QMenu(
this);
1077 if (_allowdefaultstate) {
1078 mMenu->addAction(
QIcon(colorPixmap(
QColor(), menuIconSize)), _defaultstatestring,
1080 mMenu->addSeparator();
1084 ensureColorListInstance();
1085 n = _colorlist->list.size();
1086 for (k = 0; k < n; ++k) {
1088 QColor col = _colorlist->list[nk];
1092 if (col.
alpha() == 255)
1097 QAction *a = mMenu->addAction(
QIcon(colorPixmap(col, menuIconSize)), collabel,
1099 a->setIconVisibleInMenu(
true);
1100 a->setProperty(
"setColor", QVariant::fromValue<QColor>(col));
1103 mMenu->addSeparator();
1105 mMenu->addAction(tr(
"Custom ..."),
this, SLOT(
requestColor()));
1107 if (menuRelFont != NULL)
1116 QPushButton::paintEvent(e);
1122void KLFColorChooser::_setpix()
1125 _pix = colorPixmap(_color, _size);
1142 pix.
fill(Qt::transparent);
1144 pix.
fill(Qt::black);
1161 p.setRenderHint(QPainter::Antialiasing);
1168 QPen pen(
QColor(127,0,0), 2.f, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
1227int KLFColorChooser::staticUserMaxColors = 10;
1233 staticUserMaxColors = maxColors;
1237void KLFColorChooser::ensureColorListInstance()
1239 if ( _colorlist == 0 )
1240 _colorlist =
new KLFColorList(staticUserMaxColors);
1245 ensureColorListInstance();
1246 _colorlist->list = colors;
1247 _colorlist->notifyListChanged();
1253 ensureColorListInstance();
void setAlphaEnabled(bool alpha_enabled)
KLFColorChooser(QWidget *parent)
void setAutoAddToList(bool autoadd)
void paintEvent(QPaintEvent *event)
void setPixXAlignFactor(float xalignfactor)
void setAllowDefaultState(bool allow)
void colorChanged(const QColor &newcolor)
void setShowSize(const QSize &size)
static QList< QColor > colorList()
void setSenderPropertyColor()
static void setUserMaxColors(int maxcolors)
void setPixYAlignFactor(float yalignfactor)
void setColor(const QColor &color)
virtual QSize sizeHint() const
void setDefaultStateString(const QString &str)
static void setColorList(const QList< QColor > &colorlist)
void setSqSize(int sqsize)
void paintEvent(QPaintEvent *event)
void resizeEvent(QResizeEvent *event)
void mousePressEvent(QMouseEvent *event)
void wantRemoveColor(const QColor &color)
void keyPressEvent(QKeyEvent *event)
void setRemovable(bool removable)
KLFColorClickSquare(QColor color=Qt::white, int size=16, bool removable=true, QWidget *parent=0)
void contextMenuEvent(QContextMenuEvent *event)
void setColorComponent(const QString &component)
KLFColorComponentSpinBox(QWidget *parent)
void colorChanged(const QColor &color)
void setColor(const QColor &color)
QString _colorcomponent_b
static int valueMax(const QString &component)
QColor colorFromValues(QColor color_base, int value_a, int value_b=-1)
static int valueFromNewColor(const QColor &color, const QString &component)
int valueAFromNewColor(const QColor &color) const
bool refreshColorFromInternalValues(int value_a, int value_b=-1)
int valueBFromNewColor(const QColor &color) const
A dialog to let the user select a color.
KLFColorChooseWidget * colorChooseWidget()
static QColor getColor(QColor startwith=Qt::black, bool alphaenabled=true, QWidget *parent=0)
void setColor(const QColor &color)
virtual ~KLFColorDialog()
KLFColorDialog(QWidget *parent=0)
A Layout that places widgets left to right, top to bottom.
void clearAll(bool deleteItems=true)
virtual void addWidget(QWidget *w, int hstretch=0, int vstretch=0, Qt::Alignment align=0)
@ FlushBegin
Leave all extra space at end of line.
KLFGridFlowLayout(int columns, QWidget *parent)
virtual void insertGridFlowWidget(QWidget *w, Qt::Alignment align=0)
QList< QWidget * > mGridFlowWidgets
void setRelPointSize(int relps)
#define KLF_DEBUG_BLOCK(msg)
Utility to debug the execution of a block.
#define KLF_ASSERT_NOT_NULL(ptr, msg, failaction)
Asserting Non-NULL pointers (NON-FATAL)
#define KLF_DEBUG_TEE(expr)
Print the value of expression and return it.
#define klfDbg(streamableItems)
print debug stream items
Base declarations for klatexformula and some utilities.
const char * constData() const
void setHsv(int h, int s, int v, int a)
void setRgb(int r, int g, int b, int a)
void setPixel(int x, int y, uint index_or_rgb)
Qt::KeyboardModifiers modifiers() const
void append(const T &value)
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
void fillRect(const QRectF &rectangle, const QBrush &brush)
void setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
const QRect & rect() const
void setColor(ColorGroup group, ColorRole role, const QColor &color)
void fill(const QColor &color)
bool exactMatch(const QString &str) const
QSize expandedTo(const QSize &otherSize) const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QByteArray toLocal8Bit() const
bool contains(const QString &str, Qt::CaseSensitivity cs) const
Qt::Orientation orientation() const