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/lassert.h"
27 #include "support/docstream.h"
28 #include "support/gettext.h"
31 #include <QTextCursor>
32 #include <QTextDocument>
40 ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller)
41 : controller_(controller), document_(new QTextDocument(this)),
42 highlighter_(new LaTeXHighlighter(document_))
46 connect(viewFullSourceCB, SIGNAL(clicked()),
47 this, SLOT(updateView()));
48 connect(autoUpdateCB, SIGNAL(toggled(bool)),
49 updatePB, SLOT(setDisabled(bool)));
50 connect(updatePB, SIGNAL(clicked()),
51 this, SLOT(updateView()));
53 // setting a document at this point trigger an assertion in Qt
54 // so we disable the signals here:
55 document_->blockSignals(true);
56 viewSourceTV->setDocument(document_);
57 document_->blockSignals(false);
58 viewSourceTV->setReadOnly(true);
59 ///dialog_->viewSourceTV->setAcceptRichText(false);
60 // this is personal. I think source code should be in fixed-size font
61 QFont font(guiApp->typewriterFontName());
62 font.setKerning(false);
63 font.setFixedPitch(true);
64 font.setStyleHint(QFont::TypeWriter);
65 viewSourceTV->setFont(font);
66 // again, personal taste
67 viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
71 void ViewSourceWidget::updateView()
73 BufferView * view = controller_.bufferview();
75 document_->setPlainText(QString());
79 if (autoUpdateCB->isChecked())
80 document_->setPlainText(controller_.getContent(
81 viewFullSourceCB->isChecked()));
83 GuiViewSource::Row row = controller_.getRows();
84 QTextCursor c = QTextCursor(viewSourceTV->document());
85 c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, row.begin);
86 c.select(QTextCursor::BlockUnderCursor);
87 c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
88 row.end - row.begin + 1);
89 viewSourceTV->setTextCursor(c);
93 GuiViewSource::GuiViewSource(GuiView & parent,
94 Qt::DockWidgetArea area, Qt::WindowFlags flags)
95 : DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
97 widget_ = new ViewSourceWidget(*this);
102 GuiViewSource::~GuiViewSource()
108 void GuiViewSource::updateView()
110 widget_->updateView();
114 void GuiViewSource::enableView(bool enable)
117 // In the opposite case, updateView() will be called anyway.
118 widget_->updateView();
119 widget_->setEnabled(enable);
123 bool GuiViewSource::initialiseParams(string const & /*source*/)
125 setWindowTitle(title());
130 QString GuiViewSource::getContent(bool fullSource)
132 // get the *top* level paragraphs that contain the cursor,
133 // or the selected text
137 BufferView * view = bufferview();
138 if (!view->cursor().selection()) {
139 par_begin = view->cursor().bottom().pit();
142 par_begin = view->cursor().selectionBegin().bottom().pit();
143 par_end = view->cursor().selectionEnd().bottom().pit();
145 if (par_begin > par_end)
146 swap(par_begin, par_end);
147 odocstringstream ostr;
148 view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
149 return toqstr(ostr.str());
153 GuiViewSource::Row GuiViewSource::getRows() const
155 BufferView const * view = bufferview();
156 CursorSlice beg = view->cursor().selectionBegin().bottom();
157 CursorSlice end = view->cursor().selectionEnd().bottom();
159 int begrow = view->buffer().texrow().
160 getRowFromIdPos(beg.paragraph().id(), beg.pos());
161 int endrow = view->buffer().texrow().
162 getRowFromIdPos(end.paragraph().id(), end.pos());
163 int nextendrow = view->buffer().texrow().
164 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
167 row.end = endrow == nextendrow ? endrow : (nextendrow - 1);
172 QString GuiViewSource::title() const
176 return qt_("LaTeX Source");
178 return qt_("DocBook Source");
180 return qt_("Literate Source");
182 LASSERT(false, /**/);
187 void GuiViewSource::saveSession() const
189 Dialog::saveSession();
192 sessionKey() + "/fullsource", widget_->viewFullSourceCB->isChecked());
194 sessionKey() + "/autoupdate", widget_->autoUpdateCB->isChecked());
198 void GuiViewSource::restoreSession()
200 Dialog::restoreSession();
202 widget_->viewFullSourceCB->setChecked(
203 settings.value(sessionKey() + "/fullsource").toBool());
204 widget_->autoUpdateCB->setChecked(
205 settings.value(sessionKey() + "/autoupdate").toBool());
209 Dialog * createGuiViewSource(GuiView & lv)
211 return new GuiViewSource(lv);
215 } // namespace frontend
218 #include "GuiViewSource_moc.cpp"