Use ContextObserver to update the StatusBar

Instead of calling StatusBar::updateUsingEditor(Editor*), now the StatusBar
is a ContextObserver that observe changes in the active doc::Site. The
Editor notifies a ActiveSiteChange event when the active frame/layer
is changed.

Fix #657
This commit is contained in:
David Capello 2015-05-05 13:24:26 -03:00
parent c3d7a96a87
commit a489efdf03
4 changed files with 46 additions and 53 deletions

View File

@ -267,19 +267,28 @@ void Editor::setLayer(const Layer* layer)
m_layer = const_cast<Layer*>(layer); m_layer = const_cast<Layer*>(layer);
m_observers.notifyAfterLayerChanged(this); m_observers.notifyAfterLayerChanged(this);
// The active layer has changed.
if (isActive())
UIContext::instance()->notifyActiveSiteChanged();
updateStatusBar(); updateStatusBar();
} }
void Editor::setFrame(frame_t frame) void Editor::setFrame(frame_t frame)
{ {
if (m_frame != frame) { if (m_frame == frame)
m_observers.notifyBeforeFrameChanged(this); return;
m_frame = frame;
m_observers.notifyAfterFrameChanged(this);
invalidate(); m_observers.notifyBeforeFrameChanged(this);
updateStatusBar(); m_frame = frame;
} m_observers.notifyAfterFrameChanged(this);
// The active frame has changed.
if (isActive())
UIContext::instance()->notifyActiveSiteChanged();
invalidate();
updateStatusBar();
} }
void Editor::getSite(Site* site) const void Editor::getSite(Site* site) const

View File

@ -197,10 +197,13 @@ StatusBar::StatusBar()
tooltipManager->addTooltipFor(m_slider, "Cel Opacity", JI_BOTTOM); tooltipManager->addTooltipFor(m_slider, "Cel Opacity", JI_BOTTOM);
App::instance()->CurrentToolChange.connect(&StatusBar::onCurrentToolChange, this); App::instance()->CurrentToolChange.connect(&StatusBar::onCurrentToolChange, this);
UIContext::instance()->addObserver(this);
} }
StatusBar::~StatusBar() StatusBar::~StatusBar()
{ {
UIContext::instance()->removeObserver(this);
delete m_tipwindow; // widget delete m_tipwindow; // widget
delete m_commandsBox; delete m_commandsBox;
} }
@ -223,9 +226,6 @@ void StatusBar::clearText()
bool StatusBar::setStatusText(int msecs, const char *format, ...) bool StatusBar::setStatusText(int msecs, const char *format, ...)
{ {
// TODO this call should be in an observer of the "current frame" property changes.
updateCurrentFrame(current_editor);
if ((ui::clock() > m_timeout) || (msecs > 0)) { if ((ui::clock() > m_timeout) || (msecs > 0)) {
char buf[256]; // TODO warning buffer overflow char buf[256]; // TODO warning buffer overflow
va_list ap; va_list ap;
@ -408,12 +408,6 @@ void StatusBar::onPaint(ui::PaintEvent& ev)
} }
} }
void StatusBar::updateUsingEditor(Editor* editor)
{
updateFromDocument(editor);
updateCurrentFrame(editor);
}
bool StatusBar::CustomizedTipWindow::onProcessMessage(Message* msg) bool StatusBar::CustomizedTipWindow::onProcessMessage(Message* msg)
{ {
switch (msg->type()) { switch (msg->type()) {
@ -452,50 +446,40 @@ void StatusBar::onCelOpacityChange()
} }
} }
void StatusBar::updateFromDocument(Editor* editor) void StatusBar::onActiveSiteChange(const doc::Site& site)
{ {
try { if (site.document() && site.sprite()) {
if (editor && editor->document()) { m_hasDoc = true;
const DocumentReader reader(editor->document(), 100); m_commandsBox->setVisible(true);
m_hasDoc = true;
m_commandsBox->setVisible(true);
// Cel opacity // Current frame
const Cel* cel; m_currentFrame->setTextf("%d", site.frame()+1);
if (editor->sprite()->supportAlpha() &&
editor->layer() && // Cel opacity
editor->layer()->isImage() && const Cel* cel;
!editor->layer()->isBackground() && if (site.sprite()->supportAlpha() &&
(cel = editor->layer()->cel(editor->frame()))) { site.layer() &&
m_slider->setValue(MID(0, cel->opacity(), 255)); site.layer()->isImage() &&
m_slider->setEnabled(true); !site.layer()->isBackground() &&
} (cel = site.layer()->cel(site.frame()))) {
else { m_slider->setValue(MID(0, cel->opacity(), 255));
m_slider->setValue(255); m_slider->setEnabled(true);
m_slider->setEnabled(false);
}
} }
else { else {
m_hasDoc = false; m_slider->setValue(255);
m_commandsBox->setVisible(false); m_slider->setEnabled(false);
} }
} }
catch (const LockedDocumentException&) { else {
m_slider->setEnabled(false); m_hasDoc = false;
m_commandsBox->setVisible(false);
} }
} }
void StatusBar::updateCurrentFrame(Editor* editor)
{
if (editor && editor->sprite())
m_currentFrame->setTextf("%d", editor->frame()+1);
}
void StatusBar::newFrame() void StatusBar::newFrame()
{ {
Command* cmd = CommandsModule::instance()->getCommandByName(CommandId::NewFrame); Command* cmd = CommandsModule::instance()->getCommandByName(CommandId::NewFrame);
UIContext::instance()->executeCommand(cmd); UIContext::instance()->executeCommand(cmd);
updateCurrentFrame(current_editor);
} }
} // namespace app } // namespace app

View File

@ -11,6 +11,7 @@
#include "app/color.h" #include "app/color.h"
#include "base/observers.h" #include "base/observers.h"
#include "doc/context_observer.h"
#include "doc/layer_index.h" #include "doc/layer_index.h"
#include "ui/base.h" #include "ui/base.h"
#include "ui/widget.h" #include "ui/widget.h"
@ -36,7 +37,8 @@ namespace app {
class Tool; class Tool;
} }
class StatusBar : public ui::Widget { class StatusBar : public ui::Widget
, public doc::ContextObserver {
static StatusBar* m_instance; static StatusBar* m_instance;
public: public:
static StatusBar* instance() { return m_instance; } static StatusBar* instance() { return m_instance; }
@ -51,18 +53,17 @@ namespace app {
void showColor(int msecs, const char* text, const Color& color, int alpha); void showColor(int msecs, const char* text, const Color& color, int alpha);
void showTool(int msecs, tools::Tool* tool); void showTool(int msecs, tools::Tool* tool);
void updateUsingEditor(Editor* editor);
protected: protected:
void onResize(ui::ResizeEvent& ev) override; void onResize(ui::ResizeEvent& ev) override;
void onPreferredSize(ui::PreferredSizeEvent& ev) override; void onPreferredSize(ui::PreferredSizeEvent& ev) override;
void onPaint(ui::PaintEvent& ev) override; void onPaint(ui::PaintEvent& ev) override;
// ContextObserver impl
void onActiveSiteChange(const doc::Site& site) override;
private: private:
void onCurrentToolChange(); void onCurrentToolChange();
void onCelOpacityChange(); void onCelOpacityChange();
void updateFromDocument(Editor* editor);
void updateCurrentFrame(Editor* editor);
void newFrame(); void newFrame();
enum State { SHOW_TEXT, SHOW_COLOR, SHOW_TOOL }; enum State { SHOW_TEXT, SHOW_COLOR, SHOW_TOOL };

View File

@ -97,7 +97,6 @@ void UIContext::setActiveView(DocumentView* docView)
mainWin->getPreviewEditor()->updateUsingEditor(current_editor); mainWin->getPreviewEditor()->updateUsingEditor(current_editor);
mainWin->getTimeline()->updateUsingEditor(current_editor); mainWin->getTimeline()->updateUsingEditor(current_editor);
StatusBar::instance()->updateUsingEditor(current_editor);
// Change the image-type of color bar. // Change the image-type of color bar.
ColorBar::instance()->setPixelFormat(app_get_current_pixel_format()); ColorBar::instance()->setPixelFormat(app_get_current_pixel_format());