On Wed, Jun 10, 2015 at 07:45:46PM +0200, Enrico Forestieri wrote:
> commit 21e908b8c49f0b5a7df1eca9b36d2632e0838880
> Author: Enrico Forestieri <for...@lyx.org>
> Date:   Wed Jun 10 19:21:27 2015 +0200
> 
>     Delay regeneration of previews on zoom changes
>     
>     Until now the regeneration process was starting as soon as the zoom scale
>     factor was changed. This was causing some glitches, especially if the zoom
>     was changed by the mouse wheel, as on each change the process was started
>     again and again making zoom changes painful and causing races such that
>     one could end up with the text at some zoom factor and the previews at
>     another one. After this commit, the regeneration is started only after
>     the zoom factor has been stable for about 1 second. In this way, one can
>     use the mouse wheel for changing back and forth the zoom factor at own's
>     heart desire without any slow down due to the regeneration process running
>     in the background. For those using previews with numbered math equations,
>     a nice possibility for getting the equations correctly numbered in 
> sequence
>     (after removing or adding an equation) is using the shortcuts Alt+ and 
> Alt-
>     in rapid sequence (less than a second between the keystrokes). Previously,
>     this would have triggered twice the regeneration, but now only once.

Richard, the attached would be the corresponding patch for stable.
I have been testing it quite heavily and it seems to work w/o glitches.

-- 
Enrico
diff --git a/src/Makefile.am b/src/Makefile.am
index 120bff9..df17ab4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -316,7 +316,7 @@ endif
 
 #########################  Qt stuff  ##############################
 
-MOCHEADER = Compare.h
+MOCHEADER = Compare.h PreviewLoader.h
 
 if INSTALL_WINDOWS
 
@@ -330,6 +330,9 @@ MOCEDFILES = $(MOCHEADER:%.h=moc_%.cpp)
 BUILT_SOURCES += $(MOCEDFILES)
 CLEANFILES += $(MOCEDFILES)
 
+moc_PreviewLoader.cpp: graphics/PreviewLoader.h
+       $(MOC4) $(MOCFLAG) -o $@ $<
+
 moc_%.cpp: %.h
        $(MOC4) $(MOCFLAG) -o $@ $<
 
diff --git a/src/graphics/CMakeLists.txt b/src/graphics/CMakeLists.txt
index d0708bd..2b38d0c 100644
--- a/src/graphics/CMakeLists.txt
+++ b/src/graphics/CMakeLists.txt
@@ -13,6 +13,8 @@ lyx_add_msvc_pch(graphics)
 
 
 include_directories(${TOP_SRC_DIR}/src/graphics)
+lyx_automoc(${TOP_SRC_DIR}/src/graphics/PreviewLoader.cpp)
+include_directories(${CMAKE_CURRENT_BINARY_DIR} ${QT_INCLUDES})
 
 if(NOT LYX_MERGE_FILES)
        add_library(graphics ${library_type} ${graphics_sources} 
${graphics_headers})
@@ -21,6 +23,7 @@ else()
        add_library(graphics ${library_type} ${_allinone_files})
 endif()
 set_target_properties(graphics PROPERTIES FOLDER "applications/LyX")
+qt_use_modules(graphics Core)
 
 lyx_add_gcc_pch(graphics)
 
diff --git a/src/graphics/PreviewLoader.cpp b/src/graphics/PreviewLoader.cpp
index 7d2f81c..3705bde 100644
--- a/src/graphics/PreviewLoader.cpp
+++ b/src/graphics/PreviewLoader.cpp
@@ -44,6 +44,8 @@
 #include <fstream>
 #include <iomanip>
 
+#include <QTimer>
+
 using namespace std;
 using namespace lyx::support;
 
@@ -207,6 +209,8 @@ public:
        /// \p wait whether to wait for the process to complete or, instead,
        /// to do it in the background.
        void startLoading(bool wait = false);
+       ///
+       void refreshPreviews();
 
        /// Emit this signal when an image is ready for display.
        boost::signal<void(PreviewImage const &)> imageReady;
@@ -247,6 +251,8 @@ private:
        Buffer const & buffer_;
        ///
        mutable int font_scaling_factor_;
+       ///
+       QTimer * delay_refresh_;
 
        /// We don't own this
        static lyx::Converter const * pconverter_;
@@ -301,6 +307,12 @@ void PreviewLoader::startLoading(bool wait) const
 }
 
 
+void PreviewLoader::refreshPreviews()
+{
+       pimpl_->refreshPreviews();
+}
+
+
 boost::signals::connection PreviewLoader::connect(slot_type const & slot) const
 {
        return pimpl_->imageReady.connect(slot);
@@ -396,11 +408,18 @@ PreviewLoader::Impl::Impl(PreviewLoader & p, Buffer const 
& b)
                : int(0.01 * lyxrc.dpi * lyxrc.zoom * 
lyxrc.preview_scale_factor);
        if (!pconverter_)
                pconverter_ = setConverter("lyxpreview");
+
+       delay_refresh_ = new QTimer(&parent_);
+       delay_refresh_->setSingleShot(true);
+       QObject::connect(delay_refresh_, SIGNAL(timeout()),
+                        &parent_, SLOT(refreshPreviews()));
 }
 
 
 PreviewLoader::Impl::~Impl()
 {
+       delete delay_refresh_;
+
        InProgressProcesses::iterator ipit  = in_progress_.begin();
        InProgressProcesses::iterator ipend = in_progress_.end();
 
@@ -416,18 +435,32 @@ PreviewLoader::Impl::preview(string const & 
latex_snippet) const
                ? int(75.0 * buffer_.params().html_math_img_scale)
                : int(0.01 * lyxrc.dpi * lyxrc.zoom * 
lyxrc.preview_scale_factor);
        if (font_scaling_factor_ != fs) {
-               font_scaling_factor_ = fs;
-               Cache::const_iterator cit = cache_.begin();
-               Cache::const_iterator cend = cache_.end();
-               while (cit != cend)
-                       parent_.remove((cit++)->first);
-               buffer_.updatePreviews();
+               // Schedule refresh of all previews on zoom changes.
+               // The previews are regenerated only after the zoom factor
+               // has not been changed for about 1 second.
+               delay_refresh_->start(1000);
        }
+       // Don't try to access the cache until we are finished.
+       if (delay_refresh_->isActive())
+               return 0;
        Cache::const_iterator it = cache_.find(latex_snippet);
        return (it == cache_.end()) ? 0 : it->second.get();
 }
 
 
+void PreviewLoader::Impl::refreshPreviews()
+{
+       font_scaling_factor_ = buffer_.isExporting()
+               ? int(75.0 * buffer_.params().html_math_img_scale)
+               : int(0.01 * lyxrc.dpi * lyxrc.zoom * 
lyxrc.preview_scale_factor);
+       Cache::const_iterator cit = cache_.begin();
+       Cache::const_iterator cend = cache_.end();
+       while (cit != cend)
+               parent_.remove((cit++)->first);
+       buffer_.updatePreviews();
+}
+
+
 namespace {
 
 class FindSnippet {
@@ -782,3 +815,5 @@ void PreviewLoader::Impl::dumpData(odocstream & os,
 
 } // namespace graphics
 } // namespace lyx
+
+#include "moc_PreviewLoader.cpp"
diff --git a/src/graphics/PreviewLoader.h b/src/graphics/PreviewLoader.h
index 6910984..7f2c71e 100644
--- a/src/graphics/PreviewLoader.h
+++ b/src/graphics/PreviewLoader.h
@@ -20,6 +20,8 @@
 
 #include <boost/signal.hpp>
 
+#include <QObject>
+
 #include "ColorCode.h"
 
 namespace lyx {
@@ -30,7 +32,8 @@ namespace graphics {
 
 class PreviewImage;
 
-class PreviewLoader {
+class PreviewLoader : public QObject {
+       Q_OBJECT
 public:
        /** We need buffer because we require the preamble to the
         *  LaTeX file.
@@ -91,6 +94,10 @@ public:
        /// The foreground color used
        static ColorCode foregroundColor() { return Color_preview; }
 
+public Q_SLOTS:
+       ///
+       void refreshPreviews();
+
 private:
        /// noncopyable
        PreviewLoader(PreviewLoader const &);
diff --git a/src/mathed/CMakeLists.txt b/src/mathed/CMakeLists.txt
index 04c1a51..8d354f6 100644
--- a/src/mathed/CMakeLists.txt
+++ b/src/mathed/CMakeLists.txt
@@ -14,7 +14,7 @@ list(REMOVE_ITEM mathed_sources
 
 lyx_add_msvc_pch(mathed)
 
-include_directories(${TOP_SRC_DIR}/src/mathed)
+include_directories(${TOP_SRC_DIR}/src/mathed ${QT_INCLUDES})
 
 if(NOT LYX_MERGE_FILES)
        add_library(mathed ${library_type} ${mathed_sources} ${mathed_headers})
@@ -23,6 +23,7 @@ else()
        add_library(mathed ${library_type} ${_allinone_files})
 endif()
 set_target_properties(mathed PROPERTIES FOLDER "applications/LyX")
+qt_use_modules(mathed Core)
 
 lyx_add_gcc_pch(mathed)
 

Reply via email to