From: Abdelrazak Younes Date: Wed, 6 Jul 2011 17:40:51 +0000 (+0000) Subject: Split InsetLabel::updateCommand() into: X-Git-Tag: 2.1.0beta1~2962 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=f9e9956169cea7789c8cd08003458e4e395ce9c1;p=features.git Split InsetLabel::updateCommand() into: * 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 --- diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp index 2bac66f8a6..c1dc097592 100644 --- a/src/CutAndPaste.cpp +++ b/src/CutAndPaste.cpp @@ -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)); 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(*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"); diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index c74ec631aa..971e11f2a0 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -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(); diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index 76083ad91c..2a380a74fa 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -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(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; diff --git a/src/insets/InsetLabel.h b/src/insets/InsetLabel.h index 35e8f4ba95..35edecf567 100644 --- a/src/insets/InsetLabel.h +++ b/src/insets/InsetLabel.h @@ -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_; /// diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index 804d3d32b1..503fcd6352 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -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); }