diff options
| author | Carl Hetherington <cth@carlh.net> | 2024-05-09 21:33:56 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2024-05-25 01:30:31 +0200 |
| commit | 1e6484e0fe9e3fa090ef58e17433333a5378043c (patch) | |
| tree | 5bbb2e4efa39c5aee1d93bd618e5805b38ceb39f /src | |
| parent | 00706c7d9ded27a537af4e8182e67b460ec316bc (diff) | |
Improve save-template dialog and always use a default template.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/config.cc | 19 | ||||
| -rw-r--r-- | src/lib/config.h | 3 | ||||
| -rw-r--r-- | src/lib/film.cc | 18 | ||||
| -rw-r--r-- | src/lib/film.h | 2 | ||||
| -rw-r--r-- | src/tools/dcpomatic.cc | 13 | ||||
| -rw-r--r-- | src/wx/save_template_dialog.cc | 56 | ||||
| -rw-r--r-- | src/wx/save_template_dialog.h | 12 |
7 files changed, 103 insertions, 20 deletions
diff --git a/src/lib/config.cc b/src/lib/config.cc index 879c29279..1269206ee 100644 --- a/src/lib/config.cc +++ b/src/lib/config.cc @@ -1385,6 +1385,13 @@ Config::set_dkdm_recipients_file(boost::filesystem::path file) void +Config::save_default_template(shared_ptr<const Film> film) const +{ + film->write_template(write_path("default.xml")); +} + + +void Config::save_template (shared_ptr<const Film> film, string name) const { film->write_template (template_write_path(name)); @@ -1420,6 +1427,18 @@ Config::template_read_path (string name) const boost::filesystem::path +Config::default_template_read_path() const +{ + if (!boost::filesystem::exists(read_path("default.xml"))) { + auto film = std::make_shared<const Film>(optional<boost::filesystem::path>()); + save_default_template(film); + } + + return read_path("default.xml"); +} + + +boost::filesystem::path Config::template_write_path (string name) const { return write_path("templates") / tidy_for_filename (name); diff --git a/src/lib/config.h b/src/lib/config.h index 1a59f4bad..b8dab9a44 100644 --- a/src/lib/config.h +++ b/src/lib/config.h @@ -1268,10 +1268,13 @@ public: void copy_and_link (boost::filesystem::path new_file) const; bool have_write_permission () const; + void save_default_template(std::shared_ptr<const Film> film) const; void save_template (std::shared_ptr<const Film> film, std::string name) const; bool existing_template (std::string name) const; + /** @return Template names (not including the default) */ std::vector<std::string> templates() const; boost::filesystem::path template_read_path (std::string name) const; + boost::filesystem::path default_template_read_path() const; void rename_template (std::string old_name, std::string new_name) const; void delete_template (std::string name) const; diff --git a/src/lib/film.cc b/src/lib/film.cc index b25228ff9..32c236d36 100644 --- a/src/lib/film.cc +++ b/src/lib/film.cc @@ -153,6 +153,14 @@ int const Film::current_state_version = 38; /** Construct a Film object in a given directory. * + * Some initial values are taken from the Config object, and usually then overwritten + * by reading in the default template. Setting up things like _dcp_content_type in + * this constructor is useful so that if people configured such things in old versions + * they are used when the first default template is written by Config + * + * At some point these config entries can be removed, and this constructor could + * perhaps read the default template itself. + * * @param dir Film directory. */ @@ -1953,10 +1961,14 @@ Film::content_summary (DCPTimePeriod period) const } void -Film::use_template (string name) +Film::use_template(optional<string> name) { - _template_film.reset (new Film (optional<boost::filesystem::path>())); - _template_film->read_metadata (Config::instance()->template_read_path(name)); + _template_film = std::make_shared<Film>(optional<boost::filesystem::path>()); + if (name) { + _template_film->read_metadata(Config::instance()->template_read_path(*name)); + } else { + _template_film->read_metadata(Config::instance()->default_template_read_path()); + } _use_isdcf_name = _template_film->_use_isdcf_name; _dcp_content_type = _template_film->_dcp_content_type; _container = _template_film->_container; diff --git a/src/lib/film.h b/src/lib/film.h index baac7c4fe..d71435566 100644 --- a/src/lib/film.h +++ b/src/lib/film.h @@ -136,7 +136,7 @@ public: boost::filesystem::path file (boost::filesystem::path f) const; boost::filesystem::path dir (boost::filesystem::path d, bool create = true) const; - void use_template (std::string name); + void use_template(boost::optional<std::string> name); std::list<std::string> read_metadata (boost::optional<boost::filesystem::path> path = boost::optional<boost::filesystem::path> ()); void write_metadata (); void write_metadata (boost::filesystem::path path) const; diff --git a/src/tools/dcpomatic.cc b/src/tools/dcpomatic.cc index 63802279f..0a6728731 100644 --- a/src/tools/dcpomatic.cc +++ b/src/tools/dcpomatic.cc @@ -454,12 +454,13 @@ public: } } + /** Make a new film in the given path, using template_name as a template + * (or the default template if it's empty). + */ void new_film (boost::filesystem::path path, optional<string> template_name) { auto film = make_shared<Film>(path); - if (template_name) { - film->use_template (template_name.get()); - } + film->use_template(template_name); film->set_name (path.filename().generic_string()); film->write_metadata (); set_film (film); @@ -626,7 +627,11 @@ private: SaveTemplateDialog dialog(this); if (dialog.ShowModal() == wxID_OK) { try { - Config::instance()->save_template(_film, dialog.name()); + if (dialog.name()) { + Config::instance()->save_template(_film, *dialog.name()); + } else { + Config::instance()->save_default_template(_film); + } } catch (exception& e) { error_dialog(this, _("Could not save template."), std_to_wx(e.what())); } diff --git a/src/wx/save_template_dialog.cc b/src/wx/save_template_dialog.cc index 6a6644efe..0917387c9 100644 --- a/src/wx/save_template_dialog.cc +++ b/src/wx/save_template_dialog.cc @@ -18,54 +18,90 @@ */ + +#include "dcpomatic_choice.h" #include "save_template_dialog.h" #include "wx_util.h" #include "lib/config.h" +#include "lib/constants.h" + using std::string; #if BOOST_VERSION >= 106100 using namespace boost::placeholders; #endif + +using boost::optional; + + SaveTemplateDialog::SaveTemplateDialog (wxWindow* parent) : TableDialog (parent, _("Save template"), 2, 1, true) { - add (_("Template name"), true); - _name = add (new wxTextCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (300, -1))); - _name->SetFocus (); + _default = add(new wxRadioButton(this, wxID_ANY, _("Save as default"))); + add_spacer(); + _existing = add(new wxRadioButton(this, wxID_ANY, _("Save over existing template"))); + _existing_name = add(new Choice(this)); + _new = add(new wxRadioButton(this, wxID_ANY, _("Save as new with name"))); + _new_name = add(new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(300, -1))); + + _default->SetFocus (); layout (); - wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK, this)); + auto ok = dynamic_cast<wxButton *>(FindWindowById(wxID_OK, this)); ok->Bind (wxEVT_BUTTON, boost::bind(&SaveTemplateDialog::check, this, _1)); - _name->Bind (wxEVT_TEXT, boost::bind(&SaveTemplateDialog::setup_sensitivity, this)); + _default->Bind(wxEVT_RADIOBUTTON, boost::bind(&SaveTemplateDialog::setup_sensitivity, this)); + _existing->Bind(wxEVT_RADIOBUTTON, boost::bind(&SaveTemplateDialog::setup_sensitivity, this)); + _new->Bind(wxEVT_RADIOBUTTON, boost::bind(&SaveTemplateDialog::setup_sensitivity, this)); + _existing_name->Bind(wxEVT_TEXT, boost::bind(&SaveTemplateDialog::setup_sensitivity, this)); setup_sensitivity (); + + for (auto name: Config::instance()->templates()) { + _existing_name->add_entry(name); + } } void SaveTemplateDialog::setup_sensitivity () { - wxButton* ok = dynamic_cast<wxButton *>(FindWindowById(wxID_OK, this)); + auto const have_templates = !Config::instance()->templates().empty(); + _existing->Enable(have_templates); + _existing_name->Enable(have_templates && _existing->GetValue()); + _new_name->Enable(_new->GetValue()); + + auto ok = dynamic_cast<wxButton *>(FindWindowById(wxID_OK, this)); if (ok) { - ok->Enable (!_name->GetValue().IsEmpty()); + ok->Enable(_default->GetValue() || (_existing->GetValue() && have_templates) || (_new->GetValue() && !_new_name->GetValue().IsEmpty())); } } -string +optional<string> SaveTemplateDialog::name () const { - return wx_to_std (_name->GetValue ()); + if (_default->GetValue()) { + return {}; + } else if (_existing->GetValue()) { + DCPOMATIC_ASSERT(_existing_name->get()); + auto index = *_existing_name->get(); + auto templates = Config::instance()->templates(); + DCPOMATIC_ASSERT(index < int(templates.size())); + return templates[index]; + } else { + return wx_to_std(_new_name->GetValue()); + } } + void SaveTemplateDialog::check (wxCommandEvent& ev) { bool ok = true; - if (Config::instance()->existing_template (wx_to_std (_name->GetValue ()))) { + if (_new->GetValue() && Config::instance()->existing_template(wx_to_std(_new_name->GetValue()))) { ok = confirm_dialog (this, _("There is already a template with this name. Do you want to overwrite it?")); } diff --git a/src/wx/save_template_dialog.h b/src/wx/save_template_dialog.h index 7e4808ca2..101d35ddf 100644 --- a/src/wx/save_template_dialog.h +++ b/src/wx/save_template_dialog.h @@ -18,18 +18,26 @@ */ + #include "table_dialog.h" +class Choice; + + class SaveTemplateDialog : public TableDialog { public: explicit SaveTemplateDialog (wxWindow* parent); - std::string name () const; + boost::optional<std::string> name() const; private: void setup_sensitivity (); void check (wxCommandEvent& ev); - wxTextCtrl* _name; + wxRadioButton* _default; + wxRadioButton* _existing; + Choice* _existing_name; + wxRadioButton* _new; + wxTextCtrl* _new_name; }; |
