From e449e70e3854da3aeda8dca1de22cabaf6ae0557 Mon Sep 17 00:00:00 2001 From: Guillaume Munch Date: Sun, 20 Nov 2016 19:53:16 +0100 Subject: [PATCH] Simplify Changers interface In fact having an extra parameter "bool cond" is no longer useful because it can now always be emulated with a ternary operator: Changers dummy = cond ? do_change() : Changer(); --- src/FontInfo.cpp | 16 +++---- src/FontInfo.h | 8 ++-- src/MetricsInfo.cpp | 75 ++++++++++++++++-------------- src/MetricsInfo.h | 8 ++-- src/insets/InsetCollapsable.cpp | 5 +- src/mathed/InsetMathAMSArray.cpp | 6 +-- src/mathed/InsetMathArray.cpp | 6 +-- src/mathed/InsetMathDecoration.cpp | 6 ++- src/mathed/InsetMathDiagram.cpp | 6 ++- src/mathed/InsetMathEnsureMath.cpp | 6 ++- src/mathed/InsetMathFontOld.cpp | 6 ++- src/mathed/InsetMathFrac.cpp | 6 ++- src/mathed/InsetMathHull.cpp | 6 ++- src/mathed/InsetMathSubstack.cpp | 6 +-- src/mathed/InsetMathXYMatrix.cpp | 6 +-- src/support/RefChanger.h | 12 ++--- 16 files changed, 96 insertions(+), 88 deletions(-) diff --git a/src/FontInfo.cpp b/src/FontInfo.cpp index 67255c29d4..afcc2765df 100644 --- a/src/FontInfo.cpp +++ b/src/FontInfo.cpp @@ -271,29 +271,29 @@ FontInfo & FontInfo::realize(FontInfo const & tmplt) } -Changer FontInfo::changeColor(ColorCode const color, bool cond) +Changer FontInfo::changeColor(ColorCode const color) { - return make_change(color_, color, cond); + return make_change(color_, color); } -Changer FontInfo::changeShape(FontShape const shape, bool cond) +Changer FontInfo::changeShape(FontShape const shape) { - return make_change(shape_, shape, cond); + return make_change(shape_, shape); } -Changer FontInfo::changeStyle(MathStyle const new_style, bool cond) +Changer FontInfo::changeStyle(MathStyle const new_style) { - return make_change(style_, new_style, cond); + return make_change(style_, new_style); } -Changer FontInfo::change(FontInfo font, bool realiz, bool cond) +Changer FontInfo::change(FontInfo font, bool realiz) { if (realiz) font.realize(*this); - return make_change(*this, font, cond); + return make_change(*this, font); } diff --git a/src/FontInfo.h b/src/FontInfo.h index b9d8cedd0d..c093474c45 100644 --- a/src/FontInfo.h +++ b/src/FontInfo.h @@ -145,14 +145,14 @@ public: } /// Temporarily replace the color with \param color. - Changer changeColor(ColorCode const color, bool cond = true); + Changer changeColor(ColorCode const color); /// Temporarily replace the shape with \param shape. - Changer changeShape(FontShape const shape, bool cond = true); + Changer changeShape(FontShape const shape); /// Temporarily replace the style - Changer changeStyle(MathStyle style, bool cond = true); + Changer changeStyle(MathStyle style); /// Temporarily replace the FontInfo with \param font, and optionally /// \param realize the \param font against the current FontInfo. - Changer change(FontInfo font, bool realize = false, bool cond = true); + Changer change(FontInfo font, bool realize = false); private: friend bool operator==(FontInfo const & lhs, FontInfo const & rhs); diff --git a/src/MetricsInfo.cpp b/src/MetricsInfo.cpp index 96f2fc709c..3f2b13bc6d 100644 --- a/src/MetricsInfo.cpp +++ b/src/MetricsInfo.cpp @@ -58,25 +58,21 @@ MetricsBase::MetricsBase(BufferView * b, FontInfo f, int w) } -Changer MetricsBase::changeFontSet(string const & name, bool cond) +Changer MetricsBase::changeFontSet(string const & name) { RefChanger rc = make_save(*this); - if (!cond) - rc->keep(); - else { - ColorCode oldcolor = font.color(); - string const oldname = fontname; - fontname = name; - if (isMathFont(name) || isMathFont(oldname)) - font = sane_font; - augmentFont(font, name); - font.setSize(rc->old.font.size()); - font.setStyle(rc->old.font.style()); - if (name != "lyxtex" - && ((isTextFont(oldname) && oldcolor != Color_foreground) - || (isMathFont(oldname) && oldcolor != Color_math))) - font.setColor(oldcolor); - } + ColorCode oldcolor = font.color(); + string const oldname = fontname; + fontname = name; + if (isMathFont(name) || isMathFont(oldname)) + font = sane_font; + augmentFont(font, name); + font.setSize(rc->old.font.size()); + font.setStyle(rc->old.font.style()); + if (name != "lyxtex" + && ((isTextFont(oldname) && oldcolor != Color_foreground) + || (isMathFont(oldname) && oldcolor != Color_math))) + font.setColor(oldcolor); return move(rc); } @@ -152,35 +148,42 @@ Color PainterInfo::textColor(Color const & color) const } -Changer MetricsBase::changeScript(bool cond) +Changer MetricsBase::changeScript() { switch (font.style()) { case LM_ST_DISPLAY: case LM_ST_TEXT: - return font.changeStyle(LM_ST_SCRIPT, cond); + return font.changeStyle(LM_ST_SCRIPT); case LM_ST_SCRIPT: case LM_ST_SCRIPTSCRIPT: - return font.changeStyle(LM_ST_SCRIPTSCRIPT, cond); - } - //remove Warning - LASSERT(false, return Changer()); -} - - -Changer MetricsBase::changeFrac(bool cond) -{ - switch (font.style()) { - case LM_ST_DISPLAY: - return font.changeStyle(LM_ST_TEXT, cond); - case LM_ST_TEXT: - return font.changeStyle(LM_ST_SCRIPT, cond); - case LM_ST_SCRIPT: - case LM_ST_SCRIPTSCRIPT: - return font.changeStyle(LM_ST_SCRIPTSCRIPT, cond); + return font.changeStyle(LM_ST_SCRIPTSCRIPT); } //remove Warning return Changer(); } +Changer MetricsBase::changeFrac() +{ + switch (font.style()) { + case LM_ST_DISPLAY: + return font.changeStyle(LM_ST_TEXT); + case LM_ST_TEXT: + return font.changeStyle(LM_ST_SCRIPT); + case LM_ST_SCRIPT: + case LM_ST_SCRIPTSCRIPT: + return font.changeStyle(LM_ST_SCRIPTSCRIPT); + } + //remove Warning + return Changer(); +} + + +Changer MetricsBase::changeArray() +{ + return (font.style() == LM_ST_DISPLAY) ? font.changeStyle(LM_ST_TEXT) + : Changer(); +} + + } // namespace lyx diff --git a/src/MetricsInfo.h b/src/MetricsInfo.h index 93eeb8ae19..b350747b4b 100644 --- a/src/MetricsInfo.h +++ b/src/MetricsInfo.h @@ -53,11 +53,13 @@ public: int macro_nesting; /// Temporarily change a full font. - Changer changeFontSet(std::string const & font, bool cond = true); + Changer changeFontSet(std::string const & font); // Temporarily change to the style suitable for use in fractions - Changer changeFrac(bool cond = true); + Changer changeFrac(); + // Temporarily change to the style suitable for use in arrays + Changer changeArray(); // Temporarily change the style to (script)script style - Changer changeScript(bool cond = true); + Changer changeScript(); /// int solidLineThickness() const { return solid_line_thickness_; } /// diff --git a/src/insets/InsetCollapsable.cpp b/src/insets/InsetCollapsable.cpp index 250b25cbc0..38c744efd1 100644 --- a/src/insets/InsetCollapsable.cpp +++ b/src/insets/InsetCollapsable.cpp @@ -274,8 +274,9 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const } // Do not draw the cue for INSERTED -- it is already in the button and // that's enough. - Changer dummy = make_change(pi.change_, Change(), - pi.change_.type == Change::INSERTED); + Changer dummy = (pi.change_.type == Change::INSERTED) + ? make_change(pi.change_, Change()) + : Changer(); InsetText::draw(pi, textx, texty); break; } diff --git a/src/mathed/InsetMathAMSArray.cpp b/src/mathed/InsetMathAMSArray.cpp index 2ed4f2017d..24a1fef7de 100644 --- a/src/mathed/InsetMathAMSArray.cpp +++ b/src/mathed/InsetMathAMSArray.cpp @@ -85,8 +85,7 @@ char const * InsetMathAMSArray::name_right() const void InsetMathAMSArray::metrics(MetricsInfo & mi, Dimension & dim) const { - FontInfo & f = mi.base.font; - Changer dummy = f.changeStyle(LM_ST_TEXT, f.style() == LM_ST_DISPLAY); + Changer dummy = mi.base.changeArray(); InsetMathGrid::metrics(mi, dim); } @@ -98,8 +97,7 @@ void InsetMathAMSArray::draw(PainterInfo & pi, int x, int y) const // Drawing the deco after changeStyle does not work mathed_draw_deco(pi, x + 1, yy, 5, dim.height(), from_ascii(name_left())); mathed_draw_deco(pi, x + dim.width() - 8, yy, 5, dim.height(), from_ascii(name_right())); - FontInfo & f = pi.base.font; - Changer dummy = f.changeStyle(LM_ST_TEXT, f.style() == LM_ST_DISPLAY); + Changer dummy = pi.base.changeArray(); InsetMathGrid::draw(pi, x, y); } diff --git a/src/mathed/InsetMathArray.cpp b/src/mathed/InsetMathArray.cpp index cc007e97a5..ad476ae17f 100644 --- a/src/mathed/InsetMathArray.cpp +++ b/src/mathed/InsetMathArray.cpp @@ -74,8 +74,7 @@ Inset * InsetMathArray::clone() const void InsetMathArray::metrics(MetricsInfo & mi, Dimension & dim) const { - FontInfo & f = mi.base.font; - Changer dummy = f.changeStyle(LM_ST_TEXT, f.style() == LM_ST_DISPLAY); + Changer dummy = mi.base.changeArray(); InsetMathGrid::metrics(mi, dim); } @@ -83,8 +82,7 @@ void InsetMathArray::metrics(MetricsInfo & mi, Dimension & dim) const void InsetMathArray::draw(PainterInfo & pi, int x, int y) const { setPosCache(pi, x, y); - FontInfo & f = pi.base.font; - Changer dummy = f.changeStyle(LM_ST_TEXT, f.style() == LM_ST_DISPLAY); + Changer dummy = pi.base.changeArray(); InsetMathGrid::draw(pi, x, y); } diff --git a/src/mathed/InsetMathDecoration.cpp b/src/mathed/InsetMathDecoration.cpp index acc49743fa..e152efe6b1 100644 --- a/src/mathed/InsetMathDecoration.cpp +++ b/src/mathed/InsetMathDecoration.cpp @@ -107,7 +107,8 @@ void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const { bool really_change_font = currentMode() == TEXT_MODE && isMathFont(mi.base.fontname); - Changer dummy = mi.base.changeFontSet("textnormal", really_change_font); + Changer dummy = really_change_font ? mi.base.changeFontSet("textnormal") + : Changer(); cell(0).metrics(mi, dim); @@ -130,7 +131,8 @@ void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const { bool really_change_font = currentMode() == TEXT_MODE && isMathFont(pi.base.fontname); - Changer dummy = pi.base.changeFontSet("textnormal", really_change_font); + Changer dummy = really_change_font ? pi.base.changeFontSet("textnormal") + : Changer(); cell(0).draw(pi, x + 1, y); Dimension const & dim0 = cell(0).dimension(*pi.base.bv); diff --git a/src/mathed/InsetMathDiagram.cpp b/src/mathed/InsetMathDiagram.cpp index 18d7c60851..9031b40d5e 100644 --- a/src/mathed/InsetMathDiagram.cpp +++ b/src/mathed/InsetMathDiagram.cpp @@ -49,7 +49,8 @@ int InsetMathDiagram::rowsep() const void InsetMathDiagram::metrics(MetricsInfo & mi, Dimension & dim) const { FontInfo & f = mi.base.font; - Changer dummy = f.changeStyle(LM_ST_TEXT, f.style() == LM_ST_DISPLAY); + Changer dummy = (f.style() == LM_ST_DISPLAY) ? f.changeStyle(LM_ST_TEXT) + : Changer(); InsetMathGrid::metrics(mi, dim); } @@ -58,7 +59,8 @@ void InsetMathDiagram::draw(PainterInfo & pi, int x, int y) const { setPosCache(pi, x, y); FontInfo & f = pi.base.font; - Changer dummy = f.changeStyle(LM_ST_TEXT, f.style() == LM_ST_DISPLAY); + Changer dummy = (f.style() == LM_ST_DISPLAY) ? f.changeStyle(LM_ST_TEXT) + : Changer(); InsetMathGrid::draw(pi, x, y); } diff --git a/src/mathed/InsetMathEnsureMath.cpp b/src/mathed/InsetMathEnsureMath.cpp index 382c6fedd8..d73a1cb359 100644 --- a/src/mathed/InsetMathEnsureMath.cpp +++ b/src/mathed/InsetMathEnsureMath.cpp @@ -39,7 +39,8 @@ Inset * InsetMathEnsureMath::clone() const void InsetMathEnsureMath::metrics(MetricsInfo & mi, Dimension & dim) const { bool really_change_font = isTextFont(mi.base.fontname); - Changer dummy = mi.base.changeFontSet("mathnormal", really_change_font); + Changer dummy = really_change_font ? mi.base.changeFontSet("mathnormal") + : Changer(); cell(0).metrics(mi, dim); metricsMarkers(mi, dim); } @@ -48,7 +49,8 @@ void InsetMathEnsureMath::metrics(MetricsInfo & mi, Dimension & dim) const void InsetMathEnsureMath::draw(PainterInfo & pi, int x, int y) const { bool really_change_font = isTextFont(pi.base.fontname); - Changer dummy = pi.base.changeFontSet("mathnormal", really_change_font); + Changer dummy = really_change_font ? pi.base.changeFontSet("mathnormal") + : Changer(); cell(0).draw(pi, x, y); drawMarkers(pi, x, y); } diff --git a/src/mathed/InsetMathFontOld.cpp b/src/mathed/InsetMathFontOld.cpp index aa574bd1c6..11ba283fdf 100644 --- a/src/mathed/InsetMathFontOld.cpp +++ b/src/mathed/InsetMathFontOld.cpp @@ -59,7 +59,8 @@ void InsetMathFontOld::metrics(MetricsInfo & mi, Dimension & dim) const // When \cal is used in text mode, the font is not changed bool really_change_font = fontname != "textcal"; - Changer dummy = mi.base.changeFontSet(fontname, really_change_font); + Changer dummy = really_change_font ? mi.base.changeFontSet(fontname) + : Changer(); cell(0).metrics(mi, dim); metricsMarkers(mi, dim); } @@ -76,7 +77,8 @@ void InsetMathFontOld::draw(PainterInfo & pi, int x, int y) const // When \cal is used in text mode, the font is not changed bool really_change_font = fontname != "textcal"; - Changer dummy = pi.base.changeFontSet(fontname, really_change_font); + Changer dummy = really_change_font ? pi.base.changeFontSet(fontname) + : Changer(); cell(0).draw(pi, x + 1, y); drawMarkers(pi, x, y); } diff --git a/src/mathed/InsetMathFrac.cpp b/src/mathed/InsetMathFrac.cpp index 42cc3a2f58..140205fa67 100644 --- a/src/mathed/InsetMathFrac.cpp +++ b/src/mathed/InsetMathFrac.cpp @@ -182,7 +182,8 @@ void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const cell(2).metrics(mi, dim2); dim.wid += dim2.wid + 4; } - Changer dummy = mi.base.font.changeShape(UP_SHAPE, kind_ == UNITFRAC); + Changer dummy = (kind_ == UNITFRAC) ? mi.base.font.changeShape(UP_SHAPE) + : Changer(); Changer dummy2 = mi.base.changeScript(); cell(0).metrics(mi, dim0); cell(1).metrics(mi, dim1); @@ -253,7 +254,8 @@ void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const cell(2).draw(pi, x + 1, y); xx += cell(2).dimension(*pi.base.bv).wid + 4; } - Changer dummy = pi.base.font.changeShape(UP_SHAPE, kind_ == UNITFRAC); + Changer dummy = (kind_ == UNITFRAC) ? pi.base.font.changeShape(UP_SHAPE) + : Changer(); // nice fraction // FIXME: // * the solidus should be \kern-2mu/\kern-1mu. diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index 1cba6ecb19..52442b225b 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -600,7 +600,8 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const if (previewState(bv)) { // Do not draw change tracking cue if taken care of by RowPainter // already. - Changer dummy = make_change(pi.change_, Change(), !canPaintChange(*bv)); + Changer dummy = !canPaintChange(*bv) ? make_change(pi.change_, Change()) + : Changer(); if (previewTooSmall(dim)) { // we have an extra frame preview_->draw(pi, x + ERROR_FRAME_WIDTH, y); @@ -615,7 +616,8 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const ColorCode color = pi.selected && lyxrc.use_system_colors ? Color_selectiontext : standardColor(); bool const really_change_color = pi.base.font.color() == Color_none; - Changer dummy0 = pi.base.font.changeColor(color, really_change_color); + Changer dummy0 = really_change_color ? pi.base.font.changeColor(color) + : Changer(); Changer dummy1 = pi.base.changeFontSet(standardFont()); Changer dummy2 = pi.base.font.changeStyle(display() ? LM_ST_DISPLAY : LM_ST_TEXT); diff --git a/src/mathed/InsetMathSubstack.cpp b/src/mathed/InsetMathSubstack.cpp index 461c88e58d..4bcc7c539c 100644 --- a/src/mathed/InsetMathSubstack.cpp +++ b/src/mathed/InsetMathSubstack.cpp @@ -45,16 +45,14 @@ Inset * InsetMathSubstack::clone() const void InsetMathSubstack::metrics(MetricsInfo & mi, Dimension & dim) const { - FontInfo & f = mi.base.font; - Changer dummy = f.changeStyle(LM_ST_TEXT, f.style() == LM_ST_DISPLAY); + Changer dummy = mi.base.changeArray(); InsetMathGrid::metrics(mi, dim); } void InsetMathSubstack::draw(PainterInfo & pi, int x, int y) const { - FontInfo & f = pi.base.font; - Changer dummy = f.changeStyle(LM_ST_TEXT, f.style() == LM_ST_DISPLAY); + Changer dummy = pi.base.changeArray(); InsetMathGrid::draw(pi, x + 1, y); } diff --git a/src/mathed/InsetMathXYMatrix.cpp b/src/mathed/InsetMathXYMatrix.cpp index 743c07ebd3..5e3bdb5057 100644 --- a/src/mathed/InsetMathXYMatrix.cpp +++ b/src/mathed/InsetMathXYMatrix.cpp @@ -49,8 +49,7 @@ int InsetMathXYMatrix::rowsep() const void InsetMathXYMatrix::metrics(MetricsInfo & mi, Dimension & dim) const { - FontInfo & f = mi.base.font; - Changer dummy = f.changeStyle(LM_ST_TEXT, f.style() == LM_ST_DISPLAY); + Changer dummy = mi.base.changeArray(); InsetMathGrid::metrics(mi, dim); } @@ -58,8 +57,7 @@ void InsetMathXYMatrix::metrics(MetricsInfo & mi, Dimension & dim) const void InsetMathXYMatrix::draw(PainterInfo & pi, int x, int y) const { setPosCache(pi, x, y); - FontInfo & f = pi.base.font; - Changer dummy = f.changeStyle(LM_ST_TEXT, f.style() == LM_ST_DISPLAY); + Changer dummy = pi.base.changeArray(); InsetMathGrid::draw(pi, x, y); } diff --git a/src/support/RefChanger.h b/src/support/RefChanger.h index baf3d840eb..a61abbd72b 100644 --- a/src/support/RefChanger.h +++ b/src/support/RefChanger.h @@ -66,16 +66,14 @@ template RefChanger make_save(X & ref) return make_unique>(ref); } -/// Temporarily assign value \param val to \param ref. If \param cond is false, -/// then the assignation does not happen and the RefChanger starts disabled. +/// Temporarily assign value val to reference ref. +/// To apply the change conditionnally, one can write: +/// Changer dummy = (cond) ? make_change(a, b) : Changer(); template -RefChanger make_change(X & ref, X const val, bool cond = true) +RefChanger make_change(X & ref, X const val) { auto rc = make_save(ref); - if (!cond) - rc->keep(); - else - ref = val; + ref = val; return rc; }