]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiViewSource.cpp
do what the FIXME suggested
[lyx.git] / src / frontends / qt4 / GuiViewSource.cpp
index ef34dc5500b3bab9c2396efab43e1e5435c1d358..245dc71b18f54932f8fac89e85d7380ad8438f50 100644 (file)
 
 #include <config.h>
 
+#include "GuiApplication.h"
 #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 "support/docstream.h"
+#include "support/gettext.h"
+
 #include <QTextCursor>
 #include <QTextDocument>
-#include <boost/tuple/tuple.hpp>
 
-using std::string;
+using namespace std;
 
 namespace lyx {
 namespace frontend {
 
-GuiViewSourceDialog::GuiViewSourceDialog(ControlViewSource & controller)
+ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller)
        :       controller_(controller), document_(new QTextDocument(this)),
                highlighter_(new LaTeXHighlighter(document_))
 {
        setupUi(this);
-       setWindowTitle(qt_("LaTeX Source"));
 
        connect(viewFullSourceCB, SIGNAL(clicked()),
                this, SLOT(updateView()));
@@ -48,13 +49,13 @@ 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
-       QFont font(toqstr(theApp()->typewriterFontName()));
+       QFont font(guiApp->typewriterFontName());
        font.setKerning(false);
        font.setFixedPitch(true);
        font.setStyleHint(QFont::TypeWriter);
@@ -64,126 +65,56 @@ GuiViewSourceDialog::GuiViewSourceDialog(ControlViewSource & controller)
 }
 
 
-void GuiViewSourceDialog::updateView()
+void ViewSourceWidget::updateView()
 {
        if (autoUpdateCB->isChecked())
                update(viewFullSourceCB->isChecked());
 
-       int beg, end;
-       boost::tie(beg, end) = controller_.getRows();
+       GuiViewSource::Row row = controller_.getRows();
        QTextCursor c = QTextCursor(viewSourceTV->document());
-       c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, beg);
+       c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, row.begin);
        c.select(QTextCursor::BlockUnderCursor);
-       c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, end - beg + 1);
+       c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
+               row.end - row.begin + 1);
        viewSourceTV->setTextCursor(c);
-       QWidget::update();
 }
 
 
-void GuiViewSourceDialog::update(bool full_source)
+void ViewSourceWidget::update(bool full_source)
 {
-       document_->setPlainText(toqstr(controller_.updateContent(full_source)));
+       document_->setPlainText(controller_.getContent(full_source));
 }
 
 
-
-/////////////////////////////////////////////////////////////////////
-//
-// LaTeXHighlighter
-//
-/////////////////////////////////////////////////////////////////////
-
-
-LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
-       : QSyntaxHighlighter(parent)
+GuiViewSource::GuiViewSource(GuiView & parent,
+               Qt::DockWidgetArea area, Qt::WindowFlags flags)
+       : DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
 {
-       keywordFormat.setForeground(Qt::darkBlue);
-       keywordFormat.setFontWeight(QFont::Bold);
-       commentFormat.setForeground(Qt::darkGray);
-       mathFormat.setForeground(Qt::red);
+       widget_ = new ViewSourceWidget(*this);
+       setWidget(widget_);
 }
 
 
-void LaTeXHighlighter::highlightBlock(QString const & text)
+GuiViewSource::~GuiViewSource()
 {
-       // $ $
-       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);
-       }
-       // 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);
-       }
+       delete widget_;
 }
 
 
-ControlViewSource::ControlViewSource(Dialog & parent)
-       : Controller(parent)
-{}
+void GuiViewSource::updateView()
+{
+       widget_->updateView();
+}
 
 
-bool ControlViewSource::initialiseParams(string const & /*source*/)
+bool GuiViewSource::initialiseParams(string const & /*source*/)
 {
+       setWindowTitle(title());
        return true;
 }
 
 
-docstring const ControlViewSource::updateContent(bool fullSource)
+QString GuiViewSource::getContent(bool fullSource)
 {
        // get the *top* level paragraphs that contain the cursor,
        // or the selected text
@@ -199,14 +130,14 @@ docstring const ControlViewSource::updateContent(bool fullSource)
                par_end = view->cursor().selectionEnd().bottom().pit();
        }
        if (par_begin > par_end)
-               std::swap(par_begin, par_end);
+               swap(par_begin, par_end);
        odocstringstream ostr;
        view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
-       return ostr.str();
+       return toqstr(ostr.str());
 }
 
 
-std::pair<int, int> ControlViewSource::getRows() const
+GuiViewSource::Row GuiViewSource::getRows() const
 {
        BufferView const * view = bufferview();
        CursorSlice beg = view->cursor().selectionBegin().bottom();
@@ -218,29 +149,31 @@ std::pair<int, int> ControlViewSource::getRows() const
                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));
+       Row row;
+       row.begin = begrow;
+       row.end = endrow == nextendrow ? endrow : (nextendrow - 1);
+       return row;
 }
 
 
-docstring const ControlViewSource::title() const
+QString GuiViewSource::title() const
 {
        switch (docType()) {
                case LATEX:
-                       return _("LaTeX Source");
+                       return qt_("LaTeX Source");
                case DOCBOOK:
-                       return _("DocBook Source");
+                       return qt_("DocBook Source");
                case LITERATE:
-                       return _("Literate Source");
-               default:
-                       BOOST_ASSERT(false);
-                       return docstring();
+                       return qt_("Literate Source");
        }
+       BOOST_ASSERT(false);
+       return QString();
 }
 
 
-Dialog * createGuiViewSource(LyXView & lv)
+Dialog * createGuiViewSource(GuiView & lv)
 {
-       return new GuiViewSource(static_cast<GuiViewBase &>(lv));
+       return new GuiViewSource(lv);
 }