Code refactoring for Highlighter class

This commit is contained in:
YuSanka 2021-12-22 12:30:19 +01:00
parent f2aeca3a71
commit c767781827
6 changed files with 89 additions and 40 deletions

View File

@ -52,9 +52,6 @@ PreferencesDialog::PreferencesDialog(wxWindow* parent) :
build(); build();
m_highlighter.set_timer_owner(this, 0); m_highlighter.set_timer_owner(this, 0);
this->Bind(wxEVT_TIMER, [this](wxTimerEvent&) {
m_highlighter.blink();
});
} }
void PreferencesDialog::show(const std::string& highlight_opt_key /*= std::string()*/, const std::string& tab_name/*= std::string()*/) void PreferencesDialog::show(const std::string& highlight_opt_key /*= std::string()*/, const std::string& tab_name/*= std::string()*/)

View File

@ -71,9 +71,8 @@ protected:
void init_highlighter(const t_config_option_key& opt_key); void init_highlighter(const t_config_option_key& opt_key);
std::vector<ConfigOptionsGroup*> optgroups(); std::vector<ConfigOptionsGroup*> optgroups();
Highlighter m_highlighter; HighlighterForWx m_highlighter;
std::map<std::string, BlinkingBitmap*> m_blinkers;
std::map<std::string, BlinkingBitmap*> m_blinkers;
}; };
} // GUI } // GUI

View File

@ -85,10 +85,6 @@ Tab::Tab(wxBookCtrlBase* parent, const wxString& title, Preset::Type type) :
})); }));
m_highlighter.set_timer_owner(this, 0); m_highlighter.set_timer_owner(this, 0);
this->Bind(wxEVT_TIMER, [this](wxTimerEvent&)
{
m_highlighter.blink();
});
} }
void Tab::set_type() void Tab::set_type()

View File

@ -218,7 +218,7 @@ protected:
bool m_completed { false }; bool m_completed { false };
ConfigOptionMode m_mode = comExpert; // to correct first Tab update_visibility() set mode to Expert ConfigOptionMode m_mode = comExpert; // to correct first Tab update_visibility() set mode to Expert
Highlighter m_highlighter; HighlighterForWx m_highlighter;
DynamicPrintConfig m_cache_config; DynamicPrintConfig m_cache_config;

View File

@ -983,19 +983,48 @@ void BlinkingBitmap::blink()
namespace Slic3r { namespace Slic3r {
namespace GUI { namespace GUI {
void Highlighter::set_timer_owner(wxEvtHandler* owner, int timerid/* = wxID_ANY*/) void Highlighter::set_timer_owner(wxWindow* owner, int timerid/* = wxID_ANY*/)
{ {
m_timer.SetOwner(owner, timerid); m_timer.SetOwner(owner, timerid);
bind_timer(owner);
} }
void Highlighter::init(std::pair<OG_CustomCtrl*, bool*> params) bool Highlighter::init(bool input_failed)
{ {
if (m_timer.IsRunning()) if (input_failed)
invalidate(); return false;
if (!params.first || !params.second)
return;
m_timer.Start(300, false); m_timer.Start(300, false);
return true;
}
void Highlighter::invalidate()
{
if (m_timer.IsRunning())
m_timer.Stop();
m_blink_counter = 0;
}
void Highlighter::blink()
{
if ((++m_blink_counter) == 11)
invalidate();
}
// HighlighterForWx
void HighlighterForWx::bind_timer(wxWindow* owner)
{
owner->Bind(wxEVT_TIMER, [this](wxTimerEvent&) {
blink();
});
}
// using OG_CustomCtrl where arrow will be rendered and flag indicated "show/hide" state of this arrow
void HighlighterForWx::init(std::pair<OG_CustomCtrl*, bool*> params)
{
invalidate();
if (!Highlighter::init(!params.first && !params.second))
return;
m_custom_ctrl = params.first; m_custom_ctrl = params.first;
m_show_blink_ptr = params.second; m_show_blink_ptr = params.second;
@ -1004,22 +1033,20 @@ void Highlighter::init(std::pair<OG_CustomCtrl*, bool*> params)
m_custom_ctrl->Refresh(); m_custom_ctrl->Refresh();
} }
void Highlighter::init(BlinkingBitmap* blinking_bmp) // - using a BlinkingBitmap. Change state of this bitmap
void HighlighterForWx::init(BlinkingBitmap* blinking_bmp)
{ {
if (m_timer.IsRunning()) invalidate();
invalidate(); if (!Highlighter::init(!blinking_bmp))
if (!blinking_bmp)
return; return;
m_timer.Start(300, false);
m_blinking_bitmap = blinking_bmp; m_blinking_bitmap = blinking_bmp;
m_blinking_bitmap->activate(); m_blinking_bitmap->activate();
} }
void Highlighter::invalidate() void HighlighterForWx::invalidate()
{ {
m_timer.Stop(); Highlighter::invalidate();
if (m_custom_ctrl && m_show_blink_ptr) { if (m_custom_ctrl && m_show_blink_ptr) {
*m_show_blink_ptr = false; *m_show_blink_ptr = false;
@ -1031,11 +1058,9 @@ void Highlighter::invalidate()
m_blinking_bitmap->invalidate(); m_blinking_bitmap->invalidate();
m_blinking_bitmap = nullptr; m_blinking_bitmap = nullptr;
} }
m_blink_counter = 0;
} }
void Highlighter::blink() void HighlighterForWx::blink()
{ {
if (m_custom_ctrl && m_show_blink_ptr) { if (m_custom_ctrl && m_show_blink_ptr) {
*m_show_blink_ptr = !*m_show_blink_ptr; *m_show_blink_ptr = !*m_show_blink_ptr;
@ -1046,8 +1071,7 @@ void Highlighter::blink()
else else
return; return;
if ((++m_blink_counter) == 11) Highlighter::blink();
invalidate();
} }
}// GUI }// GUI

View File

@ -382,26 +382,59 @@ namespace Slic3r {
namespace GUI { namespace GUI {
class OG_CustomCtrl; class OG_CustomCtrl;
// Highlighter is used as an instrument to put attention to some UI control
class Highlighter class Highlighter
{ {
OG_CustomCtrl* m_custom_ctrl{ nullptr }; int m_blink_counter { 0 };
bool* m_show_blink_ptr{ nullptr };
BlinkingBitmap* m_blinking_bitmap{ nullptr };
int m_blink_counter{ 0 };
wxTimer m_timer; wxTimer m_timer;
public: public:
void set_timer_owner(wxEvtHandler* owner, int timerid = wxID_ANY); Highlighter() {}
void init(std::pair<OG_CustomCtrl*, bool*>); ~Highlighter() {}
void init(BlinkingBitmap* blinking_bitmap);
void set_timer_owner(wxWindow* owner, int timerid = wxID_ANY);
virtual void bind_timer(wxWindow* owner) = 0;
bool init(bool input_failed);
void blink(); void blink();
void invalidate(); void invalidate();
}; };
class HighlighterForWx : public Highlighter
{
// There are 2 possible cases to use HighlighterForWx:
// - using a BlinkingBitmap. Change state of this bitmap
BlinkingBitmap* m_blinking_bitmap { nullptr };
// - using OG_CustomCtrl where arrow will be rendered and flag indicated "show/hide" state of this arrow
OG_CustomCtrl* m_custom_ctrl { nullptr };
bool* m_show_blink_ptr { nullptr };
public:
HighlighterForWx() {}
~HighlighterForWx() {}
void bind_timer(wxWindow* owner) override;
void init(BlinkingBitmap* blinking_bitmap);
void init(std::pair<OG_CustomCtrl*, bool*>);
void blink();
void invalidate();
};
/*
class HighlighterForImGUI : public Highlighter
{
public:
HighlighterForImGUI() {}
~HighlighterForImGUI() {}
void init();
void blink();
void invalidate();
};
*/
} // GUI } // GUI
} // Slic3r } // Slic3r
#endif // slic3r_GUI_wxExtensions_hpp_ #endif // slic3r_GUI_wxExtensions_hpp_