[Xfce4-commits] [panel-plugins/xfce4-whiskermenu-plugin] 149/473: Use STL to sort items and categories.

noreply at xfce.org noreply at xfce.org
Mon Feb 16 23:55:19 CET 2015


This is an automated email from the git hooks/post-receive script.

gottcode pushed a commit to branch master
in repository panel-plugins/xfce4-whiskermenu-plugin.

commit f905c1e8c7a51533aab5988cab42b6977c1a814b
Author: Graeme Gott <graeme at gottcode.org>
Date:   Mon Jul 29 06:59:55 2013 -0400

    Use STL to sort items and categories.
---
 src/applications_page.cpp |   44 +++++++++++++++++++++++++-------------------
 src/category.cpp          |   11 +++++++++++
 src/category.hpp          |    2 ++
 src/element.hpp           |   12 +++++++++++-
 src/favorites_page.cpp    |   19 +++++++++----------
 src/favorites_page.hpp    |    2 +-
 6 files changed, 59 insertions(+), 31 deletions(-)

diff --git a/src/applications_page.cpp b/src/applications_page.cpp
index 5b58fff..724aeae 100644
--- a/src/applications_page.cpp
+++ b/src/applications_page.cpp
@@ -130,20 +130,27 @@ void ApplicationsPage::load_applications()
 		load_menu(m_garcon_menu);
 	}
 
+	// Sort items and categories
+	for (std::vector<Category*>::const_iterator i = m_categories.begin(), end = m_categories.end(); i != end; ++i)
+	{
+		(*i)->sort();
+	}
+	std::sort(m_categories.begin(), m_categories.end(), &Element::less_than);
+
 	// Create sorted list of menu items
-	std::multimap<std::string, Launcher*> sorted_items;
+	std::vector<Launcher*> sorted_items;
+	sorted_items.reserve(m_items.size());
 	for (std::map<std::string, Launcher*>::const_iterator i = m_items.begin(), end = m_items.end(); i != end; ++i)
 	{
-		gchar* collation_key = g_utf8_collate_key(i->second->get_text(), -1);
-		sorted_items.insert(std::make_pair(collation_key, i->second));
-		g_free(collation_key);
+		sorted_items.push_back(i->second);
 	}
+	std::sort(sorted_items.begin(), sorted_items.end(), &Element::less_than);
 
 	// Add all items to model
 	LauncherModel model;
-	for (std::multimap<std::string, Launcher*>::const_iterator i = sorted_items.begin(), end = sorted_items.end(); i != end; ++i)
+	for (std::vector<Launcher*>::const_iterator i = sorted_items.begin(), end = sorted_items.end(); i != end; ++i)
 	{
-		model.append_item(i->second);
+		model.append_item(*i);
 	}
 	m_model = model.get_model();
 	g_object_ref(m_model);
@@ -220,21 +227,20 @@ void ApplicationsPage::load_menu(GarconMenu* menu)
 		g_object_unref(directory);
 	}
 
-	// Add submenus
-	GList* menus = garcon_menu_get_menus(menu);
-	for (GList* li = menus; li != NULL; li = li->next)
+	// Add menu elements
+	GList* elements = garcon_menu_get_elements(menu);
+	for (GList* li = elements; li != NULL; li = li->next)
 	{
-		load_menu(GARCON_MENU(li->data));
-	}
-	g_list_free(menus);
-
-	// Add items
-	GList* items = garcon_menu_get_items(menu);
-	for (GList* li = items; li != NULL; li = li->next)
-	{
-		load_menu_item(GARCON_MENU_ITEM(li->data));
+		if (GARCON_IS_MENU_ITEM(li->data))
+		{
+			load_menu_item(GARCON_MENU_ITEM(li->data));
+		}
+		else if (GARCON_IS_MENU(li->data))
+		{
+			load_menu(GARCON_MENU(li->data));
+		}
 	}
-	g_list_free(items);
+	g_list_free(elements);
 
 	// Only track single level of categories
 	if (first_level)
diff --git a/src/category.cpp b/src/category.cpp
index e67c894..e0337c3 100644
--- a/src/category.cpp
+++ b/src/category.cpp
@@ -16,9 +16,12 @@
 
 #include "category.hpp"
 
+#include "launcher.hpp"
 #include "launcher_model.hpp"
 #include "section_button.hpp"
 
+#include <algorithm>
+
 using namespace WhiskerMenu;
 
 //-----------------------------------------------------------------------------
@@ -85,6 +88,14 @@ GtkTreeModel* Category::get_model()
 
 //-----------------------------------------------------------------------------
 
+void Category::sort()
+{
+	unset_model();
+	std::sort(m_items.begin(), m_items.end(), &Element::less_than);
+}
+
+//-----------------------------------------------------------------------------
+
 void Category::unset_model()
 {
 	if (m_model)
diff --git a/src/category.hpp b/src/category.hpp
index d0ae965..614d107 100644
--- a/src/category.hpp
+++ b/src/category.hpp
@@ -62,6 +62,8 @@ public:
 		unset_model();
 	}
 
+	void sort();
+
 private:
 	void unset_model();
 
diff --git a/src/element.hpp b/src/element.hpp
index 541a3de..bfed283 100644
--- a/src/element.hpp
+++ b/src/element.hpp
@@ -30,7 +30,8 @@ class Element
 public:
 	Element() :
 		m_icon(NULL),
-		m_text(NULL)
+		m_text(NULL),
+		m_sort_key(NULL)
 	{
 	}
 
@@ -38,6 +39,7 @@ public:
 	{
 		g_free(m_icon);
 		g_free(m_text);
+		g_free(m_sort_key);
 	}
 
 	virtual int get_type() const = 0;
@@ -52,6 +54,11 @@ public:
 		return m_text;
 	}
 
+	static bool less_than(const Element* lhs, const Element* rhs)
+	{
+		return g_strcmp0(lhs->m_sort_key, rhs->m_sort_key) < 0;
+	}
+
 protected:
 	void set_icon(const gchar* icon)
 	{
@@ -66,16 +73,19 @@ protected:
 	void set_text(const gchar* text)
 	{
 		m_text = g_strdup(text);
+		m_sort_key = g_utf8_collate_key(m_text, -1);
 	}
 
 	void set_text(gchar* text)
 	{
 		m_text = text;
+		m_sort_key = g_utf8_collate_key(m_text, -1);
 	}
 
 private:
 	gchar* m_icon;
 	gchar* m_text;
+	gchar* m_sort_key;
 };
 
 }
diff --git a/src/favorites_page.cpp b/src/favorites_page.cpp
index a5e8c67..c8ed504 100644
--- a/src/favorites_page.cpp
+++ b/src/favorites_page.cpp
@@ -85,7 +85,7 @@ void FavoritesPage::extend_context_menu(GtkWidget* menu)
 
 //-----------------------------------------------------------------------------
 
-void FavoritesPage::sort(std::map<std::string, Launcher*>& items) const
+void FavoritesPage::sort(std::vector<Launcher*>& items) const
 {
 	const std::vector<std::string>& desktop_ids = get_desktop_ids();
 	for (std::vector<std::string>::const_iterator i = desktop_ids.begin(), end = desktop_ids.end(); i != end; ++i)
@@ -95,10 +95,9 @@ void FavoritesPage::sort(std::map<std::string, Launcher*>& items) const
 		{
 			continue;
 		}
-		gchar* collation_key = g_utf8_collate_key(launcher->get_text(), -1);
-		items[collation_key] = launcher;
-		g_free(collation_key);
+		items.push_back(launcher);
 	}
+	std::sort(items.begin(), items.end(), &Element::less_than);
 }
 
 //-----------------------------------------------------------------------------
@@ -106,11 +105,11 @@ void FavoritesPage::sort(std::map<std::string, Launcher*>& items) const
 void FavoritesPage::sort_ascending()
 {
 	std::vector<std::string> desktop_ids;
-	std::map<std::string, Launcher*> items;
+	std::vector<Launcher*> items;
 	sort(items);
-	for (std::map<std::string, Launcher*>::const_iterator i = items.begin(), end = items.end(); i != end; ++i)
+	for (std::vector<Launcher*>::const_iterator i = items.begin(), end = items.end(); i != end; ++i)
 	{
-		desktop_ids.push_back(i->second->get_desktop_id());
+		desktop_ids.push_back((*i)->get_desktop_id());
 	}
 	set_desktop_ids(desktop_ids);
 }
@@ -120,11 +119,11 @@ void FavoritesPage::sort_ascending()
 void FavoritesPage::sort_descending()
 {
 	std::vector<std::string> desktop_ids;
-	std::map<std::string, Launcher*> items;
+	std::vector<Launcher*> items;
 	sort(items);
-	for (std::map<std::string, Launcher*>::const_reverse_iterator i = items.rbegin(), end = items.rend(); i != end; ++i)
+	for (std::vector<Launcher*>::const_reverse_iterator i = items.rbegin(), end = items.rend(); i != end; ++i)
 	{
-		desktop_ids.push_back(i->second->get_desktop_id());
+		desktop_ids.push_back((*i)->get_desktop_id());
 	}
 	set_desktop_ids(desktop_ids);
 }
diff --git a/src/favorites_page.hpp b/src/favorites_page.hpp
index a0e9a7d..230d371 100644
--- a/src/favorites_page.hpp
+++ b/src/favorites_page.hpp
@@ -33,7 +33,7 @@ public:
 
 private:
 	void extend_context_menu(GtkWidget* menu);
-	void sort(std::map<std::string, Launcher*>& items) const;
+	void sort(std::vector<Launcher*>& items) const;
 	void sort_ascending();
 	void sort_descending();
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list