diff --git a/src/kcolorcombo.h b/src/kcolorcombo.h --- a/src/kcolorcombo.h +++ b/src/kcolorcombo.h @@ -56,6 +56,13 @@ explicit KColorCombo(QWidget *parent = nullptr); ~KColorCombo() override; + /** Struct used in NamedColors list */ + struct KNamedColor + { + QString colorName; + QColor color; + }; + /** * Selects the color @p col. */ @@ -80,12 +87,34 @@ **/ void setColors(const QList &colors); + /** + * Set a custom list of named colors to choose from, in place of the + * standard list. + * @param colors map os named colors. If empty, the selction list + * reverts to the standard list. + **/ + void setNamedColors(const QList &colors); + + /** + * Inserts a named color at a specific position in the standard list + * @param index position in the list + * @param namedColor name and color + **/ + void insertNamedColor(int index, const KNamedColor &namedColor); + /** * Return the list of colors available for selection. * @return list of colors **/ QList colors() const; + /** + * Return the list of named colors available for selecion. + * If no named color, return namedColor is empty. + * @return list of named colors + **/ + QList namedColors() const; + /** * Clear the color list and don't show it, till the next setColor() call **/ diff --git a/src/kcolorcombo.cpp b/src/kcolorcombo.cpp --- a/src/kcolorcombo.cpp +++ b/src/kcolorcombo.cpp @@ -183,7 +183,7 @@ void _k_slotHighlighted(int index); KColorCombo *q; - QList colorList; + QList colorList; QColor customColor; QColor internalcolor; }; @@ -205,11 +205,12 @@ } } } else { - int i = colorList.indexOf(color); - if (i >= 0) { - q->setCurrentIndex(i + 1); - internalcolor = color; - return; + for (int i = 0; i < colorList.count(); ++i) { + if (colorList[i].color == color) { + q->setCurrentIndex(i + 1); + internalcolor = color; + return; + } } } } @@ -241,19 +242,43 @@ } void KColorCombo::setColors(const QList &colors) +{ + QList namedColors; + for (int colorIndex = 0; colorIndex < colors.count(); colorIndex++) { + namedColors.insert(colorIndex, { QString(), colors[colorIndex] }); + } + setNamedColors(namedColors); +} + +void KColorCombo::setNamedColors(const QList &colors) { clear(); d->colorList = colors; d->addColors(); } +void KColorCombo::insertNamedColor(int index, const KNamedColor &namedColor) +{ + QList namedColorList = namedColors(); + namedColorList.insert(index, namedColor); + setNamedColors(namedColorList); +} + QList KColorCombo::colors() const +{ + QList list; + for (int index = 0; index < STANDARD_PALETTE_SIZE; ++index) { + list += namedColors()[index].color; + } + return list; +} + +QList KColorCombo::namedColors() const { if (d->colorList.isEmpty()) { - QList list; - list.reserve(STANDARD_PALETTE_SIZE); - for (int i = 0; i < STANDARD_PALETTE_SIZE; ++i) { - list += standardColor(i); + QList list; + for (int index = 0; index < STANDARD_PALETTE_SIZE; ++index) { + list += {QString(), standardColor(index)}; } return list; } else { @@ -317,7 +342,7 @@ } else if (colorList.isEmpty()) { internalcolor = standardColor(index - 1); } else { - internalcolor = colorList[index - 1]; + internalcolor = colorList[index - 1].color; } emit q->activated(internalcolor); @@ -330,25 +355,25 @@ } else if (colorList.isEmpty()) { internalcolor = standardColor(index - 1); } else { - internalcolor = colorList[index - 1]; + internalcolor = colorList[index - 1].color; } emit q->highlighted(internalcolor); } void KColorComboPrivate::addColors() { q->addItem(KColorCombo::tr("Custom...", "Custom color")); - if (colorList.isEmpty()) { for (int i = 0; i < STANDARD_PALETTE_SIZE; ++i) { q->addItem(QString()); q->setItemData(i + 1, standardColor(i), KColorComboDelegate::ColorRole); } } else { for (int i = 0, count = colorList.count(); i < count; ++i) { q->addItem(QString()); - q->setItemData(i + 1, colorList[i], KColorComboDelegate::ColorRole); + q->setItemData(i + 1, colorList[i].color, KColorComboDelegate::ColorRole); + q->setItemText(i + 1, colorList[i].colorName); } } }