2006-04-15 14:13:41 +00:00
|
|
|
/**
|
2007-08-31 05:53:55 +00:00
|
|
|
* \file GuiViewSource.cpp
|
2006-04-15 14:13:41 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
|
|
|
* \author Bo Peng
|
2007-03-25 01:25:29 +00:00
|
|
|
* \author Abdelrazak Younes
|
2006-04-15 14:13:41 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiViewSource.h"
|
2007-10-06 19:03:41 +00:00
|
|
|
#include "LaTeXHighlighter.h"
|
2006-04-15 14:13:41 +00:00
|
|
|
#include "qt_helpers.h"
|
2006-10-03 14:21:15 +00:00
|
|
|
|
2007-09-08 16:36:53 +00:00
|
|
|
#include "Application.h"
|
2007-10-06 15:03:58 +00:00
|
|
|
#include "BufferView.h"
|
|
|
|
#include "Buffer.h"
|
|
|
|
#include "Cursor.h"
|
|
|
|
#include "gettext.h"
|
|
|
|
#include "Paragraph.h"
|
|
|
|
#include "TexRow.h"
|
2007-08-13 15:22:13 +00:00
|
|
|
|
2007-11-01 22:17:22 +00:00
|
|
|
#include "support/docstream.h"
|
|
|
|
|
2007-08-13 15:22:13 +00:00
|
|
|
#include <QTextCursor>
|
2007-04-24 14:08:53 +00:00
|
|
|
#include <QTextDocument>
|
2007-09-08 19:22:41 +00:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2007-10-06 15:03:58 +00:00
|
|
|
using std::string;
|
|
|
|
|
2006-04-15 14:13:41 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2007-10-09 09:43:56 +00:00
|
|
|
ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller)
|
2007-09-08 16:04:25 +00:00
|
|
|
: controller_(controller), document_(new QTextDocument(this)),
|
2007-09-05 20:33:29 +00:00
|
|
|
highlighter_(new LaTeXHighlighter(document_))
|
2007-04-24 14:08:53 +00:00
|
|
|
{
|
|
|
|
setupUi(this);
|
2007-09-08 16:32:11 +00:00
|
|
|
setWindowTitle(qt_("LaTeX Source"));
|
2007-04-24 14:08:53 +00:00
|
|
|
|
|
|
|
connect(viewFullSourceCB, SIGNAL(clicked()),
|
2007-09-09 12:06:37 +00:00
|
|
|
this, SLOT(updateView()));
|
2007-04-24 14:08:53 +00:00
|
|
|
connect(autoUpdateCB, SIGNAL(toggled(bool)),
|
|
|
|
updatePB, SLOT(setDisabled(bool)));
|
|
|
|
connect(updatePB, SIGNAL(clicked()),
|
2007-09-09 12:06:37 +00:00
|
|
|
this, SLOT(updateView()));
|
2006-04-15 14:13:41 +00:00
|
|
|
|
2007-04-24 14:08:53 +00:00
|
|
|
// setting a document at this point trigger an assertion in Qt
|
|
|
|
// so we disable the signals here:
|
2007-10-09 09:43:56 +00:00
|
|
|
document_->blockSignals(true);
|
|
|
|
viewSourceTV->setDocument(document_);
|
|
|
|
document_->blockSignals(false);
|
2007-04-24 14:08:53 +00:00
|
|
|
viewSourceTV->setReadOnly(true);
|
|
|
|
///dialog_->viewSourceTV->setAcceptRichText(false);
|
|
|
|
// this is personal. I think source code should be in fixed-size font
|
|
|
|
QFont font(toqstr(theApp()->typewriterFontName()));
|
|
|
|
font.setKerning(false);
|
|
|
|
font.setFixedPitch(true);
|
|
|
|
font.setStyleHint(QFont::TypeWriter);
|
|
|
|
viewSourceTV->setFont(font);
|
|
|
|
// again, personal taste
|
|
|
|
viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-09 09:43:56 +00:00
|
|
|
void ViewSourceWidget::updateView()
|
2007-04-24 14:08:53 +00:00
|
|
|
{
|
|
|
|
if (autoUpdateCB->isChecked())
|
2007-09-05 20:33:29 +00:00
|
|
|
update(viewFullSourceCB->isChecked());
|
2007-04-24 14:08:53 +00:00
|
|
|
|
2007-08-13 14:24:49 +00:00
|
|
|
int beg, end;
|
2007-09-08 16:04:25 +00:00
|
|
|
boost::tie(beg, end) = controller_.getRows();
|
2007-08-13 14:24:49 +00:00
|
|
|
QTextCursor c = QTextCursor(viewSourceTV->document());
|
|
|
|
c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, beg);
|
|
|
|
c.select(QTextCursor::BlockUnderCursor);
|
|
|
|
c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, end - beg + 1);
|
|
|
|
viewSourceTV->setTextCursor(c);
|
2007-04-24 14:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-09 09:43:56 +00:00
|
|
|
void ViewSourceWidget::update(bool full_source)
|
2007-09-05 20:33:29 +00:00
|
|
|
{
|
2007-10-09 09:43:56 +00:00
|
|
|
document_->setPlainText(controller_.getContent(full_source));
|
2007-09-05 20:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-09 09:43:56 +00:00
|
|
|
GuiViewSource::GuiViewSource(GuiViewBase & parent, Qt::DockWidgetArea area, Qt::WindowFlags flags)
|
|
|
|
: DockView(parent, "view-source", area, flags)
|
|
|
|
{
|
|
|
|
widget_ = new ViewSourceWidget(*this);
|
|
|
|
setWidget(widget_);
|
|
|
|
setWindowTitle(widget_->windowTitle());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GuiViewSource::~GuiViewSource()
|
|
|
|
{
|
|
|
|
delete widget_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiViewSource::updateView()
|
|
|
|
{
|
|
|
|
widget_->updateView();
|
|
|
|
}
|
2007-10-06 15:03:58 +00:00
|
|
|
|
|
|
|
|
2007-10-09 09:43:56 +00:00
|
|
|
bool GuiViewSource::initialiseParams(string const & /*source*/)
|
2007-10-06 15:03:58 +00:00
|
|
|
{
|
2007-10-09 09:43:56 +00:00
|
|
|
setWindowTitle(title());
|
2007-10-06 15:03:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-09 09:43:56 +00:00
|
|
|
QString GuiViewSource::getContent(bool fullSource)
|
2007-10-06 15:03:58 +00:00
|
|
|
{
|
|
|
|
// get the *top* level paragraphs that contain the cursor,
|
|
|
|
// or the selected text
|
|
|
|
pit_type par_begin;
|
|
|
|
pit_type par_end;
|
|
|
|
|
|
|
|
BufferView * view = bufferview();
|
|
|
|
if (!view->cursor().selection()) {
|
|
|
|
par_begin = view->cursor().bottom().pit();
|
|
|
|
par_end = par_begin;
|
|
|
|
} else {
|
|
|
|
par_begin = view->cursor().selectionBegin().bottom().pit();
|
|
|
|
par_end = view->cursor().selectionEnd().bottom().pit();
|
|
|
|
}
|
|
|
|
if (par_begin > par_end)
|
|
|
|
std::swap(par_begin, par_end);
|
|
|
|
odocstringstream ostr;
|
|
|
|
view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
|
2007-10-09 09:43:56 +00:00
|
|
|
return toqstr(ostr.str());
|
2007-10-06 15:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-09 09:43:56 +00:00
|
|
|
std::pair<int, int> GuiViewSource::getRows() const
|
2007-10-06 15:03:58 +00:00
|
|
|
{
|
|
|
|
BufferView const * view = bufferview();
|
|
|
|
CursorSlice beg = view->cursor().selectionBegin().bottom();
|
|
|
|
CursorSlice end = view->cursor().selectionEnd().bottom();
|
|
|
|
|
|
|
|
int begrow = view->buffer().texrow().
|
|
|
|
getRowFromIdPos(beg.paragraph().id(), beg.pos());
|
|
|
|
int endrow = view->buffer().texrow().
|
|
|
|
getRowFromIdPos(end.paragraph().id(), end.pos());
|
|
|
|
int nextendrow = view->buffer().texrow().
|
|
|
|
getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
|
|
|
|
return std::make_pair(begrow, endrow == nextendrow ? endrow : (nextendrow - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-09 09:43:56 +00:00
|
|
|
QString GuiViewSource::title() const
|
2007-10-06 15:03:58 +00:00
|
|
|
{
|
|
|
|
switch (docType()) {
|
|
|
|
case LATEX:
|
2007-10-09 09:43:56 +00:00
|
|
|
return qt_("LaTeX Source");
|
2007-10-06 15:03:58 +00:00
|
|
|
case DOCBOOK:
|
2007-10-09 09:43:56 +00:00
|
|
|
return qt_("DocBook Source");
|
2007-10-06 15:03:58 +00:00
|
|
|
case LITERATE:
|
2007-10-09 09:43:56 +00:00
|
|
|
return qt_("Literate Source");
|
2007-10-06 15:03:58 +00:00
|
|
|
}
|
2007-10-09 09:43:56 +00:00
|
|
|
BOOST_ASSERT(false);
|
|
|
|
return QString();
|
2007-10-06 15:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Dialog * createGuiViewSource(LyXView & lv)
|
|
|
|
{
|
|
|
|
return new GuiViewSource(static_cast<GuiViewBase &>(lv));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-15 14:13:41 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2006-05-18 08:51:12 +00:00
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiViewSource_moc.cpp"
|