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 "Paragraph.h"
26 #include "support/docstream.h"
27 #include "support/gettext.h"
29 #include <QTextCursor>
30 #include <QTextDocument>
37 ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller)
38 : controller_(controller), document_(new QTextDocument(this)),
39 highlighter_(new LaTeXHighlighter(document_))
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(guiApp->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 ViewSourceWidget::updateView()
70 if (autoUpdateCB->isChecked())
71 update(viewFullSourceCB->isChecked());
73 GuiViewSource::Row row = controller_.getRows();
74 QTextCursor c = QTextCursor(viewSourceTV->document());
75 c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, row.begin);
76 c.select(QTextCursor::BlockUnderCursor);
77 c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
78 row.end - row.begin + 1);
79 viewSourceTV->setTextCursor(c);
83 void ViewSourceWidget::update(bool full_source)
85 document_->setPlainText(controller_.getContent(full_source));
89 GuiViewSource::GuiViewSource(GuiView & parent,
90 Qt::DockWidgetArea area, Qt::WindowFlags flags)
91 : DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
93 widget_ = new ViewSourceWidget(*this);
98 GuiViewSource::~GuiViewSource()
104 void GuiViewSource::updateView()
106 widget_->updateView();
110 bool GuiViewSource::initialiseParams(string const & /*source*/)
112 setWindowTitle(title());
117 QString GuiViewSource::getContent(bool fullSource)
119 // get the *top* level paragraphs that contain the cursor,
120 // or the selected text
124 BufferView * view = bufferview();
125 if (!view->cursor().selection()) {
126 par_begin = view->cursor().bottom().pit();
129 par_begin = view->cursor().selectionBegin().bottom().pit();
130 par_end = view->cursor().selectionEnd().bottom().pit();
132 if (par_begin > par_end)
133 swap(par_begin, par_end);
134 odocstringstream ostr;
135 view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
136 return toqstr(ostr.str());
140 GuiViewSource::Row GuiViewSource::getRows() const
142 BufferView const * view = bufferview();
143 CursorSlice beg = view->cursor().selectionBegin().bottom();
144 CursorSlice end = view->cursor().selectionEnd().bottom();
146 int begrow = view->buffer().texrow().
147 getRowFromIdPos(beg.paragraph().id(), beg.pos());
148 int endrow = view->buffer().texrow().
149 getRowFromIdPos(end.paragraph().id(), end.pos());
150 int nextendrow = view->buffer().texrow().
151 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
154 row.end = endrow == nextendrow ? endrow : (nextendrow - 1);
159 QString GuiViewSource::title() const
163 return qt_("LaTeX Source");
165 return qt_("DocBook Source");
167 return qt_("Literate Source");
174 Dialog * createGuiViewSource(GuiView & lv)
176 return new GuiViewSource(lv);
180 } // namespace frontend
183 #include "GuiViewSource_moc.cpp"