From 565b4e9fd215282b18f2a3a983c959c84548a630 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Fri, 12 Jun 2015 17:04:00 +0200 Subject: [PATCH] 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. This is an amendment to 0e2ea9d4, so no status line is needed. --- src/Makefile.am | 5 +++- src/graphics/CMakeLists.txt | 3 +++ src/graphics/PreviewLoader.cpp | 47 +++++++++++++++++++++++++++++----- src/graphics/PreviewLoader.h | 9 ++++++- src/mathed/CMakeLists.txt | 3 ++- 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 120bff9881..df17ab4afc 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 d0708bd309..2b38d0c98e 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 7d2f81cce1..1a8b7fadd6 100644 --- a/src/graphics/PreviewLoader.cpp +++ b/src/graphics/PreviewLoader.cpp @@ -44,6 +44,8 @@ #include #include +#include + 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 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 done. + 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 6910984c9e..7f2c71e33f 100644 --- a/src/graphics/PreviewLoader.h +++ b/src/graphics/PreviewLoader.h @@ -20,6 +20,8 @@ #include +#include + #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 04c1a51939..8d354f6a13 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)