コード例 #1
0
ファイル: xmlwriter.cpp プロジェクト: 3163504123/phantomjs
void XmlWriter::readFragment(const QTextBlock &currentBlock,
                             QDomElement blockElement,
                             QDomDocument *document)
{
//! [3] //! [4]
    QTextBlock::iterator it;
    for (it = currentBlock.begin(); !(it.atEnd()); ++it) {
        QTextFragment currentFragment = it.fragment();
        if (currentFragment.isValid())
//! [3] //! [5]
            processFragment(currentFragment);
//! [4] //! [5]

        if (currentFragment.isValid()) {
            QDomElement fragmentElement = document->createElement("fragment");
            blockElement.appendChild(fragmentElement);

            fragmentElement.setAttribute("length", currentFragment.length());
            QDomText fragmentText = document->createTextNode(currentFragment.text());

            fragmentElement.appendChild(fragmentText);
        }
//! [6] //! [7]
    }
//! [7] //! [6]
}
コード例 #2
0
ファイル: text.cpp プロジェクト: aeliot/MuseScore
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();
            }
      }
コード例 #3
0
ファイル: xl_highlighter.cpp プロジェクト: caivega/tao-3D
bool XLHighlighter::showSelectionInFragment(QTextFragment fragment)
// ----------------------------------------------------------------------------
//    Highlight (part of) fragment containing code related to a selected object
// ----------------------------------------------------------------------------
{
    bool matched = false;
    int spos= fragment.position();
    int epos = spos + fragment.length();
    int blockstart = currentBlock().begin().fragment().position();

    XL::stream_range frag(spos, epos);
    XL::stream_ranges::iterator r;
    for (r = selected.begin(); r != selected.end(); r++)
    {
        XL::stream_range i = intersect((*r), frag);
        if (i.first != -1)
        {
            int hstart = (int)(i.first) - blockstart;
            int hcount = (int)(i.second - i.first);
            setFormat(hstart, hcount, selectedFormat);
            matched = true;
        }
    }
    return matched;
}
コード例 #4
0
void TextDocument::replaceImageUrl(const QUrl &oldName, const QString &newName) {
	QList <QPair<int, int> > fragments;

	QTextBlock block = begin();

	while(block.isValid()) {
		QTextBlock::iterator iterator;
		for(iterator = block.begin(); !(iterator.atEnd()); ++iterator) {
			QTextFragment fragment = iterator.fragment();
			if(fragment.isValid() && fragment.charFormat().isImageFormat()) {
				QTextImageFormat format = fragment.charFormat().toImageFormat();
				if (QUrl::fromEncoded(format.name().toUtf8()) != oldName) {continue;}
				fragments.append(QPair<int, int>(fragment.position(), fragment.length()));
			}
		}
		block = block.next();
	}


	QTextCursor cursor(this);
	cursor.beginEditBlock();
	QPair<int, int> pair;
	foreach (pair, fragments) {
		cursor.setPosition(pair.first);
		cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, pair.second);
		QTextImageFormat format = cursor.charFormat().toImageFormat();
		format.setName(newName);
		cursor.mergeCharFormat(format);
	}
コード例 #5
0
void ChangeFollower::processUpdates(const QList<int> &changedStyles)
{
    KoStyleManager *sm = m_styleManager.data();
    if (!sm) {
        // since the stylemanager would be the one calling this method, I doubt this
        // will ever happen.  But better safe than sorry..
        deleteLater();
        return;
    }

    // optimization strategy;  store the formatid of the formats we checked into
    // a qset for 'hits' and 'ignores' and avoid the copying of the format
    // (fragment.charFormat() / block.blockFormat()) when the formatId is
    // already checked previosly

    QTextCursor cursor(m_document);
    QTextBlock block = cursor.block();
    while (block.isValid()) {
        QTextBlockFormat bf = block.blockFormat();
        int id = bf.intProperty(KoParagraphStyle::StyleId);
        if (id > 0 && changedStyles.contains(id)) {
            cursor.setPosition(block.position());
            KoParagraphStyle *style = sm->paragraphStyle(id);
            Q_ASSERT(style);

            style->applyStyle(bf);
            cursor.setBlockFormat(bf);
        }
        QTextCharFormat cf = block.charFormat();
        id = cf.intProperty(KoCharacterStyle::StyleId);
        if (id > 0 && changedStyles.contains(id)) {
            KoCharacterStyle *style = sm->characterStyle(id);
            Q_ASSERT(style);
            style->applyStyle(block);
        }

        QTextBlock::iterator iter = block.begin();
        while (! iter.atEnd()) {
            QTextFragment fragment = iter.fragment();
            cf = fragment.charFormat();
            id = cf.intProperty(KoCharacterStyle::StyleId);
            if (id > 0 && changedStyles.contains(id)) {
                // create selection
                cursor.setPosition(fragment.position());
                cursor.setPosition(fragment.position() + fragment.length(), QTextCursor::KeepAnchor);
                KoCharacterStyle *style = sm->characterStyle(id);
                Q_ASSERT(style);

                style->applyStyle(cf);
                cursor.mergeCharFormat(cf);
            }
            iter++;
        }
        block = block.next();
    }
}
コード例 #6
0
FontDia::FontDia(QTextCursor* cursor, QWidget* parent)
        : KDialog(parent),
        m_cursor(cursor)
{
    //First find out if we have more than one charFormat in our selection. If so, m_initialFormat/m_style will get initialised with the charFormat at the cursor's position. The tabs will get informed of this.

    if (m_cursor->hasSelection()) {
        int begin = qMin(m_cursor->anchor(), m_cursor->position());
        int end = qMax(m_cursor->anchor(), m_cursor->position());
        QTextBlock block = m_cursor->block().document()->findBlock(begin);
        m_uniqueFormat = true;
        QTextCursor caret(*m_cursor);
        caret.setPosition(begin+1);
        m_initialFormat = caret.charFormat();
        while (block.isValid() && block.position() < end) {
            QTextBlock::iterator iter = block.begin();
            while (! iter.atEnd()) {
                QTextFragment fragment = iter.fragment();
                if (fragment.position() >= end)
                    break;
                if (fragment.position() + fragment.length() <= begin) {
                    iter++;
                    continue;
                }
                if (!(m_uniqueFormat = (fragment.charFormat() == m_initialFormat)))
                    break;
                iter++;
            }
            if (!m_uniqueFormat)
                break;
            block = block.next();
        }
    }
    else {
        m_initialFormat = cursor->charFormat();
        m_uniqueFormat = true;
    }

    setCaption(i18n("Select Font"));
    setModal(true);
    setButtons(Ok | Cancel | Reset | Apply);
    setDefaultButton(Ok);

    m_characterGeneral = new CharacterGeneral(this, m_uniqueFormat);
    m_characterGeneral->hideStyleName(true);
    setMainWidget(m_characterGeneral);

    connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
    connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
    connect(this, SIGNAL(resetClicked()), this, SLOT(slotReset()));
    initTabs();
}
コード例 #7
0
ファイル: main.cpp プロジェクト: danwagnerco/PySide
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTextEdit *editor = new QTextEdit;

    QTextDocument *document = new QTextDocument(editor);
    QTextCursor cursor(document);

    QTextImageFormat imageFormat;
    imageFormat.setName(":/images/advert.png");
    cursor.insertImage(imageFormat);

    QTextBlock block = cursor.block();
    QTextFragment fragment;
    QTextBlock::iterator it;

    for (it = block.begin(); !(it.atEnd()); ++it) {
        fragment = it.fragment();

        if (fragment.contains(cursor.position()))
            break;
    }

//! [0]
    if (fragment.isValid()) {
        QTextImageFormat newImageFormat = fragment.charFormat().toImageFormat();

        if (newImageFormat.isValid()) {
            newImageFormat.setName(":/images/newimage.png");
            QTextCursor helper = cursor;

            helper.setPosition(fragment.position());
            helper.setPosition(fragment.position() + fragment.length(),
                               QTextCursor::KeepAnchor);
            helper.setCharFormat(newImageFormat);
//! [0] //! [1]
        }
//! [1] //! [2]
    }
//! [2]

    cursor.insertBlock();
    cursor.insertText("Code less. Create more.");

    editor->setDocument(document);
    editor->setWindowTitle(tr("Text Document Image Format"));
    editor->resize(320, 480);
    editor->show();

    return app.exec();
}
コード例 #8
0
void FlatTextarea::removeSingleEmoji() {
	QString text;
	QTextFragment fragment;

	getSingleEmojiFragment(text, fragment);

	if (!text.isEmpty()) {
		QTextCursor t(textCursor());
		t.setPosition(fragment.position());
		t.setPosition(fragment.position() + fragment.length(), QTextCursor::KeepAnchor);
		t.removeSelectedText();
		setTextCursor(t);
	}
}
コード例 #9
0
void XmlWriter::processBlock(QDomElement &parent, const QTextBlock &block)
{
    QDomElement blockElement = document->createElement("block");
    blockElement.setAttribute("position", block.position());
    blockElement.setAttribute("length", block.length());
    parent.appendChild(blockElement);

    QTextBlock::iterator it;
    for (it = block.begin(); !(it.atEnd()); ++it) {
        QTextFragment fragment = it.fragment();

        if (fragment.isValid()) {
            QDomElement fragmentElement = document->createElement("fragment");
            blockElement.appendChild(fragmentElement);

            fragmentElement.setAttribute("length", fragment.length());
            QDomText fragmentText = document->createTextNode(fragment.text());

            fragmentElement.appendChild(fragmentText);
        }
    }
}
コード例 #10
0
void QQuickTextNodeEngine::addTextBlock(QTextDocument *textDocument, const QTextBlock &block, const QPointF &position, const QColor &textColor, const QColor &anchorColor, int selectionStart, int selectionEnd)
{
    Q_ASSERT(textDocument);
#ifndef QT_NO_IM
    int preeditLength = block.isValid() ? block.layout()->preeditAreaText().length() : 0;
    int preeditPosition = block.isValid() ? block.layout()->preeditAreaPosition() : -1;
#endif

    QVarLengthArray<QTextLayout::FormatRange> colorChanges;
    mergeFormats(block.layout(), &colorChanges);

    QPointF blockPosition = textDocument->documentLayout()->blockBoundingRect(block).topLeft() + position;
    if (QTextList *textList = block.textList()) {
        QPointF pos = blockPosition;
        QTextLayout *layout = block.layout();
        if (layout->lineCount() > 0) {
            QTextLine firstLine = layout->lineAt(0);
            Q_ASSERT(firstLine.isValid());

            setCurrentLine(firstLine);

            QRectF textRect = firstLine.naturalTextRect();
            pos += textRect.topLeft();
            if (block.textDirection() == Qt::RightToLeft)
                pos.rx() += textRect.width();

            const QTextCharFormat charFormat = block.charFormat();
            QFont font(charFormat.font());
            QFontMetricsF fontMetrics(font);
            QTextListFormat listFormat = textList->format();

            QString listItemBullet;
            switch (listFormat.style()) {
            case QTextListFormat::ListCircle:
                listItemBullet = QChar(0x25E6); // White bullet
                break;
            case QTextListFormat::ListSquare:
                listItemBullet = QChar(0x25AA); // Black small square
                break;
            case QTextListFormat::ListDecimal:
            case QTextListFormat::ListLowerAlpha:
            case QTextListFormat::ListUpperAlpha:
            case QTextListFormat::ListLowerRoman:
            case QTextListFormat::ListUpperRoman:
                listItemBullet = textList->itemText(block);
                break;
            default:
                listItemBullet = QChar(0x2022); // Black bullet
                break;
            };

            QSizeF size(fontMetrics.width(listItemBullet), fontMetrics.height());
            qreal xoff = fontMetrics.width(QLatin1Char(' '));
            if (block.textDirection() == Qt::LeftToRight)
                xoff = -xoff - size.width();
            setPosition(pos + QPointF(xoff, 0));

            QTextLayout layout;
            layout.setFont(font);
            layout.setText(listItemBullet); // Bullet
            layout.beginLayout();
            QTextLine line = layout.createLine();
            line.setPosition(QPointF(0, 0));
            layout.endLayout();

            QList<QGlyphRun> glyphRuns = layout.glyphRuns();
            for (int i=0; i<glyphRuns.size(); ++i)
                addUnselectedGlyphs(glyphRuns.at(i));
        }
    }

    int textPos = block.position();
    QTextBlock::iterator blockIterator = block.begin();

    while (!blockIterator.atEnd()) {
        QTextFragment fragment = blockIterator.fragment();
        QString text = fragment.text();
        if (text.isEmpty())
            continue;

        QTextCharFormat charFormat = fragment.charFormat();
        QFont font(charFormat.font());
        QFontMetricsF fontMetrics(font);

        int fontHeight = fontMetrics.descent() + fontMetrics.ascent();
        int valign = charFormat.verticalAlignment();
        if (valign == QTextCharFormat::AlignSuperScript)
            setPosition(QPointF(blockPosition.x(), blockPosition.y() - fontHeight / 2));
        else if (valign == QTextCharFormat::AlignSubScript)
            setPosition(QPointF(blockPosition.x(), blockPosition.y() + fontHeight / 6));
        else
            setPosition(blockPosition);

        if (text.contains(QChar::ObjectReplacementCharacter)) {
            QTextFrame *frame = qobject_cast<QTextFrame *>(textDocument->objectForFormat(charFormat));
            if (frame && frame->frameFormat().position() == QTextFrameFormat::InFlow) {
                int blockRelativePosition = textPos - block.position();
                QTextLine line = block.layout()->lineForTextPosition(blockRelativePosition);
                if (!currentLine().isValid()
                        || line.lineNumber() != currentLine().lineNumber()) {
                    setCurrentLine(line);
                }

                QQuickTextNodeEngine::SelectionState selectionState =
                        (selectionStart < textPos + text.length()
                         && selectionEnd >= textPos)
                        ? QQuickTextNodeEngine::Selected
                        : QQuickTextNodeEngine::Unselected;

                addTextObject(QPointF(), charFormat, selectionState, textDocument, textPos);
            }
            textPos += text.length();
        } else {
            if (charFormat.foreground().style() != Qt::NoBrush)
                setTextColor(charFormat.foreground().color());
            else if (charFormat.isAnchor())
                setTextColor(anchorColor);
            else
                setTextColor(textColor);

            int fragmentEnd = textPos + fragment.length();
#ifndef QT_NO_IM
            if (preeditPosition >= 0
                    && (preeditPosition + block.position()) >= textPos
                    && (preeditPosition + block.position()) <= fragmentEnd) {
                fragmentEnd += preeditLength;
            }
#endif
            if (charFormat.background().style() != Qt::NoBrush) {
                QTextLayout::FormatRange additionalFormat;
                additionalFormat.start = textPos - block.position();
                additionalFormat.length = fragmentEnd - textPos;
                additionalFormat.format = charFormat;
                colorChanges << additionalFormat;
            }

            textPos = addText(block, charFormat, textColor, colorChanges, textPos, fragmentEnd,
                                                 selectionStart, selectionEnd);
        }

        ++blockIterator;
    }

#ifndef QT_NO_IM
    if (preeditLength >= 0 && textPos <= block.position() + preeditPosition) {
        setPosition(blockPosition);
        textPos = block.position() + preeditPosition;
        QTextLine line = block.layout()->lineForTextPosition(preeditPosition);
        if (!currentLine().isValid()
                || line.lineNumber() != currentLine().lineNumber()) {
            setCurrentLine(line);
        }
        textPos = addText(block, block.charFormat(), textColor, colorChanges,
                                             textPos, textPos + preeditLength,
                                             selectionStart, selectionEnd);
    }
#endif

    setCurrentLine(QTextLine()); // Reset current line because the text layout changed
    m_hasContents = true;
}
コード例 #11
0
void KoTextWriter::saveParagraph(const QTextBlock &block, int from, int to)
{
    QString changeName = QString();

    QTextBlockFormat blockFormat = block.blockFormat();
    int outlineLevel = blockFormat.intProperty(KoParagraphStyle::OutlineLevel);
    if (outlineLevel > 0) {
        d->writer->startElement("text:h", false);
        d->writer->addAttribute("text:outline-level", outlineLevel);
    } else 
        d->writer->startElement("text:p", false);

    QString styleName = saveParagraphStyle(block);
    if (!styleName.isEmpty())
        d->writer->addAttribute("text:style-name", styleName);

    // Write the fragments and their formats
    QTextCursor cursor(block);
    QTextCharFormat blockCharFormat = cursor.blockCharFormat();
    QTextCharFormat previousCharFormat;
    QTextBlock::iterator it;
    for (it = block.begin(); !(it.atEnd()); ++it) {
        QTextFragment currentFragment = it.fragment();
        const int fragmentStart = currentFragment.position();
        const int fragmentEnd = fragmentStart + currentFragment.length();
        if (to != -1 && fragmentStart >= to)
            break;
        if (currentFragment.isValid()) {
            QTextCharFormat charFormat = currentFragment.charFormat();
            QTextCharFormat compFormat = charFormat;
            bool identical;
            previousCharFormat.clearProperty(KoCharacterStyle::ChangeTrackerId);
            compFormat.clearProperty(KoCharacterStyle::ChangeTrackerId);
            if (previousCharFormat == compFormat)
                identical = true;
            else
                identical = false;

            if ( d->changeTracker && d->changeTracker->containsInlineChanges(charFormat) && d->changeTracker->elementById(charFormat.property(KoCharacterStyle::ChangeTrackerId).toInt())->isEnabled()) {
                KoGenChange change;
                d->changeTracker->saveInlineChange(charFormat.property(KoCharacterStyle::ChangeTrackerId).toInt(), change);
                changeName = d->sharedData->genChanges().insert(change);
                d->writer->startElement("text:change-start", false);
                d->writer->addAttribute("text:change-id", changeName);
                d->writer->endElement();
            }

            KoInlineObject *inlineObject = d->layout->inlineTextObjectManager()->inlineTextObject(charFormat);
            if (currentFragment.length() == 1 && inlineObject
                    && currentFragment.text()[0].unicode() == QChar::ObjectReplacementCharacter) {
                inlineObject->saveOdf(d->context);
            } else {
                QString styleName = saveCharacterStyle(charFormat, blockCharFormat);
                if (charFormat.isAnchor()) {
                    d->writer->startElement("text:a", false);
                    d->writer->addAttribute("xlink:type", "simple");
                    d->writer->addAttribute("xlink:href", charFormat.anchorHref());
                } else if (!styleName.isEmpty() /*&& !identical*/) {
                    d->writer->startElement("text:span", false);
                    d->writer->addAttribute("text:style-name", styleName);
                }

                QString text = currentFragment.text();
                int spanFrom = fragmentStart >= from ? 0 : from;
                int spanTo = to == -1 ? fragmentEnd : (fragmentEnd > to ? to : fragmentEnd);
                if (spanFrom != fragmentStart || spanTo != fragmentEnd) { // avoid mid, if possible
                    d->writer->addTextSpan(text.mid(spanFrom - fragmentStart, spanTo - spanFrom));
                } else {
                    d->writer->addTextSpan(text);
                }

                if ((!styleName.isEmpty() /*&& !identical*/) || charFormat.isAnchor())
                    d->writer->endElement();
            } // if (inlineObject)

            if (!changeName.isEmpty()) {
                d->writer->startElement("text:change-end", false);
                d->writer->addAttribute("text:change-id",changeName);
                d->writer->endElement();
                changeName=QString();
            }
            previousCharFormat = charFormat;
        } // if (fragment.valid())
    } // foeach(fragment)

    d->writer->endElement();
}
コード例 #12
0
ファイル: TextEditEx.cpp プロジェクト: 13W/icq-desktop
    QString TextEditEx::getPlainText(int _from, int _to) const
    {
        if (_from == _to)
            return "";

        if (_to != -1 && _to < _from)
        {
            assert(!"invalid data");
            return "";
        }

        QString out_string;
        QTextStream result(&out_string);

        int pos_start = 0;
        int length = 0;

        bool first = true;

        for (QTextBlock it_block = document()->begin(); it_block != document()->end(); it_block = it_block.next())
        {
            if (!first)
                result << '\n';

            pos_start = it_block.position();
            if (_to != -1 && pos_start >= _to)
                break;

            for (QTextBlock::iterator it_fragment = it_block.begin(); it_fragment != it_block.end(); ++it_fragment)
            {
                QTextFragment currentFragment = it_fragment.fragment();

                if (currentFragment.isValid())
                {
                    pos_start = currentFragment.position();
                    length = currentFragment.length();

                    if (pos_start + length <= _from)
                        continue;

                    if (_to != -1 && pos_start >= _to)
                        break;

                    first = false;

                    if (currentFragment.charFormat().isImageFormat())
                    {
                        if (pos_start < _from)
                            continue;

                        QTextImageFormat imgFmt = currentFragment.charFormat().toImageFormat();

                        auto iter = resource_index_.find(imgFmt.name());
                        if (iter != resource_index_.end())
                            result << iter->second;
                    }
                    else
                    {
                        QString fragment_text = currentFragment.text();

                        int c_start = std::max((_from - pos_start), 0);
                        int count = -1;
                        if (_to != -1 && _to <= pos_start + length)
                            count = _to - pos_start - c_start;

                        QString txt = fragment_text.mid(c_start, count);
                        txt.remove(QChar::SoftHyphen);

                        QChar *uc = txt.data();
                        QChar *e = uc + txt.size();

                        for (; uc != e; ++uc) {
                            switch (uc->unicode()) {
                            case 0xfdd0: // QTextBeginningOfFrame
                            case 0xfdd1: // QTextEndOfFrame
                            case QChar::ParagraphSeparator:
                            case QChar::LineSeparator:
                                *uc = QLatin1Char('\n');
                                break;
                            case QChar::Nbsp:
                                *uc = QLatin1Char(' ');
                                break;
                            default:
                                ;
                            }
                        }

                        result << txt;
                    }
                }
            }
        }

        return out_string;
    }