]> git.lyx.org Git - features.git/commitdiff
* Add ability to rename branches (bug 4128).
authorJürgen Spitzmüller <spitz@lyx.org>
Fri, 10 Jul 2009 06:49:51 +0000 (06:49 +0000)
committerJürgen Spitzmüller <spitz@lyx.org>
Fri, 10 Jul 2009 06:49:51 +0000 (06:49 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@30442 a592a061-630c-0410-9148-cb99ea01b6c8

13 files changed:
src/BranchList.cpp
src/BranchList.h
src/Buffer.cpp
src/Buffer.h
src/FuncCode.h
src/LaTeXFeatures.cpp
src/LyXAction.cpp
src/frontends/qt4/GuiBranches.cpp
src/frontends/qt4/GuiBranches.h
src/frontends/qt4/GuiDocument.cpp
src/frontends/qt4/GuiDocument.h
src/frontends/qt4/ui/BranchesUi.ui
src/insets/InsetBranch.h

index a593c5478fc91e9b421537f6cc849b32b2590238..02f8b8688d4d377976588f7d58ec7fa7c0bdfca6 100644 (file)
@@ -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
index ce21d48e2866bdc1751f019ba26792ddad0797cb..08a719a1c7cbb0c92076839cf330680eebc25478 100644 (file)
@@ -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:
        ///
index f0c4a53859b7688dd5b793203c5a48b283f3cba6..53ade6faf3683655839783effc631bc4f9a0f8f3 100644 (file)
@@ -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<docstring> & 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<InsetBranch &>(*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<InsetInclude const &>(*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 "
index 71e8ac32c13ce92f42abfc2f3b86b2b074f610b4..012f73bf67e97b9c174ce373d2cd27728a143a92 100644 (file)
@@ -509,6 +509,8 @@ public:
 
        /// return a list of all used branches (also in children)
        void getUsedBranches(std::list<docstring> &, 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
index e618bbe1f6c2198e674b27bee4a25b8e11fee340..3d879d04245bdaeb8d6e6c27263a7b403eda84fb 100644 (file)
@@ -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
 };
index e39dc92bb7d15cfd1dc44a1d6fb9ee26f0754f4b..918fc0b3c695203b97f0a43cd345f2801d2ca26a 100644 (file)
@@ -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");
 
index dace7fa57e2ec5453a84326eb0f0f1574a73167b..4282139e42e3e5a8b0d04cd09a7867b153e8a104 100644 (file)
@@ -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 <OLDNAME> <NEWNAME>
+ * \li Params: <OLDNAME>: Current name of the branch to be renamed
+ *             <NEWNAME>: 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.
index 806f344f138867c19645fc95264c5f16791adc8a..45e60de9624c0a101de3a247dc9de2b1c4d8a02c 100644 (file)
 
 #include "ui_BranchesUnknownUi.h"
 
+#include "frontends/alert.h"
+
 #include "Buffer.h"
 #include "BufferParams.h"
 
+#include "support/gettext.h"
 #include "support/lstrings.h"
 
 #include <QListWidget>
@@ -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());
index 3edf0970ea4e0f0ef486e762e4614620190fbe08..514878ce20a81a999c9a9c56b2075856e78b8bb8 100644 (file)
@@ -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();
index 979c71cd269cf18b603bb111c1fe4113b3869b97..a77a32cb92b6a94b61ebbeecf466f2b86a224926 100644 (file)
@@ -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); }
 
 
index ea87a21bc34eb8fe8784187cb2b480899a334174..af26f40b1b7d7240b8391b946fb5c37f936d885c 100644 (file)
@@ -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();
index 4ceab17015d3146fc302b4a7c741b4b26210aae1..dd165ffa7ef1da6bcc1d2b034f648c3e84538e9d 100644 (file)
    <property name="spacing" >
     <number>6</number>
    </property>
-   <item row="7" column="2" colspan="2" >
-    <widget class="QPushButton" name="unknownPB" >
+   <item row="3" column="3" >
+    <widget class="QPushButton" name="renamePB" >
      <property name="toolTip" >
-      <string>Show undefined branches used in this document.</string>
+      <string>Change the name of the selected branch</string>
      </property>
      <property name="text" >
-      <string>&amp;Undefined Branches</string>
+      <string>Re&amp;name...</string>
+     </property>
+    </widget>
+   </item>
+   <item row="2" column="3" >
+    <widget class="QPushButton" name="removePB" >
+     <property name="toolTip" >
+      <string>Remove the selected branch</string>
+     </property>
+     <property name="text" >
+      <string>&amp;Remove</string>
      </property>
     </widget>
    </item>
    <item row="4" column="3" >
+    <widget class="QPushButton" name="colorPB" >
+     <property name="toolTip" >
+      <string>Define or change background color</string>
+     </property>
+     <property name="text" >
+      <string>Alter Co&amp;lor...</string>
+     </property>
+    </widget>
+   </item>
+   <item row="5" column="3" >
     <spacer>
      <property name="orientation" >
       <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeHint" >
       <size>
-       <width>20</width>
-       <height>20</height>
+       <width>83</width>
+       <height>61</height>
       </size>
      </property>
     </spacer>
    </item>
-   <item row="7" column="0" colspan="2" >
-    <spacer>
-     <property name="orientation" >
-      <enum>Qt::Horizontal</enum>
+   <item row="0" column="1" colspan="2" >
+    <widget class="QLineEdit" name="newBranchLE" />
+   </item>
+   <item row="0" column="3" >
+    <widget class="QPushButton" name="addBranchPB" >
+     <property name="toolTip" >
+      <string>Add a new branch to the list</string>
      </property>
-     <property name="sizeHint" >
-      <size>
-       <width>251</width>
-       <height>20</height>
-      </size>
+     <property name="text" >
+      <string>&amp;Add</string>
      </property>
-    </spacer>
+    </widget>
    </item>
-   <item row="1" column="0" colspan="3" >
-    <widget class="QLabel" name="availableLB" >
+   <item row="0" column="0" >
+    <widget class="QLabel" name="newBranchLA" >
      <property name="text" >
-      <string>A&amp;vailable Branches:</string>
+      <string>&amp;New:</string>
      </property>
      <property name="buddy" >
-      <cstring>branchesTW</cstring>
+      <cstring>newBranchLE</cstring>
      </property>
     </widget>
    </item>
-   <item row="2" column="3" >
-    <widget class="QPushButton" name="removePB" >
-     <property name="toolTip" >
-      <string>Remove the selected branch</string>
-     </property>
-     <property name="text" >
-      <string>&amp;Remove</string>
-     </property>
-    </widget>
+   <item rowspan="5" row="2" column="0" colspan="3" >
+    <widget class="QTreeWidget" name="branchesTW" />
    </item>
-   <item row="5" column="3" >
+   <item row="6" column="3" >
     <widget class="QPushButton" name="activatePB" >
      <property name="toolTip" >
       <string>Toggle the selected branch</string>
      </property>
     </widget>
    </item>
-   <item row="3" column="3" >
-    <widget class="QPushButton" name="colorPB" >
-     <property name="toolTip" >
-      <string>Define or change background color</string>
-     </property>
+   <item row="1" column="0" colspan="3" >
+    <widget class="QLabel" name="availableLB" >
      <property name="text" >
-      <string>Alter Co&amp;lor...</string>
+      <string>A&amp;vailable Branches:</string>
+     </property>
+     <property name="buddy" >
+      <cstring>branchesTW</cstring>
      </property>
     </widget>
    </item>
-   <item rowspan="5" row="2" column="0" colspan="3" >
-    <widget class="QTreeWidget" name="branchesTW" />
-   </item>
-   <item row="0" column="0" >
-    <widget class="QLabel" name="newBranchLA" >
-     <property name="text" >
-      <string>&amp;New:</string>
+   <item row="7" column="0" colspan="2" >
+    <spacer>
+     <property name="orientation" >
+      <enum>Qt::Horizontal</enum>
      </property>
-     <property name="buddy" >
-      <cstring>newBranchLE</cstring>
+     <property name="sizeHint" >
+      <size>
+       <width>251</width>
+       <height>20</height>
+      </size>
      </property>
-    </widget>
+    </spacer>
    </item>
-   <item row="0" column="3" >
-    <widget class="QPushButton" name="addBranchPB" >
+   <item row="7" column="2" colspan="2" >
+    <widget class="QPushButton" name="unknownPB" >
      <property name="toolTip" >
-      <string>Add a new branch to the list</string>
+      <string>Show undefined branches used in this document.</string>
      </property>
      <property name="text" >
-      <string>&amp;Add</string>
+      <string>&amp;Undefined Branches</string>
      </property>
     </widget>
    </item>
-   <item row="0" column="1" colspan="2" >
-    <widget class="QLineEdit" name="newBranchLE" />
-   </item>
   </layout>
  </widget>
  <includes>
index a2f6c28a20b47fb52094aa068d912aada2fddc75..0c42e8fbe6934c99b1a7708fa4149301bcbb0cba 100644 (file)
@@ -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:
        ///