Centralize legacy document file saves
This commit is contained in:
@@ -106,6 +106,21 @@ struct DocumentFileSavePlan {
|
||||
DocumentFileWriteDecision write_decision = DocumentFileWriteDecision::save_now;
|
||||
};
|
||||
|
||||
class DocumentFileSaveServices {
|
||||
public:
|
||||
virtual ~DocumentFileSaveServices() = default;
|
||||
|
||||
virtual void save_document_file(const DocumentFileSavePlan& plan) = 0;
|
||||
virtual void prompt_overwrite_document_file(const DocumentFileSavePlan& plan) = 0;
|
||||
};
|
||||
|
||||
class DocumentVersionSaveServices {
|
||||
public:
|
||||
virtual ~DocumentVersionSaveServices() = default;
|
||||
|
||||
virtual void save_document_version(const DocumentVersionTarget& target) = 0;
|
||||
};
|
||||
|
||||
struct NewDocumentPlan {
|
||||
DocumentFileTarget target;
|
||||
int resolution = 0;
|
||||
@@ -339,6 +354,22 @@ template <typename ExistsPredicate>
|
||||
return pp::foundation::Result<DocumentFileSavePlan>::success(std::move(plan));
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status execute_document_file_save_plan(
|
||||
const DocumentFileSavePlan& plan,
|
||||
DocumentFileSaveServices& services)
|
||||
{
|
||||
switch (plan.write_decision) {
|
||||
case DocumentFileWriteDecision::save_now:
|
||||
services.save_document_file(plan);
|
||||
return pp::foundation::Status::success();
|
||||
case DocumentFileWriteDecision::prompt_overwrite:
|
||||
services.prompt_overwrite_document_file(plan);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
return pp::foundation::Status::invalid_argument("unknown document file save write decision");
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr pp::foundation::Result<int> document_resolution_from_index(int index) noexcept
|
||||
{
|
||||
constexpr std::array<int, 6> resolutions{ 512, 1024, 1536, 2048, 4096, 8192 };
|
||||
@@ -471,4 +502,16 @@ template <typename ExistsPredicate>
|
||||
pp::foundation::Status::out_of_range("no available document version target"));
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status execute_document_version_save(
|
||||
const DocumentVersionTarget& target,
|
||||
DocumentVersionSaveServices& services)
|
||||
{
|
||||
if (target.name.empty() || target.path.empty()) {
|
||||
return pp::foundation::Status::invalid_argument("document version target requires a name and path");
|
||||
}
|
||||
|
||||
services.save_document_version(target);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -280,11 +280,9 @@ void App::dialog_save_ver()
|
||||
return;
|
||||
}
|
||||
|
||||
doc_name = target.value().name;
|
||||
doc_path = target.value().path;
|
||||
canvas->m_canvas->m_unsaved = true;
|
||||
title_update();
|
||||
canvas->m_canvas->project_save(doc_path);
|
||||
const auto status = pp::panopainter::execute_legacy_document_version_save(*this, target.value());
|
||||
if (!status.ok())
|
||||
LOG("Document version save action failed: %s", status.message);
|
||||
}
|
||||
|
||||
void App::save_document(pp::app::DocumentSaveIntent intent)
|
||||
@@ -332,34 +330,9 @@ void App::dialog_save()
|
||||
return;
|
||||
}
|
||||
|
||||
auto action = [this, dialog, plan = plan.value()] {
|
||||
canvas->m_canvas->project_save(plan.target.path);
|
||||
doc_name = plan.target.name;
|
||||
doc_path = plan.target.path;
|
||||
doc_dir = plan.target.directory;
|
||||
title_update();
|
||||
dialog->destroy();
|
||||
App::I->hideKeyboard();
|
||||
};
|
||||
|
||||
if (plan.value().write_decision == pp::app::DocumentFileWriteDecision::prompt_overwrite)
|
||||
{
|
||||
// ask confirm is file already exist
|
||||
auto msgbox = new NodeMessageBox();
|
||||
msgbox->set_manager(&layout);
|
||||
msgbox->init();
|
||||
msgbox->m_title->set_text("Warning");
|
||||
msgbox->m_message->set_text(("Are you sure you want to overwrite " + plan.value().target.name + "?").c_str());
|
||||
msgbox->btn_ok->on_click = [this, msgbox, action](Node*) {
|
||||
action();
|
||||
msgbox->destroy();
|
||||
};
|
||||
layout[main_id]->add_child(msgbox);
|
||||
}
|
||||
else
|
||||
{
|
||||
action();
|
||||
}
|
||||
const auto status = pp::panopainter::execute_legacy_document_file_save_plan(*this, plan.value(), dialog);
|
||||
if (!status.ok())
|
||||
LOG("Document file save action failed: %s", status.message);
|
||||
};
|
||||
dialog->btn_cancel->on_click = [this, dialog](Node*)
|
||||
{
|
||||
|
||||
@@ -71,6 +71,74 @@ private:
|
||||
std::shared_ptr<NodeDialogNewDoc> dialog_;
|
||||
};
|
||||
|
||||
void save_legacy_document_file(
|
||||
App& app,
|
||||
const pp::app::DocumentFileSavePlan& plan,
|
||||
const std::shared_ptr<NodeDialogSave>& dialog)
|
||||
{
|
||||
app.canvas->m_canvas->project_save(plan.target.path);
|
||||
app.doc_name = plan.target.name;
|
||||
app.doc_path = plan.target.path;
|
||||
app.doc_dir = plan.target.directory;
|
||||
app.title_update();
|
||||
dialog->destroy();
|
||||
App::I->hideKeyboard();
|
||||
}
|
||||
|
||||
class LegacyDocumentFileSaveServices final : public pp::app::DocumentFileSaveServices {
|
||||
public:
|
||||
LegacyDocumentFileSaveServices(App& app, std::shared_ptr<NodeDialogSave> dialog) noexcept
|
||||
: app_(app)
|
||||
, dialog_(std::move(dialog))
|
||||
{
|
||||
}
|
||||
|
||||
void save_document_file(const pp::app::DocumentFileSavePlan& plan) override
|
||||
{
|
||||
save_legacy_document_file(app_, plan, dialog_);
|
||||
}
|
||||
|
||||
void prompt_overwrite_document_file(const pp::app::DocumentFileSavePlan& plan) override
|
||||
{
|
||||
auto msgbox = new NodeMessageBox();
|
||||
msgbox->set_manager(&app_.layout);
|
||||
msgbox->init();
|
||||
msgbox->m_title->set_text("Warning");
|
||||
msgbox->m_message->set_text(("Are you sure you want to overwrite " + plan.target.name + "?").c_str());
|
||||
auto* app = &app_;
|
||||
auto dialog = dialog_;
|
||||
msgbox->btn_ok->on_click = [app, msgbox, dialog, plan](Node*) {
|
||||
save_legacy_document_file(*app, plan, dialog);
|
||||
msgbox->destroy();
|
||||
};
|
||||
app_.layout[app_.main_id]->add_child(msgbox);
|
||||
}
|
||||
|
||||
private:
|
||||
App& app_;
|
||||
std::shared_ptr<NodeDialogSave> dialog_;
|
||||
};
|
||||
|
||||
class LegacyDocumentVersionSaveServices final : public pp::app::DocumentVersionSaveServices {
|
||||
public:
|
||||
explicit LegacyDocumentVersionSaveServices(App& app) noexcept
|
||||
: app_(app)
|
||||
{
|
||||
}
|
||||
|
||||
void save_document_version(const pp::app::DocumentVersionTarget& target) override
|
||||
{
|
||||
app_.doc_name = target.name;
|
||||
app_.doc_path = target.path;
|
||||
app_.canvas->m_canvas->m_unsaved = true;
|
||||
app_.title_update();
|
||||
app_.canvas->m_canvas->project_save(app_.doc_path);
|
||||
}
|
||||
|
||||
private:
|
||||
App& app_;
|
||||
};
|
||||
|
||||
class LegacyCloseRequestServices final : public pp::app::CloseRequestServices {
|
||||
public:
|
||||
LegacyCloseRequestServices(App& app, bool& dialog_already_opened) noexcept
|
||||
@@ -213,4 +281,21 @@ pp::foundation::Status execute_legacy_new_document_plan(
|
||||
return pp::app::execute_new_document_plan(plan, services);
|
||||
}
|
||||
|
||||
pp::foundation::Status execute_legacy_document_file_save_plan(
|
||||
App& app,
|
||||
const pp::app::DocumentFileSavePlan& plan,
|
||||
std::shared_ptr<NodeDialogSave> dialog)
|
||||
{
|
||||
LegacyDocumentFileSaveServices services(app, std::move(dialog));
|
||||
return pp::app::execute_document_file_save_plan(plan, services);
|
||||
}
|
||||
|
||||
pp::foundation::Status execute_legacy_document_version_save(
|
||||
App& app,
|
||||
const pp::app::DocumentVersionTarget& target)
|
||||
{
|
||||
LegacyDocumentVersionSaveServices services(app);
|
||||
return pp::app::execute_document_version_save(target, services);
|
||||
}
|
||||
|
||||
} // namespace pp::panopainter
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
class App;
|
||||
class NodeDialogNewDoc;
|
||||
class NodeDialogSave;
|
||||
|
||||
namespace pp::panopainter {
|
||||
|
||||
@@ -30,4 +31,13 @@ namespace pp::panopainter {
|
||||
const pp::app::NewDocumentPlan& plan,
|
||||
std::shared_ptr<NodeDialogNewDoc> dialog);
|
||||
|
||||
[[nodiscard]] pp::foundation::Status execute_legacy_document_file_save_plan(
|
||||
App& app,
|
||||
const pp::app::DocumentFileSavePlan& plan,
|
||||
std::shared_ptr<NodeDialogSave> dialog);
|
||||
|
||||
[[nodiscard]] pp::foundation::Status execute_legacy_document_version_save(
|
||||
App& app,
|
||||
const pp::app::DocumentVersionTarget& target);
|
||||
|
||||
} // namespace pp::panopainter
|
||||
|
||||
Reference in New Issue
Block a user