From 744ee152faeb376a142d26c09784d1a7e9dd303d Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Tue, 5 Jan 2010 13:16:55 +0000 Subject: [PATCH] Cleanup a bit the pargraph ids generation in order to (trying to) fix #6415 Enrico please verify that reverse dvi is fixed. * Text and InsetText: create two private constructors and transfer some initialisation code from InsetText. * Paragraph: id generation is transfered to Text. May be transfered to Buffer in the future, we'll see. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@32766 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 10 ++++++++-- src/Paragraph.cpp | 21 ++++++++++----------- src/Paragraph.h | 4 +++- src/Text.cpp | 34 ++++++++++++++++++++++++++++++++++ src/Text.h | 15 +++++++++------ src/insets/InsetText.cpp | 30 +++--------------------------- src/insets/InsetText.h | 4 ---- 7 files changed, 67 insertions(+), 51 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index bd73057017..efc83fa491 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -315,9 +315,15 @@ Buffer::Buffer(string const & file, bool readonly, Buffer const * cloned_buffer) { LYXERR(Debug::INFO, "Buffer::Buffer()"); if (cloned_buffer) { - d->inset = static_cast(cloned_buffer->d->inset->clone()); + d->inset = new InsetText(*cloned_buffer->d->inset); d->inset->setBuffer(*this); - } else + // FIXME 1: optimize this loop somewhat, maybe by creatinga new + // greneral recursive Inset::setId(). + DocIterator it = doc_iterator_begin(this); + DocIterator cloned_it = doc_iterator_begin(cloned_buffer); + for (; !it.atEnd(); it.forwardPar(), cloned_it.forwardPar()) + it.paragraph().setId(cloned_it.paragraph().id()); + } else d->inset = new InsetText(this); d->inset->setAutoBreakRows(true); d->inset->getText(0)->setMacrocontextPosition(par_iterator_begin()); diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index 0711d2492b..bfaec69d14 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -189,9 +189,8 @@ public: FontList fontlist_; /// - unsigned int id_; - /// - static unsigned int paragraph_id; + int id_; + /// ParagraphParameters params_; @@ -216,9 +215,6 @@ public: }; -// Initialization of the counter for the paragraph id's, -unsigned int Paragraph::Private::paragraph_id = 0; - namespace { struct special_phrase { @@ -240,20 +236,18 @@ size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase); Paragraph::Private::Private(Paragraph * owner, Layout const & layout) - : owner_(owner), inset_owner_(0), begin_of_body_(0), layout_(&layout) + : owner_(owner), inset_owner_(0), id_(-1), begin_of_body_(0), layout_(&layout) { - id_ = paragraph_id++; text_.reserve(100); } Paragraph::Private::Private(Private const & p, Paragraph * owner) : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_), - params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_), + id_(p.id_), params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_), begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_), layout_(p.layout_) { - id_ = paragraph_id++; } @@ -265,7 +259,6 @@ Paragraph::Private::Private(Private const & p, Paragraph * owner, begin_of_body_(p.begin_of_body_), words_(p.words_), layout_(p.layout_) { - id_ = paragraph_id++; if (beg >= pos_type(p.text_.size())) return; text_ = p.text_.substr(beg, end - beg); @@ -2654,6 +2647,12 @@ int Paragraph::id() const } +void Paragraph::setId(int id) +{ + d->id_ = id; +} + + Layout const & Paragraph::layout() const { return *d->layout_; diff --git a/src/Paragraph.h b/src/Paragraph.h index 485e239b66..761e6e9b5d 100644 --- a/src/Paragraph.h +++ b/src/Paragraph.h @@ -92,7 +92,7 @@ class Paragraph public: /// Paragraph(); - /// + /// Copy constructor. Paragraph(Paragraph const &); /// Partial copy constructor. /// Copy the Paragraph contents from \p beg to \p end (without end). @@ -103,6 +103,8 @@ public: ~Paragraph(); /// int id() const; + /// + void setId(int id); /// void addChangesToToc(DocIterator const & cdit, Buffer const & buf) const; diff --git a/src/Text.cpp b/src/Text.cpp index 5803c2d4ad..66c6090aa2 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -170,6 +170,40 @@ void mergeParagraph(BufferParams const & bparams, pars.erase(boost::next(pars.begin(), par_offset + 1)); } +// Initialization of the counter for the paragraph id's, +// +// FIXME: There should be a more intelligent way to generate and use the +// paragraph ids per buffer instead a global static counter for all InsetText +// in the running program. +static int paragraph_id = -1; + +Text::Text(InsetText * owner, bool use_default_layout) + : owner_(owner), autoBreakRows_(false), undo_counter_(0) +{ + pars_.push_back(Paragraph()); + Paragraph & par = pars_.back(); + par.setId(++paragraph_id); + par.setInsetOwner(owner); + DocumentClass const & dc = owner->buffer().params().documentClass(); + if (use_default_layout) + par.setDefaultLayout(dc); + else + par.setPlainLayout(dc); +} + + +Text::Text(InsetText * owner, Text const & text) + : owner_(owner), autoBreakRows_(text.autoBreakRows_), undo_counter_(0) +{ + pars_ = text.pars_; + ParagraphList::iterator const end = pars_.end(); + ParagraphList::iterator it = pars_.begin(); + for (; it != end; ++it) { + it->setId(++paragraph_id); + it->setInsetOwner(owner); + } +} + pit_type Text::depthHook(pit_type pit, depth_type depth) const { diff --git a/src/Text.h b/src/Text.h index c8073eec71..98964e9b20 100644 --- a/src/Text.h +++ b/src/Text.h @@ -36,14 +36,17 @@ class Lexer; class PainterInfo; class Spacing; -/// This class encapsulates the main text data and operations in LyX +/// This class encapsulates the main text data and operations in LyX. +/// This is more or less the private implementation of InsetText. class Text { -public: - /// constructor - explicit Text(InsetText * owner) - : owner_(owner), autoBreakRows_(false), undo_counter_(0) - {} +private: + /// Default constructor. + Text(InsetText * owner, bool use_default_layout); + /// Copy constructor. + Text(InsetText * owner, Text const & text); + +public: /// \return true if there's no content at all. /// \warning a non standard layout on an empty paragraph doesn't // count as empty. diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp index b7dc42bb72..3c85bfd470 100644 --- a/src/insets/InsetText.cpp +++ b/src/insets/InsetText.cpp @@ -75,20 +75,17 @@ using graphics::PreviewLoader; ///////////////////////////////////////////////////////////////////// InsetText::InsetText(Buffer * buf, UsePlain type) - : Inset(buf), drawFrame_(false), frame_color_(Color_insetframe), text_(this) + : Inset(buf), drawFrame_(false), frame_color_(Color_insetframe), + text_(this, type == DefaultLayout) { - initParagraphs(type); } InsetText::InsetText(InsetText const & in) - : Inset(in), text_(this) + : Inset(in), text_(this, in.text_) { - text_.autoBreakRows_ = in.text_.autoBreakRows_; drawFrame_ = in.drawFrame_; frame_color_ = in.frame_color_; - text_.paragraphs() = in.text_.paragraphs(); - setParagraphOwner(); } @@ -101,27 +98,6 @@ void InsetText::setBuffer(Buffer & buf) } -void InsetText::initParagraphs(UsePlain type) -{ - LASSERT(paragraphs().empty(), /**/); - paragraphs().push_back(Paragraph()); - Paragraph & ourpar = paragraphs().back(); - ourpar.setInsetOwner(this); - DocumentClass const & dc = buffer_->params().documentClass(); - if (type == DefaultLayout) - ourpar.setDefaultLayout(dc); - else - ourpar.setPlainLayout(dc); -} - - -void InsetText::setParagraphOwner() -{ - for_each(paragraphs().begin(), paragraphs().end(), - bind(&Paragraph::setInsetOwner, _1, this)); -} - - void InsetText::clear() { ParagraphList & pars = paragraphs(); diff --git a/src/insets/InsetText.h b/src/insets/InsetText.h index be35505130..43ef3f717d 100644 --- a/src/insets/InsetText.h +++ b/src/insets/InsetText.h @@ -205,10 +205,6 @@ protected: /// docstring getCaptionHTML(OutputParams const &) const; private: - /// - void initParagraphs(UsePlain type); - /// - void setParagraphOwner(); /// bool drawFrame_; /// -- 2.39.5