Skip to content

Commit

Permalink
implement wordwrap with simpletext
Browse files Browse the repository at this point in the history
  • Loading branch information
wschweer committed Dec 10, 2012
1 parent 44041c2 commit 1e0150d
Show file tree
Hide file tree
Showing 52 changed files with 50,562 additions and 23,068 deletions.
63 changes: 45 additions & 18 deletions libmscore/simpletext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SimpleText::SimpleText(const SimpleText& st)
: Element(st)
{
_text = st._text;
_layout = st._layout;
_textStyle = st._textStyle;
_layoutToParentWidth = st._layoutToParentWidth;
frame = st.frame;
Expand All @@ -53,7 +54,7 @@ void SimpleText::draw(QPainter* p) const
p->setFont(textStyle().fontPx(spatium()));
p->setBrush(Qt::NoBrush);
p->setPen(textColor());
foreach(const TLine& t, _text)
foreach(const TLine& t, _layout)
p->drawText(t.pos, t.text);
}

Expand Down Expand Up @@ -111,21 +112,59 @@ QColor SimpleText::textColor() const

void SimpleText::layout()
{
int n = _text.size();
QFontMetricsF fm(_textStyle.fontPx(spatium()));
QStringList sl = _text.split('\n');
_layout.clear();
if (parent() && layoutToParentWidth()) {
Element* e = parent();
qreal w = e->width();
if (e->type() == HBOX || e->type() == VBOX || e->type() == TBOX) {
Box* b = static_cast<Box*>(e);
w -= ((b->leftMargin() + b->rightMargin()) * MScore::DPMM);
}
foreach(QString s, sl) {
if (fm.width(s) < w)
_layout.append(TLine(s));
else {
int n = s.size();
int sidx = 0;
int eidx = n-1;
while (eidx > sidx) {
while (fm.width(s.mid(sidx, eidx-sidx+1)) > w) {
--eidx;
while (eidx > sidx) {
if (s[eidx].isSpace())
break;
--eidx;
}
}
if (eidx == sidx)
eidx = n-1;
_layout.append(TLine(s.mid(sidx, eidx-sidx+1)));
sidx = eidx;
eidx = n-1;
}
}
}
}
else {
foreach(QString s, sl)
_layout.append(TLine(s));
}
int n = _layout.size();
if (!n) {
setPos(QPointF());
setbbox(QRectF());
return;
}

QFontMetricsF fm(_textStyle.fontPx(spatium()));
QPointF o(_textStyle.offset(spatium()));

QRectF bb;
qreal lh = lineHeight();
qreal ly = .0;
for (int i = 0; i < n; ++i) {
TLine* t = &_text[i];
TLine* t = &_layout[i];

QRectF r(fm.tightBoundingRect(t->text));

Expand Down Expand Up @@ -209,10 +248,7 @@ qreal SimpleText::baseLine() const

void SimpleText::setText(const QString& s)
{
QStringList sl = s.split('\n');
_text.clear();
foreach(QString s, sl)
_text.append(TLine(s));
_text = s;
}

//---------------------------------------------------------
Expand All @@ -221,15 +257,6 @@ void SimpleText::setText(const QString& s)

QString SimpleText::getText() const
{
QString s;
int n = _text.size();
if (n == 0)
return s;
s = _text[0].text;
for (int i = 1; i < n; ++i) {
s += '\n';
s += _text[i].text;
}
return s;
return _text;
}

3 changes: 2 additions & 1 deletion libmscore/simpletext.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ struct TLine {
class SimpleText : public Element {
Q_OBJECT

QList<TLine> _text;
QString _text;
QList<TLine> _layout;
QRectF frame; // calculated in layout()

bool _layoutToParentWidth;
Expand Down
1 change: 1 addition & 0 deletions mscore/navigator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,4 @@ void Navigator::paintEvent(QPaintEvent* ev)
p.translate(-pos);
}
}

22 changes: 11 additions & 11 deletions mscore/textproperties.ui
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@
<string>offset in Space units</string>
</property>
<property name="text">
<string>Space</string>
<string extracomment="spatium unit">Space</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -828,14 +828,14 @@
<property name="suffix">
<string>sp</string>
</property>
<property name="singleStep">
<double>0.10</double>
</property>
<property name="minimum">
<double>0.0</double>
<double>0.000000000000000</double>
</property>
<property name="maximum">
<double>999.0</double>
<double>999.000000000000000</double>
</property>
<property name="singleStep">
<double>0.100000000000000</double>
</property>
</widget>
</item>
Expand Down Expand Up @@ -866,14 +866,14 @@
<property name="suffix">
<string>sp</string>
</property>
<property name="singleStep">
<double>0.50</double>
</property>
<property name="minimum">
<double>0.0</double>
<double>0.000000000000000</double>
</property>
<property name="maximum">
<double>999.0</double>
<double>999.000000000000000</double>
</property>
<property name="singleStep">
<double>0.500000000000000</double>
</property>
</widget>
</item>
Expand Down
Loading

0 comments on commit 1e0150d

Please sign in to comment.