Fix LFUN_LAYOUT_MODULE_ADD: We need to make sure that the module

isn't already added, that its requirements are satisfied, and that
it doesn't conflict with any loaded modules. Mostly, this just
moves existing code into a new routine where we can get at it.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26995 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Richard Heck 2008-10-20 19:48:32 +00:00
parent 67dfff2d37
commit 2351615f0a
3 changed files with 60 additions and 57 deletions

View File

@ -1481,13 +1481,6 @@ void BufferParams::addDefaultModules()
for (; mit != men; mit++) {
string const & modName = *mit;
// see if we're already in use
if (find(layoutModules_.begin(), layoutModules_.end(), modName) !=
layoutModules_.end()) {
LYXERR(Debug::TCLASS, "Default module `" << modName <<
"' not added because already used.");
continue;
}
// make sure the user hasn't removed it
if (find(removedModules_.begin(), removedModules_.end(), modName) !=
removedModules_.end()) {
@ -1496,50 +1489,12 @@ void BufferParams::addDefaultModules()
continue;
}
// Now we want to check the list of selected modules to see if any of them
// excludes this one, or if we exclude one of them.
LyXModule * thismod = moduleList[modName];
if (!thismod) {
LYXERR0("Adding default module " << modName <<
" even though it is unavailable.");
modulesToAdd.push_back(modName);
continue;
}
bool foundit = false;
vector<string> const ourExcMods = thismod->getExcludedModules();
vector<string>::const_iterator const eit = ourExcMods.begin();
vector<string>::const_iterator const een = ourExcMods.end();
// so iterate over the selected modules...
LayoutModuleList::const_iterator lit = layoutModules_.begin();
LayoutModuleList::const_iterator const len = layoutModules_.end();
for (; lit != len; lit++) {
LyXModule * lm = moduleList[*lit];
if (!lm)
continue;
vector<string> const & exc = lm->getExcludedModules();
// ...and see if this one excludes us.
if (find(exc.begin(), exc.end(), modName) != exc.end()) {
foundit = true;
LYXERR(Debug::TCLASS, "Default module `" << modName <<
"' not added because excluded by loaded module `" <<
*lit << "'.");
break;
}
if (find(eit, een, *lit) != een) {
foundit = true;
LYXERR(Debug::TCLASS, "Default module `" << modName <<
"' not added because it excludes loaded module `" <<
*lit << "'.");
break;
}
}
if (!foundit) {
if (moduleCanBeAdded(modName)) {
LYXERR(Debug::TCLASS, "Default module `" << modName << "' added.");
modulesToAdd.push_back(modName);
}
} else
LYXERR(Debug::TCLASS,
"Default module `" << modName << "' could not be added.");
}
// OK, now we can add the default modules.
@ -1643,7 +1598,47 @@ void BufferParams::makeDocumentClass()
}
bool BufferParams::addLayoutModule(string const & modName)
bool BufferParams::moduleCanBeAdded(string const & modName) const
{
// Is the module already present?
LayoutModuleList::const_iterator it = layoutModules_.begin();
LayoutModuleList::const_iterator end = layoutModules_.end();
for (; it != end; it++)
if (*it == modName)
return false;
LyXModule const * const lm = moduleList[modName];
if (!lm)
return true;
LayoutModuleList::const_iterator mit = getModules().begin();
LayoutModuleList::const_iterator const men = getModules().end();
// Check for conflicts with used modules
for (; mit != men; ++mit)
if (!LyXModule::areCompatible(modName, *mit))
return false;
// Check whether some required module is available
vector<string> const reqs = lm->getRequiredModules();
if (reqs.empty())
return true;
mit = getModules().begin(); // reset
vector<string>::const_iterator rit = reqs.begin();
vector<string>::const_iterator ren = reqs.end();
bool foundOne = false;
for (; rit != ren; ++rit) {
if (find(mit, men, *rit) != men) {
foundOne = true;
break;
}
}
return foundOne;
}
bool BufferParams::addLayoutModule(string const & modName)
{
LayoutModuleList::const_iterator it = layoutModules_.begin();
LayoutModuleList::const_iterator end = layoutModules_.end();

View File

@ -133,13 +133,14 @@ public:
std::set<std::string> const & getRemovedModules() const
{ return removedModules_; }
///
/// Add a module to the list of modules in use.
/// Returns true if module was successfully added.
/// The makeClass variable signals whether to call makeDocumentClass. This
/// need not be done if we know this isn't the final time through, or if
/// the BufferParams do not represent the parameters for an actual buffer
/// (as in GuiDocument).
/// Add a module to the list of modules in use. This checks only that the
/// module is not already in the list, so use moduleIsCompatible first if
/// you want to check for compatibility.
/// \return true if module was successfully added.
bool addLayoutModule(std::string const & modName);
/// checks to make sure module's requriements are satisfied, that it does
/// not conflict with already-present modules, isn't already loaded, etc.
bool moduleCanBeAdded(std::string const & modName) const;
///
void addRemovedModule(std::string const & modName)
{ removedModules_.insert(modName); }

View File

@ -1499,7 +1499,14 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
case LFUN_LAYOUT_MODULE_ADD: {
LASSERT(lyx_view_, /**/);
Buffer * buffer = lyx_view_->buffer();
DocumentClass const * const oldClass = buffer->params().documentClassPtr();
BufferParams const & params = buffer->params();
if (!params.moduleCanBeAdded(argument)) {
LYXERR0("Module `" << argument <<
"' cannot be added due to failed requirements or "
"conflicts with installed modules.");
break;
}
DocumentClass const * const oldClass = params.documentClassPtr();
view()->cursor().recordUndoFullDocument();
buffer->params().addLayoutModule(argument);
buffer->params().makeDocumentClass();