Write-lock the Document only when we press OK button

in LayerProperties, CanvasSize, LoadMask, and SaveFile commands.
This commit is contained in:
David Capello 2011-03-26 23:41:37 -03:00
parent e6b1b1ffa8
commit 6395ad3817
4 changed files with 54 additions and 36 deletions

View File

@ -61,8 +61,8 @@ bool CanvasSizeCommand::onEnabled(Context* context)
void CanvasSizeCommand::onExecute(Context* context)
{
ActiveDocumentWriter document(context);
Sprite* sprite(document->getSprite());
const ActiveDocumentReader document(context);
const Sprite* sprite(document->getSprite());
if (context->isUiAvailable()) {
JWidget left, top, right, bottom, ok;
@ -109,14 +109,17 @@ void CanvasSizeCommand::onExecute(Context* context)
if (y2 <= y1) y2 = y1+1;
{
UndoTransaction undoTransaction(document, "Canvas Size");
DocumentWriter documentWriter(document);
UndoTransaction undoTransaction(documentWriter, "Canvas Size");
int bgcolor = color_utils::color_for_image(context->getSettings()->getBgColor(), sprite->getImgType());
bgcolor = color_utils::fixup_color_for_background(sprite->getImgType(), bgcolor);
undoTransaction.cropSprite(x1, y1, x2-x1, y2-y1, bgcolor);
undoTransaction.commit();
documentWriter->generateMaskBoundaries();
}
document->generateMaskBoundaries();
update_screen_for_document(document);
}

View File

@ -61,9 +61,9 @@ bool LayerPropertiesCommand::onEnabled(Context* context)
void LayerPropertiesCommand::onExecute(Context* context)
{
ActiveDocumentWriter document(context);
Sprite* sprite(document->getSprite());
Layer* layer = sprite->getCurrentLayer();
const ActiveDocumentReader document(context);
const Sprite* sprite(document->getSprite());
const Layer* layer = sprite->getCurrentLayer();
FramePtr window(new Frame(false, "Layer Properties"));
Box* box1 = new Box(JI_VERTICAL);
@ -94,7 +94,9 @@ void LayerPropertiesCommand::onExecute(Context* context)
window->open_window_fg();
if (window->get_killer() == button_ok) {
layer->setName(entry_name->getText());
DocumentWriter documentWriter(document);
const_cast<Layer*>(layer)->setName(entry_name->getText());
update_screen_for_document(document);
}

View File

@ -60,15 +60,12 @@ void LoadMaskCommand::onLoadParams(Params* params)
bool LoadMaskCommand::onEnabled(Context* context)
{
const ActiveDocumentReader document(context);
const Sprite* sprite(document ? document->getSprite(): 0);
return sprite != NULL;
return document != NULL;
}
void LoadMaskCommand::onExecute(Context* context)
{
ActiveDocumentWriter document(context);
Sprite* sprite(document->getSprite());
undo::UndoHistory* undo(document->getUndoHistory());
const ActiveDocumentReader document(context);
base::string filename = m_filename;
@ -85,17 +82,23 @@ void LoadMaskCommand::onExecute(Context* context)
throw base::Exception("Error loading .msk file: %s",
static_cast<const char*>(m_filename.c_str()));
// Add the mask change into the undo history.
if (undo->isEnabled()) {
undo->setLabel("Mask Load");
undo->setModification(undo::DoesntModifyDocument);
undo->undo_set_mask(document);
{
DocumentWriter documentWriter(document);
undo::UndoHistory* undo(documentWriter->getUndoHistory());
// Add the mask change into the undo history.
if (undo->isEnabled()) {
undo->setLabel("Mask Load");
undo->setModification(undo::DoesntModifyDocument);
undo->undo_set_mask(documentWriter);
}
documentWriter->setMask(mask);
mask_free(mask);
documentWriter->generateMaskBoundaries();
}
document->setMask(mask);
mask_free(mask);
document->generateMaskBoundaries();
update_screen_for_document(document);
}

View File

@ -139,15 +139,11 @@ static void save_document_in_background(Document* document, bool mark_as_saved)
delete data->progress;
fop_free(fop);
delete data;
// Update the tab for the document. In this moment, the document is
// already marked as saved, so the * is not shown in the tab.
app_update_document_tab(document);
}
/*********************************************************************/
static void save_as_dialog(Document* document, const char* dlg_title, bool mark_as_saved)
static void save_as_dialog(const DocumentReader& document, const char* dlg_title, bool mark_as_saved)
{
char exts[4096];
base::string filename;
@ -183,11 +179,15 @@ static void save_as_dialog(Document* document, const char* dlg_title, bool mark_
/* "no": we must back to select other file-name */
}
// Change the document file name
document->setFilename(filename.c_str());
{
DocumentWriter documentWriter(document);
// Save the document
save_document_in_background(document, mark_as_saved);
// Change the document file name
documentWriter->setFilename(filename.c_str());
// Save the document
save_document_in_background(documentWriter, mark_as_saved);
}
}
//////////////////////////////////////////////////////////////////////
@ -223,12 +223,14 @@ bool SaveFileCommand::onEnabled(Context* context)
// [main thread]
void SaveFileCommand::onExecute(Context* context)
{
ActiveDocumentWriter document(context);
const ActiveDocumentReader document(context);
// If the document is associated to a file in the file-system, we can
// save it directly without user interaction.
if (document->isAssociatedToFile()) {
save_document_in_background(document, true);
DocumentWriter documentWriter(document);
save_document_in_background(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
@ -236,6 +238,8 @@ void SaveFileCommand::onExecute(Context* context)
else {
save_as_dialog(document, "Save File", true);
}
update_screen_for_document(document);
}
//////////////////////////////////////////////////////////////////////
@ -268,8 +272,9 @@ bool SaveFileAsCommand::onEnabled(Context* context)
void SaveFileAsCommand::onExecute(Context* context)
{
ActiveDocumentWriter document(context);
const ActiveDocumentReader document(context);
save_as_dialog(document, "Save As", true);
update_screen_for_document(document);
}
//////////////////////////////////////////////////////////////////////
@ -302,14 +307,19 @@ bool SaveFileCopyAsCommand::onEnabled(Context* context)
void SaveFileCopyAsCommand::onExecute(Context* context)
{
ActiveDocumentWriter document(context);
const ActiveDocumentReader document(context);
base::string old_filename = document->getFilename();
// show "Save As" dialog
save_as_dialog(document, "Save Copy As", false);
// Restore the file name.
document->setFilename(old_filename.c_str());
{
DocumentWriter documentWriter(document);
documentWriter->setFilename(old_filename.c_str());
}
update_screen_for_document(document);
}
//////////////////////////////////////////////////////////////////////