commit 275b7af75e5367c449d0f680d0965994c92d37d9
Author: Jean-Marc Lasgouttes <lasgout...@lyx.org>
Date:   Tue May 20 12:19:26 2014 +0200

    Code cleanup: get rid of RenderPreview::status()
    
    This method is a proxy for LyXRC::preview that forces to forward
    declare some wrapper around an enum...
    
    Instead, two simple static methods previewMath() and previewText() are
    introduced, that make the code much easier to follow.

diff --git a/src/LyXRC.h b/src/LyXRC.h
index 14f73d9..2814959 100644
--- a/src/LyXRC.h
+++ b/src/LyXRC.h
@@ -560,17 +560,6 @@ public:
 };
 
 
-/** \c LyXRC_PreviewStatus is a wrapper for LyXRC::PreviewStatus.
- *  It can be forward-declared and passed as a function argument without
- *  having to expose LyXRC.h.
- */
-class LyXRC_PreviewStatus {
-       LyXRC::PreviewStatus val_;
-public:
-       LyXRC_PreviewStatus(LyXRC::PreviewStatus val) : val_(val) {}
-       operator LyXRC::PreviewStatus() const { return val_; }
-};
-
 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
 
 ///
diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp
index 278168a..50c6e4b 100644
--- a/src/insets/InsetExternal.cpp
+++ b/src/insets/InsetExternal.cpp
@@ -542,8 +542,7 @@ static void 
add_preview_and_start_loading(RenderMonitoredPreview & renderer,
 {
        InsetExternalParams const & params = inset.params();
 
-       if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
-           isPreviewWanted(params)) {
+       if (RenderPreview::previewText() && isPreviewWanted(params)) {
                renderer.setAbsFile(params.filename);
                docstring const snippet = latexString(inset);
                renderer.addPreview(snippet, buffer);
@@ -577,7 +576,7 @@ void InsetExternal::setParams(InsetExternalParams const & p)
                || !lyxrc.display_graphics
                || params_.preview_mode == PREVIEW_OFF
                || (params_.preview_mode == PREVIEW_INSTANT
-                   && RenderPreview::status() == LyXRC::PREVIEW_OFF)) {
+                   && !RenderPreview::previewText())) {
                RenderButton * button_ptr = renderer_->asButton();
                if (!button_ptr) {
                        renderer_.reset(new RenderButton);
diff --git a/src/insets/InsetIPA.cpp b/src/insets/InsetIPA.cpp
index 6fe0a90..33ede55 100644
--- a/src/insets/InsetIPA.cpp
+++ b/src/insets/InsetIPA.cpp
@@ -19,7 +19,6 @@
 #include "FuncStatus.h"
 #include "LaTeXFeatures.h"
 #include "Lexer.h"
-#include "LyXRC.h"
 #include "MetricsInfo.h"
 #include "OutputParams.h"
 #include "RenderPreview.h"
@@ -122,8 +121,7 @@ void InsetIPA::preparePreview(DocIterator const & pos) const
 
 bool InsetIPA::previewState(BufferView * bv) const
 {
-       if (!editing(bv) && (RenderPreview::status() == LyXRC::PREVIEW_ON ||
-                            RenderPreview::status() == 
LyXRC::PREVIEW_NO_MATH)) {
+       if (!editing(bv) && RenderPreview::previewText()) {
                graphics::PreviewImage const * pimage =
                        preview_->getPreviewImage(bv->buffer());
                return pimage && pimage->image();
diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp
index 506f358..ff4a329 100644
--- a/src/insets/InsetInclude.cpp
+++ b/src/insets/InsetInclude.cpp
@@ -31,7 +31,6 @@
 #include "LayoutFile.h"
 #include "LayoutModuleList.h"
 #include "LyX.h"
-#include "LyXRC.h"
 #include "Lexer.h"
 #include "MetricsInfo.h"
 #include "output_plaintext.h"
@@ -989,7 +988,7 @@ void InsetInclude::metrics(MetricsInfo & mi, Dimension & 
dim) const
        LBUFERR(mi.base.bv);
 
        bool use_preview = false;
-       if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
+       if (RenderPreview::previewText()) {
                graphics::PreviewImage const * pimage =
                        preview_->getPreviewImage(mi.base.bv->buffer());
                use_preview = pimage && pimage->image();
@@ -1015,7 +1014,7 @@ void InsetInclude::draw(PainterInfo & pi, int x, int y) 
const
        LBUFERR(pi.base.bv);
 
        bool use_preview = false;
-       if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
+       if (RenderPreview::previewText()) {
                graphics::PreviewImage const * pimage =
                        preview_->getPreviewImage(pi.base.bv->buffer());
                use_preview = pimage && pimage->image();
@@ -1094,8 +1093,7 @@ void add_preview(RenderMonitoredPreview & renderer, 
InsetInclude const & inset,
                 Buffer const & buffer)
 {
        InsetCommandParams const & params = inset.params();
-       if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
-           preview_wanted(params, buffer)) {
+       if (RenderPreview::previewText() && preview_wanted(params, buffer)) {
                renderer.setAbsFile(includedFileName(buffer, params));
                docstring const snippet = latexString(inset);
                renderer.addPreview(snippet, buffer);
diff --git a/src/insets/InsetPreview.cpp b/src/insets/InsetPreview.cpp
index dd23cee..2d2f39e 100644
--- a/src/insets/InsetPreview.cpp
+++ b/src/insets/InsetPreview.cpp
@@ -16,7 +16,6 @@
 #include "BufferView.h"
 #include "Cursor.h"
 #include "Lexer.h"
-#include "LyXRC.h"
 #include "MetricsInfo.h"
 #include "OutputParams.h"
 #include "RenderPreview.h"
@@ -81,8 +80,7 @@ void InsetPreview::preparePreview(DocIterator const & pos) 
const
 
 bool InsetPreview::previewState(BufferView * bv) const
 {
-       if (!editing(bv) && (RenderPreview::status() == LyXRC::PREVIEW_ON ||
-                            RenderPreview::status() == 
LyXRC::PREVIEW_NO_MATH)) {
+       if (!editing(bv) && RenderPreview::previewText()) {
                graphics::PreviewImage const * pimage =
                        preview_->getPreviewImage(bv->buffer());
                return pimage && pimage->image();
diff --git a/src/insets/RenderPreview.cpp b/src/insets/RenderPreview.cpp
index d9946e8..9d7304f 100644
--- a/src/insets/RenderPreview.cpp
+++ b/src/insets/RenderPreview.cpp
@@ -39,9 +39,33 @@ using namespace lyx::support;
 namespace lyx {
 
 
-LyXRC_PreviewStatus RenderPreview::status()
+bool RenderPreview::previewText()
 {
-       return lyxrc.preview;
+       // Use a switch to trigger a warning if the enum is changed.
+       switch(lyxrc.preview) {
+       case LyXRC::PREVIEW_ON:
+       case LyXRC::PREVIEW_NO_MATH:
+               return true;
+       case LyXRC::PREVIEW_OFF:
+               break;
+       }
+
+       return false;
+}
+
+
+bool RenderPreview::previewMath()
+{
+       // Use a switch to trigger a warning if the enum is changed.
+       switch(lyxrc.preview) {
+       case LyXRC::PREVIEW_ON:
+               return true;
+       case LyXRC::PREVIEW_NO_MATH:
+       case LyXRC::PREVIEW_OFF:
+               break;
+       }
+
+       return false;
 }
 
 
@@ -171,7 +195,7 @@ void RenderPreview::draw(PainterInfo & pi, int x, int y) 
const
 
 void RenderPreview::startLoading(Buffer const & buffer, bool forexport) const
 {
-       if (!forexport && (status() == LyXRC::PREVIEW_OFF || snippet_.empty()))
+       if (!forexport && (lyxrc.preview == LyXRC::PREVIEW_OFF || 
snippet_.empty()))
                return;
 
        graphics::PreviewLoader * loader = buffer.loader();
@@ -184,7 +208,7 @@ void RenderPreview::addPreview(docstring const & 
latex_snippet,
                                Buffer const & buffer, 
                                bool ignore_lyxrc)
 {
-       if (status() == LyXRC::PREVIEW_OFF && !ignore_lyxrc)
+       if (lyxrc.preview == LyXRC::PREVIEW_OFF && !ignore_lyxrc)
                return;
 
        graphics::PreviewLoader * loader = buffer.loader();
@@ -197,7 +221,7 @@ void RenderPreview::addPreview(docstring const & 
latex_snippet,
                                graphics::PreviewLoader & ploader, 
                                bool ignore_lyxrc)
 {
-       if (status() == LyXRC::PREVIEW_OFF && !ignore_lyxrc)
+       if (lyxrc.preview == LyXRC::PREVIEW_OFF && !ignore_lyxrc)
                return;
 
        // FIXME UNICODE
diff --git a/src/insets/RenderPreview.h b/src/insets/RenderPreview.h
index a0f447e..cc7a4b6 100644
--- a/src/insets/RenderPreview.h
+++ b/src/insets/RenderPreview.h
@@ -44,8 +44,10 @@ class PreviewLoader;
 
 class RenderPreview : public RenderBase, public boost::signals::trackable {
 public:
-       /// a wrapper for lyxrc.preview
-       static LyXRC_PreviewStatus status();
+       /// Return true if preview is enabled in text (from LyXRC::preview)
+       static bool previewText();
+       /// Return true if preview is enabled in mathed (from LyXRC::preview)
+       static bool previewMath();
 
        RenderPreview(Inset const *);
        RenderPreview(RenderPreview const &, Inset const *);
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index eb11bc2..4347a61 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -413,8 +413,8 @@ ColorCode InsetMathHull::standardColor() const
 
 bool InsetMathHull::previewState(const BufferView *const bv) const
 {
-       if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON
-               && type_ != hullRegexp)
+       if (!editing(bv) && RenderPreview::previewMath()
+           && type_ != hullRegexp)
        {
                graphics::PreviewImage const * pimage =
                        preview_->getPreviewImage(bv->buffer());
@@ -615,7 +615,7 @@ void InsetMathHull::initUnicodeMath() const
 void InsetMathHull::addPreview(DocIterator const & inset_pos,
        graphics::PreviewLoader & /*ploader*/) const
 {
-       if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
+       if (RenderPreview::previewMath()) {
                preparePreview(inset_pos);
        }
 }
@@ -626,7 +626,7 @@ void InsetMathHull::preparePreview(DocIterator const & pos,
 {
        // there is no need to do all the macro stuff if we're not
        // actually going to generate the preview.
-       if (RenderPreview::status() != LyXRC::PREVIEW_ON && !forexport)
+       if (!RenderPreview::previewMath() && !forexport)
                return;
 
        Buffer const * buffer = pos.buffer();
@@ -690,7 +690,7 @@ void InsetMathHull::loadPreview(DocIterator const & pos) 
const
 
 bool InsetMathHull::notifyCursorLeaves(Cursor const & old, Cursor & cur)
 {
-       if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
+       if (RenderPreview::previewMath()) {
                reloadPreview(old);
                cur.screenUpdateFlags(Update::Force);
        }
diff --git a/src/mathed/MathMacroTemplate.cpp b/src/mathed/MathMacroTemplate.cpp
index 53cec03..ce3494c 100644
--- a/src/mathed/MathMacroTemplate.cpp
+++ b/src/mathed/MathMacroTemplate.cpp
@@ -34,7 +34,6 @@
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "Lexer.h"
-#include "LyXRC.h" 
 
 #include "frontends/Painter.h"
 
@@ -833,7 +832,7 @@ void fixMacroInstances(Cursor & cur, DocIterator const & 
inset_pos,
                MathMacro * macro = insetMath->asMacro();
                if (macro && macro->name() == name && macro->folded()) {
                        fix(macro);
-                       if (RenderPreview::status() == LyXRC::PREVIEW_ON)
+                       if (RenderPreview::previewMath())
                                preview_reload_needed = true;
                }
        }

Reply via email to