Fix bug #7500: There is presently no way in the GUI to update local

layout to the current format.

This probably isn't needed for branch, since local layout was a
"hidden feature" prior to 2.0, and one can update local layout by:
  (a) copying to a file
  (b) running layout2layout on that file
  (c) pasting back into LyX
So we should probably just leave this in trunk.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@38622 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Richard Heck 2011-05-08 00:54:17 +00:00
parent 33ee5c9ae8
commit 1493191f77
6 changed files with 115 additions and 25 deletions

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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

View File

@ -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);
}

View File

@ -289,9 +289,11 @@ Q_SIGNALS:
private:
void validate();
void convert();
private Q_SLOTS:
void textChanged();
void validatePressed();
void convertPressed();
private:
BufferId current_id_;

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LocalLayoutUi</class>
<widget class="QWidget" name="LocalLayoutUi">
@ -6,14 +7,14 @@
<x>0</x>
<y>0</y>
<width>377</width>
<height>371</height>
<height>434</height>
</rect>
</property>
<property name="windowTitle">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="4">
<item row="0" column="0" colspan="2">
<widget class="QTextEdit" name="locallayoutTE">
<property name="toolTip">
<string>Document-specific layout information</string>
@ -26,8 +27,18 @@
</property>
</widget>
</item>
<item row="2" column="1" colspan="3">
<widget class="QLabel" name="infoLB">
<item row="1" column="0">
<widget class="QPushButton" name="validatePB">
<property name="toolTip">
<string/>
</property>
<property name="text">
<string>&amp;Validate</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="validLB">
<property name="contextMenuPolicy">
<enum>Qt::NoContextMenu</enum>
</property>
@ -35,7 +46,7 @@
<string>Errors reported in terminal.</string>
</property>
<property name="text">
<string>Press button to check validity...</string>
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
@ -43,12 +54,25 @@
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="validatePB">
<widget class="QPushButton" name="convertPB">
<property name="text">
<string>Convert</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="convertLB">
<property name="contextMenuPolicy">
<enum>Qt::NoContextMenu</enum>
</property>
<property name="toolTip">
<string/>
<string>Errors reported in terminal.</string>
</property>
<property name="text">
<string>&amp;Validate</string>
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>