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 "GuiApplication.h"
16 #include "GuiViewSource.h"
17 #include "LaTeXHighlighter.h"
18 #include "qt_helpers.h"
20 #include "BufferView.h"
23 #include "support/gettext.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(guiApp->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());
75 GuiViewSource::Row row = controller_.getRows();
76 QTextCursor c = QTextCursor(viewSourceTV->document());
77 c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, row.begin);
78 c.select(QTextCursor::BlockUnderCursor);
79 c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
80 row.end - row.begin + 1);
81 viewSourceTV->setTextCursor(c);
85 void ViewSourceWidget::update(bool full_source)
87 document_->setPlainText(controller_.getContent(full_source));
91 GuiViewSource::GuiViewSource(GuiView & parent,
92 Qt::DockWidgetArea area, Qt::WindowFlags flags)
93 : DockView(parent, "view-source", area, flags)
95 widget_ = new ViewSourceWidget(*this);
97 setWindowTitle(widget_->windowTitle());
101 GuiViewSource::~GuiViewSource()
107 void GuiViewSource::updateView()
109 widget_->updateView();
113 bool GuiViewSource::initialiseParams(string const & /*source*/)
115 setWindowTitle(title());
120 QString GuiViewSource::getContent(bool fullSource)
122 // get the *top* level paragraphs that contain the cursor,
123 // or the selected text
127 BufferView * view = bufferview();
128 if (!view->cursor().selection()) {
129 par_begin = view->cursor().bottom().pit();
132 par_begin = view->cursor().selectionBegin().bottom().pit();
133 par_end = view->cursor().selectionEnd().bottom().pit();
135 if (par_begin > par_end)
136 std::swap(par_begin, par_end);
137 odocstringstream ostr;
138 view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
139 return toqstr(ostr.str());
143 GuiViewSource::Row GuiViewSource::getRows() const
145 BufferView const * view = bufferview();
146 CursorSlice beg = view->cursor().selectionBegin().bottom();
147 CursorSlice end = view->cursor().selectionEnd().bottom();
149 int begrow = view->buffer().texrow().
150 getRowFromIdPos(beg.paragraph().id(), beg.pos());
151 int endrow = view->buffer().texrow().
152 getRowFromIdPos(end.paragraph().id(), end.pos());
153 int nextendrow = view->buffer().texrow().
154 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
157 row.end = endrow == nextendrow ? endrow : (nextendrow - 1);
162 QString GuiViewSource::title() const
166 return qt_("LaTeX Source");
168 return qt_("DocBook Source");
170 return qt_("Literate Source");
177 Dialog * createGuiViewSource(GuiView & lv)
179 return new GuiViewSource(lv);
183 } // namespace frontend
186 #include "GuiViewSource_moc.cpp"