KisPaletteEditor.cpp 22.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 *  Copyright (c) 2018 Michael Zhou <simeirxh@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <QHash>
#include <QString>
#include <QScopedPointer>
#include <QPointer>
#include <QFormLayout>
#include <QCheckBox>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>
#include <QComboBox>
#include <QMessageBox>

#include <KoDialog.h>
#include <KoFileDialog.h>
#include <KoColorSet.h>
#include <KisSwatchGroup.h>
#include <kis_signal_compressor.h>
#include <KisViewManager.h>
#include <KisDocument.h>
#include <KoResourceServer.h>
#include <KoResourceServerProvider.h>
#include <KisPaletteModel.h>
#include <kis_color_button.h>

#include "KisPaletteEditor.h"

struct KisPaletteEditor::PaletteInfo {
    QString name;
    QString filename;
    int columnCount;
    bool isGlobal;
    bool isReadOnly;
    QHash<QString, KisSwatchGroup> groups;
};

struct KisPaletteEditor::Private
{
57 58 59 60
    bool isGlobalModified {false};
    bool isNameModified {false};
    bool isFilenameModified {false};
    bool isColumnCountModified {false};
61 62 63
    QSet<QString> modifiedGroupNames; // key is original group name
    QSet<QString> newGroupNames;
    QSet<QString> keepColorGroups;
64
    QSet<QString> pathsToRemove;
65 66 67 68 69
    QString groupBeingRenamed;
    QPointer<KisPaletteModel> model;
    QPointer<KisViewManager> view;
    PaletteInfo modified;
    QPointer<KoDialog> query;
70
    KoResourceServer<KoColorSet> *rServer {0};
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    QPalette normalPalette;
    QPalette warnPalette;
};

KisPaletteEditor::KisPaletteEditor(QObject *parent)
    : QObject(parent)
    , m_d(new Private)
{
    m_d->rServer = KoResourceServerProvider::instance()->paletteServer();
    m_d->warnPalette.setColor(QPalette::Text, Qt::red);
}

KisPaletteEditor::~KisPaletteEditor()
{ }

void KisPaletteEditor::setPaletteModel(KisPaletteModel *model)
{
    if (!model) { return; }
    m_d->model = model;
    slotPaletteChanged();
92
    connect(model, SIGNAL(sigPaletteChanged()), SLOT(slotPaletteChanged()));
93
    connect(model, SIGNAL(sigPaletteModified()), SLOT(slotSetDocumentModified()));
94 95 96 97 98 99 100 101 102 103 104
}

void KisPaletteEditor::setView(KisViewManager *view)
{
    m_d->view = view;
}

void KisPaletteEditor::addPalette()
{
    if (!m_d->view) { return; }
    if (!m_d->view->document()) { return; }
105

Michael Zhou's avatar
Michael Zhou committed
106 107 108
    KoDialog dlg;
    QFormLayout layout;
    dlg.mainWidget()->setLayout(&layout);
109 110
    QLabel lbl(i18nc("Label for line edit to set a palette name.","Name"));
    QLineEdit le(i18nc("Default name for a new palette","New Palette"));
Michael Zhou's avatar
Michael Zhou committed
111
    layout.addRow(&lbl, &le);
112 113 114 115 116 117 118 119 120

    QLabel lbl2(i18nc("Label for line edit to set a palette filename.","File Name"));
    QLineEdit le2(i18nc("Default file name for a new palette", "New Palette"));
    layout.addRow(&lbl2, &le2);


    QCheckBox chkSaveInDocument(i18n("Save Palette in the Current Document"));
    chkSaveInDocument.setChecked(false);
    layout.addRow(&chkSaveInDocument);
121

Michael Zhou's avatar
Michael Zhou committed
122
    if (dlg.exec() != QDialog::Accepted) { return; }
123

124
    KoColorSet *newColorSet = new KoColorSet(newPaletteFileName(!chkSaveInDocument.isChecked(), le2.text()));
125
    newColorSet->setPaletteType(KoColorSet::KPL);
126
    newColorSet->setIsGlobal(!chkSaveInDocument.isChecked());
127 128
    newColorSet->setIsEditable(true);
    newColorSet->setValid(true);
Michael Zhou's avatar
Michael Zhou committed
129
    newColorSet->setName(le.text());
130

131
    m_d->rServer->addResource(newColorSet, !chkSaveInDocument.isChecked());
132
    m_d->rServer->removeFromBlacklist(newColorSet);
133 134 135 136 137 138

    uploadPaletteList();
}

void KisPaletteEditor::importPalette()
{
139
    KoFileDialog dialog(0, KoFileDialog::OpenFile, "Open Palette");
140

141 142
    dialog.setDefaultDir(QDir::homePath());
    dialog.setMimeTypeFilters(QStringList() << "krita/x-colorset" << "application/x-gimp-color-palette");
143

144 145
    QString filename = dialog.filename();
    if (filename.isEmpty()) { return; }
146
    if (duplicateExistsFilename(filename, false)) {
147 148 149 150 151 152
        QMessageBox message;
        message.setWindowTitle(i18n("Can't Import Palette"));
        message.setText(i18n("Can't import palette: there's already imported with the same filename"));
        message.exec();
        return;
    }
153 154 155 156 157 158

    QMessageBox messageBox;
    messageBox.setText(i18n("Do you want to store this palette in your current image?"));
    messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    bool global = (messageBox.exec() == QMessageBox::Yes);

159 160
    KoColorSet *colorSet = new KoColorSet(filename);
    colorSet->load();
161
    QString name = filenameFromPath(colorSet->filename());
162

163
    if (duplicateExistsFilename(name, false)) {
164
        colorSet->setFilename(newPaletteFileName(global));
165 166 167
    } else {
        colorSet->setFilename(name);
    }
168 169 170

    colorSet->setIsGlobal(global);
    m_d->rServer->addResource(colorSet, global);
171
    m_d->rServer->removeFromBlacklist(colorSet);
172 173 174 175 176 177 178 179 180 181 182 183

    uploadPaletteList();
}

void KisPaletteEditor::removePalette(KoColorSet *cs)
{
    if (!m_d->view) { return; }
    if (!m_d->view->document()) { return; }
    if (!cs || !cs->isEditable()) {
        return;
    }

184
    if (cs->isGlobal()) {
185
        m_d->rServer->removeResourceAndBlacklist(cs);
186 187 188
        QFile::remove(cs->filename());
        return;
    }
189
    m_d->rServer->removeResourceFromServer(cs);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    uploadPaletteList();
}

int KisPaletteEditor::rowNumberOfGroup(const QString &oriName) const
{
    if (!m_d->modified.groups.contains(oriName)) { return 0; }
    return m_d->modified.groups[oriName].rowCount();
}

bool KisPaletteEditor::duplicateExistsGroupName(const QString &name) const
{
    if (name == m_d->groupBeingRenamed) { return false; }
    Q_FOREACH (const KisSwatchGroup &g, m_d->modified.groups.values()) {
        if (name == g.name()) { return true; }
    }
    return false;
}

208 209 210 211 212
bool KisPaletteEditor::duplicateExistsOriginalGroupName(const QString &name) const
{
    return m_d->modified.groups.contains(name);
}

213 214 215 216 217 218 219 220 221 222 223 224
QString KisPaletteEditor::oldNameFromNewName(const QString &newName) const
{
    Q_FOREACH (const QString &oldGroupName, m_d->modified.groups.keys()) {
        if (m_d->modified.groups[oldGroupName].name() == newName) {
            return oldGroupName;
        }
    }
    return QString();
}

void KisPaletteEditor::rename(const QString &newName)
{
225
    if (newName.isEmpty()) { return; }
226 227 228 229 230 231
    m_d->isNameModified = true;
    m_d->modified.name = newName;
}

void KisPaletteEditor::changeFilename(const QString &newName)
{
232
    if (newName.isEmpty()) { return; }
233
    m_d->isFilenameModified = true;
234
    m_d->pathsToRemove.insert(m_d->modified.filename);
235 236 237 238 239
    if (m_d->modified.isGlobal) {
        m_d->modified.filename = m_d->rServer->saveLocation() + newName;
    } else {
        m_d->modified.filename = newName;
    }
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
}

void KisPaletteEditor::changeColCount(int newCount)
{
    m_d->isColumnCountModified = true;
    m_d->modified.columnCount = newCount;
}

QString KisPaletteEditor::addGroup()
{
    KoDialog dlg;
    m_d->query = &dlg;

    QVBoxLayout layout(&dlg);
    dlg.mainWidget()->setLayout(&layout);

    QLabel lblName(i18n("Name"), &dlg);
    layout.addWidget(&lblName);
    QLineEdit leName(&dlg);
259
    leName.setText(newGroupName());
260 261 262 263 264 265 266 267 268 269 270
    connect(&leName, SIGNAL(textChanged(QString)), SLOT(slotGroupNameChanged(QString)));
    layout.addWidget(&leName);
    QLabel lblRowCount(i18n("Row count"), &dlg);
    layout.addWidget(&lblRowCount);
    QSpinBox spxRow(&dlg);
    spxRow.setValue(20);
    layout.addWidget(&spxRow);

    if (dlg.exec() != QDialog::Accepted) { return QString(); }
    if (duplicateExistsGroupName(leName.text())) { return QString(); }

271 272 273 274 275
    QString realName = leName.text();
    QString name = realName;
    if (duplicateExistsOriginalGroupName(name)) {
        name = newGroupName();
    }
276 277
    m_d->modified.groups[name] = KisSwatchGroup();
    KisSwatchGroup &newGroup = m_d->modified.groups[name];
278 279
    newGroup.setName(realName);
    m_d->newGroupNames.insert(name);
280
    newGroup.setRowCount(spxRow.value());
281
    return realName;
282 283 284 285 286 287 288 289 290 291 292 293 294
}

bool KisPaletteEditor::removeGroup(const QString &name)
{
    KoDialog window;
    window.setWindowTitle(i18nc("@title:window", "Removing Group"));
    QFormLayout editableItems(&window);
    QCheckBox chkKeep(&window);
    window.mainWidget()->setLayout(&editableItems);
    editableItems.addRow(i18nc("Shows up when deleting a swatch group", "Keep the Colors"), &chkKeep);
    if (window.exec() != KoDialog::Accepted) { return false; }

    m_d->modified.groups.remove(name);
295
    m_d->newGroupNames.remove(name);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    if (chkKeep.isChecked()) {
        m_d->keepColorGroups.insert(name);
    }
    return true;
}

QString KisPaletteEditor::renameGroup(const QString &oldName)
{
    if (oldName.isEmpty() || oldName == KoColorSet::GLOBAL_GROUP_NAME) { return QString(); }

    KoDialog dlg;
    m_d->query = &dlg;
    m_d->groupBeingRenamed = m_d->modified.groups[oldName].name();

    QFormLayout form(&dlg);
    dlg.mainWidget()->setLayout(&form);

    QLineEdit leNewName;
    connect(&leNewName, SIGNAL(textChanged(QString)), SLOT(slotGroupNameChanged(QString)));
    leNewName.setText(m_d->modified.groups[oldName].name());

    form.addRow(i18nc("Renaming swatch group", "New name"), &leNewName);

    if (dlg.exec() != KoDialog::Accepted) { return QString(); }
    if (leNewName.text().isEmpty()) { return QString(); }
    if (duplicateExistsGroupName(leNewName.text())) { return QString(); }

    m_d->modified.groups[oldName].setName(leNewName.text());
    m_d->modifiedGroupNames.insert(oldName);
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    return leNewName.text();
}

void KisPaletteEditor::slotGroupNameChanged(const QString &newName)
{
    QLineEdit *leGroupName = qobject_cast<QLineEdit*>(sender());
    if (duplicateExistsGroupName(newName) || newName == QString()) {
        leGroupName->setPalette(m_d->warnPalette);
        if (m_d->query->button(KoDialog::Ok)) {
            m_d->query->button(KoDialog::Ok)->setEnabled(false);
        }
        return;
    }
    leGroupName->setPalette(m_d->normalPalette);
    if (m_d->query->button(KoDialog::Ok)) {
        m_d->query->button(KoDialog::Ok)->setEnabled(true);
    }
}

void KisPaletteEditor::changeGroupRowCount(const QString &name, int newRowCount)
{
    if (!m_d->modified.groups.contains(name)) { return; }
    m_d->modified.groups[name].setRowCount(newRowCount);
    m_d->modifiedGroupNames.insert(name);
}

void KisPaletteEditor::setGlobal(bool isGlobal)
{
    m_d->isGlobalModified = true;
    m_d->modified.isGlobal = isGlobal;
}

void KisPaletteEditor::setEntry(const KoColor &color, const QModelIndex &index)
{
    Q_ASSERT(m_d->model);
    if (!m_d->model->colorSet()->isEditable()) { return; }
    if (!m_d->view) { return; }
    if (!m_d->view->document()) { return; }
364 365 366 367
    KisSwatch c = KisSwatch(color);
    c.setId(QString::number(m_d->model->colorSet()->colorCount() + 1));
    c.setName(i18nc("Default name for a color swatch","Color %1", QString::number(m_d->model->colorSet()->colorCount()+1)));
    m_d->model->setEntry(c, index);
368 369
}

370
void KisPaletteEditor::slotSetDocumentModified()
371
{
372
    m_d->view->document()->setModified(true);
373 374 375 376 377 378 379 380 381 382 383 384
}

void KisPaletteEditor::removeEntry(const QModelIndex &index)
{
    Q_ASSERT(m_d->model);
    if (!m_d->model->colorSet()->isEditable()) { return; }
    if (!m_d->view) { return; }
    if (!m_d->view->document()) { return; }
    if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) {
        removeGroup(qvariant_cast<QString>(index.data(KisPaletteModel::GroupNameRole)));
        updatePalette();
    } else {
385
        m_d->model->removeEntry(index, false);
386 387 388 389 390 391 392 393 394 395 396 397 398 399
    }
    if (m_d->model->colorSet()->isGlobal()) {
        m_d->model->colorSet()->save();
        return;
    }
}

void KisPaletteEditor::modifyEntry(const QModelIndex &index)
{
    if (!m_d->model->colorSet()->isEditable()) { return; }
    if (!m_d->view) { return; }
    if (!m_d->view->document()) { return; }

    KoDialog dlg;
400
    dlg.setCaption(i18nc("@title:window", "Add a Color"));
401 402 403 404 405 406 407
    QFormLayout *editableItems = new QFormLayout(&dlg);
    dlg.mainWidget()->setLayout(editableItems);

    QString groupName = qvariant_cast<QString>(index.data(Qt::DisplayRole));
    if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) {
        renameGroup(groupName);
        updatePalette();
408 409 410
    }
    else {

411
        QLineEdit *lnIDName = new QLineEdit(&dlg);
412
        QLineEdit *lnGroupName = new QLineEdit(&dlg);
413 414 415
        KisColorButton *bnColor = new KisColorButton(&dlg);
        QCheckBox *chkSpot = new QCheckBox(&dlg);
        chkSpot->setToolTip(i18nc("@info:tooltip", "A spot color is a color that the printer is able to print without mixing the paints it has available to it. The opposite is called a process color."));
416 417 418

        KisSwatch entry = m_d->model->getEntry(index);

419
        editableItems->addRow(i18n("ID"), lnIDName);
420
        editableItems->addRow(i18nc("Name for a swatch group", "Swatch group name"), lnGroupName);
421
        editableItems->addRow(i18n("Color"), bnColor);
422
        editableItems->addRow(i18n("Spot color"), chkSpot);
423

424 425 426 427
        lnGroupName->setText(entry.name());
        lnIDName->setText(entry.id());
        bnColor->setColor(entry.color());
        chkSpot->setChecked(entry.spotColor());
428

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
        if (dlg.exec() == KoDialog::Accepted) {
            entry.setName(lnGroupName->text());
            entry.setId(lnIDName->text());
            entry.setColor(bnColor->color());
            entry.setSpotColor(chkSpot->isChecked());
            m_d->model->setEntry(entry, index);
        }
    }
}

void KisPaletteEditor::addEntry(const KoColor &color)
{
    Q_ASSERT(m_d->model);
    if (!m_d->view) { return; }
    if (!m_d->view->document()) { return; }
    if (!m_d->model->colorSet()->isEditable()) { return; }
Michael Zhou's avatar
Michael Zhou committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    KoDialog window;
    window.setWindowTitle(i18nc("@title:window", "Add a new Colorset Entry"));
    QFormLayout editableItems(&window);
    window.mainWidget()->setLayout(&editableItems);
    QComboBox cmbGroups(&window);
    cmbGroups.addItems(m_d->model->colorSet()->getGroupNames());
    QLineEdit lnIDName(&window);
    QLineEdit lnName(&window);
    KisColorButton bnColor(&window);
    QCheckBox chkSpot(&window);
    chkSpot.setToolTip(i18nc("@info:tooltip", "A spot color is a color that the printer is able to print without mixing the paints it has available to it. The opposite is called a process color."));
    editableItems.addRow(i18n("Group"), &cmbGroups);
    editableItems.addRow(i18n("ID"), &lnIDName);
    editableItems.addRow(i18n("Name"), &lnName);
    editableItems.addRow(i18n("Color"), &bnColor);
    editableItems.addRow(i18nc("Spot color", "Spot"), &chkSpot);
    cmbGroups.setCurrentIndex(0);
462
    lnName.setText(i18nc("Default name for a color swatch","Color %1", QString::number(m_d->model->colorSet()->colorCount()+1)));
Michael Zhou's avatar
Michael Zhou committed
463 464 465
    lnIDName.setText(QString::number(m_d->model->colorSet()->colorCount() + 1));
    bnColor.setColor(color);
    chkSpot.setChecked(false);
466

Michael Zhou's avatar
Michael Zhou committed
467
    if (window.exec() != KoDialog::Accepted) { return; }
468

Michael Zhou's avatar
Michael Zhou committed
469
    QString groupName = cmbGroups.currentText();
470 471

    KisSwatch newEntry;
Michael Zhou's avatar
Michael Zhou committed
472 473 474 475
    newEntry.setColor(bnColor.color());
    newEntry.setName(lnName.text());
    newEntry.setId(lnIDName.text());
    newEntry.setSpotColor(chkSpot.isChecked());
476 477 478 479 480 481 482 483 484 485 486 487 488
    m_d->model->addEntry(newEntry, groupName);

    if (m_d->model->colorSet()->isGlobal()) {
        m_d->model->colorSet()->save();
        return;
    }
    m_d->modifiedGroupNames.insert(groupName);
    m_d->modified.groups[groupName].addEntry(newEntry);
}

void KisPaletteEditor::updatePalette()
{
    Q_ASSERT(m_d->model);
489 490
    Q_ASSERT(m_d->model->colorSet());
    if (!m_d->model->colorSet()->isEditable()) { return; }
491 492 493 494 495
    if (!m_d->view) { return; }
    if (!m_d->view->document()) { return; }
    KoColorSet *palette = m_d->model->colorSet();
    PaletteInfo &modified = m_d->modified;

496 497 498 499 500 501 502
    if (m_d->isColumnCountModified) {
        palette->setColumnCount(modified.columnCount);
    }
    if (m_d->isNameModified) {
        palette->setName(modified.name);
    }
    if (m_d->isFilenameModified) {
503
        QString originalPath = palette->filename();
504 505 506
        palette->setFilename(modified.filename);
        if (palette->isGlobal()) {
            if (!palette->save()) {
507
                palette->setFilename(newPaletteFileName(true));
508 509
                palette->save();
            }
510
            QFile::remove(originalPath);
511 512
        }
    }
513 514 515 516 517 518 519 520 521 522 523 524 525
    if (m_d->isGlobalModified) {
        palette->setIsGlobal(modified.isGlobal);
        if (modified.isGlobal) {
            setGlobal();
        } else {
            setNonGlobal();
        }
    }
    Q_FOREACH (const QString &groupName, palette->getGroupNames()) {
        if (!modified.groups.contains(groupName)) {
            m_d->model->removeGroup(groupName, m_d->keepColorGroups.contains(groupName));
        }
    }
526
    m_d->keepColorGroups.clear();
527 528 529
    Q_FOREACH (const QString &groupName, palette->getGroupNames()) {
        if (m_d->modifiedGroupNames.contains(groupName)) {
            m_d->model->setRowNumber(groupName, modified.groups[groupName].rowCount());
530 531 532 533 534
            if (groupName != modified.groups[groupName].name()) {
                m_d->model->renameGroup(groupName, modified.groups[groupName].name());
                modified.groups[modified.groups[groupName].name()] = modified.groups[groupName];
                modified.groups.remove(groupName);
            }
535 536
        }
    }
537
    m_d->modifiedGroupNames.clear();
538 539 540
    Q_FOREACH (const QString &newGroupName, m_d->newGroupNames) {
        m_d->model->addGroup(modified.groups[newGroupName]);
    }
541
    m_d->newGroupNames.clear();
542

543 544 545
    if (m_d->model->colorSet()->isGlobal()) {
        m_d->model->colorSet()->save();
    }
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
}

void KisPaletteEditor::slotPaletteChanged()
{
    Q_ASSERT(m_d->model);
    if (!m_d->model->colorSet()) { return; }
    KoColorSet *palette = m_d->model->colorSet();
    m_d->modified.groups.clear();
    m_d->keepColorGroups.clear();
    m_d->newGroupNames.clear();
    m_d->modifiedGroupNames.clear();

    m_d->modified.name = palette->name();
    m_d->modified.filename = palette->filename();
    m_d->modified.columnCount = palette->columnCount();
    m_d->modified.isGlobal = palette->isGlobal();
    m_d->modified.isReadOnly = !palette->isEditable();

    Q_FOREACH (const QString &groupName, palette->getGroupNames()) {
        KisSwatchGroup *cs = palette->getGroup(groupName);
        m_d->modified.groups[groupName] = KisSwatchGroup(*cs);
    }
}

void KisPaletteEditor::setGlobal()
{
    Q_ASSERT(m_d->model);
    if (!m_d->view) { return; }
    if (!m_d->view->document()) { return; }
    if (!m_d->model->colorSet()) { return; }

    KoColorSet *colorSet = m_d->model->colorSet();
    QString saveLocation = m_d->rServer->saveLocation();
579
    QString name = filenameFromPath(colorSet->filename());
580 581 582 583 584

    QFileInfo fileInfo(saveLocation + name);

    colorSet->setFilename(fileInfo.filePath());
    colorSet->setIsGlobal(true);
585
    m_d->rServer->removeFromBlacklist(colorSet);
586 587 588 589 590 591
    if (!colorSet->save()) {
        QMessageBox message;
        message.setWindowTitle(i18n("Saving palette failed"));
        message.setText(i18n("Failed to save global palette file. Please set it to non-global, or you will lose the file when you close Krita"));
        message.exec();
    }
592 593 594 595

    uploadPaletteList();
}

596
bool KisPaletteEditor::duplicateExistsFilename(const QString &filename, bool global) const
597
{
598 599 600
    QString prefix;
    if (global) {
        prefix = m_d->rServer->saveLocation();
601 602
    }

603 604 605
    Q_FOREACH (const KoResource *r, KoResourceServerProvider::instance()->paletteServer()->resources()) {
        if (r->filename() == prefix + filename && r != m_d->model->colorSet()) {
            return true;
606 607
        }
    }
608

609 610 611
    return false;
}

612 613 614 615 616
QString KisPaletteEditor::relativePathFromSaveLocation() const
{
    return filenameFromPath(m_d->modified.filename);
}

617 618 619 620 621 622
void KisPaletteEditor::setNonGlobal()
{
    Q_ASSERT(m_d->model);
    if (!m_d->view) { return; }
    if (!m_d->view->document()) { return; }
    if (!m_d->model->colorSet()) { return; }
623

624
    KoColorSet *colorSet = m_d->model->colorSet();
625
    QString name = filenameFromPath(colorSet->filename());
626
    QFile::remove(colorSet->filename());
627

628
    if (duplicateExistsFilename(name, false)) {
629
        colorSet->setFilename(newPaletteFileName(false));
630 631 632
    } else {
        colorSet->setFilename(name);
    }
633

634 635 636 637 638
    colorSet->setIsGlobal(false);

    uploadPaletteList();
}

639
QString KisPaletteEditor::newPaletteFileName(bool isGlobal, const QString &filename)
640 641 642 643 644 645 646 647
{
    QSet<QString> nameSet;

    Q_FOREACH (const KoResource *r, m_d->rServer->resources()) {
        nameSet.insert(r->filename());
    }

    KoColorSet tmpColorSet;
648 649 650
    QString result = (filename.isEmpty() ? "new_palette" : filename);

    qDebug() << "savelocation" << m_d->rServer->saveLocation();
651

652 653 654 655
    if (isGlobal) {
        result = m_d->rServer->saveLocation() + result;
    }

656 657 658 659
    int i = 0;
    while (nameSet.contains(result + QString::number(i) + tmpColorSet.defaultFileExtension())) {
        i++;
    }
660
    result = result + (i > 0 ? QString::number(i) : "") + tmpColorSet.defaultFileExtension();
661 662 663
    return result;
}

664 665
QString KisPaletteEditor::newGroupName() const
{
666 667 668
    int i = 1;
    QString groupname = i18nc("Default new group name", "New Group %1", QString::number(i));
    while (m_d->modified.groups.contains(groupname)) {
669
        i++;
670
        groupname = i18nc("Default new group name", "New Group %1", QString::number(i));
671
    }
672
    return groupname;
673 674
}

675 676 677 678 679 680 681 682 683 684 685 686
void KisPaletteEditor::uploadPaletteList() const
{
    QList<KoColorSet *> list;
    Q_FOREACH (KoResource * paletteResource, m_d->rServer->resources()) {
        KoColorSet *palette = static_cast<KoColorSet*>(paletteResource);
        Q_ASSERT(palette);
        if (!palette->isGlobal()) {
            list.append(palette);
        }
    }
    m_d->view->document()->setPaletteList(list);
}
687

688
QString KisPaletteEditor::filenameFromPath(const QString &path) const
689 690 691
{
    return QDir::fromNativeSeparators(path).section('/', -1, -1);
}