First step towards model/view separation of Paragraph class.

- ParagraphMetrics: new class gathering everything related to metrics.
- Paragraph: now derives from ParagraphMetrics.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16335 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2006-12-19 14:27:38 +00:00
parent 1ca55d1c8f
commit 30f8a6327f
2 changed files with 137 additions and 108 deletions

View File

@ -69,6 +69,88 @@ using std::ostream;
using std::ostringstream;
ParagraphMetrics::ParagraphMetrics()
{
}
ParagraphMetrics::ParagraphMetrics(ParagraphMetrics const & pm)
: dim_(pm.dim_), rows_(pm.rows_), rowSignature_(pm.rowSignature_)
{
}
ParagraphMetrics & ParagraphMetrics::operator=(ParagraphMetrics const & pm)
{
rows_ = pm.rows_;
dim_ = pm.dim_;
rowSignature_ = pm.rowSignature_;
return *this;
}
Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
{
BOOST_ASSERT(!rows().empty());
// If boundary is set we should return the row on which
// the character before is inside.
if (pos > 0 && boundary)
--pos;
RowList::iterator rit = rows_.end();
RowList::iterator const begin = rows_.begin();
for (--rit; rit != begin && rit->pos() > pos; --rit)
;
return *rit;
}
Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
{
BOOST_ASSERT(!rows().empty());
// If boundary is set we should return the row on which
// the character before is inside.
if (pos > 0 && boundary)
--pos;
RowList::const_iterator rit = rows_.end();
RowList::const_iterator const begin = rows_.begin();
for (--rit; rit != begin && rit->pos() > pos; --rit)
;
return *rit;
}
size_t ParagraphMetrics::pos2row(pos_type pos) const
{
BOOST_ASSERT(!rows().empty());
RowList::const_iterator rit = rows_.end();
RowList::const_iterator const begin = rows_.begin();
for (--rit; rit != begin && rit->pos() > pos; --rit)
;
return rit - begin;
}
void ParagraphMetrics::dump() const
{
lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
for (size_t i = 0; i != rows_.size(); ++i) {
lyxerr << " row " << i << ": ";
rows_[i].dump();
}
}
Paragraph::Paragraph()
: begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
{
@ -78,9 +160,8 @@ Paragraph::Paragraph()
Paragraph::Paragraph(Paragraph const & par)
: itemdepth(par.itemdepth), insetlist(par.insetlist),
dim_(par.dim_),
rows_(par.rows_), rowSignature_(par.rowSignature_),
: ParagraphMetrics(par),
itemdepth(par.itemdepth), insetlist(par.insetlist),
layout_(par.layout_),
text_(par.text_), begin_of_body_(par.begin_of_body_),
pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
@ -105,15 +186,14 @@ Paragraph & Paragraph::operator=(Paragraph const & par)
for (; it != end; ++it)
it->inset = it->inset->clone().release();
rows_ = par.rows_;
dim_ = par.dim_;
rowSignature_ = par.rowSignature_;
layout_ = par.layout();
text_ = par.text_;
begin_of_body_ = par.begin_of_body_;
delete pimpl_;
pimpl_ = new Pimpl(*par.pimpl_, this);
ParagraphMetrics::operator=(par);
}
return *this;
}
@ -1538,58 +1618,6 @@ bool Paragraph::allowEmpty() const
}
Row & Paragraph::getRow(pos_type pos, bool boundary)
{
BOOST_ASSERT(!rows().empty());
// If boundary is set we should return the row on which
// the character before is inside.
if (pos > 0 && boundary)
--pos;
RowList::iterator rit = rows_.end();
RowList::iterator const begin = rows_.begin();
for (--rit; rit != begin && rit->pos() > pos; --rit)
;
return *rit;
}
Row const & Paragraph::getRow(pos_type pos, bool boundary) const
{
BOOST_ASSERT(!rows().empty());
// If boundary is set we should return the row on which
// the character before is inside.
if (pos > 0 && boundary)
--pos;
RowList::const_iterator rit = rows_.end();
RowList::const_iterator const begin = rows_.begin();
for (--rit; rit != begin && rit->pos() > pos; --rit)
;
return *rit;
}
size_t Paragraph::pos2row(pos_type pos) const
{
BOOST_ASSERT(!rows().empty());
RowList::const_iterator rit = rows_.end();
RowList::const_iterator const begin = rows_.begin();
for (--rit; rit != begin && rit->pos() > pos; --rit)
;
return rit - begin;
}
char_type Paragraph::transformChar(char_type c, pos_type pos) const
{
if (!Encodings::is_arabic(c))
@ -1626,16 +1654,6 @@ char_type Paragraph::transformChar(char_type c, pos_type pos) const
}
void Paragraph::dump() const
{
lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
for (size_t i = 0; i != rows_.size(); ++i) {
lyxerr << " row " << i << ": ";
rows_[i].dump();
}
}
bool Paragraph::hfillExpansion(Row const & row, pos_type pos) const
{
if (!isHfill(pos))

View File

@ -58,9 +58,56 @@ public:
pos_type first, last;
};
/// Helper class for Paragraph Metrics.
/// \todo FIXME: this class deserves its own .[Ch] files.
/// Then, the storage of such object should be done in \c BufferView
/// (most probably in the \c CoordCache class along \c Point objects).
class ParagraphMetrics {
public:
ParagraphMetrics();
ParagraphMetrics(ParagraphMetrics const & pm);
ParagraphMetrics & operator=(ParagraphMetrics const & pm);
///
Row & getRow(pos_type pos, bool boundary);
///
Row const & getRow(pos_type pos, bool boundary) const;
///
size_t pos2row(pos_type pos) const;
/// LyXText::redoParagraph updates this
Dimension & dim() { return dim_; }
/// total height of paragraph
unsigned int height() const { return dim_.height(); }
/// total width of paragraph, may differ from workwidth
unsigned int width() const { return dim_.width(); }
/// ascend of paragraph above baseline
unsigned int ascent() const { return dim_.ascent(); }
/// descend of paragraph below baseline
unsigned int descent() const { return dim_.descent(); }
/// LyXText updates the rows using this access point
RowList & rows() { return rows_; }
/// The painter and others use this
RowList const & rows() const { return rows_; }
///
RowSignature & rowSignature() const { return rowSignature_; }
/// dump some information to lyxerr
void dump() const;
private:
///
mutable RowList rows_;
///
mutable RowSignature rowSignature_;
/// cached dimensions of paragraph
Dimension dim_;
};
/// A Paragraph holds all text, attributes and insets in a text paragraph
class Paragraph {
/// \todo FIXME: any reference to ParagraphMetrics (including inheritance)
/// should go in order to complete the Model/View separation of this class.
class Paragraph: public ParagraphMetrics {
public:
///
enum {
@ -359,49 +406,14 @@ public:
ParagraphParameters & params();
///
ParagraphParameters const & params() const;
///
Row & getRow(pos_type pos, bool boundary);
///
Row const & getRow(pos_type pos, bool boundary) const;
///
size_t pos2row(pos_type pos) const;
/// total height of paragraph
unsigned int height() const { return dim_.height(); }
/// total width of paragraph, may differ from workwidth
unsigned int width() const { return dim_.width(); }
/// ascend of paragraph above baseline
unsigned int ascent() const { return dim_.ascent(); }
/// descend of paragraph below baseline
unsigned int descent() const { return dim_.descent(); }
/// LyXText updates the rows using this access point
RowList & rows() { return rows_; }
/// The painter and others use this
RowList const & rows() const { return rows_; }
///
RowSignature & rowSignature() const { return rowSignature_; }
///
bool hfillExpansion(Row const & row, pos_type pos) const;
/// LyXText::redoParagraph updates this
Dimension & dim() { return dim_; }
/// dump some information to lyxerr
void dump() const;
public:
///
InsetList insetlist;
private:
/// cached dimensions of paragraph
Dimension dim_;
///
mutable RowList rows_;
///
mutable RowSignature rowSignature_;
///
LyXLayout_ptr layout_;
@ -421,7 +433,6 @@ private:
Pimpl * pimpl_;
};
} // namespace lyx
#endif // PARAGRAPH_H