[gnote] Update tableofcontents plugin for popover



commit e9e15fcbdc1a73b18d2f3010e7d4130d41259812
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Wed Dec 30 20:36:22 2015 +0200

    Update tableofcontents plugin for popover

 src/addins/tableofcontents/Makefile.am             |    2 -
 .../tableofcontents/tableofcontents.desktop.in.in  |    5 +-
 .../tableofcontents/tableofcontentsaction.cpp      |   75 ---------
 .../tableofcontents/tableofcontentsaction.hpp      |   49 ------
 .../tableofcontents/tableofcontentsnoteaddin.cpp   |  158 +++++++++++++++++++-
 .../tableofcontents/tableofcontentsnoteaddin.hpp   |   15 ++-
 6 files changed, 168 insertions(+), 136 deletions(-)
---
diff --git a/src/addins/tableofcontents/Makefile.am b/src/addins/tableofcontents/Makefile.am
index af481e8..75cdd85 100644
--- a/src/addins/tableofcontents/Makefile.am
+++ b/src/addins/tableofcontents/Makefile.am
@@ -17,8 +17,6 @@ tableofcontents_la_SOURCES = \
        tableofcontentsnoteaddin.cpp \
        tableofcontentsmenuitem.hpp  \
        tableofcontentsmenuitem.cpp  \
-       tableofcontentsaction.hpp    \
-       tableofcontentsaction.cpp    \
        tableofcontentsutils.hpp     \
        tableofcontentsutils.cpp     \
        $(NULL)
diff --git a/src/addins/tableofcontents/tableofcontents.desktop.in.in 
b/src/addins/tableofcontents/tableofcontents.desktop.in.in
index 08fd78f..7bce45f 100644
--- a/src/addins/tableofcontents/tableofcontents.desktop.in.in
+++ b/src/addins/tableofcontents/tableofcontents.desktop.in.in
@@ -4,9 +4,12 @@ _Name=Table of Contents
 _Description=Navigate long structured notes. Set section and subsection headings in your note, and the Table 
of Contents will show in a menu.
 _Authors=Luc Pionchon
 Category=Tools
-Version=1.0.1
+Version=1.1.0
 DefaultEnabled=false
 Module=tableofcontents
 _Copyright=© 2013 Luc Pionchon
 LibgnoteRelease= LIBGNOTE_RELEASE@
 LibgnoteVersionInfo= LIBGNOTE_VERSION_INFO@
+[Actions]
+actions_void=tableofcontents-heading1,tableofcontents-heading2,tableofcontents-help
+actions_int=tableofcontents-goto-heading
diff --git a/src/addins/tableofcontents/tableofcontentsnoteaddin.cpp 
b/src/addins/tableofcontents/tableofcontentsnoteaddin.cpp
index 9d3a6ca..8f2f46e 100644
--- a/src/addins/tableofcontents/tableofcontentsnoteaddin.cpp
+++ b/src/addins/tableofcontents/tableofcontentsnoteaddin.cpp
@@ -23,6 +23,7 @@
 
 #include <glibmm/i18n.h>
 
+#include <gtkmm/modelbutton.h>
 #include <gtkmm/stock.h>
 #include <gtkmm/separatormenuitem.h>
 
@@ -37,7 +38,7 @@
 #include "tableofcontents.hpp"
 #include "tableofcontentsnoteaddin.hpp"
 #include "tableofcontentsmenuitem.hpp"
-#include "tableofcontentsaction.hpp"
+#include "tableofcontentsutils.hpp"
 
 namespace tableofcontents {
 
@@ -82,16 +83,21 @@ void TableofcontentsNoteAddin::on_note_opened ()
   m_toc_menu->signal_hide().connect(
     sigc::mem_fun(*this, &TableofcontentsNoteAddin::on_menu_hidden));
 
-  Glib::RefPtr<Gtk::Action> action = TableofcontentsAction::create(
-    sigc::mem_fun(*this, &TableofcontentsNoteAddin::update_menu));
-  add_note_action(action, gnote::TABLE_OF_CONTENTS_ORDER);
+  auto win = get_window();
+  win->signal_foregrounded.connect(sigc::mem_fun(*this, &TableofcontentsNoteAddin::on_foregrounded));
+  win->signal_backgrounded.connect(sigc::mem_fun(*this, &TableofcontentsNoteAddin::on_backgrounded));
+
+  auto buffer = get_note()->get_buffer();
+  if(buffer) {
+    buffer->signal_changed().connect(sigc::mem_fun(*this, &TableofcontentsNoteAddin::on_note_changed));
+  }
 
   // Reacts to key press events
-  get_window()->signal_key_press_event().connect(
+  win->signal_key_press_event().connect(
     sigc::mem_fun(*this, &TableofcontentsNoteAddin::on_key_pressed));
 
   // TOC can show up also in the contextual menu
-  get_window()->editor()->signal_populate_popup().connect(
+  win->editor()->signal_populate_popup().connect(
     sigc::mem_fun(*this, &TableofcontentsNoteAddin::on_populate_popup));
 
   // Heading tags
@@ -101,9 +107,77 @@ void TableofcontentsNoteAddin::on_note_opened ()
 }
 
 
-void TableofcontentsNoteAddin::update_menu(Gtk::Menu *menu)
+void TableofcontentsNoteAddin::on_foregrounded()
 {
-  populate_toc_menu(menu);
+  auto host = get_window()->host();
+  m_level_1_action_cid = host->find_action("tableofcontents-heading1")->signal_activate()
+    .connect(sigc::mem_fun(*this, &TableofcontentsNoteAddin::on_level_1_action));
+  m_level_2_action_cid = host->find_action("tableofcontents-heading2")->signal_activate()
+    .connect(sigc::mem_fun(*this, &TableofcontentsNoteAddin::on_level_2_action));
+  m_toc_help_action_cid = host->find_action("tableofcontents-help")->signal_activate()
+    .connect(sigc::mem_fun(*this, &TableofcontentsNoteAddin::on_toc_help_action));
+  auto goto_action = host->find_action("tableofcontents-goto-heading");
+  goto_action->set_state(Glib::Variant<gint32>::create(-1));
+  m_goto_heading_cid = goto_action->signal_activate()
+    .connect(sigc::mem_fun(*this, &TableofcontentsNoteAddin::on_goto_heading));
+}
+
+
+void TableofcontentsNoteAddin::on_backgrounded()
+{
+  m_level_1_action_cid.disconnect();
+  m_level_2_action_cid.disconnect();
+  m_toc_help_action_cid.disconnect();
+  m_goto_heading_cid.disconnect();
+}
+
+
+std::map<int, Gtk::Widget*> TableofcontentsNoteAddin::get_actions_popover_widgets() const
+{
+  auto widgets = NoteAddin::get_actions_popover_widgets();
+  auto toc_item = gnote::utils::create_popover_submenu_button("tableofcontents-menu", _("Table of 
Contents"));
+  gnote::utils::add_item_to_ordered_map(widgets, gnote::TABLE_OF_CONTENTS_ORDER, toc_item);
+  auto toc_menu = gnote::utils::create_popover_submenu("tableofcontents-menu");
+  gnote::utils::add_item_to_ordered_map(widgets, 100000, toc_menu);
+
+  int top = 0;
+  int sub_top = 0;
+  Gtk::Grid *sub = manage(new Gtk::Grid);
+  gnote::utils::set_common_popover_widget_props(*sub);
+  std::vector<Gtk::Widget*> toc_items;
+  get_toc_popover_items(toc_items);
+  if(toc_items.size()) {
+    for(auto & toc_button : toc_items) {
+      sub->attach(*toc_button, 1, sub_top++, 1, 1);
+    }
+
+    toc_menu->attach(*sub, 0, top++, 1, 1);
+    sub_top = 0;
+    sub = manage(new Gtk::Grid);
+    gnote::utils::set_common_popover_widget_props(*sub);
+  }
+
+  auto item = manage(gnote::utils::create_popover_button("win.tableofcontents-heading1", _("Heading 1")));
+  item->add_accelerator("activate", get_window()->get_accel_group(), GDK_KEY_1, Gdk::CONTROL_MASK, 
Gtk::ACCEL_VISIBLE);
+  sub->attach(*item, 0, sub_top++, 1, 1);
+
+  item = manage(gnote::utils::create_popover_button("win.tableofcontents-heading2", _("Heading 2")));
+  item->add_accelerator("activate", get_window()->get_accel_group(), GDK_KEY_2, Gdk::CONTROL_MASK, 
Gtk::ACCEL_VISIBLE);
+  sub->attach(*item, 0, sub_top++, 1, 1);
+
+  item = manage(gnote::utils::create_popover_button("win.tableofcontents-help", _("Table of Contents 
Help")));
+  sub->attach(*item, 0, sub_top++, 1, 1);
+  toc_menu->attach(*sub, 0, top++, 1, 1);
+
+  sub = manage(new Gtk::Grid);
+  gnote::utils::set_common_popover_widget_props(*sub);
+  sub_top = 0;
+  auto back_item = gnote::utils::create_popover_submenu_button("main", _("_Back"));
+  dynamic_cast<Gtk::ModelButton*>(back_item)->property_inverted() = true;
+  sub->attach(*back_item, 0, sub_top++, 1, 1);
+  toc_menu->attach(*sub, 0, top++, 1, 1);
+
+  return widgets;
 }
 
 
@@ -273,6 +347,39 @@ void TableofcontentsNoteAddin::get_tableofcontents_menu_items(std::list<Tableofc
 }
 
 
+void TableofcontentsNoteAddin::get_toc_popover_items(std::vector<Gtk::Widget*> & items) const
+{
+  std::vector<TocItem> toc_items;
+
+  get_toc_items(toc_items);
+  if(toc_items.size()) {
+    auto item = 
dynamic_cast<Gtk::ModelButton*>(gnote::utils::create_popover_button("win.tableofcontents-goto-heading", ""));
+    Gtk::Label *label = (Gtk::Label*)item->get_child();
+    label->set_markup("<b>" + get_note()->get_title() + "</b>");
+    gtk_actionable_set_action_target_value(GTK_ACTIONABLE(item->gobj()), g_variant_new_int32(0));
+    item->property_role() = Gtk::BUTTON_ROLE_NORMAL;
+    item->property_inverted() = true;
+    item->property_centered() = false;
+    items.push_back(item);
+  }
+
+  for(auto & toc_item : toc_items) {
+    if(toc_item.heading_level == Heading::Level_2) {
+      toc_item.heading = "→  " + toc_item.heading;
+    }
+    auto item = 
dynamic_cast<Gtk::ModelButton*>(gnote::utils::create_popover_button("win.tableofcontents-goto-heading", 
toc_item.heading));
+    if(toc_item.heading_level == Heading::Level_1) {
+      item->set_image(*manage(new Gtk::Image(Gtk::Stock::GO_FORWARD, Gtk::ICON_SIZE_MENU)));
+    }
+    gtk_actionable_set_action_target_value(GTK_ACTIONABLE(item->gobj()), 
g_variant_new_int32(toc_item.heading_position));
+    item->property_role() = Gtk::BUTTON_ROLE_NORMAL;
+    item->property_inverted() = true;
+    item->property_centered() = false;
+    items.push_back(item);
+  }
+}
+
+
 void TableofcontentsNoteAddin::on_level_1_activated()
 {
   headification_switch (Heading::Level_1);
@@ -297,6 +404,24 @@ void TableofcontentsNoteAddin::on_toc_help_activated()
 }
 
 
+void TableofcontentsNoteAddin::on_level_1_action(const Glib::VariantBase&)
+{
+  on_level_1_activated();
+  on_note_changed();
+}
+
+void TableofcontentsNoteAddin::on_level_2_action(const Glib::VariantBase&)
+{
+  on_level_2_activated();
+  on_note_changed();
+}
+
+void TableofcontentsNoteAddin::on_toc_help_action(const Glib::VariantBase&)
+{
+  on_toc_help_activated();
+}
+
+
 bool TableofcontentsNoteAddin::on_key_pressed(GdkEventKey *ev)
 //return true if signal handled, false otherwise
 //NOTE: if a menu item has an accelerator,
@@ -393,4 +518,21 @@ void TableofcontentsNoteAddin::headification_switch (Heading::Type heading_reque
 }
 
 
+void TableofcontentsNoteAddin::on_goto_heading(const Glib::VariantBase & param)
+{
+  int value = Glib::VariantBase::cast_dynamic<Glib::Variant<gint32>>(param).get();
+  goto_heading(get_note(), value);
+}
+
+
+void TableofcontentsNoteAddin::on_note_changed()
+{
+  auto win = get_note()->get_window();
+  if(!win) {
+    return;
+  }
+  win->signal_popover_widgets_changed();
+}
+
+
 } //namespace
diff --git a/src/addins/tableofcontents/tableofcontentsnoteaddin.hpp 
b/src/addins/tableofcontents/tableofcontentsnoteaddin.hpp
index 3980801..1c1b2d1 100644
--- a/src/addins/tableofcontents/tableofcontentsnoteaddin.hpp
+++ b/src/addins/tableofcontents/tableofcontentsnoteaddin.hpp
@@ -61,9 +61,9 @@ public:
   virtual void initialize() override;
   virtual void shutdown() override;
   virtual void on_note_opened() override;
+  virtual std::map<int, Gtk::Widget*> get_actions_popover_widgets() const override;
 
 private:
-  void update_menu(Gtk::Menu *menu);
   void on_menu_hidden();
   bool on_key_pressed (GdkEventKey *ev);
   void on_populate_popup (Gtk::Menu* popup_menu);
@@ -71,6 +71,13 @@ private:
   void on_level_2_activated ();
   void on_toc_popup_activated ();
   void on_toc_help_activated ();
+  void on_level_1_action(const Glib::VariantBase&);
+  void on_level_2_action(const Glib::VariantBase&);
+  void on_toc_help_action(const Glib::VariantBase&);
+  void on_foregrounded();
+  void on_backgrounded();
+  void on_goto_heading(const Glib::VariantBase&);
+  void on_note_changed();
 
 
   void populate_toc_menu (Gtk::Menu *toc_menu, bool has_action_entries = true);
@@ -86,6 +93,7 @@ private:
   };
   void get_toc_items(std::vector<TocItem> & items) const;
   void get_tableofcontents_menu_items (std::list<TableofcontentsMenuItem*> & items);
+  void get_toc_popover_items(std::vector<Gtk::Widget*> & items) const;
 
   void headification_switch (Heading::Type heading_request);
 
@@ -95,6 +103,11 @@ private:
   Glib::RefPtr<Gtk::TextTag> m_tag_bold; // the tags used to mark headings
   Glib::RefPtr<Gtk::TextTag> m_tag_large;
   Glib::RefPtr<Gtk::TextTag> m_tag_huge;
+
+  sigc::connection m_level_1_action_cid;
+  sigc::connection m_level_2_action_cid;
+  sigc::connection m_toc_help_action_cid;
+  sigc::connection m_goto_heading_cid;
 };
 
 


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