void Text::layout1() { if (styled() && !_editMode) SimpleText::layout(); else { _doc->setDefaultFont(textStyle().font(spatium())); qreal w = -1.0; QPointF o(textStyle().offset(spatium())); if (parent() && layoutToParentWidth()) { Element* e = parent(); 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); } } QTextOption to = _doc->defaultTextOption(); to.setUseDesignMetrics(true); to.setWrapMode(w <= 0.0 ? QTextOption::NoWrap : QTextOption::WrapAtWordBoundaryOrAnywhere); _doc->setDefaultTextOption(to); if (w <= 0.0) w = _doc->idealWidth(); _doc->setTextWidth(w); QSizeF size(_doc->size()); if (align() & ALIGN_BOTTOM) o.ry() -= size.height(); else if (align() & ALIGN_VCENTER) o.ry() -= (size.height() * .5); else if (align() & ALIGN_BASELINE) o.ry() -= baseLine(); if (align() & ALIGN_RIGHT) o.rx() -= size.width(); else if (align() & ALIGN_HCENTER) o.rx() -= (size.width() * .5); setbbox(QRectF(QPointF(0.0, 0.0), size)); _doc->setModified(false); setPos(o); } if (parent()) { Element* e = parent(); qreal w, h, xo, yo; if (layoutToParentWidth()) { if (e->type() == HBOX || e->type() == VBOX || e->type() == TBOX) { // consider inner margins of frame Box* b = static_cast<Box*>(e); xo = b->leftMargin() * MScore::DPMM; yo = b->topMargin() * MScore::DPMM; w = b->width() - xo - b->rightMargin() * MScore::DPMM; h = b->height() - yo - b->bottomMargin() * MScore::DPMM; } else { w = e->width(); h = e->height(); xo = 0.0; yo = 0.0; } QPointF ro(_textStyle.reloff() * .01); rxpos() += xo + ro.x() * w; rypos() += yo + ro.y() * h; } if (e->type() == SEGMENT) { Segment* s = static_cast<Segment*>(e); rypos() += s->measure()->system()->staff(staffIdx())->y(); } } if (hasFrame()) layoutFrame(); }
void Text::clear() { if (styled()) SimpleText::clear(); else _doc->clear(); }
void Text::startEdit(MuseScoreView*, const QPointF& p) { _editMode = true; if (styled()) { createDoc(); setUnstyledText(SimpleText::getText()); layout(); } _cursor = new QTextCursor(_doc); _cursor->setVisualNavigation(true); setCursor(p); if (_cursor->position() == 0 && align()) { QTextBlockFormat bf = _cursor->blockFormat(); Qt::Alignment alignment = 0; if (align() & ALIGN_HCENTER) alignment |= Qt::AlignHCenter; else if (align() & ALIGN_LEFT) alignment |= Qt::AlignLeft; else if (align() & ALIGN_RIGHT) alignment |= Qt::AlignRight; bf.setAlignment(alignment); setBlockFormat(bf); } qreal w = 2.0; // 8.0 / view->matrix().m11(); score()->addRefresh(canvasBoundingRect().adjusted(-w, -w, w, w)); }
void Text::draw(QPainter* painter) const { drawFrame(painter); if (styled()) { SimpleText::draw(painter); return; } QAbstractTextDocumentLayout::PaintContext c; bool printing = score() && score()->printing(); if (_cursor && _doc && _cursor->document() == _doc && !printing) { if (_cursor->hasSelection()) { QAbstractTextDocumentLayout::Selection selection; selection.cursor = *_cursor; selection.format.setBackground(c.palette.brush(QPalette::Active, QPalette::Highlight)); selection.format.setForeground(c.palette.brush(QPalette::Active, QPalette::HighlightedText)); c.selections.append(selection); } c.cursorPosition = _cursor->position(); } else c.cursorPosition = -1; if ((printing || !score()->showInvisible()) && !visible()) return; c.palette.setColor(QPalette::Text, textColor()); _doc->documentLayout()->draw(painter, c); }
QTextDocumentFragment Text::getFragment() const { if (styled()) return QTextDocumentFragment::fromPlainText(getText()); else return QTextDocumentFragment(_doc); }
void Text::spatiumChanged(qreal oldVal, qreal newVal) { Element::spatiumChanged(oldVal, newVal); if (!sizeIsSpatiumDependent() || styled()) return; qreal v = newVal / oldVal; QTextCursor c(_doc); QTextBlock cb = _doc->begin(); while (cb.isValid()) { QTextBlock::iterator i(cb.begin()); for (; !i.atEnd(); ++i) { QTextFragment f = i.fragment(); if (f.isValid()) { int pos = f.position(); int len = f.length(); c.setPosition(pos, QTextCursor::MoveAnchor); c.setPosition(pos + len, QTextCursor::KeepAnchor); QTextCharFormat cf = c.charFormat(); QFont font = cf.font(); font.setPointSizeF(font.pointSizeF() * v); cf.setFont(font); c.setCharFormat(cf); } } cb = cb.next(); } }
void Text::writeProperties(Xml& xml, bool writeText) const { Element::writeProperties(xml); if (xml.clipboardmode || styled()) xml.tag("style", textStyle().name()); if (xml.clipboardmode || !styled()) _textStyle.writeProperties(xml); if (writeText) { if (styled()) xml.tag("text", getText()); else { xml.stag("html-data"); xml.writeHtml(_doc->toHtml("utf-8")); xml.etag(); } } }
void Text::setText(const QString& s) { if (styled()) SimpleText::setText(s); else setUnstyledText(s); textChanged(); }
void Text::styleChanged() { if (styled()) { if (_styleIndex != TEXT_STYLE_UNKNOWN) setTextStyle(score()->textStyle(_styleIndex)); setText(getText()); // destroy formatting score()->setLayoutAll(true); } }
void Text::layout() { if (styled() && !_editMode) { SimpleText::layout(); } else { _doc->setDefaultFont(textStyle().font(spatium())); qreal w = -1.0; qreal x = 0.0; qreal y = 0.0; if (parent() && layoutToParentWidth()) { w = parent()->width(); if (parent()->type() == HBOX || parent()->type() == VBOX || parent()->type() == TBOX) { Box* box = static_cast<Box*>(parent()); x += box->leftMargin() * MScore::DPMM; y += box->topMargin() * MScore::DPMM; w = box->width() - ((box->leftMargin() + box->rightMargin()) * MScore::DPMM); } } QTextOption to = _doc->defaultTextOption(); to.setUseDesignMetrics(true); to.setWrapMode(w <= 0.0 ? QTextOption::NoWrap : QTextOption::WrapAtWordBoundaryOrAnywhere); _doc->setDefaultTextOption(to); if (w < 0.0) w = _doc->idealWidth(); _doc->setTextWidth(w); setbbox(QRectF(QPointF(0.0, 0.0), _doc->size())); if (hasFrame()) layoutFrame(); _doc->setModified(false); textStyle().layout(this); // process alignment #if 0 // TODO TEXT_STYLE_TEXTLINE if ((textStyle().align() & ALIGN_VCENTER) && (textStyle() == TEXT_STYLE_TEXTLINE)) { // special case: vertically centered text with TextLine needs to // take into account the line width TextLineSegment* tls = static_cast<TextLineSegment*>(parent()); TextLine* tl = tls->textLine(); if (tl) { qreal textlineLineWidth = point(tl->lineWidth()); rypos() -= textlineLineWidth * .5; } } #endif rxpos() += x; rypos() += y; } if (parent() && parent()->type() == SEGMENT) { Segment* s = static_cast<Segment*>(parent()); rypos() += s ? s->measure()->system()->staff(staffIdx())->y() : 0.0; } adjustReadPos(); }
void Text::endCursorEdit() { delete _cursor; _cursor = 0; if (styled()) { SimpleText::setText(_doc->toPlainText()); delete _doc; _doc = 0; } }
void Text::moveCursor(int col) { if (styled()) { SimpleText::moveCursor(col); return; } if (_cursor) _cursor->setPosition(col); }
void Text::moveCursorToEnd() { if (styled()) { SimpleText::moveCursorToEnd(); return; } if (_cursor) _cursor->movePosition(QTextCursor::End); }
void Text::setUnstyled() { if (!styled()) return; _styleIndex = TEXT_STYLE_UNSTYLED; if (_editMode) return; createDoc(); if (!SimpleText::isEmpty()) setUnstyledText(SimpleText::getText()); }
void Text::endEdit() { if (styled()) SimpleText::endEdit(); else { _editMode = false; endCursorEdit(); layoutEdit(); textChanged(); } }
qreal Text::baseLine() const { if (styled()) return SimpleText::baseLine(); for (QTextBlock tb = _doc->begin(); tb.isValid(); tb = tb.next()) { const QTextLayout* tl = tb.layout(); if (tl->lineCount()) return (tl->lineAt(0).ascent() + tl->position().y()); } return 0.0; }
QTextCursor* Text::startCursorEdit() { if (styled()) { qDebug("Text::startCursorEdit(): edit styled text\n"); return 0; } if (_cursor) { qDebug("Text::startCursorEdit(): cursor already active\n"); return 0; } _cursor = new QTextCursor(_doc); return _cursor; }
void Text::startEdit(MuseScoreView* view, const QPointF& p) { if (styled()) { SimpleText::startEdit(view, p); return; } _cursor = new QTextCursor(_doc); _cursor->setVisualNavigation(true); setCursor(p); qreal w = 2.0; // 8.0 / view->matrix().m11(); score()->rebuildBspTree(); score()->addRefresh(canvasBoundingRect().adjusted(-w, -w, w, w)); }
void Text::startEdit(MuseScoreView*, const QPointF& p) { _editMode = true; if (styled()) { createDoc(); setUnstyledText(SimpleText::getText()); layout(); } _cursor = new QTextCursor(_doc); _cursor->setVisualNavigation(true); setCursor(p); qreal w = 2.0; // 8.0 / view->matrix().m11(); score()->rebuildBspTree(); score()->addRefresh(canvasBoundingRect().adjusted(-w, -w, w, w)); }
void Text::addChar(int code) { if (styled()) { SimpleText::addChar(code); return; } QString ss; if (code & 0xffff0000) { ss = QChar(QChar::highSurrogate(code)); ss += QChar(QChar::lowSurrogate(code)); } else ss = QChar(code); _cursor->insertText(ss); score()->setLayoutAll(true); score()->end(); }
QPainterPath Text::shape() const { if (styled()) return SimpleText::shape(); QPainterPath pp; for (QTextBlock tb = _doc->begin(); tb.isValid(); tb = tb.next()) { QTextLayout* tl = tb.layout(); int n = tl->lineCount(); for (int i = 0; i < n; ++i) { QTextLine l = tl->lineAt(i); QRectF r(l.naturalTextRect().translated(tl->position())); r.adjust(-l.position().x(), 0.0, 0.0, 0.0); pp.addRect(r); } } return pp; }
bool Text::setCursor(const QPointF& p, QTextCursor::MoveMode mode) { if (styled()) return SimpleText::setCursor(p, mode); QPointF pt = p - canvasPos(); if (!bbox().contains(pt)) return false; int idx = _doc->documentLayout()->hitTest(pt, Qt::FuzzyHit); if (idx == -1) return true; if (_cursor) { _cursor->setPosition(idx, mode); if (_cursor->hasSelection()) QApplication::clipboard()->setText(_cursor->selectedText(), QClipboard::Selection); } return true; }
void Text::draw(QPainter* painter) const { drawFrame(painter); if (styled() && !_editMode) { SimpleText::draw(painter); return; } QAbstractTextDocumentLayout::PaintContext c; bool printing = score() && score()->printing(); if (_cursor && _doc && _cursor->document() == _doc && !printing) { if (_cursor->hasSelection()) { QAbstractTextDocumentLayout::Selection selection; selection.cursor = *_cursor; selection.format.setBackground(c.palette.brush(QPalette::Active, QPalette::Highlight)); selection.format.setForeground(c.palette.brush(QPalette::Active, QPalette::HighlightedText)); c.selections.append(selection); } c.cursorPosition = _cursor->position(); } else c.cursorPosition = -1; if ((printing || !score()->showInvisible()) && !visible()) return; c.palette.setColor(QPalette::Text, textColor()); #if 1 // make it thread save { QWriteLocker locker(&docRenderLock); QScopedPointer<QTextDocument> __doc(_doc->clone()); __doc.data()->documentLayout()->draw(painter, c); // _doc->documentLayout()->draw(painter, c); } #else _doc->documentLayout()->draw(painter, c); #endif }
void Text::spatiumChanged(qreal oldVal, qreal newVal) { Element::spatiumChanged(oldVal, newVal); #if 0 qDebug("Text::spatiumChanged %d -- %s %s %p %f\n", sizeIsSpatiumDependent(), name(), parent() ? parent()->name() : "?", this, newVal); #endif if (!sizeIsSpatiumDependent() || styled()) return; qreal v = newVal / oldVal; QTextCursor c(_doc); c.movePosition(QTextCursor::Start); for (;;) { c.select(QTextCursor::BlockUnderCursor); QTextCharFormat cf = c.charFormat(); QFont font = cf.font(); font.setPointSizeF(font.pointSizeF() * v); cf.setFont(font); c.setCharFormat(cf); if (!c.movePosition(QTextCursor::NextBlock)) break; } }
void Text::addSymbol(const SymCode& s) { if (styled()) { SimpleText::addSymbol(s); return; } if (s.fontId >= 0) { QTextCharFormat nFormat(_cursor->charFormat()); nFormat.setFontFamily(fontId2font(s.fontId).family()); QString ss; if (s.code >= 0x10000) { ss = QChar(QChar::highSurrogate(s.code)); ss += QChar(QChar::lowSurrogate(s.code)); } else ss = QChar(s.code); _cursor->insertText(ss, nFormat); } else _cursor->insertText(QChar(s.code)); score()->setLayoutAll(true); score()->end(); }
void FiguredBass::layout() { if (!styled()) setTextStyle(TEXT_STYLE_FIGURED_BASS); Lyrics::layout(); qreal lh = lineSpacing() * score()->styleD(ST_figuredBassLineHeight); System* sys = measure()->system(); if (sys == 0) { qDebug("lyrics layout: no system!"); abort(); } const QList<Lyrics*>* ll = &(chordRest()->lyricsList()); int line = ll->indexOf(this); qreal y = lh * line + point(score()->styleS(ST_figuredBassDistance)) + sys->staff(staffIdx())->bbox().height(); QString s = getText(); qreal x = symbols[score()->symIdx()][quartheadSym].width(magS()) * .5; QFontMetricsF fm(style().font(spatium())); for (int i = 0; i < s.size(); ++i) { if (s[i].isNumber()) { if (i) x += -fm.width(s.left(i)); int startIdx = i; for (; i < s.size(); ++i) { if (!s[i].isNumber()) break; } x += -(fm.width(s.mid(startIdx, i - startIdx)) * .5); break; } } x -= spatium() * .25; // DEBUG setPos(x, y); }
QString Text::getHtml() const { return styled() ? "" : _doc->toHtml("utf-8"); }
QString Text::getText() const { return (styled() && !_editMode) ? SimpleText::getText() : _doc->toPlainText(); }
void Text::setModified(bool v) { if (!styled()) _doc->setModified(v); }
bool Text::isEmpty() const { return styled() ? SimpleText::getText().isEmpty() : _doc->isEmpty(); }