From 7e0e2dbb822fd5d2764300af876386bea8716fc0 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Tue, 22 Jul 2003 20:42:40 +0000 Subject: [PATCH] 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 --- src/frontends/controllers/ChangeLog | 5 ++ src/frontends/controllers/ControlGraphics.C | 4 -- src/frontends/qt2/ChangeLog | 6 ++ src/frontends/qt2/QGraphics.C | 10 +++- src/frontends/xforms/ChangeLog | 6 ++ src/frontends/xforms/FormGraphics.C | 10 +++- src/insets/ChangeLog | 8 +++ src/insets/insetgraphics.C | 17 +++--- src/insets/insetgraphicsParams.C | 8 +-- src/insets/insetgraphicsParams.h | 3 +- src/support/Makefile.am | 2 + src/support/filename.C | 65 +++++++++++++++++++++ src/support/filename.h | 54 +++++++++++++++++ 13 files changed, 176 insertions(+), 22 deletions(-) create mode 100644 src/support/filename.C create mode 100644 src/support/filename.h diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index 68cf1bb39c..82db2ce373 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,8 @@ +2003-07-21 Angus Leeming + + * ControlGraphics.C (initialiseParams, dispatchParams): no need + to modify the filename for reasons of 'good UI' anymore. + 2003-07-21 Angus Leeming * ControlGraphics.C (initialiseParams, dispatchParams): pass a diff --git a/src/frontends/controllers/ControlGraphics.C b/src/frontends/controllers/ControlGraphics.C index 05fca1d771..1018db7954 100644 --- a/src/frontends/controllers/ControlGraphics.C +++ b/src/frontends/controllers/ControlGraphics.C @@ -54,8 +54,6 @@ bool ControlGraphics::initialiseParams(string const & data) InsetGraphicsParams params; InsetGraphicsMailer::string2params(data, bufpath, params); params_.reset(new InsetGraphicsParams(params)); - // make relative for good UI - params_->filename = MakeRelPath(params_->filename, bufpath); return true; } @@ -70,8 +68,6 @@ void ControlGraphics::dispatchParams() { string const buffer_path = kernel().buffer()->filePath(); InsetGraphicsParams tmp_params(params()); - // core requires absolute path during runtime - tmp_params.filename = MakeAbsPath(tmp_params.filename, buffer_path); string const lfun = InsetGraphicsMailer::params2string(tmp_params, buffer_path); kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun)); diff --git a/src/frontends/qt2/ChangeLog b/src/frontends/qt2/ChangeLog index 236ec7543b..467838439d 100644 --- a/src/frontends/qt2/ChangeLog +++ b/src/frontends/qt2/ChangeLog @@ -1,3 +1,9 @@ +2003-07-21 Angus Leeming + + * 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 * QExternal.C (update_contents, apply): diff --git a/src/frontends/qt2/QGraphics.C b/src/frontends/qt2/QGraphics.C index ea90488b83..49d314be0e 100644 --- a/src/frontends/qt2/QGraphics.C +++ b/src/frontends/qt2/QGraphics.C @@ -21,6 +21,7 @@ #include "support/filetools.h" #include "support/lyxlib.h" #include "insets/insetgraphicsParams.h" +#include "buffer.h" #include "lyxrc.h" #include "lengthcombo.h" #include "qt_helpers.h" @@ -159,11 +160,13 @@ void QGraphics::update_contents() 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 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 dialog_->lbX->setText(toqstr(token(bb, ' ', 0))); dialog_->lbY->setText(toqstr(token(bb, ' ', 1))); @@ -299,7 +302,8 @@ void QGraphics::apply() { InsetGraphicsParams & igp = controller().params(); - igp.filename = fromqstr(dialog_->filename->text()); + igp.filename.set(fromqstr(dialog_->filename->text()), + kernel().buffer()->filePath()); // the bb section igp.bb.erase(); diff --git a/src/frontends/xforms/ChangeLog b/src/frontends/xforms/ChangeLog index d443ea2509..09961a5774 100644 --- a/src/frontends/xforms/ChangeLog +++ b/src/frontends/xforms/ChangeLog @@ -1,3 +1,9 @@ +2003-07-21 Angus Leeming + + * FormGraphics.C (apply, update): + changes due to the use of the FileName class to store the graphics + file name. + 2003-07-21 Angus Leeming * FormExternal.C (update, apply): diff --git a/src/frontends/xforms/FormGraphics.C b/src/frontends/xforms/FormGraphics.C index cb984e7cf8..b884829301 100644 --- a/src/frontends/xforms/FormGraphics.C +++ b/src/frontends/xforms/FormGraphics.C @@ -22,6 +22,7 @@ #include "Tooltips.h" #include "xforms_helpers.h" +#include "buffer.h" #include "debug.h" // for lyxerr #include "lyxrc.h" // for lyxrc.display_graphics @@ -292,7 +293,8 @@ void FormGraphics::apply() InsetGraphicsParams & igp = controller().params(); // 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)); if (igp.lyxscale == 0) { @@ -424,7 +426,9 @@ void FormGraphics::update() { InsetGraphicsParams & igp = controller().params(); // 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()); switch (igp.display) { @@ -479,7 +483,7 @@ void FormGraphics::update() { // the bb section // set the bounding box values, if exists. First we need the whole // 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); // the extra section diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 7ae3509345..22407f3e7e 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,11 @@ +2003-07-21 Angus Leeming + + * 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 * insetgraphics.[Ch] (string2params, params2string): passed a diff --git a/src/insets/insetgraphics.C b/src/insets/insetgraphics.C index cf6917c091..46f49c08ea 100644 --- a/src/insets/insetgraphics.C +++ b/src/insets/insetgraphics.C @@ -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 // filename. - string orig_file = params().filename; - string const rel_file = MakeRelPath(orig_file, buf->filePath()); + string orig_file = params().filename.absFilename(); + string const rel_file = params().filename.relFilename(buf->filePath()); if (!IsFileReadable(orig_file)) { 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. lyxerr[Debug::GRAPHICS] << "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 // we have to test three different cases #warning uh, but can our cache handle it ? no. - string const file_ = params().filename; + string const file_ = params().filename.absFilename(); bool const file_exists = !file_.empty() && (IsFileReadable(file_) || // original @@ -543,7 +544,8 @@ int InsetGraphics::ascii(Buffer const *, ostream & os, int) const // 1. Convert file to ascii using gifscii // 2. Read ascii output file and add it to the output stream. // 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; } @@ -575,7 +577,8 @@ void InsetGraphics::validate(LaTeXFeatures & features) const if (params().filename.empty()) return; - features.includeFile(graphic_label, RemoveExtension(params().filename)); + features.includeFile(graphic_label, + RemoveExtension(params().filename.absFilename())); features.require("graphicx"); diff --git a/src/insets/insetgraphicsParams.C b/src/insets/insetgraphicsParams.C index f9e7f29ce7..60aae033ee 100644 --- a/src/insets/insetgraphicsParams.C +++ b/src/insets/insetgraphicsParams.C @@ -144,7 +144,7 @@ void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const // Do not write the default values if (!filename.empty()) { - os << "\tfilename " << MakeRelPath(filename, bufpath) << '\n'; + os << "\tfilename " << filename.outputFilename(bufpath) << '\n'; } if (lyxscale != 100) os << "\tlyxscale " << lyxscale << '\n'; @@ -189,7 +189,7 @@ bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const { if (token == "filename") { lex.eatLine(); - filename = MakeAbsPath(lex.getString(), bufpath); + filename.set(lex.getString(), bufpath); } else if (token == "lyxscale") { lex.next(); 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 pars; - pars.filename = filename; + pars.filename = filename.absFilename(); pars.scale = lyxscale; pars.angle = rotateAngle; @@ -267,7 +267,7 @@ lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const pars.bb = bb; // 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; if (!tmp.empty()) { unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0)); diff --git a/src/insets/insetgraphicsParams.h b/src/insets/insetgraphicsParams.h index c288c65bc2..8b626a64ec 100644 --- a/src/insets/insetgraphicsParams.h +++ b/src/insets/insetgraphicsParams.h @@ -17,6 +17,7 @@ #include "graphics/GraphicsTypes.h" #include "LString.h" #include "lyxlength.h" +#include "support/filename.h" class LyXLex; @@ -31,7 +32,7 @@ namespace graphics { struct InsetGraphicsParams { /// Image filename. - string filename; + lyx::support::FileName filename; /// Scaling the Screen inside Lyx unsigned int lyxscale; /// How to display the image inside LyX diff --git a/src/support/Makefile.am b/src/support/Makefile.am index 2431c2f8b5..245610485b 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -30,6 +30,8 @@ libsupport_la_SOURCES = \ boost-inst.C \ chdir.C \ copy.C \ + filename.C \ + filename.h \ filetools.C \ filetools.h \ forkedcall.C \ diff --git a/src/support/filename.C b/src/support/filename.C new file mode 100644 index 0000000000..cd4d475282 --- /dev/null +++ b/src/support/filename.C @@ -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 + +#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 diff --git a/src/support/filename.h b/src/support/filename.h new file mode 100644 index 0000000000..8f406755a2 --- /dev/null +++ b/src/support/filename.h @@ -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