Skip to content

Commit

Permalink
Make reloading a brush preset slot-specific
Browse files Browse the repository at this point in the history
It's on Ctrl+P default. Before it would just restore whichever preset
was clicked on last, regardless of which slot it had been loaded into.
Now it restores the last preset loaded into the current slot, which
makes more sense. It also now remembers these across program restarts,
rather than having to click on the preset again to make it register as
being the last one.
  • Loading branch information
askmeaboutlo0m committed Aug 24, 2023
1 parent 4f72944 commit 4c1b067
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 35 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 1 addition & 12 deletions src/desktop/docks/brushpalettedock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -501,9 +492,7 @@ void BrushPalette::applyToBrushSettings(const QModelIndex &proxyIndex)
return;
}

d->previousBrush = v.value<brushes::ActiveBrush>();
d->havePreviousBrush = true;
d->brushSettings->setCurrentBrush(d->previousBrush);
d->brushSettings->setCurrentBrush(v.value<brushes::ActiveBrush>());
}

void BrushPalette::showPresetContextMenu(const QPoint &pos)
Expand Down
1 change: 0 additions & 1 deletion src/desktop/docks/brushpalettedock.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Q_OBJECT
public slots:
void importBrushes();
void exportBrushes();
void reloadPreset();

private slots:
void tagIndexChanged(int proxyRow);
Expand Down
2 changes: 1 addition & 1 deletion src/desktop/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
45 changes: 33 additions & 12 deletions src/desktop/toolwidgets/brushsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern "C" {
#include <QJsonObject>
#include <QPointer>
#include <QMenu>
#include <optional>

namespace tools {

Expand All @@ -38,6 +39,7 @@ struct BrushSettings::Private {
QStandardItemModel *blendModes, *eraseModes;

brushes::ActiveBrush brushSlots[BRUSH_COUNT];
std::optional<brushes::ActiveBrush> lastPresets[BRUSH_COUNT];
widgets::GroupedToolButton *brushSlotButton[BRUSH_COUNT];
QWidget *brushSlotWidget = nullptr;

Expand Down Expand Up @@ -305,6 +307,7 @@ void BrushSettings::setCurrentBrush(brushes::ActiveBrush brush)
}

currentBrush.setActiveType(activeType);
d->lastPresets[d->current] = brush;
updateUi();
}

Expand Down Expand Up @@ -372,6 +375,14 @@ void BrushSettings::toggleRecolorMode()
}
}

void BrushSettings::reloadPreset()
{
const std::optional<brushes::ActiveBrush> &opt = d->lastPresets[d->current];
if(opt.has_value()) {
setCurrentBrush(opt.value());
}
}

void BrushSettings::setEraserMode(bool erase)
{
Q_ASSERT(!isCurrentEraserSlot());
Expand Down Expand Up @@ -757,20 +768,24 @@ ToolProperties BrushSettings::saveToolSettings()
cfg.setValue(toolprop::smoothing, d->ui.smoothingBox->value() - d->globalSmoothing);

for(int i=0;i<BRUSH_COUNT;++i) {
const brushes::ActiveBrush &brush = d->brushSlots[i];
QJsonObject b = brush.toJson();

b["_slot"] = QJsonObject {
{"color", brush.qColor().name()},
};

cfg.setValue(
ToolProperties::Value<QByteArray> {
QStringLiteral("brush%1").arg(i),
QByteArray()
},
QJsonDocument(b).toJson(QJsonDocument::Compact)
d->brushSlots[i].toJson(true)
);

const std::optional<brushes::ActiveBrush> &opt = d->lastPresets[i];
if(opt.has_value()) {
cfg.setValue(
ToolProperties::Value<QByteArray> {
QStringLiteral("last%1").arg(i),
QByteArray()
},
opt.value().toJson()
);
}
}

return cfg;
Expand All @@ -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<QByteArray> {
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());
Expand Down
1 change: 1 addition & 0 deletions src/desktop/toolwidgets/brushsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public slots:
void setGlobalSmoothing(int smoothing);
void toggleEraserMode() override;
void toggleRecolorMode() override;
void reloadPreset();

signals:
void colorChanged(const QColor &color);
Expand Down
25 changes: 20 additions & 5 deletions src/libclient/brushes/brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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},
Expand All @@ -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"];
Expand All @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/libclient/brushes/brush.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/libclient/brushes/brushpresetmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4c1b067

Please sign in to comment.