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-11-18 20:36:52 +00:00
|
|
|
#include "GuiApplication.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-10-06 15:03:58 +00:00
|
|
|
#include "Buffer.h"
|
2011-05-13 19:39:56 +00:00
|
|
|
#include "BufferParams.h"
|
|
|
|
#include "BufferView.h"
|
2007-10-06 15:03:58 +00:00
|
|
|
#include "Cursor.h"
|
2010-12-10 10:34:24 +00:00
|
|
|
#include "Format.h"
|
2007-10-06 15:03:58 +00:00
|
|
|
#include "Paragraph.h"
|
|
|
|
#include "TexRow.h"
|
2007-08-13 15:22:13 +00:00
|
|
|
|
2008-10-18 13:51:48 +00:00
|
|
|
#include "support/debug.h"
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2007-11-01 22:17:22 +00:00
|
|
|
#include "support/docstream.h"
|
2007-11-29 22:38:53 +00:00
|
|
|
#include "support/gettext.h"
|
2007-11-01 22:17:22 +00:00
|
|
|
|
2008-10-18 14:14:58 +00:00
|
|
|
#include <boost/crc.hpp>
|
|
|
|
|
2013-05-05 06:05:51 +00:00
|
|
|
#include <QBoxLayout>
|
2008-05-14 14:24:55 +00:00
|
|
|
#include <QSettings>
|
2007-08-13 15:22:13 +00:00
|
|
|
#include <QTextCursor>
|
2007-04-24 14:08:53 +00:00
|
|
|
#include <QTextDocument>
|
2008-05-14 14:24:55 +00:00
|
|
|
#include <QVariant>
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-10-06 15:03:58 +00:00
|
|
|
|
2006-04-15 14:13:41 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2008-10-18 13:20:17 +00:00
|
|
|
ViewSourceWidget::ViewSourceWidget()
|
|
|
|
: bv_(0), document_(new QTextDocument(this)),
|
2011-01-04 00:00:20 +00:00
|
|
|
highlighter_(new LaTeXHighlighter(document_)),
|
|
|
|
force_getcontent_(true)
|
2007-04-24 14:08:53 +00:00
|
|
|
{
|
|
|
|
setupUi(this);
|
|
|
|
|
2011-10-29 21:00:23 +00:00
|
|
|
connect(contentsCO, SIGNAL(activated(int)),
|
|
|
|
this, SLOT(contentsChanged()));
|
2007-04-24 14:08:53 +00:00
|
|
|
connect(autoUpdateCB, SIGNAL(toggled(bool)),
|
|
|
|
updatePB, SLOT(setDisabled(bool)));
|
2009-04-01 20:32:26 +00:00
|
|
|
connect(autoUpdateCB, SIGNAL(toggled(bool)),
|
|
|
|
this, SLOT(updateView()));
|
2012-10-05 12:30:20 +00:00
|
|
|
connect(masterPerspectiveCB, SIGNAL(toggled(bool)),
|
|
|
|
this, SLOT(updateView()));
|
2007-04-24 14:08:53 +00:00
|
|
|
connect(updatePB, SIGNAL(clicked()),
|
2007-09-09 12:06:37 +00:00
|
|
|
this, SLOT(updateView()));
|
2010-12-10 10:34:24 +00:00
|
|
|
connect(outputFormatCO, SIGNAL(activated(int)),
|
2011-12-09 19:48:50 +00:00
|
|
|
this, SLOT(setViewFormat()));
|
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
|
2007-11-18 20:36:52 +00:00
|
|
|
QFont font(guiApp->typewriterFontName());
|
2007-04-24 14:08:53 +00:00
|
|
|
font.setKerning(false);
|
|
|
|
font.setFixedPitch(true);
|
|
|
|
font.setStyleHint(QFont::TypeWriter);
|
|
|
|
viewSourceTV->setFont(font);
|
|
|
|
// again, personal taste
|
|
|
|
viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-18 14:14:58 +00:00
|
|
|
static size_t crcCheck(docstring const & s)
|
|
|
|
{
|
|
|
|
boost::crc_32_type crc;
|
|
|
|
crc.process_bytes(&s[0], sizeof(char_type) * s.size());
|
|
|
|
return crc.checksum();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-18 13:20:17 +00:00
|
|
|
/** get the source code of selected paragraphs, or the whole document
|
|
|
|
\param fullSource get full source code
|
2008-10-18 14:14:58 +00:00
|
|
|
\return true if the content has changed since last call.
|
2008-10-18 13:20:17 +00:00
|
|
|
*/
|
2011-10-29 21:00:23 +00:00
|
|
|
static bool getContent(BufferView const * view, Buffer::OutputWhat output,
|
2012-10-05 12:30:20 +00:00
|
|
|
QString & qstr, string const format, bool force_getcontent,
|
|
|
|
bool master)
|
2008-10-18 13:20:17 +00:00
|
|
|
{
|
|
|
|
// get the *top* level paragraphs that contain the cursor,
|
|
|
|
// or the selected text
|
|
|
|
pit_type par_begin;
|
|
|
|
pit_type par_end;
|
|
|
|
|
|
|
|
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)
|
|
|
|
swap(par_begin, par_end);
|
|
|
|
odocstringstream ostr;
|
2012-10-05 12:30:20 +00:00
|
|
|
view->buffer().getSourceCode(ostr, format, par_begin, par_end + 1,
|
|
|
|
output, master);
|
2008-10-18 14:14:58 +00:00
|
|
|
docstring s = ostr.str();
|
|
|
|
static size_t crc = 0;
|
|
|
|
size_t newcrc = crcCheck(s);
|
2011-01-04 00:00:20 +00:00
|
|
|
if (newcrc == crc && !force_getcontent)
|
2008-10-18 14:14:58 +00:00
|
|
|
return false;
|
|
|
|
crc = newcrc;
|
|
|
|
qstr = toqstr(s);
|
|
|
|
return true;
|
2008-10-18 13:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ViewSourceWidget::setBufferView(BufferView const * bv)
|
|
|
|
{
|
2011-01-04 00:00:20 +00:00
|
|
|
if (bv_ != bv)
|
|
|
|
force_getcontent_ = true;
|
2008-10-18 13:20:17 +00:00
|
|
|
bv_ = bv;
|
2009-04-01 20:32:26 +00:00
|
|
|
setEnabled(bv ? true : false);
|
2008-10-18 13:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-29 21:00:23 +00:00
|
|
|
void ViewSourceWidget::contentsChanged()
|
2011-01-04 00:00:20 +00:00
|
|
|
{
|
|
|
|
if (autoUpdateCB->isChecked())
|
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-09 19:48:50 +00:00
|
|
|
void ViewSourceWidget::setViewFormat()
|
|
|
|
{
|
|
|
|
view_format_ = outputFormatCO->itemData(
|
|
|
|
outputFormatCO->currentIndex()).toString();
|
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
|
2012-03-10 16:49:20 +00:00
|
|
|
|
2007-10-09 09:43:56 +00:00
|
|
|
void ViewSourceWidget::updateView()
|
2007-04-24 14:08:53 +00:00
|
|
|
{
|
2008-10-18 13:20:17 +00:00
|
|
|
if (!bv_) {
|
2008-05-13 21:28:04 +00:00
|
|
|
document_->setPlainText(QString());
|
|
|
|
setEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-10 10:34:24 +00:00
|
|
|
|
2009-01-24 12:38:05 +00:00
|
|
|
setEnabled(true);
|
2007-04-24 14:08:53 +00:00
|
|
|
|
2011-12-09 19:48:50 +00:00
|
|
|
string const format = fromqstr(view_format_);
|
2010-12-10 10:34:24 +00:00
|
|
|
|
2008-10-18 14:14:58 +00:00
|
|
|
QString content;
|
2011-10-29 21:00:23 +00:00
|
|
|
Buffer::OutputWhat output = Buffer::CurrentParagraph;
|
|
|
|
if (contentsCO->currentIndex() == 1)
|
|
|
|
output = Buffer::FullSource;
|
|
|
|
else if (contentsCO->currentIndex() == 2)
|
|
|
|
output = Buffer::OnlyPreamble;
|
|
|
|
else if (contentsCO->currentIndex() == 3)
|
|
|
|
output = Buffer::OnlyBody;
|
|
|
|
|
2012-10-05 12:30:20 +00:00
|
|
|
if (getContent(bv_, output, content, format,
|
|
|
|
force_getcontent_, masterPerspectiveCB->isChecked()))
|
2008-10-18 14:14:58 +00:00
|
|
|
document_->setPlainText(content);
|
2008-10-18 13:20:17 +00:00
|
|
|
|
|
|
|
CursorSlice beg = bv_->cursor().selectionBegin().bottom();
|
|
|
|
CursorSlice end = bv_->cursor().selectionEnd().bottom();
|
|
|
|
int const begrow = bv_->buffer().texrow().
|
|
|
|
getRowFromIdPos(beg.paragraph().id(), beg.pos());
|
|
|
|
int endrow = bv_->buffer().texrow().
|
|
|
|
getRowFromIdPos(end.paragraph().id(), end.pos());
|
|
|
|
int const nextendrow = bv_->buffer().texrow().
|
|
|
|
getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
|
|
|
|
if (endrow != nextendrow)
|
|
|
|
endrow = nextendrow - 1;
|
|
|
|
|
2007-08-13 14:24:49 +00:00
|
|
|
QTextCursor c = QTextCursor(viewSourceTV->document());
|
2008-10-18 13:20:17 +00:00
|
|
|
c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, begrow);
|
2007-08-13 14:24:49 +00:00
|
|
|
c.select(QTextCursor::BlockUnderCursor);
|
2007-11-29 22:12:19 +00:00
|
|
|
c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
|
2008-10-18 13:20:17 +00:00
|
|
|
endrow - begrow + 1);
|
2007-08-13 14:24:49 +00:00
|
|
|
viewSourceTV->setTextCursor(c);
|
2007-04-24 14:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-10 10:34:24 +00:00
|
|
|
void ViewSourceWidget::updateDefaultFormat()
|
|
|
|
{
|
|
|
|
if (!bv_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
outputFormatCO->blockSignals(true);
|
|
|
|
outputFormatCO->clear();
|
|
|
|
outputFormatCO->addItem(qt_("Default"),
|
|
|
|
QVariant(QString("default")));
|
2011-12-09 19:48:50 +00:00
|
|
|
|
2012-03-10 16:49:20 +00:00
|
|
|
int index = 0;
|
2012-03-09 22:24:20 +00:00
|
|
|
vector<string> tmp = bv_->buffer().params().backends();
|
|
|
|
vector<string>::const_iterator it = tmp.begin();
|
|
|
|
vector<string>::const_iterator en = tmp.end();
|
|
|
|
for (; it != en; ++it) {
|
|
|
|
string const format = *it;
|
|
|
|
Format const * fmt = formats.getFormat(format);
|
2012-05-09 07:14:11 +00:00
|
|
|
if (!fmt) {
|
2012-03-09 22:24:20 +00:00
|
|
|
LYXERR0("Can't find format for backend " << format << "!");
|
2012-05-10 19:44:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-03-09 22:24:20 +00:00
|
|
|
|
2012-03-10 16:49:20 +00:00
|
|
|
QString const pretty = qt_(fmt->prettyname());
|
|
|
|
QString const qformat = toqstr(format);
|
|
|
|
outputFormatCO->addItem(pretty, QVariant(qformat));
|
|
|
|
if (qformat == view_format_)
|
|
|
|
index = outputFormatCO->count() -1;
|
2011-12-09 19:48:50 +00:00
|
|
|
}
|
2012-03-10 16:49:20 +00:00
|
|
|
outputFormatCO->setCurrentIndex(index);
|
2012-03-09 22:24:20 +00:00
|
|
|
|
2010-12-10 10:34:24 +00:00
|
|
|
outputFormatCO->blockSignals(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-06 21:50:23 +00:00
|
|
|
void ViewSourceWidget::resizeEvent (QResizeEvent * event)
|
2013-05-05 06:05:51 +00:00
|
|
|
{
|
2013-05-06 21:50:23 +00:00
|
|
|
QSize const & formSize = formLayout->sizeHint();
|
|
|
|
// minimize the size of the part that contains the buttons
|
|
|
|
if (width() * formSize.height() < height() * formSize.width()) {
|
2013-05-05 06:05:51 +00:00
|
|
|
layout_->setDirection(QBoxLayout::TopToBottom);
|
|
|
|
} else {
|
|
|
|
layout_->setDirection(QBoxLayout::LeftToRight);
|
|
|
|
}
|
2013-05-06 21:50:23 +00:00
|
|
|
QWidget::resizeEvent(event);
|
2013-05-05 06:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-29 22:12:19 +00:00
|
|
|
GuiViewSource::GuiViewSource(GuiView & parent,
|
|
|
|
Qt::DockWidgetArea area, Qt::WindowFlags flags)
|
2008-02-05 12:43:19 +00:00
|
|
|
: DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
|
2007-10-09 09:43:56 +00:00
|
|
|
{
|
2012-10-26 00:42:27 +00:00
|
|
|
widget_ = new ViewSourceWidget;
|
2007-10-09 09:43:56 +00:00
|
|
|
setWidget(widget_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GuiViewSource::~GuiViewSource()
|
|
|
|
{
|
|
|
|
delete widget_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiViewSource::updateView()
|
|
|
|
{
|
2009-04-01 20:32:26 +00:00
|
|
|
if (widget_->autoUpdateCB->isChecked()) {
|
2008-10-18 13:20:17 +00:00
|
|
|
widget_->setBufferView(bufferview());
|
2008-06-01 20:03:36 +00:00
|
|
|
widget_->updateView();
|
2008-10-18 13:20:17 +00:00
|
|
|
}
|
2012-10-05 12:30:20 +00:00
|
|
|
widget_->masterPerspectiveCB->setEnabled(buffer().parent());
|
2007-10-09 09:43:56 +00:00
|
|
|
}
|
2007-10-06 15:03:58 +00:00
|
|
|
|
|
|
|
|
2008-05-13 21:28:04 +00:00
|
|
|
void GuiViewSource::enableView(bool enable)
|
|
|
|
{
|
2009-01-24 12:38:05 +00:00
|
|
|
widget_->setBufferView(bufferview());
|
2010-12-10 10:34:24 +00:00
|
|
|
widget_->updateDefaultFormat();
|
2009-01-24 12:38:05 +00:00
|
|
|
if (!enable)
|
2008-05-13 21:28:04 +00:00
|
|
|
// In the opposite case, updateView() will be called anyway.
|
|
|
|
widget_->updateView();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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::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
|
|
|
}
|
2013-04-25 21:27:10 +00:00
|
|
|
LATTEST(false);
|
2007-10-09 09:43:56 +00:00
|
|
|
return QString();
|
2007-10-06 15:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-14 14:24:55 +00:00
|
|
|
void GuiViewSource::saveSession() const
|
|
|
|
{
|
|
|
|
Dialog::saveSession();
|
|
|
|
QSettings settings;
|
2011-10-29 21:00:23 +00:00
|
|
|
// see below
|
|
|
|
// settings.setValue(
|
|
|
|
// sessionKey() + "/output", widget_->contentsCO->currentIndex());
|
2008-05-14 14:24:55 +00:00
|
|
|
settings.setValue(
|
|
|
|
sessionKey() + "/autoupdate", widget_->autoUpdateCB->isChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiViewSource::restoreSession()
|
|
|
|
{
|
2008-10-03 13:34:10 +00:00
|
|
|
DockView::restoreSession();
|
2008-10-18 13:27:59 +00:00
|
|
|
// FIXME: Full source updating is too slow to be done at startup.
|
2011-10-29 21:00:23 +00:00
|
|
|
//widget_->outputCO-setCurrentIndex(
|
|
|
|
// settings.value(sessionKey() + "/output", false).toInt());
|
|
|
|
widget_->contentsCO->setCurrentIndex(0);
|
2008-05-14 14:24:55 +00:00
|
|
|
QSettings settings;
|
|
|
|
widget_->autoUpdateCB->setChecked(
|
2008-06-01 20:03:36 +00:00
|
|
|
settings.value(sessionKey() + "/autoupdate", true).toBool());
|
2009-01-24 12:38:05 +00:00
|
|
|
widget_->updateView();
|
2008-05-14 14:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-23 09:44:02 +00:00
|
|
|
Dialog * createGuiViewSource(GuiView & lv)
|
2007-10-06 15:03:58 +00:00
|
|
|
{
|
2007-11-29 22:12:19 +00:00
|
|
|
return new GuiViewSource(lv);
|
2007-10-06 15:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-15 14:13:41 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2006-05-18 08:51:12 +00:00
|
|
|
|
2008-11-14 14:28:50 +00:00
|
|
|
#include "moc_GuiViewSource.cpp"
|