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 commit is contained in:
Enrico Forestieri 2015-06-10 19:21:27 +02:00
parent 4feec2ef4b
commit 21e908b8c4
3 changed files with 48 additions and 9 deletions

View File

@ -343,7 +343,7 @@ endif
######################### Qt stuff ############################## ######################### Qt stuff ##############################
MOCHEADER = Compare.h MOCHEADER = Compare.h PreviewLoader.h
if INSTALL_WINDOWS if INSTALL_WINDOWS
@ -357,6 +357,9 @@ MOCEDFILES = $(MOCHEADER:%.h=moc_%.cpp)
BUILT_SOURCES += $(MOCEDFILES) BUILT_SOURCES += $(MOCEDFILES)
CLEANFILES += $(MOCEDFILES) CLEANFILES += $(MOCEDFILES)
moc_PreviewLoader.cpp: graphics/PreviewLoader.h
$(AM_V_GEN)$(QT_MOC) $(MOCFLAG) -o $@ $<
moc_%.cpp: %.h moc_%.cpp: %.h
$(AM_V_GEN)$(QT_MOC) $(MOCFLAG) -o $@ $< $(AM_V_GEN)$(QT_MOC) $(MOCFLAG) -o $@ $<

View File

@ -44,6 +44,8 @@
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <QTimer>
using namespace std; using namespace std;
using namespace lyx::support; using namespace lyx::support;
@ -212,6 +214,8 @@ public:
/// \p wait whether to wait for the process to complete or, instead, /// \p wait whether to wait for the process to complete or, instead,
/// to do it in the background. /// to do it in the background.
void startLoading(bool wait = false); void startLoading(bool wait = false);
///
void refreshPreviews();
/// Emit this signal when an image is ready for display. /// Emit this signal when an image is ready for display.
boost::signal<void(PreviewImage const &)> imageReady; boost::signal<void(PreviewImage const &)> imageReady;
@ -255,6 +259,8 @@ private:
Buffer const & buffer_; Buffer const & buffer_;
/// ///
mutable int font_scaling_factor_; mutable int font_scaling_factor_;
///
QTimer * delay_refresh_;
/// We don't own this /// We don't own this
static lyx::Converter const * pconverter_; static lyx::Converter const * pconverter_;
@ -321,6 +327,12 @@ void PreviewLoader::startLoading(bool wait) const
} }
void PreviewLoader::refreshPreviews()
{
pimpl_->refreshPreviews();
}
boost::signals::connection PreviewLoader::connect(slot_type const & slot) const boost::signals::connection PreviewLoader::connect(slot_type const & slot) const
{ {
return pimpl_->imageReady.connect(slot); return pimpl_->imageReady.connect(slot);
@ -414,6 +426,11 @@ PreviewLoader::Impl::Impl(PreviewLoader & p, Buffer const & b)
font_scaling_factor_ = int(buffer_.fontScalingFactor()); font_scaling_factor_ = int(buffer_.fontScalingFactor());
if (!pconverter_) if (!pconverter_)
pconverter_ = setConverter("lyxpreview"); pconverter_ = setConverter("lyxpreview");
delay_refresh_ = new QTimer(&parent_);
delay_refresh_->setSingleShot(true);
QObject::connect(delay_refresh_, SIGNAL(timeout()),
&parent_, SLOT(refreshPreviews()));
} }
@ -432,19 +449,30 @@ PreviewLoader::Impl::preview(string const & latex_snippet) const
{ {
int fs = int(buffer_.fontScalingFactor()); int fs = int(buffer_.fontScalingFactor());
if (font_scaling_factor_ != fs) { if (font_scaling_factor_ != fs) {
// Refresh all previews on zoom changes // Schedule refresh of all previews on zoom changes.
font_scaling_factor_ = fs; // The previews are regenerated only after the zoom factor
Cache::const_iterator cit = cache_.begin(); // has not been changed for about 1 second.
Cache::const_iterator cend = cache_.end(); delay_refresh_->start(1000);
while (cit != cend)
parent_.remove((cit++)->first);
buffer_.updatePreviews();
} }
// 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); Cache::const_iterator it = cache_.find(latex_snippet);
return (it == cache_.end()) ? 0 : it->second.get(); return (it == cache_.end()) ? 0 : it->second.get();
} }
void PreviewLoader::Impl::refreshPreviews()
{
font_scaling_factor_ = int(buffer_.fontScalingFactor());
Cache::const_iterator cit = cache_.begin();
Cache::const_iterator cend = cache_.end();
while (cit != cend)
parent_.remove((cit++)->first);
buffer_.updatePreviews();
}
namespace { namespace {
class FindSnippet { class FindSnippet {
@ -851,3 +879,5 @@ void PreviewLoader::Impl::dumpData(odocstream & os,
} // namespace graphics } // namespace graphics
} // namespace lyx } // namespace lyx
#include "moc_PreviewLoader.cpp"

View File

@ -19,6 +19,7 @@
#define PREVIEWLOADER_H #define PREVIEWLOADER_H
#include <boost/signal.hpp> #include <boost/signal.hpp>
#include <QObject>
#include "support/docstring.h" #include "support/docstring.h"
#include "ColorCode.h" #include "ColorCode.h"
@ -31,7 +32,8 @@ namespace graphics {
class PreviewImage; class PreviewImage;
class PreviewLoader { class PreviewLoader : public QObject {
Q_OBJECT
public: public:
/** We need buffer because we require the preamble to the /** We need buffer because we require the preamble to the
* LaTeX file. * LaTeX file.
@ -99,6 +101,10 @@ public:
double displayPixelRatio() const ; double displayPixelRatio() const ;
public Q_SLOTS:
///
void refreshPreviews();
private: private:
/// noncopyable /// noncopyable
PreviewLoader(PreviewLoader const &); PreviewLoader(PreviewLoader const &);