[gnote] Use vector for app popover building



commit 517e4c7cbc23aae032badcb887c89eab1d047c37
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Mar 24 17:47:46 2019 +0200

    Use vector for app popover building

 src/actionmanager.cpp     | 37 ++++---------------------------------
 src/actionmanager.hpp     |  6 ++----
 src/iactionmanager.cpp    |  3 ++-
 src/iactionmanager.hpp    | 34 ++++++++++++++++++++++++++++++++--
 src/searchnoteswidget.cpp | 16 +++++++++++++---
 5 files changed, 53 insertions(+), 43 deletions(-)
---
diff --git a/src/actionmanager.cpp b/src/actionmanager.cpp
index db281956..c87f297a 100644
--- a/src/actionmanager.cpp
+++ b/src/actionmanager.cpp
@@ -90,8 +90,7 @@ namespace gnote {
     register_main_window_action("increase-indent", NULL, true);
     register_main_window_action("decrease-indent", NULL, true);
 
-    signal_build_main_window_search_popover.connect(sigc::mem_fun(*this, 
&ActionManager::add_app_menu_new_section));
-    signal_build_main_window_search_popover.connect(sigc::mem_fun(*this, 
&ActionManager::add_app_menu_trailing_sections));
+    signal_build_main_window_search_popover.connect(sigc::mem_fun(*this, 
&ActionManager::add_app_menu_items));
   }
 
 
@@ -189,39 +188,11 @@ namespace gnote {
     return cbacks;
   }
 
-  bool ActionManager::add_app_menu_section(std::map<int, Gtk::Widget*> & widgets, int & order, int section)
+  void ActionManager::add_app_menu_items(std::vector<PopoverWidget> & widgets)
   {
-    std::pair<AppMenuItemMultiMap::const_iterator, AppMenuItemMultiMap::const_iterator>
-    range = m_app_menu_items.equal_range(section);
-
-    if(range.first != m_app_menu_items.end()) {
-      for(AppMenuItemMultiMap::const_iterator iter = range.first; iter != range.second; ++iter) {
-        widgets.insert(std::make_pair(order++, utils::create_popover_button(iter->second.action_def, 
iter->second.label)));
-      }
-
-      return true;
-    }
-
-    return false;
-  }
-
-  void ActionManager::add_app_menu_new_section(std::map<int, Gtk::Widget*> & widgets)
-  {
-    int order = 0;
-    if(add_app_menu_section(widgets, order, APP_ACTION_NEW)) {
-      // end section
-      widgets[order] = NULL;
-    }
-  }
-
-  void ActionManager::add_app_menu_trailing_sections(std::map<int, Gtk::Widget*> & widgets)
-  {
-    int order = 100000;
-    if(add_app_menu_section(widgets, order, APP_ACTION_MANAGE)) {
-      // end section
-      widgets[order++] = NULL;
+    for(auto& iter : m_app_menu_items) {
+      widgets.push_back(PopoverWidget(iter.first, iter.second.order, 
utils::create_popover_button(iter.second.action_def, iter.second.label)));
     }
-    add_app_menu_section(widgets, order, APP_ACTION_LAST);
   }
 
 }
diff --git a/src/actionmanager.hpp b/src/actionmanager.hpp
index c9715203..697e5435 100644
--- a/src/actionmanager.hpp
+++ b/src/actionmanager.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012-2013,2015-2018 Aurimas Cernius
+ * Copyright (C) 2012-2013,2015-2019 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
@@ -60,9 +60,7 @@ private:
   void make_app_actions();
   void make_app_menu_items();
   Glib::RefPtr<Gio::Menu> make_app_menu_section(int section) const;
-  bool add_app_menu_section(std::map<int, Gtk::Widget*> & widgets, int & order, int section);
-  void add_app_menu_new_section(std::map<int, Gtk::Widget*>&);
-  void add_app_menu_trailing_sections(std::map<int, Gtk::Widget*>&);
+  void add_app_menu_items(std::vector<PopoverWidget>&);
 
   std::vector<Glib::RefPtr<Gio::SimpleAction> > m_app_actions;
 
diff --git a/src/iactionmanager.cpp b/src/iactionmanager.cpp
index 790040ca..89a6c97a 100644
--- a/src/iactionmanager.cpp
+++ b/src/iactionmanager.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2013 Aurimas Cernius
+ * Copyright (C) 2013,2019 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -25,6 +25,7 @@ namespace gnote {
 const int IActionManager::APP_ACTION_NEW = 1;
 const int IActionManager::APP_ACTION_MANAGE = 2;
 const int IActionManager::APP_ACTION_LAST = 3;
+const int IActionManager::APP_CUSTOM_SECTION = 1000;
 
 IActionManager::~IActionManager()
 {}
diff --git a/src/iactionmanager.hpp b/src/iactionmanager.hpp
index cf386a89..ae114063 100644
--- a/src/iactionmanager.hpp
+++ b/src/iactionmanager.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2013,2015-2017 Aurimas Cernius
+ * Copyright (C) 2013,2015-2017,2019 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -52,6 +52,36 @@ public:
   static const int APP_ACTION_NEW;
   static const int APP_ACTION_MANAGE;
   static const int APP_ACTION_LAST;
+  static const int APP_CUSTOM_SECTION;
+
+  struct PopoverWidget
+  {
+    Gtk::Widget *widget;
+    int section;
+    int order;
+    int secondary_order;
+
+    PopoverWidget(int ord, Gtk::Widget *w)
+      : widget(w)
+      , section(APP_ACTION_MANAGE)
+      , order(ord)
+      {}
+
+    PopoverWidget(int sec, int ord, Gtk::Widget *w)
+      : widget(w)
+      , section(sec)
+      , order(ord)
+      {}
+
+    bool operator< (const PopoverWidget & other)
+      {
+        if(section != other.section)
+          return section < other.section;
+        if(order != other.order)
+          return order < other.order;
+        return secondary_order < other.secondary_order;
+      }
+  };
 
   virtual ~IActionManager();
 
@@ -69,7 +99,7 @@ public:
   virtual void unregister_main_window_search_callback(const Glib::ustring & id) = 0;
   virtual std::map<Glib::ustring, sigc::slot<void, const Glib::VariantBase&>> 
get_main_window_search_callbacks() = 0;
   sigc::signal<void> signal_main_window_search_actions_changed;
-  sigc::signal<void, std::map<int, Gtk::Widget*>&> signal_build_main_window_search_popover;
+  sigc::signal<void, std::vector<PopoverWidget>&> signal_build_main_window_search_popover;
 };
 
 }
diff --git a/src/searchnoteswidget.cpp b/src/searchnoteswidget.cpp
index 0153509b..f58dc164 100644
--- a/src/searchnoteswidget.cpp
+++ b/src/searchnoteswidget.cpp
@@ -1433,11 +1433,21 @@ void SearchNotesWidget::size_internals()
 
 std::vector<Gtk::Widget*> SearchNotesWidget::get_popover_widgets()
 {
-  std::map<int, Gtk::Widget*> popover_widgets;
+  std::vector<IActionManager::PopoverWidget> popover_widgets;
+  popover_widgets.reserve(20);
   IActionManager::obj().signal_build_main_window_search_popover(popover_widgets);
+  for(unsigned i = 0; i < popover_widgets.size(); ++i) {
+    popover_widgets[i].secondary_order = i;
+  }
+  std::sort(popover_widgets.begin(), popover_widgets.end());
   std::vector<Gtk::Widget*> widgets;
-  for(auto widget : popover_widgets) {
-    widgets.push_back(widget.second);
+  int section = popover_widgets.size() ? popover_widgets.front().section : 0;
+  for(auto& widget : popover_widgets) {
+    if (section != widget.section) {
+      widgets.push_back(NULL);
+      section = widget.section;
+    }
+    widgets.push_back(widget.widget);
   }
   return widgets;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]