mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-12-22 21:21:32 +00:00
Create a grfx::Loader class and so move large chunks of code out of
frontends/screen.C and insets/insetgraphics.C. Leave mathed/preview.C untouched as an excercise for Andr� ;-) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4489 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
b02f64437d
commit
1bb197b5d4
@ -1,3 +1,8 @@
|
||||
2002-06-26 Angus Leeming <leeming@lyx.org>
|
||||
|
||||
* screen.C: moved the image loading stuff into a new class grfx::Loader.
|
||||
Simplifies this file enormously.
|
||||
|
||||
2002-06-25 Angus Leeming <leeming@lyx.org>
|
||||
|
||||
* screen.C: add a SplashScreen class to load up the pixmap once again.
|
||||
|
@ -29,15 +29,14 @@
|
||||
#include "lyxfont.h"
|
||||
#include "version.h"
|
||||
|
||||
#include "graphics/GraphicsCache.h"
|
||||
#include "graphics/GraphicsCacheItem.h"
|
||||
#include "graphics/GraphicsLoader.h"
|
||||
#include "graphics/GraphicsImage.h"
|
||||
#include "graphics/GraphicsParams.h"
|
||||
|
||||
#include "support/filetools.h" // LibFileSearch
|
||||
|
||||
#include <boost/utility.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/signals/trackable.hpp>
|
||||
|
||||
using std::min;
|
||||
using std::max;
|
||||
@ -45,36 +44,25 @@ using std::endl;
|
||||
|
||||
namespace {
|
||||
|
||||
class SplashScreen : boost::noncopyable {
|
||||
class SplashScreen : boost::noncopyable, boost::signals::trackable {
|
||||
public:
|
||||
/// This is a singleton class. Get the instance.
|
||||
static SplashScreen const & get();
|
||||
///
|
||||
grfx::GImage const * image() const { return graphic_->image().get(); }
|
||||
grfx::GImage const * image() const { return loader_.image(); }
|
||||
///
|
||||
string const & text() const { return text_; }
|
||||
///
|
||||
LyXFont const & font() const { return font_; }
|
||||
|
||||
private:
|
||||
/** Make the c-tor, d-tor private so we can control how many objects
|
||||
/** Make the c-tor private so we can control how many objects
|
||||
* are instantiated.
|
||||
*/
|
||||
SplashScreen();
|
||||
|
||||
///
|
||||
~SplashScreen();
|
||||
|
||||
/** Connected to grfx::GCacheItem::statusChanged, so will generate the
|
||||
* pixmap as soon as the file is loaded into memory.
|
||||
*/
|
||||
void createPixmap();
|
||||
|
||||
/** Must store a copy of the cached item to ensure that it is not
|
||||
* erased unexpectedly by the cache itself.
|
||||
*/
|
||||
grfx::GraphicPtr graphic_;
|
||||
/// The loading status of the image.
|
||||
grfx::ImageStatus status_;
|
||||
grfx::Loader loader_;
|
||||
/// The text to be written on top of the pixmap
|
||||
string const text_;
|
||||
/// in this font...
|
||||
@ -90,8 +78,7 @@ SplashScreen const & SplashScreen::get()
|
||||
|
||||
|
||||
SplashScreen::SplashScreen()
|
||||
: status_(grfx::WaitingToLoad),
|
||||
text_(lyx_version ? lyx_version : "unknown")
|
||||
: text_(lyx_version ? lyx_version : "unknown")
|
||||
{
|
||||
string const file = LibFileSearch("images", "banner", "xpm");
|
||||
if (file.empty())
|
||||
@ -104,54 +91,13 @@ SplashScreen::SplashScreen()
|
||||
font_.setColor(LColor::yellow);
|
||||
|
||||
// Load up the graphics file
|
||||
grfx::GCache & gc = grfx::GCache::get();
|
||||
if (!gc.inCache(file))
|
||||
gc.add(file);
|
||||
// We /must/ make a local copy of this.
|
||||
graphic_ = gc.graphic(file);
|
||||
|
||||
if (graphic_->status() == grfx::Loaded) {
|
||||
createPixmap();
|
||||
} else {
|
||||
graphic_->statusChanged.connect(
|
||||
boost::bind(&SplashScreen::createPixmap, this));
|
||||
graphic_->startLoading();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SplashScreen::~SplashScreen()
|
||||
{
|
||||
if (!graphic_.get())
|
||||
return;
|
||||
|
||||
string const file = graphic_->filename();
|
||||
graphic_.reset();
|
||||
|
||||
// If only the cache itself now references this item, then it will be
|
||||
// removed.
|
||||
grfx::GCache::get().remove(file);
|
||||
}
|
||||
|
||||
|
||||
void SplashScreen::createPixmap()
|
||||
{
|
||||
if (!graphic_.get() || status_ != grfx::WaitingToLoad)
|
||||
return;
|
||||
|
||||
// We aren't going to modify the image, so don't bother making a
|
||||
// local copy
|
||||
grfx::GImage * const image = graphic_->image().get();
|
||||
if (!image)
|
||||
return;
|
||||
|
||||
if (image->getPixmap()) {
|
||||
status_ = grfx::Loaded;
|
||||
return;
|
||||
}
|
||||
|
||||
bool const success = image->setPixmap(grfx::GParams());
|
||||
status_ = success ? grfx::Loaded : grfx::ErrorLoading;
|
||||
loader_.reset(file);
|
||||
// We aren't interested here in when the image is loaded.
|
||||
// If it isn't ready when we want it, then we ignore it.
|
||||
// loader_->statusChanged.connect(
|
||||
// boost::bind(&SplashScreen::statusChanged, this));
|
||||
if (loader_.status() == grfx::WaitingToLoad)
|
||||
loader_.startLoading();
|
||||
}
|
||||
|
||||
} // namespace anon
|
||||
|
@ -1,3 +1,8 @@
|
||||
2002-06-26 Angus Leeming <leeming@lyx.org>
|
||||
|
||||
* xformsGImage.C (load, setPixmap): changes aassociated with moving
|
||||
grfx::DisplayType out of grfx::GParams.
|
||||
|
||||
2002-06-25 Angus Leeming <leeming@lyx.org>
|
||||
|
||||
* FormPreferences.C (LnFmisc::apply): Comment out the call to
|
||||
|
@ -203,7 +203,7 @@ void xformsGImage::load(string const & filename, SignalTypePtr on_finish)
|
||||
|
||||
bool xformsGImage::setPixmap(GParams const & params)
|
||||
{
|
||||
if (!image_ || params.display == GParams::NONE)
|
||||
if (!image_ || params.display == NoDisplay)
|
||||
return false;
|
||||
|
||||
Display * display = fl_get_display();
|
||||
@ -213,14 +213,14 @@ bool xformsGImage::setPixmap(GParams const & params)
|
||||
|
||||
int color_key;
|
||||
switch (params.display) {
|
||||
case GParams::MONOCHROME:
|
||||
case MonochromeDisplay:
|
||||
color_key = FL_IMAGE_MONO;
|
||||
break;
|
||||
case GParams::GRAYSCALE:
|
||||
case GrayscaleDisplay:
|
||||
color_key = FL_IMAGE_GRAY;
|
||||
break;
|
||||
case GParams::COLOR:
|
||||
default: // NONE cannot happen!
|
||||
case ColorDisplay:
|
||||
default: // NoDisplay cannot happen!
|
||||
color_key = FL_IMAGE_RGB;
|
||||
break;
|
||||
}
|
||||
|
@ -1,3 +1,21 @@
|
||||
2002-06-26 Angus Leeming <leeming@lyx.org>
|
||||
|
||||
* GraphicsTypes.h: add "Ready" to the ImageStatus enum.
|
||||
move the DisplayType enum out of grfx::GParams to here.
|
||||
|
||||
* GraphicsCacheItem.h: re-write preliminary description to reflect
|
||||
current reality.
|
||||
|
||||
* GraphicsParams.[Ch]:
|
||||
* GraphicsImageXPM.C (load, setPixmap): changes aassociated with moving
|
||||
grfx::DisplayType out of grfx::GParams.
|
||||
|
||||
* GraphicsLoader.[Ch]: new files. Factor out the image loading stuff
|
||||
that was in frontends/screen.C and insets/insetgraphics.C into a
|
||||
single, easy-to-use class.
|
||||
|
||||
* Makefile.am: add these files.
|
||||
|
||||
2002-06-25 Angus Leeming <leeming@lyx.org>
|
||||
|
||||
* GraphicsCache.[Ch]:
|
||||
|
@ -8,13 +8,10 @@
|
||||
* \author Angus Leeming <a.leeming@ic.ac.uk>
|
||||
*
|
||||
* The graphics cache is a container of GCacheItems. Each GCacheItem, defined
|
||||
* here represents a separate image file. However, each file can be viewed in
|
||||
* different ways (different sizes, rotations etc), so each GCacheItem itself
|
||||
* contains a list of ModifiedItems, also defined here. Each ModifiedItem
|
||||
* has a GParams variable that defines the way it will be viewed. It also
|
||||
* contains a list of the graphics insets that refer to it, so calls through
|
||||
* the GCache to GCacheItem ultimately return the loading status and image
|
||||
* for that particular graphics inset.
|
||||
* here represents a separate image file. The routines here can be used to
|
||||
* load the graphics file into memory at which point (status() == grfx::Loaded).
|
||||
* The user is then free to access image() in order to transform the image
|
||||
* (rotate, scale, clip) and to generate the pixmap.
|
||||
*
|
||||
* The graphics cache supports fully asynchronous:
|
||||
* file conversion to a loadable format;
|
||||
@ -22,8 +19,6 @@
|
||||
*
|
||||
* Whether you get that, of course, depends on grfx::GConverter and on the
|
||||
* grfx::GImage-derived image class.
|
||||
*
|
||||
* Image modification (scaling, rotation etc) is blocking.
|
||||
*/
|
||||
|
||||
#ifndef GRAPHICSCACHEITEM_H
|
||||
|
@ -157,7 +157,7 @@ void GImageXPM::load(string const & filename, GImage::SignalTypePtr on_finish)
|
||||
|
||||
bool GImageXPM::setPixmap(GParams const & params)
|
||||
{
|
||||
if (image_.empty() || params.display == GParams::NONE) {
|
||||
if (image_.empty() || params.display == NoDisplay) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -182,14 +182,14 @@ bool GImageXPM::setPixmap(GParams const & params)
|
||||
// The XPM file format allows multiple pixel colours to be defined
|
||||
// as c_color, g_color or m_color.
|
||||
switch (params.display) {
|
||||
case GParams::MONOCHROME:
|
||||
case MonochromeDisplay:
|
||||
attrib.color_key = XPM_MONO;
|
||||
break;
|
||||
case GParams::GRAYSCALE:
|
||||
case GrayscaleDisplay:
|
||||
attrib.color_key = XPM_GRAY;
|
||||
break;
|
||||
case GParams::COLOR:
|
||||
default: // NONE cannot happen!
|
||||
case ColorDisplay:
|
||||
default: // NoDisplay cannot happen!
|
||||
attrib.color_key = XPM_COLOR;
|
||||
break;
|
||||
}
|
||||
|
214
src/graphics/GraphicsLoader.C
Normal file
214
src/graphics/GraphicsLoader.C
Normal file
@ -0,0 +1,214 @@
|
||||
/*
|
||||
* \file GraphicsLoader.C
|
||||
* Copyright 2002 the LyX Team
|
||||
* Read the file COPYING
|
||||
*
|
||||
* \author Angus Leeming <leeming@lyx.org>
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
|
||||
#include "GraphicsLoader.h"
|
||||
#include "GraphicsCache.h"
|
||||
#include "GraphicsCacheItem.h"
|
||||
#include "GraphicsImage.h"
|
||||
#include "GraphicsParams.h"
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
namespace grfx {
|
||||
|
||||
struct Loader::Impl {
|
||||
///
|
||||
Impl(Loader &, GParams const &);
|
||||
///
|
||||
~Impl();
|
||||
///
|
||||
void setFile(string const & file);
|
||||
///
|
||||
void unsetOldFile();
|
||||
///
|
||||
void createPixmap();
|
||||
///
|
||||
void statusChanged();
|
||||
|
||||
///
|
||||
Loader & parent_;
|
||||
/// The loading status of the image.
|
||||
ImageStatus status_;
|
||||
/** Must store a copy of the cached item to ensure that it is not
|
||||
* erased unexpectedly by the cache itself.
|
||||
*/
|
||||
GraphicPtr graphic_;
|
||||
///
|
||||
GParams params_;
|
||||
/// We modify a local copy of the image once it is loaded.
|
||||
ImagePtr image_;
|
||||
};
|
||||
|
||||
|
||||
Loader::Impl::Impl(Loader & parent, GParams const & params)
|
||||
: parent_(parent), status_(WaitingToLoad), params_(params)
|
||||
{}
|
||||
|
||||
|
||||
Loader::Impl::~Impl()
|
||||
{
|
||||
unsetOldFile();
|
||||
}
|
||||
|
||||
|
||||
void Loader::Impl::setFile(string const & file)
|
||||
{
|
||||
if (file.empty())
|
||||
return;
|
||||
|
||||
GCache & gc = GCache::get();
|
||||
if (!gc.inCache(file))
|
||||
gc.add(file);
|
||||
|
||||
// We /must/ make a local copy of this.
|
||||
graphic_ = gc.graphic(file);
|
||||
status_ = graphic_->status();
|
||||
|
||||
if (status_ == Loaded) {
|
||||
createPixmap();
|
||||
}
|
||||
|
||||
// It's easiest to do this without checking
|
||||
parent_.statusChanged();
|
||||
}
|
||||
|
||||
|
||||
void Loader::Impl::unsetOldFile()
|
||||
{
|
||||
if (!graphic_.get())
|
||||
return;
|
||||
|
||||
string const old_file = graphic_->filename();
|
||||
graphic_.reset();
|
||||
GCache::get().remove(old_file);
|
||||
|
||||
status_ = WaitingToLoad;
|
||||
params_ = GParams();
|
||||
image_.reset();
|
||||
}
|
||||
|
||||
|
||||
void Loader::Impl::statusChanged()
|
||||
{
|
||||
status_ = graphic_->status();
|
||||
if (status_ == Loaded)
|
||||
createPixmap();
|
||||
|
||||
parent_.statusChanged();
|
||||
}
|
||||
|
||||
|
||||
void Loader::Impl::createPixmap()
|
||||
{
|
||||
if (!graphic_.get() || image_.get() ||
|
||||
params_.display == NoDisplay || status_ != Loaded)
|
||||
return;
|
||||
|
||||
image_.reset(graphic_->image()->clone());
|
||||
|
||||
// These do nothing if there's nothing to do
|
||||
image_->clip(params_);
|
||||
image_->rotate(params_);
|
||||
image_->scale(params_);
|
||||
|
||||
bool const success = image_->setPixmap(params_);
|
||||
|
||||
if (success) {
|
||||
status_ = Ready;
|
||||
} else {
|
||||
image_.reset();
|
||||
status_ = ErrorGeneratingPixmap;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loader::Loader()
|
||||
: pimpl_(new Impl(*this, GParams()))
|
||||
{}
|
||||
|
||||
|
||||
Loader::Loader(string const & file, DisplayType type)
|
||||
: pimpl_(new Impl(*this, GParams()))
|
||||
{
|
||||
pimpl_->params_.display = type;
|
||||
pimpl_->setFile(file);
|
||||
}
|
||||
|
||||
|
||||
Loader::Loader(string const & file, GParams const & params)
|
||||
: pimpl_(new Impl(*this, params))
|
||||
{
|
||||
pimpl_->setFile(file);
|
||||
}
|
||||
|
||||
|
||||
void Loader::reset(string const & file, DisplayType type)
|
||||
{
|
||||
pimpl_->unsetOldFile();
|
||||
|
||||
pimpl_->params_ = GParams();
|
||||
pimpl_->params_.display = type;
|
||||
pimpl_->setFile(file);
|
||||
}
|
||||
|
||||
|
||||
void Loader::reset(string const & file, GParams const & params)
|
||||
{
|
||||
pimpl_->unsetOldFile();
|
||||
|
||||
pimpl_->params_ = params;
|
||||
pimpl_->setFile(file);
|
||||
}
|
||||
|
||||
|
||||
void Loader::reset(GParams const & params)
|
||||
{
|
||||
pimpl_->params_ = params;
|
||||
|
||||
if (pimpl_->status_ == Loaded)
|
||||
pimpl_->createPixmap();
|
||||
}
|
||||
|
||||
|
||||
void Loader::startLoading()
|
||||
{
|
||||
if (pimpl_->status_ != WaitingToLoad || !pimpl_->graphic_.get())
|
||||
return;
|
||||
|
||||
pimpl_->graphic_->statusChanged.connect(
|
||||
boost::bind(&Loader::Impl::statusChanged,
|
||||
pimpl_.get()));
|
||||
pimpl_->graphic_->startLoading();
|
||||
}
|
||||
|
||||
|
||||
string const & Loader::filename() const
|
||||
{
|
||||
static string const empty;
|
||||
return pimpl_->graphic_.get() ? pimpl_->graphic_->filename() : empty;
|
||||
}
|
||||
|
||||
|
||||
ImageStatus Loader::status() const
|
||||
{
|
||||
return pimpl_->status_;
|
||||
}
|
||||
|
||||
|
||||
GImage const * Loader::image() const
|
||||
{
|
||||
return pimpl_->image_.get();
|
||||
}
|
||||
|
||||
} // namespace grfx
|
84
src/graphics/GraphicsLoader.h
Normal file
84
src/graphics/GraphicsLoader.h
Normal file
@ -0,0 +1,84 @@
|
||||
// -*- C++ -*-
|
||||
/**
|
||||
* \file GraphicsLoader.h
|
||||
* Copyright 2002 the LyX Team
|
||||
* Read the file COPYING
|
||||
*
|
||||
* \author Angus Leeming <leeming@lyx.org>
|
||||
*
|
||||
* The public view of the graphics cache.
|
||||
* * The user supplies an image file and the display parameters.
|
||||
* * He can change the file or the display parameters through a reset() method.
|
||||
* * He must start the loading process explicitly with startLoading().
|
||||
* * He receives a statusChanged signal when the loading status changes.
|
||||
* * When (status() == Ready), he uses image() to access the loaded image
|
||||
* and passes it to the Painter.
|
||||
*
|
||||
* What could be simpler?
|
||||
*/
|
||||
|
||||
#ifndef GRAPHICSLOADER_H
|
||||
#define GRAPHICSLOADER_H
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma interface
|
||||
#endif
|
||||
|
||||
#include "GraphicsTypes.h"
|
||||
#include "LString.h"
|
||||
|
||||
#include <boost/signals/signal0.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace grfx {
|
||||
|
||||
class GParams;
|
||||
|
||||
/** One image, one instance of grfx::Loader, although the image can be
|
||||
* changed.
|
||||
*/
|
||||
class Loader {
|
||||
public:
|
||||
/// Must use the reset methods to make this instance usable.
|
||||
Loader();
|
||||
/// The image is not transformed, just displayed as-is.
|
||||
Loader(string const & file_with_path, DisplayType = ColorDisplay);
|
||||
/// The image is transformed before display.
|
||||
Loader(string const & file_with_path, GParams const &);
|
||||
|
||||
/// The file can be changed, or the display params, or both.
|
||||
void reset(string const & file_with_path, DisplayType = ColorDisplay);
|
||||
///
|
||||
void reset(string const & file_with_path, GParams const &);
|
||||
///
|
||||
void reset(GParams const &);
|
||||
|
||||
/// Returns the absolute path of the loaded (loading?) file.
|
||||
string const & filename() const;
|
||||
///
|
||||
bool empty() const { return filename().empty(); }
|
||||
|
||||
/// We are explicit about when we begin the loading process.
|
||||
void startLoading();
|
||||
|
||||
/// How far have we got in loading the image?
|
||||
ImageStatus status() const;
|
||||
|
||||
/// This signal is emitted when the image loading status changes.
|
||||
boost::signal0<void> statusChanged;
|
||||
|
||||
/** The loaded image with Pixmap set.
|
||||
* If the Pixmap is not yet set (see status() for why...), returns 0.
|
||||
*/
|
||||
GImage const * image() const;
|
||||
|
||||
private:
|
||||
/// Use the Pimpl idiom to hide the internals.
|
||||
class Impl;
|
||||
/// The pointer never changes although *pimpl_'s contents may.
|
||||
std::auto_ptr<Impl> const pimpl_;
|
||||
};
|
||||
|
||||
} // namespace grfx
|
||||
|
||||
#endif // GRAPHICSLOADER_H
|
@ -20,7 +20,7 @@
|
||||
namespace grfx {
|
||||
|
||||
GParams::GParams()
|
||||
: display(COLOR),
|
||||
: display(ColorDisplay),
|
||||
width(0),
|
||||
height(0),
|
||||
scale(0),
|
||||
|
@ -16,6 +16,7 @@
|
||||
#pragma interface
|
||||
#endif
|
||||
|
||||
#include "GraphicsTypes.h"
|
||||
#include "LString.h"
|
||||
|
||||
|
||||
@ -46,15 +47,6 @@ struct GParams
|
||||
{
|
||||
GParams();
|
||||
|
||||
/// How is the image to be displayed on the LyX screen?
|
||||
enum DisplayType {
|
||||
COLOR,
|
||||
GRAYSCALE,
|
||||
MONOCHROME,
|
||||
/// We aren't going to display it at all!
|
||||
NONE
|
||||
};
|
||||
|
||||
DisplayType display;
|
||||
|
||||
/// The image filename.
|
||||
|
@ -41,21 +41,35 @@ namespace grfx {
|
||||
Loading,
|
||||
/// The image is being converted to a loadable format.
|
||||
Converting,
|
||||
/// The image has been loaded into memory.
|
||||
Loaded,
|
||||
/// The image is in memory and is being scaled, rotated, etc.
|
||||
ScalingEtc,
|
||||
/// All finished. Can display the image.
|
||||
Loaded,
|
||||
Ready,
|
||||
///
|
||||
ErrorNoFile,
|
||||
///
|
||||
ErrorConverting,
|
||||
///
|
||||
ErrorLoading,
|
||||
/// Fall back on the unmodified image?
|
||||
ErrorScalingEtc,
|
||||
///
|
||||
ErrorGeneratingPixmap,
|
||||
/// The data is not in the cache at all!
|
||||
ErrorUnknown
|
||||
};
|
||||
|
||||
/// How is the image to be displayed on the LyX screen?
|
||||
enum DisplayType {
|
||||
///
|
||||
ColorDisplay,
|
||||
///
|
||||
GrayscaleDisplay,
|
||||
///
|
||||
MonochromeDisplay,
|
||||
///
|
||||
NoDisplay
|
||||
};
|
||||
}
|
||||
|
||||
#endif // GRAPHICSTYPES_H
|
||||
|
@ -19,6 +19,8 @@ libgraphics_la_SOURCES = \
|
||||
GraphicsConverter.C \
|
||||
GraphicsImage.h \
|
||||
GraphicsImage.C \
|
||||
GraphicsLoader.h \
|
||||
GraphicsLoader.C \
|
||||
$(GRAPHICSIMAGEXPM) GraphicsParams.C \
|
||||
GraphicsParams.h \
|
||||
GraphicsTypes.h
|
||||
|
@ -1,3 +1,13 @@
|
||||
2002-06-26 Angus Leeming <leeming@lyx.org>
|
||||
|
||||
* insetgraphics.C: moved the image loading stuff into a new class
|
||||
grfx::Loader. Simplifies this file enormously.
|
||||
|
||||
* insetgraphics.h: no longer #include "graphics/GraphicsTypes.h".
|
||||
|
||||
* insetgraphicsParams.C: changes aassociated with moving
|
||||
grfx::DisplayType out of grfx::GParams.
|
||||
|
||||
2002-06-26 André Pönitz <poenitz@gmx.net>
|
||||
|
||||
* insetgraphics.C: use os::external_path where necesaary according to a
|
||||
|
@ -78,8 +78,7 @@ TODO Before initial production release:
|
||||
#include "insets/insetgraphics.h"
|
||||
#include "insets/insetgraphicsParams.h"
|
||||
|
||||
#include "graphics/GraphicsCache.h"
|
||||
#include "graphics/GraphicsCacheItem.h"
|
||||
#include "graphics/GraphicsLoader.h"
|
||||
#include "graphics/GraphicsImage.h"
|
||||
|
||||
#include "frontends/LyXView.h"
|
||||
@ -106,6 +105,7 @@ TODO Before initial production release:
|
||||
#include "support/os.h"
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/signals/trackable.hpp>
|
||||
|
||||
#include <algorithm> // For the std::max
|
||||
|
||||
@ -150,102 +150,29 @@ string const uniqueID()
|
||||
} // namespace anon
|
||||
|
||||
|
||||
struct InsetGraphics::Cache
|
||||
struct InsetGraphics::Cache : boost::signals::trackable
|
||||
{
|
||||
///
|
||||
Cache(InsetGraphics &);
|
||||
///
|
||||
~Cache();
|
||||
///
|
||||
void reset(grfx::GraphicPtr const &);
|
||||
///
|
||||
bool empty() const { return !graphic_.get(); }
|
||||
///
|
||||
grfx::ImageStatus status() const;
|
||||
///
|
||||
void setStatus(grfx::ImageStatus);
|
||||
///
|
||||
grfx::GImage * image() const;
|
||||
///
|
||||
string const filename() const;
|
||||
///
|
||||
void update(string const & file_with_path);
|
||||
///
|
||||
void modify();
|
||||
|
||||
///
|
||||
int old_ascent;
|
||||
///
|
||||
grfx::GraphicPtr graphic_;
|
||||
grfx::Loader loader;
|
||||
|
||||
private:
|
||||
/// The connection to cache_->statusChanged.
|
||||
boost::signals::connection cc_;
|
||||
///
|
||||
grfx::ImageStatus status_;
|
||||
///
|
||||
grfx::GParams params_;
|
||||
///
|
||||
grfx::ImagePtr modified_image_;
|
||||
///
|
||||
InsetGraphics & parent_;
|
||||
};
|
||||
|
||||
|
||||
InsetGraphics::Cache::Cache(InsetGraphics & p)
|
||||
: old_ascent(0), status_(grfx::ErrorUnknown), parent_(p)
|
||||
{}
|
||||
|
||||
|
||||
InsetGraphics::Cache::~Cache()
|
||||
: old_ascent(0), parent_(p)
|
||||
{
|
||||
string const old_file = filename();
|
||||
graphic_.reset();
|
||||
|
||||
if (!old_file.empty()) {
|
||||
grfx::GCache & gc = grfx::GCache::get();
|
||||
gc.remove(old_file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void InsetGraphics::Cache::reset(grfx::GraphicPtr const & graphic)
|
||||
{
|
||||
string const old_file = filename();
|
||||
string const new_file = graphic.get() ? graphic->filename() : string();
|
||||
if (old_file == new_file)
|
||||
return;
|
||||
|
||||
graphic_ = graphic;
|
||||
|
||||
if (!old_file.empty()) {
|
||||
grfx::GCache & gc = grfx::GCache::get();
|
||||
gc.remove(old_file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
grfx::ImageStatus InsetGraphics::Cache::status() const
|
||||
{
|
||||
return status_;
|
||||
}
|
||||
|
||||
|
||||
void InsetGraphics::Cache::setStatus(grfx::ImageStatus new_status)
|
||||
{
|
||||
status_ = new_status;
|
||||
}
|
||||
|
||||
|
||||
grfx::GImage * InsetGraphics::Cache::image() const
|
||||
{
|
||||
return modified_image_.get();
|
||||
}
|
||||
|
||||
|
||||
string const InsetGraphics::Cache::filename() const
|
||||
{
|
||||
return empty() ? string() : graphic_->filename();
|
||||
loader.statusChanged.connect(
|
||||
boost::bind(&InsetGraphics::statusChanged, &parent_));
|
||||
}
|
||||
|
||||
|
||||
@ -253,64 +180,8 @@ void InsetGraphics::Cache::update(string const & file_with_path)
|
||||
{
|
||||
lyx::Assert(!file_with_path.empty());
|
||||
|
||||
// Check whether the file has changed.
|
||||
string current_file = filename();
|
||||
|
||||
if (current_file == file_with_path) {
|
||||
modify();
|
||||
return;
|
||||
}
|
||||
|
||||
// It /has/ changed.
|
||||
// Remove the connection to any previous grfx::CacheItems
|
||||
grfx::GCache & gc = grfx::GCache::get();
|
||||
if (!current_file.empty() && gc.inCache(current_file)) {
|
||||
graphic_.reset();
|
||||
gc.remove(current_file);
|
||||
cc_.disconnect();
|
||||
}
|
||||
|
||||
// Update the cache to point to the new file
|
||||
if (!gc.inCache(file_with_path))
|
||||
gc.add(file_with_path);
|
||||
|
||||
graphic_ = gc.graphic(file_with_path);
|
||||
cc_ = graphic_->statusChanged.connect(
|
||||
boost::bind(&InsetGraphics::statusChanged, &parent_));
|
||||
|
||||
setStatus(graphic_->status());
|
||||
if (status() == grfx::Loaded)
|
||||
modify();
|
||||
}
|
||||
|
||||
|
||||
void InsetGraphics::Cache::modify()
|
||||
{
|
||||
// The image has not been loaded from file
|
||||
if (!graphic_->image().get())
|
||||
return;
|
||||
|
||||
string const path = OnlyPath(filename());
|
||||
grfx::GParams params = parent_.params().asGParams(path);
|
||||
|
||||
if (params == params_)
|
||||
return;
|
||||
|
||||
params_ = params;
|
||||
setStatus(grfx::ScalingEtc);
|
||||
modified_image_.reset(graphic_->image()->clone());
|
||||
modified_image_->clip(params);
|
||||
modified_image_->rotate(params);
|
||||
modified_image_->scale(params);
|
||||
|
||||
bool const success = modified_image_->setPixmap(params);
|
||||
|
||||
if (success) {
|
||||
setStatus(grfx::Loaded);
|
||||
} else {
|
||||
modified_image_.reset();
|
||||
setStatus(grfx::ErrorScalingEtc);
|
||||
}
|
||||
string const path = OnlyPath(file_with_path);
|
||||
loader.reset(file_with_path, parent_.params().asGParams(path));
|
||||
}
|
||||
|
||||
|
||||
@ -333,7 +204,6 @@ InsetGraphics::InsetGraphics(InsetGraphics const & ig,
|
||||
|
||||
InsetGraphics::~InsetGraphics()
|
||||
{
|
||||
cache_->reset(grfx::GraphicPtr());
|
||||
// Emits the hide signal to the dialog connected (if any)
|
||||
hideDialog();
|
||||
}
|
||||
@ -343,7 +213,7 @@ string const InsetGraphics::statusMessage() const
|
||||
{
|
||||
string msg;
|
||||
|
||||
switch (cache_->status()) {
|
||||
switch (cache_->loader.status()) {
|
||||
case grfx::WaitingToLoad:
|
||||
msg = _("Waiting for draw request to start loading...");
|
||||
break;
|
||||
@ -353,23 +223,26 @@ string const InsetGraphics::statusMessage() const
|
||||
case grfx::Converting:
|
||||
msg = _("Converting to loadable format...");
|
||||
break;
|
||||
case grfx::Loaded:
|
||||
msg = _("Loaded into memory. Must now generate pixmap.");
|
||||
break;
|
||||
case grfx::ScalingEtc:
|
||||
msg = _("Scaling etc...");
|
||||
break;
|
||||
case grfx::Loaded:
|
||||
msg = _("Loaded.");
|
||||
case grfx::Ready:
|
||||
msg = _("Ready to display");
|
||||
break;
|
||||
case grfx::ErrorNoFile:
|
||||
msg = _("No file found!");
|
||||
break;
|
||||
case grfx::ErrorLoading:
|
||||
msg = _("Error loading file into memory");
|
||||
break;
|
||||
case grfx::ErrorConverting:
|
||||
msg = _("Error converting to loadable format");
|
||||
break;
|
||||
case grfx::ErrorScalingEtc:
|
||||
msg = _("Error scaling etc");
|
||||
case grfx::ErrorLoading:
|
||||
msg = _("Error loading file into memory");
|
||||
break;
|
||||
case grfx::ErrorGeneratingPixmap:
|
||||
msg = _("Error generating the pixmap");
|
||||
break;
|
||||
case grfx::ErrorUnknown:
|
||||
msg = _("No image");
|
||||
@ -382,10 +255,10 @@ string const InsetGraphics::statusMessage() const
|
||||
|
||||
bool InsetGraphics::imageIsDrawable() const
|
||||
{
|
||||
if (!cache_->image() || cache_->status() != grfx::Loaded)
|
||||
if (!cache_->loader.image() || cache_->loader.status() != grfx::Ready)
|
||||
return false;
|
||||
|
||||
return cache_->image()->getPixmap() != 0;
|
||||
return cache_->loader.image()->getPixmap() != 0;
|
||||
}
|
||||
|
||||
|
||||
@ -393,7 +266,7 @@ int InsetGraphics::ascent(BufferView *, LyXFont const &) const
|
||||
{
|
||||
cache_->old_ascent = 50;
|
||||
if (imageIsDrawable())
|
||||
cache_->old_ascent = cache_->image()->getHeight();
|
||||
cache_->old_ascent = cache_->loader.image()->getHeight();
|
||||
return cache_->old_ascent;
|
||||
}
|
||||
|
||||
@ -407,7 +280,7 @@ int InsetGraphics::descent(BufferView *, LyXFont const &) const
|
||||
int InsetGraphics::width(BufferView *, LyXFont const & font) const
|
||||
{
|
||||
if (imageIsDrawable())
|
||||
return cache_->image()->getWidth();
|
||||
return cache_->loader.image()->getWidth();
|
||||
else {
|
||||
int font_width = 0;
|
||||
|
||||
@ -454,8 +327,8 @@ void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
|
||||
int old_x = int(x);
|
||||
x += lwidth;
|
||||
|
||||
if (cache_->status() == grfx::WaitingToLoad) {
|
||||
cache_->graphic_->startLoading();
|
||||
if (cache_->loader.status() == grfx::WaitingToLoad) {
|
||||
cache_->loader.startLoading();
|
||||
}
|
||||
|
||||
// This will draw the graphics. If the graphics has not been loaded yet,
|
||||
@ -465,7 +338,7 @@ void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
|
||||
if (imageIsDrawable()) {
|
||||
paint.image(old_x + 2, baseline - lascent,
|
||||
lwidth - 4, lascent + ldescent,
|
||||
*cache_->image());
|
||||
*cache_->loader.image());
|
||||
|
||||
} else {
|
||||
|
||||
@ -1011,10 +884,6 @@ void InsetGraphics::validate(LaTeXFeatures & features) const
|
||||
|
||||
void InsetGraphics::statusChanged()
|
||||
{
|
||||
cache_->setStatus(cache_->graphic_->status());
|
||||
if (cache_->status() == grfx::Loaded)
|
||||
cache_->modify();
|
||||
|
||||
current_view->updateInset(this, false);
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,6 @@
|
||||
#endif
|
||||
|
||||
#include "insets/inset.h"
|
||||
#include "graphics/GraphicsTypes.h"
|
||||
#include "insets/insetgraphicsParams.h"
|
||||
|
||||
// We need a signal here to hide an active dialog when we are deleted.
|
||||
|
@ -359,30 +359,30 @@ grfx::GParams InsetGraphicsParams::asGParams(string const & filepath) const
|
||||
if (display == InsetGraphicsParams::DEFAULT) {
|
||||
|
||||
if (lyxrc.display_graphics == "mono")
|
||||
pars.display = grfx::GParams::MONOCHROME;
|
||||
pars.display = grfx::MonochromeDisplay;
|
||||
else if (lyxrc.display_graphics == "gray")
|
||||
pars.display = grfx::GParams::GRAYSCALE;
|
||||
pars.display = grfx::GrayscaleDisplay;
|
||||
else if (lyxrc.display_graphics == "color")
|
||||
pars.display = grfx::GParams::COLOR;
|
||||
pars.display = grfx::ColorDisplay;
|
||||
else
|
||||
pars.display = grfx::GParams::NONE;
|
||||
pars.display = grfx::NoDisplay;
|
||||
|
||||
} else if (display == InsetGraphicsParams::NONE) {
|
||||
pars.display = grfx::GParams::NONE;
|
||||
pars.display = grfx::NoDisplay;
|
||||
|
||||
} else if (display == InsetGraphicsParams::MONOCHROME) {
|
||||
pars.display = grfx::GParams::MONOCHROME;
|
||||
pars.display = grfx::MonochromeDisplay;
|
||||
|
||||
} else if (display == InsetGraphicsParams::GRAYSCALE) {
|
||||
pars.display = grfx::GParams::GRAYSCALE;
|
||||
pars.display = grfx::GrayscaleDisplay;
|
||||
|
||||
} else if (display == InsetGraphicsParams::COLOR) {
|
||||
pars.display = grfx::GParams::COLOR;
|
||||
pars.display = grfx::ColorDisplay;
|
||||
}
|
||||
|
||||
// Override the above if we're not using a gui
|
||||
if (!lyxrc.use_gui) {
|
||||
pars.display = grfx::GParams::NONE;
|
||||
pars.display = grfx::NoDisplay;
|
||||
}
|
||||
|
||||
if (lyxsize_type == InsetGraphicsParams::SCALE) {
|
||||
|
@ -1,3 +1,6 @@
|
||||
2002-06-26 Angus Leeming <leeming@lyx.org>
|
||||
|
||||
* formula.C: use lyxerr, not cerr.
|
||||
|
||||
2002-06-24 André Pönitz <poenitz@gmx.net>
|
||||
|
||||
|
@ -193,7 +193,7 @@ void InsetFormula::draw(BufferView * bv, LyXFont const & font,
|
||||
WriteStream wi(os, false, false);
|
||||
par_->write(wi);
|
||||
if (preview(os.str(), preview_)) {
|
||||
cerr << "image could be drawn\n";
|
||||
lyxerr << "image could be drawn\n";
|
||||
pi.pain.image(x + w + 2, y - a + 1, w - 2, h - 2, *(preview_->image()));
|
||||
} else {
|
||||
pi.pain.fillRectangle(x + w, y - a, w, h, LColor::white);
|
||||
|
Loading…
Reference in New Issue
Block a user