Add a basic management dialogue for templates.
authorCarl Hetherington <cth@carlh.net>
Wed, 24 Aug 2016 15:45:34 +0000 (16:45 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 24 Aug 2016 15:45:34 +0000 (16:45 +0100)
src/lib/config.cc
src/lib/config.h
src/tools/dcpomatic.cc
src/wx/new_film_dialog.cc
src/wx/rename_template_dialog.cc [new file with mode: 0644]
src/wx/rename_template_dialog.h [new file with mode: 0644]
src/wx/save_template_dialog.cc
src/wx/save_template_dialog.h
src/wx/templates_dialog.cc [new file with mode: 0644]
src/wx/templates_dialog.h [new file with mode: 0644]
src/wx/wscript

index 8f9cfab4b2543cdb330b21df2be533073981458e..1d9cce64e9fb81516081423b7d8a740492f8c306 100644 (file)
@@ -597,7 +597,7 @@ Config::save_template (shared_ptr<const Film> film, string name) const
 }
 
 list<string>
-Config::template_names () const
+Config::templates () const
 {
        list<string> n;
        for (boost::filesystem::directory_iterator i (path("templates")); i != boost::filesystem::directory_iterator(); ++i) {
@@ -617,3 +617,15 @@ Config::template_path (string name) const
 {
        return path("templates") / tidy_for_filename (name);
 }
+
+void
+Config::rename_template (string old_name, string new_name) const
+{
+       boost::filesystem::rename (template_path (old_name), template_path (new_name));
+}
+
+void
+Config::delete_template (string name) const
+{
+       boost::filesystem::remove (template_path (name));
+}
index 213c13a83db81beeee4e3b85957fed6f67914431..e18cd33121da70ea46a7429afd832bb17a8ce610 100644 (file)
@@ -517,8 +517,10 @@ public:
 
        void save_template (boost::shared_ptr<const Film> film, std::string name) const;
        bool existing_template (std::string name) const;
-       std::list<std::string> template_names () const;
+       std::list<std::string> templates () const;
        boost::filesystem::path template_path (std::string name) const;
+       void rename_template (std::string old_name, std::string new_name) const;
+       void delete_template (std::string name) const;
 
        static Config* instance ();
        static void drop ();
index 63e5ca6b14834e85dc1acbed2aac0a2845cace52..3440536e6479bc9d9b8a92d27ae0deb3b7e8b4b1 100644 (file)
@@ -39,6 +39,7 @@
 #include "wx/report_problem_dialog.h"
 #include "wx/video_waveform_dialog.h"
 #include "wx/save_template_dialog.h"
+#include "wx/templates_dialog.h"
 #include "lib/film.h"
 #include "lib/config.h"
 #include "lib/util.h"
@@ -158,6 +159,7 @@ enum {
        ID_tools_video_waveform,
        ID_tools_hints,
        ID_tools_encoding_servers,
+       ID_tools_manage_templates,
        ID_tools_check_for_updates,
        ID_tools_restore_default_preferences,
        ID_help_report_a_problem,
@@ -176,6 +178,7 @@ public:
                , _servers_list_dialog (0)
                , _config_dialog (0)
                , _kdm_dialog (0)
+               , _templates_dialog (0)
                , _file_menu (0)
                , _history_items (0)
                , _history_position (0)
@@ -227,6 +230,7 @@ public:
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_video_waveform, this),    ID_tools_video_waveform);
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_hints, this),             ID_tools_hints);
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_encoding_servers, this),  ID_tools_encoding_servers);
+               Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_manage_templates, this),  ID_tools_manage_templates);
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_restore_default_preferences, this), ID_tools_restore_default_preferences);
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_about, this),              wxID_ABOUT);
@@ -417,13 +421,7 @@ private:
                SaveTemplateDialog* d = new SaveTemplateDialog (this);
                int const r = d->ShowModal ();
                if (r == wxID_OK) {
-                       bool ok = true;
-                       if (Config::instance()->existing_template (d->name ())) {
-                               ok = confirm_dialog (d, _("There is already a template with this name.  Do you want to overwrite it?"));
-                       }
-                       if (ok) {
-                               Config::instance()->save_template (_film, d->name ());
-                       }
+                       Config::instance()->save_template (_film, d->name ());
                }
                d->Destroy ();
        }
@@ -701,6 +699,15 @@ private:
                _servers_list_dialog->Show ();
        }
 
+       void tools_manage_templates ()
+       {
+               if (!_templates_dialog) {
+                       _templates_dialog = new TemplatesDialog (this);
+               }
+
+               _templates_dialog->Show ();
+       }
+
        void tools_check_for_updates ()
        {
                UpdateChecker::instance()->run ();
@@ -890,6 +897,7 @@ private:
                add_item (tools, _("Video waveform..."), ID_tools_video_waveform, NEEDS_FILM);
                add_item (tools, _("Hints..."), ID_tools_hints, 0);
                add_item (tools, _("Encoding servers..."), ID_tools_encoding_servers, 0);
+               add_item (tools, _("Manage templates..."), ID_tools_manage_templates, 0);
                add_item (tools, _("Check for updates"), ID_tools_check_for_updates, 0);
                tools->AppendSeparator ();
                add_item (tools, _("Restore default preferences"), ID_tools_restore_default_preferences, ALWAYS);
@@ -980,6 +988,7 @@ private:
        ServersListDialog* _servers_list_dialog;
        wxPreferencesEditor* _config_dialog;
        KDMDialog* _kdm_dialog;
+       TemplatesDialog* _templates_dialog;
        wxMenu* _file_menu;
        shared_ptr<Film> _film;
        int _history_items;
index 907b289b5c5fba150902771f1df7125ef0353784..5d4581d3c48787a4818a087a78955a5aa02b0bef 100644 (file)
@@ -62,7 +62,7 @@ NewFilmDialog::NewFilmDialog (wxWindow* parent)
        _name->SetFocus ();
        _template_name->Enable (false);
 
-       BOOST_FOREACH (string i, Config::instance()->template_names ()) {
+       BOOST_FOREACH (string i, Config::instance()->templates ()) {
                _template_name->Append (std_to_wx (i));
        }
 
diff --git a/src/wx/rename_template_dialog.cc b/src/wx/rename_template_dialog.cc
new file mode 100644 (file)
index 0000000..6651d09
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
+
+    This file is part of DCP-o-matic.
+
+    DCP-o-matic is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    DCP-o-matic is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "rename_template_dialog.h"
+
+RenameTemplateDialog::RenameTemplateDialog (wxWindow* parent)
+       : TableDialog (parent, _("Rename template"), 2, 1, true)
+{
+       add (_("New name"), true);
+       _name = add (new wxTextCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (300, -1)));
+       layout ();
+}
+
+wxString
+RenameTemplateDialog::get () const
+{
+       return _name->GetValue ();
+}
+
+void
+RenameTemplateDialog::set (wxString s)
+{
+       _name->SetValue (s);
+}
diff --git a/src/wx/rename_template_dialog.h b/src/wx/rename_template_dialog.h
new file mode 100644 (file)
index 0000000..bef9f54
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
+
+    This file is part of DCP-o-matic.
+
+    DCP-o-matic is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    DCP-o-matic is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "table_dialog.h"
+
+class RenameTemplateDialog : public TableDialog
+{
+public:
+       RenameTemplateDialog (wxWindow* parent);
+
+       void set (wxString n);
+       wxString get () const;
+
+private:
+       wxTextCtrl* _name;
+};
index eff61a2563a6e505fc40c29859ea810504f5464b..23dc0a85a85fe8d2b4319f03168f8fa1d7fc0180 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "save_template_dialog.h"
 #include "wx_util.h"
+#include "lib/config.h"
 #include <boost/foreach.hpp>
 
 using std::string;
@@ -31,6 +32,9 @@ SaveTemplateDialog::SaveTemplateDialog (wxWindow* parent)
        _name = add (new wxTextCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (300, -1)));
        _name->SetFocus ();
        layout ();
+
+       wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK, this));
+       ok->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&SaveTemplateDialog::check, this, _1));
 }
 
 string
@@ -38,3 +42,20 @@ SaveTemplateDialog::name () const
 {
        return wx_to_std (_name->GetValue ());
 }
+
+void
+SaveTemplateDialog::check (wxCommandEvent& ev)
+{
+       bool ok = true;
+
+       if (_name->GetValue().IsEmpty()) {
+               error_dialog (this, _("Template names must not be empty."));
+               ok = false;
+       } else if (Config::instance()->existing_template (wx_to_std (_name->GetValue ()))) {
+               ok = confirm_dialog (this, _("There is already a template with this name.  Do you want to overwrite it?"));
+       }
+
+       if (ok) {
+               ev.Skip ();
+       }
+}
index 8069fa2f0c39c6ada445c562d2dc627d10754d52..5fc4300f8ea9378da5ecb4f4e21ff920d9fa03ed 100644 (file)
@@ -28,5 +28,7 @@ public:
        std::string name () const;
 
 private:
+       void check (wxCommandEvent& ev);
+
        wxTextCtrl* _name;
 };
diff --git a/src/wx/templates_dialog.cc b/src/wx/templates_dialog.cc
new file mode 100644 (file)
index 0000000..eb0d491
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
+
+    This file is part of DCP-o-matic.
+
+    DCP-o-matic is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    DCP-o-matic is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "templates_dialog.h"
+#include "wx_util.h"
+#include "rename_template_dialog.h"
+#include "lib/config.h"
+#include <wx/wx.h>
+#include <boost/foreach.hpp>
+
+using std::string;
+using boost::bind;
+
+TemplatesDialog::TemplatesDialog (wxWindow* parent)
+       : wxDialog (parent, wxID_ANY, _("Templates"))
+{
+       _sizer = new wxBoxSizer (wxVERTICAL);
+       SetSizer (_sizer);
+
+       wxSizer* hs = new wxBoxSizer (wxHORIZONTAL);
+       _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (200, 100), wxLC_REPORT | wxLC_SINGLE_SEL);
+
+       wxListItem ip;
+       ip.SetId (0);
+       ip.SetText (_("Template"));
+       ip.SetWidth (200);
+       _list->InsertColumn (0, ip);
+
+       hs->Add (_list, 1, wxEXPAND, DCPOMATIC_SIZER_GAP);
+
+       {
+               wxSizer* s = new wxBoxSizer (wxVERTICAL);
+               _rename = new wxButton (this, wxID_ANY, _("Rename..."));
+               s->Add (_rename, 0, wxTOP | wxBOTTOM, 2);
+               _remove = new wxButton (this, wxID_ANY, _("Remove"));
+               s->Add (_remove, 0, wxTOP | wxBOTTOM, 2);
+               hs->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
+       }
+
+       _sizer->Add (hs, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
+
+       wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
+       if (buttons) {
+               _sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
+       }
+
+       _rename->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&TemplatesDialog::rename_clicked, this));
+       _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&TemplatesDialog::remove_clicked, this));
+
+       _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind (&TemplatesDialog::selection_changed, this));
+       _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind (&TemplatesDialog::selection_changed, this));
+       _list->Bind (wxEVT_SIZE, bind (&TemplatesDialog::resized, this, _1));
+       _config_connection = Config::instance()->Changed.connect (bind (&TemplatesDialog::refresh, this));
+
+       refresh ();
+       selection_changed ();
+}
+
+void
+TemplatesDialog::refresh ()
+{
+       _list->DeleteAllItems ();
+
+       BOOST_FOREACH (string i, Config::instance()->templates()) {
+               wxListItem list_item;
+               int const n = _list->GetItemCount ();
+               list_item.SetId (n);
+               _list->InsertItem (list_item);
+               _list->SetItem (n, 0, std_to_wx (i));
+       }
+}
+
+void
+TemplatesDialog::layout ()
+{
+       _sizer->Layout ();
+}
+
+void
+TemplatesDialog::selection_changed ()
+{
+       int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+       _rename->Enable (i >= 0);
+       _remove->Enable (i >= 0);
+}
+
+void
+TemplatesDialog::rename_clicked ()
+{
+       int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+       if (item == -1) {
+               return;
+       }
+
+       wxListItem li;
+       li.m_itemId = item;
+       li.m_col = 0;
+       li.m_mask = wxLIST_MASK_TEXT;
+       _list->GetItem (li);
+
+       RenameTemplateDialog* d = new RenameTemplateDialog (this);
+       d->set (li.m_text);
+       if (d->ShowModal() == wxID_OK) {
+               if (!d->get().IsEmpty()) {
+                       Config::instance()->rename_template (wx_to_std (li.m_text), wx_to_std (d->get ()));
+                       _list->SetItem (item, 0, d->get());
+               } else {
+                       error_dialog (this, _("Template names must not be empty."));
+               }
+       }
+       d->Destroy ();
+}
+
+void
+TemplatesDialog::remove_clicked ()
+{
+       int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+       if (i == -1) {
+               return;
+       }
+
+       wxListItem li;
+       li.m_itemId = i;
+       li.m_col = 0;
+       li.m_mask = wxLIST_MASK_TEXT;
+       _list->GetItem (li);
+
+       Config::instance()->delete_template (wx_to_std (li.m_text));
+       _list->DeleteItem (i);
+
+       selection_changed ();
+}
+
+void
+TemplatesDialog::resized (wxSizeEvent& ev)
+{
+       _list->SetColumnWidth (0, GetSize().GetWidth());
+       ev.Skip ();
+}
diff --git a/src/wx/templates_dialog.h b/src/wx/templates_dialog.h
new file mode 100644 (file)
index 0000000..0395739
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
+
+    This file is part of DCP-o-matic.
+
+    DCP-o-matic is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    DCP-o-matic is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include <wx/wx.h>
+#include <wx/listctrl.h>
+#include <boost/signals2.hpp>
+
+class TemplatesDialog : public wxDialog
+{
+public:
+       TemplatesDialog (wxWindow* parent);
+
+       void refresh ();
+       void layout ();
+
+private:
+       void selection_changed ();
+       void rename_clicked ();
+       void remove_clicked ();
+       void resized (wxSizeEvent& ev);
+
+       wxButton* _rename;
+       wxButton* _remove;
+       wxListCtrl* _list;
+       wxBoxSizer* _sizer;
+
+       boost::signals2::scoped_connection _config_connection;
+};
index 1f9aed6b3c4c79aebaf453630ce9ad36e73bb1fe..f89b60c40fc4f707d29a1ff656a6c681eb74ec12 100644 (file)
@@ -74,6 +74,7 @@ sources = """
           playhead_to_frame_dialog.cc
           repeat_dialog.cc
           report_problem_dialog.cc
+          rename_template_dialog.cc
           rgba_colour_picker.cc
           save_template_dialog.cc
           screen_dialog.cc
@@ -85,6 +86,7 @@ sources = """
           subtitle_view.cc
           system_font_dialog.cc
           table_dialog.cc
+          templates_dialog.cc
           text_subtitle_appearance_dialog.cc
           time_picker.cc
           timecode.cc