From 132003aac5a30bc133f82a453a558945f13d83ff Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Sun, 2 Mar 2008 17:04:10 +0000 Subject: [PATCH] Avoid duplicate labels when the user change an InsetLabel. * InsetLabel: - update(): new method for changing the label. Uses the Buffer reference cache instead of a lookup. - LFUN_INSET_MODIFY: use update() instead of Buffer::changeRefIfUnique(). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23393 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/insets/InsetLabel.cpp | 39 +++++++++++++++++++++++++++++++++++---- src/insets/InsetLabel.h | 2 ++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index c23cee2b06..9a9cb31ddd 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -14,6 +14,7 @@ #include "InsetRef.h" +#include "buffer_funcs.h" #include "Buffer.h" #include "BufferView.h" #include "DispatchResult.h" @@ -26,6 +27,7 @@ #include "frontends/alert.h" +#include "support/convert.h" #include "support/lyxalgo.h" #include "support/gettext.h" #include "support/lstrings.h" @@ -41,6 +43,38 @@ InsetLabel::InsetLabel(InsetCommandParams const & p) {} +void InsetLabel::update(docstring const & new_label) +{ + docstring const old_label = getParam("name"); + if (old_label == new_label) + return; + + docstring label = new_label; + int i = 0; + while (buffer().insetLabel(label)) { + label = new_label + '-' + convert(i); + ++i; + } + setParam("name", label); + + Buffer::References const & refs = buffer().references(old_label); + Buffer::References::const_iterator it = refs.begin(); + Buffer::References::const_iterator end = refs.end(); + for (; it != end; ++it) + it->first->setParam("reference", label); + + 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 been changed to %2$s."), new_label, label)); + } + // We need an update of the Buffer reference cache. This is achieved by + // updateLabel(). + lyx::updateLabels(buffer()); +} + + ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */) { static ParamInfo param_info_; @@ -104,10 +138,7 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd) cur.noUpdate(); break; } - if (p["name"] != params()["name"]) - cur.bv().buffer().changeRefsIfUnique(params()["name"], - p["name"], REF_CODE); - setParams(p); + update(p["name"]); break; } diff --git a/src/insets/InsetLabel.h b/src/insets/InsetLabel.h index 13284c8db2..e9e2669c10 100644 --- a/src/insets/InsetLabel.h +++ b/src/insets/InsetLabel.h @@ -52,6 +52,8 @@ protected: private: /// Inset * clone() const { return new InsetLabel(*this); } + /// + void update(docstring const & new_label); }; -- 2.39.2