From: Jürgen Spitzmüller Date: Fri, 10 Jul 2009 06:49:51 +0000 (+0000) Subject: * Add ability to rename branches (bug 4128). X-Git-Tag: 2.0.0~6118 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=5fdd2daa9c30c70a6c049376bd293addb05b5aad;p=features.git * Add ability to rename branches (bug 4128). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@30442 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/BranchList.cpp b/src/BranchList.cpp index a593c5478f..02f8b8688d 100644 --- a/src/BranchList.cpp +++ b/src/BranchList.cpp @@ -150,4 +150,25 @@ bool BranchList::remove(docstring const & s) } +bool BranchList::rename(docstring const & oldname, + docstring const & newname, bool const merge) +{ + if (newname.empty()) + return false; + if (find_if(list.begin(), list.end(), + BranchNamesEqual(newname)) != list.end()) { + // new name already taken + if (merge) + return remove(oldname); + return false; + } + + Branch * branch = find(oldname); + if (!branch) + return false; + branch->setBranch(newname); + return true; +} + + } // namespace lyx diff --git a/src/BranchList.h b/src/BranchList.h index ce21d48e28..08a719a1c7 100644 --- a/src/BranchList.h +++ b/src/BranchList.h @@ -105,6 +105,12 @@ public: * \returns true if a branch is removed. */ bool remove(docstring const &); + /** rename an branch in list + * \returns true if renaming succeeded. + * if \p merge is true, the branch will be removed + * if a branch with the newname already exists. + */ + bool rename(docstring const &, docstring const &, bool const merge = false); private: /// diff --git a/src/Buffer.cpp b/src/Buffer.cpp index f0c4a53859..53ade6faf3 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -1623,6 +1623,7 @@ bool Buffer::getStatus(FuncRequest const & cmd, FuncStatus & flag) } case LFUN_BRANCH_ADD: + case LFUN_BRANCHES_RENAME: case LFUN_BUFFER_PRINT: // if no Buffer is present, then of course we won't be called! flag.setEnabled(true); @@ -1705,6 +1706,13 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr) break; } + case LFUN_BRANCHES_RENAME: { + docstring const oldname = from_utf8(func.getArg(0)); + docstring const newname = from_utf8(func.getArg(1)); + renameBranches(oldname, newname); + break; + } + case LFUN_BUFFER_PRINT: { // we'll assume there's a problem until we succeed dr.setError(true); @@ -2405,6 +2413,54 @@ void Buffer::getUsedBranches(std::list & result, bool const from_mast } +void Buffer::renameBranches(docstring const & oldname, docstring const & newname) +{ + // Iterate over buffer, starting with first paragraph + // The scope must be bigger than any lookup DocIterator + // later. For the global lookup, lastpit+1 is used, hence + // we use lastpit+2 here. + DocIterator it = par_iterator_begin(); + DocIterator scope = it; + scope.pit() = scope.lastpit() + 2; + pit_type lastpit = it.lastpit(); + + while (it.pit() <= lastpit) { + Paragraph & par = it.paragraph(); + + // iterate over the insets of the current paragraph + InsetList const & insets = par.insetList(); + InsetList::const_iterator iit = insets.begin(); + InsetList::const_iterator end = insets.end(); + for (; iit != end; ++iit) { + it.pos() = iit->pos; + + if (iit->inset->lyxCode() == BRANCH_CODE) { + // get buffer of external file + InsetBranch & br = + static_cast(*iit->inset); + if (br.branch() == oldname) + br.rename(newname); + continue; + } + + // is it an external file? + if (iit->inset->lyxCode() == INCLUDE_CODE) { + // get buffer of external file + InsetInclude const & inset = + static_cast(*iit->inset); + Buffer * child = inset.getChildBuffer(); + if (!child) + continue; + child->renameBranches(oldname, newname); + } + } + // next paragraph + it.pit()++; + it.pos() = 0; + } +} + + void Buffer::updateMacroInstances() const { LYXERR(Debug::MACROS, "updateMacroInstances for " diff --git a/src/Buffer.h b/src/Buffer.h index 71e8ac32c1..012f73bf67 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -509,6 +509,8 @@ public: /// return a list of all used branches (also in children) void getUsedBranches(std::list &, bool const from_master = false) const; + /// rename all branches of \p oldname in the buffer to \p newname. + void renameBranches(docstring const & oldname, docstring const & newname); /// sets the buffer_ member for every inset in this buffer. // FIXME This really shouldn't be needed, but at the moment it's not diff --git a/src/FuncCode.h b/src/FuncCode.h index e618bbe1f6..3d879d0424 100644 --- a/src/FuncCode.h +++ b/src/FuncCode.h @@ -436,6 +436,7 @@ enum FuncCode LFUN_BUFFER_EXPORT, // Lgb 97-07-29 LFUN_BUFFER_TOGGLE_COMPRESSION, // bpeng 20060427 LFUN_BRANCH_ADD, // spitz 20090707 + LFUN_BRANCHES_RENAME, // spitz 20090709 LFUN_LASTACTION // end of the table }; diff --git a/src/LaTeXFeatures.cpp b/src/LaTeXFeatures.cpp index e39dc92bb7..918fc0b3c6 100644 --- a/src/LaTeXFeatures.cpp +++ b/src/LaTeXFeatures.cpp @@ -195,8 +195,11 @@ static docstring const changetracking_none_def = from_ascii( "\\newcommand{\\lyxdeleted}[3]{}\n"); static docstring const textgreek_def = from_ascii( - "\\DeclareRobustCommand{\\greektext}{%\n" - " \\fontencoding{LGR}\\selectfont\\def\\encodingdefault{LGR}}\n" + "\\providecommand*{\\perispomeni}{\\char126}\n" + "\\AtBeginDocument{\\DeclareRobustCommand{\\greektext}{%\n" + " \\fontencoding{LGR}\\selectfont\\def\\encodingdefault{LGR}\n" + " \\renewcommand{\\~}{\\perispomeni}\n" + "}}\n" "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n" "\\DeclareFontEncoding{LGR}{}{}\n"); diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index dace7fa57e..4282139e42 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -3317,6 +3317,17 @@ void LyXAction::init() */ { LFUN_BRANCH_DEACTIVATE, "branch-deactivate", AtPoint, Buffer }, +/*! + * \var lyx::FuncCode lyx::LFUN_BRANCHES_RENAME + * \li Action: Rename all branches of a given name in a document + * \li Syntax: branches-rename + * \li Params: : Current name of the branch to be renamed + * : New name of the branch + * \li Origin: spitz, 9 Jul 2009 + * \endvar + */ + { LFUN_BRANCHES_RENAME, "branches-rename", Noop, Buffer }, + /*! * \var lyx::FuncCode lyx::LFUN_LABEL_COPY_AS_REF * \li Action: Copies the label at the cursor as a cross-reference to be pasted elsewhere. diff --git a/src/frontends/qt4/GuiBranches.cpp b/src/frontends/qt4/GuiBranches.cpp index 806f344f13..45e60de962 100644 --- a/src/frontends/qt4/GuiBranches.cpp +++ b/src/frontends/qt4/GuiBranches.cpp @@ -20,9 +20,12 @@ #include "ui_BranchesUnknownUi.h" +#include "frontends/alert.h" + #include "Buffer.h" #include "BufferParams.h" +#include "support/gettext.h" #include "support/lstrings.h" #include @@ -141,6 +144,40 @@ void GuiBranches::on_removePB_pressed() } +void GuiBranches::on_renamePB_pressed() +{ + QTreeWidgetItem * selItem = branchesTW->currentItem(); + QString sel_branch; + if (selItem != 0) + sel_branch = selItem->text(0); + if (!sel_branch.isEmpty()) { + docstring newname; + docstring const oldname = qstring_to_ucs4(sel_branch); + bool success = false; + if (Alert::askForText(newname, _("Enter new branch name"))) { + if (branchlist_.find(newname)) { + docstring text = support::bformat( + _("A branch with the name \"%1$s\" already exists.\n" + "Do you want to merge branch \"%2$s\" with that one?"), + newname, oldname); + if (frontend::Alert::prompt(_("Branch already exists"), + text, 0, 1, _("&Merge"), _("&Cancel")) == 0) + success = branchlist_.rename(oldname, newname, true); + } else + success = branchlist_.rename(oldname, newname); + newBranchLE->clear(); + updateView(); + } + if (!success) + Alert::error(_("Renaming failed"), + _("The branch could not be renamed.")); + else + // emit signal + renameBranches(oldname, newname); + } +} + + void GuiBranches::on_activatePB_pressed() { toggleBranch(branchesTW->currentItem()); diff --git a/src/frontends/qt4/GuiBranches.h b/src/frontends/qt4/GuiBranches.h index 3edf0970ea..514878ce20 100644 --- a/src/frontends/qt4/GuiBranches.h +++ b/src/frontends/qt4/GuiBranches.h @@ -50,6 +50,7 @@ public: Q_SIGNALS: void changed(); + void renameBranches(docstring const &, docstring const &); protected: void toggleBranch(QTreeWidgetItem *); @@ -59,6 +60,7 @@ protected: protected Q_SLOTS: void on_addBranchPB_pressed(); void on_removePB_pressed(); + void on_renamePB_pressed(); void on_activatePB_pressed(); void on_branchesTW_itemDoubleClicked(QTreeWidgetItem *, int); void on_colorPB_clicked(); diff --git a/src/frontends/qt4/GuiDocument.cpp b/src/frontends/qt4/GuiDocument.cpp index 979c71cd26..a77a32cb92 100644 --- a/src/frontends/qt4/GuiDocument.cpp +++ b/src/frontends/qt4/GuiDocument.cpp @@ -946,6 +946,8 @@ GuiDocument::GuiDocument(GuiView & lv) branchesModule = new GuiBranches; connect(branchesModule, SIGNAL(changed()), this, SLOT(change_adaptor())); + connect(branchesModule, SIGNAL(renameBranches(docstring const &, docstring const &)), + this, SLOT(branchesRename(docstring const &, docstring const &))); updateUnknownBranches(); // preamble @@ -2812,6 +2814,13 @@ void GuiDocument::updateUnknownBranches() } +void GuiDocument::branchesRename(docstring const & oldname, docstring const & newname) +{ + docstring const arg = '"' + oldname + '"' + " " + '"' + newname + '"'; + dispatch(FuncRequest(LFUN_BRANCHES_RENAME, arg)); +} + + Dialog * createGuiDocument(GuiView & lv) { return new GuiDocument(lv); } diff --git a/src/frontends/qt4/GuiDocument.h b/src/frontends/qt4/GuiDocument.h index ea87a21bc3..af26f40b1b 100644 --- a/src/frontends/qt4/GuiDocument.h +++ b/src/frontends/qt4/GuiDocument.h @@ -105,6 +105,7 @@ private Q_SLOTS: void changeBackgroundColor(); void deleteBackgroundColor(); void xetexChanged(bool); + void branchesRename(docstring const &, docstring const &); private: /// validate listings parameters and return an error message, if any QString validateListingsParameters(); diff --git a/src/frontends/qt4/ui/BranchesUi.ui b/src/frontends/qt4/ui/BranchesUi.ui index 4ceab17015..dd165ffa7e 100644 --- a/src/frontends/qt4/ui/BranchesUi.ui +++ b/src/frontends/qt4/ui/BranchesUi.ui @@ -19,17 +19,37 @@ 6 - - + + - Show undefined branches used in this document. + Change the name of the selected branch - &Undefined Branches + Re&name... + + + + + + + Remove the selected branch + + + &Remove + + + Define or change background color + + + Alter Co&lor... + + + + Qt::Vertical @@ -39,46 +59,39 @@ - 20 - 20 + 83 + 61 - - - - Qt::Horizontal + + + + + + + Add a new branch to the list - - - 251 - 20 - + + &Add - + - - + + - A&vailable Branches: + &New: - branchesTW + newBranchLE - - - - Remove the selected branch - - - &Remove - - + + - + Toggle the selected branch @@ -88,42 +101,39 @@ - - - - Define or change background color - + + - Alter Co&lor... + A&vailable Branches: + + + branchesTW - - - - - - - &New: + + + + Qt::Horizontal - - newBranchLE + + + 251 + 20 + - + - - + + - Add a new branch to the list + Show undefined branches used in this document. - &Add + &Undefined Branches - - - diff --git a/src/insets/InsetBranch.h b/src/insets/InsetBranch.h index a2f6c28a20..0c42e8fbe6 100644 --- a/src/insets/InsetBranch.h +++ b/src/insets/InsetBranch.h @@ -53,6 +53,8 @@ public: static void string2params(std::string const &, InsetBranchParams &); /// docstring branch() const { return params_.branch; } + /// + void rename(docstring const & newname) { params_.branch = newname; } private: ///