本文整理汇总了C++中QTextFragment::isValid方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextFragment::isValid方法的具体用法?C++ QTextFragment::isValid怎么用?C++ QTextFragment::isValid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextFragment
的用法示例。
在下文中一共展示了QTextFragment::isValid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: readFragment
void XmlWriter::readFragment(const QTextBlock ¤tBlock,
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: spatiumChanged
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: 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);
}
示例4: 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;
}
示例5: 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();
}
示例6: fileNameFromDiffSpecification
QString MercurialEditor::fileNameFromDiffSpecification(const QTextBlock &diffFileSpec) const
{
const QString filechangeId(QLatin1String("+++ b/"));
QTextBlock::iterator iterator;
for (iterator = diffFileSpec.begin(); !(iterator.atEnd()); iterator++) {
QTextFragment fragment = iterator.fragment();
if(fragment.isValid()) {
if (fragment.text().startsWith(filechangeId)) {
const QString filename = fragment.text().remove(0, filechangeId.size());
return findDiffFile(filename, MercurialPlugin::instance()->versionControl());
}
}
}
return QString();
}
示例7: 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);
}
}
}
示例8: PyBool_FromLong
static PyObject *meth_QTextFragment_isValid(PyObject *sipSelf, PyObject *sipArgs)
{
PyObject *sipParseErr = NULL;
{
QTextFragment *sipCpp;
if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QTextFragment, &sipCpp))
{
bool sipRes;
Py_BEGIN_ALLOW_THREADS
sipRes = sipCpp->isValid();
Py_END_ALLOW_THREADS
return PyBool_FromLong(sipRes);
}
}
示例9: processBlockContent
QString TextDocumentSerializer::processBlockContent(QTextBlock block)
{
QTextBlock::iterator it = block.begin();
QString text;
while(!it.atEnd()){
QTextFragment currentFragment = it.fragment();
if (currentFragment.isValid())
text += processFragment(currentFragment);
++it;
}
if(block.textList())
return QString("<li>%1</li>").arg(text);
else
return QString("%1<br>").arg(text);
}
示例10: 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();
}
}
示例11: 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;
}
示例12: processBlock
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);
}
}
}
示例13: 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;
}
示例14: textDocumentToMinimalHTML
QString Tools::textDocumentToMinimalHTML(QTextDocument* document) {
QString result =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; margin: 0px; }\n"
"</style></head><body>\n";
QFont defaultFont;
for (QTextBlock blockIt = document->begin(); blockIt != document->end(); blockIt = blockIt.next()) {
result += HTM::PAR;
for (QTextBlock::iterator subIt = blockIt.begin(); !(subIt.atEnd()); ++subIt) {
QTextFragment currentFragment = subIt.fragment();
if (currentFragment.isValid()) {
QTextCharFormat charFmt = currentFragment.charFormat();
const QColor& textColor = charFmt.foreground().color();
bool isTextBlack = (textColor == QColor() || textColor == QColor(Qt::black));
if (charFmt.font() == defaultFont && isTextBlack) {
result += Qt::escape(currentFragment.text());
continue;
}
//If we use charFmt.fontWeight, setting a tag overrides it and all characters become non-bold.
//So we use <b> </b> instead
bool bold = (charFmt.fontWeight() >= QFont::Bold);
if (bold)
result += "<b>";
//Compose style string (font and color)
result += "<span style=\"";
if (charFmt.fontFamily() != defaultFont.family() && !charFmt.fontFamily().isEmpty())
result += QString(HTM::FONT_FAMILY).arg(charFmt.fontFamily());
if (charFmt.fontItalic())
result += QString(HTM::FONT_STYLE).arg(HTM::ITALIC);
if (charFmt.fontUnderline())
result += QString(HTM::TEXT_DECORATION).arg(HTM::UNDERLINE);
if (charFmt.fontStrikeOut())
result += QString(HTM::TEXT_DECORATION).arg(HTM::LINE_THROUGH);
/*if (charFmt.fontWeight() != defaultFont.weight()) {
QFont::Weight weight = (charFmt.fontWeight() >= QFont::Bold) ? QFont::Bold : QFont::Normal;
result += QString(HTM::FONT_WEIGHT).arg(weight);
}*/
if (charFmt.fontPointSize() != defaultFont.pointSize() && charFmt.fontPointSize() != 0)
result += QString(HTM::FONT_SIZE).arg(charFmt.fontPointSize());
if (!isTextBlack)
result += QString(HTM::COLOR).arg(textColor.name());
result += "\">" + Qt::escape(currentFragment.text()) + "</span>";
if (bold)
result += "</b>";
}
}
result += HTM::_PAR;
}
result += "</body></html>";
return result;
}
示例15: getPlainText
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;
}