[LyX/master] Cache icons of dynamic menu buttons

Jean-Marc Lasgouttes lasgouttes at lyx.org
Wed May 5 12:47:02 UTC 2021


commit e4b80698be91708ce1b9fd86d6032245ef4f3dc7
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Tue May 4 14:41:24 2021 +0200

    Cache icons of dynamic menu buttons
    
    The current code would load the same icons again and again and LyX
    memory footprint would grow.
    
    Tentative fixe to bug #12271.
---
 src/frontends/qt/GuiToolbar.cpp |   21 +++++++++++++++++----
 src/frontends/qt/GuiToolbar.h   |    8 ++++++++
 src/frontends/qt/GuiView.cpp    |    3 +++
 3 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/src/frontends/qt/GuiToolbar.cpp b/src/frontends/qt/GuiToolbar.cpp
index 8057d84..351e826 100644
--- a/src/frontends/qt/GuiToolbar.cpp
+++ b/src/frontends/qt/GuiToolbar.cpp
@@ -62,6 +62,11 @@ using namespace lyx::support;
 namespace lyx {
 namespace frontend {
 
+QIcon DynamicMenuButton::icon_textstyle_apply_;
+QIcon DynamicMenuButton::icon_undo_;
+QIcon DynamicMenuButton::icon_paste_;
+
+
 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
 	: QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
 	  owner_(owner), command_buffer_(nullptr), tbinfo_(tbinfo), filled_(false),
@@ -349,6 +354,14 @@ bool DynamicMenuButton::isMenuType(string const & s)
 }
 
 
+void DynamicMenuButton::resetIconCache()
+{
+	icon_textstyle_apply_ = getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false);
+	icon_undo_ =  getIcon(FuncRequest(LFUN_UNDO), false);
+	icon_paste_ = getIcon(FuncRequest(LFUN_PASTE), false);
+}
+
+
 void DynamicMenuButton::updateTriggered()
 {
 	QMenu * m = menu();
@@ -390,7 +403,7 @@ void DynamicMenuButton::updateTriggered()
 		m->clear();
 		setPopupMode(QToolButton::MenuButtonPopup);
 		if (!bv) {
-			QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
+			QToolButton::setIcon(icon_textstyle_apply_);
 			setEnabled(false);
 			return;
 		}
@@ -411,20 +424,20 @@ void DynamicMenuButton::updateTriggered()
 		}
 		// Add item to reset to defaults
 		Action * reset_act = new Action(FuncRequest(LFUN_FONT_DEFAULT, FuncRequest::TOOLBAR),
-						getIcon(FuncRequest(LFUN_UNDO), false),
+						icon_undo_,
 						qt_("&Reset to default"),
 						qt_("Reset all font settings to their defaults"), this);
 		m->addAction(reset_act);
 		if (default_act)
 			QToolButton::setDefaultAction(default_act);
-		QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
+		QToolButton::setIcon(icon_textstyle_apply_);
 		setEnabled(lyx::getStatus(FuncRequest(LFUN_TEXTSTYLE_APPLY)).enabled()
 			   || lyx::getStatus(FuncRequest(LFUN_FONT_DEFAULT)).enabled());
 	} else if (menutype == "paste") {
 		m->clear();
 		setPopupMode(QToolButton::MenuButtonPopup);
 		Action * default_action = new Action(FuncRequest(LFUN_PASTE),
-						     getIcon(FuncRequest(LFUN_PASTE), false),
+						     icon_paste_,
 						     qt_("Paste"), qt_("Paste"), this);
 		if (!bv) {
 			setEnabled(false);
diff --git a/src/frontends/qt/GuiToolbar.h b/src/frontends/qt/GuiToolbar.h
index d658f3f..cf269b6 100644
--- a/src/frontends/qt/GuiToolbar.h
+++ b/src/frontends/qt/GuiToolbar.h
@@ -96,6 +96,8 @@ public:
 	~DynamicMenuButton();
 	///
 	static bool isMenuType(std::string const & s);
+	///
+	static void resetIconCache();
 protected:
 	///
 	void initialize() override;
@@ -107,6 +109,12 @@ protected:
 protected Q_SLOTS:
 	///
 	void updateTriggered() override;
+private:
+	/// These icons are needed at each updateTriggered() call, and
+	/// therefore we have to cache them.
+	static QIcon icon_textstyle_apply_;
+	static QIcon icon_undo_;
+	static QIcon icon_paste_;
 };
 
 
diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp
index a5925f0..d6e9a85 100644
--- a/src/frontends/qt/GuiView.cpp
+++ b/src/frontends/qt/GuiView.cpp
@@ -1059,6 +1059,8 @@ void GuiView::constructToolbars()
 	// extracts the toolbars from the backend
 	for (ToolbarInfo const & inf : guiApp->toolbars())
 		d.toolbars_[inf.name] =  new GuiToolbar(inf, *this);
+
+	DynamicMenuButton::resetIconCache();
 }
 
 
@@ -1863,6 +1865,7 @@ void GuiView::updateToolbars()
 
 void GuiView::refillToolbars()
 {
+	DynamicMenuButton::resetIconCache();
 	for (auto const & tb_p : d.toolbars_)
 		tb_p.second->refill();
 }


More information about the lyx-cvs mailing list