+namespace {
+
+vector<string> getRequiredList(string const & modName)
+{
+ LyXModule const * const mod = moduleList[modName];
+ if (!mod)
+ return vector<string>(); //empty such thing
+ return mod->getRequiredModules();
+}
+
+
+vector<string> getExcludedList(string const & modName)
+{
+ LyXModule const * const mod = moduleList[modName];
+ if (!mod)
+ return vector<string>(); //empty such thing
+ return mod->getExcludedModules();
+}
+
+
+docstring getModuleDescription(string const & modName)
+{
+ LyXModule const * const mod = moduleList[modName];
+ if (!mod)
+ return _("Module not found!");
+ return _(mod->getDescription());
+}
+
+
+vector<string> getPackageList(string const & modName)
+{
+ LyXModule const * const mod = moduleList[modName];
+ if (!mod)
+ return vector<string>(); //empty such thing
+ return mod->getPackageList();
+}
+
+
+bool isModuleAvailable(string const & modName)
+{
+ LyXModule * mod = moduleList[modName];
+ if (!mod)
+ return false;
+ return mod->isAvailable();
+}
+
+} // anonymous namespace
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// ModuleSelectionManager
+//
+/////////////////////////////////////////////////////////////////////
+
+/// SelectionManager for use with modules
+class ModuleSelectionManager : public GuiSelectionManager
+{
+public:
+ ModuleSelectionManager(
+ QListView * availableLV,
+ QListView * selectedLV,
+ QPushButton * addPB,
+ QPushButton * delPB,
+ QPushButton * upPB,
+ QPushButton * downPB,
+ GuiIdListModel * availableModel,
+ GuiIdListModel * selectedModel)
+ : GuiSelectionManager(availableLV, selectedLV, addPB, delPB,
+ upPB, downPB, availableModel, selectedModel)
+ {}
+
+
+private:
+ ///
+ virtual void updateAddPB();
+ ///
+ virtual void updateUpPB();
+ ///
+ virtual void updateDownPB();
+ ///
+ virtual void updateDelPB();
+ /// returns availableModel as a GuiIdListModel
+ GuiIdListModel * getAvailableModel()
+ {
+ return dynamic_cast<GuiIdListModel *>(availableModel);
+ }
+ /// returns selectedModel as a GuiIdListModel
+ GuiIdListModel * getSelectedModel()
+ {
+ return dynamic_cast<GuiIdListModel *>(selectedModel);
+ }
+};
+
+void ModuleSelectionManager::updateAddPB()
+{
+ int const arows = availableModel->rowCount();
+ QModelIndexList const availSels =
+ availableLV->selectionModel()->selectedIndexes();
+ if (arows == 0 || availSels.isEmpty() || isSelected(availSels.first())) {
+ addPB->setEnabled(false);
+ return;
+ }
+
+ QModelIndex const & idx = availableLV->selectionModel()->currentIndex();
+ string const modName = getAvailableModel()->getIDString(idx.row());
+ vector<string> reqs = getRequiredList(modName);
+ vector<string> excl = getExcludedList(modName);
+
+ if (reqs.empty() && excl.empty()) {
+ addPB->setEnabled(true);
+ return;
+ }
+
+ int const srows = selectedModel->rowCount();
+ vector<string> selModList;
+ for (int i = 0; i < srows; ++i)
+ selModList.push_back(getSelectedModel()->getIDString(i));
+
+ vector<string>::const_iterator selModStart = selModList.begin();
+ vector<string>::const_iterator selModEnd = selModList.end();
+
+ //Check whether some required module is available
+ if (!reqs.empty()) {
+ bool foundOne = false;
+ vector<string>::const_iterator it = reqs.begin();
+ vector<string>::const_iterator end = reqs.end();
+ for (; it != end; ++it) {
+ if (find(selModStart, selModEnd, *it) != selModEnd) {
+ foundOne = true;
+ break;
+ }
+ }
+ if (!foundOne) {
+ addPB->setEnabled(false);
+ return;
+ }
+ }
+
+ //Check whether any excluded module is being used
+ if (!excl.empty()) {
+ vector<string>::const_iterator it = excl.begin();
+ vector<string>::const_iterator end = excl.end();
+ for (; it != end; ++it) {
+ if (find(selModStart, selModEnd, *it) != selModEnd) {
+ addPB->setEnabled(false);
+ return;
+ }
+ }
+ }
+
+ addPB->setEnabled(true);
+}
+
+
+void ModuleSelectionManager::updateDownPB()
+{
+ int const srows = selectedModel->rowCount();
+ if (srows == 0) {
+ downPB->setEnabled(false);
+ return;
+ }
+ QModelIndexList const selSels =
+ selectedLV->selectionModel()->selectedIndexes();
+ //disable if empty or last item is selected
+ if (selSels.empty() || selSels.first().row() == srows - 1) {
+ downPB->setEnabled(false);
+ return;
+ }
+ //determine whether immediately succeding element requires this one
+ QModelIndex const & curIdx = selectedLV->selectionModel()->currentIndex();
+ int curRow = curIdx.row();
+ if (curRow < 0 || curRow >= srows - 1) { //this shouldn't happen...
+ downPB->setEnabled(false);
+ return;
+ }
+ string const curModName = getSelectedModel()->getIDString(curRow);
+ string const nextModName = getSelectedModel()->getIDString(curRow + 1);
+
+ vector<string> reqs = getRequiredList(nextModName);
+
+ //if it doesn't require anything....
+ if (reqs.empty()) {
+ downPB->setEnabled(true);
+ return;
+ }
+
+ //FIXME This should perhaps be more flexible and check whether, even
+ //if this one is required, there is also an earlier one that is required.
+ //enable it if this module isn't required
+ downPB->setEnabled(
+ find(reqs.begin(), reqs.end(), curModName) == reqs.end());
+}
+
+void ModuleSelectionManager::updateUpPB()
+{
+ int const srows = selectedModel->rowCount();
+ if (srows == 0) {
+ upPB->setEnabled(false);
+ return;
+ }
+ QModelIndexList const selSels =
+ selectedLV->selectionModel()->selectedIndexes();
+ //disable if empty or first item is selected
+ if (selSels.empty() || selSels.first().row() == 0) {
+ upPB->setEnabled(false);
+ return;
+ }
+
+ //determine whether immediately preceding element is required by this one
+ QModelIndex const & curIdx = selectedLV->selectionModel()->currentIndex();
+ int curRow = curIdx.row();
+ if (curRow <= -1 || curRow > srows - 1) { //sanity check
+ downPB->setEnabled(false);
+ return;
+ }
+ string const curModName = getSelectedModel()->getIDString(curRow);
+ vector<string> reqs = getRequiredList(curModName);
+
+ //if this one doesn't require anything....
+ if (reqs.empty()) {
+ upPB->setEnabled(true);
+ return;
+ }
+
+ string preModName = getSelectedModel()->getIDString(curRow - 1);
+
+ //NOTE This is less flexible than it might be. You could check whether, even
+ //if this one is required, there is also an earlier one that is required.
+ //enable it if the preceding module isn't required
+ upPB->setEnabled(find(reqs.begin(), reqs.end(), preModName) == reqs.end());
+}
+
+void ModuleSelectionManager::updateDelPB()
+{
+ int const srows = selectedModel->rowCount();
+ if (srows == 0) {
+ deletePB->setEnabled(false);
+ return;
+ }
+ QModelIndexList const selSels =
+ selectedLV->selectionModel()->selectedIndexes();
+ if (selSels.empty() || selSels.first().row() < 0) {
+ deletePB->setEnabled(false);
+ return;
+ }
+
+ //determine whether some LATER module requires this one
+ //NOTE Things are arranged so that this is the only way there
+ //can be a problem. At least, we hope so.
+ QModelIndex const & curIdx =
+ selectedLV->selectionModel()->currentIndex();
+ int const curRow = curIdx.row();
+ if (curRow < 0 || curRow >= srows) { //this shouldn't happen
+ deletePB->setEnabled(false);
+ return;
+ }
+
+ QString const curModName = curIdx.data().toString();
+
+ //We're looking here for a reason NOT to enable the button. If we
+ //find one, we disable it and return. If we don't, we'll end up at
+ //the end of the function, and then we enable it.
+ for (int i = curRow + 1; i < srows; ++i) {
+ string const thisMod = getSelectedModel()->getIDString(i);
+ vector<string> reqs = getRequiredList(thisMod);
+ //does this one require us?
+ if (find(reqs.begin(), reqs.end(), fromqstr(curModName)) == reqs.end())
+ //no...
+ continue;
+
+ //OK, so this module requires us
+ //is there an EARLIER module that also satisfies the require?
+ //NOTE We demand that it be earlier to keep the list of modules
+ //consistent with the rule that a module must be proceeded by a
+ //required module. There would be more flexible ways to proceed,
+ //but that would be a lot more complicated, and the logic here is
+ //already complicated. (That's why I've left the debugging code.)
+ //lyxerr << "Testing " << thisMod << std::endl;
+ bool foundOne = false;
+ for (int j = 0; j < curRow; ++j) {
+ string const mod = getSelectedModel()->getIDString(j);
+ //lyxerr << "In loop: Testing " << mod << std::endl;
+ //do we satisfy the require?
+ if (find(reqs.begin(), reqs.end(), mod) != reqs.end()) {
+ //lyxerr << mod << " does the trick." << std::endl;
+ foundOne = true;
+ break;
+ }
+ }
+ //did we find a module to satisfy the require?
+ if (!foundOne) {
+ //lyxerr << "No matching module found." << std::endl;
+ deletePB->setEnabled(false);
+ return;
+ }
+ }
+ //lyxerr << "All's well that ends well." << std::endl;
+ deletePB->setEnabled(true);
+}