Timeline: we've to observe the Context to know if the Document is destroyed

In this way the timeline can stop observing the Document when it's closed.
This commit is contained in:
David Capello 2013-11-10 20:29:51 -03:00
parent d8fd4736d4
commit c9ab56cf69
2 changed files with 31 additions and 6 deletions

View File

@ -115,23 +115,28 @@ Timeline::Timeline()
, m_context(UIContext::instance())
, m_document(NULL)
{
m_context->addObserver(this);
}
Timeline::~Timeline()
{
if (m_document)
m_document->removeObserver(this);
detachDocument();
m_context->removeObserver(this);
}
void Timeline::updateUsingEditor(Editor* editor)
{
if (m_document)
m_document->removeObserver(this);
DocumentView* view = editor->getDocumentView();
DocumentLocation location;
view->getDocumentLocation(&location);
// Do nothing, we've already viewing this document in the timeline.
if (m_document == location.document())
return;
detachDocument();
m_document = location.document();
m_sprite = location.sprite();
m_layer = location.layer();
@ -147,8 +152,16 @@ void Timeline::updateUsingEditor(Editor* editor)
setFocusStop(true);
regenerateLayers();
}
m_document->addObserver(this);
void Timeline::detachDocument()
{
if (m_document) {
m_document->removeObserver(this);
m_document = NULL;
invalidate();
}
}
bool Timeline::isMovingCel() const
@ -739,6 +752,12 @@ void Timeline::onPreferredSize(PreferredSizeEvent& ev)
ev.setPreferredSize(Size(32, 32));
}
void Timeline::onRemoveDocument(Context* context, Document* document)
{
if (document == m_document)
detachDocument();
}
void Timeline::onAddLayer(DocumentEvent& ev)
{
ASSERT(ev.layer() != NULL);

View File

@ -19,6 +19,7 @@
#ifndef APP_UI_TIMELINE_H_INCLUDED
#define APP_UI_TIMELINE_H_INCLUDED
#include "app/context_observer.h"
#include "app/document_observer.h"
#include "base/compiler_specific.h"
#include "raster/frame_number.h"
@ -40,6 +41,7 @@ namespace app {
class Editor;
class Timeline : public ui::Widget
, public ContextObserver
, public DocumentObserver {
public:
enum State {
@ -78,7 +80,11 @@ namespace app {
void onRemoveFrame(DocumentEvent& ev) OVERRIDE;
void onTotalFramesChanged(DocumentEvent& ev) OVERRIDE;
// ContextObserver impl.
void onRemoveDocument(Context* context, Document* document) OVERRIDE;
private:
void detachDocument();
void setCursor(int x, int y);
void getDrawableLayers(const gfx::Rect& clip, int* first_layer, int* last_layer);
void getDrawableFrames(const gfx::Rect& clip, FrameNumber* first_frame, FrameNumber* last_frame);