Commit e1de0135 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Allow the user to choose between saving and importing palettes locally or into the document

BUG:410137
parent bafa8f37
...@@ -67,7 +67,7 @@ struct KisPaletteEditor::Private ...@@ -67,7 +67,7 @@ struct KisPaletteEditor::Private
QPointer<KisViewManager> view; QPointer<KisViewManager> view;
PaletteInfo modified; PaletteInfo modified;
QPointer<KoDialog> query; QPointer<KoDialog> query;
KoResourceServer<KoColorSet> *rServer; KoResourceServer<KoColorSet> *rServer {0};
QPalette normalPalette; QPalette normalPalette;
QPalette warnPalette; QPalette warnPalette;
...@@ -109,20 +109,26 @@ void KisPaletteEditor::addPalette() ...@@ -109,20 +109,26 @@ void KisPaletteEditor::addPalette()
QLabel lbl(i18nc("Label for line edit to set a palette name.","Name")); QLabel lbl(i18nc("Label for line edit to set a palette name.","Name"));
QLineEdit le(i18nc("Default name for a new palette","New Palette")); QLineEdit le(i18nc("Default name for a new palette","New Palette"));
layout.addRow(&lbl, &le); layout.addRow(&lbl, &le);
QCheckBox chkGlobal(i18n("Save Palette in the Current Document"));
chkGlobal.setChecked(false); QLabel lbl2(i18nc("Label for line edit to set a palette filename.","File Name"));
layout.addRow(&chkGlobal); 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);
if (dlg.exec() != QDialog::Accepted) { return; } if (dlg.exec() != QDialog::Accepted) { return; }
KoColorSet *newColorSet = new KoColorSet(newPaletteFileName(false)); KoColorSet *newColorSet = new KoColorSet(newPaletteFileName(!chkSaveInDocument.isChecked(), le2.text()));
newColorSet->setPaletteType(KoColorSet::KPL); newColorSet->setPaletteType(KoColorSet::KPL);
newColorSet->setIsGlobal(!chkGlobal.isChecked()); newColorSet->setIsGlobal(!chkSaveInDocument.isChecked());
newColorSet->setIsEditable(true); newColorSet->setIsEditable(true);
newColorSet->setValid(true); newColorSet->setValid(true);
newColorSet->setName(le.text()); newColorSet->setName(le.text());
m_d->rServer->addResource(newColorSet); m_d->rServer->addResource(newColorSet, !chkSaveInDocument.isChecked());
m_d->rServer->removeFromBlacklist(newColorSet); m_d->rServer->removeFromBlacklist(newColorSet);
uploadPaletteList(); uploadPaletteList();
...@@ -131,8 +137,10 @@ void KisPaletteEditor::addPalette() ...@@ -131,8 +137,10 @@ void KisPaletteEditor::addPalette()
void KisPaletteEditor::importPalette() void KisPaletteEditor::importPalette()
{ {
KoFileDialog dialog(0, KoFileDialog::OpenFile, "Open Palette"); KoFileDialog dialog(0, KoFileDialog::OpenFile, "Open Palette");
dialog.setDefaultDir(QDir::homePath()); dialog.setDefaultDir(QDir::homePath());
dialog.setMimeTypeFilters(QStringList() << "krita/x-colorset" << "application/x-gimp-color-palette"); dialog.setMimeTypeFilters(QStringList() << "krita/x-colorset" << "application/x-gimp-color-palette");
QString filename = dialog.filename(); QString filename = dialog.filename();
if (filename.isEmpty()) { return; } if (filename.isEmpty()) { return; }
if (duplicateExistsFilename(filename, false)) { if (duplicateExistsFilename(filename, false)) {
...@@ -142,16 +150,24 @@ void KisPaletteEditor::importPalette() ...@@ -142,16 +150,24 @@ void KisPaletteEditor::importPalette()
message.exec(); message.exec();
return; return;
} }
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);
KoColorSet *colorSet = new KoColorSet(filename); KoColorSet *colorSet = new KoColorSet(filename);
colorSet->load(); colorSet->load();
QString name = filenameFromPath(colorSet->filename()); QString name = filenameFromPath(colorSet->filename());
if (duplicateExistsFilename(name, false)) { if (duplicateExistsFilename(name, false)) {
colorSet->setFilename(newPaletteFileName(false)); colorSet->setFilename(newPaletteFileName(global));
} else { } else {
colorSet->setFilename(name); colorSet->setFilename(name);
} }
colorSet->setIsGlobal(false);
m_d->rServer->addResource(colorSet); colorSet->setIsGlobal(global);
m_d->rServer->addResource(colorSet, global);
m_d->rServer->removeFromBlacklist(colorSet); m_d->rServer->removeFromBlacklist(colorSet);
uploadPaletteList(); uploadPaletteList();
...@@ -604,20 +620,23 @@ void KisPaletteEditor::setNonGlobal() ...@@ -604,20 +620,23 @@ void KisPaletteEditor::setNonGlobal()
if (!m_d->view) { return; } if (!m_d->view) { return; }
if (!m_d->view->document()) { return; } if (!m_d->view->document()) { return; }
if (!m_d->model->colorSet()) { return; } if (!m_d->model->colorSet()) { return; }
KoColorSet *colorSet = m_d->model->colorSet(); KoColorSet *colorSet = m_d->model->colorSet();
QString name = filenameFromPath(colorSet->filename()); QString name = filenameFromPath(colorSet->filename());
QFile::remove(colorSet->filename()); QFile::remove(colorSet->filename());
if (duplicateExistsFilename(name, false)) { if (duplicateExistsFilename(name, false)) {
colorSet->setFilename(newPaletteFileName(false)); colorSet->setFilename(newPaletteFileName(false));
} else { } else {
colorSet->setFilename(name); colorSet->setFilename(name);
} }
colorSet->setIsGlobal(false); colorSet->setIsGlobal(false);
uploadPaletteList(); uploadPaletteList();
} }
QString KisPaletteEditor::newPaletteFileName(bool isGlobal) QString KisPaletteEditor::newPaletteFileName(bool isGlobal, const QString &filename)
{ {
QSet<QString> nameSet; QSet<QString> nameSet;
...@@ -626,7 +645,9 @@ QString KisPaletteEditor::newPaletteFileName(bool isGlobal) ...@@ -626,7 +645,9 @@ QString KisPaletteEditor::newPaletteFileName(bool isGlobal)
} }
KoColorSet tmpColorSet; KoColorSet tmpColorSet;
QString result = "new_palette_"; QString result = (filename.isEmpty() ? "new_palette" : filename);
qDebug() << "savelocation" << m_d->rServer->saveLocation();
if (isGlobal) { if (isGlobal) {
result = m_d->rServer->saveLocation() + result; result = m_d->rServer->saveLocation() + result;
...@@ -636,7 +657,7 @@ QString KisPaletteEditor::newPaletteFileName(bool isGlobal) ...@@ -636,7 +657,7 @@ QString KisPaletteEditor::newPaletteFileName(bool isGlobal)
while (nameSet.contains(result + QString::number(i) + tmpColorSet.defaultFileExtension())) { while (nameSet.contains(result + QString::number(i) + tmpColorSet.defaultFileExtension())) {
i++; i++;
} }
result = result + QString::number(i) + tmpColorSet.defaultFileExtension(); result = result + (i > 0 ? QString::number(i) : "") + tmpColorSet.defaultFileExtension();
return result; return result;
} }
......
...@@ -129,7 +129,7 @@ private Q_SLOTS: ...@@ -129,7 +129,7 @@ private Q_SLOTS:
void slotSetDocumentModified(); void slotSetDocumentModified();
private: private:
QString newPaletteFileName(bool isGlobal); QString newPaletteFileName(bool isGlobal, const QString &filename = QString());
QString newGroupName() const; QString newGroupName() const;
void setNonGlobal(); void setNonGlobal();
void setGlobal(); void setGlobal();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment