Make template descriptions editable in template manager
authorJohannes Mueller <github@johannes-mueller.org>
Thu, 17 Aug 2017 19:42:32 +0000 (21:42 +0200)
committerRobin Gareus <robin@gareus.org>
Sun, 20 Aug 2017 19:09:30 +0000 (21:09 +0200)
gtk2_ardour/template_dialog.cc
gtk2_ardour/template_dialog.h

index ffeeaa8637c5cafc387ae513433eda0f35e0d1fa..e5b430a7bb6240f05b2b4464100c56491fcfeb2b 100644 (file)
@@ -24,6 +24,7 @@
 #include <glib/gstdio.h>
 
 #include <gtkmm/filechooserdialog.h>
+#include <gtkmm/frame.h>
 #include <gtkmm/notebook.h>
 #include <gtkmm/separator.h>
 #include <gtkmm/scrolledwindow.h>
@@ -76,6 +77,8 @@ TemplateDialog::TemplateDialog ()
 TemplateManager::TemplateManager ()
        : HBox ()
        , ProgressReporter ()
+       , _save_desc (_("Save Description"))
+       , _desc_dirty (false)
        , _remove_button (_("Remove"))
        , _rename_button (_("Rename"))
        , _export_all_templates_button (_("Export all"))
@@ -99,11 +102,11 @@ TemplateManager::TemplateManager ()
        sw->add (_template_treeview);
        sw->set_size_request (300, 200);
 
-
        VBox* vb_btns = manage (new VBox);
        vb_btns->set_spacing (4);
        vb_btns->pack_start (_rename_button, false, false);
        vb_btns->pack_start (_remove_button, false, false);
+       vb_btns->pack_start (_save_desc, false, false);
 
        _rename_button.set_sensitive (false);
        _rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::start_edit));
@@ -127,7 +130,21 @@ TemplateManager::TemplateManager ()
        vb->pack_start (*sw);
        vb->pack_start (_progress_bar);
 
+       Frame* desc_frame = manage (new Frame (_("Description")));
+
+       _description_editor.set_wrap_mode (Gtk::WRAP_WORD);
+       _description_editor.set_size_request (300,400);
+       _description_editor.set_border_width (6);
+
+       _save_desc.set_sensitive (false);
+       _save_desc.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::save_template_desc));
+
+       _description_editor.get_buffer()->signal_changed().connect (sigc::mem_fun (*this, &TemplateManager::set_desc_dirty));
+
+       desc_frame->add (_description_editor);
+
        pack_start (*vb);
+       pack_start (*desc_frame);
        pack_start (*vb_btns);
 
        show_all_children ();
@@ -145,6 +162,7 @@ TemplateManager::setup_model (const vector<TemplateInfo>& templates)
 
                row[_template_columns.name] = it->name;
                row[_template_columns.path] = it->path;
+               row[_template_columns.description] = it->description;
        }
 
        _export_all_templates_button.set_sensitive (!templates.empty ());
@@ -158,6 +176,10 @@ TemplateManager::row_selection_changed ()
                Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
                if (it) {
                        has_selection = true;
+                       const string desc = it->get_value (_template_columns.description);
+                       _description_editor.get_buffer()->set_text (desc);
+                       _desc_dirty = false;
+                       _save_desc.set_sensitive (false);
                }
        }
 
@@ -211,6 +233,51 @@ TemplateManager::start_edit ()
        _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
 }
 
+void
+TemplateManager::set_desc_dirty ()
+{
+       _desc_dirty = true;
+       _save_desc.set_sensitive (true);
+}
+
+void
+TemplateManager::save_template_desc ()
+{
+       const Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
+       const string file_path = template_file (it);
+
+       const string desc_txt = _description_editor.get_buffer()->get_text ();
+       it->set_value (_template_columns.description, desc_txt);
+
+       XMLTree tree;
+
+       if (!tree.read(file_path)) {
+               error << string_compose (_("Could not parse template file \"%1\"."), file_path) << endmsg;
+               return;
+       }
+
+       XMLNode* md = tree.root()->child (X_("Metadata"));
+       if (!md) {
+               md = new XMLNode (X_("Metadata"));
+               tree.root()->add_child_nocopy (*md);
+       }
+       XMLNode* desc = md->child (X_("description"));
+       if (!desc) {
+               desc = new XMLNode (X_("description"));
+               md->add_child_nocopy (*desc);
+       }
+       XMLNode* dn = new XMLNode (X_("content"), desc_txt);
+       desc->add_child_nocopy (*dn);
+
+       if (!tree.write ()) {
+               error << string_compose(X_("Could not write to template file \"%1\"."), file_path) << endmsg;
+               return;
+       }
+
+       _save_desc.set_sensitive (false);
+       _desc_dirty = false;
+}
+
 bool
 TemplateManager::key_event (GdkEventKey* ev)
 {
@@ -431,7 +498,7 @@ TemplateManager::update_progress_gui (float p)
 void SessionTemplateManager::init ()
 {
        vector<TemplateInfo> templates;
-       find_session_templates (templates);
+       find_session_templates (templates, /* read_xml = */ true);
        setup_model (templates);
 
        _progress_bar.hide ();
@@ -521,13 +588,21 @@ SessionTemplateManager::delete_selected_template ()
        row_selection_changed ();
 }
 
-
 string
 SessionTemplateManager::templates_dir () const
 {
        return user_template_directory ();
 }
 
+
+string
+SessionTemplateManager::template_file (const TreeModel::const_iterator& item) const
+{
+       const string path = item->get_value (_template_columns.path);
+       const string name = item->get_value (_template_columns.name);
+       return Glib::build_filename (path, name+".template");
+}
+
 bool
 SessionTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
 {
@@ -627,6 +702,13 @@ RouteTemplateManager::templates_dir () const
        return user_route_template_directory ();
 }
 
+
+string
+RouteTemplateManager::template_file (const TreeModel::const_iterator& item) const
+{
+       return item->get_value (_template_columns.path);
+}
+
 bool
 RouteTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
 {
index 0d1ba29e8c8b09c4321678ca90de1fa13c668e2f..d80ff366d9aa05d320c87d5c49581a06f78aef90 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <gtkmm/liststore.h>
 #include <gtkmm/progressbar.h>
+#include <gtkmm/textview.h>
 #include <gtkmm/treeview.h>
 
 #include "ardour_dialog.h"
@@ -64,15 +65,20 @@ protected:
        void validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name);
        void start_edit ();
 
+       void set_desc_dirty ();
+
        bool key_event (GdkEventKey* ev);
 
        virtual void rename_template (Gtk::TreeModel::iterator& item, const Glib::ustring& new_name) = 0;
        virtual void delete_selected_template () = 0;
 
+       virtual void save_template_desc ();
+
        void export_all_templates ();
        void import_template_set ();
 
        virtual std::string templates_dir () const = 0;
+       virtual std::string template_file (const Gtk::TreeModel::const_iterator& item) const = 0;
 
        virtual bool adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const = 0;
 
@@ -82,10 +88,12 @@ protected:
                SessionTemplateColumns () {
                        add (name);
                        add (path);
+                       add (description);
                }
 
                Gtk::TreeModelColumn<std::string> name;
                Gtk::TreeModelColumn<std::string> path;
+               Gtk::TreeModelColumn<std::string> description;
        };
 
        SessionTemplateColumns _template_columns;
@@ -95,6 +103,10 @@ protected:
        Gtk::CellRendererText _validating_cellrenderer;
        Gtk::TreeView::Column _validated_column;
 
+       Gtk::TextView _description_editor;
+       Gtk::Button _save_desc;
+       bool _desc_dirty;
+
        Gtk::Button _remove_button;
        Gtk::Button _rename_button;
 
@@ -120,6 +132,7 @@ private:
        void delete_selected_template ();
 
        std::string templates_dir () const;
+       std::string template_file (const Gtk::TreeModel::const_iterator& item) const;
 
        bool adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const;
 };
@@ -138,6 +151,7 @@ private:
        void delete_selected_template ();
 
        std::string templates_dir () const;
+       std::string template_file (const Gtk::TreeModel::const_iterator& item) const;
 
        bool adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const;
 };