本文整理汇总了C++中QTextFragment::charFormat方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextFragment::charFormat方法的具体用法?C++ QTextFragment::charFormat怎么用?C++ QTextFragment::charFormat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextFragment
的用法示例。
在下文中一共展示了QTextFragment::charFormat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: replaceImageUrl
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);
}
示例2: processCustomFragment
void AudioTextDocumentDirector::processCustomFragment(const QTextFragment& fragment, const QTextDocument* doc)
{
if ( fragment.charFormat().objectType() != AudioType )
return Grantlee::MarkupDirector::processCustomFragment( fragment, doc );
QString name = fragment.charFormat().property( AudioProperty ).toString();
m_builder->addAudioTag( name );
}
示例3: writeInlineCharacter
void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextFragment &fragment) const
{
writer.writeStartElement(drawNS, QString::fromLatin1("frame"));
if (m_strategy == 0) {
// don't do anything.
}
else if (fragment.charFormat().isImageFormat()) {
QTextImageFormat imageFormat = fragment.charFormat().toImageFormat();
writer.writeAttribute(drawNS, QString::fromLatin1("name"), imageFormat.name());
// vvv Copy pasted mostly from Qt =================
QImage image;
QString name = imageFormat.name();
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
name.prepend(QLatin1String("qrc"));
QUrl url = QUrl::fromEncoded(name.toUtf8());
const QVariant data = m_document->resource(QTextDocument::ImageResource, url);
if (data.type() == QVariant::Image) {
image = qvariant_cast<QImage>(data);
} else if (data.type() == QVariant::ByteArray) {
image.loadFromData(data.toByteArray());
}
if (image.isNull()) {
QString context;
if (QTextImageHandler::externalLoader)
image = QTextImageHandler::externalLoader(name, context);
if (image.isNull()) { // try direct loading
name = imageFormat.name(); // remove qrc:/ prefix again
image.load(name);
}
}
// ^^^ Copy pasted mostly from Qt =================
if (! image.isNull()) {
QBuffer imageBytes;
QImageWriter imageWriter(&imageBytes, "png");
imageWriter.write(image);
QString filename = m_strategy->createUniqueImageName();
m_strategy->addFile(filename, QString::fromLatin1("image/png"), imageBytes.data());
// get the width/height from the format.
qreal width = (imageFormat.hasProperty(QTextFormat::ImageWidth)) ? imageFormat.width() : image.width();
writer.writeAttribute(svgNS, QString::fromLatin1("width"), pixelToPoint(width));
qreal height = (imageFormat.hasProperty(QTextFormat::ImageHeight)) ? imageFormat.height() : image.height();
writer.writeAttribute(svgNS, QString::fromLatin1("height"), pixelToPoint(height));
writer.writeStartElement(drawNS, QString::fromLatin1("image"));
writer.writeAttribute(xlinkNS, QString::fromLatin1("href"), filename);
writer.writeEndElement(); // image
}
}
writer.writeEndElement(); // frame
}
示例4: setHtml
/*!
Replaces the entire contents of the document
with the given HTML-formatted text in the \a text string
*/
void RichString::setHtml(const QString &text)
{
QTextDocument doc;
doc.setHtml(text);
QTextBlock block = doc.firstBlock();
QTextBlock::iterator it;
for (it = block.begin(); !(it.atEnd()); ++it) {
QTextFragment textFragment = it.fragment();
if (textFragment.isValid()) {
Format fmt;
fmt.setFont(textFragment.charFormat().font());
fmt.setFontColor(textFragment.charFormat().foreground().color());
addFragment(textFragment.text(), fmt);
}
}
}
示例5: processUpdates
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: getSingleEmoji
EmojiPtr FlatTextarea::getSingleEmoji() const {
QString text;
QTextFragment fragment;
getSingleEmojiFragment(text, fragment);
if (!text.isEmpty()) {
QTextCharFormat format = fragment.charFormat();
return emojiFromUrl(static_cast<const QTextImageFormat*>(&format)->name());
}
return 0;
}
示例7: toStringFromDocument
QString Dialog::toStringFromDocument() {
QTextDocument *doc = message()->document();
QString txt;
for (QTextBlock bl = doc->begin(); bl != doc->end(); bl = bl.next())
if (bl.isValid()) {
for (QTextBlock::iterator it = bl.begin(); !it.atEnd(); ++it) {
QTextFragment fragm = it.fragment();
if (fragm.isValid() && fragm.charFormat().isImageFormat()) {
QString imgName = fragm.charFormat().toImageFormat().name();
txt += imgName;
} else if (fragm.isValid())
txt += fragm.text();
}
if (bl != doc->begin())
txt += "\n";
}
int i = (int)txt.size() - 1;
while (i >= 0 && (txt[i] == ' ' || txt[i] == '\n')) --i;
txt.remove(i + 1, txt.size() - i - 1);
return txt;
}
示例8: getSingleEmoji
EmojiPtr FlatTextarea::getSingleEmoji() const {
QString text;
QTextFragment fragment;
getSingleEmojiFragment(text, fragment);
if (!text.isEmpty()) {
QTextCharFormat format = fragment.charFormat();
QString imageName = static_cast<const QTextImageFormat*>(&format)->name();
return getEmoji(imageName.mid(8).toUInt(0, 16));
}
return 0;
}
示例9: main
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();
}
示例10: mouseMoveEvent
void ChatView::mouseMoveEvent(QMouseEvent *event)
{
QTextFragment frag = getFragmentUnderMouse(event->pos());
QString cardName = getCardNameUnderMouse(frag);
if (!cardName.isEmpty()) {
viewport()->setCursor(Qt::PointingHandCursor);
emit cardNameHovered(cardName);
} else if (frag.charFormat().isAnchor())
viewport()->setCursor(Qt::PointingHandCursor);
else
viewport()->setCursor(Qt::IBeamCursor);
QTextBrowser::mouseMoveEvent(event);
}
示例11: getSingleEmoji
EmojiPtr FlatTextarea::getSingleEmoji() const {
QString text;
QTextFragment fragment;
getSingleEmojiFragment(text, fragment);
if (!text.isEmpty()) {
QTextCharFormat format = fragment.charFormat();
QString imageName = static_cast<QTextImageFormat*>(&format)->name();
if (imageName.startsWith(qstr("emoji://e."))) {
return emojiFromUrl(imageName);
}
}
return 0;
}
示例12: parse
FormattedMessage FormattedMessage::parse(const QTextDocument *document)
{
FormattedMessage result;
QString text;
QTextBlock block = document->firstBlock();
bool firstParagraph = true;
while (block.isValid())
{
bool firstFragment = true;
for (QTextBlock::iterator it = block.begin(); !it.atEnd(); ++it)
{
QTextFragment fragment = it.fragment();
if (!fragment.isValid())
continue;
if (!firstParagraph && firstFragment)
text = '\n' + fragment.text();
else
text = fragment.text();
QTextCharFormat format = fragment.charFormat();
parseImages(result, text,
format.font().bold(),
format.font().italic(),
format.font().underline(),
format.foreground().color());
firstFragment = false;
}
if (firstFragment)
parseImages(result, "\n", false, false, false, QColor());
block = block.next();
firstParagraph = false;
}
return result;
}
示例13: selectAndMarkAnchor
void MoveViewController::selectAndMarkAnchor(const QString& link)
{
QTextBlock block = this->document->begin();
while(block != this->document->end())
{
QTextBlock::iterator it;
for(it = block.begin(); !it.atEnd(); ++it)
{
QTextFragment fragment = it.fragment();
if(!fragment.isValid())
{
continue;
}
QTextCharFormat format = fragment.charFormat();
if(format.isAnchor() && format.anchorHref() == link)
{
QTextCursor cursor = this->textCursor();
cursor.setPosition(fragment.position());
int len = 0;
bool finished = false;
// we want to mark (highlight) everything from the
// start of the anchor until the end of the move
// the end of the move is indicated by an empty space
// (there is always an empty space after a move)
while(!finished && !it.atEnd()) {
if(it.fragment().text().startsWith(" ") || len >= 6) {
finished = true;
} else {
len+= it.fragment().text().length();
it++;
}
}
cursor.setPosition(fragment.position() + len, QTextCursor::KeepAnchor);
setTextCursor(cursor);
ensureCursorVisible();
return;
}
}
block = block.next();
}
}
示例14: processFragment
QString TextDocumentSerializer::processFragment(QTextFragment fragment)
{
QTextCharFormat format = fragment.charFormat();
QString text = fragment.text();
if(format.isImageFormat())
text = getImageTag(format.toImageFormat());
if(format.fontFamily().contains("Courier"))
text = QString("<code>%1</code>").arg(text);
if(format.fontWeight() == QFont::Bold)
text = QString("<b>%1</b>").arg(text);
if(format.fontItalic())
text = QString("<i>%1</i>").arg(text);
text = text.replace(QChar(0x2028), "<br>");
return text;
}
示例15: toSimpleHtml
QString RichTextLineEdit::toSimpleHtml() const
{
QString html;
for (QTextBlock block = document()->begin(); block.isValid();
block = block.next()) {
for (QTextBlock::iterator i = block.begin(); !i.atEnd();
++i) {
QTextFragment fragment = i.fragment();
if (fragment.isValid()) {
QTextCharFormat format = fragment.charFormat();
QColor color = format.foreground().color();
//QString text = Qt::escape(fragment.text()); //deleted for Qt5
QString text = QString(fragment.text()).toHtmlEscaped(); //added for Qt5
QStringList tags;
if (format.verticalAlignment() ==
QTextCharFormat::AlignSubScript)
tags << "sub";
else if (format.verticalAlignment() ==
QTextCharFormat::AlignSuperScript)
tags << "sup";
if (format.fontItalic())
tags << "i";
if (format.fontWeight() > QFont::Normal)
tags << "b";
if (format.fontStrikeOut())
tags << "s";
while (!tags.isEmpty())
text = QString("<%1>%2</%1>")
.arg(tags.takeFirst()).arg(text);
if (color != QColor(Qt::black))
text = QString("<font color=\"%1\">%2</font>")
.arg(color.name()).arg(text);
html += text;
}
}
}
return html;
}