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/Debug.h"
27 #include "support/lassert.h"
28 #include "support/docstream.h"
29 #include "support/gettext.h"
32 #include <QTextCursor>
33 #include <QTextDocument>
41 ViewSourceWidget::ViewSourceWidget()
42 : bv_(0), document_(new QTextDocument(this)),
43 highlighter_(new LaTeXHighlighter(document_))
47 connect(viewFullSourceCB, SIGNAL(clicked()),
48 this, SLOT(updateView()));
49 connect(autoUpdateCB, SIGNAL(toggled(bool)),
50 updatePB, SLOT(setDisabled(bool)));
51 connect(updatePB, SIGNAL(clicked()),
52 this, SLOT(updateView()));
54 // setting a document at this point trigger an assertion in Qt
55 // so we disable the signals here:
56 document_->blockSignals(true);
57 viewSourceTV->setDocument(document_);
58 document_->blockSignals(false);
59 viewSourceTV->setReadOnly(true);
60 ///dialog_->viewSourceTV->setAcceptRichText(false);
61 // this is personal. I think source code should be in fixed-size font
62 QFont font(guiApp->typewriterFontName());
63 font.setKerning(false);
64 font.setFixedPitch(true);
65 font.setStyleHint(QFont::TypeWriter);
66 viewSourceTV->setFont(font);
67 // again, personal taste
68 viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
72 /** get the source code of selected paragraphs, or the whole document
73 \param fullSource get full source code
75 static QString getContent(BufferView const * view, bool fullSource)
77 // get the *top* level paragraphs that contain the cursor,
78 // or the selected text
82 if (!view->cursor().selection()) {
83 par_begin = view->cursor().bottom().pit();
86 par_begin = view->cursor().selectionBegin().bottom().pit();
87 par_end = view->cursor().selectionEnd().bottom().pit();
89 if (par_begin > par_end)
90 swap(par_begin, par_end);
91 odocstringstream ostr;
92 view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
93 return toqstr(ostr.str());
97 void ViewSourceWidget::setBufferView(BufferView const * bv)
103 void ViewSourceWidget::updateView()
106 document_->setPlainText(QString());
111 document_->setPlainText(getContent(bv_, viewFullSourceCB->isChecked()));
113 CursorSlice beg = bv_->cursor().selectionBegin().bottom();
114 CursorSlice end = bv_->cursor().selectionEnd().bottom();
115 int const begrow = bv_->buffer().texrow().
116 getRowFromIdPos(beg.paragraph().id(), beg.pos());
117 int endrow = bv_->buffer().texrow().
118 getRowFromIdPos(end.paragraph().id(), end.pos());
119 int const nextendrow = bv_->buffer().texrow().
120 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
121 if (endrow != nextendrow)
122 endrow = nextendrow - 1;
124 QTextCursor c = QTextCursor(viewSourceTV->document());
125 c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, begrow);
126 c.select(QTextCursor::BlockUnderCursor);
127 c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
128 endrow - begrow + 1);
129 viewSourceTV->setTextCursor(c);
133 GuiViewSource::GuiViewSource(GuiView & parent,
134 Qt::DockWidgetArea area, Qt::WindowFlags flags)
135 : DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
137 widget_ = new ViewSourceWidget();
142 GuiViewSource::~GuiViewSource()
148 void GuiViewSource::updateView()
150 if (widget_->autoUpdateCB->isChecked()) {
151 widget_->setBufferView(bufferview());
152 widget_->updateView();
157 void GuiViewSource::enableView(bool enable)
160 // In the opposite case, updateView() will be called anyway.
161 widget_->setBufferView(bufferview());
162 widget_->updateView();
164 widget_->setEnabled(enable);
168 bool GuiViewSource::initialiseParams(string const & /*source*/)
170 setWindowTitle(title());
175 QString GuiViewSource::title() const
179 return qt_("LaTeX Source");
181 return qt_("DocBook Source");
183 return qt_("Literate Source");
185 LASSERT(false, /**/);
190 void GuiViewSource::saveSession() const
192 Dialog::saveSession();
195 sessionKey() + "/fullsource", widget_->viewFullSourceCB->isChecked());
197 sessionKey() + "/autoupdate", widget_->autoUpdateCB->isChecked());
201 void GuiViewSource::restoreSession()
203 DockView::restoreSession();
205 widget_->viewFullSourceCB->setChecked(
206 settings.value(sessionKey() + "/fullsource", false).toBool());
207 widget_->autoUpdateCB->setChecked(
208 settings.value(sessionKey() + "/autoupdate", true).toBool());
212 Dialog * createGuiViewSource(GuiView & lv)
214 return new GuiViewSource(lv);
218 } // namespace frontend
221 #include "GuiViewSource_moc.cpp"