2 * \file GuiViewSource.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiViewSource.h"
16 #include "LaTeXHighlighter.h"
17 #include "qt_helpers.h"
19 #include "Application.h"
20 #include "BufferView.h"
24 #include "Paragraph.h"
27 #include <QTextCursor>
28 #include <QTextDocument>
29 #include <boost/tuple/tuple.hpp>
36 GuiViewSourceDialog::GuiViewSourceDialog(ControlViewSource & controller)
37 : controller_(controller), document_(new QTextDocument(this)),
38 highlighter_(new LaTeXHighlighter(document_))
41 setWindowTitle(qt_("LaTeX Source"));
43 connect(viewFullSourceCB, SIGNAL(clicked()),
44 this, SLOT(updateView()));
45 connect(autoUpdateCB, SIGNAL(toggled(bool)),
46 updatePB, SLOT(setDisabled(bool)));
47 connect(updatePB, SIGNAL(clicked()),
48 this, SLOT(updateView()));
50 // setting a document at this point trigger an assertion in Qt
51 // so we disable the signals here:
52 document()->blockSignals(true);
53 viewSourceTV->setDocument(document());
54 document()->blockSignals(false);
55 viewSourceTV->setReadOnly(true);
56 ///dialog_->viewSourceTV->setAcceptRichText(false);
57 // this is personal. I think source code should be in fixed-size font
58 QFont font(toqstr(theApp()->typewriterFontName()));
59 font.setKerning(false);
60 font.setFixedPitch(true);
61 font.setStyleHint(QFont::TypeWriter);
62 viewSourceTV->setFont(font);
63 // again, personal taste
64 viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
68 void GuiViewSourceDialog::updateView()
70 if (autoUpdateCB->isChecked())
71 update(viewFullSourceCB->isChecked());
74 boost::tie(beg, end) = controller_.getRows();
75 QTextCursor c = QTextCursor(viewSourceTV->document());
76 c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, beg);
77 c.select(QTextCursor::BlockUnderCursor);
78 c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, end - beg + 1);
79 viewSourceTV->setTextCursor(c);
84 void GuiViewSourceDialog::update(bool full_source)
86 document_->setPlainText(toqstr(controller_.updateContent(full_source)));
90 ControlViewSource::ControlViewSource(Dialog & parent)
95 bool ControlViewSource::initialiseParams(string const & /*source*/)
101 docstring const ControlViewSource::updateContent(bool fullSource)
103 // get the *top* level paragraphs that contain the cursor,
104 // or the selected text
108 BufferView * view = bufferview();
109 if (!view->cursor().selection()) {
110 par_begin = view->cursor().bottom().pit();
113 par_begin = view->cursor().selectionBegin().bottom().pit();
114 par_end = view->cursor().selectionEnd().bottom().pit();
116 if (par_begin > par_end)
117 std::swap(par_begin, par_end);
118 odocstringstream ostr;
119 view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
124 std::pair<int, int> ControlViewSource::getRows() const
126 BufferView const * view = bufferview();
127 CursorSlice beg = view->cursor().selectionBegin().bottom();
128 CursorSlice end = view->cursor().selectionEnd().bottom();
130 int begrow = view->buffer().texrow().
131 getRowFromIdPos(beg.paragraph().id(), beg.pos());
132 int endrow = view->buffer().texrow().
133 getRowFromIdPos(end.paragraph().id(), end.pos());
134 int nextendrow = view->buffer().texrow().
135 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
136 return std::make_pair(begrow, endrow == nextendrow ? endrow : (nextendrow - 1));
140 docstring const ControlViewSource::title() const
144 return _("LaTeX Source");
146 return _("DocBook Source");
148 return _("Literate Source");
156 Dialog * createGuiViewSource(LyXView & lv)
158 return new GuiViewSource(static_cast<GuiViewBase &>(lv));
162 } // namespace frontend
165 #include "GuiViewSource_moc.cpp"