Krita Source Code Documentation
Loading...
Searching...
No Matches
KoEditColorSetDialog.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2007 Fredy Yanardi <fyanardi@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "KoEditColorSetDialog.h"
8
9#include <KoIcon.h>
10
11#include <QScrollArea>
12#include <QHBoxLayout>
13#include <QColorDialog>
14#include <QInputDialog>
15
16#include <klocalizedstring.h>
17#include <kmessagebox.h>
18
20#include <KoColorPatch.h>
22#include <KoFileDialog.h>
23
24// debug
25#include <WidgetsDebug.h>
26
27KoEditColorSetWidget::KoEditColorSetWidget(const QList<KoColorSet *> &palettes, const QString &activePalette, QWidget *parent)
28 : QWidget(parent),
29 m_colorSets(palettes),
30 m_gridLayout(0),
31 m_activeColorSet(0),
32 m_activePatch(0),
33 m_initialColorSetCount(palettes.count()),
34 m_activeColorSetRequested(false)
35{
36 widget.setupUi(this);
37 foreach (KoColorSet *colorSet, m_colorSets) {
38 //colorSet->load(); resources are loaded at startup...
39 widget.selector->addItem(colorSet->name());
40 }
41 connect(widget.selector, SIGNAL(currentIndexChanged(int)), this, SLOT(setActiveColorSet(int)));
42
43 // A widget that shows all colors from active palette
44 // FIXME no need to hand code the QScrollArea if designer can add QScrollArea (Qt 4.4?)
45 m_scrollArea = new QScrollArea(widget.patchesFrame);
46
47 int index = 0;
48 foreach (KoColorSet *set, m_colorSets) {
49 if (set->name() == activePalette) {
50 m_activeColorSet = set;
51 index = widget.selector->findText(set->name());
52 widget.selector->setCurrentIndex(index);
53 }
54 }
55 if (!m_activeColorSet && !palettes.isEmpty()) {
56 m_activeColorSet = palettes.first();
57 index = widget.selector->findText(m_activeColorSet->name());
58 }
59
60 int columns = 16;
61 if(m_activeColorSet) {
62 columns = m_activeColorSet->columnCount();
63 if (columns==0){
64 columns = 16;
65 }
66 }
67 m_scrollArea->setMinimumWidth(columns*(12+2));
68
69 QHBoxLayout *layout = new QHBoxLayout(widget.patchesFrame);
70 layout->setContentsMargins(0, 0, 0, 0);
71 layout->addWidget(m_scrollArea);
72
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"));
77
78 setEnabled(m_activeColorSet != 0);
79 setActiveColorSet(index);
80 widget.remove->setEnabled(false); // initially no color selected
81
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()));
86}
87
88KoEditColorSetWidget::~KoEditColorSetWidget()
89{
90 // only delete new color sets
91 uint colorSetCount = m_colorSets.count();
92 for( uint i = m_initialColorSetCount; i < colorSetCount; ++i ) {
93 KoColorSet * cs = m_colorSets[i];
94 // if the active color set was requested by activeColorSet()
95 // the caller takes ownership and then we do not delete it here
96 if( cs == m_activeColorSet && m_activeColorSetRequested )
97 continue;
98 delete cs;
99 }
100}
101
102void KoEditColorSetWidget::setActiveColorSet(int index)
103{
104 if (m_gridLayout) {
105 qDeleteAll(m_gridLayout->children());
106 delete m_gridLayout;
107 m_activePatch = 0;
108 }
109
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);
114
115 m_activeColorSet = m_colorSets.value(index);
116 setEnabled(m_activeColorSet != 0);
117 int columns = 16;
118
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++) {
124 KoColorPatch *patch = new KoColorPatch(widget.patchesFrame);
125 KoColorSetEntry c = m_activeColorSet->getColorGlobal(i);
126 patch->setColor(c.color());
127 patch->setToolTip(c.name());
128 connect(patch, SIGNAL(triggered(KoColorPatch*)), this, SLOT(setTextLabel(KoColorPatch*)));
129 m_gridLayout->addWidget(patch, i/columns, i%columns);
130 }
131 }
132
133 m_scrollArea->setMinimumWidth(columns*(12+2));
134 m_scrollArea->setWidget(wdg);
135}
136
137void KoEditColorSetWidget::setTextLabel(KoColorPatch *patch)
138{
139 widget.colorName->setText(patch->toolTip());
140 if (m_activePatch) {
141 m_activePatch->setFrameShape(QFrame::NoFrame);
142 m_activePatch->setFrameShadow(QFrame::Plain);
143 }
144 m_activePatch = patch;
145 m_activePatch->setFrameShape(QFrame::Panel);
146 m_activePatch->setFrameShadow(QFrame::Raised);
147 widget.remove->setEnabled(true);
148}
149
150void KoEditColorSetWidget::addColor()
151{
152 QColor color;
153
154 color = QColorDialog::getColor(color);
155 if (color.isValid()) {
156 KoColorSetEntry newEntry(
157 KoColor(color, KoColorSpaceRegistry::instance()->rgb8()),
158 QInputDialog::getText(this, i18n("Add Color To Palette"), i18n("Color name:")));
159 KoColorPatch *patch = new KoColorPatch(widget.patchesFrame);
160 patch->setColor(newEntry.color());
161 patch->setToolTip(newEntry.name());
162 connect(patch, SIGNAL(triggered(KoColorPatch*)), this, SLOT(setTextLabel(KoColorPatch*)));
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);
167 }
168}
169
170void KoEditColorSetWidget::removeColor()
171{
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());
178 break;
179 }
180 }
181}
182
183void KoEditColorSetWidget::open()
184{
185 Q_ASSERT(m_activeColorSet);
186 KoFileDialog dialog(this, KoFileDialog::OpenFile, "OpenColorSet");
187 dialog.setDefaultDir(m_activeColorSet->filename());
188 dialog.setMimeTypeFilters(QStringList() << "application/x-gimp-color-palette");
189 QString fileName = dialog.filename();
190 KoColorSet *colorSet = new KoColorSet(fileName);
191 colorSet->load();
192 m_colorSets.append(colorSet);
193 widget.selector->addItem(colorSet->name());
194 widget.selector->setCurrentIndex(widget.selector->count() - 1);
195}
196
197void KoEditColorSetWidget::save()
198{
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"));
202}
203
204KoColorSet *KoEditColorSetWidget::activeColorSet()
205{
206 m_activeColorSetRequested = true;
207 return m_activeColorSet;
208}
209
210KoEditColorSetDialog::KoEditColorSetDialog(const QList<KoColorSet *> &palettes, const QString &activePalette, QWidget *parent)
212{
213 ui = new KoEditColorSetWidget(palettes, activePalette, this);
214 setMainWidget(ui);
215 setCaption(i18n("Add/Remove Colors"));
216 enableButton(KoDialog::Ok, ui->isEnabled());
217}
218
219KoEditColorSetDialog::~KoEditColorSetDialog()
220{
221 delete ui;
222}
223
224KoColorSet *KoEditColorSetDialog::activeColorSet()
225{
226 return ui->activeColorSet();
227}
QList< QString > QStringList
unsigned int uint
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void setColor(const KoColor &c)
A dialog base class with standard buttons and predefined layouts.
Definition KoDialog.h:116
@ Ok
Show Ok button. (this button accept()s the dialog; result set to QDialog::Accepted)
Definition KoDialog.h:127
#define koIcon(name)
Use these macros for icons without any issues.
Definition kis_icon.h:25
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)
Definition KisForest.h:327
static KoColorSpaceRegistry * instance()
bool load(KisResourcesInterfaceSP resourcesInterface)
QString name