namespace lyx {
namespace frontend {
-ViewSourceWidget::ViewSourceWidget()
- : bv_(0), document_(new QTextDocument(this)),
- highlighter_(new LaTeXHighlighter(document_)),
- update_timer_(new QTimer(this))
+ViewSourceWidget::ViewSourceWidget(QWidget * parent)
+ : QWidget(parent),
+ document_(new QTextDocument(this)),
+ highlighter_(new LaTeXHighlighter(document_))
{
setupUi(this);
connect(masterPerspectiveCB, SIGNAL(toggled(bool)),
this, SLOT(contentsChanged()));
connect(updatePB, SIGNAL(clicked()),
- this, SLOT(updateViewNow()));
+ this, SIGNAL(needUpdate()));
connect(outputFormatCO, SIGNAL(activated(int)),
this, SLOT(setViewFormat(int)));
- connect(outputFormatCO, SIGNAL(activated(int)),
- this, SLOT(contentsChanged()));
-
- // setting the update timer
- update_timer_->setSingleShot(true);
- connect(update_timer_, SIGNAL(timeout()),
- this, SLOT(realUpdateView()));
// setting a document at this point trigger an assertion in Qt
// so we disable the signals here:
}
-void ViewSourceWidget::getContent(BufferView const * view,
+void ViewSourceWidget::getContent(BufferView const & view,
Buffer::OutputWhat output, docstring & str, string const & format,
bool master)
{
pit_type par_begin;
pit_type par_end;
- if (!view->cursor().selection()) {
- par_begin = view->cursor().bottom().pit();
+ 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();
+ 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;
- texrow_ = view->buffer()
+ texrow_ = view.buffer()
.getSourceCode(ostr, format, par_begin, par_end + 1, output, master);
//ensure that the last line can always be selected in its full width
str = ostr.str() + "\n";
}
-void ViewSourceWidget::setBufferView(BufferView const * bv)
-{
- if (bv_ != bv) {
- setText();
- bv_ = bv;
- }
- setEnabled(bv ? true : false);
-}
-
-
bool ViewSourceWidget::setText(QString const & qstr)
{
bool const changed = document_->toPlainText() != qstr;
void ViewSourceWidget::contentsChanged()
{
if (autoUpdateCB->isChecked())
- updateViewNow();
+ Q_EMIT needUpdate();
}
string format = fromqstr(outputFormatCO->itemData(index).toString());
if (view_format_ != format) {
view_format_ = format;
- // emit signal
- formatChanged();
+ Q_EMIT needUpdate();
}
}
-void ViewSourceWidget::updateView()
+int ViewSourceWidget::updateDelay() const
{
const int long_delay = 400;
const int short_delay = 60;
// a shorter delay if just the current paragraph is shown
- update_timer_->start((contentsCO->currentIndex() == 0) ?
- short_delay : long_delay);
+ return (contentsCO->currentIndex() == 0) ? short_delay : long_delay;
+}
+
+
+void GuiViewSource::scheduleUpdate()
+{
+ update_timer_->start(widget_->updateDelay());
}
-void ViewSourceWidget::updateViewNow()
+void GuiViewSource::scheduleUpdateNow()
{
update_timer_->start(0);
}
-void ViewSourceWidget::realUpdateView()
+void GuiViewSource::realUpdateView()
+{
+ widget_->updateView(bufferview());
+ updateTitle();
+}
+
+
+void ViewSourceWidget::updateView(BufferView const * bv)
{
- if (!bv_) {
+ if (!bv) {
setText();
setEnabled(false);
return;
output = Buffer::OnlyBody;
docstring content;
- getContent(bv_, output, content, view_format_,
+ getContent(*bv, output, content, view_format_,
masterPerspectiveCB->isChecked());
QString old = document_->toPlainText();
QString qcontent = toqstr(content);
}
#endif
// prevent gotoCursor()
- viewSourceTV->blockSignals(true);
+ QSignalBlocker blocker(viewSourceTV);
bool const changed = setText(qcontent);
if (changed && !texrow_) {
// position-to-row is unavailable
// we jump to the first modification
- const QChar * oc = old.constData();
- const QChar * nc = qcontent.constData();
+ int length = min(old.length(), qcontent.length());
int pos = 0;
- while (*oc != '\0' && *nc != '\0' && *oc == *nc) {
- ++oc;
- ++nc;
- ++pos;
- }
+ for (; pos < length && old.at(pos) == qcontent.at(pos); ++pos) {}
QTextCursor c = QTextCursor(viewSourceTV->document());
//get some space below the cursor
c.setPosition(pos);
} else if (texrow_) {
// Use the available position-to-row conversion to highlight
// the current selection in the source
- std::pair<int,int> rows = texrow_->rowFromCursor(bv_->cursor());
+ std::pair<int,int> rows = texrow_->rowFromCursor(bv->cursor());
int const beg_row = rows.first;
int const end_row = rows.second;
viewSourceTV->setTextCursor(c);
viewSourceTV->horizontalScrollBar()->setValue(h_scroll);
} // else if (texrow)
- viewSourceTV->blockSignals(false);
}
if (!texrow_)
return;
int row = viewSourceTV->textCursor().blockNumber() + 1;
- dispatch(TexRow::goToFunc(texrow_->getEntriesFromRow(row)));
+ dispatch(texrow_->goToFuncFromRow(row));
}
-void ViewSourceWidget::updateDefaultFormat()
+void ViewSourceWidget::updateDefaultFormat(BufferView const & bv)
{
- if (!bv_)
- return;
-
- outputFormatCO->blockSignals(true);
+ QSignalBlocker blocker(outputFormatCO);
outputFormatCO->clear();
outputFormatCO->addItem(qt_("Default"),
QVariant(QString("default")));
int index = 0;
- vector<string> tmp = bv_->buffer().params().backends();
+ 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) {
index = outputFormatCO->count() -1;
}
setViewFormat(index);
-
- outputFormatCO->blockSignals(false);
}
.toBool();
autoUpdateCB->setChecked(checked);
if (checked)
- updateView();
+ Q_EMIT needUpdate();
}
GuiViewSource::GuiViewSource(GuiView & parent,
Qt::DockWidgetArea area, Qt::WindowFlags flags)
- : DockView(parent, "view-source", qt_("Code Preview"), area, flags)
+ : DockView(parent, "view-source", qt_("Code Preview"), area, flags),
+ widget_(new ViewSourceWidget(this)),
+ update_timer_(new QTimer(this))
{
- widget_ = new ViewSourceWidget;
setWidget(widget_);
- connect(widget_, SIGNAL(formatChanged()), this, SLOT(updateTitle()));
+
+ // setting the update timer
+ update_timer_->setSingleShot(true);
+ connect(update_timer_, SIGNAL(timeout()),
+ this, SLOT(realUpdateView()));
+
+ connect(widget_, SIGNAL(needUpdate()), this, SLOT(scheduleUpdateNow()));
}
-GuiViewSource::~GuiViewSource()
+void GuiViewSource::onBufferViewChanged()
{
- delete widget_;
+ widget_->setText();
+ widget_->setEnabled((bool)bufferview());
}
void GuiViewSource::updateView()
{
if (widget_->autoUpdateCB->isChecked()) {
- widget_->setBufferView(bufferview());
- widget_->updateView();
+ widget_->setEnabled((bool)bufferview());
+ scheduleUpdate();
}
widget_->masterPerspectiveCB->setEnabled(buffer().parent());
updateTitle();
void GuiViewSource::enableView(bool enable)
{
- widget_->setBufferView(bufferview());
- widget_->updateDefaultFormat();
+ widget_->setEnabled((bool)bufferview());
+ if (bufferview())
+ widget_->updateDefaultFormat(*bufferview());
if (!enable)
// In the opposite case, updateView() will be called anyway.
widget_->contentsChanged();