git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26940 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2008-10-18 13:20:17 +00:00
parent 496ee475f0
commit bcbf7c6452
2 changed files with 81 additions and 84 deletions

View File

@ -23,6 +23,7 @@
#include "Paragraph.h"
#include "TexRow.h"
#include "support/Debug.h"
#include "support/lassert.h"
#include "support/docstream.h"
#include "support/gettext.h"
@ -37,8 +38,8 @@ using namespace std;
namespace lyx {
namespace frontend {
ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller)
: controller_(controller), document_(new QTextDocument(this)),
ViewSourceWidget::ViewSourceWidget()
: bv_(0), document_(new QTextDocument(this)),
highlighter_(new LaTeXHighlighter(document_))
{
setupUi(this);
@ -68,73 +69,16 @@ ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller)
}
void ViewSourceWidget::updateView()
{
BufferView const * view = controller_.bufferview();
if (!view) {
document_->setPlainText(QString());
setEnabled(false);
return;
}
document_->setPlainText(controller_.getContent(
viewFullSourceCB->isChecked()));
GuiViewSource::Row row = controller_.getRows();
QTextCursor c = QTextCursor(viewSourceTV->document());
c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, row.begin);
c.select(QTextCursor::BlockUnderCursor);
c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
row.end - row.begin + 1);
viewSourceTV->setTextCursor(c);
}
GuiViewSource::GuiViewSource(GuiView & parent,
Qt::DockWidgetArea area, Qt::WindowFlags flags)
: DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
{
widget_ = new ViewSourceWidget(*this);
setWidget(widget_);
}
GuiViewSource::~GuiViewSource()
{
delete widget_;
}
void GuiViewSource::updateView()
{
if (widget_->autoUpdateCB->isChecked())
widget_->updateView();
}
void GuiViewSource::enableView(bool enable)
{
if (!enable)
// In the opposite case, updateView() will be called anyway.
widget_->updateView();
widget_->setEnabled(enable);
}
bool GuiViewSource::initialiseParams(string const & /*source*/)
{
setWindowTitle(title());
return true;
}
QString GuiViewSource::getContent(bool fullSource)
/** get the source code of selected paragraphs, or the whole document
\param fullSource get full source code
*/
static QString getContent(BufferView const * view, bool fullSource)
{
// get the *top* level paragraphs that contain the cursor,
// or the selected text
pit_type par_begin;
pit_type par_end;
BufferView const * view = bufferview();
if (!view->cursor().selection()) {
par_begin = view->cursor().bottom().pit();
par_end = par_begin;
@ -150,22 +94,81 @@ QString GuiViewSource::getContent(bool fullSource)
}
GuiViewSource::Row GuiViewSource::getRows() const
void ViewSourceWidget::setBufferView(BufferView const * bv)
{
BufferView const * view = bufferview();
CursorSlice beg = view->cursor().selectionBegin().bottom();
CursorSlice end = view->cursor().selectionEnd().bottom();
bv_ = bv;
}
int begrow = view->buffer().texrow().
void ViewSourceWidget::updateView()
{
if (!bv_) {
document_->setPlainText(QString());
setEnabled(false);
return;
}
document_->setPlainText(getContent(bv_, viewFullSourceCB->isChecked()));
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 = view->buffer().texrow().
int endrow = bv_->buffer().texrow().
getRowFromIdPos(end.paragraph().id(), end.pos());
int nextendrow = view->buffer().texrow().
int const nextendrow = bv_->buffer().texrow().
getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
Row row;
row.begin = begrow;
row.end = endrow == nextendrow ? endrow : (nextendrow - 1);
return row;
if (endrow != nextendrow)
endrow = nextendrow - 1;
QTextCursor c = QTextCursor(viewSourceTV->document());
c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, begrow);
c.select(QTextCursor::BlockUnderCursor);
c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
endrow - begrow + 1);
viewSourceTV->setTextCursor(c);
}
GuiViewSource::GuiViewSource(GuiView & parent,
Qt::DockWidgetArea area, Qt::WindowFlags flags)
: DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
{
widget_ = new ViewSourceWidget();
setWidget(widget_);
}
GuiViewSource::~GuiViewSource()
{
delete widget_;
}
void GuiViewSource::updateView()
{
if (widget_->autoUpdateCB->isChecked()) {
widget_->setBufferView(bufferview());
widget_->updateView();
}
}
void GuiViewSource::enableView(bool enable)
{
if (!enable) {
// In the opposite case, updateView() will be called anyway.
widget_->setBufferView(bufferview());
widget_->updateView();
}
widget_->setEnabled(enable);
}
bool GuiViewSource::initialiseParams(string const & /*source*/)
{
setWindowTitle(title());
return true;
}

View File

@ -37,7 +37,9 @@ class ViewSourceWidget : public QWidget, public Ui::ViewSourceUi
Q_OBJECT
public:
ViewSourceWidget(GuiViewSource &);
ViewSourceWidget();
///
void setBufferView(BufferView const * bv);
public Q_SLOTS:
// update content
@ -45,7 +47,7 @@ public Q_SLOTS:
private:
///
GuiViewSource & controller_;
BufferView const * bv_;
///
QTextDocument * document_;
/// LaTeX syntax highlighter
@ -83,14 +85,6 @@ public:
/// The title displayed by the dialog reflects source type.
QString title() const;
/** get the source code of selected paragraphs, or the whole document
\param fullSource get full source code
*/
QString getContent(bool fullSource);
// cursor position in the source code
struct Row { int begin; int end; };
Row getRows() const;
private:
/// The encapsulated widget.
ViewSourceWidget * widget_;