Enable the external inset to handle unknown templates gracefully.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7146 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Angus Leeming 2003-06-11 14:30:04 +00:00
parent 822efad779
commit 19e1271c83
10 changed files with 126 additions and 39 deletions

View File

@ -1,7 +1,12 @@
2003-06-11 Angus Leeming <leeming@lyx.org>
* ControlExternal.[Ch]: changes due to InsetExternal::Params no longer
storing the ExternalTemplate but its name.
2003-06-10 Angus Leeming <leeming@lyx.org> 2003-06-10 Angus Leeming <leeming@lyx.org>
* ControlExternal.C (editExternal): invokes new LFUN_EXTERNAL_EDIT rather * ControlExternal.C (editExternal): invokes new LFUN_EXTERNAL_EDIT
than generate a new inset itself. rather than generate a new inset itself.
2003-06-06 John Levon <levon@movementarian.org> 2003-06-06 John Levon <levon@movementarian.org>

View File

@ -19,6 +19,8 @@
#include "helper_funcs.h" #include "helper_funcs.h"
#include "lyxrc.h" #include "lyxrc.h"
#include "insets/ExternalTemplate.h"
#include "support/LAssert.h" #include "support/LAssert.h"
#include <vector> #include <vector>
@ -111,12 +113,26 @@ ExternalTemplate ControlExternal::getTemplate(int i) const
ExternalTemplateManager::Templates::const_iterator i1 ExternalTemplateManager::Templates::const_iterator i1
= ExternalTemplateManager::get().getTemplates().begin(); = ExternalTemplateManager::get().getTemplates().begin();
std::advance(i1, i); std::advance(i1, i);
return i1->second; return i1->second;
} }
namespace {
ExternalTemplate const * getTemplatePtr(InsetExternal::Params const & params)
{
ExternalTemplateManager & etm = ExternalTemplateManager::get();
ExternalTemplate const & templ = etm.getTemplateByName(params.templatename);
if (templ.lyxName.empty())
return 0;
return &templ;
}
} // namespace anon
string const ControlExternal::Browse(string const & input) const string const ControlExternal::Browse(string const & input) const
{ {
string const title = _("Select external file"); string const title = _("Select external file");
@ -124,10 +140,10 @@ string const ControlExternal::Browse(string const & input) const
string const bufpath = kernel().buffer()->filePath(); string const bufpath = kernel().buffer()->filePath();
/// Determine the template file extension /// Determine the template file extension
ExternalTemplate const & et = params().templ; string pattern = "*";
string pattern = et.fileRegExp; ExternalTemplate const * const et_ptr = getTemplatePtr(params());
if (pattern.empty()) if (et_ptr)
pattern = "*"; pattern = et_ptr->fileRegExp;
// FIXME: a temporary hack until the FileDialog interface is updated // FIXME: a temporary hack until the FileDialog interface is updated
pattern += '|'; pattern += '|';

View File

@ -20,6 +20,9 @@
#include <boost/scoped_ptr.hpp> #include <boost/scoped_ptr.hpp>
class ExternalTemplate;
class ControlExternal : public Dialog::Controller { class ControlExternal : public Dialog::Controller {
public: public:
/// ///

View File

@ -1,3 +1,8 @@
2003-06-11 Angus Leeming <leeming@lyx.org>
* QExternal.C: changes due to InsetExternal::Params no longer
storing the ExternalTemplate but its name.
2003-06-10 Angus Leeming <leeming@lyx.org> 2003-06-10 Angus Leeming <leeming@lyx.org>
* QDocument.C (apply): compilation fix after Lars' changes ;-) * QDocument.C (apply): compilation fix after Lars' changes ;-)

View File

@ -13,6 +13,9 @@
#include "ControlExternal.h" #include "ControlExternal.h"
#include "qt_helpers.h" #include "qt_helpers.h"
#include "insets/ExternalTemplate.h"
#include "support/lstrings.h" #include "support/lstrings.h"
#include "support/tostr.h" #include "support/tostr.h"
@ -63,7 +66,7 @@ void QExternal::update_contents()
dialog_->fileED->setText(toqstr(params.filename)); dialog_->fileED->setText(toqstr(params.filename));
dialog_->externalCO->setCurrentItem(controller().getTemplateNumber(params.templ.lyxName)); dialog_->externalCO->setCurrentItem(controller().getTemplateNumber(params.templatename));
dialog_->externalTV->setText(toqstr(helpText())); dialog_->externalTV->setText(toqstr(helpText()));
int item = 0; int item = 0;
@ -100,7 +103,8 @@ void QExternal::apply()
params.filename = fromqstr(dialog_->fileED->text()); params.filename = fromqstr(dialog_->fileED->text());
params.templ = controller().getTemplate(dialog_->externalCO->currentItem()); params.templatename =
controller().getTemplate(dialog_->externalCO->currentItem()).lyxName;
switch (dialog_->showCB->currentItem()) { switch (dialog_->showCB->currentItem()) {
case 0: params.display = grfx::DefaultDisplay; break; case 0: params.display = grfx::DefaultDisplay; break;

View File

@ -1,3 +1,8 @@
2003-06-11 Angus Leeming <leeming@lyx.org>
* FormExternal.C: changes due to InsetExternal::Params no longer
storing the ExternalTemplate but its name.
2003-06-10 Lars Gullik Bjønnes <larsbj@lyx.org> 2003-06-10 Lars Gullik Bjønnes <larsbj@lyx.org>
* XFormsView.C: remvoe current_view global variable. * XFormsView.C: remvoe current_view global variable.

View File

@ -24,6 +24,8 @@
#include "gettext.h" #include "gettext.h"
#include "insets/ExternalTemplate.h"
#include "support/tostr.h" #include "support/tostr.h"
#include "support/lstrings.h" #include "support/lstrings.h"
#include "lyx_forms.h" #include "lyx_forms.h"
@ -42,7 +44,7 @@ void FormExternal::apply()
params.filename = fl_get_input(dialog_->input_filename); params.filename = fl_get_input(dialog_->input_filename);
int const choice = fl_get_choice(dialog_->choice_template) - 1; int const choice = fl_get_choice(dialog_->choice_template) - 1;
params.templ = controller().getTemplate(choice); params.templatename = controller().getTemplate(choice).lyxName;
params.lyxscale = strToInt(getString(dialog_->input_lyxscale)); params.lyxscale = strToInt(getString(dialog_->input_lyxscale));
if (params.lyxscale == 0) if (params.lyxscale == 0)
@ -120,7 +122,7 @@ void FormExternal::update()
fl_set_input(dialog_->input_filename, params.filename.c_str()); fl_set_input(dialog_->input_filename, params.filename.c_str());
int ID = controller().getTemplateNumber(params.templ.lyxName); int ID = controller().getTemplateNumber(params.templatename);
if (ID < 0) ID = 0; if (ID < 0) ID = 0;
fl_set_choice(dialog_->choice_template, ID+1); fl_set_choice(dialog_->choice_template, ID+1);

View File

@ -1,3 +1,14 @@
2003-06-11 Angus Leeming <leeming@lyx.org>
* insetexternal.[Ch]: InsetExternal::Params no longer stores the
ExternalTemplate but its name, templatename. This means that a file
containing a reference to an External Template not installed on the
current machine will be saved retaining this info rather than inserting
the first "valid" template from the list of templates.
As a by product, the visual statement that the Template is missing is
now very clear!
2003-06-10 Lars Gullik Bjønnes <larsbj@lyx.org> 2003-06-10 Lars Gullik Bjønnes <larsbj@lyx.org>
* insettext.C (appendParagraphs): remove some commented code. * insettext.C (appendParagraphs): remove some commented code.

View File

@ -85,7 +85,6 @@ InsetExternal::InsetExternal()
: renderer_(new GraphicInset) : renderer_(new GraphicInset)
{ {
renderer_->connect(boost::bind(&InsetExternal::statusChanged, this)); renderer_->connect(boost::bind(&InsetExternal::statusChanged, this));
params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
} }
@ -201,34 +200,60 @@ grfx::Params get_grfx_params(InsetExternal::Params const & eparams,
return gparams; return gparams;
} }
ExternalTemplate const * getTemplatePtr(InsetExternal::Params const & params)
{
ExternalTemplateManager & etm = ExternalTemplateManager::get();
ExternalTemplate const & templ = etm.getTemplateByName(params.templatename);
if (templ.lyxName.empty())
return 0;
return &templ;
}
string const getScreenLabel(InsetExternal::Params const & params)
{
ExternalTemplate const * const ptr = getTemplatePtr(params);
if (!ptr)
return bformat(_("External template %1$s is not installed"),
params.templatename);
return doSubstitution(params, 0, ptr->guiName);
}
} // namespace anon } // namespace anon
void InsetExternal::setParams(Params const & p, string const & filepath) void InsetExternal::setParams(Params const & p, string const & filepath)
{ {
// The stored params; what we would like to happen in an ideal world.
params_.filename = p.filename; params_.filename = p.filename;
params_.templ = p.templ; params_.templatename = p.templatename;
params_.display = p.display; params_.display = p.display;
params_.lyxscale = p.lyxscale; params_.lyxscale = p.lyxscale;
// A temporary set of params; whether the thing can be displayed
// within LyX depends on the availability of this template.
Params tmp = params_;
if (!getTemplatePtr(params_))
tmp.display = grfx::NoDisplay;
// Update the display using the new parameters. // Update the display using the new parameters.
if (params_.filename.empty() || !filepath.empty()) if (params_.filename.empty() || !filepath.empty())
renderer_->update(get_grfx_params(params_, filepath)); renderer_->update(get_grfx_params(tmp, filepath));
string const msg = doSubstitution(params_, 0, params_.templ.guiName); renderer_->setNoDisplayMessage(getScreenLabel(params_));
renderer_->setNoDisplayMessage(msg);
} }
string const InsetExternal::editMessage() const string const InsetExternal::editMessage() const
{ {
return doSubstitution(params_, 0, params_.templ.guiName); return getScreenLabel(params_);
} }
void InsetExternal::write(Buffer const *, ostream & os) const void InsetExternal::write(Buffer const *, ostream & os) const
{ {
os << "External\n" os << "External\n"
<< "\ttemplate " << params_.templ.lyxName << '\n'; << "\ttemplate " << params_.templatename << '\n';
if (!params_.filename.empty()) if (!params_.filename.empty())
os << "\tfilename " << params_.filename << '\n'; os << "\tfilename " << params_.filename << '\n';
@ -270,10 +295,7 @@ void InsetExternal::read(Buffer const * buffer, LyXLex & lex)
switch (lex.lex()) { switch (lex.lex()) {
case EX_TEMPLATE: { case EX_TEMPLATE: {
lex.next(); lex.next();
string const name = lex.getString(); params.templatename = lex.getString();
ExternalTemplateManager & etm =
ExternalTemplateManager::get();
params.templ = etm.getTemplateByName(name);
break; break;
} }
@ -320,20 +342,15 @@ void InsetExternal::read(Buffer const * buffer, LyXLex & lex)
lex.popTable(); lex.popTable();
// Replace the inset's store // Replace the inset's store
params_ = params; string const path = buffer ? buffer->filePath() : string();
setParams(params, path);
lyxerr[Debug::INFO] << "InsetExternal::Read: " lyxerr[Debug::INFO] << "InsetExternal::Read: "
<< "template: '" << params_.templ.lyxName << "template: '" << params_.templatename
<< "' filename: '" << params_.filename << "' filename: '" << params_.filename
<< "' display: '" << params_.display << "' display: '" << params_.display
<< "' scale: '" << params_.lyxscale << "' scale: '" << params_.lyxscale
<< '\'' << endl; << '\'' << endl;
// Update the display using the new parameters.
if (buffer)
renderer_->update(get_grfx_params(params_, buffer->filePath()));
string const msg = doSubstitution(params_, 0, params_.templ.guiName);
renderer_->setNoDisplayMessage(msg);
} }
@ -341,13 +358,17 @@ int InsetExternal::write(string const & format,
Buffer const * buf, ostream & os, Buffer const * buf, ostream & os,
bool external_in_tmpdir) const bool external_in_tmpdir) const
{ {
ExternalTemplate const & et = params_.templ; ExternalTemplate const * const et_ptr = getTemplatePtr(params_);
if (!et_ptr)
return 0;
ExternalTemplate const & et = *et_ptr;
ExternalTemplate::Formats::const_iterator cit = ExternalTemplate::Formats::const_iterator cit =
et.formats.find(format); et.formats.find(format);
if (cit == et.formats.end()) { if (cit == et.formats.end()) {
lyxerr << "External template format '" << format lyxerr << "External template format '" << format
<< "' not specified in template " << "' not specified in template "
<< params_.templ.lyxName << endl; << params_.templatename << endl;
return 0; return 0;
} }
@ -371,7 +392,11 @@ int InsetExternal::latex(Buffer const * buf, ostream & os,
// If the template has specified a PDFLaTeX output, then we try and // If the template has specified a PDFLaTeX output, then we try and
// use that. // use that.
if (runparams.flavor == LatexRunParams::PDFLATEX) { if (runparams.flavor == LatexRunParams::PDFLATEX) {
ExternalTemplate const & et = params_.templ; ExternalTemplate const * const et_ptr = getTemplatePtr(params_);
if (!et_ptr)
return 0;
ExternalTemplate const & et = *et_ptr;
ExternalTemplate::Formats::const_iterator cit = ExternalTemplate::Formats::const_iterator cit =
et.formats.find("PDFLaTeX"); et.formats.find("PDFLaTeX");
if (cit != et.formats.end()) if (cit != et.formats.end())
@ -402,7 +427,11 @@ int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
void InsetExternal::validate(LaTeXFeatures & features) const void InsetExternal::validate(LaTeXFeatures & features) const
{ {
ExternalTemplate const & et = params_.templ; ExternalTemplate const * const et_ptr = getTemplatePtr(params_);
if (!et_ptr)
return;
ExternalTemplate const & et = *et_ptr;
ExternalTemplate::Formats::const_iterator cit = ExternalTemplate::Formats::const_iterator cit =
et.formats.find("LaTeX"); et.formats.find("LaTeX");
@ -422,7 +451,11 @@ void InsetExternal::updateExternal(string const & format,
Buffer const * buf, Buffer const * buf,
bool external_in_tmpdir) const bool external_in_tmpdir) const
{ {
ExternalTemplate const & et = params_.templ; ExternalTemplate const * const et_ptr = getTemplatePtr(params_);
if (!et_ptr)
return;
ExternalTemplate const & et = *et_ptr;
if (!et.automaticProduction) if (!et.automaticProduction)
return; return;
@ -535,7 +568,11 @@ void editExternal(InsetExternal::Params const & params, Buffer const * buffer)
if (!buffer) if (!buffer)
return; return;
ExternalTemplate const & et = params.templ; ExternalTemplate const * const et_ptr = getTemplatePtr(params);
if (!et_ptr)
return;
ExternalTemplate const & et = *et_ptr;
if (et.editCommand.empty()) if (et.editCommand.empty())
return; return;

View File

@ -13,7 +13,6 @@
#define INSET_EXTERNAL_H #define INSET_EXTERNAL_H
#include "inset.h" #include "inset.h"
#include "ExternalTemplate.h"
#include "graphics/GraphicsTypes.h" #include "graphics/GraphicsTypes.h"
#include "LString.h" #include "LString.h"
@ -32,10 +31,10 @@ public:
~Params(); ~Params();
/// the filename /// the filename
string filename; string filename;
/// the current template used
string templatename;
/// The name of the tempfile used for manipulations. /// The name of the tempfile used for manipulations.
string tempname; string tempname;
/// the current template used
ExternalTemplate templ;
/// how the inset is displayed by LyX /// how the inset is displayed by LyX
grfx::DisplayType display; grfx::DisplayType display;
/// The scale of the displayed graphic (If shown). /// The scale of the displayed graphic (If shown).