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 "support/docstream.h"
29 #include <QTextCursor>
30 #include <QTextDocument>
31 #include <boost/tuple/tuple.hpp>
38 ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller)
39 : controller_(controller), document_(new QTextDocument(this)),
40 highlighter_(new LaTeXHighlighter(document_))
43 setWindowTitle(qt_("LaTeX Source"));
45 connect(viewFullSourceCB, SIGNAL(clicked()),
46 this, SLOT(updateView()));
47 connect(autoUpdateCB, SIGNAL(toggled(bool)),
48 updatePB, SLOT(setDisabled(bool)));
49 connect(updatePB, SIGNAL(clicked()),
50 this, SLOT(updateView()));
52 // setting a document at this point trigger an assertion in Qt
53 // so we disable the signals here:
54 document_->blockSignals(true);
55 viewSourceTV->setDocument(document_);
56 document_->blockSignals(false);
57 viewSourceTV->setReadOnly(true);
58 ///dialog_->viewSourceTV->setAcceptRichText(false);
59 // this is personal. I think source code should be in fixed-size font
60 QFont font(toqstr(theApp()->typewriterFontName()));
61 font.setKerning(false);
62 font.setFixedPitch(true);
63 font.setStyleHint(QFont::TypeWriter);
64 viewSourceTV->setFont(font);
65 // again, personal taste
66 viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
70 void ViewSourceWidget::updateView()
72 if (autoUpdateCB->isChecked())
73 update(viewFullSourceCB->isChecked());
76 boost::tie(beg, end) = controller_.getRows();
77 QTextCursor c = QTextCursor(viewSourceTV->document());
78 c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, beg);
79 c.select(QTextCursor::BlockUnderCursor);
80 c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, end - beg + 1);
81 viewSourceTV->setTextCursor(c);
85 void ViewSourceWidget::update(bool full_source)
87 document_->setPlainText(controller_.getContent(full_source));
91 GuiViewSource::GuiViewSource(GuiView & parent, Qt::DockWidgetArea area, Qt::WindowFlags flags)
92 : DockView(parent, "view-source", area, flags)
94 widget_ = new ViewSourceWidget(*this);
96 setWindowTitle(widget_->windowTitle());
100 GuiViewSource::~GuiViewSource()
106 void GuiViewSource::updateView()
108 widget_->updateView();
112 bool GuiViewSource::initialiseParams(string const & /*source*/)
114 setWindowTitle(title());
119 QString GuiViewSource::getContent(bool fullSource)
121 // get the *top* level paragraphs that contain the cursor,
122 // or the selected text
126 BufferView * view = bufferview();
127 if (!view->cursor().selection()) {
128 par_begin = view->cursor().bottom().pit();
131 par_begin = view->cursor().selectionBegin().bottom().pit();
132 par_end = view->cursor().selectionEnd().bottom().pit();
134 if (par_begin > par_end)
135 std::swap(par_begin, par_end);
136 odocstringstream ostr;
137 view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
138 return toqstr(ostr.str());
142 std::pair<int, int> GuiViewSource::getRows() const
144 BufferView const * view = bufferview();
145 CursorSlice beg = view->cursor().selectionBegin().bottom();
146 CursorSlice end = view->cursor().selectionEnd().bottom();
148 int begrow = view->buffer().texrow().
149 getRowFromIdPos(beg.paragraph().id(), beg.pos());
150 int endrow = view->buffer().texrow().
151 getRowFromIdPos(end.paragraph().id(), end.pos());
152 int nextendrow = view->buffer().texrow().
153 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
154 return std::make_pair(begrow, endrow == nextendrow ? endrow : (nextendrow - 1));
158 QString GuiViewSource::title() const
162 return qt_("LaTeX Source");
164 return qt_("DocBook Source");
166 return qt_("Literate Source");
173 Dialog * createGuiViewSource(LyXView & lv)
175 return new GuiViewSource(static_cast<GuiView &>(lv));
179 } // namespace frontend
182 #include "GuiViewSource_moc.cpp"