diff --git a/ChangeLog b/ChangeLog index 94a890ca9b..1eb3285d88 100644 --- a/ChangeLog +++ b/ChangeLog @@ -34,6 +34,7 @@ Unreleased Version 2.2.0-pre * Fix: Make the flipbook remember your last crop, frame range and playback speed for the current window. Thanks Ben for finding. * Fix: Don't mark guests as registered. Thanks to xxxx for reporting. * Fix: Allow assigning a shortcut to open the Layouts dialog (F9 by default) and to the entries in the Help menu (nothing by default.) + * Fix: Make reloading the last brush preset slot-specific, since it's nonsense to clobber your current slot with the last preset you set in another one. 2023-07-31 Version 2.2.0-beta.6 * Fix: Don't forget account password when entering a wrong session password. diff --git a/src/desktop/docks/brushpalettedock.cpp b/src/desktop/docks/brushpalettedock.cpp index b4e6441603..3b51698445 100644 --- a/src/desktop/docks/brushpalettedock.cpp +++ b/src/desktop/docks/brushpalettedock.cpp @@ -61,8 +61,6 @@ struct BrushPalette::Private { tools::BrushSettings *brushSettings; brushes::Tag currentTag; brushes::ActiveBrush newBrush; - brushes::ActiveBrush previousBrush; - bool havePreviousBrush = false; QComboBox *tagComboBox; QLineEdit *searchLineEdit; @@ -286,13 +284,6 @@ void BrushPalette::exportBrushes() showExportDialog(); } -void BrushPalette::reloadPreset() -{ - if(d->brushSettings && d->havePreviousBrush) { - d->brushSettings->setCurrentBrush(d->previousBrush); - } -} - void BrushPalette::tagIndexChanged(int row) { d->currentTag = d->tagModel->getTagAt(row); @@ -501,9 +492,7 @@ void BrushPalette::applyToBrushSettings(const QModelIndex &proxyIndex) return; } - d->previousBrush = v.value(); - d->havePreviousBrush = true; - d->brushSettings->setCurrentBrush(d->previousBrush); + d->brushSettings->setCurrentBrush(v.value()); } void BrushPalette::showPresetContextMenu(const QPoint &pos) diff --git a/src/desktop/docks/brushpalettedock.h b/src/desktop/docks/brushpalettedock.h index 9e0777f7d1..c6c34de9ed 100644 --- a/src/desktop/docks/brushpalettedock.h +++ b/src/desktop/docks/brushpalettedock.h @@ -35,7 +35,6 @@ Q_OBJECT public slots: void importBrushes(); void exportBrushes(); - void reloadPreset(); private slots: void tagIndexChanged(int proxyRow); diff --git a/src/desktop/mainwindow.cpp b/src/desktop/mainwindow.cpp index 433a0ff06c..f69c27e0b9 100644 --- a/src/desktop/mainwindow.cpp +++ b/src/desktop/mainwindow.cpp @@ -3645,7 +3645,7 @@ void MainWindow::setupActions() connect(smallerbrush, &QAction::triggered, this, [this]() { m_dockToolSettings->stepAdjustCurrent1(false); }); connect(biggerbrush, &QAction::triggered, this, [this]() { m_dockToolSettings->stepAdjustCurrent1(true); }); - connect(reloadPreset, &QAction::triggered, m_dockBrushPalette, &docks::BrushPalette::reloadPreset); + connect(reloadPreset, &QAction::triggered, m_dockToolSettings->brushSettings(), &tools::BrushSettings::reloadPreset); toolshortcuts->addAction(currentEraseMode); toolshortcuts->addAction(currentRecolorMode); diff --git a/src/desktop/toolwidgets/brushsettings.cpp b/src/desktop/toolwidgets/brushsettings.cpp index 111620630d..f3d8c83616 100644 --- a/src/desktop/toolwidgets/brushsettings.cpp +++ b/src/desktop/toolwidgets/brushsettings.cpp @@ -26,6 +26,7 @@ extern "C" { #include #include #include +#include namespace tools { @@ -38,6 +39,7 @@ struct BrushSettings::Private { QStandardItemModel *blendModes, *eraseModes; brushes::ActiveBrush brushSlots[BRUSH_COUNT]; + std::optional lastPresets[BRUSH_COUNT]; widgets::GroupedToolButton *brushSlotButton[BRUSH_COUNT]; QWidget *brushSlotWidget = nullptr; @@ -305,6 +307,7 @@ void BrushSettings::setCurrentBrush(brushes::ActiveBrush brush) } currentBrush.setActiveType(activeType); + d->lastPresets[d->current] = brush; updateUi(); } @@ -372,6 +375,14 @@ void BrushSettings::toggleRecolorMode() } } +void BrushSettings::reloadPreset() +{ + const std::optional &opt = d->lastPresets[d->current]; + if(opt.has_value()) { + setCurrentBrush(opt.value()); + } +} + void BrushSettings::setEraserMode(bool erase) { Q_ASSERT(!isCurrentEraserSlot()); @@ -757,20 +768,24 @@ ToolProperties BrushSettings::saveToolSettings() cfg.setValue(toolprop::smoothing, d->ui.smoothingBox->value() - d->globalSmoothing); for(int i=0;ibrushSlots[i]; - QJsonObject b = brush.toJson(); - - b["_slot"] = QJsonObject { - {"color", brush.qColor().name()}, - }; - cfg.setValue( ToolProperties::Value { QStringLiteral("brush%1").arg(i), QByteArray() }, - QJsonDocument(b).toJson(QJsonDocument::Compact) + d->brushSlots[i].toJson(true) ); + + const std::optional &opt = d->lastPresets[i]; + if(opt.has_value()) { + cfg.setValue( + ToolProperties::Value { + QStringLiteral("last%1").arg(i), + QByteArray() + }, + opt.value().toJson() + ); + } } return cfg; @@ -788,11 +803,17 @@ void BrushSettings::restoreToolSettings(const ToolProperties &cfg) QByteArray() }) ).object(); - const QJsonObject s = o["_slot"].toObject(); + brush = brushes::ActiveBrush::fromJson(o, true); - brush = brushes::ActiveBrush::fromJson(o); - const auto color = QColor(s["color"].toString()); - brush.setQColor(color.isValid() ? color : Qt::black); + const QJsonObject lo = QJsonDocument::fromJson( + cfg.value(ToolProperties::Value { + QStringLiteral("last%1").arg(i), + QByteArray() + }) + ).object(); + if(!lo.isEmpty()) { + d->lastPresets[i] = brushes::ActiveBrush::fromJson(lo); + } if(!d->shareBrushSlotColor) d->brushSlotButton[i]->setColorSwatch(brush.qColor()); diff --git a/src/desktop/toolwidgets/brushsettings.h b/src/desktop/toolwidgets/brushsettings.h index 8ac6fe57a0..ca5dd7f2eb 100644 --- a/src/desktop/toolwidgets/brushsettings.h +++ b/src/desktop/toolwidgets/brushsettings.h @@ -62,6 +62,7 @@ public slots: void setGlobalSmoothing(int smoothing); void toggleEraserMode() override; void toggleRecolorMode() override; + void reloadPreset(); signals: void colorChanged(const QColor &color); diff --git a/src/libclient/brushes/brush.cpp b/src/libclient/brushes/brush.cpp index 39b95e2c0b..ce938b09c9 100644 --- a/src/libclient/brushes/brush.cpp +++ b/src/libclient/brushes/brush.cpp @@ -707,9 +707,9 @@ void ActiveBrush::setSmoothing(int smoothing) } } -QJsonObject ActiveBrush::toJson() const +QByteArray ActiveBrush::toJson(bool includeSlotProperties) const { - return QJsonObject { + QJsonObject json{ {"type", "dp-active"}, {"version", 1}, {"active_type", m_activeType == CLASSIC ? "classic" : "mypaint"}, @@ -718,9 +718,15 @@ QJsonObject ActiveBrush::toJson() const {"mypaint", m_myPaint.toJson()}, }}, }; + if(includeSlotProperties) { + json["_slot"] = QJsonObject { + {"color", qColor().name()}, + }; + } + return QJsonDocument{json}.toJson(QJsonDocument::Compact); } -QJsonObject ActiveBrush::toExportJson(const QString &description) const +QByteArray ActiveBrush::toExportJson(const QString &description) const { QJsonObject json{ {"comment", description}, @@ -734,10 +740,10 @@ QJsonObject ActiveBrush::toExportJson(const QString &description) const } else { m_myPaint.exportToJson(json); } - return json; + return QJsonDocument{json}.toJson(QJsonDocument::Indented); } -ActiveBrush ActiveBrush::fromJson(const QJsonObject &json) +ActiveBrush ActiveBrush::fromJson(const QJsonObject &json, bool includeSlotProperties) { ActiveBrush brush; const QJsonValue type = json["type"]; @@ -755,6 +761,15 @@ ActiveBrush ActiveBrush::fromJson(const QJsonObject &json) } else { qWarning("ActiveBrush::fromJson: type is neither dp-active, dp-classic nor dp-mypaint!"); } + + if(includeSlotProperties) { + QJsonObject slot = json["_slot"].toObject(); + if(!slot.isEmpty()) { + QColor color = QColor(slot["color"].toString()); + brush.setQColor(color.isValid() ? color : Qt::black); + } + } + return brush; } diff --git a/src/libclient/brushes/brush.h b/src/libclient/brushes/brush.h index f83a4e4fa4..90f2b6a215 100644 --- a/src/libclient/brushes/brush.h +++ b/src/libclient/brushes/brush.h @@ -200,9 +200,9 @@ class ActiveBrush final int smoothing() const; void setSmoothing(int smoothing); - QJsonObject toJson() const; - QJsonObject toExportJson(const QString &description) const; - static ActiveBrush fromJson(const QJsonObject &json); + QByteArray toJson(bool includeSlotProperties = false) const; + QByteArray toExportJson(const QString &description) const; + static ActiveBrush fromJson(const QJsonObject &json, bool includeSlotProperties = false); bool fromExportJson(const QJsonObject &json); QString presetType() const; diff --git a/src/libclient/brushes/brushpresetmodel.cpp b/src/libclient/brushes/brushpresetmodel.cpp index 0741c66dde..62e7951da4 100644 --- a/src/libclient/brushes/brushpresetmodel.cpp +++ b/src/libclient/brushes/brushpresetmodel.cpp @@ -959,7 +959,7 @@ void BrushPresetTagModel::exportPreset( } ActiveBrush brush = ActiveBrush::fromJson(doc.object()); - QByteArray exportData = QJsonDocument{brush.toExportJson(preset.description)}.toJson(QJsonDocument::Indented); + QByteArray exportData = brush.toExportJson(preset.description); QString presetName = getExportName(presetId, preset.name); QString presetPath = QStringLiteral("%1/%2").arg(tagPath, presetName);