From 24fdfc7d5e6c8f85dcb1ed83ec42a53d40776a52 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Sat, 9 Feb 2008 15:23:05 +0000 Subject: [PATCH] start work on less-likely-to-misuse iterators. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22898 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 43 ++++++------------------ src/Buffer.h | 4 +-- src/BufferView.cpp | 47 ++++++++++++++------------- src/Cursor.cpp | 3 +- src/DocIterator.h | 10 ++++-- src/ParIterator.cpp | 9 ----- src/ParIterator.h | 2 -- src/frontends/qt4/GuiErrorList.cpp | 11 +++---- src/frontends/qt4/GuiSpellchecker.cpp | 4 +-- 9 files changed, 52 insertions(+), 81 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 5e00debe35..7858c42ce8 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -1485,60 +1485,37 @@ bool Buffer::isMultiLingual() const } -ParConstIterator Buffer::getParFromID(int const id) const +DocIterator Buffer::getParFromID(int const id) const { - ParConstIterator it = par_iterator_begin(); - ParConstIterator const end = par_iterator_end(); - - if (id < 0) { - // John says this is called with id == -1 from undo - lyxerr << "getParFromID(), id: " << id << endl; - return end; - } - - for (; it != end; ++it) - if (it->id() == id) - return it; - - return end; -} - - -ParIterator Buffer::getParFromID(int const id) -{ - ParIterator it = par_iterator_begin(); - ParIterator const end = par_iterator_end(); - if (id < 0) { // John says this is called with id == -1 from undo lyxerr << "getParFromID(), id: " << id << endl; - return end; + return doc_iterator_end(inset()); } - for (; it != end; ++it) - if (it->id() == id) + for (DocIterator it = doc_iterator_begin(inset()); !it.atEnd(); it.forwardPar()) + if (it.paragraph().id() == id) return it; - return end; + return doc_iterator_end(inset()); } bool Buffer::hasParWithID(int const id) const { - ParConstIterator const it = getParFromID(id); - return it != par_iterator_end(); + return !getParFromID(id).atEnd(); } ParIterator Buffer::par_iterator_begin() { - return lyx::par_iterator_begin(inset()); + return ParIterator(doc_iterator_begin(inset())); } ParIterator Buffer::par_iterator_end() { - return lyx::par_iterator_end(inset()); + return ParIterator(doc_iterator_end(inset())); } @@ -2010,8 +1987,8 @@ void Buffer::updateMacroInstances() const { LYXERR(Debug::MACROS, "updateMacroInstances for " << d->filename.onlyFileName()); - ParConstIterator it = par_iterator_begin(); - ParConstIterator end = par_iterator_end(); + DocIterator it = doc_iterator_begin(inset()); + DocIterator end = doc_iterator_end(inset()); for (; it != end; it.forwardPos()) { // look for MathData cells in InsetMathNest insets Inset * inset = it.nextInset(); diff --git a/src/Buffer.h b/src/Buffer.h index 3ec296080c..7bfc8c35fa 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -144,9 +144,7 @@ public: pit_type &, pos_type &, Font const &, docstring const &, bool); /// - ParIterator getParFromID(int id); - /// - ParConstIterator getParFromID(int id) const; + DocIterator getParFromID(int id) const; /// do we have a paragraph with this id? bool hasParWithID(int id) const; diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 5870b0476c..4c3ab44f65 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -429,7 +429,7 @@ void BufferView::updateScrollbar() << " curr par: " << d->cursor_.bottom().pit() << " default height " << defaultRowHeight()); - int const parsize = int(t.paragraphs().size()); + size_t const parsize = t.paragraphs().size(); if (d->par_height_.size() != parsize) { d->par_height_.clear(); // FIXME: We assume a default paragraph height of 2 rows. This @@ -449,7 +449,7 @@ void BufferView::updateScrollbar() int top_pos = first.second->position() - first.second->ascent(); int bottom_pos = last.second->position() + last.second->descent(); bool first_visible = first.first == 0 && top_pos >= 0; - bool last_visible = last.first == parsize - 1 && bottom_pos <= height_; + bool last_visible = last.first + 1 == int(parsize) && bottom_pos <= height_; if (first_visible && last_visible) { d->scrollbarParameters_.min = 0; d->scrollbarParameters_.max = 0; @@ -457,7 +457,7 @@ void BufferView::updateScrollbar() } d->scrollbarParameters_.min = top_pos; - for (size_t i = 0; i != first.first; ++i) + for (size_t i = 0; i != size_t(first.first); ++i) d->scrollbarParameters_.min -= d->par_height_[i]; d->scrollbarParameters_.max = bottom_pos; for (size_t i = last.first + 1; i != parsize; ++i) @@ -527,7 +527,7 @@ void BufferView::scrollDocView(int value) // find paragraph at target position int par_pos = d->scrollbarParameters_.min; pit_type i = 0; - for (; i != d->par_height_.size(); ++i) { + for (; i != int(d->par_height_.size()); ++i) { par_pos += d->par_height_[i]; if (par_pos >= value) break; @@ -631,7 +631,7 @@ bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos, int top_id, pos_type top_pos) { bool success = false; - DocIterator doc_it; + DocIterator dit; d->cursor_.clearSelection(); @@ -639,19 +639,19 @@ bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos, // This is the case for a 'live' bookmark when unique paragraph ID // is used to track bookmarks. if (top_id > 0) { - ParIterator par = buffer_.getParFromID(top_id); - if (par != buffer_.par_iterator_end()) { - doc_it = makeDocIterator(par, min(par->size(), top_pos)); + dit = buffer_.getParFromID(top_id); + if (!dit.atEnd()) { + dit.pos() = min(dit.paragraph().size(), top_pos); // Some slices of the iterator may not be // reachable (e.g. closed collapsable inset) // so the dociterator may need to be // shortened. Otherwise, setCursor may crash // lyx when the cursor can not be set to these // insets. - size_t const n = doc_it.depth(); + size_t const n = dit.depth(); for (size_t i = 0; i < n; ++i) - if (doc_it[i].inset().editable() != Inset::HIGHLY_EDITABLE) { - doc_it.resize(i); + if (dit[i].inset().editable() != Inset::HIGHLY_EDITABLE) { + dit.resize(i); break; } success = true; @@ -664,16 +664,17 @@ bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos, // restoration is inaccurate. If a bookmark was within an inset, // it will be restored to the left of the outmost inset that contains // the bookmark. - if (static_cast(bottom_pit) < buffer_.paragraphs().size()) { - doc_it = doc_iterator_begin(buffer_.inset()); - doc_it.pit() = bottom_pit; - doc_it.pos() = min(bottom_pos, doc_it.paragraph().size()); + if (bottom_pit < int(buffer_.paragraphs().size())) { + dit = doc_iterator_begin(buffer_.inset()); + + dit.pit() = bottom_pit; + dit.pos() = min(bottom_pos, dit.paragraph().size()); success = true; } if (success) { // Note: only bottom (document) level pit is set. - setCursor(doc_it); + setCursor(dit); // set the current font. d->cursor_.setCurrentFont(); // To center the screen on this new position we need the @@ -997,17 +998,17 @@ bool BufferView::dispatch(FuncRequest const & cmd) for (Buffer * b = &buffer_; i == 0 || b != &buffer_; b = theBufferList().next(b)) { - ParIterator par = b->getParFromID(id); - if (par == b->par_iterator_end()) { + DocIterator dit = b->getParFromID(id); + if (dit.atEnd()) { LYXERR(Debug::INFO, "No matching paragraph found! [" << id << "]."); } else { - LYXERR(Debug::INFO, "Paragraph " << par->id() + LYXERR(Debug::INFO, "Paragraph " << dit.paragraph().id() << " found in buffer `" << b->absFileName() << "'."); if (b == &buffer_) { // Set the cursor - setCursor(makeDocIterator(par, 0)); + setCursor(dit); showCursor(); } else { // Switch to other buffer view and resend cmd @@ -1672,9 +1673,9 @@ bool BufferView::mouseSetCursor(Cursor & cur, bool select) // For an example, see bug 2933: // http://bugzilla.lyx.org/show_bug.cgi?id=2933 // The code below could maybe be moved to a DocIterator method. - //lyxerr << "cur before " << cur <size()) - : pit->size(); + pos_type const s = dit.paragraph().size(); + pos_type const end = err.pos_end ? min(err.pos_end, s) : s; pos_type const start = min(err.pos_start, end); pos_type const range = end - start; - DocIterator const dit = makeDocIterator(pit, start); + dit.pos() = start; bufferview()->putSelectionAt(dit, range, false); // FIXME: If we used an LFUN, we would not need this line: bufferview()->processUpdateFlags(Update::Force | Update::FitCursor); diff --git a/src/frontends/qt4/GuiSpellchecker.cpp b/src/frontends/qt4/GuiSpellchecker.cpp index 86d628c77a..9c4ad163cc 100644 --- a/src/frontends/qt4/GuiSpellchecker.cpp +++ b/src/frontends/qt4/GuiSpellchecker.cpp @@ -333,8 +333,8 @@ void GuiSpellchecker::check() ptrdiff_t start = 0; ptrdiff_t total = 0; - DocIterator it = DocIterator(buffer().inset()); - for (start = 0; it != cur; it.forwardPos()) + DocIterator it = doc_iterator_begin(buffer().inset()); + for (start = 1; it != cur; it.forwardPos()) ++start; for (total = start; it; it.forwardPos()) -- 2.39.2