mirror of
https://github.com/aseprite/aseprite.git
synced 2024-09-02 13:09:27 +00:00
Update saving/loading dock layouts w/tinyxml2 library
This commit is contained in:
parent
72b61fc2e7
commit
2cf03962a6
@ -29,7 +29,9 @@
|
|||||||
|
|
||||||
namespace app {
|
namespace app {
|
||||||
|
|
||||||
static void save_dock_layout(TiXmlElement* elem, const Dock* dock)
|
using namespace tinyxml2;
|
||||||
|
|
||||||
|
static void save_dock_layout(XMLElement* elem, const Dock* dock)
|
||||||
{
|
{
|
||||||
for (const auto child : dock->children()) {
|
for (const auto child : dock->children()) {
|
||||||
const int side = dock->whichSideChildIsDocked(child);
|
const int side = dock->whichSideChildIsDocked(child);
|
||||||
@ -46,30 +48,28 @@ static void save_dock_layout(TiXmlElement* elem, const Dock* dock)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
TiXmlElement childElem("");
|
XMLElement* childElem = elem->InsertNewChildElement("");
|
||||||
|
|
||||||
if (auto subdock = dynamic_cast<const Dock*>(child)) {
|
if (auto subdock = dynamic_cast<const Dock*>(child)) {
|
||||||
childElem.SetValue("dock");
|
childElem->SetValue("dock");
|
||||||
if (!sideStr.empty())
|
if (!sideStr.empty())
|
||||||
childElem.SetAttribute("side", sideStr);
|
childElem->SetAttribute("side", sideStr.c_str());
|
||||||
|
|
||||||
save_dock_layout(&childElem, subdock);
|
save_dock_layout(childElem, subdock);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Set the widget ID as the element name, e.g. <timeline />,
|
// Set the widget ID as the element name, e.g. <timeline />,
|
||||||
// <colorbar />, etc.
|
// <colorbar />, etc.
|
||||||
childElem.SetValue(child->id());
|
childElem->SetValue(child->id().c_str());
|
||||||
if (!sideStr.empty())
|
if (!sideStr.empty())
|
||||||
childElem.SetAttribute("side", sideStr);
|
childElem->SetAttribute("side", sideStr.c_str());
|
||||||
if (size.w) childElem.SetAttribute("width", size.w);
|
if (size.w) childElem->SetAttribute("width", size.w);
|
||||||
if (size.h) childElem.SetAttribute("height", size.h);
|
if (size.h) childElem->SetAttribute("height", size.h);
|
||||||
}
|
}
|
||||||
|
|
||||||
elem->InsertEndChild(childElem);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void load_dock_layout(const TiXmlElement* elem, Dock* dock)
|
static void load_dock_layout(const XMLElement* elem, Dock* dock)
|
||||||
{
|
{
|
||||||
const char* elemNameStr = elem->Value();
|
const char* elemNameStr = elem->Value();
|
||||||
if (!elemNameStr) {
|
if (!elemNameStr) {
|
||||||
@ -119,15 +119,14 @@ static void load_dock_layout(const TiXmlElement* elem, Dock* dock)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
LayoutPtr Layout::MakeFromXmlElement(const TiXmlElement* layoutElem)
|
LayoutPtr Layout::MakeFromXmlElement(const XMLElement* layoutElem)
|
||||||
{
|
{
|
||||||
auto layout = std::make_shared<Layout>();
|
auto layout = std::make_shared<Layout>();
|
||||||
if (auto name = layoutElem->Attribute("name")) {
|
if (auto name = layoutElem->Attribute("name")) {
|
||||||
layout->m_id = name;
|
layout->m_id = name;
|
||||||
layout->m_name = name;
|
layout->m_name = name;
|
||||||
}
|
}
|
||||||
|
layout->m_elem = layoutElem->DeepClone(&layout->m_dummyDoc)->ToElement();
|
||||||
layout->m_elem.reset(layoutElem->Clone()->ToElement());
|
|
||||||
return layout;
|
return layout;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,9 +139,9 @@ LayoutPtr Layout::MakeFromDock(const std::string& id,
|
|||||||
layout->m_id = id;
|
layout->m_id = id;
|
||||||
layout->m_name = name;
|
layout->m_name = name;
|
||||||
|
|
||||||
layout->m_elem = std::make_unique<TiXmlElement>("layout");
|
layout->m_elem = layout->m_dummyDoc.NewElement("layout");
|
||||||
layout->m_elem->SetAttribute("name", name);
|
layout->m_elem->SetAttribute("name", name.c_str());
|
||||||
save_dock_layout(layout->m_elem.get(), dock);
|
save_dock_layout(layout->m_elem, dock);
|
||||||
|
|
||||||
return layout;
|
return layout;
|
||||||
}
|
}
|
||||||
@ -163,7 +162,7 @@ bool Layout::loadLayout(Dock* dock) const
|
|||||||
if (!m_elem)
|
if (!m_elem)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
TiXmlElement* elem = m_elem->FirstChildElement();
|
XMLElement* elem = m_elem->FirstChildElement();
|
||||||
while (elem) {
|
while (elem) {
|
||||||
load_dock_layout(elem, dock);
|
load_dock_layout(elem, dock);
|
||||||
elem = elem->NextSiblingElement();
|
elem = elem->NextSiblingElement();
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class TiXmlElement;
|
#include "tinyxml2.h"
|
||||||
|
|
||||||
namespace app {
|
namespace app {
|
||||||
class Dock;
|
class Dock;
|
||||||
@ -24,14 +24,14 @@ namespace app {
|
|||||||
static constexpr const char* kDefault = "_default_";
|
static constexpr const char* kDefault = "_default_";
|
||||||
static constexpr const char* kMirroredDefault = "_mirrored_default_";
|
static constexpr const char* kMirroredDefault = "_mirrored_default_";
|
||||||
|
|
||||||
static LayoutPtr MakeFromXmlElement(const TiXmlElement* layoutElem);
|
static LayoutPtr MakeFromXmlElement(const tinyxml2::XMLElement* layoutElem);
|
||||||
static LayoutPtr MakeFromDock(const std::string& id,
|
static LayoutPtr MakeFromDock(const std::string& id,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const Dock* dock);
|
const Dock* dock);
|
||||||
|
|
||||||
const std::string& id() const { return m_id; }
|
const std::string& id() const { return m_id; }
|
||||||
const std::string& name() const { return m_name; }
|
const std::string& name() const { return m_name; }
|
||||||
const TiXmlElement* xmlElement() const { return m_elem.get(); }
|
const tinyxml2::XMLElement* xmlElement() const { return m_elem; }
|
||||||
|
|
||||||
bool matchId(const std::string& id) const;
|
bool matchId(const std::string& id) const;
|
||||||
bool loadLayout(Dock* dock) const;
|
bool loadLayout(Dock* dock) const;
|
||||||
@ -39,7 +39,8 @@ namespace app {
|
|||||||
private:
|
private:
|
||||||
std::string m_id;
|
std::string m_id;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
std::unique_ptr<TiXmlElement> m_elem;
|
tinyxml2::XMLDocument m_dummyDoc;
|
||||||
|
tinyxml2::XMLElement* m_elem = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace app
|
} // namespace app
|
||||||
|
@ -20,6 +20,8 @@
|
|||||||
|
|
||||||
namespace app {
|
namespace app {
|
||||||
|
|
||||||
|
using namespace tinyxml2;
|
||||||
|
|
||||||
Layouts::Layouts()
|
Layouts::Layouts()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -65,11 +67,11 @@ bool Layouts::addLayout(const LayoutPtr& layout)
|
|||||||
|
|
||||||
void Layouts::load(const std::string& fn)
|
void Layouts::load(const std::string& fn)
|
||||||
{
|
{
|
||||||
XmlDocumentRef doc = app::open_xml(fn);
|
XMLDocumentRef doc = app::open_xml(fn);
|
||||||
TiXmlHandle handle(doc.get());
|
XMLHandle handle(doc.get());
|
||||||
TiXmlElement* layoutElem = handle
|
XMLElement* layoutElem = handle
|
||||||
.FirstChild("layouts")
|
.FirstChildElement("layouts")
|
||||||
.FirstChild("layout").ToElement();
|
.FirstChildElement("layout").ToElement();
|
||||||
|
|
||||||
while (layoutElem) {
|
while (layoutElem) {
|
||||||
m_layouts.push_back(Layout::MakeFromXmlElement(layoutElem));
|
m_layouts.push_back(Layout::MakeFromXmlElement(layoutElem));
|
||||||
@ -79,16 +81,17 @@ void Layouts::load(const std::string& fn)
|
|||||||
|
|
||||||
void Layouts::save(const std::string& fn) const
|
void Layouts::save(const std::string& fn) const
|
||||||
{
|
{
|
||||||
XmlDocumentRef doc(new TiXmlDocument());
|
auto doc = std::make_unique<XMLDocument>();
|
||||||
TiXmlElement layoutsElem("layouts");
|
XMLElement* layoutsElem = doc->NewElement("layouts");
|
||||||
|
|
||||||
for (const auto& layout : m_layouts)
|
for (const auto& layout : m_layouts) {
|
||||||
layoutsElem.InsertEndChild(*layout->xmlElement());
|
layoutsElem->InsertEndChild(
|
||||||
|
layout->xmlElement()->DeepClone(doc.get()));
|
||||||
|
}
|
||||||
|
|
||||||
TiXmlDeclaration declaration("1.0", "utf-8", "");
|
doc->InsertEndChild(doc->NewDeclaration("xml version=\"1.0\" encoding=\"utf-8\""));
|
||||||
doc->InsertEndChild(declaration);
|
|
||||||
doc->InsertEndChild(layoutsElem);
|
doc->InsertEndChild(layoutsElem);
|
||||||
save_xml(doc, fn);
|
save_xml(doc.get(), fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
Loading…
Reference in New Issue
Block a user