mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-09 18:31:04 +00:00
Get rid of hte dynamic_casts in InsetExternal.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7980 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
6d1789d4db
commit
afc9aaf6eb
@ -1,6 +1,17 @@
|
|||||||
|
2003-10-25 Angus Leeming <leeming@lyx.org>
|
||||||
|
|
||||||
|
* render_base.h (asButton, asGraphic, asPreview, asMonitoredPreview):
|
||||||
|
new virtual member functions, specialised...
|
||||||
|
* render_button.h (asButton):
|
||||||
|
* render_graphic.h (asGraphic):
|
||||||
|
* render_preview.h (asPreview, asMonitoredPreview): ... here.
|
||||||
|
|
||||||
|
* insetexternal.C: use these new functions rather than dynamic_cast.
|
||||||
|
Various other small simplifications.
|
||||||
|
|
||||||
2003-10-22 Angus Leeming <leeming@lyx.org>
|
2003-10-22 Angus Leeming <leeming@lyx.org>
|
||||||
|
|
||||||
w * insetexternal.[Ch]: add the ability to 'preview' the external file
|
* insetexternal.[Ch]: add the ability to 'preview' the external file
|
||||||
if so desired.
|
if so desired.
|
||||||
|
|
||||||
* insetinclude.C: changes due to the changes in the RenderPreview
|
* insetinclude.C: changes due to the changes in the RenderPreview
|
||||||
|
@ -99,6 +99,7 @@ TempName::operator=(TempName const & other)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
/// The translator between the Display enum and corresponding lyx string.
|
/// The translator between the Display enum and corresponding lyx string.
|
||||||
@ -118,6 +119,7 @@ Translator<DisplayType, string> const initTranslator()
|
|||||||
|
|
||||||
} // namespace anon
|
} // namespace anon
|
||||||
|
|
||||||
|
|
||||||
Translator<DisplayType, string> const & displayTranslator()
|
Translator<DisplayType, string> const & displayTranslator()
|
||||||
{
|
{
|
||||||
static Translator<DisplayType, string> const translator =
|
static Translator<DisplayType, string> const translator =
|
||||||
@ -383,55 +385,6 @@ bool InsetExternalParams::read(Buffer const & buffer, LyXLex & lex)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
enum RenderType {
|
|
||||||
RENDERBUTTON,
|
|
||||||
RENDERGRAPHIC,
|
|
||||||
RENDERPREVIEW
|
|
||||||
};
|
|
||||||
|
|
||||||
RenderType getRenderType(InsetExternalParams const & p)
|
|
||||||
{
|
|
||||||
if (!external::getTemplatePtr(p) || p.filename.empty() ||
|
|
||||||
p.display == external::NoDisplay)
|
|
||||||
return RENDERBUTTON;
|
|
||||||
|
|
||||||
if (p.display == external::PreviewDisplay) {
|
|
||||||
if (RenderPreview::activated())
|
|
||||||
return RENDERPREVIEW;
|
|
||||||
return RENDERBUTTON;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (p.display == external::DefaultDisplay &&
|
|
||||||
lyxrc.display_graphics == graphics::NoDisplay)
|
|
||||||
return RENDERBUTTON;
|
|
||||||
return RENDERGRAPHIC;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
RenderGraphic &
|
|
||||||
graphicRenderer(boost::scoped_ptr<RenderBase> const & renderer)
|
|
||||||
{
|
|
||||||
RenderGraphic * const ptr =
|
|
||||||
dynamic_cast<RenderGraphic *>(renderer.get());
|
|
||||||
BOOST_ASSERT(ptr);
|
|
||||||
return *ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
RenderMonitoredPreview &
|
|
||||||
previewRenderer(boost::scoped_ptr<RenderBase> const & renderer)
|
|
||||||
{
|
|
||||||
RenderMonitoredPreview * const ptr =
|
|
||||||
dynamic_cast<RenderMonitoredPreview *>(renderer.get());
|
|
||||||
BOOST_ASSERT(ptr);
|
|
||||||
return *ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace anon
|
|
||||||
|
|
||||||
|
|
||||||
InsetExternal::InsetExternal()
|
InsetExternal::InsetExternal()
|
||||||
: renderer_(new RenderButton)
|
: renderer_(new RenderButton)
|
||||||
{}
|
{}
|
||||||
@ -443,41 +396,14 @@ InsetExternal::InsetExternal(InsetExternal const & other)
|
|||||||
params_(other.params_),
|
params_(other.params_),
|
||||||
renderer_(other.renderer_->clone())
|
renderer_(other.renderer_->clone())
|
||||||
{
|
{
|
||||||
switch (getRenderType(params_)) {
|
if (renderer_->asMonitoredPreview() != 0) {
|
||||||
case RENDERBUTTON:
|
RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
|
||||||
break;
|
|
||||||
|
|
||||||
case RENDERGRAPHIC: {
|
|
||||||
RenderGraphic * const ptr =
|
|
||||||
dynamic_cast<RenderGraphic *>(renderer_.get());
|
|
||||||
if (!ptr) {
|
|
||||||
lyxerr << "InsetExternal::InsetExternal "
|
|
||||||
<< "Not a RenderGraphic!!"
|
|
||||||
<< std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ptr->connect(boost::bind(&InsetExternal::statusChanged, this));
|
|
||||||
// RenderGraphic & renderer = graphicRenderer(renderer_);
|
|
||||||
// renderer.connect(boost::bind(&InsetExternal::statusChanged, this));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case RENDERPREVIEW: {
|
|
||||||
RenderMonitoredPreview * const ptr =
|
|
||||||
dynamic_cast<RenderMonitoredPreview *>(renderer_.get());
|
|
||||||
if (!ptr) {
|
|
||||||
lyxerr << "InsetExternal::InsetExternal "
|
|
||||||
<< "Not a RenderPreview!!"
|
|
||||||
<< std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ptr->connect(boost::bind(&InsetExternal::statusChanged, this));
|
ptr->connect(boost::bind(&InsetExternal::statusChanged, this));
|
||||||
ptr->fileChanged(boost::bind(&InsetExternal::fileChanged, this));
|
ptr->fileChanged(boost::bind(&InsetExternal::fileChanged, this));
|
||||||
// RenderMonitoredPreview & renderer = previewRenderer(renderer_);
|
|
||||||
// renderer.connect(boost::bind(&InsetExternal::statusChanged, this));
|
} else if (renderer_->asGraphic() != 0 ) {
|
||||||
// renderer.fileChanged(boost::bind(&InsetExternal::fileChanged, this));
|
RenderGraphic * const ptr = renderer_->asGraphic();
|
||||||
break;
|
ptr->connect(boost::bind(&InsetExternal::statusChanged, this));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -556,6 +482,33 @@ void InsetExternal::draw(PainterInfo & pi, int x, int y) const
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
enum RenderType {
|
||||||
|
RENDERBUTTON,
|
||||||
|
RENDERGRAPHIC,
|
||||||
|
RENDERPREVIEW
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
RenderType getRenderType(InsetExternalParams const & p)
|
||||||
|
{
|
||||||
|
if (!external::getTemplatePtr(p) ||
|
||||||
|
p.filename.empty() ||
|
||||||
|
p.display == external::NoDisplay)
|
||||||
|
return RENDERBUTTON;
|
||||||
|
|
||||||
|
if (p.display == external::PreviewDisplay) {
|
||||||
|
if (RenderPreview::activated())
|
||||||
|
return RENDERPREVIEW;
|
||||||
|
return RENDERBUTTON;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p.display == external::DefaultDisplay &&
|
||||||
|
lyxrc.display_graphics == graphics::NoDisplay)
|
||||||
|
return RENDERBUTTON;
|
||||||
|
return RENDERGRAPHIC;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
graphics::Params get_grfx_params(InsetExternalParams const & eparams)
|
graphics::Params get_grfx_params(InsetExternalParams const & eparams)
|
||||||
{
|
{
|
||||||
graphics::Params gparams;
|
graphics::Params gparams;
|
||||||
@ -620,14 +573,11 @@ InsetExternalParams const & InsetExternal::params() const
|
|||||||
void InsetExternal::setParams(InsetExternalParams const & p,
|
void InsetExternal::setParams(InsetExternalParams const & p,
|
||||||
Buffer const & buffer)
|
Buffer const & buffer)
|
||||||
{
|
{
|
||||||
// The stored params; what we would like to happen in an ideal world.
|
|
||||||
params_ = p;
|
params_ = p;
|
||||||
|
|
||||||
// We display the inset as a button by default.
|
|
||||||
switch (getRenderType(params_)) {
|
switch (getRenderType(params_)) {
|
||||||
case RENDERBUTTON: {
|
case RENDERBUTTON: {
|
||||||
RenderButton * button_ptr =
|
RenderButton * button_ptr = renderer_->asButton();
|
||||||
dynamic_cast<RenderButton *>(renderer_.get());
|
|
||||||
if (!button_ptr) {
|
if (!button_ptr) {
|
||||||
button_ptr = new RenderButton;
|
button_ptr = new RenderButton;
|
||||||
renderer_.reset(button_ptr);
|
renderer_.reset(button_ptr);
|
||||||
@ -637,8 +587,7 @@ void InsetExternal::setParams(InsetExternalParams const & p,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
} case RENDERGRAPHIC: {
|
} case RENDERGRAPHIC: {
|
||||||
RenderGraphic * graphic_ptr =
|
RenderGraphic * graphic_ptr = renderer_->asGraphic();
|
||||||
dynamic_cast<RenderGraphic *>(renderer_.get());
|
|
||||||
if (!graphic_ptr) {
|
if (!graphic_ptr) {
|
||||||
graphic_ptr = new RenderGraphic;
|
graphic_ptr = new RenderGraphic;
|
||||||
graphic_ptr->connect(
|
graphic_ptr->connect(
|
||||||
@ -649,9 +598,10 @@ void InsetExternal::setParams(InsetExternalParams const & p,
|
|||||||
graphic_ptr->update(get_grfx_params(params_));
|
graphic_ptr->update(get_grfx_params(params_));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
} case RENDERPREVIEW: {
|
} case RENDERPREVIEW: {
|
||||||
RenderMonitoredPreview * preview_ptr =
|
RenderMonitoredPreview * preview_ptr =
|
||||||
dynamic_cast<RenderMonitoredPreview *>(renderer_.get());
|
renderer_->asMonitoredPreview();
|
||||||
if (!preview_ptr) {
|
if (!preview_ptr) {
|
||||||
preview_ptr = new RenderMonitoredPreview;
|
preview_ptr = new RenderMonitoredPreview;
|
||||||
preview_ptr->connect(
|
preview_ptr->connect(
|
||||||
@ -677,12 +627,13 @@ void InsetExternal::fileChanged() const
|
|||||||
if (!buffer_ptr)
|
if (!buffer_ptr)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
RenderMonitoredPreview & renderer = previewRenderer(renderer_);
|
RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
|
||||||
|
BOOST_ASSERT(ptr);
|
||||||
|
|
||||||
Buffer const & buffer = *buffer_ptr;
|
Buffer const & buffer = *buffer_ptr;
|
||||||
renderer.removePreview(buffer);
|
ptr->removePreview(buffer);
|
||||||
add_preview(renderer, *this, buffer);
|
add_preview(*ptr, *this, buffer);
|
||||||
renderer.startLoading(buffer);
|
ptr->startLoading(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -819,15 +770,14 @@ void add_preview(RenderMonitoredPreview & renderer, InsetExternal const & inset,
|
|||||||
|
|
||||||
void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const
|
void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const
|
||||||
{
|
{
|
||||||
if (getRenderType(params_) != RENDERPREVIEW)
|
RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
|
||||||
|
if (!ptr)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
RenderMonitoredPreview & renderer = previewRenderer(renderer_);
|
|
||||||
|
|
||||||
if (preview_wanted(params())) {
|
if (preview_wanted(params())) {
|
||||||
renderer.setAbsFile(params_.filename.absFilename());
|
ptr->setAbsFile(params_.filename.absFilename());
|
||||||
string const snippet = latex_string(*this, ploader.buffer());
|
string const snippet = latex_string(*this, ploader.buffer());
|
||||||
renderer.addPreview(snippet, ploader);
|
ptr->addPreview(snippet, ploader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,10 @@
|
|||||||
class MetricsInfo;
|
class MetricsInfo;
|
||||||
class PainterInfo;
|
class PainterInfo;
|
||||||
|
|
||||||
|
class RenderButton;
|
||||||
|
class RenderGraphic;
|
||||||
|
class RenderPreview;
|
||||||
|
class RenderMonitoredPreview;
|
||||||
|
|
||||||
class RenderBase {
|
class RenderBase {
|
||||||
public:
|
public:
|
||||||
@ -29,6 +33,12 @@ public:
|
|||||||
/// draw inset and update (xo, yo)-cache
|
/// draw inset and update (xo, yo)-cache
|
||||||
virtual void draw(PainterInfo & pi, int x, int y) const = 0;
|
virtual void draw(PainterInfo & pi, int x, int y) const = 0;
|
||||||
|
|
||||||
|
/// equivalent to dynamic_cast
|
||||||
|
virtual RenderButton * asButton() { return 0; }
|
||||||
|
virtual RenderGraphic * asGraphic() { return 0; }
|
||||||
|
virtual RenderPreview * asPreview() { return 0; }
|
||||||
|
virtual RenderMonitoredPreview * asMonitoredPreview() { return 0; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
RenderBase() {}
|
RenderBase() {}
|
||||||
RenderBase(RenderBase const &) {}
|
RenderBase(RenderBase const &) {}
|
||||||
|
@ -37,6 +37,9 @@ public:
|
|||||||
///
|
///
|
||||||
void setBox(Box b) { button_box_ = b; }
|
void setBox(Box b) { button_box_ = b; }
|
||||||
|
|
||||||
|
/// equivalent to dynamic_cast
|
||||||
|
virtual RenderButton * asButton() { return this; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// The stored data.
|
/// The stored data.
|
||||||
std::string text_;
|
std::string text_;
|
||||||
|
@ -44,6 +44,9 @@ public:
|
|||||||
typedef boost::signal0<void>::slot_type slot_type;
|
typedef boost::signal0<void>::slot_type slot_type;
|
||||||
boost::signals::connection connect(slot_type const &) const;
|
boost::signals::connection connect(slot_type const &) const;
|
||||||
|
|
||||||
|
/// equivalent to dynamic_cast
|
||||||
|
virtual RenderGraphic * asGraphic() { return this; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Not implemented.
|
/// Not implemented.
|
||||||
RenderGraphic & operator=(RenderGraphic const &);
|
RenderGraphic & operator=(RenderGraphic const &);
|
||||||
|
@ -79,6 +79,9 @@ public:
|
|||||||
typedef boost::signal0<void>::slot_type slot_type;
|
typedef boost::signal0<void>::slot_type slot_type;
|
||||||
boost::signals::connection connect(slot_type const &);
|
boost::signals::connection connect(slot_type const &);
|
||||||
|
|
||||||
|
/// equivalent to dynamic_cast
|
||||||
|
virtual RenderPreview * asPreview() { return this; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Not implemented.
|
/// Not implemented.
|
||||||
void operator=(RenderPreview const &);
|
void operator=(RenderPreview const &);
|
||||||
@ -117,6 +120,9 @@ public:
|
|||||||
/// Connect and you'll be informed when the file changes.
|
/// Connect and you'll be informed when the file changes.
|
||||||
boost::signals::connection fileChanged(slot_type const &);
|
boost::signals::connection fileChanged(slot_type const &);
|
||||||
|
|
||||||
|
/// equivalent to dynamic_cast
|
||||||
|
virtual RenderMonitoredPreview * asMonitoredPreview() { return this; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
///
|
///
|
||||||
mutable lyx::support::FileMonitor monitor_;
|
mutable lyx::support::FileMonitor monitor_;
|
||||||
|
Loading…
Reference in New Issue
Block a user