From 99d025e2da2fcd50f7b6da8e2f192cfb0b30c7d8 Mon Sep 17 00:00:00 2001 From: Richard Heck Date: Thu, 19 Apr 2018 23:33:40 -0400 Subject: [PATCH] Fix bug #991. Patch adapts Georg's work on #7404 to this case. (cherry picked from commit 3847e0ef7726afc41e6ed69f625d36153ccccc33) --- src/frontends/qt4/GuiDocument.cpp | 68 +++++++++++++++++++++++++++ src/frontends/qt4/GuiDocument.h | 12 ++++- src/frontends/qt4/ui/LocalLayoutUi.ui | 56 ++++++++++++---------- src/frontends/qt4/ui/PreambleUi.ui | 9 +++- 4 files changed, 119 insertions(+), 26 deletions(-) diff --git a/src/frontends/qt4/GuiDocument.cpp b/src/frontends/qt4/GuiDocument.cpp index 645d12a379..6bd7776bcf 100644 --- a/src/frontends/qt4/GuiDocument.cpp +++ b/src/frontends/qt4/GuiDocument.cpp @@ -59,11 +59,13 @@ #include "insets/InsetListingsParams.h" #include "support/debug.h" +#include "support/docstream.h" #include "support/FileName.h" #include "support/filetools.h" #include "support/gettext.h" #include "support/lassert.h" #include "support/lstrings.h" +#include "support/TempFile.h" #include "frontends/alert.h" @@ -472,6 +474,7 @@ PreambleModule::PreambleModule(QWidget * parent) connect(preambleTE, SIGNAL(textChanged()), this, SIGNAL(changed())); connect(findLE, SIGNAL(textEdited(const QString &)), this, SLOT(checkFindButton())); connect(findButtonPB, SIGNAL(clicked()), this, SLOT(findText())); + connect(editPB, SIGNAL(clicked()), this, SLOT(editExternal())); connect(findLE, SIGNAL(returnPressed()), this, SLOT(findText())); checkFindButton(); // https://stackoverflow.com/questions/13027091/how-to-override-tab-width-in-qt @@ -546,6 +549,36 @@ void PreambleModule::closeEvent(QCloseEvent * e) } +void PreambleModule::editExternal() { + if (!current_id_) + return; + + if (tempfile_) { + preambleTE->setReadOnly(false); + FileName const tempfilename = tempfile_->name(); + docstring const s = tempfilename.fileContents("UTF-8"); + preambleTE->document()->setPlainText(toqstr(s)); + tempfile_.reset(); + editPB->setText(qt_("Edit")); + changed(); + return; + } + + string const format = + current_id_->params().documentClass().outputFormat(); + string const ext = theFormats().extension(format); + tempfile_.reset(new TempFile("preamble_editXXXXXX." + ext)); + FileName const tempfilename = tempfile_->name(); + string const name = tempfilename.toFilesystemEncoding(); + ofdocstream os(name.c_str()); + os << qstring_to_ucs4(preambleTE->document()->toPlainText()); + os.close(); + preambleTE->setReadOnly(true); + theFormats().edit(*current_id_, tempfilename, format); + editPB->setText(qt_("End Edit")); + changed(); +} + ///////////////////////////////////////////////////////////////////// // // LocalLayout @@ -561,6 +594,7 @@ LocalLayout::LocalLayout(QWidget * parent) connect(locallayoutTE, SIGNAL(textChanged()), this, SLOT(textChanged())); connect(validatePB, SIGNAL(clicked()), this, SLOT(validatePressed())); connect(convertPB, SIGNAL(clicked()), this, SLOT(convertPressed())); + connect(editPB, SIGNAL(clicked()), this, SLOT(editExternal())); } @@ -685,6 +719,38 @@ void LocalLayout::validatePressed() { } +void LocalLayout::editExternal() { + if (!current_id_) + return; + + if (tempfile_) { + locallayoutTE->setReadOnly(false); + FileName const tempfilename = tempfile_->name(); + docstring const s = tempfilename.fileContents("UTF-8"); + locallayoutTE->document()->setPlainText(toqstr(s)); + tempfile_.reset(); + editPB->setText(qt_("Edit")); + changed(); + return; + } + + string const format = + current_id_->params().documentClass().outputFormat(); + string const ext = theFormats().extension(format); + tempfile_.reset(new TempFile("preamble_editXXXXXX." + ext)); + FileName const tempfilename = tempfile_->name(); + string const name = tempfilename.toFilesystemEncoding(); + ofdocstream os(name.c_str()); + os << qstring_to_ucs4(locallayoutTE->document()->toPlainText()); + os.close(); + locallayoutTE->setReadOnly(true); + theFormats().edit(*current_id_, tempfilename, format); + editPB->setText(qt_("End Edit")); + validatePB->setEnabled(false); + hideConvert(); + changed(); +} + ///////////////////////////////////////////////////////////////////// // // DocumentDialog @@ -4261,6 +4327,8 @@ bool GuiDocument::isValid() return validateListingsParameters().isEmpty() && localLayout->isValid() && + !localLayout->editing() && + !preambleModule->editing() && ( // if we're asking for skips between paragraphs !textLayoutModule->skipRB->isChecked() || diff --git a/src/frontends/qt4/GuiDocument.h b/src/frontends/qt4/GuiDocument.h index e4f32e2570..dd51bac8c4 100644 --- a/src/frontends/qt4/GuiDocument.h +++ b/src/frontends/qt4/GuiDocument.h @@ -44,6 +44,10 @@ class LayoutModuleList; class LyXModule; class TextClass; +namespace support { + class TempFile; +} + namespace frontend { class FloatPlacement; @@ -55,7 +59,7 @@ class LocalLayout; class FontModule; /// -typedef void const * BufferId; +typedef Buffer const * BufferId; template class UiWidget : public QWidget, public UI @@ -323,6 +327,7 @@ public: PreambleModule(QWidget * parent); void update(BufferParams const & params, BufferId id); void apply(BufferParams & params); + bool editing() const { return (bool)tempfile_; } Q_SIGNALS: /// signal that something's changed in the Widget. @@ -335,11 +340,13 @@ private: typedef std::map > Coords; Coords preamble_coords_; BufferId current_id_; + unique_ptr tempfile_; private Q_SLOTS: /// void checkFindButton(); void findText(); + void editExternal(); }; @@ -351,6 +358,7 @@ public: void update(BufferParams const & params, BufferId id); void apply(BufferParams & params); bool isValid() const { return validated_; } + bool editing() const { return (bool)tempfile_; } Q_SIGNALS: /// signal that something's changed in the Widget. @@ -364,10 +372,12 @@ private Q_SLOTS: void textChanged(); void validatePressed(); void convertPressed(); + void editExternal(); private: BufferId current_id_; bool validated_; + unique_ptr tempfile_; }; diff --git a/src/frontends/qt4/ui/LocalLayoutUi.ui b/src/frontends/qt4/ui/LocalLayoutUi.ui index 6698fac7af..7202199ce6 100644 --- a/src/frontends/qt4/ui/LocalLayoutUi.ui +++ b/src/frontends/qt4/ui/LocalLayoutUi.ui @@ -1,3 +1,4 @@ + LocalLayoutUi @@ -13,30 +14,30 @@ - - + + + + Qt::NoContextMenu + - Document-specific layout information + Errors reported in terminal. - + - - false + + Qt::AlignCenter - - - - - + + - &Validate + Convert - + Qt::NoContextMenu @@ -52,26 +53,33 @@ - - + + - Convert + Edit - - - - Qt::NoContextMenu - + + - Errors reported in terminal. + + &Validate + + + + + + + Document-specific layout information + + - - Qt::AlignCenter + + false diff --git a/src/frontends/qt4/ui/PreambleUi.ui b/src/frontends/qt4/ui/PreambleUi.ui index 1bc1c52879..4877b585d2 100644 --- a/src/frontends/qt4/ui/PreambleUi.ui +++ b/src/frontends/qt4/ui/PreambleUi.ui @@ -39,7 +39,14 @@ - + + + + Edit + + + + false -- 2.39.5