commit a584c8176d409f9154995eb6829d2438302e1dd8
Author: Guillaume Munch <[email protected]>
Date:   Sat Jul 23 15:29:40 2016 +0100

    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.
---
 src/TextClass.cpp                 |   27 +++++++++++------
 src/TextClass.h                   |    6 +++-
 src/frontends/qt4/GuiDocument.cpp |   58 +++++++++++++++++++++++-------------
 src/frontends/qt4/GuiDocument.h   |    1 +
 4 files changed, 61 insertions(+), 31 deletions(-)

diff --git a/src/TextClass.cpp b/src/TextClass.cpp
index b282ba3..a783437 100644
--- a/src/TextClass.cpp
+++ b/src/TextClass.cpp
@@ -63,6 +63,12 @@ namespace lyx {
 //
 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 {
 
 class LayoutNamesEqual : public unary_function<Layout, bool> {
@@ -79,26 +85,29 @@ 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");
        if (script.empty()) {
                LYXERR0("Could not find layout conversion "
-                         "script layout2layout.py.");
+                       "script layout2layout.py.");
                return false;
        }
 
        ostringstream command;
        command << os::python() << ' ' << 
quoteName(script.toFilesystemEncoding())
-               << ' ' << quoteName(filename.toFilesystemEncoding())
-               << ' ' << quoteName(tempfile.toFilesystemEncoding());
+               << " -t " << format
+               << ' ' << quoteName(filename.toFilesystemEncoding())
+               << ' ' << quoteName(tempfile.toFilesystemEncoding());
        string const command_str = command.str();
 
        LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
 
        cmd_ret const ret = runCommand(command_str);
        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 true;
@@ -290,7 +299,7 @@ std::string TextClass::convert(std::string const & str)
        os.close();
        TempFile tmp2("convert_localXXXXXX.layout");
        FileName const tempfile = tmp2.name();
-       bool success = layout2layout(fn, tempfile);
+       bool success = layout2layout(fn, tempfile, LYXFILE_LAYOUT_FORMAT);
        if (!success)
                return "";
        ifstream is(tempfile.toFilesystemEncoding().c_str());
@@ -375,13 +384,13 @@ TextClass::ReturnValues TextClass::read(std::string const 
& str, ReadType rt)
        os << str;
        os.close();
 
-       // now try to convert it
-       bool const worx = convertLayoutFormat(tempfile, rt);
-       if (!worx) {
+       // now try to convert it to LAYOUT_FORMAT
+       if (!convertLayoutFormat(tempfile, rt)) {
                LYXERR0("Unable to convert internal layout information to 
format "
                        << LAYOUT_FORMAT);
                return ERROR;
        }
+
        return OK_OLDFORMAT;
 }
 
diff --git a/src/TextClass.h b/src/TextClass.h
index 88f9303..22d426c 100644
--- a/src/TextClass.h
+++ b/src/TextClass.h
@@ -171,7 +171,8 @@ public:
        ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
        /// validates the layout information passed in 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);
 
        ///////////////////////////////////////////////////////////////////
@@ -514,6 +515,9 @@ std::ostream & operator<<(std::ostream & os, PageSides p);
 
 /// current format of layout files
 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
diff --git a/src/frontends/qt4/GuiDocument.cpp 
b/src/frontends/qt4/GuiDocument.cpp
index 3faa902..e116001 100644
--- a/src/frontends/qt4/GuiDocument.cpp
+++ b/src/frontends/qt4/GuiDocument.cpp
@@ -545,6 +545,15 @@ void LocalLayout::apply(BufferParams & params)
 }
 
 
+void LocalLayout::hideConvert()
+{
+       convertPB->setEnabled(false);
+       convertLB->setText("");
+       convertPB->hide();
+       convertLB->hide();
+}
+
+
 void LocalLayout::textChanged()
 {
        static const QString message =
@@ -556,15 +565,14 @@ void LocalLayout::textChanged()
                validated_ = true;
                validatePB->setEnabled(false);
                validLB->setText("");
-               convertPB->hide();
-               convertLB->hide();
+               hideConvert();
                changed();
        } else if (!validatePB->isEnabled()) {
                // if that's already enabled, we shouldn't need to do anything.
                validated_ = false;
                validLB->setText(message);
                validatePB->setEnabled(true);
-               convertPB->setEnabled(false);
+               hideConvert();
                changed();
        }
 }
@@ -574,44 +582,52 @@ void LocalLayout::convert() {
        string const layout =
                fromqstr(locallayoutTE->document()->toPlainText().trimmed());
        string const newlayout = TextClass::convert(layout);
-       LYXERR0(newlayout);
-       if (newlayout.empty()) {
-               Alert::error(_("Conversion Failed!"),
-                     _("Failed to convert local layout to current format."));
-       } else {
+       if (!newlayout.empty())
                locallayoutTE->setPlainText(toqstr(newlayout));
-       }
        validate();
 }
 
 
 void LocalLayout::convertPressed() {
        convert();
+       hideConvert();
        changed();
 }
 
 
 void LocalLayout::validate() {
-       static const QString valid = qt_("Layout is valid!");
-       static const QString vtext =
-               toqstr("<p style=\"font-weight: bold; \">")
-                 + valid + toqstr("</p>");
-       static const QString invalid = qt_("Layout is invalid!");
-       static const QString ivtext =
-               toqstr("<p style=\"color: #c00000; font-weight: bold; \">")
-                 + invalid + toqstr("</p>");
-
+       // Bold text
+       static const QString vpar("<p style=\"font-weight: bold;\">%1</p>");
+       // Flashy red bold text
+       static const QString ivpar("<p style=\"color: #c00000; font-weight: 
bold; \">"
+                                  "%1</p>");
        string const layout =
                fromqstr(locallayoutTE->document()->toPlainText().trimmed());
        if (!layout.empty()) {
                TextClass::ReturnValues const ret = TextClass::validate(layout);
                validated_ = (ret == TextClass::OK) || (ret == 
TextClass::OK_OLDFORMAT);
                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) {
                        convertPB->show();
-                       convertPB->setEnabled(true);
-                       convertLB->setText(qt_("Convert to current format"));
+                       // 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);
+                               convertLB->setText(qt_("Convert to current 
format"));
+                       }
                        convertLB->show();
                } else {
                        convertPB->hide();
diff --git a/src/frontends/qt4/GuiDocument.h b/src/frontends/qt4/GuiDocument.h
index 097abff..e0d42c9 100644
--- a/src/frontends/qt4/GuiDocument.h
+++ b/src/frontends/qt4/GuiDocument.h
@@ -322,6 +322,7 @@ Q_SIGNALS:
 private:
        void validate();
        void convert();
+       void hideConvert();
 private Q_SLOTS:
        void textChanged();
        void validatePressed();

Reply via email to