From c9ab56cf69cef0501efdace082019c57ae42a631 Mon Sep 17 00:00:00 2001 From: David Capello Date: Sun, 10 Nov 2013 20:29:51 -0300 Subject: [PATCH] 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. --- src/app/ui/timeline.cpp | 31 +++++++++++++++++++++++++------ src/app/ui/timeline.h | 6 ++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/app/ui/timeline.cpp b/src/app/ui/timeline.cpp index 84c3bcd2d..78db884c1 100644 --- a/src/app/ui/timeline.cpp +++ b/src/app/ui/timeline.cpp @@ -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); diff --git a/src/app/ui/timeline.h b/src/app/ui/timeline.h index ca13d2e93..70c6b1770 100644 --- a/src/app/ui/timeline.h +++ b/src/app/ui/timeline.h @@ -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);