Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Revise the color palette code

Mathias Weinc541c7380e04
diff --git a/libs/pigment/resources/KoColorSet.cpp b/libs/pigment/resources/KoColorSet.cpp
index 78d8a65522..f86b554993 100644
--- a/libs/pigment/resources/KoColorSet.cpp
+++ b/libs/pigment/resources/KoColorSet.cpp
@@ -129,6 +129,7 @@ struct AddSwatchCommand : public KUndo2Command
else {
modifiedGroup->setSwatch(m_swatch, m_x, m_y);
}
+ m_colorSet->notifySwatchChanged(m_groupName, m_x, m_y);
}
/// revert the actions done in redo
@@ -136,6 +137,7 @@ struct AddSwatchCommand : public KUndo2Command
{
KisSwatchGroupSP modifiedGroup = m_colorSet->getGroup(m_groupName);
modifiedGroup->removeSwatch(m_x, m_y);
+ m_colorSet->notifySwatchChanged(m_groupName, m_x, m_y);
}
private:
@@ -163,12 +165,14 @@ struct RemoveSwatchCommand : public KUndo2Command
void redo() override
{
m_group->removeSwatch(m_x, m_y);
+ m_colorSet->notifySwatchChanged(m_group->name(), m_x, m_y);
}
/// revert the actions done in redo
void undo() override
{
m_group->setSwatch(m_swatch, m_x, m_y);
+ m_colorSet->notifySwatchChanged(m_group->name(), m_x, m_y);
}
private:
@@ -196,6 +200,7 @@ struct ChangeGroupNameCommand : public KUndo2Command
{
KisSwatchGroupSP group = m_colorSet->getGroup(m_oldGroupName);
group->setName(m_newGroupName);
+ Q_EMIT m_colorSet->entryChanged(0, m_colorSet->startRowForGroup(m_newGroupName));
}
/// revert the actions done in redo
@@ -203,6 +208,7 @@ struct ChangeGroupNameCommand : public KUndo2Command
{
KisSwatchGroupSP group = m_colorSet->getGroup(m_newGroupName);
group->setName(m_oldGroupName);
+ Q_EMIT m_colorSet->entryChanged(0, m_colorSet->startRowForGroup(m_oldGroupName));
}
private:
@@ -240,8 +246,10 @@ struct MoveGroupCommand : public KUndo2Command
if (m_groupNameInsertBefore != KoColorSet::GLOBAL_GROUP_NAME &&
m_groupName != KoColorSet::GLOBAL_GROUP_NAME)
{
+ Q_EMIT m_colorSet->layoutAboutToChange();
KisSwatchGroupSP group = m_colorSet->d->swatchGroups.takeAt(m_oldIndex);
m_colorSet->d->swatchGroups.insert(m_newIndex, group);
+ Q_EMIT m_colorSet->layoutChanged();
}
}
@@ -249,8 +257,10 @@ struct MoveGroupCommand : public KUndo2Command
/// revert the actions done in redo
void undo() override
{
+ Q_EMIT m_colorSet->layoutAboutToChange();
KisSwatchGroupSP group = m_colorSet->d->swatchGroups.takeAt(m_newIndex);
m_colorSet->d->swatchGroups.insert(m_oldIndex, group);
+ Q_EMIT m_colorSet->layoutChanged();
}
private:
@@ -279,7 +289,9 @@ struct AddGroupCommand : public KUndo2Command
group->setName(m_groupName);
group->setColumnCount(m_columnCount);
group->setRowCount(m_rowCount);
+ Q_EMIT m_colorSet->layoutAboutToChange();
m_colorSet->d->swatchGroups.append(group);
+ Q_EMIT m_colorSet->layoutChanged();
}
@@ -296,7 +308,9 @@ struct AddGroupCommand : public KUndo2Command
idx++;
}
if (found) {
+ Q_EMIT m_colorSet->layoutAboutToChange();
m_colorSet->d->swatchGroups.takeAt(idx);
+ Q_EMIT m_colorSet->layoutChanged();
}
}
@@ -336,12 +350,15 @@ struct RemoveGroupCommand : public KUndo2Command
}
}
+ Q_EMIT m_colorSet->layoutAboutToChange();
m_colorSet->d->swatchGroups.removeOne(m_oldGroup);
+ Q_EMIT m_colorSet->layoutChanged();
}
/// revert the actions done in redo
void undo() override
{
+ Q_EMIT m_colorSet->layoutAboutToChange();
m_colorSet->d->swatchGroups.insert(m_groupIndex, m_oldGroup);
// remove all colors that were inserted into global
@@ -353,6 +370,7 @@ struct RemoveGroupCommand : public KUndo2Command
info.row + m_startingRow);
}
}
+ Q_EMIT m_colorSet->layoutChanged();
}
private:
@@ -384,15 +402,19 @@ struct ClearCommand : public KUndo2Command
m_colorSet->d->swatchGroups.clear();
KisSwatchGroupSP global(new KisSwatchGroup);
global->setName(KoColorSet::GLOBAL_GROUP_NAME);
+ Q_EMIT m_colorSet->layoutAboutToChange();
m_colorSet->d->swatchGroups.append(global);
+ Q_EMIT m_colorSet->layoutChanged();
}
/// revert the actions done in redo
void undo() override
{
+ Q_EMIT m_colorSet->layoutAboutToChange();
m_colorSet->d->swatchGroups = m_OldColorSet->d->swatchGroups;
KUndo2Command::undo();
+ Q_EMIT m_colorSet->layoutChanged();
}
private:
@@ -412,20 +434,24 @@ struct SetColumnCountCommand : public KUndo2Command
/// redo the command
void redo() override
{
+ Q_EMIT m_colorSet->layoutAboutToChange();
for (KisSwatchGroupSP &group : m_colorSet->d->swatchGroups) {
group->setColumnCount(m_columnsCount);
}
m_colorSet->d->columns = m_columnsCount;
+ Q_EMIT m_colorSet->layoutChanged();
}
/// revert the actions done in redo
void undo() override
{
+ Q_EMIT m_colorSet->layoutAboutToChange();
for (KisSwatchGroupSP &group : m_colorSet->d->swatchGroups) {
group->setColumnCount(m_oldColumnsCount);
}
m_colorSet->d->columns = m_oldColumnsCount;
+ Q_EMIT m_colorSet->layoutChanged();
}
private:
@@ -746,6 +772,15 @@ void KoColorSet::setModified(bool _modified)
}
}
+void KoColorSet::notifySwatchChanged(const QString &groupName, int column, int row)
+{
+ int startRow = 0;
+ if (!groupName.isEmpty()) {
+ startRow = startRowForGroup(groupName) + 1;
+ }
+ Q_EMIT entryChanged(column, startRow + row);
+}
+
void KoColorSet::canUndoChanged(bool canUndo)
{
diff --git a/libs/pigment/resources/KoColorSet.h b/libs/pigment/resources/KoColorSet.h
index eb8ca8e073..d7fb0b39f5 100644
--- a/libs/pigment/resources/KoColorSet.h
+++ b/libs/pigment/resources/KoColorSet.h
@@ -254,6 +254,9 @@ public:
Q_SIGNALS:
void modified();
+ void layoutAboutToChange();
+ void layoutChanged();
+ void entryChanged(int column, int row);
private Q_SLOTS:
@@ -265,6 +268,7 @@ private Q_SLOTS:
private:
void setModified(bool);
+ void notifySwatchChanged(const QString& groupName, int column, int row);
friend struct AddSwatchCommand;
friend struct RemoveSwatchCommand;
diff --git a/libs/ui/KisPaletteEditor.cpp b/libs/ui/KisPaletteEditor.cpp
index 97ee281f66..b6be11e24a 100644
--- a/libs/ui/KisPaletteEditor.cpp
+++ b/libs/ui/KisPaletteEditor.cpp
@@ -43,8 +43,7 @@ struct KisPaletteEditor::PaletteInfo {
struct KisPaletteEditor::Private
{
- bool isNameModified {false};
- bool isColumnCountModified {false};
+ bool isEditing {false};
QSet<QString> modifiedGroupNames; // key is original group name
QSet<QString> newGroupNames;
QSet<QString> keepColorGroups;
@@ -75,6 +74,9 @@ KisPaletteEditor::~KisPaletteEditor()
void KisPaletteEditor::setPaletteModel(KisPaletteModel *model)
{
if (!model) { return; }
+ if (m_d->model) {
+ m_d->model->disconnect(this);
+ }
m_d->model = model;
slotPaletteChanged();
connect(model, SIGNAL(sigPaletteChanged()), SLOT(slotPaletteChanged()));
@@ -204,19 +206,26 @@ QString KisPaletteEditor::oldNameFromNewName(const QString &newName) const
void KisPaletteEditor::rename(const QString &newName)
{
+ if (!m_d->isEditing) {
+ return;
+ }
if (newName.isEmpty()) { return; }
- m_d->isNameModified = true;
m_d->modifiedPaletteInfo.name = newName;
}
void KisPaletteEditor::changeColumnCount(int newCount)
{
- m_d->isColumnCountModified = true;
+ if (!m_d->isEditing) {
+ return;
+ }
m_d->modifiedPaletteInfo.columnCount = newCount;
}
QString KisPaletteEditor::addGroup()
{
+ if (!m_d->isEditing) {
+ return QString();
+ }
KoDialog dialog;
m_d->query = &dialog;
@@ -249,6 +258,9 @@ QString KisPaletteEditor::addGroup()
bool KisPaletteEditor::removeGroup(const QString &name)
{
+ if (!m_d->isEditing) {
+ return false;
+ }
KoDialog dialog;
dialog.setWindowTitle(i18nc("@title:dialog", "Removing Swatch Group"));
QFormLayout *editableItems = new QFormLayout(dialog.mainWidget());
@@ -267,6 +279,9 @@ bool KisPaletteEditor::removeGroup(const QString &name)
QString KisPaletteEditor::renameGroup(const QString &oldName)
{
+ if (!m_d->isEditing) {
+ return QString();
+ }
if (oldName.isEmpty() || oldName == KoColorSet::GLOBAL_GROUP_NAME) { return QString(); }
KoDialog dialog;
@@ -309,6 +324,9 @@ void KisPaletteEditor::slotGroupNameChanged(const QString &newName)
void KisPaletteEditor::changeGroupRowCount(const QString &name, int newRowCount)
{
+ if (!m_d->isEditing) {
+ return;
+ }
if (!m_d->modifiedPaletteInfo.groups.contains(name)) { return; }
m_d->modifiedPaletteInfo.groups[name]->setRowCount(newRowCount);
m_d->modifiedGroupNames.insert(name);
@@ -333,7 +351,6 @@ void KisPaletteEditor::setEntry(const KoColor &color, const QModelIndex &index)
void KisPaletteEditor::slotSetDocumentModified()
{
if (m_d->modifiedPaletteInfo.storageLocation == m_d->view->document()->linkedResourcesStorageId()) {
- updatePalette();
KisResourceUserOperations::updateResourceWithUserInput(m_d->view->mainWindowAsQWidget(), m_d->model->colorSet());
m_d->view->document()->setModified(true);
}
@@ -346,11 +363,12 @@ void KisPaletteEditor::removeEntry(const QModelIndex &index)
if (!m_d->view) { return; }
if (!m_d->view->document()) { return; }
if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) {
+ startEditing();
removeGroup(qvariant_cast<QString>(index.data(KisPaletteModel::GroupNameRole)));
+ endEditing();
} else {
m_d->model->removeSwatch(index, false);
}
- updatePalette();
}
void KisPaletteEditor::modifyEntry(const QModelIndex &index)
@@ -364,8 +382,9 @@ void KisPaletteEditor::modifyEntry(const QModelIndex &index)
QString groupName = qvariant_cast<QString>(index.data(Qt::DisplayRole));
if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) {
+ startEditing();
renameGroup(groupName);
- updatePalette();
+ endEditing();
}
else {
@@ -454,8 +473,42 @@ bool KisPaletteEditor::isModified() const
}
}
-void KisPaletteEditor::updatePalette()
+void KisPaletteEditor::startEditing()
+{
+ if (m_d->isEditing) {
+ qWarning() << "KisPaletteEditor::startEditing(): restarting palette editing.";
+ clearStagedChanges();
+ }
+
+ if (!m_d->model || !m_d->model->colorSet()) {
+ return;
+ }
+ KoColorSetSP palette = m_d->model->colorSet();
+
+ m_d->modifiedPaletteInfo.name = palette->name();
+ m_d->modifiedPaletteInfo.storageLocation = palette->storageLocation();
+ m_d->modifiedPaletteInfo.columnCount = palette->columnCount();
+
+ Q_FOREACH (const QString &groupName, palette->swatchGroupNames()) {
+ KisSwatchGroupSP cs = palette->getGroup(groupName);
+ m_d->modifiedPaletteInfo.groups[groupName].reset(new KisSwatchGroup(*cs.data()));
+ }
+
+ m_d->isEditing = true;
+}
+
+void KisPaletteEditor::endEditing(bool applyChanges)
{
+ if (!m_d->isEditing) {
+ qWarning("KisPaletteEditor::endEditing(): not in editing state; ignoring.");
+ return;
+ }
+
+ if (!applyChanges) {
+ clearStagedChanges();
+ return;
+ }
+
if (!m_d->model) return;
if (!m_d->model->colorSet()) return;
if (!m_d->view) return;
@@ -464,10 +517,10 @@ void KisPaletteEditor::updatePalette()
KoColorSetSP palette = m_d->model->colorSet();
PaletteInfo &modified = m_d->modifiedPaletteInfo;
- if (m_d->isColumnCountModified) {
+ if (modified.columnCount != palette->columnCount()) {
m_d->model->setColumnCount(modified.columnCount);
}
- if (m_d->isNameModified) {
+ if (modified.name != palette->name()) {
KisResourceUserOperations::renameResourceWithUserInput(m_d->view->mainWindowAsQWidget(), palette, m_d->modifiedPaletteInfo.name);
}
QString resourceLocation = m_d->model->colorSet()->storageLocation();
@@ -480,7 +533,6 @@ void KisPaletteEditor::updatePalette()
m_d->model->removeGroup(groupName, m_d->keepColorGroups.contains(groupName));
}
}
- m_d->keepColorGroups.clear();
Q_FOREACH (const QString &groupName, palette->swatchGroupNames()) {
if (m_d->modifiedGroupNames.contains(groupName)) {
m_d->model->setRowCountForGroup(groupName, modified.groups[groupName]->rowCount());
@@ -491,11 +543,20 @@ void KisPaletteEditor::updatePalette()
}
}
}
- m_d->modifiedGroupNames.clear();
Q_FOREACH (const QString &newGroupName, m_d->newGroupNames) {
- m_d->model->addGroup(newGroupName);
+ m_d->model->addGroup(newGroupName, palette->columnCount(), m_d->modifiedPaletteInfo.groups[newGroupName]->rowCount());
}
+
+ clearStagedChanges();
+}
+
+void KisPaletteEditor::clearStagedChanges()
+{
+ m_d->modifiedPaletteInfo.groups.clear();
+ m_d->keepColorGroups.clear();
m_d->newGroupNames.clear();
+ m_d->modifiedGroupNames.clear();
+ m_d->isEditing = false;
}
void KisPaletteEditor::saveNewPaletteVersion()
@@ -528,21 +589,7 @@ void KisPaletteEditor::saveNewPaletteVersion()
void KisPaletteEditor::slotPaletteChanged()
{
Q_ASSERT(m_d->model);
- if (!m_d->model->colorSet()) { return; }
- KoColorSetSP palette = m_d->model->colorSet();
- m_d->modifiedPaletteInfo.groups.clear();
- m_d->keepColorGroups.clear();
- m_d->newGroupNames.clear();
- m_d->modifiedGroupNames.clear();
-
- m_d->modifiedPaletteInfo.name = palette->name();
- m_d->modifiedPaletteInfo.storageLocation = palette->storageLocation();
- m_d->modifiedPaletteInfo.columnCount = palette->columnCount();
-
- Q_FOREACH (const QString &groupName, palette->swatchGroupNames()) {
- KisSwatchGroupSP cs = palette->getGroup(groupName);
- m_d->modifiedPaletteInfo.groups[groupName].reset(new KisSwatchGroup(*cs.data()));
- }
+ clearStagedChanges();
}
QString KisPaletteEditor::newGroupName() const
diff --git a/libs/ui/KisPaletteEditor.h b/libs/ui/KisPaletteEditor.h
index fdd5147d0d..6724de69f4 100644
--- a/libs/ui/KisPaletteEditor.h
+++ b/libs/ui/KisPaletteEditor.h
@@ -59,36 +59,41 @@ public:
QString oldNameFromNewName(const QString &newName) const;
/**
- * @brief rename
+ * @brief Stage a palette rename.
* @param newName
*/
void rename(const QString &newName);
/**
- * @brief changeColumnCount
+ * @brief Stage a change of the palette's column count.
*/
void changeColumnCount(int);
/**
- * @brief addGroup
+ * @brief Stage the addition of a new swatch group.
* @return new group's name if change accepted, empty string if cancelled
*/
QString addGroup();
/**
- * @brief removeGroup
+ * @brief Stage the removal of a group.
* @param name original group name
* @return true if change accepted, false if cancelled
*/
bool removeGroup(const QString &name);
/**
- * @brief renameGroup
+ * @brief Stage a rename of a group.
* @param oldName
* @return new name if change accepted, empty string if cancelled
*/
QString renameGroup(const QString &oldName);
+ /**
+ * @brief Stage a change to the row count of a group.
+ * @param name
+ * @param newRowCount
+ */
void changeGroupRowCount(const QString &name, int newRowCount);
void setStorageLocation(QString location);
@@ -104,10 +109,22 @@ public:
bool isModified() const;
/**
- * @brief updatePalette
- * MUST be called to make the changes into the resource server
+ * @brief Start editing the current palette.
+ *
+ * This must be called before any calls that stage changes,
+ * otherwise those calls have no effect.
+ * All staged changes get applied when calling endEditing().
+ *
+ * Adding, removing and updating swatches happens immediately
+ */
+ void startEditing();
+ /**
+ * @brief End editing and either apply or discard staged changes.
+ * @param applyChanges If set to false, the palette remains unchanged and
+ * staged changes are discarded.
*/
- void updatePalette();
+ void endEditing(bool applyChanges = true);
+ void clearStagedChanges();
/**
* @brief saveNewPaletteVersion
diff --git a/libs/ui/dialogs/KisDlgPaletteEditor.cpp b/libs/ui/dialogs/KisDlgPaletteEditor.cpp
index 929756c1e5..c2e59ecb94 100644
--- a/libs/ui/dialogs/KisDlgPaletteEditor.cpp
+++ b/libs/ui/dialogs/KisDlgPaletteEditor.cpp
@@ -34,12 +34,13 @@
#include <kstandardguiitem.h>
-KisDlgPaletteEditor::KisDlgPaletteEditor()
- : m_ui(new Ui_WdgDlgPaletteEditor)
+KisDlgPaletteEditor::KisDlgPaletteEditor(KisPaletteEditor *editor, QWidget *parent, Qt::WindowFlags f)
+ : QDialog(parent, f)
+ , m_ui(new Ui_WdgDlgPaletteEditor)
, m_actAddGroup(new QAction(i18n("Add a swatch group")))
, m_actDelGroup(new QAction(i18nc("Group as Swatch Group in a Palette", "Remove selected group")))
, m_actRenGroup(new QAction(i18nc("Group as Swatch Group in a Palette", "Rename selected group")))
- , m_paletteEditor(new KisPaletteEditor(this))
+ , m_paletteEditor(editor)
, m_currentGroupOriginalName(KoColorSet::GLOBAL_GROUP_NAME)
{
setWindowTitle(i18n("Palette Editor"));
@@ -70,8 +71,6 @@ KisDlgPaletteEditor::KisDlgPaletteEditor()
m_ui->storageLocation->setEnabled(false);
connect(m_ui->storageLocation, SIGNAL(currentIndexChanged(int)), SLOT(slotSetGlobal()));
- connect(this, SIGNAL(accepted()), SLOT(slotAccepted()));
-
m_warnPalette.setColor(QPalette::Text, Qt::red);
KGuiItem::assign(m_ui->buttonBox->button(QDialogButtonBox::Ok), KStandardGuiItem::ok());
@@ -81,13 +80,12 @@ KisDlgPaletteEditor::KisDlgPaletteEditor()
KisDlgPaletteEditor::~KisDlgPaletteEditor()
{ }
-void KisDlgPaletteEditor::setPaletteModel(KisPaletteModel *model)
+void KisDlgPaletteEditor::initialize(KisPaletteModel *model)
{
m_colorSet = model->colorSet();
if (m_colorSet.isNull()) {
return;
}
- m_paletteEditor->setPaletteModel(model);
// don't let editor make changes when initializing
const QSignalBlocker blocker2(m_ui->lineEditName);
@@ -118,11 +116,6 @@ void KisDlgPaletteEditor::setPaletteModel(KisPaletteModel *model)
m_ui->bnAddGroup->setEnabled(true);
}
-void KisDlgPaletteEditor::setView(KisViewManager *view)
-{
- m_paletteEditor->setView(view);
-}
-
void KisDlgPaletteEditor::slotAddGroup()
{
QString newGroupName = m_paletteEditor->addGroup();
@@ -184,8 +177,3 @@ void KisDlgPaletteEditor::slotColCountChanged(int newCount)
{
m_paletteEditor->changeColumnCount(newCount);
}
-
-void KisDlgPaletteEditor::slotAccepted()
-{
- m_paletteEditor->updatePalette();
-}
diff --git a/libs/ui/dialogs/KisDlgPaletteEditor.h b/libs/ui/dialogs/KisDlgPaletteEditor.h
index 0ddb64933a..c063ee70ae 100644
--- a/libs/ui/dialogs/KisDlgPaletteEditor.h
+++ b/libs/ui/dialogs/KisDlgPaletteEditor.h
@@ -39,13 +39,12 @@ class KRITAUI_EXPORT KisDlgPaletteEditor : public QDialog
{
Q_OBJECT
public:
- explicit KisDlgPaletteEditor();
+ explicit KisDlgPaletteEditor(KisPaletteEditor *editor, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
~KisDlgPaletteEditor();
public:
- void setPaletteModel(KisPaletteModel *);
+ void initialize(KisPaletteModel *);
KoColorSetSP palette() const { return m_colorSet; }
- void setView(KisViewManager *);
private Q_SLOTS:
void slotDelGroup();
@@ -60,8 +59,6 @@ private Q_SLOTS:
void slotNameChanged();
void slotColCountChanged(int);
- void slotAccepted();
-
private:
QString oldNameFromNewName(const QString &newName) const;
@@ -70,7 +67,7 @@ private:
QScopedPointer<QAction> m_actAddGroup;
QScopedPointer<QAction> m_actDelGroup;
QScopedPointer<QAction> m_actRenGroup;
- QScopedPointer<KisPaletteEditor> m_paletteEditor;
+ QPointer<KisPaletteEditor> m_paletteEditor;
QSharedPointer<KoColorSet> m_colorSet;
QString m_currentGroupOriginalName;
diff --git a/libs/widgets/KisPaletteComboBox.cpp b/libs/widgets/KisPaletteComboBox.cpp
index a546d9e232..7f79050ffe 100644
--- a/libs/widgets/KisPaletteComboBox.cpp
+++ b/libs/widgets/KisPaletteComboBox.cpp
@@ -87,6 +87,9 @@ void KisPaletteComboBox::slotPaletteChanged()
}
QModelIndex idx = m_view->currentIndex();
if (!idx.isValid()) { return; }
+ // FIXME! checkIndex() is just a workaround to not trigger ASSERT failures amidst model resets/row changes.
+ // This class really needs a rewrite to conform with Qt's MVC patterns...
+ if (!m_model->checkIndex(idx, QAbstractItemModel::CheckIndexOption::IndexIsValid)) { return; }
if (qvariant_cast<bool>(idx.data(KisPaletteModel::IsGroupNameRole))) { return; }
if (!qvariant_cast<bool>(idx.data(KisPaletteModel::CheckSlotRole))) { return; }
diff --git a/libs/widgets/KisPaletteModel.cpp b/libs/widgets/KisPaletteModel.cpp
index a2ee17e6f8..7b35b5294f 100644
--- a/libs/widgets/KisPaletteModel.cpp
+++ b/libs/widgets/KisPaletteModel.cpp
@@ -20,7 +20,7 @@
#include <KoColorDisplayRendererInterface.h>
#include <KisResourceModel.h>
#include <QFileInfo>
-
+#include <QScopedValueRollback>
KisPaletteModel::KisPaletteModel(QObject* parent)
: QAbstractTableModel(parent)
@@ -102,10 +102,20 @@ QModelIndex KisPaletteModel::index(int row, int column, const QModelIndex& paren
void KisPaletteModel::setColorSet(KoColorSetSP colorSet)
{
+ if (colorSet == m_colorSet) {
+ return;
+ }
+
beginResetModel();
m_colorSet = colorSet;
+ if (m_colorSet) {
+ m_colorSet->disconnect(this);
+ }
if (colorSet) {
connect(colorSet.data(), SIGNAL(modified()), this, SIGNAL(sigPaletteModified()));
+ connect(colorSet.data(), SIGNAL(layoutChanged()), this, SLOT(slotLayoutChanged()));
+ connect(colorSet.data(), SIGNAL(layoutAboutToChange()), this, SLOT(slotLayoutAboutToChange()));
+ connect(colorSet.data(), SIGNAL(entryChanged(int,int)), this, SLOT(slotEntryChanged(int,int)));
}
endResetModel();
Q_EMIT sigPaletteChanged();
@@ -146,6 +156,7 @@ void KisPaletteModel::removeSwatch(const QModelIndex &index, bool keepColors)
void KisPaletteModel::removeGroup(const QString &groupName, bool keepColors)
{
+ QScopedValueRollback editMarker(m_editing, true);
int removeStart = m_colorSet->startRowForGroup(groupName);
int removedRowCount = m_colorSet->getGroup(groupName)->rowCount();
@@ -170,7 +181,7 @@ bool KisPaletteModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
if (!finalIndex.isValid()) { return false; }
if (data->hasFormat("krita/x-colorsetgroup")) {
- // dragging group not supported for now
+ QScopedValueRollback editMarker(m_editing, true);
QByteArray encodedData = data->data("krita/x-colorsetgroup");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
@@ -189,7 +200,6 @@ bool KisPaletteModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
}
m_colorSet->moveGroup(groupNameDragged, groupNameDroppedOn);
endMoveRows();
- Q_EMIT sigPaletteModified();
}
return true;
}
@@ -216,7 +226,6 @@ bool KisPaletteModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
}
}
setSwatch(entry, finalIndex);
- Q_EMIT sigPaletteModified();
}
return true;
@@ -279,6 +288,7 @@ void KisPaletteModel::setSwatch(const KisSwatch &entry, const QModelIndex &index
void KisPaletteModel::changeGroupName(const QString &groupName, const QString &newName)
{
+ QScopedValueRollback editMarker(m_editing, true);
beginResetModel();
m_colorSet->changeGroupName(groupName, newName);
endResetModel();
@@ -286,6 +296,7 @@ void KisPaletteModel::changeGroupName(const QString &groupName, const QString &n
KisSwatchGroupSP KisPaletteModel::addGroup(const QString &groupName, int _columnCount, int _rowCount)
{
+ QScopedValueRollback editMarker(m_editing, true);
beginInsertRows(QModelIndex(), rowCount(), rowCount() + _rowCount);
m_colorSet->addGroup(groupName, _columnCount, _rowCount);
endInsertRows();
@@ -294,6 +305,7 @@ KisSwatchGroupSP KisPaletteModel::addGroup(const QString &groupName, int _column
void KisPaletteModel::setRowCountForGroup(const QString &groupName, int rowCount)
{
+ QScopedValueRollback editMarker(m_editing, true);
beginResetModel();
KisSwatchGroupSP g = m_colorSet->getGroup(groupName);
if (g) {
@@ -304,20 +316,17 @@ void KisPaletteModel::setRowCountForGroup(const QString &groupName, int rowCount
void KisPaletteModel::setColumnCount(int colCount)
{
- beginResetModel();
m_colorSet->setColumnCount(colCount);
- endResetModel();
}
void KisPaletteModel::clear()
{
- beginResetModel();
m_colorSet->clear();
- endResetModel();
}
void KisPaletteModel::clear(int defaultColumnsCount)
{
+ QScopedValueRollback editMarker(m_editing, true);
beginResetModel();
m_colorSet->clear();
m_colorSet->setColumnCount(defaultColumnsCount);
@@ -424,6 +433,27 @@ void KisPaletteModel::slotPaletteModified()
}
}
+void KisPaletteModel::slotLayoutAboutToChange()
+{
+ // when m_editing is true, we have the information about model changes and don't need to reset
+ if (!m_editing) {
+ beginResetModel();
+ }
+}
+
+void KisPaletteModel::slotLayoutChanged()
+{
+ if (!m_editing) {
+ endResetModel();
+ }
+}
+
+void KisPaletteModel::slotEntryChanged(int column, int row)
+{
+ QModelIndex index = createIndex(row, column);
+ Q_EMIT dataChanged(index, index);
+}
+
void KisPaletteModel::slotExternalPaletteModified(QSharedPointer<KoColorSet> resource)
{
if (resource && resource == m_colorSet) {
diff --git a/libs/widgets/KisPaletteModel.h b/libs/widgets/KisPaletteModel.h
index cdf54cf533..9a1d15d0ad 100644
--- a/libs/widgets/KisPaletteModel.h
+++ b/libs/widgets/KisPaletteModel.h
@@ -121,6 +121,9 @@ private Q_SLOTS:
void slotDisplayConfigurationChanged();
void slotPaletteModified();
+ void slotLayoutAboutToChange();
+ void slotLayoutChanged();
+ void slotEntryChanged(int column, int row);
private:
@@ -135,7 +138,7 @@ private:
private /* member variables */:
KoColorSetSP m_colorSet;
QPointer<const KoColorDisplayRendererInterface> m_displayRenderer;
-
+ bool m_editing {false};
};
#endif
diff --git a/libs/widgets/kis_palette_view.cpp b/libs/widgets/kis_palette_view.cpp
index 2353b8e5f4..f9a266af90 100644
--- a/libs/widgets/kis_palette_view.cpp
+++ b/libs/widgets/kis_palette_view.cpp
@@ -286,7 +286,6 @@ void KisPaletteView::slotAdditionalGuiUpdate()
setRowHeight(rowNumber, fontMetrics().lineSpacing() + 6);
verticalHeader()->resizeSection(rowNumber, fontMetrics().lineSpacing() + 6);
}
- Q_EMIT sigPaletteUpdatedFromModel();
}
void KisPaletteView::slotCurrentSelectionChanged(const QModelIndex &newCurrent)
diff --git a/libs/widgets/kis_palette_view.h b/libs/widgets/kis_palette_view.h
index 6c398ff357..bb8bb66966 100644
--- a/libs/widgets/kis_palette_view.h
+++ b/libs/widgets/kis_palette_view.h
@@ -95,7 +95,6 @@ public:
Q_SIGNALS:
void sigIndexSelected(const QModelIndex &index);
void sigColorSelected(const KoColor &);
- void sigPaletteUpdatedFromModel();
public Q_SLOTS:
/**
diff --git a/plugins/dockers/palettedocker/palettedocker_dock.cpp b/plugins/dockers/palettedocker/palettedocker_dock.cpp
index 565f0e01b9..573c765ef9 100644
--- a/plugins/dockers/palettedocker/palettedocker_dock.cpp
+++ b/plugins/dockers/palettedocker/palettedocker_dock.cpp
@@ -107,7 +107,7 @@ PaletteDockerDock::PaletteDockerDock( )
connect(m_ui->paletteView, SIGNAL(clicked(QModelIndex)), SLOT(slotPaletteIndexClicked(QModelIndex)));
connect(m_ui->paletteView, SIGNAL(doubleClicked(QModelIndex)),
SLOT(slotPaletteIndexDoubleClicked(QModelIndex)));
- connect(m_ui->paletteView, SIGNAL(sigPaletteUpdatedFromModel()), SLOT(slotUpdateLblPaletteName()));
+ connect(m_model, SIGNAL(sigPaletteModified()), SLOT(slotUpdateLblPaletteName()));
connect(m_ui->cmbNameList, SIGNAL(sigColorSelected(const KoColor&)), SLOT(slotNameListSelection(const KoColor&)));
connect(m_ui->bnLock, SIGNAL(toggled(bool)), SLOT(slotLockPalette(bool)));
@@ -276,7 +276,6 @@ void PaletteDockerDock::slotSetColorSet(KoColorSetSP colorSet)
{
if (m_currentColorSet == colorSet) {
slotUpdateLblPaletteName();
- m_paletteEditor->updatePalette();
return;
}
@@ -288,7 +287,7 @@ void PaletteDockerDock::slotSetColorSet(KoColorSetSP colorSet)
if (m_paletteEditor->isModified() && m_currentColorSet != colorSet) {
m_paletteEditor->saveNewPaletteVersion();
}
- else if (colorSet) {
+ if (colorSet) {
m_model->setColorSet(colorSet);
@@ -332,13 +331,12 @@ void PaletteDockerDock::slotSetColorSet(KoColorSetSP colorSet)
void PaletteDockerDock::slotEditPalette()
{
- KisDlgPaletteEditor dlg;
+ KisDlgPaletteEditor dlg(m_paletteEditor.data());
if (!m_currentColorSet) { return; }
- dlg.setPaletteModel(m_model);
- dlg.setView(m_view);
- if (dlg.exec() != QDialog::Accepted){ return; }
-
- slotSetColorSet(m_currentColorSet); // update GUI
+ dlg.initialize(m_model);
+ m_paletteEditor->startEditing();
+ bool applyChanges = (dlg.exec() == QDialog::Accepted);
+ m_paletteEditor->endEditing(applyChanges);
}
void PaletteDockerDock::slotSavePalette()
@@ -451,18 +449,13 @@ void PaletteDockerDock::setRedoToolTip(const QString &text)
void PaletteDockerDock::undo()
{
m_currentColorSet->undoStack()->undo();
- m_model->slotExternalPaletteModified(m_currentColorSet);
- m_paletteEditor->updatePalette();
slotUpdateLblPaletteName();
}
void PaletteDockerDock::redo()
{
m_currentColorSet->undoStack()->redo();
- m_model->slotExternalPaletteModified(m_currentColorSet);
- m_paletteEditor->updatePalette();
slotUpdateLblPaletteName();
-
}