mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-12-05 03:08:00 -05:00
Save Named Gizmo Groups in the Toolbar Layout
The toolbar layout gains a list of named groups of gizmo bar icons. Loading drops members that are not gizmo bar icons or that an earlier group already holds, drops groups left with fewer than two members, and keeps each group's members together in the bar order. Entries this build does not recognise as groups are kept on save.
This commit is contained in:
1 parent
dec510e1a5
commit
bd7c172c09
3 files changed
+130
-5
No files matched your search
@@ -3,7 +3,9 @@
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Slic3r { namespace GUI {
|
||||
@@ -15,6 +17,9 @@ constexpr const char* KEY_MAIN = "main";
|
||||
constexpr const char* KEY_MAIN_HIDDEN = "main_hidden";
|
||||
constexpr const char* KEY_GIZMOS = "gizmos";
|
||||
constexpr const char* KEY_GIZMOS_HIDDEN = "gizmos_hidden";
|
||||
constexpr const char* KEY_GROUPS = "groups";
|
||||
constexpr const char* KEY_GROUP_NAME = "name";
|
||||
constexpr const char* KEY_GROUP_MEMBERS = "members";
|
||||
|
||||
std::vector<std::string> read_string_array(const nlohmann::json& object, const char* key)
|
||||
{
|
||||
@@ -128,8 +133,22 @@ ToolbarLayout parse_toolbar_layout(const std::string& value)
|
||||
layout.main = merge_toolbar_group(stock_main_toolbar_ids(), read_string_array(parsed, KEY_MAIN), read_string_array(parsed, KEY_MAIN_HIDDEN));
|
||||
layout.gizmos = merge_toolbar_group(stock_gizmo_bar_ids(), read_string_array(parsed, KEY_GIZMOS), read_string_array(parsed, KEY_GIZMOS_HIDDEN));
|
||||
|
||||
const auto groups = parsed.find(KEY_GROUPS);
|
||||
const bool groups_known = groups != parsed.end() && groups->is_array();
|
||||
if (groups_known)
|
||||
for (const nlohmann::json& entry : *groups) {
|
||||
const auto name = entry.is_object() ? entry.find(KEY_GROUP_NAME) : entry.end();
|
||||
const auto members = entry.is_object() ? entry.find(KEY_GROUP_MEMBERS) : entry.end();
|
||||
if (entry.is_object() && name != entry.end() && name->is_string() && members != entry.end() && members->is_array())
|
||||
layout.groups.push_back({name->get<std::string>(), read_string_array(entry, KEY_GROUP_MEMBERS)});
|
||||
else
|
||||
layout.other_groups.push_back(entry);
|
||||
}
|
||||
normalize_gizmo_groups(layout);
|
||||
|
||||
for (const auto& [key, field] : parsed.items())
|
||||
if (key != KEY_VERSION && key != KEY_MAIN && key != KEY_MAIN_HIDDEN && key != KEY_GIZMOS && key != KEY_GIZMOS_HIDDEN)
|
||||
if (key != KEY_VERSION && key != KEY_MAIN && key != KEY_MAIN_HIDDEN && key != KEY_GIZMOS && key != KEY_GIZMOS_HIDDEN &&
|
||||
!(key == KEY_GROUPS && groups_known))
|
||||
layout.extra[key] = field;
|
||||
return layout;
|
||||
}
|
||||
@@ -139,13 +158,51 @@ std::string serialize_toolbar_layout(const ToolbarLayout& layout)
|
||||
nlohmann::json out = nlohmann::json::object();
|
||||
write_group(out, layout.main, stock_main_toolbar_ids(), KEY_MAIN, KEY_MAIN_HIDDEN);
|
||||
write_group(out, layout.gizmos, stock_gizmo_bar_ids(), KEY_GIZMOS, KEY_GIZMOS_HIDDEN);
|
||||
if (!layout.groups.empty() || !layout.other_groups.empty()) {
|
||||
nlohmann::json groups = nlohmann::json::array();
|
||||
for (const ToolbarGizmoGroup& group : layout.groups)
|
||||
groups.push_back({{KEY_GROUP_NAME, group.name}, {KEY_GROUP_MEMBERS, group.members}});
|
||||
for (const nlohmann::json& entry : layout.other_groups)
|
||||
groups.push_back(entry);
|
||||
out[KEY_GROUPS] = std::move(groups);
|
||||
}
|
||||
if (out.empty() && layout.extra.empty())
|
||||
return {};
|
||||
|
||||
out[KEY_VERSION] = std::max(layout.version, 1);
|
||||
for (const auto& [key, field] : layout.extra.items())
|
||||
out[key] = field;
|
||||
// A "groups" value this build could not read gives way to the groups made here.
|
||||
if (key != KEY_GROUPS || out.find(KEY_GROUPS) == out.end())
|
||||
out[key] = field;
|
||||
return out.dump();
|
||||
}
|
||||
|
||||
void normalize_gizmo_groups(ToolbarLayout& layout)
|
||||
{
|
||||
std::vector<std::string>& order = layout.gizmos.order;
|
||||
std::set<std::string> taken;
|
||||
std::vector<ToolbarGizmoGroup> kept;
|
||||
for (const ToolbarGizmoGroup& group : layout.groups) {
|
||||
ToolbarGizmoGroup clean{group.name, {}};
|
||||
for (const std::string& id : order)
|
||||
if (contains(group.members, id) && contains(stock_gizmo_bar_ids(), id) && taken.count(id) == 0)
|
||||
clean.members.push_back(id);
|
||||
if (clean.members.size() < 2)
|
||||
continue;
|
||||
taken.insert(clean.members.begin(), clean.members.end());
|
||||
kept.push_back(std::move(clean));
|
||||
}
|
||||
|
||||
// Each group's members follow its first member, in their existing relative order.
|
||||
for (const ToolbarGizmoGroup& group : kept) {
|
||||
const std::vector<std::string> rest(group.members.begin() + 1, group.members.end());
|
||||
order.erase(std::remove_if(order.begin(), order.end(), [&rest](const std::string& id) { return contains(rest, id); }), order.end());
|
||||
order.insert(std::next(std::find(order.begin(), order.end(), group.members.front())), rest.begin(), rest.end());
|
||||
}
|
||||
|
||||
const auto position = [&order](const ToolbarGizmoGroup& group) { return std::find(order.begin(), order.end(), group.members.front()) - order.begin(); };
|
||||
std::stable_sort(kept.begin(), kept.end(), [&position](const ToolbarGizmoGroup& a, const ToolbarGizmoGroup& b) { return position(a) < position(b); });
|
||||
layout.groups = std::move(kept);
|
||||
}
|
||||
|
||||
}} // namespace Slic3r::GUI
|
||||
@@ -23,17 +23,32 @@ struct ToolbarGroupLayout
|
||||
bool operator!=(const ToolbarGroupLayout& rhs) const { return !(*this == rhs); }
|
||||
};
|
||||
|
||||
// A user-made group of gizmo bar icons that share one bar button. Members are gizmo bar ids in their bar order.
|
||||
struct ToolbarGizmoGroup
|
||||
{
|
||||
std::string name;
|
||||
std::vector<std::string> members;
|
||||
|
||||
bool operator==(const ToolbarGizmoGroup& rhs) const { return name == rhs.name && members == rhs.members; }
|
||||
bool operator!=(const ToolbarGizmoGroup& rhs) const { return !(*this == rhs); }
|
||||
};
|
||||
|
||||
struct ToolbarLayout
|
||||
{
|
||||
int version = 1;
|
||||
ToolbarGroupLayout main;
|
||||
ToolbarGroupLayout gizmos;
|
||||
// The user's gizmo groups, in bar order.
|
||||
std::vector<ToolbarGizmoGroup> groups;
|
||||
// Entries of the saved "groups" array that are not user groups, kept so a save does not drop them.
|
||||
nlohmann::json other_groups = nlohmann::json::array();
|
||||
// Top-level fields this build does not know (written by a newer one), kept so a save does not drop them.
|
||||
nlohmann::json extra = nlohmann::json::object();
|
||||
|
||||
bool operator==(const ToolbarLayout& rhs) const
|
||||
{
|
||||
return version == rhs.version && main == rhs.main && gizmos == rhs.gizmos && extra == rhs.extra;
|
||||
return version == rhs.version && main == rhs.main && gizmos == rhs.gizmos && groups == rhs.groups &&
|
||||
other_groups == rhs.other_groups && extra == rhs.extra;
|
||||
}
|
||||
bool operator!=(const ToolbarLayout& rhs) const { return !(*this == rhs); }
|
||||
};
|
||||
@@ -57,4 +72,9 @@ ToolbarLayout parse_toolbar_layout(const std::string& value);
|
||||
// Returns "" when the layout is stock and carries no unknown fields: the caller then erases the key.
|
||||
std::string serialize_toolbar_layout(const ToolbarLayout& layout);
|
||||
|
||||
// Drops group members that are not gizmo bar ids or that an earlier group already has, and groups left with fewer
|
||||
// than two members; then moves each group's members next to its first one in gizmos.order and keeps the groups and
|
||||
// their members in bar order.
|
||||
void normalize_gizmo_groups(ToolbarLayout& layout);
|
||||
|
||||
}} // namespace Slic3r::GUI
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -58,12 +59,12 @@ TEST_CASE("ToolbarLayout round-trips a change and writes only differing parts",
|
||||
|
||||
TEST_CASE("ToolbarLayout keeps unknown fields and a newer version", "[ToolbarLayout]")
|
||||
{
|
||||
ToolbarLayout l = parse_toolbar_layout(R"({"version":2,"groups":[{"id":"paint"}],"gizmos_hidden":["seam"]})");
|
||||
ToolbarLayout l = parse_toolbar_layout(R"({"version":2,"future":[{"id":"x"}],"gizmos_hidden":["seam"]})");
|
||||
CHECK(l.version == 2);
|
||||
l.gizmos.hidden.insert("cut");
|
||||
const nlohmann::json j = nlohmann::json::parse(serialize_toolbar_layout(l));
|
||||
CHECK(j["version"] == 2);
|
||||
CHECK(j["groups"][0]["id"] == "paint");
|
||||
CHECK(j["future"][0]["id"] == "x");
|
||||
|
||||
// Stock lists with unknown fields still serialise, so a newer build's data is never dropped silently.
|
||||
ToolbarLayout s = stock_toolbar_layout();
|
||||
@@ -87,3 +88,50 @@ TEST_CASE("ToolbarLayout gizmo ids cover the bar without svg and simplify", "[To
|
||||
CHECK(std::count(stock_gizmo_bar_ids().begin(), stock_gizmo_bar_ids().end(), "svg") == 0);
|
||||
CHECK(parse_toolbar_layout(R"({"gizmos":["simplify","move"]})").gizmos.order.front() == "move");
|
||||
}
|
||||
|
||||
TEST_CASE("ToolbarLayout user groups round-trip", "[ToolbarLayout]")
|
||||
{
|
||||
ToolbarLayout l = stock_toolbar_layout();
|
||||
l.groups.push_back({"Surface", {"fuzzy_skin", "texture_displacement"}});
|
||||
normalize_gizmo_groups(l);
|
||||
const ToolbarLayout back = parse_toolbar_layout(serialize_toolbar_layout(l));
|
||||
CHECK(back == l);
|
||||
REQUIRE(back.groups.size() == 1);
|
||||
CHECK(back.groups[0].name == "Surface");
|
||||
}
|
||||
|
||||
TEST_CASE("ToolbarLayout user groups drop bad members", "[ToolbarLayout]")
|
||||
{
|
||||
const ToolbarLayout l = parse_toolbar_layout(R"({"version":1,"groups":[
|
||||
{"name":"A","members":["seam","nope","fdm_supports"]},
|
||||
{"name":"B","members":["seam","fuzzy_skin"]},
|
||||
{"name":"C","members":["move","svg"]}]})");
|
||||
// B loses seam to A and keeps one member, so it is dropped; svg is not a bar id, so C is dropped too.
|
||||
REQUIRE(l.groups.size() == 1);
|
||||
CHECK(l.groups[0] == ToolbarGizmoGroup{"A", {"fdm_supports", "seam"}});
|
||||
}
|
||||
|
||||
TEST_CASE("ToolbarLayout user group members are made contiguous", "[ToolbarLayout]")
|
||||
{
|
||||
const ToolbarLayout l = parse_toolbar_layout(R"({"version":1,"groups":[{"name":"G","members":["move","brim_ears"]}]})");
|
||||
const std::vector<std::string>& o = l.gizmos.order;
|
||||
const auto at = std::find(o.begin(), o.end(), "move") - o.begin();
|
||||
CHECK(o[static_cast<size_t>(at) + 1] == "brim_ears");
|
||||
}
|
||||
|
||||
TEST_CASE("ToolbarLayout keeps other group entries and writes no empty groups", "[ToolbarLayout]")
|
||||
{
|
||||
const ToolbarLayout l = parse_toolbar_layout(R"({"version":1,"groups":[{"id":"paint"}]})");
|
||||
CHECK(l.groups.empty());
|
||||
CHECK(serialize_toolbar_layout(l).find("\"paint\"") != std::string::npos);
|
||||
CHECK(serialize_toolbar_layout(stock_toolbar_layout()).empty());
|
||||
}
|
||||
|
||||
TEST_CASE("ToolbarLayout bad groups keep other settings", "[ToolbarLayout]")
|
||||
{
|
||||
const ToolbarLayout l = parse_toolbar_layout(R"({"version":1,"main":["layersediting","add"],"gizmos_hidden":["move"],
|
||||
"groups":[{"name":"X","members":["nope","svg"]},{"name":"Y","members":"seam"}]})");
|
||||
CHECK(l.groups.empty());
|
||||
CHECK(l.gizmos.hidden.count("move") == 1);
|
||||
CHECK(l.main.order.front() == "layersediting");
|
||||
}
|
||||
Reference in new issue
Block a user