From 70d5f953c9a62802450abe2e5e1157357769a09b Mon Sep 17 00:00:00 2001 From: Richard Heck Date: Sat, 25 Oct 2008 13:32:54 +0000 Subject: [PATCH] Fix for another part of 5403, and related bugs, anyway. The InsetLayout tags should not be so case-dependent, and we ought to use an Enum instead. of relying upon string comparisons. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@27104 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Text3.cpp | 14 ++++++++------ src/frontends/qt4/Menus.cpp | 15 ++++++++------- src/insets/InsetLayout.cpp | 30 ++++++++++++++++++++++++++---- src/insets/InsetLayout.h | 15 +++++++++++++-- 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/Text3.cpp b/src/Text3.cpp index 2d7b15659c..11f5e4e901 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -1992,10 +1992,10 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd, string s = cmd.getArg(0); InsetLayout il = cur.buffer().params().documentClass().insetLayout(from_utf8(s)); - if (il.lyxtype() != "charstyle" && - il.lyxtype() != "custom" && - il.lyxtype() != "element" && - il.lyxtype ()!= "standard") + if (il.lyxtype() != InsetLayout::CHARSTYLE && + il.lyxtype() != InsetLayout::CUSTOM && + il.lyxtype() != InsetLayout::ELEMENT && + il.lyxtype ()!= InsetLayout::STANDARD) enable = false; break; } @@ -2145,9 +2145,11 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd, case LFUN_INSET_DISSOLVE: if (!cmd.argument().empty()) { - InsetLayout il = cur.inset().getLayout(cur.buffer().params()); + InsetLayout const & il = cur.inset().getLayout(cur.buffer().params()); + InsetLayout::InsetLyXType const type = + translateLyXType(to_utf8(cmd.argument())); enable = cur.inset().lyxCode() == FLEX_CODE - && il.lyxtype() == to_utf8(cmd.argument()); + && il.lyxtype() == type; } else { enable = !isMainText(cur.bv().buffer()) && cur.inset().nargs() == 1; diff --git a/src/frontends/qt4/Menus.cpp b/src/frontends/qt4/Menus.cpp index f40c54fa96..f8fcc96382 100644 --- a/src/frontends/qt4/Menus.cpp +++ b/src/frontends/qt4/Menus.cpp @@ -288,7 +288,7 @@ public: void expandFormats(MenuItem::Kind kind, Buffer const * buf); void expandFloatListInsert(Buffer const * buf); void expandFloatInsert(Buffer const * buf); - void expandFlexInsert(Buffer const * buf, std::string s); + void expandFlexInsert(Buffer const * buf, InsetLayout::InsetLyXType type); void expandToc2(Toc const & toc_list, size_t from, size_t to, int depth); void expandToc(Buffer const * buf); void expandPasteRecent(Buffer const * buf); @@ -834,7 +834,8 @@ void MenuDefinition::expandFloatInsert(Buffer const * buf) } -void MenuDefinition::expandFlexInsert(Buffer const * buf, string s) +void MenuDefinition::expandFlexInsert( + Buffer const * buf, InsetLayout::InsetLyXType type) { if (!buf) { add(MenuItem(MenuItem::Command, qt_("No Document Open!"), @@ -847,13 +848,13 @@ void MenuDefinition::expandFlexInsert(Buffer const * buf, string s) TextClass::InsetLayouts::const_iterator end = insetLayouts.end(); for (; cit != end; ++cit) { docstring const label = cit->first; - if (cit->second.lyxtype() == s) + if (cit->second.lyxtype() == type) addWithStatusCheck(MenuItem(MenuItem::Command, toqstr(translateIfPossible(label)), FuncRequest(LFUN_FLEX_INSERT, label))); } // FIXME This is a little clunky. - if (items_.empty() && s == "custom") + if (items_.empty() && type == InsetLayout::CUSTOM) add(MenuItem(MenuItem::Command, qt_("No custom insets defined!"), FuncRequest(LFUN_NOACTION))); @@ -1358,15 +1359,15 @@ void Menus::Impl::expand(MenuDefinition const & frommenu, break; case MenuItem::CharStyles: - tomenu.expandFlexInsert(buf, "charstyle"); + tomenu.expandFlexInsert(buf, InsetLayout::CHARSTYLE); break; case MenuItem::Custom: - tomenu.expandFlexInsert(buf, "custom"); + tomenu.expandFlexInsert(buf, InsetLayout::CUSTOM); break; case MenuItem::Elements: - tomenu.expandFlexInsert(buf, "element"); + tomenu.expandFlexInsert(buf, InsetLayout::ELEMENT); break; case MenuItem::FloatListInsert: diff --git a/src/insets/InsetLayout.cpp b/src/insets/InsetLayout.cpp index 823e61b085..a46cdb4d1e 100644 --- a/src/insets/InsetLayout.cpp +++ b/src/insets/InsetLayout.cpp @@ -31,8 +31,8 @@ using std::vector; namespace lyx { InsetLayout::InsetLayout() : - name_(from_ascii("undefined")), labelstring_(from_ascii("UNDEFINED")), - decoration_(InsetLayout::Default), + name_(from_ascii("undefined")), lyxtype_(STANDARD), + labelstring_(from_ascii("UNDEFINED")), decoration_(InsetLayout::Default), font_(sane_font), labelfont_(sane_font), bgcolor_(Color_error), multipar_(false), custompars_(false), forceplain_(true), passthru_(false), needprotect_(false), freespacing_(false), @@ -136,9 +136,14 @@ bool InsetLayout::read(Lexer & lex, TextClass & tclass) break; } switch (le) { - case IL_LYXTYPE: - lex >> lyxtype_; + case IL_LYXTYPE: { + string lt; + lex >> lt; + lyxtype_ = translateLyXType(lt); + if (lyxtype_ == NOLYXTYPE) + LYXERR0("Unknown LyXType `" << lt << "'."); break; + } case IL_LATEXTYPE: lex >> latextype_; break; @@ -258,4 +263,21 @@ bool InsetLayout::read(Lexer & lex, TextClass & tclass) return true; } + +InsetLayout::InsetLyXType translateLyXType(std::string const & str) +{ + + if (support::compare_ascii_no_case(str, "charstyle") == 0) + return InsetLayout::CHARSTYLE; + if (support::compare_ascii_no_case(str, "custom") == 0) + return InsetLayout::CUSTOM; + if (support::compare_ascii_no_case(str, "element") == 0) + return InsetLayout::ELEMENT; + if (support::compare_ascii_no_case(str, "end") == 0) + return InsetLayout::END; + if (support::compare_ascii_no_case(str, "standard") == 0) + return InsetLayout::STANDARD; + return InsetLayout::NOLYXTYPE; +} + } //namespace lyx diff --git a/src/insets/InsetLayout.h b/src/insets/InsetLayout.h index 8d7193021b..7cab643560 100644 --- a/src/insets/InsetLayout.h +++ b/src/insets/InsetLayout.h @@ -38,12 +38,20 @@ public: Conglomerate, Default }; + enum InsetLyXType { + NOLYXTYPE, + CHARSTYLE, + CUSTOM, + ELEMENT, + END, + STANDARD + }; /// bool read(Lexer & lexrc, TextClass & tclass); /// docstring name() const { return name_; }; /// - std::string lyxtype() const { return lyxtype_; }; + InsetLyXType lyxtype() const { return lyxtype_; }; /// docstring labelstring() const { return labelstring_; }; /// @@ -88,7 +96,7 @@ private: * Values are 'charstyle', 'custom' (things that by default look like a * footnote), 'element' (docbook), 'standard'. */ - std::string lyxtype_; + InsetLyXType lyxtype_; /// docstring labelstring_; /// @@ -127,6 +135,9 @@ private: bool forceltr_; }; +/// +InsetLayout::InsetLyXType translateLyXType(std::string const & str); + } // namespace lyx #endif -- 2.39.2