Also handle Route templates

Following measures:

* Split up into two classes
  * TemplateDialog: the general dialog
  * TemplateManager: A widget to rename and remove templates

* Make TemplateManager abstract and derive a class for session templates and
  one for route templates. This is needed, as session templates and route
  templates are stored in a different way. Thus we need different methods to
  rename and remove them.
This commit is contained in:
Johannes Mueller 2017-07-03 14:30:05 +02:00 committed by Robin Gareus
parent 6a973d5c42
commit a5ba520f75
2 changed files with 193 additions and 90 deletions

View File

@ -20,6 +20,7 @@
#include <glib/gstdio.h>
#include <gtkmm/notebook.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/treeiter.h>
@ -36,27 +37,45 @@ using namespace PBD;
using namespace ARDOUR;
TemplateDialog::TemplateDialog ()
: ArdourDialog (_("Manage Templates"))
: ArdourDialog ("Manage Templates")
{
Notebook* nb = manage (new Notebook);
SessionTemplateManager* session_tm = manage (new SessionTemplateManager);
session_tm->init ();
nb->append_page (*session_tm, _("Session Templates"));
RouteTemplateManager* route_tm = manage (new RouteTemplateManager);
route_tm->init ();
nb->append_page (*route_tm, _("Track Templates"));
get_vbox()->pack_start (*nb);
add_button (_("Ok"), Gtk::RESPONSE_OK);
show_all_children ();
}
TemplateManager::TemplateManager ()
: HBox ()
, _remove_button (_("Remove"))
, _rename_button (_("Rename"))
{
_session_template_model = ListStore::create (_session_template_columns);
setup_session_templates ();
_session_template_treeview.set_model (_session_template_model);
_template_model = ListStore::create (_template_columns);
_template_treeview.set_model (_template_model);
_validated_column.set_title (_("Template Name"));
_validated_column.pack_start (_validating_cellrenderer);
_session_template_treeview.append_column (_validated_column);
_template_treeview.append_column (_validated_column);
_validating_cellrenderer.property_editable() = true;
_validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateDialog::render_template_names));
_validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateDialog::validate_edit));
_session_template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateDialog::row_selection_changed));
_session_template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateDialog::key_event));
_validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateManager::render_template_names));
_validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateManager::validate_edit));
_template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
_template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateManager::key_event));
ScrolledWindow* sw = manage (new ScrolledWindow);
sw->property_hscrollbar_policy() = POLICY_AUTOMATIC;
sw->add (_session_template_treeview);
sw->add (_template_treeview);
sw->set_size_request (300, 200);
@ -66,45 +85,37 @@ TemplateDialog::TemplateDialog ()
vb->pack_start (_remove_button, false, false);
_rename_button.set_sensitive (false);
_rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateDialog::start_edit));
_rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::start_edit));
_remove_button.set_sensitive (false);
_remove_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateDialog::delete_selected_template));
_remove_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::delete_selected_template));
HBox* hb = manage (new HBox);
hb->set_spacing (6);
hb->pack_start (*sw);
hb->pack_start (*vb);
get_vbox()->pack_start (*hb);
set_spacing (6);
pack_start (*sw);
pack_start (*vb);
show_all_children ();
add_button (_("Ok"), Gtk::RESPONSE_OK);
}
void
TemplateDialog::setup_session_templates ()
TemplateManager::setup_model (const vector<TemplateInfo>& templates)
{
vector<TemplateInfo> templates;
find_session_templates (templates);
_template_model->clear ();
_session_template_model->clear ();
for (vector<TemplateInfo>::iterator it = templates.begin(); it != templates.end(); ++it) {
for (vector<TemplateInfo>::const_iterator it = templates.begin(); it != templates.end(); ++it) {
TreeModel::Row row;
row = *(_session_template_model->append ());
row = *(_template_model->append ());
row[_session_template_columns.name] = it->name;
row[_session_template_columns.path] = it->path;
row[_template_columns.name] = it->name;
row[_template_columns.path] = it->path;
}
}
void
TemplateDialog::row_selection_changed ()
TemplateManager::row_selection_changed ()
{
bool has_selection = false;
if (_session_template_treeview.get_selection()->count_selected_rows () != 0) {
Gtk::TreeModel::const_iterator it = _session_template_treeview.get_selection()->get_selected ();
if (_template_treeview.get_selection()->count_selected_rows () != 0) {
Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
if (it) {
has_selection = true;
}
@ -115,28 +126,28 @@ TemplateDialog::row_selection_changed ()
}
void
TemplateDialog::render_template_names (Gtk::CellRenderer*, const Gtk::TreeModel::iterator& it)
TemplateManager::render_template_names (Gtk::CellRenderer*, const Gtk::TreeModel::iterator& it)
{
if (it) {
_validating_cellrenderer.property_text () = it->get_value (_session_template_columns.name);
_validating_cellrenderer.property_text () = it->get_value (_template_columns.name);
}
}
void
TemplateDialog::validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name)
TemplateManager::validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name)
{
const TreePath path (path_string);
TreeModel::iterator current = _session_template_model->get_iter (path);
TreeModel::iterator current = _template_model->get_iter (path);
if (current->get_value (_session_template_columns.name) == new_name) {
if (current->get_value (_template_columns.name) == new_name) {
return;
}
TreeModel::Children rows = _session_template_model->children ();
TreeModel::Children rows = _template_model->children ();
bool found = false;
for (TreeModel::Children::const_iterator it = rows.begin(); it != rows.end(); ++it) {
if (it->get_value (_session_template_columns.name) == new_name) {
if (it->get_value (_template_columns.name) == new_name) {
found = true;
break;
}
@ -152,46 +163,16 @@ TemplateDialog::validate_edit (const Glib::ustring& path_string, const Glib::ust
}
void
TemplateDialog::start_edit ()
TemplateManager::start_edit ()
{
TreeModel::Path path;
TreeViewColumn* col;
_session_template_treeview.get_cursor (path, col);
_session_template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
}
void
TemplateDialog::delete_selected_template ()
{
if (_session_template_treeview.get_selection()->count_selected_rows() == 0) {
return;
}
Gtk::TreeModel::const_iterator it = _session_template_treeview.get_selection()->get_selected();
if (!it) {
return;
}
const string path = it->get_value (_session_template_columns.path);
const string name = it->get_value (_session_template_columns.name);
const string file_path = Glib::build_filename (path, name+".template");
if (g_unlink (file_path.c_str()) != 0) {
error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
return;
}
if (g_rmdir (path.c_str()) != 0) {
error << string_compose(_("Could not delete template directory \"%1\": %2"), path, strerror (errno)) << endmsg;
}
_session_template_model->erase (it);
row_selection_changed ();
_template_treeview.get_cursor (path, col);
_template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
}
bool
TemplateDialog::key_event (GdkEventKey* ev)
TemplateManager::key_event (GdkEventKey* ev)
{
if (ev->keyval == GDK_KEY_F2) {
start_edit ();
@ -205,18 +186,30 @@ TemplateDialog::key_event (GdkEventKey* ev)
return false;
}
void
TemplateDialog::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
void SessionTemplateManager::init ()
{
const string path = item->get_value (_session_template_columns.path);
const string name = item->get_value (_session_template_columns.name);
vector<TemplateInfo> templates;
find_session_templates (templates);
setup_model (templates);
}
void RouteTemplateManager::init ()
{
vector<TemplateInfo> templates;
find_route_templates (templates);
setup_model (templates);
}
void
SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
{
const string path = item->get_value (_template_columns.path);
const string name = item->get_value (_template_columns.name);
const string old_filepath = Glib::build_filename (path, name+".template");
const string new_filepath = Glib::build_filename (path, new_name+".template");
const string new_path = Glib::build_filename (user_template_directory(), new_name);
cout << old_filepath << " " << new_filepath << endl;
cout << path << " " << new_path << endl;
const string new_path = Glib::build_filename (user_template_directory (), new_name);
if (g_rename (old_filepath.c_str(), new_filepath.c_str()) != 0) {
error << string_compose (_("Renaming of the template file failed: %1"), strerror (errno)) << endmsg;
@ -232,6 +225,75 @@ TemplateDialog::rename_template (TreeModel::iterator& item, const Glib::ustring&
return;
}
item->set_value (_session_template_columns.name, string(new_name));
item->set_value (_session_template_columns.path, new_path);
item->set_value (_template_columns.name, string (new_name));
item->set_value (_template_columns.path, new_path);
}
void
SessionTemplateManager::delete_selected_template ()
{
if (_template_treeview.get_selection()->count_selected_rows() == 0) {
return;
}
Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
if (!it) {
return;
}
const string path = it->get_value (_template_columns.path);
const string name = it->get_value (_template_columns.name);
const string file_path = Glib::build_filename (path, name+".template");
if (g_unlink (file_path.c_str()) != 0) {
error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
return;
}
if (g_rmdir (path.c_str()) != 0) {
error << string_compose(_("Could not delete template directory \"%1\": %2"), path, strerror (errno)) << endmsg;
}
_template_model->erase (it);
row_selection_changed ();
}
void
RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
{
const string old_filepath = item->get_value (_template_columns.path);
const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
if (g_rename (old_filepath.c_str(), new_filepath.c_str()) != 0) {
error << string_compose (_("Renaming of the template file failed: %1"), strerror (errno)) << endmsg;
return;
}
item->set_value (_template_columns.name, string (new_name));
item->set_value (_template_columns.path, new_filepath);
}
void
RouteTemplateManager::delete_selected_template ()
{
if (_template_treeview.get_selection()->count_selected_rows() == 0) {
return;
}
Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
if (!it) {
return;
}
const string file_path = it->get_value (_template_columns.path);
if (g_unlink (file_path.c_str()) != 0) {
error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
return;
}
_template_model->erase (it);
row_selection_changed ();
}

View File

@ -28,14 +28,27 @@
#include "ardour_dialog.h"
namespace ARDOUR {
struct TemplateInfo;
}
class TemplateDialog : public ArdourDialog
{
public:
TemplateDialog ();
~TemplateDialog () {}
};
private:
void setup_session_templates ();
class TemplateManager : public Gtk::HBox
{
public:
virtual ~TemplateManager () {}
virtual void init () = 0;
protected:
TemplateManager ();
void setup_model (const std::vector<ARDOUR::TemplateInfo>& templates);
void row_selection_changed ();
void render_template_names (Gtk::CellRenderer* rnd, const Gtk::TreeModel::iterator& it);
@ -44,8 +57,8 @@ private:
bool key_event (GdkEventKey* ev);
void rename_template (Gtk::TreeModel::iterator& item, const Glib::ustring& new_name);
void delete_selected_template ();
virtual void rename_template (Gtk::TreeModel::iterator& item, const Glib::ustring& new_name) = 0;
virtual void delete_selected_template () = 0;
struct SessionTemplateColumns : public Gtk::TreeModel::ColumnRecord {
SessionTemplateColumns () {
@ -57,10 +70,10 @@ private:
Gtk::TreeModelColumn<std::string> path;
};
SessionTemplateColumns _session_template_columns;
Glib::RefPtr<Gtk::ListStore> _session_template_model;
SessionTemplateColumns _template_columns;
Glib::RefPtr<Gtk::ListStore> _template_model;
Gtk::TreeView _session_template_treeview;
Gtk::TreeView _template_treeview;
Gtk::CellRendererText _validating_cellrenderer;
Gtk::TreeView::Column _validated_column;
@ -68,4 +81,32 @@ private:
Gtk::Button _rename_button;
};
class SessionTemplateManager : public TemplateManager
{
public:
SessionTemplateManager () : TemplateManager () {}
~SessionTemplateManager () {}
void init ();
private:
void rename_template (Gtk::TreeModel::iterator& item, const Glib::ustring& new_name);
void delete_selected_template ();
};
class RouteTemplateManager : public TemplateManager
{
public:
RouteTemplateManager () : TemplateManager () {}
~RouteTemplateManager () {}
void init ();
private:
void rename_template (Gtk::TreeModel::iterator& item, const Glib::ustring& new_name);
void delete_selected_template ();
};
#endif /* __gtk2_ardour_template_dialog_h__ */