Changeset View
Changeset View
Standalone View
Standalone View
libs/ui/dialogs/KisDlgPaletteEditor.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 Michael Zhou <simeirxh@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include <QAction> | ||||
20 | #include <QFileInfo> | ||||
21 | #include <QSpinBox> | ||||
22 | #include <QLabel> | ||||
23 | #include <QVBoxLayout> | ||||
24 | #include <QLineEdit> | ||||
25 | #include <QMessageBox> | ||||
26 | #include <QComboBox> | ||||
27 | #include <QInputDialog> | ||||
28 | #include <QFormLayout> | ||||
29 | #include <QPicture> | ||||
30 | #include <QPushButton> | ||||
31 | #include <QSignalBlocker> | ||||
32 | | ||||
33 | #include <KoResourceServerProvider.h> | ||||
34 | #include <KoResourceServer.h> | ||||
35 | #include <KoDialog.h> | ||||
36 | #include <KoColorSet.h> | ||||
37 | #include <kis_global.h> | ||||
38 | #include <KisPaletteModel.h> | ||||
39 | | ||||
40 | #include "KisPaletteEditor.h" | ||||
41 | | ||||
42 | #include "ui_WdgDlgPaletteEditor.h" | ||||
43 | | ||||
44 | #include "KisDlgPaletteEditor.h" | ||||
45 | | ||||
46 | KisDlgPaletteEditor::KisDlgPaletteEditor() | ||||
47 | : m_ui(new Ui_WdgDlgPaletteEditor) | ||||
48 | , m_actAddGroup(new QAction(i18n("Add a group"))) | ||||
49 | , m_actDelGroup(new QAction(i18n("Delete this group"))) | ||||
50 | , m_actRenGroup(new QAction(i18n("Rename this group"))) | ||||
51 | , m_paletteEditor(new KisPaletteEditor(this)) | ||||
52 | , m_currentGroupOriginalName(KoColorSet::GLOBAL_GROUP_NAME) | ||||
53 | { | ||||
54 | setWindowTitle(i18n("Palette Editor")); | ||||
55 | | ||||
56 | m_ui->setupUi(this); | ||||
57 | m_ui->gbxPalette->setTitle(i18n("Palette settings")); | ||||
58 | m_ui->labelFilename->setText(i18n("Filename")); | ||||
59 | m_ui->labelName->setText(i18n("Palette Name")); | ||||
60 | m_ui->bnAddGroup->setDefaultAction(m_actAddGroup.data()); | ||||
61 | | ||||
62 | m_ui->gbxGroup->setTitle(i18n("Group settings")); | ||||
63 | m_ui->labelColCount->setText(i18n("Column count")); | ||||
64 | m_ui->labelRowCount->setText(i18n("Row count")); | ||||
65 | m_ui->bnDelGroup->setDefaultAction(m_actDelGroup.data()); | ||||
66 | m_ui->bnRenGroup->setDefaultAction(m_actRenGroup.data()); | ||||
67 | | ||||
68 | connect(m_actAddGroup.data(), SIGNAL(triggered(bool)), SLOT(slotAddGroup())); | ||||
69 | connect(m_actDelGroup.data(), SIGNAL(triggered(bool)), SLOT(slotDelGroup())); | ||||
70 | connect(m_actRenGroup.data(), SIGNAL(triggered(bool)), SLOT(slotRenGroup())); | ||||
71 | connect(m_ui->spinBoxRow, SIGNAL(valueChanged(int)), SLOT(slotRowCountChanged(int))); | ||||
72 | connect(m_ui->spinBoxCol, SIGNAL(valueChanged(int)), SLOT(slotColCountChanged(int))); | ||||
73 | connect(m_ui->lineEditName, SIGNAL(editingFinished()), SLOT(slotNameChanged())); | ||||
74 | connect(m_ui->lineEditFilename, SIGNAL(textEdited(QString)), SLOT(slotFilenameChanged(QString))); | ||||
75 | connect(m_ui->lineEditFilename, SIGNAL(editingFinished()), SLOT(slotFilenameInputFinished())); | ||||
76 | connect(m_ui->ckxGlobal, SIGNAL(stateChanged(int)), SLOT(slotSetGlobal(int))); | ||||
77 | connect(m_ui->ckxReadOnly, SIGNAL(stateChanged(int)), SLOT(slotSetReadOnly(int))); | ||||
78 | | ||||
79 | connect(this, SIGNAL(accepted()), SLOT(slotAccepted())); | ||||
80 | | ||||
81 | m_warnPalette.setColor(QPalette::Text, Qt::red); | ||||
82 | } | ||||
83 | | ||||
84 | KisDlgPaletteEditor::~KisDlgPaletteEditor() | ||||
85 | { } | ||||
86 | | ||||
87 | void KisDlgPaletteEditor::setPaletteModel(KisPaletteModel *model) | ||||
88 | { | ||||
89 | m_colorSet = model->colorSet(); | ||||
90 | if (m_colorSet.isNull()) { | ||||
91 | return; | ||||
92 | } | ||||
93 | m_paletteEditor->setPaletteModel(model); | ||||
94 | | ||||
95 | // don't let editor make changes when initializing | ||||
96 | const QSignalBlocker blocker1(m_ui->lineEditFilename); | ||||
97 | const QSignalBlocker blocker2(m_ui->lineEditName); | ||||
98 | const QSignalBlocker blocker3(m_ui->spinBoxCol); | ||||
99 | const QSignalBlocker blocker4(m_ui->spinBoxRow); | ||||
100 | const QSignalBlocker blocker5(m_ui->ckxGlobal); | ||||
101 | const QSignalBlocker blocker6(m_ui->ckxReadOnly); | ||||
102 | const QSignalBlocker blocker7(m_ui->cbxGroup); | ||||
103 | | ||||
104 | m_ui->lineEditName->setText(m_colorSet->name()); | ||||
105 | m_ui->lineEditFilename->setText(m_paletteEditor->relativePathFromSaveLocation()); | ||||
106 | m_ui->spinBoxCol->setValue(m_colorSet->columnCount()); | ||||
107 | m_ui->ckxGlobal->setCheckState(m_colorSet->isGlobal() ? Qt::Checked : Qt::Unchecked); | ||||
108 | m_ui->ckxReadOnly->setCheckState(!m_colorSet->isEditable() ? Qt::Checked : Qt::Unchecked); | ||||
109 | | ||||
110 | Q_FOREACH (const QString & groupName, m_colorSet->getGroupNames()) { | ||||
111 | m_ui->cbxGroup->addItem(groupName); | ||||
112 | } | ||||
113 | | ||||
114 | connect(m_ui->cbxGroup, SIGNAL(currentTextChanged(QString)), SLOT(slotGroupChosen(QString))); | ||||
115 | m_ui->cbxGroup->setCurrentText(KoColorSet::GLOBAL_GROUP_NAME); | ||||
116 | m_ui->bnDelGroup->setEnabled(false); | ||||
117 | m_ui->bnRenGroup->setEnabled(false); | ||||
118 | | ||||
119 | m_ui->spinBoxRow->setValue(m_paletteEditor->rowNumberOfGroup(KoColorSet::GLOBAL_GROUP_NAME)); | ||||
120 | | ||||
121 | bool canWrite = m_colorSet->isEditable(); | ||||
122 | m_ui->lineEditName->setEnabled(canWrite); | ||||
123 | m_ui->lineEditFilename->setEnabled(canWrite); | ||||
124 | m_ui->spinBoxCol->setEnabled(canWrite); | ||||
125 | m_ui->spinBoxRow->setEnabled(canWrite); | ||||
126 | m_ui->ckxGlobal->setEnabled(canWrite); | ||||
127 | m_ui->ckxReadOnly->setEnabled(canWrite && m_colorSet->isGlobal()); | ||||
128 | m_ui->bnAddGroup->setEnabled(canWrite); | ||||
129 | } | ||||
130 | | ||||
131 | void KisDlgPaletteEditor::setView(KisViewManager *view) | ||||
132 | { | ||||
133 | m_paletteEditor->setView(view); | ||||
134 | } | ||||
135 | | ||||
136 | void KisDlgPaletteEditor::slotAddGroup() | ||||
137 | { | ||||
138 | QString newGroupName = m_paletteEditor->addGroup(); | ||||
139 | if (!newGroupName.isEmpty()) { | ||||
140 | m_ui->cbxGroup->addItem(newGroupName); | ||||
141 | m_ui->cbxGroup->setCurrentIndex(m_ui->cbxGroup->count() - 1); | ||||
142 | }; | ||||
143 | } | ||||
144 | | ||||
145 | void KisDlgPaletteEditor::slotRenGroup() | ||||
146 | { | ||||
147 | QString newName = m_paletteEditor->renameGroup(m_currentGroupOriginalName); | ||||
148 | if (!newName.isEmpty()) { | ||||
149 | int idx = m_ui->cbxGroup->currentIndex(); | ||||
150 | m_ui->cbxGroup->removeItem(idx); | ||||
151 | m_ui->cbxGroup->insertItem(idx, newName); | ||||
152 | m_ui->cbxGroup->setCurrentIndex(idx); | ||||
153 | } | ||||
154 | } | ||||
155 | | ||||
156 | void KisDlgPaletteEditor::slotDelGroup() | ||||
157 | { | ||||
158 | int deletedIdx = m_ui->cbxGroup->currentIndex(); | ||||
159 | if (m_paletteEditor->removeGroup(m_currentGroupOriginalName)) { | ||||
160 | m_ui->cbxGroup->setCurrentIndex(0); | ||||
161 | m_ui->cbxGroup->removeItem(deletedIdx); | ||||
162 | } | ||||
163 | } | ||||
164 | | ||||
165 | void KisDlgPaletteEditor::slotGroupChosen(const QString &groupName) | ||||
166 | { | ||||
167 | if (groupName == KoColorSet::GLOBAL_GROUP_NAME) { | ||||
168 | m_ui->bnDelGroup->setEnabled(false); | ||||
169 | m_ui->bnRenGroup->setEnabled(false); | ||||
170 | } else { | ||||
171 | m_ui->bnDelGroup->setEnabled(true); | ||||
172 | m_ui->bnRenGroup->setEnabled(true); | ||||
173 | } | ||||
174 | m_currentGroupOriginalName = m_paletteEditor->oldNameFromNewName(groupName); | ||||
175 | m_ui->spinBoxRow->setValue(m_paletteEditor->rowNumberOfGroup(m_currentGroupOriginalName)); | ||||
176 | } | ||||
177 | | ||||
178 | void KisDlgPaletteEditor::slotRowCountChanged(int newCount) | ||||
179 | { | ||||
180 | m_paletteEditor->changeGroupRowCount(m_currentGroupOriginalName, newCount); | ||||
181 | } | ||||
182 | | ||||
183 | void KisDlgPaletteEditor::slotSetGlobal(int state) | ||||
184 | { | ||||
185 | bool toGlobal = (state == Qt::Checked); | ||||
186 | m_paletteEditor->setGlobal(toGlobal); | ||||
187 | if (!toGlobal) { | ||||
188 | m_ui->ckxReadOnly->setCheckState(Qt::Unchecked); | ||||
189 | } | ||||
190 | m_ui->ckxReadOnly->setEnabled(toGlobal); | ||||
191 | } | ||||
192 | | ||||
193 | void KisDlgPaletteEditor::slotSetReadOnly(int state) | ||||
194 | { | ||||
195 | m_paletteEditor->setReadOnly(state == Qt::Checked); | ||||
196 | } | ||||
197 | | ||||
198 | void KisDlgPaletteEditor::slotNameChanged() | ||||
199 | { | ||||
200 | m_paletteEditor->rename(qobject_cast<QLineEdit*>(sender())->text()); | ||||
201 | } | ||||
202 | | ||||
203 | void KisDlgPaletteEditor::slotFilenameChanged(const QString &newFilename) | ||||
204 | { | ||||
205 | if (m_paletteEditor->duplicateExistsFilename(newFilename)) { | ||||
206 | m_ui->lineEditFilename->setPalette(m_warnPalette); | ||||
207 | m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); | ||||
208 | return; | ||||
209 | } | ||||
210 | m_ui->lineEditFilename->setPalette(m_normalPalette); | ||||
211 | m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true); | ||||
212 | m_paletteEditor->changeFilename(newFilename); | ||||
213 | } | ||||
214 | | ||||
215 | void KisDlgPaletteEditor::slotFilenameInputFinished() | ||||
216 | { | ||||
217 | QString newName = m_ui->lineEditFilename->text(); | ||||
218 | if (m_paletteEditor->duplicateExistsFilename(newName)) { | ||||
219 | return; | ||||
220 | } | ||||
221 | m_paletteEditor->changeFilename(newName); | ||||
222 | } | ||||
223 | | ||||
224 | void KisDlgPaletteEditor::slotColCountChanged(int newCount) | ||||
225 | { | ||||
226 | m_paletteEditor->changeColCount(newCount); | ||||
227 | } | ||||
228 | | ||||
229 | void KisDlgPaletteEditor::slotAccepted() | ||||
230 | { | ||||
231 | m_paletteEditor->updatePalette(); | ||||
232 | } |