Minor changes/refactors

This commit is contained in:
David Capello 2016-05-31 16:53:30 -03:00
parent e0d888d5d7
commit 19a43b27ab
4 changed files with 58 additions and 53 deletions

View File

@ -94,43 +94,6 @@ private:
FileOp* m_fop;
};
static void save_document_in_background(const Context* context,
const Document* document, bool mark_as_saved,
const std::string& fn_format)
{
base::UniquePtr<FileOp> fop(
FileOp::createSaveDocumentOperation(
context, document,
document->filename().c_str(), fn_format.c_str()));
if (!fop)
return;
SaveFileJob job(fop);
job.showProgressWindow();
if (fop->hasError()) {
Console console;
console.printf(fop->error().c_str());
// We don't know if the file was saved correctly or not. So mark
// it as it should be saved again.
const_cast<Document*>(document)->impossibleToBackToSavedState();
}
// If the job was cancelled, mark the document as modified.
else if (fop->isStop()) {
const_cast<Document*>(document)->impossibleToBackToSavedState();
}
else if (context->isUIAvailable()) {
App::instance()->recentFiles()->addRecentFile(document->filename().c_str());
if (mark_as_saved)
const_cast<Document*>(document)->markAsSaved();
StatusBar::instance()
->setStatusText(2000, "File %s, saved.",
document->name().c_str());
}
}
//////////////////////////////////////////////////////////////////////
SaveFileBaseCommand::SaveFileBaseCommand(const char* short_name, const char* friendly_name, CommandFlags flags)
@ -222,9 +185,7 @@ bool SaveFileBaseCommand::saveAsDialog(Context* context,
}
// Save the document
save_document_in_background(
context, const_cast<Document*>(document),
markAsSaved, m_filenameFormat);
saveDocumentInBackground(context, const_cast<Document*>(document), markAsSaved);
// Undo resize
if (undoResize) {
@ -246,6 +207,44 @@ bool SaveFileBaseCommand::saveAsDialog(Context* context,
return true;
}
void SaveFileBaseCommand::saveDocumentInBackground(const Context* context,
const app::Document* document,
bool markAsSaved) const
{
base::UniquePtr<FileOp> fop(
FileOp::createSaveDocumentOperation(
context, document,
document->filename().c_str(),
m_filenameFormat.c_str()));
if (!fop)
return;
SaveFileJob job(fop);
job.showProgressWindow();
if (fop->hasError()) {
Console console;
console.printf(fop->error().c_str());
// We don't know if the file was saved correctly or not. So mark
// it as it should be saved again.
const_cast<Document*>(document)->impossibleToBackToSavedState();
}
// If the job was cancelled, mark the document as modified.
else if (fop->isStop()) {
const_cast<Document*>(document)->impossibleToBackToSavedState();
}
else if (context->isUIAvailable()) {
App::instance()->recentFiles()->addRecentFile(document->filename().c_str());
if (markAsSaved)
const_cast<Document*>(document)->markAsSaved();
StatusBar::instance()
->setStatusText(2000, "File %s, saved.",
document->name().c_str());
}
}
//////////////////////////////////////////////////////////////////////
class SaveFileCommand : public SaveFileBaseCommand {
@ -274,9 +273,7 @@ void SaveFileCommand::onExecute(Context* context)
ContextWriter writer(context);
Document* documentWriter = writer.document();
save_document_in_background(
context, documentWriter, true,
m_filenameFormat.c_str());
saveDocumentInBackground(context, documentWriter, true);
}
// If the document isn't associated to a file, we must to show the
// save-as dialog to the user to select for first time the file-name

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2001-2015 David Capello
// Copyright (C) 2001-2016 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
@ -14,6 +14,7 @@
#include <string>
namespace app {
class Document;
class FileSelectorDelegate;
class SaveFileBaseCommand : public Command {
@ -30,6 +31,9 @@ namespace app {
bool saveAsDialog(Context* context, const char* dlgTitle,
FileSelectorDelegate* delegate = nullptr);
void saveDocumentInBackground(const Context* context,
const app::Document* document,
bool markAsSaved) const;
std::string m_filename;
std::string m_filenameFormat;

View File

@ -220,7 +220,7 @@ done:;
FileOp* FileOp::createSaveDocumentOperation(const Context* context,
const Document* document,
const char* filename,
const char* fn_format_arg)
const char* filenameFormatArg)
{
base::UniquePtr<FileOp> fop(
new FileOp(FileOpSave, const_cast<Context*>(context)));
@ -247,7 +247,7 @@ FileOp* FileOp::createSaveDocumentOperation(const Context* context,
std::string warnings;
bool fatal = false;
/* check image type support */
// Check image type support
switch (fop->m_document->sprite()->pixelFormat()) {
case IMAGE_RGB:
@ -381,7 +381,7 @@ FileOp* FileOp::createSaveDocumentOperation(const Context* context,
fop->prepareForSequence();
std::string fn = filename;
std::string fn_format = fn_format_arg;
std::string fn_format = filenameFormatArg;
bool default_format = false;
if (fn_format.empty()) {

View File

@ -19,6 +19,7 @@
#include <string>
#include <vector>
// Flags for FileOp::createLoadDocumentOperation()
#define FILE_LOAD_SEQUENCE_NONE 0x00000001
#define FILE_LOAD_SEQUENCE_ASK 0x00000002
#define FILE_LOAD_SEQUENCE_YES 0x00000004
@ -51,8 +52,7 @@ namespace app {
FileOpSave
} FileOpType;
class IFileOpProgress
{
class IFileOpProgress {
public:
virtual ~IFileOpProgress() { }
virtual void ackFileOpProgress(double progress) = 0;
@ -61,8 +61,14 @@ namespace app {
// Structure to load & save files.
class FileOp {
public:
static FileOp* createLoadDocumentOperation(Context* context, const char* filename, int flags);
static FileOp* createSaveDocumentOperation(const Context* context, const Document* document, const char* filename, const char* fn_format);
static FileOp* createLoadDocumentOperation(Context* context,
const char* filename,
int flags);
static FileOp* createSaveDocumentOperation(const Context* context,
const Document* document,
const char* filename,
const char* filenameFormat);
~FileOp();
@ -160,12 +166,10 @@ namespace app {
};
// Available extensions for each load/save operation.
std::string get_readable_extensions();
std::string get_writable_extensions();
// High-level routines to load/save documents.
app::Document* load_document(Context* context, const char* filename);
int save_document(Context* context, doc::Document* document);