7#include "KoEditColorSetDialog.h"
13#include <QColorDialog>
14#include <QInputDialog>
16#include <klocalizedstring.h>
17#include <kmessagebox.h>
27KoEditColorSetWidget::KoEditColorSetWidget(
const QList<KoColorSet *> &palettes,
const QString &activePalette, QWidget *parent)
29 m_colorSets(palettes),
33 m_initialColorSetCount(palettes.count()),
34 m_activeColorSetRequested(false)
39 widget.selector->addItem(colorSet->
name());
41 connect(widget.selector, SIGNAL(currentIndexChanged(
int)),
this, SLOT(setActiveColorSet(
int)));
45 m_scrollArea =
new QScrollArea(widget.patchesFrame);
49 if (set->
name() == activePalette) {
50 m_activeColorSet = set;
51 index = widget.selector->findText(set->
name());
52 widget.selector->setCurrentIndex(index);
55 if (!m_activeColorSet && !palettes.isEmpty()) {
56 m_activeColorSet = palettes.first();
57 index = widget.selector->findText(m_activeColorSet->name());
61 if(m_activeColorSet) {
62 columns = m_activeColorSet->columnCount();
67 m_scrollArea->setMinimumWidth(columns*(12+2));
69 QHBoxLayout *
layout =
new QHBoxLayout(widget.patchesFrame);
70 layout->setContentsMargins(0, 0, 0, 0);
71 layout->addWidget(m_scrollArea);
73 widget.add->setIcon(
koIcon(
"list-add"));
74 widget.remove->setIcon(
koIcon(
"list-remove"));
75 widget.open->setIcon(
koIcon(
"document-open"));
76 widget.save->setIcon(
koIcon(
"document-save"));
78 setEnabled(m_activeColorSet != 0);
79 setActiveColorSet(index);
80 widget.remove->setEnabled(
false);
82 connect(widget.add, SIGNAL(clicked()),
this, SLOT(addColor()));
83 connect(widget.remove, SIGNAL(clicked()),
this, SLOT(removeColor()));
84 connect(widget.open, SIGNAL(clicked()),
this, SLOT(
open()));
85 connect(widget.save, SIGNAL(clicked()),
this, SLOT(
save()));
88KoEditColorSetWidget::~KoEditColorSetWidget()
91 uint colorSetCount = m_colorSets.count();
92 for(
uint i = m_initialColorSetCount; i < colorSetCount; ++i ) {
96 if( cs == m_activeColorSet && m_activeColorSetRequested )
102void KoEditColorSetWidget::setActiveColorSet(
int index)
105 qDeleteAll(m_gridLayout->children());
110 QWidget *wdg =
new QWidget(m_scrollArea);
111 m_gridLayout =
new QGridLayout(wdg);
112 m_gridLayout->setContentsMargins(0, 0, 0, 0);
113 m_gridLayout->setSpacing(2);
115 m_activeColorSet = m_colorSets.value(index);
116 setEnabled(m_activeColorSet != 0);
119 if (m_activeColorSet) {
120 columns = m_activeColorSet->columnCount();
121 if (columns==0){columns=16;}
122 widget.remove->setEnabled(
false);
123 for (quint32 i = 0; i < m_activeColorSet->nColors(); i++) {
125 KoColorSetEntry c = m_activeColorSet->getColorGlobal(i);
127 patch->setToolTip(c.name());
129 m_gridLayout->addWidget(patch, i/columns, i%columns);
133 m_scrollArea->setMinimumWidth(columns*(12+2));
134 m_scrollArea->setWidget(wdg);
137void KoEditColorSetWidget::setTextLabel(
KoColorPatch *patch)
139 widget.colorName->setText(patch->toolTip());
141 m_activePatch->setFrameShape(QFrame::NoFrame);
142 m_activePatch->setFrameShadow(QFrame::Plain);
144 m_activePatch = patch;
145 m_activePatch->setFrameShape(QFrame::Panel);
146 m_activePatch->setFrameShadow(QFrame::Raised);
147 widget.remove->setEnabled(
true);
150void KoEditColorSetWidget::addColor()
154 color = QColorDialog::getColor(color);
155 if (color.isValid()) {
156 KoColorSetEntry newEntry(
158 QInputDialog::getText(
this, i18n(
"Add Color To Palette"), i18n(
"Color name:")));
161 patch->setToolTip(newEntry.name());
163 Q_ASSERT(m_gridLayout);
164 Q_ASSERT(m_activeColorSet);
165 m_gridLayout->addWidget(patch, m_activeColorSet->nColors()/m_activeColorSet->columnCount(), m_activeColorSet->nColors()%m_activeColorSet->columnCount());
166 m_activeColorSet->add(newEntry);
170void KoEditColorSetWidget::removeColor()
172 Q_ASSERT(m_activeColorSet);
173 for (quint32 i = 0; i < m_activeColorSet->nColors(); i++) {
174 KoColorSetEntry c = m_activeColorSet->getColorGlobal(i);
175 if (m_activePatch->color() == c.color()) {
176 m_activeColorSet->removeAt(i);
177 setActiveColorSet(widget.selector->currentIndex());
183void KoEditColorSetWidget::open()
185 Q_ASSERT(m_activeColorSet);
187 dialog.setDefaultDir(m_activeColorSet->filename());
189 QString fileName =
dialog.filename();
192 m_colorSets.append(colorSet);
193 widget.selector->addItem(colorSet->
name());
194 widget.selector->setCurrentIndex(widget.selector->count() - 1);
197void KoEditColorSetWidget::save()
199 Q_ASSERT(m_activeColorSet);
200 if (!m_activeColorSet->save())
201 KMessageBox::error(0, i18n(
"Cannot write to palette file %1. Maybe it is read-only. ", m_activeColorSet->filename()), i18n(
"Palette"));
204KoColorSet *KoEditColorSetWidget::activeColorSet()
206 m_activeColorSetRequested =
true;
207 return m_activeColorSet;
210KoEditColorSetDialog::KoEditColorSetDialog(
const QList<KoColorSet *> &palettes,
const QString &activePalette, QWidget *parent)
213 ui =
new KoEditColorSetWidget(palettes, activePalette,
this);
215 setCaption(i18n(
"Add/Remove Colors"));
219KoEditColorSetDialog::~KoEditColorSetDialog()
224KoColorSet *KoEditColorSetDialog::activeColorSet()
226 return ui->activeColorSet();
QList< QString > QStringList
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void setColor(const KoColor &c)
A dialog base class with standard buttons and predefined layouts.
@ Ok
Show Ok button. (this button accept()s the dialog; result set to QDialog::Accepted)
#define koIcon(name)
Use these macros for icons without any issues.
QAction * save(const QObject *recvr, const char *slot, QObject *parent)
QAction * open(const QObject *recvr, const char *slot, QObject *parent)
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
static KoColorSpaceRegistry * instance()
bool load(KisResourcesInterfaceSP resourcesInterface)