Enable to specify the target format for converting the local layout

* New constant LYXFILE_LAYOUT_FORMAT in src/TextClass.cpp. This determines the
  layout format corresponding to the current lyx file format.

* The Local Layout pane is changed so that the "Convert" button does not convert
  to the internal layout format but to the current lyx file format, to make sure
  that the file does not become unreadable with a specified earlier version of
  LyX.

* If LYXFILE_LAYOUT_FORMAT == LAYOUT_FORMAT then LyX behaves as before. This is
  the value defined in master.
This commit is contained in:
Guillaume Munch 2016-07-23 15:29:40 +01:00
parent 77511ea1d5
commit a584c8176d
4 changed files with 61 additions and 31 deletions

View File

@ -63,6 +63,12 @@ namespace lyx {
// //
int const LAYOUT_FORMAT = 60; //lasgouttes LongTableNoNumber => Unnumbered int const LAYOUT_FORMAT = 60; //lasgouttes LongTableNoNumber => Unnumbered
// Layout format for the current lyx file format. Controls which format is
// targeted by Local Layout > Convert. In master, equal to LAYOUT_FORMAT.
int const LYXFILE_LAYOUT_FORMAT = LAYOUT_FORMAT;
namespace { namespace {
class LayoutNamesEqual : public unary_function<Layout, bool> { class LayoutNamesEqual : public unary_function<Layout, bool> {
@ -79,7 +85,8 @@ private:
}; };
bool layout2layout(FileName const & filename, FileName const & tempfile) bool layout2layout(FileName const & filename, FileName const & tempfile,
int const format = LAYOUT_FORMAT)
{ {
FileName const script = libFileSearch("scripts", "layout2layout.py"); FileName const script = libFileSearch("scripts", "layout2layout.py");
if (script.empty()) { if (script.empty()) {
@ -90,6 +97,7 @@ bool layout2layout(FileName const & filename, FileName const & tempfile)
ostringstream command; ostringstream command;
command << os::python() << ' ' << quoteName(script.toFilesystemEncoding()) command << os::python() << ' ' << quoteName(script.toFilesystemEncoding())
<< " -t " << format
<< ' ' << quoteName(filename.toFilesystemEncoding()) << ' ' << quoteName(filename.toFilesystemEncoding())
<< ' ' << quoteName(tempfile.toFilesystemEncoding()); << ' ' << quoteName(tempfile.toFilesystemEncoding());
string const command_str = command.str(); string const command_str = command.str();
@ -98,7 +106,8 @@ bool layout2layout(FileName const & filename, FileName const & tempfile)
cmd_ret const ret = runCommand(command_str); cmd_ret const ret = runCommand(command_str);
if (ret.first != 0) { if (ret.first != 0) {
LYXERR0("Could not run layout conversion script layout2layout.py."); if (format == LAYOUT_FORMAT)
LYXERR0("Conversion of layout with layout2layout.py has failed.");
return false; return false;
} }
return true; return true;
@ -290,7 +299,7 @@ std::string TextClass::convert(std::string const & str)
os.close(); os.close();
TempFile tmp2("convert_localXXXXXX.layout"); TempFile tmp2("convert_localXXXXXX.layout");
FileName const tempfile = tmp2.name(); FileName const tempfile = tmp2.name();
bool success = layout2layout(fn, tempfile); bool success = layout2layout(fn, tempfile, LYXFILE_LAYOUT_FORMAT);
if (!success) if (!success)
return ""; return "";
ifstream is(tempfile.toFilesystemEncoding().c_str()); ifstream is(tempfile.toFilesystemEncoding().c_str());
@ -375,13 +384,13 @@ TextClass::ReturnValues TextClass::read(std::string const & str, ReadType rt)
os << str; os << str;
os.close(); os.close();
// now try to convert it // now try to convert it to LAYOUT_FORMAT
bool const worx = convertLayoutFormat(tempfile, rt); if (!convertLayoutFormat(tempfile, rt)) {
if (!worx) {
LYXERR0("Unable to convert internal layout information to format " LYXERR0("Unable to convert internal layout information to format "
<< LAYOUT_FORMAT); << LAYOUT_FORMAT);
return ERROR; return ERROR;
} }
return OK_OLDFORMAT; return OK_OLDFORMAT;
} }

View File

@ -171,7 +171,8 @@ public:
ReturnValues read(Lexer & lex, ReadType rt = BASECLASS); ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
/// validates the layout information passed in str /// validates the layout information passed in str
static ReturnValues validate(std::string const & str); static ReturnValues validate(std::string const & str);
/// /// \return the conversion of \param str to the latest layout format
/// compatible with the lyx format.
static std::string convert(std::string const & str); static std::string convert(std::string const & str);
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
@ -514,6 +515,9 @@ std::ostream & operator<<(std::ostream & os, PageSides p);
/// current format of layout files /// current format of layout files
extern int const LAYOUT_FORMAT; extern int const LAYOUT_FORMAT;
/// layout format for the current lyx file format (usually equal to
/// LAYOUT_FORMAT)
extern int const LYXFILE_LAYOUT_FORMAT;
} // namespace lyx } // namespace lyx

View File

@ -545,6 +545,15 @@ void LocalLayout::apply(BufferParams & params)
} }
void LocalLayout::hideConvert()
{
convertPB->setEnabled(false);
convertLB->setText("");
convertPB->hide();
convertLB->hide();
}
void LocalLayout::textChanged() void LocalLayout::textChanged()
{ {
static const QString message = static const QString message =
@ -556,15 +565,14 @@ void LocalLayout::textChanged()
validated_ = true; validated_ = true;
validatePB->setEnabled(false); validatePB->setEnabled(false);
validLB->setText(""); validLB->setText("");
convertPB->hide(); hideConvert();
convertLB->hide();
changed(); changed();
} else if (!validatePB->isEnabled()) { } else if (!validatePB->isEnabled()) {
// if that's already enabled, we shouldn't need to do anything. // if that's already enabled, we shouldn't need to do anything.
validated_ = false; validated_ = false;
validLB->setText(message); validLB->setText(message);
validatePB->setEnabled(true); validatePB->setEnabled(true);
convertPB->setEnabled(false); hideConvert();
changed(); changed();
} }
} }
@ -574,44 +582,52 @@ void LocalLayout::convert() {
string const layout = string const layout =
fromqstr(locallayoutTE->document()->toPlainText().trimmed()); fromqstr(locallayoutTE->document()->toPlainText().trimmed());
string const newlayout = TextClass::convert(layout); string const newlayout = TextClass::convert(layout);
LYXERR0(newlayout); if (!newlayout.empty())
if (newlayout.empty()) {
Alert::error(_("Conversion Failed!"),
_("Failed to convert local layout to current format."));
} else {
locallayoutTE->setPlainText(toqstr(newlayout)); locallayoutTE->setPlainText(toqstr(newlayout));
}
validate(); validate();
} }
void LocalLayout::convertPressed() { void LocalLayout::convertPressed() {
convert(); convert();
hideConvert();
changed(); changed();
} }
void LocalLayout::validate() { void LocalLayout::validate() {
static const QString valid = qt_("Layout is valid!"); // Bold text
static const QString vtext = static const QString vpar("<p style=\"font-weight: bold;\">%1</p>");
toqstr("<p style=\"font-weight: bold; \">") // Flashy red bold text
+ valid + toqstr("</p>"); static const QString ivpar("<p style=\"color: #c00000; font-weight: bold; \">"
static const QString invalid = qt_("Layout is invalid!"); "%1</p>");
static const QString ivtext =
toqstr("<p style=\"color: #c00000; font-weight: bold; \">")
+ invalid + toqstr("</p>");
string const layout = string const layout =
fromqstr(locallayoutTE->document()->toPlainText().trimmed()); fromqstr(locallayoutTE->document()->toPlainText().trimmed());
if (!layout.empty()) { if (!layout.empty()) {
TextClass::ReturnValues const ret = TextClass::validate(layout); TextClass::ReturnValues const ret = TextClass::validate(layout);
validated_ = (ret == TextClass::OK) || (ret == TextClass::OK_OLDFORMAT); validated_ = (ret == TextClass::OK) || (ret == TextClass::OK_OLDFORMAT);
validatePB->setEnabled(false); validatePB->setEnabled(false);
validLB->setText(validated_ ? vtext : ivtext); validLB->setText(validated_ ? vpar.arg(qt_("Layout is valid!"))
: ivpar.arg(qt_("Layout is invalid!")));
if (ret == TextClass::OK_OLDFORMAT) { if (ret == TextClass::OK_OLDFORMAT) {
convertPB->show(); convertPB->show();
// Testing conversion to LYXFILE_LAYOUT_FORMAT at this point
// already.
if (TextClass::convert(layout).empty()) {
// Conversion failed. If LAYOUT_FORMAT > LYXFILE_LAYOUT_FORMAT,
// then maybe the layout is still valid, but its format is more
// recent than LYXFILE_LAYOUT_FORMAT. However, if LAYOUT_FORMAT
// == LYXFILE_LAYOUT_FORMAT then something is definitely wrong.
convertPB->setEnabled(false);
const QString text = (LAYOUT_FORMAT == LYXFILE_LAYOUT_FORMAT)
? ivpar.arg(qt_("Conversion to current format impossible!"))
: vpar.arg(qt_("Conversion to current stable format "
"impossible."));
convertLB->setText(text);
} else {
convertPB->setEnabled(true); convertPB->setEnabled(true);
convertLB->setText(qt_("Convert to current format")); convertLB->setText(qt_("Convert to current format"));
}
convertLB->show(); convertLB->show();
} else { } else {
convertPB->hide(); convertPB->hide();

View File

@ -322,6 +322,7 @@ Q_SIGNALS:
private: private:
void validate(); void validate();
void convert(); void convert();
void hideConvert();
private Q_SLOTS: private Q_SLOTS:
void textChanged(); void textChanged();
void validatePressed(); void validatePressed();