I managed to get the thing to work through your guidance and shameless copy-paste: let me know if you find any problems. I generalized the findText method decently, but the rest of the patch is still hardcoded. Also, I don't fully understand why <writeLatex>> is in <BufferParams> but write <LyXHTMLSource> is in <Buffer>, and how the whole outputting is done. Is there any documentation?
Lo
From 9914af1583355687e93662ac1019880799228e17 Mon Sep 17 00:00:00 2001 From: Lorenzo Bertini <lorenzobertini97@gmail.com> Date: Mon, 21 Sep 2020 20:08:32 +0200 Subject: [PATCH] Preamble section generalization --- src/Buffer.cpp | 7 ++++ src/BufferParams.cpp | 19 +++++++++++ src/BufferParams.h | 4 +++ src/frontends/qt/GuiDocument.cpp | 37 +++++++++++++++++---- src/frontends/qt/GuiDocument.h | 3 +- src/frontends/qt/ui/PreambleUi.ui | 54 +++++++++++++++++++++++-------- 6 files changed, 104 insertions(+), 20 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 2ef5768771..3090224d2e 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -2288,6 +2288,13 @@ Buffer::ExportStatus Buffer::writeLyXHTMLSource(odocstream & os, << "\n</style>\n"; } } + + // the user-defined preamble + if (!params().htmlpreamble.empty()) { + os << "<!--User specified HTML <head> commands.-->\n" + << params().htmlpreamble << "\n"; + } + os << "</head>\n"; } diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index b5ae2d4cad..22b89fec4c 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -774,6 +774,8 @@ string BufferParams::readToken(Lexer & lex, string const & token, } } else if (token == "\\begin_preamble") { readPreamble(lex); + } else if (token == "\\begin_htmlpreamble") { + readHtmlPreamble(lex); } else if (token == "\\begin_local_layout") { readLocalLayout(lex, false); } else if (token == "\\begin_forced_local_layout") { @@ -1211,6 +1213,15 @@ void BufferParams::writeFile(ostream & os, Buffer const * buf) const << to_utf8(tmppreamble) << "\n\\end_preamble\n"; } + + // the html preamble + if (!htmlpreamble.empty()) { + // remove '\n' from the end of preamble + docstring const tmphtmlpreamble = rtrim(htmlpreamble, "\n"); + os << "\\begin_htmlpreamble\n" + << to_utf8(tmphtmlpreamble) + << "\n\\end_htmlpreamble\n"; + } // the options if (!options.empty()) { @@ -2820,6 +2831,14 @@ void BufferParams::readPreamble(Lexer & lex) preamble = lex.getLongString(from_ascii("\\end_preamble")); } +void BufferParams::readHtmlPreamble(Lexer & lex) +{ + if (lex.getString() != "\\begin_htmlpreamble") + lyxerr << "Error (BufferParams::readPreamble):" + "consistency check failed." << endl; + + htmlpreamble = lex.getLongString(from_ascii("\\end_htmlpreamble")); +} void BufferParams::readLocalLayout(Lexer & lex, bool forced) { diff --git a/src/BufferParams.h b/src/BufferParams.h index b42d622fb3..ea590653e7 100644 --- a/src/BufferParams.h +++ b/src/BufferParams.h @@ -357,6 +357,8 @@ public: /// docstring preamble; /// + docstring htmlpreamble; + /// std::string options; /// use the class options defined in the layout? bool use_default_options; @@ -596,6 +598,8 @@ private: /// void readPreamble(Lexer &); /// + void readHtmlPreamble(Lexer &); + /// void readLocalLayout(Lexer &, bool); /// void readLanguage(Lexer &); diff --git a/src/frontends/qt/GuiDocument.cpp b/src/frontends/qt/GuiDocument.cpp index 9b4b97dc76..ffb639e19d 100644 --- a/src/frontends/qt/GuiDocument.cpp +++ b/src/frontends/qt/GuiDocument.cpp @@ -484,8 +484,11 @@ PreambleModule::PreambleModule(QWidget * parent) (void) new LaTeXHighlighter(preambleTE->document(), true); preambleTE->setFont(guiApp->typewriterSystemFont()); preambleTE->setWordWrapMode(QTextOption::NoWrap); + htmlPreambleTE->setFont(guiApp->typewriterSystemFont()); + htmlPreambleTE->setWordWrapMode(QTextOption::NoWrap); setFocusProxy(preambleTE); connect(preambleTE, SIGNAL(textChanged()), this, SIGNAL(changed())); + connect(htmlPreambleTE, 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())); @@ -498,8 +501,10 @@ PreambleModule::PreambleModule(QWidget * parent) // horizontalAdvance() is available starting in 5.11.0 // setTabStopDistance() is available starting in 5.10.0 preambleTE->setTabStopDistance(tabStop * metrics.horizontalAdvance(' ')); + htmlPreambleTE->setTabStopDistance(tabStop * metrics.horizontalAdvance(' ')); #else preambleTE->setTabStopWidth(tabStop * metrics.width(' ')); + htmlPreambleTE->setTabStopWidth(tabStop * metrics.width(' ')); #endif } @@ -512,13 +517,14 @@ void PreambleModule::checkFindButton() void PreambleModule::findText() { - bool const found = preambleTE->find(findLE->text()); + QTextEdit *currentTE = tabWidget->currentWidget()->findChild<QTextEdit *>(); + bool const found = currentTE->find(findLE->text()); if (!found) { // wrap - QTextCursor qtcur = preambleTE->textCursor(); + QTextCursor qtcur = currentTE->textCursor(); qtcur.movePosition(QTextCursor::Start); - preambleTE->setTextCursor(qtcur); - preambleTE->find(findLE->text()); + currentTE->setTextCursor(qtcur); + currentTE->find(findLE->text()); } } @@ -526,29 +532,44 @@ void PreambleModule::findText() void PreambleModule::update(BufferParams const & params, BufferId id) { QString preamble = toqstr(params.preamble); + QString htmlpreamble = toqstr(params.htmlpreamble); // Nothing to do if the params and preamble are unchanged. if (id == current_id_ - && preamble == preambleTE->document()->toPlainText()) + && preamble == preambleTE->document()->toPlainText() + && htmlpreamble == htmlPreambleTE->document()->toPlainText()) return; QTextCursor cur = preambleTE->textCursor(); + QTextCursor htmlCur = htmlPreambleTE->textCursor(); // Save the coords before switching to the new one. preamble_coords_[current_id_] = make_pair(cur.position(), preambleTE->verticalScrollBar()->value()); + html_preamble_coords_[current_id_] = + make_pair(htmlCur.position(), htmlPreambleTE->verticalScrollBar()->value()); // Save the params address for further use. current_id_ = id; preambleTE->document()->setPlainText(preamble); + htmlPreambleTE->document()->setPlainText(htmlpreamble); Coords::const_iterator it = preamble_coords_.find(current_id_); + Coords::const_iterator html_it = html_preamble_coords_.find(current_id_); if (it == preamble_coords_.end()) + { // First time we open this one. preamble_coords_[current_id_] = make_pair(0, 0); + html_preamble_coords_[current_id_] = make_pair(0, 0); + } else { // Restore saved coords. cur = preambleTE->textCursor(); cur.setPosition(it->second.first); preambleTE->setTextCursor(cur); preambleTE->verticalScrollBar()->setValue(it->second.second); + + htmlCur = preambleTE->textCursor(); + htmlCur.setPosition(it->second.first); + htmlPreambleTE->setTextCursor(htmlCur); + htmlPreambleTE->verticalScrollBar()->setValue(it->second.second); } } @@ -556,6 +577,7 @@ void PreambleModule::update(BufferParams const & params, BufferId id) void PreambleModule::apply(BufferParams & params) { params.preamble = qstring_to_ucs4(preambleTE->document()->toPlainText()); + params.htmlpreamble = qstring_to_ucs4(htmlPreambleTE->document()->toPlainText()); } @@ -563,8 +585,11 @@ void PreambleModule::closeEvent(QCloseEvent * e) { // Save the coords before closing. QTextCursor cur = preambleTE->textCursor(); + QTextCursor htmlCur = htmlPreambleTE->textCursor(); preamble_coords_[current_id_] = make_pair(cur.position(), preambleTE->verticalScrollBar()->value()); + html_preamble_coords_[current_id_] = + make_pair(htmlCur.position(), htmlPreambleTE->verticalScrollBar()->value()); e->accept(); } @@ -1783,7 +1808,7 @@ GuiDocument::GuiDocument(GuiView & lv) docPS->addPanel(bulletsModule, N_("Bullets")); docPS->addPanel(branchesModule, N_("Branches")); docPS->addPanel(outputModule, N_("Formats[[output]]")); - docPS->addPanel(preambleModule, N_("LaTeX Preamble")); + docPS->addPanel(preambleModule, N_("Preambles")); docPS->setCurrentPanel("Document Class"); // FIXME: hack to work around resizing bug in Qt >= 4.2 // bug verified with Qt 4.2.{0-3} (JSpitzm) diff --git a/src/frontends/qt/GuiDocument.h b/src/frontends/qt/GuiDocument.h index 4d13f44d0f..5bb92b39c8 100644 --- a/src/frontends/qt/GuiDocument.h +++ b/src/frontends/qt/GuiDocument.h @@ -366,9 +366,10 @@ Q_SIGNALS: private: void closeEvent(QCloseEvent *); void on_preambleTE_textChanged() { changed(); } + void on_htmlPreambleTE_textChanged() { changed(); } typedef std::map<BufferId, std::pair<int,int> > Coords; - Coords preamble_coords_; + Coords preamble_coords_, html_preamble_coords_; BufferId current_id_; unique_ptr<support::TempFile> tempfile_; diff --git a/src/frontends/qt/ui/PreambleUi.ui b/src/frontends/qt/ui/PreambleUi.ui index 8a7015c6dd..3de884725f 100644 --- a/src/frontends/qt/ui/PreambleUi.ui +++ b/src/frontends/qt/ui/PreambleUi.ui @@ -29,16 +29,6 @@ <property name="spacing"> <number>6</number> </property> - <item row="1" column="0"> - <widget class="QLineEdit" name="findLE"/> - </item> - <item row="1" column="1"> - <widget class="QPushButton" name="findButtonPB"> - <property name="text"> - <string>&Find</string> - </property> - </widget> - </item> <item row="1" column="2"> <widget class="QPushButton" name="editPB"> <property name="toolTip"> @@ -49,11 +39,49 @@ </property> </widget> </item> + <item row="1" column="0"> + <widget class="QLineEdit" name="findLE"/> + </item> + <item row="1" column="1"> + <widget class="QPushButton" name="findButtonPB"> + <property name="text"> + <string>&Find</string> + </property> + </widget> + </item> <item row="0" column="0" colspan="3"> - <widget class="QTextEdit" name="preambleTE"> - <property name="acceptRichText"> - <bool>false</bool> + <widget class="QTabWidget" name="tabWidget"> + <property name="currentIndex"> + <number>0</number> </property> + <widget class="QWidget" name="tabLatex"> + <attribute name="title"> + <string>LaTeX</string> + </attribute> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QTextEdit" name="preambleTE"> + <property name="acceptRichText"> + <bool>false</bool> + </property> + </widget> + </item> + </layout> + </widget> + <widget class="QWidget" name="tabHtml"> + <attribute name="title"> + <string>HTML</string> + </attribute> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <item> + <widget class="QTextEdit" name="htmlPreambleTE"> + <property name="acceptRichText"> + <bool>false</bool> + </property> + </widget> + </item> + </layout> + </widget> </widget> </item> </layout> -- 2.28.0
-- lyx-devel mailing list lyx-devel@lists.lyx.org http://lists.lyx.org/mailman/listinfo/lyx-devel