Fix crash changing ConfigureTools options when activeDoc == NULL

This commit is contained in:
David Capello 2014-11-24 20:52:52 -03:00
parent 2de3f7caff
commit a00a3b2a83
2 changed files with 140 additions and 178 deletions

View File

@ -1,7 +1,7 @@
<!-- ASEPRITE --> <!-- ASEPRITE -->
<!-- Copyright (C) 2001-2013 by David Capello --> <!-- Copyright (C) 2001-2014 by David Capello -->
<gui> <gui>
<window text="Tools Configuration" id="configure_tool"> <window text="Tools Configuration" id="tools_configuration">
<box vertical="true" childspacing="0"> <box vertical="true" childspacing="0">
<box horizontal="true" expansive="true"> <box horizontal="true" expansive="true">
<box vertical="true" childspacing="2"> <box vertical="true" childspacing="2">

View File

@ -1,5 +1,5 @@
/* Aseprite /* Aseprite
* Copyright (C) 2001-2013 David Capello * Copyright (C) 2001-2014 David Capello
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -41,140 +41,80 @@
#include "raster/mask.h" #include "raster/mask.h"
#include "ui/ui.h" #include "ui/ui.h"
#include "generated_tools_configuration.h"
namespace app { namespace app {
using namespace gfx; using namespace gfx;
using namespace ui; using namespace ui;
using namespace app::tools; using namespace app::tools;
static Window* window = NULL; class ToolsConfigurationWindow : public app::gen::ToolsConfiguration,
// Slot for App::Exit signal
static void on_exit_delete_this_widget()
{
ASSERT(window != NULL);
delete window;
}
class ConfigureTools : public Command,
public doc::ContextObserver { public doc::ContextObserver {
public: public:
ConfigureTools(); ToolsConfigurationWindow(Context* ctx) : m_ctx(ctx) {
Command* clone() const override { return new ConfigureTools(*this); } m_ctx->addObserver(this);
protected: // Slots
void onExecute(Context* context) override; this->Close.connect(Bind<void>(&ToolsConfigurationWindow::onWindowClose, this));
void onSetActiveDocument(doc::Document* document) override; tiled()->Click.connect(Bind<void>(&ToolsConfigurationWindow::onTiledClick, this));
tiledX()->Click.connect(Bind<void>(&ToolsConfigurationWindow::onTiledXYClick, this, filters::TILED_X_AXIS, tiledX()));
tiledY()->Click.connect(Bind<void>(&ToolsConfigurationWindow::onTiledXYClick, this, filters::TILED_Y_AXIS, tiledY()));
viewGrid()->Click.connect(Bind<void>(&ToolsConfigurationWindow::onViewGridClick, this));
pixelGrid()->Click.connect(Bind<void>(&ToolsConfigurationWindow::onPixelGridClick, this));
setGrid()->Click.connect(Bind<void>(&ToolsConfigurationWindow::onSetGridClick, this));
snapToGrid()->Click.connect(Bind<void>(&ToolsConfigurationWindow::onSnapToGridClick, this));
remapWindow();
centerWindow();
load_window_pos(this, "ConfigureTool");
onSetActiveDocument(m_ctx->activeDocument());
}
~ToolsConfigurationWindow() {
m_ctx->removeObserver(this);
}
private: private:
CheckBox* m_tiled; ISettings* settings() {
CheckBox* m_tiledX; ASSERT(m_ctx);
CheckBox* m_tiledY; return m_ctx->settings();
CheckBox* m_pixelGrid;
CheckBox* m_snapToGrid;
CheckBox* m_viewGrid;
ISettings* m_settings;
IDocumentSettings* m_docSettings;
void onWindowClose();
void onTiledClick();
void onTiledXYClick(int tiled_axis, CheckBox* checkbox);
void onViewGridClick();
void onPixelGridClick();
void onSetGridClick();
void onSnapToGridClick();
};
ConfigureTools::ConfigureTools()
: Command("ConfigureTools",
"Configure Tools",
CmdUIOnlyFlag)
{
m_settings = NULL;
m_docSettings = NULL;
} }
void ConfigureTools::onExecute(Context* context) IDocumentSettings* docSettings() {
{ ASSERT(settings());
m_settings = context->settings(); return settings()->getDocumentSettings(m_ctx->activeDocument());
m_docSettings = NULL;
Button* set_grid;
bool first_time = false;
if (!window) {
window = app::load_widget<Window>("tools_configuration.xml", "configure_tool");
first_time = true;
}
// If the window is opened, close it
else if (window->isVisible()) {
context->removeObserver(this);
window->closeWindow(NULL);
return;
} }
context->addObserver(this); void onSetActiveDocument(doc::Document* document) override {
IDocumentSettings* docSettings = this->docSettings();
try { tiled()->setSelected(docSettings->getTiledMode() != filters::TILED_NONE);
m_tiled = app::find_widget<CheckBox>(window, "tiled"); tiledX()->setSelected(docSettings->getTiledMode() & filters::TILED_X_AXIS ? true: false);
m_tiledX = app::find_widget<CheckBox>(window, "tiled_x"); tiledY()->setSelected(docSettings->getTiledMode() & filters::TILED_Y_AXIS ? true: false);
m_tiledY = app::find_widget<CheckBox>(window, "tiled_y"); snapToGrid()->setSelected(docSettings->getSnapToGrid());
m_snapToGrid = app::find_widget<CheckBox>(window, "snap_to_grid"); viewGrid()->setSelected(docSettings->getGridVisible());
m_viewGrid = app::find_widget<CheckBox>(window, "view_grid"); pixelGrid()->setSelected(docSettings->getPixelGridVisible());
m_pixelGrid = app::find_widget<CheckBox>(window, "pixel_grid");
set_grid = app::find_widget<Button>(window, "set_grid");
}
catch (...) {
delete window;
window = NULL;
throw;
} }
onSetActiveDocument(context->activeDocument()); void onWindowClose() {
save_window_pos(this, "ConfigureTool");
if (first_time) {
// Slots
window->Close.connect(Bind<void>(&ConfigureTools::onWindowClose, this));
m_tiled->Click.connect(Bind<void>(&ConfigureTools::onTiledClick, this));
m_tiledX->Click.connect(Bind<void>(&ConfigureTools::onTiledXYClick, this, filters::TILED_X_AXIS, m_tiledX));
m_tiledY->Click.connect(Bind<void>(&ConfigureTools::onTiledXYClick, this, filters::TILED_Y_AXIS, m_tiledY));
m_viewGrid->Click.connect(Bind<void>(&ConfigureTools::onViewGridClick, this));
m_pixelGrid->Click.connect(Bind<void>(&ConfigureTools::onPixelGridClick, this));
set_grid->Click.connect(Bind<void>(&ConfigureTools::onSetGridClick, this));
m_snapToGrid->Click.connect(Bind<void>(&ConfigureTools::onSnapToGridClick, this));
App::instance()->Exit.connect(&on_exit_delete_this_widget);
} }
// Default position void onTiledClick() {
window->remapWindow(); bool flag = tiled()->isSelected();
window->centerWindow();
// Load window configuration docSettings()->setTiledMode(
load_window_pos(window, "ConfigureTool"); flag ? filters::TILED_BOTH:
window->openWindow();
}
void ConfigureTools::onWindowClose()
{
save_window_pos(window, "ConfigureTool");
}
void ConfigureTools::onTiledClick()
{
bool flag = m_tiled->isSelected();
m_docSettings->setTiledMode(flag ? filters::TILED_BOTH:
filters::TILED_NONE); filters::TILED_NONE);
m_tiledX->setSelected(flag); tiledX()->setSelected(flag);
m_tiledY->setSelected(flag); tiledY()->setSelected(flag);
} }
void ConfigureTools::onTiledXYClick(int tiled_axis, CheckBox* checkbox) void onTiledXYClick(int tiled_axis, CheckBox* checkbox) {
{ int tiled_mode = docSettings()->getTiledMode();
int tiled_mode = m_docSettings->getTiledMode();
if (checkbox->isSelected()) if (checkbox->isSelected())
tiled_mode |= tiled_axis; tiled_mode |= tiled_axis;
@ -183,26 +123,18 @@ void ConfigureTools::onTiledXYClick(int tiled_axis, CheckBox* checkbox)
checkbox->findSibling("tiled")->setSelected(tiled_mode != filters::TILED_NONE); checkbox->findSibling("tiled")->setSelected(tiled_mode != filters::TILED_NONE);
m_docSettings->setTiledMode((filters::TiledMode)tiled_mode); docSettings()->setTiledMode((filters::TiledMode)tiled_mode);
} }
void ConfigureTools::onSnapToGridClick() void onViewGridClick() {
{ docSettings()->setGridVisible(viewGrid()->isSelected());
m_docSettings->setSnapToGrid(m_snapToGrid->isSelected());
} }
void ConfigureTools::onViewGridClick() void onPixelGridClick() {
{ docSettings()->setPixelGridVisible(pixelGrid()->isSelected());
m_docSettings->setGridVisible(m_viewGrid->isSelected());
} }
void ConfigureTools::onPixelGridClick() void onSetGridClick() {
{
m_docSettings->setPixelGridVisible(m_pixelGrid->isSelected());
}
void ConfigureTools::onSetGridClick()
{
try { try {
// TODO use the same context as in ConfigureTools::onExecute // TODO use the same context as in ConfigureTools::onExecute
const ContextReader reader(UIContext::instance()); const ContextReader reader(UIContext::instance());
@ -211,7 +143,7 @@ void ConfigureTools::onSetGridClick()
if (document && document->isMaskVisible()) { if (document && document->isMaskVisible()) {
const Mask* mask(document->mask()); const Mask* mask(document->mask());
m_docSettings->setGridBounds(mask->bounds()); docSettings()->setGridBounds(mask->bounds());
} }
else { else {
Command* grid_settings_cmd = Command* grid_settings_cmd =
@ -225,22 +157,52 @@ void ConfigureTools::onSetGridClick()
} }
} }
void ConfigureTools::onSetActiveDocument(doc::Document* document) void onSnapToGridClick() {
docSettings()->setSnapToGrid(snapToGrid()->isSelected());
}
Context* m_ctx;
};
class ConfigureTools : public Command {
public:
ConfigureTools();
Command* clone() const override { return new ConfigureTools(*this); }
protected:
void onExecute(Context* context) override;
};
static ToolsConfigurationWindow* window;
ConfigureTools::ConfigureTools()
: Command("ConfigureTools",
"Configure Tools",
CmdUIOnlyFlag)
{ {
if (!document) }
return;
m_docSettings = m_settings->getDocumentSettings(document); // Slot for App::Exit signal
ASSERT(m_docSettings); static void on_exit_delete_this_widget()
if (!m_docSettings) {
return; ASSERT(window != NULL);
delete window;
}
m_tiled->setSelected(m_docSettings->getTiledMode() != filters::TILED_NONE); void ConfigureTools::onExecute(Context* context)
m_tiledX->setSelected(m_docSettings->getTiledMode() & filters::TILED_X_AXIS ? true: false); {
m_tiledY->setSelected(m_docSettings->getTiledMode() & filters::TILED_Y_AXIS ? true: false); if (!window) {
m_snapToGrid->setSelected(m_docSettings->getSnapToGrid()); window = new ToolsConfigurationWindow(context);
m_viewGrid->setSelected(m_docSettings->getGridVisible());
m_pixelGrid->setSelected(m_docSettings->getPixelGridVisible()); App::instance()->Exit.connect(&on_exit_delete_this_widget);
}
// If the window is opened, close it
else if (window->isVisible()) {
window->closeWindow(NULL);
return;
}
window->openWindow();
} }
Command* CommandFactory::createConfigureToolsCommand() Command* CommandFactory::createConfigureToolsCommand()