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();
This commit is contained in:
Guillaume Munch 2016-11-20 19:53:16 +01:00
parent ffb865d6e8
commit e449e70e38
16 changed files with 96 additions and 88 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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<MetricsBase> 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

View File

@ -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_; }
///

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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.

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -66,16 +66,14 @@ template <typename X> RefChanger<X> make_save(X & ref)
return make_unique<RevertibleRef<X>>(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 <typename X>
RefChanger<X> make_change(X & ref, X const val, bool cond = true)
RefChanger<X> make_change(X & ref, X const val)
{
auto rc = make_save(ref);
if (!cond)
rc->keep();
else
ref = val;
ref = val;
return rc;
}