diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index 488508555d..c52255a130 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -2026,7 +2026,9 @@ void BufferParams::makeDocumentClass() doc_class_ = &(DocumentClassBundle::get().makeDocumentClass(*baseClass(), layout_modules_)); if (!local_layout.empty()) { - if (!doc_class_->read(local_layout, TextClass::MODULE)) { + TextClass::ReturnValues success = + doc_class_->read(local_layout, TextClass::MODULE); + if (success != TextClass::OK && success != TextClass::OK_OLDFORMAT) { docstring const msg = _("Error reading internal layout information"); frontend::Alert::warning(_("Read Error"), msg); } diff --git a/src/TextClass.cpp b/src/TextClass.cpp index 00558d5862..32976c35d7 100644 --- a/src/TextClass.cpp +++ b/src/TextClass.cpp @@ -268,6 +268,29 @@ bool TextClass::convertLayoutFormat(support::FileName const & filename, ReadType } +std::string TextClass::convert(std::string const & str) +{ + FileName const fn = FileName::tempName("locallayout"); + ofstream os(fn.toFilesystemEncoding().c_str()); + os << str; + os.close(); + FileName const tempfile = FileName::tempName("convert_locallayout"); + bool success = layout2layout(fn, tempfile); + if (!success) + return ""; + ifstream is(tempfile.toFilesystemEncoding().c_str()); + string ret; + string tmp; + while (!is.eof()) { + getline(is, tmp); + ret += tmp + '\n'; + } + is.close(); + tempfile.removeFile(); + return ret; +} + + TextClass::ReturnValues TextClass::readWithoutConv(FileName const & filename, ReadType rt) { if (!filename.isReadableFile()) { @@ -303,23 +326,21 @@ bool TextClass::read(FileName const & filename, ReadType rt) return retval == OK; bool const worx = convertLayoutFormat(filename, rt); - if (!worx) { + if (!worx) LYXERR0 ("Unable to convert " << filename << " to format " << LAYOUT_FORMAT); - return false; - } - return true; + return worx; } -bool TextClass::validate(std::string const & str) +TextClass::ReturnValues TextClass::validate(std::string const & str) { TextClass tc; return tc.read(str, VALIDATION); } -bool TextClass::read(std::string const & str, ReadType rt) +TextClass::ReturnValues TextClass::read(std::string const & str, ReadType rt) { Lexer lexrc(textClassTags); istringstream is(str); @@ -327,14 +348,14 @@ bool TextClass::read(std::string const & str, ReadType rt) ReturnValues retval = read(lexrc, rt); if (retval != FORMAT_MISMATCH) - return retval == OK; + return retval; // write the layout string to a temporary file FileName const tempfile = FileName::tempName("TextClass_read"); ofstream os(tempfile.toFilesystemEncoding().c_str()); if (!os) { LYXERR0("Unable to create temporary file"); - return false; + return ERROR; } os << str; os.close(); @@ -344,9 +365,10 @@ bool TextClass::read(std::string const & str, ReadType rt) if (!worx) { LYXERR0("Unable to convert internal layout information to format " << LAYOUT_FORMAT); + return ERROR; } tempfile.removeFile(); - return worx; + return OK_OLDFORMAT; } diff --git a/src/TextClass.h b/src/TextClass.h index f3d2197278..24ee2aa1f2 100644 --- a/src/TextClass.h +++ b/src/TextClass.h @@ -153,19 +153,23 @@ public: /// return values for read() enum ReturnValues { OK, + OK_OLDFORMAT, ERROR, FORMAT_MISMATCH }; /// Performs the read of the layout file. /// \return true on success. + // FIXME Should return ReturnValues.... bool read(support::FileName const & filename, ReadType rt = BASECLASS); /// - bool read(std::string const & str, ReadType rt = BASECLASS); + ReturnValues read(std::string const & str, ReadType rt = MODULE); /// ReturnValues read(Lexer & lex, ReadType rt = BASECLASS); /// validates the layout information passed in str - static bool validate(std::string const & str); + static ReturnValues validate(std::string const & str); + /// + static std::string convert(std::string const & str); /////////////////////////////////////////////////////////////////// // loading diff --git a/src/frontends/qt4/GuiDocument.cpp b/src/frontends/qt4/GuiDocument.cpp index 05414d4039..6089bc8829 100644 --- a/src/frontends/qt4/GuiDocument.cpp +++ b/src/frontends/qt4/GuiDocument.cpp @@ -537,6 +537,7 @@ LocalLayout::LocalLayout() : current_id_(0), is_valid_(false) { connect(locallayoutTE, SIGNAL(textChanged()), this, SLOT(textChanged())); connect(validatePB, SIGNAL(clicked()), this, SLOT(validatePressed())); + connect(convertPB, SIGNAL(clicked()), this, SLOT(convertPressed())); } @@ -567,8 +568,30 @@ void LocalLayout::textChanged() static const QString unknown = qt_("Press button to check validity..."); is_valid_ = false; - infoLB->setText(unknown); + validLB->setText(unknown); validatePB->setEnabled(true); + convertPB->setEnabled(false); + changed(); +} + + +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 { + locallayoutTE->setPlainText(toqstr(newlayout)); + } + validate(); +} + + +void LocalLayout::convertPressed() { + convert(); changed(); } @@ -587,12 +610,25 @@ void LocalLayout::validate() { fromqstr(locallayoutTE->document()->toPlainText().trimmed()); if (layout.empty()) { is_valid_ = true; - infoLB->setText(""); + validatePB->setEnabled(false); + validLB->setText(""); + convertPB->hide(); + convertLB->hide(); } else { - is_valid_ = TextClass::validate(layout); - infoLB->setText(is_valid_ ? vtext : ivtext); + TextClass::ReturnValues const ret = TextClass::validate(layout); + is_valid_ = (ret == TextClass::OK) || (ret == TextClass::OK_OLDFORMAT); + validatePB->setEnabled(false); + validLB->setText(is_valid_ ? vtext : ivtext); + if (ret == TextClass::OK_OLDFORMAT) { + convertPB->show(); + convertPB->setEnabled(true); + convertLB->setText(qt_("Convert to current format")); + convertLB->show(); + } else { + convertPB->hide(); + convertLB->hide(); + } } - validatePB->setEnabled(false); } diff --git a/src/frontends/qt4/GuiDocument.h b/src/frontends/qt4/GuiDocument.h index b438f5f7de..1b5aad13b8 100644 --- a/src/frontends/qt4/GuiDocument.h +++ b/src/frontends/qt4/GuiDocument.h @@ -289,9 +289,11 @@ Q_SIGNALS: private: void validate(); + void convert(); private Q_SLOTS: void textChanged(); void validatePressed(); + void convertPressed(); private: BufferId current_id_; diff --git a/src/frontends/qt4/ui/LocalLayoutUi.ui b/src/frontends/qt4/ui/LocalLayoutUi.ui index ffee557871..57ef0da37e 100644 --- a/src/frontends/qt4/ui/LocalLayoutUi.ui +++ b/src/frontends/qt4/ui/LocalLayoutUi.ui @@ -1,3 +1,4 @@ + LocalLayoutUi @@ -6,14 +7,14 @@ 0 0 377 - 371 + 434 - + Document-specific layout information @@ -26,8 +27,18 @@ - - + + + + + + + &Validate + + + + + Qt::NoContextMenu @@ -35,7 +46,7 @@ Errors reported in terminal. - Press button to check validity... + Qt::AlignCenter @@ -43,12 +54,25 @@ - + + + Convert + + + + + + + Qt::NoContextMenu + - + Errors reported in terminal. - &Validate + + + + Qt::AlignCenter