A new FileName class + use by the graphics inset.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7348 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Angus Leeming 2003-07-22 20:42:40 +00:00
parent 42658b7748
commit 7e0e2dbb82
13 changed files with 176 additions and 22 deletions

View File

@ -1,3 +1,8 @@
2003-07-21 Angus Leeming <leeming@lyx.org>
* ControlGraphics.C (initialiseParams, dispatchParams): no need
to modify the filename for reasons of 'good UI' anymore.
2003-07-21 Angus Leeming <leeming@lyx.org> 2003-07-21 Angus Leeming <leeming@lyx.org>
* ControlGraphics.C (initialiseParams, dispatchParams): pass a * ControlGraphics.C (initialiseParams, dispatchParams): pass a

View File

@ -54,8 +54,6 @@ bool ControlGraphics::initialiseParams(string const & data)
InsetGraphicsParams params; InsetGraphicsParams params;
InsetGraphicsMailer::string2params(data, bufpath, params); InsetGraphicsMailer::string2params(data, bufpath, params);
params_.reset(new InsetGraphicsParams(params)); params_.reset(new InsetGraphicsParams(params));
// make relative for good UI
params_->filename = MakeRelPath(params_->filename, bufpath);
return true; return true;
} }
@ -70,8 +68,6 @@ void ControlGraphics::dispatchParams()
{ {
string const buffer_path = kernel().buffer()->filePath(); string const buffer_path = kernel().buffer()->filePath();
InsetGraphicsParams tmp_params(params()); InsetGraphicsParams tmp_params(params());
// core requires absolute path during runtime
tmp_params.filename = MakeAbsPath(tmp_params.filename, buffer_path);
string const lfun = string const lfun =
InsetGraphicsMailer::params2string(tmp_params, buffer_path); InsetGraphicsMailer::params2string(tmp_params, buffer_path);
kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun)); kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));

View File

@ -1,3 +1,9 @@
2003-07-21 Angus Leeming <leeming@lyx.org>
* QGraphics.C (apply, update_contents):
changes due to the use of the FileName class to store the graphics
file name.
2003-07-21 Angus Leeming <leeming@lyx.org> 2003-07-21 Angus Leeming <leeming@lyx.org>
* QExternal.C (update_contents, apply): * QExternal.C (update_contents, apply):

View File

@ -21,6 +21,7 @@
#include "support/filetools.h" #include "support/filetools.h"
#include "support/lyxlib.h" #include "support/lyxlib.h"
#include "insets/insetgraphicsParams.h" #include "insets/insetgraphicsParams.h"
#include "buffer.h"
#include "lyxrc.h" #include "lyxrc.h"
#include "lengthcombo.h" #include "lengthcombo.h"
#include "qt_helpers.h" #include "qt_helpers.h"
@ -159,11 +160,13 @@ void QGraphics::update_contents()
break; break;
} }
dialog_->filename->setText(toqstr(igp.filename)); string const name =
igp.filename.outputFilename(kernel().buffer()->filePath());
dialog_->filename->setText(toqstr(name));
// set the bounding box values // set the bounding box values
if (igp.bb.empty()) { if (igp.bb.empty()) {
string const bb = controller().readBB(igp.filename); string const bb = controller().readBB(igp.filename.absFilename());
// the values from the file always have the bigpoint-unit bp // the values from the file always have the bigpoint-unit bp
dialog_->lbX->setText(toqstr(token(bb, ' ', 0))); dialog_->lbX->setText(toqstr(token(bb, ' ', 0)));
dialog_->lbY->setText(toqstr(token(bb, ' ', 1))); dialog_->lbY->setText(toqstr(token(bb, ' ', 1)));
@ -299,7 +302,8 @@ void QGraphics::apply()
{ {
InsetGraphicsParams & igp = controller().params(); InsetGraphicsParams & igp = controller().params();
igp.filename = fromqstr(dialog_->filename->text()); igp.filename.set(fromqstr(dialog_->filename->text()),
kernel().buffer()->filePath());
// the bb section // the bb section
igp.bb.erase(); igp.bb.erase();

View File

@ -1,3 +1,9 @@
2003-07-21 Angus Leeming <leeming@lyx.org>
* FormGraphics.C (apply, update):
changes due to the use of the FileName class to store the graphics
file name.
2003-07-21 Angus Leeming <leeming@lyx.org> 2003-07-21 Angus Leeming <leeming@lyx.org>
* FormExternal.C (update, apply): * FormExternal.C (update, apply):

View File

@ -22,6 +22,7 @@
#include "Tooltips.h" #include "Tooltips.h"
#include "xforms_helpers.h" #include "xforms_helpers.h"
#include "buffer.h"
#include "debug.h" // for lyxerr #include "debug.h" // for lyxerr
#include "lyxrc.h" // for lyxrc.display_graphics #include "lyxrc.h" // for lyxrc.display_graphics
@ -292,7 +293,8 @@ void FormGraphics::apply()
InsetGraphicsParams & igp = controller().params(); InsetGraphicsParams & igp = controller().params();
// the file section // the file section
igp.filename = getString(file_->input_filename); igp.filename.set(getString(file_->input_filename),
kernel().buffer()->filePath());
igp.lyxscale = strToInt(getString(file_->input_lyxscale)); igp.lyxscale = strToInt(getString(file_->input_lyxscale));
if (igp.lyxscale == 0) { if (igp.lyxscale == 0) {
@ -424,7 +426,9 @@ void FormGraphics::update() {
InsetGraphicsParams & igp = controller().params(); InsetGraphicsParams & igp = controller().params();
// the file section // the file section
fl_set_input(file_->input_filename, igp.filename.c_str()); string const name =
igp.filename.outputFilename(kernel().buffer()->filePath());
fl_set_input(file_->input_filename, name.c_str());
fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str()); fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
switch (igp.display) { switch (igp.display) {
@ -479,7 +483,7 @@ void FormGraphics::update() {
// the bb section // the bb section
// set the bounding box values, if exists. First we need the whole // set the bounding box values, if exists. First we need the whole
// path, because the controller knows nothing about the doc-dir // path, because the controller knows nothing about the doc-dir
updateBB(igp.filename, igp.bb); updateBB(igp.filename.absFilename(), igp.bb);
fl_set_button(bbox_->check_clip, igp.clip); fl_set_button(bbox_->check_clip, igp.clip);
// the extra section // the extra section

View File

@ -1,3 +1,11 @@
2003-07-21 Angus Leeming <leeming@lyx.org>
* insetgraphicsParams.[Ch]: store the graphics file name in
a FileName member variable rather than a string.
* insetgraphics.[Ch]:
* insetgraphicsParams.[Ch]: Associated changes.
2003-07-21 Angus Leeming <leeming@lyx.org> 2003-07-21 Angus Leeming <leeming@lyx.org>
* insetgraphics.[Ch] (string2params, params2string): passed a * insetgraphics.[Ch] (string2params, params2string): passed a

View File

@ -331,8 +331,8 @@ string const InsetGraphics::prepareFile(Buffer const * buf,
{ {
// LaTeX can cope if the graphics file doesn't exist, so just return the // LaTeX can cope if the graphics file doesn't exist, so just return the
// filename. // filename.
string orig_file = params().filename; string orig_file = params().filename.absFilename();
string const rel_file = MakeRelPath(orig_file, buf->filePath()); string const rel_file = params().filename.relFilename(buf->filePath());
if (!IsFileReadable(orig_file)) { if (!IsFileReadable(orig_file)) {
lyxerr[Debug::GRAPHICS] lyxerr[Debug::GRAPHICS]
@ -466,14 +466,15 @@ int InsetGraphics::latex(Buffer const * buf, ostream & os,
// just output a message about it in the latex output. // just output a message about it in the latex output.
lyxerr[Debug::GRAPHICS] lyxerr[Debug::GRAPHICS]
<< "insetgraphics::latex: Filename = " << "insetgraphics::latex: Filename = "
<< params().filename << endl; << params().filename.absFilename() << endl;
string const relative_file = MakeRelPath(params().filename, buf->filePath()); string const relative_file =
params().filename.relFilename(buf->filePath());
// A missing (e)ps-extension is no problem for LaTeX, so // A missing (e)ps-extension is no problem for LaTeX, so
// we have to test three different cases // we have to test three different cases
#warning uh, but can our cache handle it ? no. #warning uh, but can our cache handle it ? no.
string const file_ = params().filename; string const file_ = params().filename.absFilename();
bool const file_exists = bool const file_exists =
!file_.empty() && !file_.empty() &&
(IsFileReadable(file_) || // original (IsFileReadable(file_) || // original
@ -543,7 +544,8 @@ int InsetGraphics::ascii(Buffer const *, ostream & os, int) const
// 1. Convert file to ascii using gifscii // 1. Convert file to ascii using gifscii
// 2. Read ascii output file and add it to the output stream. // 2. Read ascii output file and add it to the output stream.
// at least we send the filename // at least we send the filename
os << '<' << bformat(_("Graphics file: %1$s"), params().filename) << ">\n"; os << '<' << bformat(_("Graphics file: %1$s"),
params().filename.absFilename()) << ">\n";
return 0; return 0;
} }
@ -575,7 +577,8 @@ void InsetGraphics::validate(LaTeXFeatures & features) const
if (params().filename.empty()) if (params().filename.empty())
return; return;
features.includeFile(graphic_label, RemoveExtension(params().filename)); features.includeFile(graphic_label,
RemoveExtension(params().filename.absFilename()));
features.require("graphicx"); features.require("graphicx");

View File

@ -144,7 +144,7 @@ void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
// Do not write the default values // Do not write the default values
if (!filename.empty()) { if (!filename.empty()) {
os << "\tfilename " << MakeRelPath(filename, bufpath) << '\n'; os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
} }
if (lyxscale != 100) if (lyxscale != 100)
os << "\tlyxscale " << lyxscale << '\n'; os << "\tlyxscale " << lyxscale << '\n';
@ -189,7 +189,7 @@ bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const
{ {
if (token == "filename") { if (token == "filename") {
lex.eatLine(); lex.eatLine();
filename = MakeAbsPath(lex.getString(), bufpath); filename.set(lex.getString(), bufpath);
} else if (token == "lyxscale") { } else if (token == "lyxscale") {
lex.next(); lex.next();
lyxscale = lex.getInteger(); lyxscale = lex.getInteger();
@ -259,7 +259,7 @@ bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const
lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const
{ {
lyx::graphics::Params pars; lyx::graphics::Params pars;
pars.filename = filename; pars.filename = filename.absFilename();
pars.scale = lyxscale; pars.scale = lyxscale;
pars.angle = rotateAngle; pars.angle = rotateAngle;
@ -267,7 +267,7 @@ lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const
pars.bb = bb; pars.bb = bb;
// Get the original Bounding Box from the file // Get the original Bounding Box from the file
string const tmp = readBB_from_PSFile(filename); string const tmp = readBB_from_PSFile(filename.absFilename());
lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl; lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
if (!tmp.empty()) { if (!tmp.empty()) {
unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0)); unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));

View File

@ -17,6 +17,7 @@
#include "graphics/GraphicsTypes.h" #include "graphics/GraphicsTypes.h"
#include "LString.h" #include "LString.h"
#include "lyxlength.h" #include "lyxlength.h"
#include "support/filename.h"
class LyXLex; class LyXLex;
@ -31,7 +32,7 @@ namespace graphics {
struct InsetGraphicsParams struct InsetGraphicsParams
{ {
/// Image filename. /// Image filename.
string filename; lyx::support::FileName filename;
/// Scaling the Screen inside Lyx /// Scaling the Screen inside Lyx
unsigned int lyxscale; unsigned int lyxscale;
/// How to display the image inside LyX /// How to display the image inside LyX

View File

@ -30,6 +30,8 @@ libsupport_la_SOURCES = \
boost-inst.C \ boost-inst.C \
chdir.C \ chdir.C \
copy.C \ copy.C \
filename.C \
filename.h \
filetools.C \ filetools.C \
filetools.h \ filetools.h \
forkedcall.C \ forkedcall.C \

65
src/support/filename.C Normal file
View File

@ -0,0 +1,65 @@
/**
* \file filename.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Angus Leeming
*
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*/
#include <config.h>
#include "filename.h"
#include "support/filetools.h"
namespace lyx {
namespace support {
FileName::FileName()
: save_abs_path_(true)
{}
void FileName::set(string const & name, string const & buffer_path)
{
save_abs_path_ = AbsolutePath(name);
name_ = save_abs_path_ ? name : MakeAbsPath(name, buffer_path);
}
void FileName::erase()
{
name_.erase();
}
string const FileName::relFilename(string const & path) const
{
return MakeRelPath(name_, path);
}
string const FileName::outputFilename(string const & buffer_path) const
{
return save_abs_path_ ? name_ : MakeRelPath(name_, buffer_path);
}
bool operator==(FileName const & lhs, FileName const & rhs)
{
return lhs.absFilename() == rhs.absFilename() &&
lhs.saveAbsPath() == rhs.saveAbsPath();
}
bool operator!=(FileName const & lhs, FileName const & rhs)
{
return !(lhs == rhs);
}
} //namespace support
} // namespace lyx

54
src/support/filename.h Normal file
View File

@ -0,0 +1,54 @@
// -*- C++-*-
/**
* \file filename.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Angus Leeming
*
* Full author contact details are available in file CREDITS
*/
#ifndef FILENAME_H
#define FILENAME_H
#include "LString.h"
namespace lyx {
namespace support {
class FileName {
public:
FileName();
/** \param filename the file in question. May have either a relative
or an absolute path.
\param buffer_path if \c filename has a relative path, generate
the absolute path using this.
*/
void set(string const & filename, string const & buffer_path);
void erase();
bool empty() const { return name_.empty(); }
bool saveAbsPath() const { return save_abs_path_; }
string const absFilename() const { return name_; }
string const relFilename(string const & buffer_path) const;
string const outputFilename(string const & buffer_path) const;
private:
string name_;
bool save_abs_path_;
};
bool operator==(FileName const &, FileName const &);
bool operator!=(FileName const &, FileName const &);
} // namespace support
} // namespace lyx
#endif