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.
This commit is contained in:
Enrico Forestieri 2015-06-12 17:04:00 +02:00
parent 4c92d969eb
commit 565b4e9fd2
5 changed files with 58 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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

View File

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