diff --git a/src/frontends/qt4/GuiGraphics.cpp b/src/frontends/qt4/GuiGraphics.cpp index f4a3928feb..0ca3ebb55c 100644 --- a/src/frontends/qt4/GuiGraphics.cpp +++ b/src/frontends/qt4/GuiGraphics.cpp @@ -296,15 +296,53 @@ void GuiGraphics::change_adaptor() void GuiGraphics::change_group(int index) { - QString const group = groupCO->itemData( + QString const new_group = groupCO->itemData( groupCO->currentIndex()).toString(); + + // check if the old group consisted only of this member + if (current_group_ != fromqstr(new_group) + && graphics::countGroupMembers(buffer(), current_group_) == 1) { + if (!new_group.isEmpty()) { + if (Alert::prompt(_("Dissolve previous group?"), + bformat(_("If you assign this graphic to group '%2$s',\n" + "the previously assigned group '%1$s' will be dissolved,\n" + "because this graphic was its only member.\n" + "How do you want to proceed?"), + from_utf8(current_group_), qstring_to_ucs4(new_group)), + 0, 0, + bformat(_("Stick with group '%1$s'"), + from_utf8(current_group_)), + bformat(_("Assign to group '%1$s' anyway"), + qstring_to_ucs4(new_group))) == 0) { + groupCO->setCurrentIndex( + groupCO->findData(toqstr(current_group_), Qt::MatchExactly)); + return; + } + } else { + if (Alert::prompt(_("Dissolve previous group?"), + bformat(_("If you sign off this graphic from group '%1$s',\n" + "the group will be dissolved,\n" + "because this graphic was its only member.\n" + "How do you want to proceed?"), + from_utf8(current_group_)), + 0, 0, + bformat(_("Stick with group '%1$s'"), + from_utf8(current_group_)), + bformat(_("Sign off from group '%1$s'"), + from_utf8(current_group_))) == 0) { + groupCO->setCurrentIndex( + groupCO->findData(toqstr(current_group_), Qt::MatchExactly)); + return; + } + } + } - if (group.isEmpty()) { + if (new_group.isEmpty()) { changed(); return; } - string grp = graphics::getGroupParams(buffer(), fromqstr(group)); + string grp = graphics::getGroupParams(buffer(), fromqstr(new_group)); if (grp.empty()) { // group does not exist yet changed(); @@ -745,6 +783,7 @@ void GuiGraphics::applyView() igp.groupId = fromqstr(groupCO->itemData( groupCO->currentIndex()).toString()); + current_group_ = igp.groupId; } @@ -780,6 +819,7 @@ bool GuiGraphics::initialiseParams(string const & data) { InsetGraphics::string2params(data, buffer(), params_); paramsToDialog(params_); + current_group_ = params_.groupId; return true; } diff --git a/src/frontends/qt4/GuiGraphics.h b/src/frontends/qt4/GuiGraphics.h index 3f91126d4e..2fbd779898 100644 --- a/src/frontends/qt4/GuiGraphics.h +++ b/src/frontends/qt4/GuiGraphics.h @@ -81,6 +81,8 @@ private: std::vector origin_ltx; /// InsetGraphicsParams params_; + /// the current graphics group + std::string current_group_; }; } // namespace frontend diff --git a/src/frontends/qt4/ui/GraphicsUi.ui b/src/frontends/qt4/ui/GraphicsUi.ui index 8092f39d4b..c320f2ae71 100644 --- a/src/frontends/qt4/ui/GraphicsUi.ui +++ b/src/frontends/qt4/ui/GraphicsUi.ui @@ -634,7 +634,7 @@ - Assigned to grou&p: + A&ssigned to group: groupCO @@ -644,10 +644,10 @@ - Click to define a new graphics group + Click to define a new graphics group. - De&fine new group... + O&pen new group... diff --git a/src/insets/InsetGraphics.cpp b/src/insets/InsetGraphics.cpp index 1116a5f96a..4c8ecad020 100644 --- a/src/insets/InsetGraphics.cpp +++ b/src/insets/InsetGraphics.cpp @@ -970,6 +970,24 @@ void getGraphicsGroups(Buffer const & b, set & ids) } +int countGroupMembers(Buffer const & b, string const & groupId) +{ + int n = 0; + if (groupId.empty()) + return n; + Inset & inset = b.inset(); + InsetIterator it = inset_iterator_begin(inset); + InsetIterator const end = inset_iterator_end(inset); + for (; it != end; ++it) + if (it->lyxCode() == GRAPHICS_CODE) { + InsetGraphics & ins = static_cast(*it); + if (ins.getParams().groupId == groupId) + ++n; + } + return n; +} + + string getGroupParams(Buffer const & b, string const & groupId) { if (groupId.empty()) diff --git a/src/insets/InsetGraphics.h b/src/insets/InsetGraphics.h index 73a5edcf99..e18d8c63cb 100644 --- a/src/insets/InsetGraphics.h +++ b/src/insets/InsetGraphics.h @@ -124,6 +124,9 @@ namespace graphics { /// Saves the list of currently used groups in the document. void getGraphicsGroups(Buffer const &, std::set &); + /// how many members has the current group? + int countGroupMembers(Buffer const &, std::string const &); + /// Returns parameters of a given graphics group (except filename). std::string getGroupParams(Buffer const &, std::string const &);