]> git.lyx.org Git - features.git/commitdiff
Split InsetLabel::updateCommand() into:
authorAbdelrazak Younes <younes@lyx.org>
Wed, 6 Jul 2011 17:40:51 +0000 (17:40 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Wed, 6 Jul 2011 17:40:51 +0000 (17:40 +0000)
 * uniqueLabel(docstring & label) to enforce label unicity
 * updateLabel() to update only the label.
 * InsetLabel::updateLabelAndRefs() to update label & refs
 * InsetLabel::updateReferences() to update the references

This fixes bug #7655

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@39250 a592a061-630c-0410-9148-cb99ea01b6c8

src/CutAndPaste.cpp
src/insets/InsetInclude.cpp
src/insets/InsetLabel.cpp
src/insets/InsetLabel.h
src/mathed/InsetMathHull.cpp

index 2bac66f8a6db2d2fe4d72f078fe13c20f6b10bbd..c1dc097592492fdb353b237261994a3d8bb13ff6 100644 (file)
@@ -227,7 +227,7 @@ pasteSelectionHelper(Cursor const & cur, ParagraphList const & parlist,
        InsetIterator const i_end = inset_iterator_end(in);
        for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
                // Even though this will also be done later, it has to be done here 
-               // since, e.g., InsetLabel::updateCommand() is going to try to access
+               // since some inset might going to try to access
                // the buffer() member.
                it->setBuffer(const_cast<Buffer &>(buffer));
                switch (it->lyxCode()) {
@@ -241,7 +241,7 @@ pasteSelectionHelper(Cursor const & cur, ParagraphList const & parlist,
                                        continue;
                                InsetLabel * lab = labels[i];
                                docstring const oldname = lab->getParam("name");
-                               lab->updateCommand(oldname, false);
+                               lab->updateLabel(oldname);
                                // We need to update the buffer reference cache.
                                cur.forceBufferUpdate();
                                docstring const newname = lab->getParam("name");
@@ -272,7 +272,7 @@ pasteSelectionHelper(Cursor const & cur, ParagraphList const & parlist,
                        // check for duplicates
                        InsetLabel & lab = static_cast<InsetLabel &>(*it);
                        docstring const oldname = lab.getParam("name");
-                       lab.updateCommand(oldname, false);
+                       lab.updateLabel(oldname);
                        // We need to update the buffer reference cache.
                        cur.forceBufferUpdate();
                        docstring const newname = lab.getParam("name");
index c74ec631aa1a5d0197f37f06f65ea1768926c395..971e11f2a0e461a347f094b16b6ba64ce5234106 100644 (file)
@@ -271,7 +271,7 @@ void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
                                        }                                       
 
                                        if (new_label != old_label) {
-                                               label_->updateCommand(new_label);
+                                               label_->updateLabelAndRefs(new_label, &cur);
                                                // the label might have been adapted (duplicate)
                                                if (new_label != label_->getParam("name")) {
                                                        new_params.addParam("label", "{" + 
@@ -1100,7 +1100,7 @@ void InsetInclude::updateCommand()
                return;
 
        docstring old_label = label_->getParam("name");
-       label_->updateCommand(old_label, false);
+       label_->updateLabel(old_label);
        // the label might have been adapted (duplicate)
        docstring new_label = label_->getParam("name");
        if (old_label == new_label)
@@ -1115,6 +1115,7 @@ void InsetInclude::updateCommand()
        setParams(p);   
 }
 
+
 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype)
 {
        Buffer const * const childbuffer = getChildBuffer();
index 76083ad91cc65b9d784ca573f8566eae8e08023c..2a380a74fae8c589db183783bfcedf66c3682d8a 100644 (file)
@@ -55,49 +55,74 @@ InsetLabel::InsetLabel(Buffer * buf, InsetCommandParams const & p)
 
 void InsetLabel::initView()
 {
-       updateCommand(getParam("name"));
+       // FIXME: This seems to be used only for inset creation so
+       // we probably just need to call updateLabel() here.
+       updateLabelAndRefs(getParam("name"));
 }
 
 
-void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
+void InsetLabel::uniqueLabel(docstring & label) const
 {
-       docstring const old_label = getParam("name");
-       docstring label = new_label;
+       docstring const new_label = label;
        int i = 1;
        while (buffer().insetLabel(label)) {
                label = new_label + '-' + convert<docstring>(i);
                ++i;
        }
-
        if (label != new_label) {
                // Warn the user that the label has been changed to something else.
                frontend::Alert::warning(_("Label names must be unique!"),
                        bformat(_("The label %1$s already exists,\n"
                        "it will be changed to %2$s."), new_label, label));
-       } else if (label == old_label)
-               // Label was not changed.
+       }
+}
+
+
+void InsetLabel::updateLabel(docstring const & new_label)
+{
+       docstring label = new_label;
+       uniqueLabel(label);
+       setParam("name", label);
+}
+
+
+void InsetLabel::updateLabelAndRefs(docstring const & new_label,
+               Cursor * cursor)
+{
+       docstring const old_label = getParam("name");
+       docstring label = new_label;
+       uniqueLabel(label);
+       if (label == old_label)
                return;
 
+       if (!cursor)
+               return;
+
+       cursor->recordUndo();
        buffer().undo().beginUndoGroup();
        buffer().markDirty();
        setParam("name", label);
+       updateReferences(old_label, label);
+       buffer().undo().endUndoGroup();
+}
+
 
-       if (updaterefs) {
-               Buffer::References & refs = buffer().references(old_label);
-               Buffer::References::iterator it = refs.begin();
-               Buffer::References::iterator end = refs.end();
-               for (; it != end; ++it) {
-                       buffer().undo().recordUndo(it->second);
-                       if (it->first->lyxCode() == MATH_REF_CODE) {
-                               InsetMathHull * mi = it->first->asInsetMath()->asHullInset();
-                               mi->asRefInset()->changeTarget(label);
-                       } else {
-                               InsetCommand * ref = it->first->asInsetCommand();
-                               ref->setParam("reference", label);
-                       }
+void InsetLabel::updateReferences(docstring const & old_label,
+               docstring const & new_label)
+{
+       Buffer::References & refs = buffer().references(old_label);
+       Buffer::References::iterator it = refs.begin();
+       Buffer::References::iterator end = refs.end();
+       for (; it != end; ++it) {
+               buffer().undo().recordUndo(it->second);
+               if (it->first->lyxCode() == MATH_REF_CODE) {
+                       InsetMathHull * mi = it->first->asInsetMath()->asHullInset();
+                       mi->asRefInset()->changeTarget(new_label);
+               } else {
+                       InsetCommand * ref = it->first->asInsetCommand();
+                       ref->setParam("reference", new_label);
                }
        }
-       buffer().undo().endUndoGroup();
 }
 
 
@@ -200,8 +225,8 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
                        break;
                }
                if (p["name"] != params()["name"]) {
-                       // undo is handled in updateCommand
-                       updateCommand(p["name"]);
+                       // undo is handled in updateLabelAndRefs
+                       updateLabelAndRefs(p["name"], &cur);
                }
                cur.forceBufferUpdate();
                break;
index 35e8f4ba953a3a857a4b7491e9ea3f3a5466fa24..35edecf567dd5ee593e36cc12ade1c8e11ac1218 100644 (file)
@@ -31,8 +31,11 @@ public:
        docstring const & counterValue() const { return counter_value_; }
        ///
        docstring const & prettyCounter() const { return pretty_counter_; }
-       ///
-       void updateCommand(docstring const & new_label, bool updaterefs = true);
+       /// Updates only the label string, doesn't handle undo nor references.
+       void updateLabel(docstring const & new_label);
+       /// Updates the label. Will handle undo/redo as well as update the
+       /// references to this label if \p cursor is passed.
+       void updateLabelAndRefs(docstring const & new_label, Cursor * cursor = 0);
 
        /// \name Public functions inherited from Inset class
        //@{
@@ -85,6 +88,11 @@ private:
        void doDispatch(Cursor & cur, FuncRequest & cmd);
        //@}
 
+       ///
+       void uniqueLabel(docstring & label) const;
+       ///
+       void updateReferences(docstring const & old_label,
+               docstring const & new_label);
        ///
        docstring screen_label_;
        ///
index 804d3d32b1abac602427e0ed7ec3539ae7e024a5..503fcd635261f4beb0c2576267a6ed854ebe4a26 100644 (file)
@@ -663,7 +663,7 @@ void InsetMathHull::label(row_type row, docstring const & label)
                        label_[row] = dummy_pointer;
                } else {
                        if (buffer_)
-                               label_[row]->updateCommand(label);
+                               label_[row]->updateLabelAndRefs(label);
                        else
                                label_[row]->setParam("name", label);
                }