X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FGuiViewSource.cpp;h=d6b141b969b6bced755bba23b96cda1ae095ff5a;hb=a0a04e4886fe33f06c3b1b2351d2a26eee681854;hp=c2f7fb562f752be3ad6f4fb1e9964592a0d5f190;hpb=cc832049a96c43ad94a659c071f70069c3e12b4c;p=lyx.git diff --git a/src/frontends/qt4/GuiViewSource.cpp b/src/frontends/qt4/GuiViewSource.cpp index c2f7fb562f..d6b141b969 100644 --- a/src/frontends/qt4/GuiViewSource.cpp +++ b/src/frontends/qt4/GuiViewSource.cpp @@ -13,18 +13,27 @@ #include #include "GuiViewSource.h" +#include "LaTeXHighlighter.h" #include "qt_helpers.h" #include "Application.h" +#include "BufferView.h" +#include "Buffer.h" +#include "Cursor.h" +#include "gettext.h" +#include "Paragraph.h" +#include "TexRow.h" #include #include #include +using std::string; + namespace lyx { namespace frontend { -GuiViewSourceDialog::GuiViewSourceDialog(ControlViewSource & controller) +ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller) : controller_(controller), document_(new QTextDocument(this)), highlighter_(new LaTeXHighlighter(document_)) { @@ -40,9 +49,9 @@ GuiViewSourceDialog::GuiViewSourceDialog(ControlViewSource & controller) // setting a document at this point trigger an assertion in Qt // so we disable the signals here: - document()->blockSignals(true); - viewSourceTV->setDocument(document()); - document()->blockSignals(false); + document_->blockSignals(true); + viewSourceTV->setDocument(document_); + document_->blockSignals(false); viewSourceTV->setReadOnly(true); ///dialog_->viewSourceTV->setAcceptRichText(false); // this is personal. I think source code should be in fixed-size font @@ -56,7 +65,7 @@ GuiViewSourceDialog::GuiViewSourceDialog(ControlViewSource & controller) } -void GuiViewSourceDialog::updateView() +void ViewSourceWidget::updateView() { if (autoUpdateCB->isChecked()) update(viewFullSourceCB->isChecked()); @@ -68,99 +77,100 @@ void GuiViewSourceDialog::updateView() c.select(QTextCursor::BlockUnderCursor); c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, end - beg + 1); viewSourceTV->setTextCursor(c); - QWidget::update(); } -void GuiViewSourceDialog::update(bool full_source) +void ViewSourceWidget::update(bool full_source) +{ + document_->setPlainText(controller_.getContent(full_source)); +} + + +GuiViewSource::GuiViewSource(GuiViewBase & parent, Qt::DockWidgetArea area, Qt::WindowFlags flags) + : DockView(parent, "view-source", area, flags) { - document_->setPlainText(toqstr(controller_.updateContent(full_source))); + widget_ = new ViewSourceWidget(*this); + setWidget(widget_); + setWindowTitle(widget_->windowTitle()); } +GuiViewSource::~GuiViewSource() +{ + delete widget_; +} + -///////////////////////////////////////////////////////////////////// -// -// LaTeXHighlighter -// -///////////////////////////////////////////////////////////////////// +void GuiViewSource::updateView() +{ + widget_->updateView(); +} -LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent) - : QSyntaxHighlighter(parent) +bool GuiViewSource::initialiseParams(string const & /*source*/) { - keywordFormat.setForeground(Qt::darkBlue); - keywordFormat.setFontWeight(QFont::Bold); - commentFormat.setForeground(Qt::darkGray); - mathFormat.setForeground(Qt::red); + setWindowTitle(title()); + return true; } -void LaTeXHighlighter::highlightBlock(QString const & text) +QString GuiViewSource::getContent(bool fullSource) { - // $ $ - QRegExp exprMath("\\$[^\\$]*\\$"); - int index = text.indexOf(exprMath); - while (index >= 0) { - int length = exprMath.matchedLength(); - setFormat(index, length, mathFormat); - index = text.indexOf(exprMath, index + length); - } - // [ ] - QRegExp exprStartDispMath("(\\\\\\[|" - "\\\\begin\\{equation\\**\\}|" - "\\\\begin\\{eqnarray\\**\\}|" - "\\\\begin\\{align(ed|at)*\\**\\}|" - "\\\\begin\\{flalign\\**\\}|" - "\\\\begin\\{gather\\**\\}|" - "\\\\begin\\{multline\\**\\}|" - "\\\\begin\\{array\\**\\}|" - "\\\\begin\\{cases\\**\\}" - ")"); - QRegExp exprEndDispMath("(\\\\\\]|" - "\\\\end\\{equation\\**\\}|" - "\\\\end\\{eqnarray\\**\\}|" - "\\\\end\\{align(ed|at)*\\**\\}|" - "\\\\end\\{flalign\\**\\}|" - "\\\\end\\{gather\\**\\}|" - "\\\\end\\{multline\\**\\}|" - "\\\\end\\{array\\**\\}|" - "\\\\end\\{cases\\**\\}" - ")"); - int startIndex = 0; - // if previous block was in 'disp math' - // start search from 0 (for end disp math) - // otherwise, start search from 'begin disp math' - if (previousBlockState() != 1) - startIndex = text.indexOf(exprStartDispMath); - while (startIndex >= 0) { - int endIndex = text.indexOf(exprEndDispMath, startIndex); - int length; - if (endIndex == -1) { - setCurrentBlockState(1); - length = text.length() - startIndex; - } else { - length = endIndex - startIndex + exprEndDispMath.matchedLength(); - } - setFormat(startIndex, length, mathFormat); - startIndex = text.indexOf(exprStartDispMath, startIndex + length); - } - // \whatever - QRegExp exprKeyword("\\\\[A-Za-z]+"); - index = text.indexOf(exprKeyword); - while (index >= 0) { - int length = exprKeyword.matchedLength(); - setFormat(index, length, keywordFormat); - index = text.indexOf(exprKeyword, index + length); + // get the *top* level paragraphs that contain the cursor, + // or the selected text + pit_type par_begin; + pit_type par_end; + + BufferView * view = bufferview(); + if (!view->cursor().selection()) { + par_begin = view->cursor().bottom().pit(); + par_end = par_begin; + } else { + par_begin = view->cursor().selectionBegin().bottom().pit(); + par_end = view->cursor().selectionEnd().bottom().pit(); } - // comment - QRegExp exprComment("(^|[^\\\\])%.*$"); - index = text.indexOf(exprComment); - while (index >= 0) { - int const length = exprComment.matchedLength(); - setFormat(index, length, commentFormat); - index = text.indexOf(exprComment, index + length); + if (par_begin > par_end) + std::swap(par_begin, par_end); + odocstringstream ostr; + view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource); + return toqstr(ostr.str()); +} + + +std::pair GuiViewSource::getRows() const +{ + BufferView const * view = bufferview(); + CursorSlice beg = view->cursor().selectionBegin().bottom(); + CursorSlice end = view->cursor().selectionEnd().bottom(); + + int begrow = view->buffer().texrow(). + getRowFromIdPos(beg.paragraph().id(), beg.pos()); + int endrow = view->buffer().texrow(). + getRowFromIdPos(end.paragraph().id(), end.pos()); + int nextendrow = view->buffer().texrow(). + getRowFromIdPos(end.paragraph().id(), end.pos() + 1); + return std::make_pair(begrow, endrow == nextendrow ? endrow : (nextendrow - 1)); +} + + +QString GuiViewSource::title() const +{ + switch (docType()) { + case LATEX: + return qt_("LaTeX Source"); + case DOCBOOK: + return qt_("DocBook Source"); + case LITERATE: + return qt_("Literate Source"); } + BOOST_ASSERT(false); + return QString(); +} + + +Dialog * createGuiViewSource(LyXView & lv) +{ + return new GuiViewSource(static_cast(lv)); }